'Creating daily sales reports from multiple input csvs with Python

I am writing a simulated sales report for a international outdoorsware company using multiple csv inputs. The output would be two reports:

  • An end of day inventory report, that calculates any change in quantity and value of each product a group member is selling or buying since the start of the business day (start.csv + changes from daily sales.csv). A group member can have a negative inventory/value. The output will be a csv named EoD_Sales_Report.csv with headers ProductId,MemberId,Quantity,TodaysPrice,Value

  • A daily profit/loss report that calculates each group's daily profit or loss in the product that they are buy/selling . The output should be a csv named ProfLoss.csv with headers GroupName,ProductName,ProfitLoss

I want to do this using only the standard library, but I am stuck on implementing the most efficient way to associate each sale by member/group to do the proper calculations required for the reports.

The input csvs are as follows teams.csv

MemberId,MemberName,GroupName
42,Carl,Ravens
43,Dave,Ravens
51,Ashley,Bengals
52,Steve,Bengals
64,Elizabeth,Bears
65,Max,Bears

products.csv

ProductId,ProductName,Brand,Denomination,Currency
32,Tent,COLEMAN,1.0,USD 
44,CookwareSet,LECREUSET,1.0,USD
92,PortableStove,CADAC,1.0,USD
82,ChefsKnives,DIAMONT,1.0,GBP 
20,CampingChair,Coleman,1.0,EUR

start.csv (beginning of business day inventory)

ProductId,MemberId,Quantity,YesterdaysPrice,Value
32,42,3,295.25,885.75
92,65,5,120.30,601.5
44,43,-5,417.97,-2089.85
32,51,2,95.25,190.5

sales.csv, -1 indicates selling, 1 indicates buying

MemberId,ProductId,BuyOrSell,Quantity,Price
42,32,-1,2,301.50
42,32,1,1,300.00
43,92,1,3,125.75
43,82,1,2,53.38
51,44,1,1,425.25
65,82,1,5,53.38
64,44,1,6,425.25
51,32,-1,4,301.50
43,92,-1,3,127.15
64,32,1,8,300.00
52,20,1,2,15.25
52,20,-1,1,17.25

The most important section of the code where this logic should be implemented is below.

import argparse
import csv 

def create_sales_reports(products,start_inventory,groups,sales):
    # Assume argparse.FileType is handling opening and writing the input/output file handles
    product_reader = csv.reader(instruments)
    for p in product_reader:
        product_id = int(p[0])
        product_symbol = str(p[1])
        brand = str(p[2])
        denomination = float(p[3])
        currency = str(p[4])

    start_reader = csv.reader(start_inventory)
    for s in start_reader:
        sod_product_id = int(s[0])
        sod_member_id = int(s[1])
        sod_quantity = int(s[2])
        sod_yesterdays_price = float(i[3])
        sod_value = float(s[4])

    group_reader = csv.reader(accounts)
    for m in group_reader:
        member_id = int(a[0])
        member_name = str(a[1])
        team_name = str(a[2])


    sales_reader = csv.reader(trades)
    for sale in sales_reader:
        sale_member_id = int(sale[0])
        trade_instrument_id = int(sale[1])
        trade_side = int(sale[2])
        trade_quantity = int(sale[3])
        trade_price = float(sale[4])



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source