92 lines
3.0 KiB
Python
92 lines
3.0 KiB
Python
from trytond.pool import Pool, PoolMeta
|
|
from trytond.model import ModelView, ModelSQL, fields
|
|
from trytond.pyson import Eval, Bool, If
|
|
from trytond.transaction import Transaction
|
|
|
|
|
|
|
|
class SaleLine(metaclass=PoolMeta):
|
|
'SaleLine'
|
|
__name__ = 'sale.line'
|
|
|
|
address_equipment = fields.Many2One('party.address', "Direccion")
|
|
product_equipment = fields.Boolean("Product Equipment")
|
|
unit_digits = fields.Function(fields.Integer('Unit Digits'),
|
|
'on_change_with_unit_digits')
|
|
|
|
|
|
def on_change_with_unit_digits(self, name=None):
|
|
if self.unit:
|
|
return self.unit.digits
|
|
return 2
|
|
|
|
@fields.depends('product', 'unit', 'quantity', 'sale',
|
|
'_parent_sale.party',
|
|
methods=['_get_tax_rule_pattern', '_get_context_sale_price',
|
|
'on_change_with_amount'])
|
|
def on_change_product(self):
|
|
Product = Pool().get('product.product')
|
|
if not self.product:
|
|
self.product_equipment = False
|
|
return
|
|
|
|
party = None
|
|
|
|
if self.sale and self.sale.party:
|
|
self.product_equipment = False
|
|
party = self.sale.party
|
|
|
|
# Set taxes before unit_price to have taxes in context of sale price
|
|
taxes = []
|
|
pattern = self._get_tax_rule_pattern()
|
|
for tax in self.product.customer_taxes_used:
|
|
if party and party.customer_tax_rule:
|
|
tax_ids = party.customer_tax_rule.apply(tax, pattern)
|
|
if tax_ids:
|
|
taxes.extend(tax_ids)
|
|
continue
|
|
taxes.append(tax.id)
|
|
|
|
if party and party.customer_tax_rule:
|
|
tax_ids = party.customer_tax_rule.apply(None, pattern)
|
|
if tax_ids:
|
|
taxes.extend(tax_ids)
|
|
self.taxes = taxes
|
|
|
|
category = self.product.sale_uom.category
|
|
if not self.unit or self.unit.category != category:
|
|
self.unit = self.product.sale_uom
|
|
self.unit_digits = self.product.sale_uom.digits
|
|
|
|
with Transaction().set_context(self._get_context_sale_price()):
|
|
self.unit_price = Product.get_sale_price([self.product],
|
|
self.quantity or 0)[self.product.id]
|
|
|
|
if self.unit_price:
|
|
self.unit_price = self.unit_price.quantize(
|
|
Decimal(1) / 10 ** self.__class__.unit_price.digits[1])
|
|
|
|
self.type = 'line'
|
|
self.amount = self.on_change_with_amount()
|
|
self.product_equipment = True
|
|
|
|
@classmethod
|
|
@ModelView.button
|
|
def process(cls, sales):
|
|
states = {'confirmed', 'processing', 'done'}
|
|
sales = [s for s in sales if s.state in states]
|
|
cls.lock(sales)
|
|
cls._process_invoice(sales)
|
|
cls._process_shipment(sales)
|
|
cls._process_invoice_shipment_states(sales)
|
|
cls._process_state(sales)
|
|
|
|
@classmethod
|
|
def view_attributes(cls):
|
|
return super(SaleLine, cls).view_attributes() + [
|
|
('//page[@id="equipment"]', 'states', {
|
|
'invisible': ~Eval('product_equipment', True),
|
|
})]
|
|
|
|
|