def inventory(): inv = InventoryModel.query.all() if request.method == 'POST': name = request.form['name'] type = request.form['type'] buying_price = request.form['buying_price'] selling_price = request.form['selling_price'] stock = request.form['stock'] serial_number = request.form['serial_number'] reorder_point = request.form['reorder_point'] # # print(name) # print(type) # print(buying_price) # print(selling_price) # print(stock) # print(serial_number) # print(reorder_point) inventory = InventoryModel(name=name, type=type, buying_price=buying_price, selling_price=selling_price, stock=stock, serial_number=serial_number, reorder_point=reorder_point) inventory.create_record() return redirect(url_for('inventory')) return render_template('inventory.html', x=inv)
def get(cls, role: str): # role_json = request.get_json() # role = role_json["role"] print(role) try: if role == USER_ROLES["worker"]: data = inventory_worker_schema.dump(InventoryModel.get_worker_inventory()) editable = {} return {"data": data, "editable": editable}, 200 if role == USER_ROLES["supervisor"]: data = inventory_supervisor_schema.dump(InventoryModel.get_supervisor_inventory()) editable = { "category": category_schema.dump(CategoryModel.get_all()) } return {"data": data, "editable": editable}, 200 if role == USER_ROLES["statistics"]: data = inventory_statistics_schema.dump(InventoryModel.get_all()) categories = category_schema.dump(CategoryModel.get_all()) status = INVENTORY_STATUS return { "data": data, "editable": { "category": categories, "inventoryStatus": status } }, 200 except: return {"message": "error"}, 400
def new_roomtype_coops(self, merchant_id, hotel_id, roomtype_ids): hotel = CooperateHotelModel.get_by_id(self.db, hotel_id) if not hotel: raise JsonException(1000, 'hotel not found') if hotel.merchant_id != merchant_id: raise JsonException(2000, 'merchant not valid') coops = CooperateRoomTypeModel.get_by_merchant_hotel_base_rooms_id(self.db, merchant_id, hotel_id, roomtype_ids) if coops: raise JsonException(1000, 'room has cooped') coops = CooperateRoomTypeModel.new_roomtype_coops(self.db, merchant_id, hotel.id, hotel.base_hotel_id, roomtype_ids, commit=False) for coop in coops: InventoryModel.insert_in_months(self.db, merchant_id, hotel_id, coop.id, hotel.base_hotel_id, coop.base_roomtype_id, 13, commit=False) r = yield HotelPusher(self.db).push_hotel_by_id(hotel_id) if not r: raise JsonException(3000, 'push hotel to stock fail') for coop in coops: r = yield InventoryAsyncPusher(self.db).push_by_roomtype(coop) if not r: raise JsonException(3001, 'push inventory to stock fail') r = yield POIRoomTypePusher(self.db).push_roomtype(coop.id) if not r: raise JsonException(3001, 'push roomtype to poi fail') self.db.commit() raise gen.Return(coops)
def delete(cls, inventory_id: int): inventory = InventoryModel.get_by_id(inventory_id) if inventory: inventory.delete_from_db() return {"message": "OK"}, 200 else: return {"message": "No inventory with that id"}, 404
def get(self, hotel_id): today = datetime.today() year = self.get_query_argument('year', today.year) month = self.get_query_argument('month', today.month) year, month = int(year), int(month) simple = self.get_query_argument('simple', 0) hotel = self.get_hotel(hotel_id) base_hotel, base_roomtypes = yield self.get_all_roomtype(hotel.base_hotel_id) cooped_roomtypes = self.get_cooped_rooms(hotel_id) base_cooped, base_will_coop = self.seperate_roomtype(base_roomtypes, cooped_roomtypes) self.valid_date(year, month) if not simple: inventorys = InventoryModel.get_by_merchant_id_and_hotel_id_and_date(self.db, self.current_user.merchant_id, hotel_id, year, month) self.merge_inventory(base_cooped, inventorys) self.merge_cooped_info(base_cooped, cooped_roomtypes) self.finish_json(result=dict( hotel=base_hotel, cooped_roomtypes=base_cooped, will_coop_roomtypes=base_will_coop, ))
def modify_inventory(self, merchant_id, hotel_id, roomtype_id, price_type, change_num, start_date, end_date): session = self.session stay_days = get_stay_days(start_date, end_date) year_months = [(day.year, day.month) for day in stay_days] year_months = {}.fromkeys(year_months).keys() inventories = InventoryModel.get_by_merchant_hotel_roomtype_dates( session, merchant_id, hotel_id, roomtype_id, year_months) for day in stay_days: inventory = get_inventory_by_date(inventories, day.year, day.month) if not inventory: continue if change_num != 0: inventory.add_val_by_day(day.day, price_type, change_num) else: inventory.set_val_by_day(day.day, price_type, change_num) r = InventoryPusher(self.session).push_by_roomtype_id(roomtype_id) if r: session.commit() return inventories else: session.rollback() return None
def get(self, product): products = InventoryModel.find_by_product(product) if products: return { 'product': [product.json() for product in products] }, 200 else: return {'message': 'Product not found!'}, 404
def post(cls): data = request.get_json() inventory = InventoryModel( code=data["code"], name=data["name"], description=data["description"], image=data["image"], price=data["price"], category=data["category"], inventoryStatus=data["inventoryStatus"], rating=data["rating"], role=data["role"] ) try: inventory.save_to_db() return {"message": "OK"}, 200 except: return {"message": "error"}, 400
def push_by_roomtype(self, roomtype): start_day = datetime.date.today() days = [start_day + datetime.timedelta(days=i) for i in xrange(90)] days = [(day.year, day.month) for day in days] days = {}.fromkeys(days).keys() inventories = InventoryModel.get_by_roomtype_and_dates(self.db, roomtype.id, days) r = yield self.post_inventory(inventories, roomtype.is_online) raise gen.Return(r)
def addInventory(): name = request.form['name'] type = request.form['type'] buying_price = request.form['bp'] selling_price = request.form['sp'] stock = request.form['stock'] serial_number = request.form['sn'] # print(name) # print(type) # print(buying_price) # print(selling_price) # print(stock) # print(serial_number) inv =InventoryModel(name=name,type=type,buying_price=buying_price,selling_price=selling_price,stock=stock,serial_number=serial_number) inv.insert_to_db() return render_template('inventory.html')
def view_sales(id): inve = InventoryModel.get_inventory_by_id(id) #print(inve.sale) #print(type(inve)) sale_of_product = inve.sale return render_template('sales.html', s_o_p=sale_of_product)
def push_by_roomtype(self, roomtype): start_day = datetime.date.today() days = [start_day + datetime.timedelta(days=i) for i in xrange(90)] days = [(day.year, day.month) for day in days] days = {}.fromkeys(days).keys() inventories = InventoryModel.get_by_roomtype_and_dates( self.db, roomtype.id, days) r = yield self.post_inventory(inventories, roomtype.is_online) raise gen.Return(r)
def sales(): s = SalesModel.query.all() if request.method == 'POST': quantity = request.form['quantity'] inventory_id = request.form['inventory_id'] # print(quantity) # print(inventory_id) sa = SalesModel(quantity=quantity, inventory_id=int(inventory_id)) sa.create_record() InventoryModel.update_stock(int(inventory_id), int(quantity)) print('sale suceesfully made') return redirect(url_for('inventory')) return redirect(url_for('inventory'))
def inventories(): inventories = InventoryModel.fetch_all() if request.method == 'POST': name = request.form['name'] inventory_type = request.form['inventory_type'] buying_price = request.form['buying_price'] selling_price = request.form['selling_price'] record = InventoryModel(name=name, inventory_type=inventory_type, buying_price=buying_price, selling_price=selling_price) record.create_record() flash("Record has been successifully created", "success") return redirect(url_for('inventories')) return render_template('inventories.html', all_inventories=inventories)
def post(self, product): product = InventoryModel.find_by_product(product) if product: return {'message': 'Product already in database!'} else: parser = reqparse.RequestParser() parser.add_argument('product', type=str, required=True, help='This field is mandatory!') parser.add_argument('price', type=float, required=True, help='This field is mandatory!') data_payload = parser.parse_args() InventoryModel.add_product(data_payload['product'], data_payload['price']) return {'message': 'Product successfully added to database!'}, 201
def put(cls, inventory_id: int): inventory_json = request.get_json() inventory = InventoryModel.get_by_id(inventory_id) print("[before save]", inventory) if inventory: inventory = inventory_schema.load(inventory_json) print("[save]", inventory) inventory.save_to_db() return inventory_schema.dump(inventory), 200 else: return {"message": "No inventory with this id"}, 404
def push_by_roomtype_id(self, roomtype_id): roomtype = CooperateRoomTypeModel.get_by_id(self.db, roomtype_id) if not roomtype: return True start_day = datetime.date.today() days = [start_day + datetime.timedelta(days=i) for i in xrange(90)] days = [(day.year, day.month) for day in days] days = {}.fromkeys(days).keys() inventories = InventoryModel.get_by_roomtype_and_dates(self.db, roomtype_id, days) return self.post_inventory(inventories, roomtype.is_online)
def push_by_roomtype_id(self, roomtype_id): roomtype = CooperateRoomTypeModel.get_by_id(self.db, roomtype_id) if not roomtype: return True start_day = datetime.date.today() days = [start_day + datetime.timedelta(days=i) for i in xrange(90)] days = [(day.year, day.month) for day in days] days = {}.fromkeys(days).keys() inventories = InventoryModel.get_by_roomtype_and_dates( self.db, roomtype_id, days) return self.post_inventory(inventories, roomtype.is_online)
def inventories(): inventories = InventoryModel.fetch_all() print(inventories) if request.method == 'POST': name = request.form['name'] type = request.form['type'] buying_price = request.form['bp'] sp = request.form['sp'] # insert record into the databases # cur.execute('INSERT INTO inventories (name, type, bp, sp) VALUES (%s,%s,%s,%s)', (name,type,buying_price,sp)) # conn.commit() record = InventoryModel(name=name, type=type, bp=buying_price, sp=sp) db.session.add(record) db.session.commit() print("record has successfully been created") return redirect(url_for('inventories')) return render_template('inventories.html', all_inventories=inventories)
def push_inventory(self, roomtype_id): self.log.info("<< push inventory (roomtype {})>>".format(roomtype_id)) from models.inventory import InventoryModel from models.cooperate_roomtype import CooperateRoomTypeModel roomtype = CooperateRoomTypeModel.get_by_id(self.session, roomtype_id) start_day = datetime.date.today() days = [start_day + datetime.timedelta(days=i) for i in xrange(90)] days = [(day.year, day.month) for day in days] days = {}.fromkeys(days).keys() inventories = InventoryModel.get_by_roomtype_and_dates(self.session, roomtype_id, days) self.post_inventory(inventories, roomtype.is_online)
def push_inventory(self, roomtype_id): self.log.info("<< push inventory (roomtype {})>>".format(roomtype_id)) from models.inventory import InventoryModel from models.cooperate_roomtype import CooperateRoomTypeModel roomtype = CooperateRoomTypeModel.get_by_id(self.session, roomtype_id) start_day = datetime.date.today() days = [start_day + datetime.timedelta(days=i) for i in xrange(90)] days = [(day.year, day.month) for day in days] days = {}.fromkeys(days).keys() inventories = InventoryModel.get_by_roomtype_and_dates( self.session, roomtype_id, days) self.post_inventory(inventories, roomtype.is_online)
def dashboard(): total_inventories = len(InventoryModel.fetch_all()) total_products = len( InventoryModel.query.filter_by(inventory_type="product").all()) total_services = len( InventoryModel.query.filter_by(inventory_type="service").all()) pie_chart = pygal.Pie() pie_chart.title = 'Products vs Services Inventory' pie_chart.add('Products', total_products) pie_chart.add('Services', total_services) pie = pie_chart.render_data_uri() return render_template('dashboard.html', total_inventories=total_inventories, total_products=total_products, total_services=total_services, chart=pie)
def editInventory(inventory_id): if request.method == 'POST': name = request.form['name'] inventory_type = request.form['inventory_type'] buying_price = request.form['buying_price'] selling_price = request.form['selling_price'] if InventoryModel.update_inventory(inventory_id=inventory_id, name=name, inventory_type=inventory_type, buying_price=buying_price, selling_price=selling_price): flash("Record has been successifully updated", "success") return redirect(url_for('inventories')) else: flash("Error occurred while editing record", "success") return redirect(url_for('inventories'))
def valid_inventory(session, order): Log.info('# valid inventory for order {}'.format(order.id)) stay_days = get_stay_days(order.checkin_date, order.checkout_date) year_months = [(day.year, day.month) for day in stay_days] year_months = {}.fromkeys(year_months).keys() inventories = InventoryModel.get_by_merchant_hotel_roomtype_dates( session, order.merchant_id, order.hotel_id, order.roomtype_id, year_months) if not inventories: Log.info("no inventory") return False for inventory in inventories: Log.info(inventory.todict()) for day in stay_days: inventory = None month = combin_year_month(day.year, day.month) Log.info('...finding {}'.format(month)) for _inventory in inventories: if _inventory.month == month: inventory = _inventory break else: Log.info('day {} not found'.format(day)) return False if inventory.get_day(day.day) < order.room_quantity: Log.info('room not enough in {}'.format(day)) return False else: Log.info('found') return True
def valid_inventory(self, order): Log.info('# valid inventory for order {}'.format(order.id)) stay_days = self.get_stay_days(order.checkin_date, order.checkout_date) year_months = [(day.year, day.month) for day in stay_days] year_months = {}.fromkeys(year_months).keys() inventories = InventoryModel.get_by_merchant_hotel_roomtype_dates( self.db, order.merchant_id, order.hotel_id, order.roomtype_id, year_months) if not inventories: Log.info("no inventory") return False for inventory in inventories: Log.info(inventory.todict()) for day in stay_days: inventory = None month = self.combin_year_month(day.year, day.month) Log.info('...finding {}'.format(month)) for _inventory in inventories: if _inventory.month == month: inventory = _inventory break else: Log.info('day {} not found'.format(day)) return False if inventory.get_day(day.day) < order.room_quantity: Log.info('room not enough in {}'.format(day)) return False else: Log.info('found') return True
def view_sales(id): # inventories = InventoryModel.fetch_all() this_inventory = InventoryModel.get_inventory_by_id(id) sales = this_inventory.sales return render_template('view_sales.html',sales=sales)
def inventory(): inve = InventoryModel.fetch_all() for each in inve: print(each.name) return render_template('inventory.html',inventory=inve)
def complete_in_four_months(self): roomtypes = CooperateRoomTypeModel.get_all(self.session) InventoryModel.insert_all_in_months(self.session, roomtypes, 13)
def _clear_inventoris_by_roomtype(self, roomtype): InventoryModel.delete_by_roomtype_id(self.db, roomtype.id, commit=False)
def get(self): products = InventoryModel.find_all_products(); if products: return {'products': [product.json() for product in products]}, 200 else: return {'message': 'No products found!'}, 404
def inventory_id(inventory_id): print(InventoryModel.fetch_inventory_by_id(inventory_id).stock) return "Hello world"