trytonpsk-dash_sale/sale.py

124 lines
3.5 KiB
Python
Raw Normal View History

2020-04-08 18:23:00 +02:00
# This file is part of purchase_discount module for Tryton.
# The COPYRIGHT file at the top level of this repository contains
# the full copyright notices and license terms.
from sql import Table
from sql.aggregate import Sum
from datetime import date
from trytond.pool import Pool, PoolMeta
from trytond.transaction import Transaction
__all__ = ['Sale']
class Sale(metaclass=PoolMeta):
__name__ = 'sale.sale'
@classmethod
def report_sales_day(cls, args, ctx):
pass
@classmethod
2020-04-10 19:14:27 +02:00
def _get_sales_in_period(cls, period, currency_id, in_thousands=False):
2020-04-08 22:23:43 +02:00
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])
2020-04-10 19:14:27 +02:00
if in_thousands:
value = int(value / 1000)
2020-04-08 22:23:43 +02:00
return value
2020-04-08 18:23:00 +02:00
@classmethod
def report_sales_month(cls, args, ctx):
pool = Pool()
Period = pool.get('account.period')
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
2020-04-10 19:14:27 +02:00
report = args['report']
2020-04-08 18:23:00 +02:00
period = periods[0]
selector_periods = Period.search([
('fiscalyear', '=', period.fiscalyear.id),
('type', '=', 'standard')
])
currency = Currency(ctx['currency'])
selector = {p.id: p.name for p in selector_periods}
2020-04-08 22:23:43 +02:00
description = currency.code
2020-04-10 19:14:27 +02:00
value = cls._get_sales_in_period(period, ctx['currency'],
report.in_thousands)
2020-04-08 18:23:00 +02:00
res = {
'value': value,
'description': description,
'selector': selector,
'default_option': period.id,
'meta': period.name,
2020-04-10 19:14:27 +02:00
'in_thousands': report.in_thousands
2020-04-08 18:23:00 +02:00
}
return res
@classmethod
def report_sales_by_month(cls, args, ctx):
pool = Pool()
2020-04-08 22:23:43 +02:00
Fiscalyear = pool.get('account.fiscalyear')
2020-04-08 18:23:00 +02:00
Currency = pool.get('currency.currency')
2020-04-08 22:23:43 +02:00
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 = []
2020-04-10 19:14:27 +02:00
report = args['report']
2020-04-08 22:23:43 +02:00
for p in periods:
2020-04-10 19:14:27 +02:00
val = cls._get_sales_in_period(p, ctx['currency'], report.in_thousands)
2020-04-08 22:23:43 +02:00
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,
2020-04-10 19:14:27 +02:00
'in_thousands': report.in_thousands
2020-04-08 22:23:43 +02:00
}
2020-04-08 18:23:00 +02:00
return res