Ejemplo n.º 1
0
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()
Ejemplo n.º 2
0
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()
Ejemplo n.º 3
0
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']
        ))
Ejemplo n.º 4
0
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']))
Ejemplo n.º 5
0
def set_our_shop_codes(arr_orders, arr_shops):
    if not (isinstance(arr_orders, list) and isinstance(arr_shops, tuple) and
            len(arr_orders) > 0 and len(arr_shops) > 0):
        return

    fields = arr_shops[1]
    last_they_shop = -1
    index, shop = None, None
    for j in xrange(0, len(arr_orders)):
        they_shop = arr_orders[j]["theyShop"]
        if last_they_shop != they_shop:
            index, shop = utils.find_by_key(arr_shops, "theyCode", they_shop)
            if index is None or shop is None:
                continue
        arr_orders[j]["shop"] = shop[fields["ourCode"]]
        arr_orders[j]["shopRegion"] = shop[fields["shopRegion"]]
        arr_orders[j]["client"] = shop[fields["kodorg"]]
        arr_orders[j]["manager"] = shop[fields["manager"]]
        if arr_orders[j]["shopRegion"] == 1:
            arr_orders[j]["orderTime"] = 5
        else:
            arr_orders[j]["orderTime"] = 0
        last_they_shop = arr_orders[j]["theyShop"]
Ejemplo n.º 6
0
def set_our_prod_codes(arr_orders, arr_prods):
    if not (isinstance(arr_orders, list) and isinstance(arr_prods, tuple) and
            len(arr_orders) > 0 and len(arr_prods) > 0):
        return

    fields = arr_prods[1]
    last_they_prod = -1
    index, prod = None, None
    for j in xrange(0, len(arr_orders)):
        they_prod = arr_orders[j]["theyProd"]
        if last_they_prod != they_prod:
            index, prod = utils.find_by_key(arr_prods, "theyCode", they_prod)
            if index is None or prod is None:
                continue
        arr_orders[j]["prod"] = prod[fields["ourCode"]]
        arr_orders[j]["koef"] = prod[fields["koef"]]
        arr_orders[j]["packSize"] = float(prod[fields["packSize"]])
        arr_orders[j]["prodWeight"] = float(prod[fields["prodWeight"]])
        arr_orders[j]["uses"] = prod[fields["uses"]]
        arr_orders[j]["exp"] = prod[fields["exp"]]
        arr_orders[j]["codeGroup"] = prod[fields["codeGroup"]].strip()
        arr_orders[j]["prodType"] = prod[fields["prodType"]]
        if arr_orders[j]["packSize"] > 0:
            arr_orders[j]["packValue"] = math.floor(arr_orders[j]["prodValue"] / arr_orders[j]["packSize"])
Ejemplo n.º 7
0
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)