Example #1
0
def get_item_sales_data():
    date_data = get_date_from_request(request)
    id_item = request.args(0)

    raw_data = db((db.sale.id_bag == db.bag.id)
                  & (db.bag_item.id_bag == db.bag.id)
                  & (db.bag_item.id_item == id_item)
                  & (db.sale.id_store.belongs(date_data.stores))
                  & (db.sale.created_on >= date_data.start_date)
                  & (db.sale.created_on < date_data.end_date)).iterselect(
                      db.bag_item.quantity,
                      db.sale.created_on,
                      orderby=db.sale.created_on)

    groups = analysis_utils.group_items(raw_data, date_data.start_date,
                                        date_data.end_date,
                                        date_data.time_step,
                                        lambda r: r.sale.created_on)
    rr = analysis_utils.reduce_groups(
        groups,
        dict(qty_total=dict(func=lambda r: float(r.bag_item.quantity))))
    data = rr['qty_total']['results']
    total_sales = DQ(sum(rr['qty_total']['results']), True, True)
    chart_data = chart_data_template_for(
        date_data.time_mode,
        [dataset_format(T('Sold quantity'), data, ACCENT_COLOR)],
        date_data.start_date)

    return dict(chart_data=chart_data,
                current_date=date_data.start_date,
                next_date=date_data.next_date,
                prev_date=date_data.prev_date,
                title=date_data.title,
                total_sales=total_sales)
def get_item_sales_data():
    date_data = get_date_from_request(request)
    id_item = request.args(0)


    raw_data = db(
        (db.sale.id_bag == db.bag.id) &
        (db.bag_item.id_bag == db.bag.id) &
        (db.bag_item.id_item == id_item) &
        (db.sale.id_store.belongs(date_data.stores)) &
        (db.sale.created_on >= date_data.start_date) &
        (db.sale.created_on < date_data.end_date)
    ).iterselect(
        db.bag_item.quantity, db.sale.created_on, orderby=db.sale.created_on
    )

    groups = analysis_utils.group_items(
        raw_data, date_data.start_date, date_data.end_date, date_data.time_step,
        lambda r : r.sale.created_on
    )
    rr = analysis_utils.reduce_groups(groups, dict(
        qty_total=dict(func=lambda r : float(r.bag_item.quantity))
    ))
    data = rr['qty_total']['results']
    total_sales = DQ(sum(rr['qty_total']['results']), True, True)
    chart_data = chart_data_template_for(
        date_data.time_mode,
        [dataset_format(T('Sold quantity'), data, ACCENT_COLOR)],
        date_data.start_date
    )

    return dict(
        chart_data=chart_data,
        current_date=date_data.start_date,
        next_date=date_data.next_date,
        prev_date=date_data.prev_date,
        title=date_data.title,

        total_sales=total_sales
    )
def get_day_sales_data():
    """ Returns relevant data for the specified day
        args: [year, month, day]
    """

    date_data = get_date_from_request(request)

    wallet_opt = get_wallet_payment_opt()

    sales_total = 0
    datasets = []
    for store_id in date_data.stores:
        data = db(
            (db.payment.id_sale == db.sale.id) &
            (db.sale.id_store == store_id) &
            (db.payment.id_payment_opt != wallet_opt.id) &
            (db.payment.created_on >= date_data.start_date) &
            (db.payment.created_on < date_data.end_date)
        ).iterselect(db.payment.ALL, orderby=db.sale.created_on)
        payments_groups = analysis_utils.group_items(
            data, date_data.start_date, date_data.end_date,
            date_data.time_step
        )

        rr = analysis_utils.reduce_groups(
            payments_groups, dict(
                payments_total=dict(
                    func=lambda r : float(r.amount - r.change_amount)
                )
            )
        )

        store_color = similar_color(ACCENT_COLOR, store_id * 100)

        sales_data = rr['payments_total']['results']
        dataset = dataset_format(
            T("Store %s") % store_id, sales_data, store_color
        )
        dataset['store_id'] = store_id
        datasets.append(dataset)
        sales_total += DQ(sum(sales_data), True)


    chart_sales_data = chart_data_template_for(
        date_data.time_mode, datasets, date_data.start_date
    )

    # important averages
    avg_sale_total = db.sale.total.avg()
    avg_sale_volume = db.sale.quantity.avg()
    total_items_sold = db.sale.quantity.sum()
    sales_data = db(
        (db.sale.id_store.belongs(date_data.stores)) &
        (db.sale.is_done == True) &
        (db.sale.created_on >= date_data.start_date) &
        (db.sale.created_on < date_data.end_date)
    ).select(avg_sale_total, avg_sale_volume, total_items_sold).first()
    avg_sale_volume = DQ(sales_data[avg_sale_volume] or 0, True)
    avg_sale_total = DQ(sales_data[avg_sale_total] or 0, True)
    # average items sell price
    avg_item_price = DQ(sales_total / (sales_data[total_items_sold] or 1), True)

    total_items_sold = DQ(sales_data[total_items_sold] or 0, True)

    expenses = db.bag_item.total_buy_price.sum()
    expenses = db(
        (db.sale.id_bag == db.bag.id) &
        (db.bag_item.id_bag == db.bag.id) &
        (db.sale.id_store.belongs(date_data.stores)) &
        (db.sale.created_on >= date_data.start_date) &
        (db.sale.created_on < date_data.end_date)
    ).select(expenses).first()[expenses]
    expenses = DQ(expenses or 0, True)

    profit = sales_total - expenses

    return dict(
        chart_data=chart_sales_data,
        current_date=date_data.start_date,
        next_date=date_data.next_date,
        prev_date=date_data.prev_date,
        title=date_data.title,

        avg_sale_total=avg_sale_total,
        avg_sale_volume=avg_sale_volume,
        total_items_sold=total_items_sold,
        avg_item_price=avg_item_price,
        sales_total=sales_total,
        profit=profit
    )
