Advertisement
gruntfutuk

prodsales

Apr 17th, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.36 KB | None | 0 0
  1. import datetime
  2. from collections import Counter
  3.  
  4. class Supplier:
  5.     def __init__(self, name, address, email, contact_no):
  6.         self.name = name
  7.         self.address = address
  8.         self.email = email
  9.         self.contact_no = contact_no
  10.  
  11. class Product:
  12.     def __init__(self, name):
  13.         self.name = name
  14.  
  15. class Company():
  16.     data_dict = []
  17.    
  18.     def purchase(self, product_obj, qty, price, date=datetime.date.today()):
  19.         self.data_dict.append({'product': product_obj.name, 'qty': qty, 'price': price, 'date': str(date)})
  20.  
  21.     def sale(self, sell_qty, sell_date=datetime.date.today()):
  22.         a = 0
  23.         p = 0
  24.         unit_val = 0
  25.         new_price = 0
  26.         newdict = (sorted(self.data_dict, key=lambda x: x['date']))
  27.  
  28.         for dt in newdict:
  29.             a += dt['qty']
  30.             p += dt['price']
  31.             if sell_date > dt['date']:
  32.                 if sell_qty <= a:
  33.                     unit_val = float(p / a)
  34.                     new_price = unit_val * a
  35.                     a -= sell_qty
  36.                     self.data_dict.append({'product': product_obj.name, 'qty': a, 'price': new_price, 'date': str(sell_date)})
  37.                     print("sold!")
  38.  
  39.             else:
  40.                 print("Sorry, not enough qty.\n")
  41.                
  42.     def __str__(self):
  43.         report = []
  44.         for entry in self.data_dict:
  45.             report.append(f"{entry['product']:12} | {entry['qty']:4} | {entry['price']:6.2f} | {entry['date']:8}")
  46.         return "\n".join(report)
  47.  
  48.  
  49. company = Company()
  50. product = Product('Computer')
  51.  
  52. MENU = '''
  53.    1. Add stock of the product
  54.    2. Sell stock of the product
  55.    R. Report
  56.    X. eXit
  57.    '''
  58.  
  59. while True:
  60.  
  61.     option = input(MENU).lower()
  62.  
  63.     if option == "1":
  64.         qty = int(input("Enter the qty of the product.\n"))
  65.         price = float(input("Enter the price of the product.\n"))
  66.         purchase_date = input("Enter purchase date.\n")
  67.         company.purchase(product, qty, price, purchase_date)
  68.  
  69.     elif option == "2":
  70.         qty = int(input("Enter the qty you wanna sell, pal!"))
  71.         sale_date = input("Enter sell date.\n")
  72.         company.sale(qty)
  73.        
  74.     elif option == "r":
  75.         print(company)
  76.         1
  77.                          
  78.     elif option == "x":
  79.         break
  80.        
  81.     else:
  82.         print("Sorry. Don't know that option.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement