def create_or_load_item_monthly_sales_profit_mat(client_id): # now = datetime.datetime.now() # year = now.year year = get_current_or_max_year(client_id) cur_sale_name = f'{year}_sales_mat' prev_sale_name = f'{year-1}_sales_mat' cur_profit_name = f'{year}_profit_mat' prev_profit_name = f'{year-1}_profit_mat' if (not does_collection_exist(cur_sale_name, client_id) or not does_collection_exist(prev_sale_name, client_id)): df = get_item_wise_sales_data(client_id, year) print("In item_analyis load data complete") item2idx, idx2item, code2name = load_item_dict(client_id) curr_df, prev_df = df[df['year'] == year], df[df['year'] == year - 1] create_collection_item_sales(cur_sale_name, curr_df, item2idx, client_id) create_collection_item_sales(prev_sale_name, prev_df, item2idx, client_id) print(f'{prev_sale_name}') create_collection_item_profit(cur_profit_name, curr_df, item2idx, client_id) create_collection_item_profit(prev_profit_name, prev_df, item2idx, client_id) print(f'{prev_profit_name}') else: item2idx, idx2item, code2name = load_item_dict(client_id) return item2idx, idx2item, code2name
def process_item_analysis(client_id): try: create_or_load_item_monthly_sales_profit_mat(client_id) except Exception as e: logging.error( f"while creating itemwise_sales_profit_mat for {client_id} in item_analysis{e}" ) try: _, _, _ = load_item_dict(client_id) except Exception as e: logging.error( f'while creating pickle code2name, idx2code for {client_id}in item_analysis {e}' ) logging.info( f'pickle for code2name, idx2code for {client_id} in item_analysis exists' ) # now = datetime.datetime.now() # year = now.year year = get_current_or_max_year(client_id) cur_sale_name = f'{year}_sales_mat' prev_sale_name = f'{year-1}_sales_mat' cur_profit_name = f'{year}_profit_mat' prev_profit_name = f'{year-1}_profit_mat' data_names = [ cur_sale_name, prev_sale_name, cur_profit_name, prev_profit_name ] collection_exist = True for data_name in data_names: if does_collection_exist(data_name, client_id): collection_exist = collection_exist and True return collection_exist
def load_n_process_salesdetail_data(client_id): sales_details = sales_table.fetch_all_by_client(client_id) # from database #sales_details = read_dataframe_from_pickle(client_id, data_name='sales') # from pickle sales_details['Datetimestamp'] = pd.to_datetime( sales_details['Datetimestamp']) sales_details['item_len'] = sales_details['Item_code'].apply( lambda x: len(x)) sales_details = sales_details[sales_details['item_len'] > 2].copy() # itemcode2name = {} # for code, name in zip(sales_details.Item_code, sales_details.Item_name): # itemcode2name[code] = name #itemname2code[name] = code item2idx, idx2item, code2name = load_item_dict(client_id) return sales_details, code2name
def process_abc_analysis(client_id): try: get_saleswise_abc_data(client_id) except Exception as e: logging.error( f"while creating sales_abc_df for {client_id} in abc_analysis{e}") try: get_profitwise_abc_data(client_id) except Exception as e: logging.error( f"while creating profit_abc for {client_id} in abc_analysis{e}") try: _, _, _ = load_item_dict(client_id) except Exception as e: logging.error( f'while creating pickle code2name, idx2code for {client_id}in abc_analysis {e}' ) return (does_collection_exist('sales_abc', client_id) and does_collection_exist('profit_abc', client_id))
def get_saleswise_abc_data(client_id): data_name = 'sales_abc' if not does_collection_exist(data_name, client_id): #df, code2name = load_n_process_salesdetail_data(client_id) df = load_n_process_salesdetail_data(client_id) item2idx, idx2item, code2name = load_item_dict(client_id) itemwise_sales = df.groupby(['Item_code'], as_index=False). \ agg({'Unitsale_price':np.average, 'Sale_quantity': np.sum}) itemwise_sales['totalsales'] = itemwise_sales[ 'Unitsale_price'] * itemwise_sales['Sale_quantity'] itemwise_sales = itemwise_sales.sort_values(by='totalsales', ascending=False) itemwise_sales['cumulativesales'] = itemwise_sales[ 'totalsales'].cumsum() itemwise_sales['%sales'] = (itemwise_sales['cumulativesales'] / itemwise_sales['totalsales'].sum()) itemwise_sales['ABC_category'] = itemwise_sales['%sales']. \ apply(lambda x: 'A' if x < 0.71 else 'B' if (x >= 0.71 and x < 0.91) else 'C') itemwise_sales.insert( 1, 'Item_name', itemwise_sales['Item_code'].apply(lambda x: code2name[x])) create_collection_and_insert_datafrme(data_name, itemwise_sales, client_id) logging.info( f'created sales_abc Mongo collection for {client_id}in abc analysis' ) else: itemwise_sales = get_dataframe_collection(data_name, client_id) logging.info( f'sales_abc Mongo collection for {client_id} in abc_analysis exists' ) return itemwise_sales