def employees_with_most_sales(): print('--- Show employees with most items sold ---\n') employees = get_employees() sales = get_sales() products_by_employee = {} for e in employees: products_by_employee[e['id']] = 0 for s in sales: employee_id = s['employee_id'] products_by_employee[employee_id] += s['num_products'] top_employees = [] for i in range(3): top_employee = {"employee_id": -1, "products": -1} for k in products_by_employee: total_products = products_by_employee[k] if total_products > top_employee['products']: top_employee = {"employee_id": k, "products": total_products} top_employees.append(top_employee) del products_by_employee[top_employee['employee_id']] print('Top 3 employees:') place = 0 for el in top_employees: place += 1 employee = find_by_key(employees, 'id', el['employee_id']) print("%s) %s %s with %s items sold" % (place, employee['name'], employee['last_name'], el['products'])) print()
def employees_with_most_sales(): print('--- En çok satılan ürünle çalışanları göster ---\n') employees = get_employees() sales = get_sales() products_by_employee = {} for e in employees: products_by_employee[e['id']] = 0 for s in sales: employee_id = s['employee_id'] products_by_employee[employee_id] += s['num_products'] top_employees = [] for i in range(3): top_employee = {"employee_id": -1, "products": -1} for k in products_by_employee: total_products = products_by_employee[k] if total_products > top_employee['products']: top_employee = {"employee_id": k, "products": total_products} top_employees.append(top_employee) del products_by_employee[top_employee['employee_id']] print('En iyi 3 Calisan:') place = 0 for el in top_employees: place += 1 employee = find_by_key(employees, 'id', el['employee_id']) print("%s) %s %s ile %s satilan urunler" % (place, employee['name'], employee['last_name'], el['products'])) print()
def register_sale(): sale = { "date": today() } print("--- Satış kaydı ---\n") print("Urunu kim satıyor?") employees = get_employees() for e in employees: print("- %s (%s)" % (e['name'], e['id'])) print() employee = select_by_id_or_name(employees, 'employee') sale['employee_id'] = employee['id'] print("Selected: %s (%s)\n" % (employee['name'], employee['id'])) print("Bu hangi ürün?") products = get_products() for p in products: print("- (%s) %s (%s adet satilabilir.)" % (p['id'], p['name'], p['quantity'])) print() product = select_by_id_or_name(products, 'product') sale['product_id'] = product['id'] print("Secilen: %s (%s) (%s stokta var)\n" % (product['name'], product['id'], (product['quantity']))) quantity = 0 while True: quantity = safe_input("int_positive", "Kaç urun var? ") if quantity > 0 and quantity <= product['quantity']: print("Emir geçerlidir. Toplam fiyat hesaplanıyor...") break else: print( "Sipariş geçersiz. Lütfen stoktaki miktardan büyük olmayan bir sayı seçin") # we are updating the reference, so this dictionary is also modified # on the products list product['quantity'] -= quantity sale['num_products'] = quantity sale['total_price'] = quantity * product['price'] print("\nToplam fiyat: %sTL (+ %sTL vergi)" % (sale['total_price'], sale['total_price'] * 0.18)) sale['id'] = len(get_sales()) print("\nBu siparişin numarasi", sale['id']) update_products(products) add_sale(sale)
def register_sale(): sale = {"date": today()} print("--- Register sale ---\n") print("Who is selling the product?") employees = get_employees() for e in employees: print("- %s (%s)" % (e['name'], e['id'])) print() employee = select_by_id_or_name(employees, 'employee') sale['employee_id'] = employee['id'] print("Selected: %s (%s)\n" % (employee['name'], employee['id'])) print("Which product is it?") products = get_products() for p in products: print("- %s (%s) (%s in stock)" % (p['name'], p['id'], p['quantity'])) print() product = select_by_id_or_name(products, 'product') sale['product_id'] = product['id'] print("Selected: %s (%s) (%s in stock)\n" % (product['name'], product['id'], (product['quantity']))) quantity = 0 while True: quantity = safe_input("int_positive", "How many items? ") if quantity > 0 and quantity <= product['quantity']: print("The order is valid. Calculating total price...") break else: print( "The order is invalid. Please choose a number that is not greater than the quantity in stock" ) # we are updating the reference, so this dictionary is also modified # on the products list product['quantity'] -= quantity sale['num_products'] = quantity sale['total_price'] = quantity * product['price'] print("\nTotal price: $%s (+ $%s tax)" % (sale['total_price'], sale['total_price'] * 0.16)) sale['id'] = len(get_sales()) print("\nThis order's id is", sale['id']) update_products(products) add_sale(sale)
def most_sold_items(): """ Get the top 3 most sold items- """ print("--- Most sold items ---\n") print("Top 3 most sold product until now:") products = get_products() sales_file = get_sales() products_sold = {} # add initial values for product in products: products_sold[product["id"]] = 0 # update the product quantities for sold_item in sales_file: product_id = sold_item["product_id"] products_sold[product_id] += sold_item["num_products"] top_products = [] # top 3 for _i in range(3): top_product = {"product_id": -1, "products": -1} for k in products_sold: total = products_sold[k] # update the curent top value if total > top_product['products']: top_product = { "product_id": k, "products": total } # add to the podium top_products.append(top_product) # delete from list to calculate other placees del products_sold[top_product['product_id']] place = 0 for el in top_products: place += 1 product = find_by_key(products, 'id', el['product_id']) print(" %s) %s with %s units sold" % ( place, product['name'], el['products'] ))
def most_sold_items(): print("--- En çok satılan ürünler ---") print("Şimdiye kadar en çok satılan 3 ürün:") products = get_products() sales_file = get_sales() products_sold = {} for sold_items in sales_file: products_sold[sold_items["num_products"]] = 0 for product in products: product_id = product["product_id"] products_sold[product_id] += product["num_products"] print(product)
def most_sold_items(): print("--- Most sold items ---") print("Top 3 most sold product until now:") products = get_products() sales_file = get_sales() products_sold = {} for sold_items in sales_file: products_sold[sold_items["num_products"]] = 0 for product in products: product_id = product["product_id"] products_sold[product_id] += product["num_products"] print(product)
def employees_with_most_sales(): """ Get the top 3 employees with most sales. Functionality is similar to most_sold_items() """ print('--- Show employees with most items sold ---\n') employees = get_employees() sales = get_sales() products_by_employee = {} # fill the dictionary with initial values for e in employees: products_by_employee[e['id']] = 0 # add the number of products sold by each employee for s in sales: employee_id = s['employee_id'] products_by_employee[employee_id] += s['num_products'] top_employees = [] # get the top 3 for i in range(3): top_employee = {"employee_id": -1, "products": -1} for k in products_by_employee: total_products = products_by_employee[k] # if the current employee has more sales, replace top_employee if total_products > top_employee['products']: top_employee = {"employee_id": k, "products": total_products} # add to podium top_employees.append(top_employee) # delete from list to calculate other placees del products_by_employee[top_employee['employee_id']] print('Top 3 employees:') place = 0 for el in top_employees: place += 1 employee = find_by_key(employees, 'id', el['employee_id']) print("%s) %s %s with %s items sold" % (place, employee['name'], employee['last_name'], el['products']))
def customer_satisfaction_form(): """ Ask a customer for their rating. Writes a review to feedback.csv """ feedback = {"date": today()} print("--- Customer satisfaction form ---\n") print('Which is your sale id (it is found on your receipt)?') total_sales = len(get_sales()) sale_id = 0 while True: sale_id = safe_input('int_positive', 'Sale id: ') # check if id exists if sale_id > -1 and sale_id < total_sales: break else: print('That sale id is invalid') print('\nHow was our service? (1, 2, 3, 4, 5)') rating = 0 while True: rating = safe_input('int_positive', 'Rating: ') if rating > 0 and rating < 6: break else: print('Please select a number from 1 to 5') feedback['sale_id'] = sale_id feedback['rating'] = rating # last available id feedback['id'] = len(get_feedbacks()) add_feedback(feedback) print('\nCool. Thanks for giving us your feedback.') print('We hope to see you again')
def generate_report(): """ Generate a report for an employee This will write to a .txt file Sample file: ``` EMPLOYYEE NAME - POSITION | ID | NAME | QUANTITY | | ---- | ------------------- | -------- | | ... | ... | ... | | ... | ... | ... | ``` """ print("--- Generate employee's sales report ---") print("\nSelect an employee:") employees = get_employees() products = get_products() sales = get_sales() # display employees to the user for e in employees: print("- %s (%s)" % (e['name'], e['id'])) print() employee = select_by_id_or_name(employees, 'employee') print("Creating file...") product_quantities = {} # will be used for the output file largest_name = 0 # add initial values for product in products: product_quantities[product['id']] = 0 length = len(product['name']) if length > largest_name: largest_name = length # update the products sold by the employee for sale in sales: if sale['employee_id'] == employee['id']: product_quantities[sale['product_id']] += sale['num_products'] filename = "sales_report_%s_%s_%s_%s.txt" % ( employee['name'], employee['last_name'], # change to valid filename format today().replace('/', '-'), # apply some randomness to the filename randint(100, 999)) file = open(filename, 'w') file.write('%s %s - %s \n\n' % (employee['name'], employee['last_name'], employee['position'].title())) file.write('| ID | NAME' + (' ' * (largest_name - 4)) + ' | QUANTITY |\n') file.write('| ---- | ' + ('-' * largest_name) + ' | -------- |\n') for k in product_quantities: product = find_by_key(products, 'id', k) line = '| %s | %s | %s |\n' % (str(k).ljust(4), product['name'].ljust(largest_name), str(product_quantities[k]).ljust(8)) file.write(line) file.close() print('File saved as "%s"' % filename)