Feat: Sale Fast Food Product by Pizza
This commit is contained in:
151
report.py
Normal file
151
report.py
Normal file
@@ -0,0 +1,151 @@
|
||||
from trytond.model import ModelSQL, ModelView, fields
|
||||
from trytond.pool import Pool
|
||||
from trytond.pyson import If, Eval
|
||||
from sql import Literal, Null
|
||||
from sql.aggregate import Min, Sum
|
||||
from sql.functions import CurrentTimestamp
|
||||
from trytond.transaction import Transaction
|
||||
import datetime
|
||||
|
||||
|
||||
class ReportSaleContext(ModelView):
|
||||
"""Contexto de reportes de miembros de familia"""
|
||||
__name__ = 'sale_fast_food.report.context'
|
||||
|
||||
from_date = fields.Date(
|
||||
"From Date",
|
||||
domain=[
|
||||
If(Eval('to_date') & Eval('from_date'),
|
||||
('from_date', '<=', Eval('to_date')),
|
||||
()),
|
||||
If(Eval('from_date'),
|
||||
('from_date', '<=', datetime.date.today()))
|
||||
])
|
||||
|
||||
to_date = fields.Date(
|
||||
"To Date",
|
||||
domain=[
|
||||
If(Eval('from_date') & Eval('to_date'),
|
||||
('to_date', '>=', Eval('from_date')),
|
||||
()),
|
||||
If(Eval('to_date'),
|
||||
('to_date', '<=', datetime.date.today()))
|
||||
])
|
||||
|
||||
|
||||
class ReportSaleAbstract(ModelSQL):
|
||||
|
||||
@classmethod
|
||||
def table_query(cls):
|
||||
from_item, tables = cls._joins()
|
||||
|
||||
final_query = from_item.select(
|
||||
*cls._columns(tables),
|
||||
where=cls._where(tables),
|
||||
group_by=cls._group_by(tables)
|
||||
)
|
||||
|
||||
return final_query
|
||||
|
||||
@classmethod
|
||||
def _joins(cls):
|
||||
pool = Pool()
|
||||
tables = {}
|
||||
|
||||
Sale = pool.get('sale.sale')
|
||||
tables['sale.sale'] = sale = Sale.__table__()
|
||||
|
||||
SaleLine = pool.get('sale.line')
|
||||
tables['sale.line'] = sale_line = SaleLine.__table__()
|
||||
|
||||
ProductTemplate = pool.get('product.template')
|
||||
tables[
|
||||
'product.template'
|
||||
] = product_template = ProductTemplate.__table__()
|
||||
|
||||
ProductProduct = pool.get('product.product')
|
||||
tables['product.product'] = product = ProductProduct.__table__()
|
||||
|
||||
from_item = sale_line.left_join(
|
||||
product,
|
||||
condition=product.id == sale_line.product
|
||||
).left_join(
|
||||
product_template,
|
||||
condition=product.template == product_template.id
|
||||
).left_join(
|
||||
sale,
|
||||
condition=sale.id == sale_line.sale
|
||||
)
|
||||
|
||||
return from_item, tables
|
||||
|
||||
@classmethod
|
||||
def _columns(cls, tables):
|
||||
columns = [
|
||||
cls._column_id(tables).as_('id'),
|
||||
Literal(0).as_('create_uid'),
|
||||
CurrentTimestamp().as_('create_date'),
|
||||
cls.write_uid.sql_cast(Literal(Null)).as_('write_uid'),
|
||||
cls.write_date.sql_cast(Literal(Null)).as_('write_date'),
|
||||
]
|
||||
|
||||
return columns
|
||||
|
||||
@classmethod
|
||||
def _where(cls, tables):
|
||||
context = Transaction().context
|
||||
where = Literal(True)
|
||||
|
||||
from_date = context.get('from_date')
|
||||
to_date = context.get('to_date')
|
||||
|
||||
sale = tables['sale.sale']
|
||||
if from_date:
|
||||
where &= sale.sale_date >= from_date
|
||||
|
||||
if to_date:
|
||||
where &= sale.sale_date <= to_date
|
||||
|
||||
return where
|
||||
|
||||
@classmethod
|
||||
def _group_by(cls, tables):
|
||||
raise NotImplementedError()
|
||||
|
||||
@classmethod
|
||||
def _column_id(cls, tables):
|
||||
sale_line = tables['sale.line']
|
||||
return Min(sale_line.id)
|
||||
|
||||
|
||||
class ReportSaleProduct(ReportSaleAbstract, ModelView):
|
||||
"""Report Sale Group by Product"""
|
||||
__name__ = 'sale_fast_food.reporting.product'
|
||||
|
||||
product_pizza = fields.Many2One('product.product', "Product")
|
||||
quantity = fields.Float("Quantity")
|
||||
|
||||
@classmethod
|
||||
def _columns(cls, tables):
|
||||
sale_line = tables['sale.line']
|
||||
|
||||
return super(ReportSaleProduct, cls)._columns(tables) + [
|
||||
sale_line.product.as_('product_pizza'),
|
||||
Sum(sale_line.quantity).as_('quantity')
|
||||
]
|
||||
|
||||
@classmethod
|
||||
def _group_by(cls, tables):
|
||||
sale_line = tables['sale.line']
|
||||
|
||||
return [
|
||||
sale_line.product
|
||||
]
|
||||
|
||||
@classmethod
|
||||
def _where(cls, tables):
|
||||
where = super(ReportSaleProduct, cls)._where(tables)
|
||||
product_template = tables['product.template']
|
||||
where &= product_template.pizza == Literal(True)
|
||||
|
||||
return where
|
||||
Reference in New Issue
Block a user