def statistic_by_day(): page_size = request.args.get('page_size', 25, type=int) page_number = request.args.get('page_number', 1, type=int) Datetime = request.args.get('Datetime') if Datetime is not None: Datetime = datetime.datetime.strptime(Datetime, '%Y-%m-%d') row_bill_detail = BillDetail.query.all() result_bill_detail = [object_as_dict(x) for x in row_bill_detail] row_bills = Bill.query.all() result_bills = [object_as_dict(x) for x in row_bills] buy_today = 0 sell_today = 0 revenue_today = 0 buy_previous = 0 sell_previous = 0 revenue_previous = 0 for x in result_bill_detail: for y in result_bills: if x["BillId"] == y['BillId'] and (y['Datetime'].date() - Datetime.date()).days == 0: if y["Type"] == 0: buy_today += x["Amount"] revenue_today -= x["Amount"] * x["Price"] elif y["Type"] == 1: sell_today += x["Amount"] revenue_today += x["Amount"] * x["Price"] elif x["BillId"] == y['BillId'] and (y['Datetime'].date() - Datetime.date()).days == -1: if y["Type"] == 0: buy_previous += x["Amount"] revenue_previous -= x["Amount"] * x["Price"] elif y["Type"] == 1: sell_previous += x["Amount"] revenue_previous += x["Amount"] * x["Price"] result = { "soMua": buy_today, "soBan": sell_today, "doanhThu": revenue_today, "soMuaTang": buy_today - buy_previous, "soBanTang": sell_today - sell_previous, "doanhThuTang": str(revenue_today / revenue_previous * 100)[0:6] if revenue_previous != 0 else 100 } return get_success(result)
def get_product_by_id(ProductId): try: row = Product.query.get(ProductId) result = object_as_dict(row) return get_success(result) except: return get_fail() return get_fail()
def get_Bill_by_id(BillId): try: row = Bill.query.get(BillId) result = object_as_dict(row) return get_success(result) except: return get_fail() return get_fail()
def search_by_name(): try: page_size = request.args.get('page_size', 25, type=int) page_number = request.args.get('page_number', 1, type=int) ProductName = request.args.get('ProductName', type=str) row = Product.query.filter(Product.ProductName.contains(ProductName.strip())).\ paginate(page=page_number, per_page=page_size).items result = [object_as_dict(x) for x in row] return get_success(result) except: return get_fail() return get_fail()
def statistic_by_month(): try: page_size = request.args.get('page_size', 25, type=int) page_number = request.args.get('page_number', 1, type=int) month = int(request.args.get('month')) year = int(request.args.get("year")) amount_day_of_month = monthrange(year, month)[1] start_day = datetime.datetime.strptime( str(year) + "-" + str(month) + "-" + "1 " + "00:00:01", '%Y-%m-%d %H:%M:%S') revenues = [] for i in range(amount_day_of_month): revenues.append(0) row_bill_detail = BillDetail.query.all() result_bill_detail = [object_as_dict(x) for x in row_bill_detail] row_bills = Bill.query.all() result_bills = [object_as_dict(x) for x in row_bills] for x in result_bill_detail: for y in result_bills: if x["BillId"] == y['BillId'] and 0 <= ( y['Datetime'].date() - start_day.date()).days < amount_day_of_month: if y["Type"] == 0: revenues[y['Datetime'].day - 1] -= x["Amount"] * x["Price"] elif y["Type"] == 1: revenues[y['Datetime'].day - 1] += x["Amount"] * x["Price"] result = [] for i in range(amount_day_of_month): result.append([str(i + 1), revenues[i]]) return get_success(result) except: return get_fail() return get_fail()
def get_all_bill(): page_size = request.args.get('page_size', 25, type=int) page_number = request.args.get('page_number', 1, type=int) try: max_length = len(Bill.query.order_by(Bill.BillId).all()) if max_length - page_size * (page_number-1) < 1: result = [] else: result = [object_as_dict(x) for x in Bill.query.order_by(Bill.BillId). paginate(page=page_number, per_page=page_size).items] return get_success(result) except: return get_fail() return get_fail()
def get_all_product(): day_in_week_str = [ 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' ] page_size = request.args.get('page_size', 25, type=int) page_number = request.args.get('page_number', 1, type=int) # try: max_length = len(Product.query.order_by(Product.ProductId).all()) if max_length - page_size * (page_number - 1) < 1: result = [] else: result = [ object_as_dict(x) for x in Product.query.order_by(Product.ProductId).paginate( page=page_number, per_page=page_size).items ] for item in result: statistic = product_amount_sell_day(item) item['AmountBuyDay'] = statistic['amount_buy_day'] item['AmountSellDay'] = statistic['amount_sell_day'] item['AmountBuyWeek'] = statistic['amount_buy_week'] item['AmountSellWeek'] = statistic['amount_sell_week'] # item['ProductInfor'] = [ # { # 'WeekDay': "Thứ 2", # 'Weather': "Nắng", # 'Temperature': "29", # 'NumberPurchased': 30, # 'NumberSold': 25 # }, # { # 'WeekDay': "Thứ 3", # 'Weather': "Mưa", # 'Temperature': "28", # 'NumberPurchased': 40, # 'NumberSold': 30 # }, # { # 'WeekDay': "Thứ 4", # 'Weather': "Nắng", # 'Temperature': "30", # 'NumberPurchased': 27, # 'NumberSold': 25 # }, # { # 'WeekDay': "Thứ 5", # 'Weather': "Mây", # 'Temperature': "27", # 'NumberPurchased': 46, # 'NumberSold': 37 # }, # { # 'WeekDay': "Thứ 6", # 'Weather': "Bão", # 'Temperature': "25", # 'NumberPurchased': 45, # 'NumberSold': 39 # }, # { # 'WeekDay': "Thứ 7", # 'Weather': "Mưa", # 'Temperature': "26", # 'NumberPurchased': 24, # 'NumberSold': 24 # }, # { # 'WeekDay': "Chủ nhật", # 'Weather': "Mưa", # 'Temperature': "29", # 'NumberPurchased': 35, # 'NumberSold': 29 # } # ] item['ProductInfor'] = product_statistic_one_week(item) for tmp in item['ProductInfor']: tmp['WeekDay'] = day_in_week_str[tmp['WeekDay']] return get_success(result) # except: # return get_fail() return get_fail()
def search(): page_size = request.args.get('page_size', 25, type=int) page_number = request.args.get('page_number', 1, type=int) TotalMoneyStart = request.args.get('TotalMoneyStart', type=str) TotalMoneyEnd = request.args.get('TotalMoneyEnd', type=str) Type = request.args.get("Type", type=int) DatetimeStart = request.args.get('DatetimeStart') if DatetimeStart is not None and DatetimeStart != "": DatetimeStart = datetime.datetime.strptime(DatetimeStart, '%Y-%m-%d') DatetimeEnd = request.args.get('DatetimeEnd') if DatetimeEnd is not None and DatetimeStart != "": DatetimeEnd = datetime.datetime.strptime(DatetimeEnd, '%Y-%m-%d') row = Bill.query.filter(Bill.Type == Type) result = [object_as_dict(x) for x in row] if TotalMoneyStart is not None and TotalMoneyStart != "": TotalMoneyStart = float(TotalMoneyStart) i = 0 while i < len(result): if result[i]['TotalMoney'] < TotalMoneyStart: result.pop(i) else: i += 1 if TotalMoneyEnd is not None and TotalMoneyEnd != "": TotalMoneyEnd = float(TotalMoneyEnd) i = 0 while i < len(result): if result[i]['TotalMoney'] > TotalMoneyEnd: result.pop(i) else: i += 1 if DatetimeStart is not None and DatetimeStart != "": i = 0 while i < len(result): if (result[i]['Datetime'] - DatetimeStart).days < -1: result.pop(i) else: i += 1 if DatetimeEnd is not None and DatetimeStart != "": i = 0 while i < len(result): if (result[i]['Datetime'] - DatetimeEnd).days > 1: result.pop(i) else: i += 1 start_index = page_size * (page_number - 1) end_index = page_size * page_number if start_index >= len(result): result = [] elif end_index > len(result): result = result[start_index: len(result)] else: result = result[start_index: end_index] return get_success(result)