def show_prod_range_as_table(self):
     """Show Shop's Product Range as a table, using Pandas.
        Shows dictionary input as a table."""
     prod_rge_dict1 = SystemState().get_import_data('product_range.csv', [])
     df = pd.DataFrame(prod_rge_dict1,
                       columns=['prod_name', 'prod_group', 'footpr_fct'])
     df.columns = ['Product name', 'Product Group', 'Footprint weight']
     print('\n', df)
 def date_req_to_report_date(self, date_request):
     """Transforms string-date-request in (list of) datetime.date dates.
        Expects: 'now', 'yesterday' or 'date YYYY-MM-DD'."""
     try:
         now = SystemState().today
         if date_request == 'now':
             req_report_date = now
         elif date_request == 'yesterday':
             req_report_date = now + datetime.timedelta(-1)
         else:                               # YYYY-MM-DD format
             if len(date_request) == 15:
                 proper_date = SystemState().proper_date(date_request[5:])
                 req_report_date = proper_date
         return req_report_date
     except UnboundLocalError:
         print("""Please provide a proper date for this type of report.
               As reporting date 'today' was taken.""")
 def get_revenue(self):
     """Returns sum_total of sell_prices within requested period."""
     report_period = self.date_req_to_report_period(self.date_str)
     table = self.inventory_list
     if type(report_period) != list:
         prices = [x for x in table if (x.sell_price
                                        and x.sell_date == report_period)]
         sells_in_period = [x for x in table
                            if (x.sell_date == report_period)]
     else:
         prices = [x for x in table if (x.sell_price
                                        and x.sell_date in report_period)]
         sells_in_period = [x for x in table
                            if (x.sell_date in report_period)]
     # stuff to export
     export_sells = input("Export the sellings for this period? y/n \n")
     if export_sells == 'y':
         sells_to_export = []
         for prod in sells_in_period:
             dict_to_append = {'Product name': prod.name,
                               'Sell date': prod.sell_date,
                               'Sell price': prod.sell_price,
                               'Discount factor': prod.discount_factor}
             sells_to_export.append(dict_to_append)
         if self.date_str == 'now':
             rep_date_str = SystemState().today
         elif self.date_str == 'yesterday':
             rep_date_str = SystemState().today + datetime.timedelta(-1)
         else:
             rep_date_str = self.date_str[5:]
         output_file = f"{rep_date_str}-sold.csv"
         exp_fields = ['Product name', 'Sell date', 'Sell price',
                       'Discount factor']
         Report().export_rep_to_csv(exp_fields,
                                    output_file, sells_to_export)
         print("Sellings for this period are exported to ", output_file)
     # end stuff to export
     revenue_raw = sum([float(x.sell_price)
                        for x in prices if float(x.sell_price)])
     revenue = '{:.2f}'.format(revenue_raw)
     return revenue
 def date_req_to_report_period(self, date_request):
     """Transforms string-date-request in (list of) datetime.date dates.
        Expects either a time-range 'date YYYY-MM',
        or a specific day: 'now', 'yesterday', 'date YYYY-MM-DD'.
        """
     try:
         now = SystemState().today
         if date_request == 'now':
             report_period = now
         if date_request == 'yesterday':
             report_period = now + datetime.timedelta(-1)
         if date_request[0:4] == 'date':
             if len(date_request) == 12:         # YYYY-MM format
                 d_obj = self.get_days_from_periode(date_request[5:])
                 date_list = [d for d in d_obj]
                 report_period = date_list
             else:                               # YYYY-MM-DD format
                 if len(date_request) == 15:
                     prop_date = SystemState().proper_date(date_request[5:])
                     report_period = prop_date
         return report_period
     except UnboundLocalError:
         print("Please provide a proper date for the report.")
 def report_time_and_result_template(self, date_request):
     """Makes a report string template, depending of input datestring. Input format
        isone of <'now', 'yesterday, 'date YYYY-MM', 'date YYYY-MM-DD'>."""
     rep_type = self.type
     if date_request == 'now':
         self.rep_string = str(f"Today's {rep_type} so far: ")
     if date_request == 'yesterday':
         self.rep_string = str(f"Yesterday's {rep_type} was: ")
     if date_request[0:4] == 'date':
         if len(date_request) == 12:         # YYYY-MM format
             self.year = date_request[5:9]
             if len(date_request) > 10:
                 self.month = date_request[10:]
             datetime_object = datetime.datetime.strptime(self.month, "%m")
             full_month_name = datetime_object.strftime("%B")
             msg = str(f"In {full_month_name} {self.year} {rep_type} is: ")
             self.rep_string = msg
         else:                               # YYYY-MM-DD format
             if len(date_request) == 15:
                 date = SystemState().proper_date(date_request[5:])
                 self.rep_string = str(f'On {date} the {rep_type} is: ')
     return self.rep_string
 def get_fd_report(self):
     """Assembles the data for the different reports."""
     if self.type == 'product_range':
         report_object = self.product_range
         Report().show_product_range_csv()         # option1 from csv
         # Report().show_prod_range_as_table()     # option2 from dicionary
     if self.type == 'inventory':
         today = SystemState().today
         not_sold = [x for x in self.inventory_list if x.sold is False]
         # check if already bought on reportdate
         was_in = [x for x in not_sold if x.buy_date <= self.report_date]
         if self.report_date != today:
             print("Today is ", today)
             print("Report date is", self.report_date)
             print("Only today's stock report is exported to inventory.csv")
         in_stock = [x for x in was_in if x.expired is False]
         stock_report = []
         if in_stock != []:
             for prod in in_stock:
                 counter = 0
                 for x in in_stock:
                     if x.name == prod.name:
                         counter += 1
                 dict_to_append = {'Product name': prod.name,
                                   'Count': f'1 of {counter}',
                                   'Buy price': prod.buy_price,
                                   'Expiration date': prod.exp_date}
                 stock_report.append(dict_to_append)
         report_object = stock_report
         fields = ['Product name', 'Count', 'Buy price', 'Expiration date']
         # avoid export of non-actual inventory reports
         if self.report_date == today:
             Report().export_rep_to_csv(fields, 'inventory.csv',
                                        stock_report)
         Report().show_inventory_csv()
     return report_object
Beispiel #7
0
import sys
import argparse
# from class_product import Product
from class_product import SystemState
from class_product import ShopAdmin
# from class_product import Product
from class_reports import Report
from class_reports import Time_range_report
from class_reports import Fixed_date_report

system_state = SystemState()
shop = ShopAdmin()
shop.inventory_list = system_state.instantiate_products([])
report = Report()
"""
cli_tool.py :
- initialises main classes
- handles command-line-tool input.
"""

# gets product_choices from file for easy maintenance of product_range
product_range = system_state.get_import_data('product_range.csv', [])
product_choices = [p.get('prod_name') for p in product_range]


# Functions to get from user commands to function calls
def report_call(args):
    arg_dict = vars(args)
    rep_type = arg_dict['report_type']
    inv_list = shop.inventory_list
    if rep_type in ['inventory', 'product_range']: