Separate method

This commit is contained in:
Oscar Alvarez 2020-04-08 15:23:43 -05:00
parent 7c3619e05c
commit 77da594ac8
1 changed files with 55 additions and 77 deletions

132
sale.py
View File

@ -18,8 +18,30 @@ class Sale(metaclass=PoolMeta):
pass pass
@classmethod @classmethod
def _get_sales_in_period(cls, period): def _get_sales_in_period(cls, period, currency_id):
pass 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 @classmethod
def report_sales_month(cls, args, ctx): def report_sales_month(cls, args, ctx):
@ -42,33 +64,12 @@ class Sale(metaclass=PoolMeta):
('type', '=', 'standard') ('type', '=', 'standard')
]) ])
currency = Currency(ctx['currency']) 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} selector = {p.id: p.name for p in selector_periods}
value = 0 description = currency.code
description = '' value = cls._get_sales_in_period(period, ctx['currency'])
if values:
value = int(values[0])
description = currency.code
res = { res = {
'value': value, 'value': value,
'description': description, 'description': description,
@ -83,58 +84,35 @@ class Sale(metaclass=PoolMeta):
@classmethod @classmethod
def report_sales_by_month(cls, args, ctx): def report_sales_by_month(cls, args, ctx):
pool = Pool() pool = Pool()
Period = pool.get('account.period') Fiscalyear = pool.get('account.fiscalyear')
Currency = pool.get('currency.currency') Currency = pool.get('currency.currency')
res = {}
# today = date.today() today = date.today()
# periods = Period.search([ fiscalyears = Fiscalyear.search([
# ('start_date', '<=', today), ('start_date', '<=', today),
# ('end_date', '>=', today), ('end_date', '>=', today),
# ('type', '=', 'standard') ])
# ]) if not fiscalyears:
# if not periods: return {}
# return res
# periods = [p for p in fiscalyears[0].periods if p.type == 'standard']
# period = periods[0]
# selector_periods = Period.search([ values = []
# ('fiscalyear', '=', period.fiscalyear.id), labels = []
# ('type', '=', 'standard') for p in periods:
# ]) val = cls._get_sales_in_period(p, ctx['currency'])
# currency = Currency(ctx['currency']) if val > 0:
# invoice = Table('account_invoice') values.append(val)
# start_date = period.start_date labels.append(p.name)
# end_date = period.end_date
# currency = Currency(ctx['currency'])
# cursor = Transaction().connection.cursor()
# select = invoice.select( res = {
# Sum(invoice.untaxed_amount_cache), 'values': values,
# group_by=invoice. 'labels': labels,
# ) 'description': 'report_sales_by_month',
# select.where = ( 'meta': currency.code,
# invoice.type == 'out') & ( 'in_thousands': True
# 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
# }
return res return res