281 lines
8.0 KiB
Python
281 lines
8.0 KiB
Python
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, Count
|
|
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
|
|
|
|
|
|
class ReportSaleByUser(ReportSaleAbstract, ModelView):
|
|
"""Report Sale Group by User"""
|
|
__name__ = 'sale_fast_food.reporting.by_user'
|
|
|
|
user = fields.Many2One('res.user', "User")
|
|
completed_sales = fields.Integer("Completed Sales")
|
|
untaxed_amount = fields.Numeric(
|
|
"Untaxed Amount", digits='currency', readonly=True)
|
|
tax_amount = fields.Numeric(
|
|
"Tax Amount", digits='currency', readonly=True)
|
|
total_amount = fields.Numeric(
|
|
"Total Amount", digits='currency', readonly=True)
|
|
total_tip_amount = fields.Numeric(
|
|
"Total Tip Amount", digits='currency', readonly=True)
|
|
currency = fields.Many2One(
|
|
'currency.currency', 'Currency', required=True)
|
|
|
|
@classmethod
|
|
def _joins(cls):
|
|
pool = Pool()
|
|
tables = {}
|
|
|
|
Sale = pool.get('sale.sale')
|
|
tables['sale.sale'] = sale = Sale.__table__()
|
|
|
|
from_item = sale
|
|
|
|
return from_item, tables
|
|
|
|
@classmethod
|
|
def _columns(cls, tables):
|
|
sale = tables['sale.sale']
|
|
return super(ReportSaleByUser, cls)._columns(tables) + [
|
|
sale.create_uid.as_('user'),
|
|
Count(Literal(1)).as_('completed_sales'),
|
|
Sum(sale.untaxed_amount_cache).as_('untaxed_amount'),
|
|
Sum(sale.tax_amount_cache).as_('tax_amount'),
|
|
Sum(sale.total_amount_cache).as_('total_amount'),
|
|
Sum(sale.total_tip_cache).as_('total_tip_amount'),
|
|
sale.currency.as_('currency'),
|
|
]
|
|
|
|
@classmethod
|
|
def _group_by(cls, tables):
|
|
sale = tables['sale.sale']
|
|
|
|
return [
|
|
sale.create_uid,
|
|
sale.currency
|
|
]
|
|
|
|
@classmethod
|
|
def _column_id(cls, tables):
|
|
sale = tables['sale.sale']
|
|
return Min(sale.id)
|
|
|
|
|
|
class ReportSaleByZone(ReportSaleAbstract, ModelView):
|
|
"""Report Sale Group by Zone"""
|
|
__name__ = 'sale_fast_food.reporting.zone'
|
|
|
|
zone = fields.Many2One('sale.zone', "Zone")
|
|
table = fields.Many2One('sale.table', "Table")
|
|
completed_sales = fields.Integer("Completed Sales")
|
|
untaxed_amount = fields.Numeric(
|
|
"Untaxed Amount", digits='currency', readonly=True)
|
|
tax_amount = fields.Numeric(
|
|
"Tax Amount", digits='currency', readonly=True)
|
|
total_amount = fields.Numeric(
|
|
"Total Amount", digits='currency', readonly=True)
|
|
total_tip_amount = fields.Numeric(
|
|
"Total Tip Amount", digits='currency', readonly=True)
|
|
currency = fields.Many2One(
|
|
'currency.currency', 'Currency', required=True)
|
|
|
|
@classmethod
|
|
def _joins(cls):
|
|
pool = Pool()
|
|
tables = {}
|
|
|
|
Sale = pool.get('sale.sale')
|
|
tables['sale.sale'] = sale = Sale.__table__()
|
|
|
|
Table = pool.get('sale.table')
|
|
tables['sale.table'] = table = Table.__table__()
|
|
|
|
Zone = pool.get('sale.zone')
|
|
tables['sale.table'] = zone = Zone.__table__()
|
|
|
|
from_item = sale.left_join(
|
|
table,
|
|
condition=table.id == sale.table
|
|
).left_join(
|
|
zone,
|
|
condition=zone.id == table.zone
|
|
)
|
|
|
|
return from_item, tables
|
|
|
|
@classmethod
|
|
def _columns(cls, tables):
|
|
sale = tables['sale.sale']
|
|
return super(ReportSaleByZone, cls)._columns(tables) + [
|
|
sale.zone.as_('zone'),
|
|
sale.table.as_('table'),
|
|
Count(Literal(1)).as_('completed_sales'),
|
|
Sum(sale.untaxed_amount_cache).as_('untaxed_amount'),
|
|
Sum(sale.tax_amount_cache).as_('tax_amount'),
|
|
Sum(sale.total_amount_cache).as_('total_amount'),
|
|
Sum(sale.total_tip_cache).as_('total_tip_amount'),
|
|
sale.currency.as_('currency'),
|
|
]
|
|
|
|
@classmethod
|
|
def _group_by(cls, tables):
|
|
sale = tables['sale.sale']
|
|
|
|
return [
|
|
sale.zone,
|
|
sale.table,
|
|
sale.currency
|
|
]
|
|
|
|
@classmethod
|
|
def _column_id(cls, tables):
|
|
sale = tables['sale.sale']
|
|
return Min(sale.id)
|