def __init__(self, args): self.args = args self.database_bought = Database(config.BOUGHT_FILE, config.BOUGHT_FIELDS) self.database_sold = Database(config.SOLD_FILE, config.SOLD_FIELDS) self.database_products = Database(config.PRODUCTS_FILE, config.PRODUCTS_FIELDS) # parse --now, --today and --yesterday today = Today().get_date() today = datetime.strptime(today, config.DATE_FORMAT) if self.args['yesterday'] == True: today = Today().get_date() today = datetime.strptime(today, config.DATE_FORMAT) today = today + timedelta(days=-1) self.today = today # --export-format self.export = self.args['export_format']
def main(): Today().get_date() arguments = Arguments() router = Router(arguments.vars) router.route()
def __init__(self, args): self.args = args self.database_bought = Database(config.BOUGHT_FILE, config.BOUGHT_FIELDS) self.database_sold = Database(config.SOLD_FILE, config.SOLD_FIELDS) # parse --now, --today and --yesterday today = Today().get_date() today = datetime.strptime(today, config.DATE_FORMAT) if self.args['yesterday'] == True: today = Today().get_date() today = datetime.strptime(today, config.DATE_FORMAT) today = today + timedelta(days=-1) self.today = today # parse --date <date> self.date_format = None self.date_start = None self.date_end = None self.date = self.args['date'] if self.args['date'] != None: if (len(self.date) == 10): # yyyy-mm-dd self.date_format = config.REPORT_DATE_FORMAT self.date_start = convert_to_date(self.date) self.date_end = convert_to_date(self.date) elif len(self.date) == 7: # yyyy-mm self.date_format = config.REPORT_YEAR_MONTH_FORMAT self.date_start = convert_to_date(self.date) self.date_end = convert_to_date(self.date) self.date_end = last_day_of_month(self.date_end) elif len(self.date) == 4: # yyyy self.date_format = config.REPORT_YEAR_FORMAT self.date_start = convert_to_date(self.date) self.date_end = convert_to_date(self.date) self.date_end = last_day_of_year(self.date_end) if self.date_start == None: raise ValueError('We need a valid date or date range')
def run(self): bought_id = self.get_bought_id() if bought_id == None: return 'ERROR: Product not in stock' self.database_sold.add({ 'id': self.database_sold.rowcount + 1, 'bought_id': bought_id, 'sell_date': Today().get_date(), 'sell_price': self.args['price'], }) return 'OK'
def route(self): # buy if self.action == 'buy': response = Buy(self.args).run() # sell if self.action == 'sell': response = Sell(self.args).run() # report if self.action == 'report': if self.report == 'inventory': response = Inventory(self.args).run() if self.report == 'revenue': response = Revenue(self.args).run() if self.report == 'profit': response = Profit(self.args).run() # export if self.action == 'export': response = Export(self.args).run() # --advance-time if self.args['advance_time'] != None: response = Today(self.args).run() # return response if response != '': print(response) # return error (1) if response[:5] == 'ERROR': sys.exit(1) sys.exit(0)
def run(self): # check id product exists in product database product = filter_list( self.database_products.data, 'product_name', [self.product_name]) barcode = '' if len(product) == 0: # generate a new barcode barcode = Barcode(config.STORE_BARCODE_PREFIX) # add product to products database self.database_products.add({ 'product_name': self.product_name, 'full_name': self.product_name.title(), 'ean13': barcode, }) else: # use preexisting barcode barcode = product[0]['ean13'] # add buy to bought database self.database_bought.add( { 'id': self.database_bought.rowcount + 1, 'product_name': self.product_name, 'buy_date': Today().get_date(), 'buy_price': self.args['price'], 'expiration_date': self.args['expiration_date'], 'ean13': barcode }) return 'OK'
def __init__(self, args): self.args = args # --database self.database_name = args['database'] if self.database_name == 'bought': self.database = Database(config.BOUGHT_FILE, config.BOUGHT_FIELDS) elif self.database_name == 'sold': self.database = Database(config.SOLD_FILE, config.SOLD_FIELDS) elif self.database_name == 'products': self.database = Database(config.PRODUCTS_FILE, config.PRODUCTS_FIELDS) # parse --now, --today and --yesterday today = Today().get_date() today = datetime.strptime(today, config.DATE_FORMAT) if self.args['yesterday'] == True: today = Today().get_date() today = datetime.strptime(today, config.DATE_FORMAT) today = today + timedelta(days=-1) self.today = today # parse --date <date> self.date_start = None self.date_end = None self.date = self.args['date'] if self.date != None: if (len(self.date) == 10): # yyyy-mm-dd self.date_start = convert_to_date(self.date) self.date_end = convert_to_date(self.date) elif len(self.date) == 7: # yyyy-mm self.date_start = convert_to_date(self.date) self.date_end = convert_to_date(self.date) self.date_end = last_day_of_month(self.date_end) elif len(self.date) == 4: # yyyy self.date_start = convert_to_date(self.date) self.date_end = convert_to_date(self.date) self.date_end = last_day_of_year(self.date_end) if self.date_start == None: raise ValueError('We need a valid date or date range') # --export-format self.export = self.args['export_format'] # parse filter type self.filter = None if self.args['now'] == True or \ self.args['today'] == True or \ self.args['yesterday'] == True: self.filter = 'date' elif self.date != None: self.filter = 'range'
def run(self): inventory = [] sold_today = filter_list_by_date(self.database_sold.data, 'sell_date', self.today) bought_today = filter_list_by_date(self.database_bought.data, 'buy_date', self.today) for item in bought_today: is_sold = filter_list(sold_today, 'bought_id', [item['id']]) if len(is_sold) == 0: # check if product_name is already in inventory list in_report = filter_list(inventory, 'product_name', [item['product_name']]) # check if product_name and buy_price is already in inventory list in_report = filter_list(in_report, 'buy_price', [item['buy_price']]) # check if product_name, buy_price and expiration_date is already in inventory list in_report = filter_list(in_report, 'expiration_date', [item['expiration_date']]) if len(in_report) > 0: # increase count if product is indeed in the list index = inventory.index(in_report[0]) inventory[index]['count'] = inventory[index]['count'] + 1 else: # add to inventory list inventory.append({ 'product_name': item['product_name'], 'count': 1, 'buy_price': item['buy_price'], 'expiration_date': item['expiration_date'], }) if len(inventory) == 0: return 'ERROR: Inventory is empty' inventory = sort_list(inventory, 'product_name') report = [] for item in inventory: # get full product name from products database product = filter_list(self.database_products.data, 'product_name', [item['product_name']]) report.append({ 'Product Name': product[0]['full_name'] if len(product) != 0 else item['product_name'], 'Count': item['count'], 'Buy Price': '€ {:,.2f}'.format(float(item['buy_price'])), 'Sum Price': '€ {:,.2f}'.format( int(item['count']) * float(item['buy_price'])), 'Expiration Date': format_date(item['expiration_date']), 'Expired': 'Yes' if Today().get_date() > format_date(item['expiration_date']) else 'No', 'EAN13': product[0]['ean13'] if len(product) != 0 else '', }) if self.export == 'csv': filename = make_filename('report_inventory_', '.csv') report_csv(filename, config.INVENTORY_REPORT_FIELDS, report) elif self.export == 'xlsx': filename = make_filename('report_inventory_', '.xlsx') report_xlsx(filename, config.INVENTORY_REPORT_FIELDS, report) elif self.export == 'json': filename = make_filename('report_inventory_', '.json') report_json(filename, report) return tabulate(report, headers='keys', tablefmt='fancy_grid', colalign=('left', 'right', 'right', 'right', 'right', 'left'))