Example #4
0
def get_day_sales_data():
    """ Returns relevant data for the specified day
        args: [year, month, day] 
    """

    date_data = get_date_from_request(request)

    wallet_opt = get_wallet_payment_opt()

    sales_total = 0
    datasets = []
    for store_id in date_data.stores:
        data = db((db.payment.id_sale == db.sale.id)
                  & (db.sale.id_store == store_id)
                  & (db.payment.id_payment_opt != wallet_opt.id)
                  & (db.payment.created_on >= date_data.start_date)
                  & (db.payment.created_on < date_data.end_date)).iterselect(
                      db.payment.ALL, orderby=db.sale.created_on)
        payments_groups = analysis_utils.group_items(data,
                                                     date_data.start_date,
                                                     date_data.end_date,
                                                     date_data.time_step)

        rr = analysis_utils.reduce_groups(
            payments_groups,
            dict(payments_total=dict(
                func=lambda r: float(r.amount - r.change_amount))))

        store_color = similar_color(ACCENT_COLOR, store_id * 100)

        sales_data = rr['payments_total']['results']
        dataset = dataset_format(
            T("Store %s") % store_id, sales_data, store_color)
        dataset['store_id'] = store_id
        datasets.append(dataset)
        sales_total += DQ(sum(sales_data), True)

    chart_sales_data = chart_data_template_for(date_data.time_mode, datasets,
                                               date_data.start_date)

    # important averages
    avg_sale_total = db.sale.total.avg()
    avg_sale_volume = db.sale.quantity.avg()
    total_items_sold = db.sale.quantity.sum()
    sales_data = db((db.sale.id_store.belongs(date_data.stores))
                    & (db.sale.is_done == True)
                    & (db.sale.created_on >= date_data.start_date)
                    & (db.sale.created_on < date_data.end_date)).select(
                        avg_sale_total, avg_sale_volume,
                        total_items_sold).first()
    avg_sale_volume = DQ(sales_data[avg_sale_volume] or 0, True)
    avg_sale_total = DQ(sales_data[avg_sale_total] or 0, True)
    # average items sell price
    avg_item_price = DQ(sales_total / (sales_data[total_items_sold] or 1),
                        True)

    total_items_sold = DQ(sales_data[total_items_sold] or 0, True)

    expenses = db.bag_item.total_buy_price.sum()
    expenses = db((db.sale.id_bag == db.bag.id)
                  & (db.bag_item.id_bag == db.bag.id)
                  & (db.sale.id_store.belongs(date_data.stores))
                  & (db.sale.created_on >= date_data.start_date)
                  & (db.sale.created_on < date_data.end_date)).select(
                      expenses).first()[expenses]
    expenses = DQ(expenses or 0, True)

    profit = sales_total - expenses

    return dict(chart_data=chart_sales_data,
                current_date=date_data.start_date,
                next_date=date_data.next_date,
                prev_date=date_data.prev_date,
                title=date_data.title,
                avg_sale_total=avg_sale_total,
                avg_sale_volume=avg_sale_volume,
                total_items_sold=total_items_sold,
                avg_item_price=avg_item_price,
                sales_total=sales_total,
                profit=profit)