|
|
|
|
@@ -2,6 +2,7 @@ from trytond.pool import Pool, PoolMeta
|
|
|
|
|
from trytond.model import ModelView, fields
|
|
|
|
|
from trytond.transaction import Transaction
|
|
|
|
|
from trytond.modules.currency.fields import Monetary
|
|
|
|
|
from trytond.pyson import Eval
|
|
|
|
|
from decimal import Decimal
|
|
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
@@ -18,6 +19,12 @@ class Sale(metaclass=PoolMeta):
|
|
|
|
|
"Total Discount", digits='currency', currency='currency'),
|
|
|
|
|
'get_amount')
|
|
|
|
|
total_discount_cache = fields.Numeric("Total Discount cache", digits='currency')
|
|
|
|
|
total_tip = fields.Function(
|
|
|
|
|
Monetary(
|
|
|
|
|
"Total Tip", digits='currency', currency='currency'),
|
|
|
|
|
'get_amount')
|
|
|
|
|
total_tip_cache = fields.Numeric("Total Tip cache", digits="currency")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def __setup__(cls):
|
|
|
|
|
@@ -40,6 +47,7 @@ class Sale(metaclass=PoolMeta):
|
|
|
|
|
self.tax_amount = Decimal('0.0')
|
|
|
|
|
self.total_amount = Decimal('0.0')
|
|
|
|
|
self.total_discount = Decimal('0.0')
|
|
|
|
|
self.total_tip = Decimal('0.0')
|
|
|
|
|
|
|
|
|
|
if self.lines:
|
|
|
|
|
for line in self.lines:
|
|
|
|
|
@@ -50,6 +58,7 @@ class Sale(metaclass=PoolMeta):
|
|
|
|
|
self.tax_amount = self.currency.round(self.tax_amount)
|
|
|
|
|
self.total_amount = self.untaxed_amount + self.tax_amount
|
|
|
|
|
if self.currency:
|
|
|
|
|
self.total_tip = self.currency.round(self.total_tip)
|
|
|
|
|
self.total_discount = self.currency.round(self.total_discount)
|
|
|
|
|
self.total_amount = self.currency.round(self.total_amount)
|
|
|
|
|
|
|
|
|
|
@@ -60,6 +69,7 @@ class Sale(metaclass=PoolMeta):
|
|
|
|
|
sale.tax_amount_cache = sale.tax_amount
|
|
|
|
|
sale.total_amount_cache = sale.total_amount
|
|
|
|
|
sale.total_discount_cache = sale.total_discount
|
|
|
|
|
sale.total_tip_cache = sale.total_tip
|
|
|
|
|
cls.save(sales)
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
@@ -68,6 +78,7 @@ class Sale(metaclass=PoolMeta):
|
|
|
|
|
tax_amount = {}
|
|
|
|
|
total_amount = {}
|
|
|
|
|
total_discount = {}
|
|
|
|
|
total_tip = {}
|
|
|
|
|
if {'tax_amount', 'total_amount'} & set(names):
|
|
|
|
|
compute_taxes = True
|
|
|
|
|
else:
|
|
|
|
|
@@ -83,6 +94,7 @@ class Sale(metaclass=PoolMeta):
|
|
|
|
|
and sale.total_amount_cache is not None):
|
|
|
|
|
untaxed_amount[sale.id] = sale.untaxed_amount_cache
|
|
|
|
|
total_discount[sale.id] = sale.total_discount_cache
|
|
|
|
|
total_tip[sale.id] = sale.total_tip_cache
|
|
|
|
|
if compute_taxes:
|
|
|
|
|
tax_amount[sale.id] = sale.tax_amount_cache
|
|
|
|
|
total_amount[sale.id] = sale.total_amount_cache
|
|
|
|
|
@@ -91,8 +103,11 @@ class Sale(metaclass=PoolMeta):
|
|
|
|
|
(line.amount for line in sale.lines
|
|
|
|
|
if line.type == 'line'), Decimal(0)),2)
|
|
|
|
|
total_discount[sale.id] = round(sum(
|
|
|
|
|
(line.discount_amount for line in sale.lines
|
|
|
|
|
if line.type == 'line'), Decimal(0)), 2)
|
|
|
|
|
(line.discount_amount * Decimal(line.quantity) for line in sale.lines
|
|
|
|
|
if line.discount_amount and line.type == 'line'), Decimal(0)), 2)
|
|
|
|
|
total_tip[sale.id] = round(sum(
|
|
|
|
|
(line.amount for line in sale.lines if line.product and line.product.tip and line.type == 'line'),
|
|
|
|
|
Decimal(0)), 2)
|
|
|
|
|
if compute_taxes:
|
|
|
|
|
tax_amount[sale.id] = sale.get_tax_amount()
|
|
|
|
|
total_amount[sale.id] = (
|
|
|
|
|
@@ -103,6 +118,7 @@ class Sale(metaclass=PoolMeta):
|
|
|
|
|
'tax_amount': tax_amount,
|
|
|
|
|
'total_amount': total_amount,
|
|
|
|
|
'total_discount': total_discount,
|
|
|
|
|
'total_tip' : total_tip
|
|
|
|
|
}
|
|
|
|
|
for key in list(result.keys()):
|
|
|
|
|
if key not in names:
|
|
|
|
|
@@ -167,10 +183,11 @@ class Sale(metaclass=PoolMeta):
|
|
|
|
|
"unit_price": str(line.amount_w_tax) if line.type != 'title' else None,
|
|
|
|
|
"taxes": str(round(line.taxes[0].rate * 100, 2))+'%'
|
|
|
|
|
if line.type != 'title' and line.taxes else None
|
|
|
|
|
} for line in record.lines]
|
|
|
|
|
} for line in record.lines if line.type == 'line' and not line.product.tip]
|
|
|
|
|
|
|
|
|
|
data["total_discount"] = str(round(record.total_discount,2))
|
|
|
|
|
data["untaxed_amount"] = str(record.untaxed_amount)
|
|
|
|
|
data["total_tip"] = str(record.total_tip)
|
|
|
|
|
data["tax_amount"] = str(record.tax_amount)
|
|
|
|
|
data["total"] = str(record.total_amount)
|
|
|
|
|
data["state"] = "SUBTOTAL" if record.state == "draft" else "CUENTA FINAL"
|
|
|
|
|
@@ -330,6 +347,17 @@ class Line(metaclass=PoolMeta):
|
|
|
|
|
pizza = fields.Integer("Pizza")
|
|
|
|
|
impreso = fields.Boolean("Impreso")
|
|
|
|
|
bought_pizza = fields.Boolean("Sold pizza")
|
|
|
|
|
rate = fields.Numeric(
|
|
|
|
|
"Rate", digits=(16, 4),
|
|
|
|
|
states={
|
|
|
|
|
'invisible': Eval('type') != 'line',
|
|
|
|
|
'readonly': Eval('sale_state') != 'draft',
|
|
|
|
|
},
|
|
|
|
|
depends=['type', 'sale_state'])
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def default_rate(cls):
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
@fields.depends('product', 'unit', 'sale',
|
|
|
|
|
'_parent_sale.party', '_parent_sale.invoice_party',
|
|
|
|
|
@@ -349,3 +377,8 @@ class Line(metaclass=PoolMeta):
|
|
|
|
|
Production = super(Line, self).get_production()
|
|
|
|
|
|
|
|
|
|
return Production
|
|
|
|
|
|
|
|
|
|
@fields.depends('discount_rate')
|
|
|
|
|
def on_change_discount_rate(self):
|
|
|
|
|
if self.discount_rate:
|
|
|
|
|
self.rate = self.discount_rate
|
|
|
|
|
|