trytonpsk-dash_sale/sale.py

166 lines
4.8 KiB
Python

# 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 __setup__(cls):
super(Sale, cls).__setup__()
@classmethod
def dash_quote(cls, args, ctx):
Shop = Pool().get('sale.shop')
Product = Pool().get('product.product')
shop_id = ctx['shop']
shop = Shop(shop_id)
for v in args['lines']:
del v['id']
del v['amount']
v['type'] = 'line'
product = Product(v['product'])
v['unit'] = product.template.default_uom.id
print(args['lines'])
to_create = {
'shop': shop_id,
'invoice_type': 'P',
'company': shop.company.id,
'party': args['party']['id'],
'sale_date': args['sale_date'],
'shipment_address': args['shipment_address']['id'],
'invoice_address': args['shipment_address']['id'],
'agent': args['agent']['id'],
'payment_term': shop.payment_term.id,
'state': 'draft',
'lines': [('create', args['lines'])],
}
sale, = cls.create([to_create])
cls.quote([sale])
res = {
'record': {'id': sale.id, 'number': sale.number},
'msg': 'successful_order',
'type': 'success',
'open_modal': True,
}
return res
@classmethod
def report_sales_day(cls, args, ctx):
pass
@classmethod
def _get_sales_in_period(cls, period, currency_id, in_thousands=False):
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])
if in_thousands:
value = int(value / 1000)
return value
@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
report = args['report']
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}
description = currency.code
value = cls._get_sales_in_period(period, ctx['currency'],
report.in_thousands)
res = {
'value': value,
'description': description,
'selector': selector,
'default_option': period.id,
'meta': period.name,
'in_thousands': report.in_thousands
}
return res
@classmethod
def report_sales_by_month(cls, args, ctx):
pool = Pool()
Fiscalyear = pool.get('account.fiscalyear')
Currency = pool.get('currency.currency')
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 = []
report = args['report']
for p in periods:
val = cls._get_sales_in_period(p, ctx['currency'], report.in_thousands)
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': report.in_thousands
}
return res