From 77da594ac8af62edfef7cdc5e9973176443def55 Mon Sep 17 00:00:00 2001 From: Oscar Alvarez Date: Wed, 8 Apr 2020 15:23:43 -0500 Subject: [PATCH] Separate method --- sale.py | 132 +++++++++++++++++++++++--------------------------------- 1 file changed, 55 insertions(+), 77 deletions(-) diff --git a/sale.py b/sale.py index d09e0e5..f812ca2 100644 --- a/sale.py +++ b/sale.py @@ -18,8 +18,30 @@ class Sale(metaclass=PoolMeta): pass @classmethod - def _get_sales_in_period(cls, period): - pass + def _get_sales_in_period(cls, period, currency_id): + invoice = Table('account_invoice') + start_date = period.start_date + end_date = period.end_date + + cursor = Transaction().connection.cursor() + select = invoice.select( + Sum(invoice.untaxed_amount_cache), + limit=1 + ) + select.where = ( + invoice.type == 'out') & ( + invoice.invoice_date >= start_date) & ( + invoice.currency == currency_id) & ( + invoice.invoice_date <= end_date) & ( + invoice.state.in_(['posted', 'paid']) + ) + + cursor.execute(*select) + values = cursor.fetchone() + value = 0 + if values and values[0]: + value = int(values[0]) + return value @classmethod def report_sales_month(cls, args, ctx): @@ -42,33 +64,12 @@ class Sale(metaclass=PoolMeta): ('type', '=', 'standard') ]) currency = Currency(ctx['currency']) - invoice = Table('account_invoice') - start_date = period.start_date - end_date = period.end_date - cursor = Transaction().connection.cursor() - select = invoice.select( - Sum(invoice.untaxed_amount_cache), - limit=1 - ) - select.where = ( - invoice.type == 'out') & ( - invoice.invoice_date >= start_date) & ( - invoice.currency == ctx['currency']) & ( - invoice.invoice_date <= end_date) & ( - invoice.state.in_(['posted', 'paid']) - ) - - cursor.execute(*select) - values = cursor.fetchone() selector = {p.id: p.name for p in selector_periods} - value = 0 - description = '' + description = currency.code + value = cls._get_sales_in_period(period, ctx['currency']) - if values: - value = int(values[0]) - description = currency.code res = { 'value': value, 'description': description, @@ -83,58 +84,35 @@ class Sale(metaclass=PoolMeta): @classmethod def report_sales_by_month(cls, args, ctx): pool = Pool() - Period = pool.get('account.period') + Fiscalyear = pool.get('account.fiscalyear') Currency = pool.get('currency.currency') - res = {} - # today = date.today() - # periods = Period.search([ - # ('start_date', '<=', today), - # ('end_date', '>=', today), - # ('type', '=', 'standard') - # ]) - # if not periods: - # return res - # - # period = periods[0] - # selector_periods = Period.search([ - # ('fiscalyear', '=', period.fiscalyear.id), - # ('type', '=', 'standard') - # ]) - # currency = Currency(ctx['currency']) - # invoice = Table('account_invoice') - # start_date = period.start_date - # end_date = period.end_date - # - # cursor = Transaction().connection.cursor() - # select = invoice.select( - # Sum(invoice.untaxed_amount_cache), - # group_by=invoice. - # ) - # select.where = ( - # invoice.type == 'out') & ( - # invoice.invoice_date >= start_date) & ( - # invoice.currency == ctx['currency']) & ( - # invoice.invoice_date <= end_date) & ( - # invoice.state.in_(['posted', 'paid']) - # ) - # - # cursor.execute(*select) - # values = cursor.fetchone() - # selector = {p.id: p.name for p in selector_periods} - # - # value = 0 - # description = '' - # - # if values: - # value = int(values[0]) - # description = currency.code - # res = { - # 'value': value, - # 'description': description, - # 'selector': selector, - # 'default_option': period.id, - # 'meta': period.name, - # 'in_thousands': True - # } + + today = date.today() + fiscalyears = Fiscalyear.search([ + ('start_date', '<=', today), + ('end_date', '>=', today), + ]) + if not fiscalyears: + return {} + + periods = [p for p in fiscalyears[0].periods if p.type == 'standard'] + + values = [] + labels = [] + for p in periods: + val = cls._get_sales_in_period(p, ctx['currency']) + if val > 0: + values.append(val) + labels.append(p.name) + + currency = Currency(ctx['currency']) + + res = { + 'values': values, + 'labels': labels, + 'description': 'report_sales_by_month', + 'meta': currency.code, + 'in_thousands': True + } return res