# This file is part of Tryton. The COPYRIGHT file at the top level of # this repository contains the full copyright notices and license terms. from trytond.pool import PoolMeta from trytond.model import fields from trytond.modules.currency.fields import Monetary from decimal import Decimal from trytond.model.exceptions import ValidationError class InvoiceLine(metaclass=PoolMeta): 'Invoice Line Extras' __name__ = 'account.invoice.line' tax_amount = fields.Function(Monetary("Tax", currency='currency', digits='currency'), 'get_amount_taxes') tax_unit = fields.Function(Monetary("Unit Tax", currency='currency', digits='currency'), 'get_unit_taxes') total_with_taxes = fields.Function(Monetary("Total With Taxes", currency='currency', digits='currency'), 'get_total_with_taxes') unit_with_tax = fields.Function(Monetary("Unit With Tax", currency='currency', digits='currency'), 'get_unit_with_tax') tax_extra_unit = Monetary( "Tax Extra Unit", currency='currency', digits='currency') total_tax_extra = fields.Function(Monetary( "Total Tax Extra", currency='currency', digits='currency'), 'get_total_tax_extra') @fields.depends('type', 'quantity', 'unit_price', 'unit', 'invoice', '_parent_invoice.currency', 'taxes', 'amount') def on_change_with_tax_amount(self): Decimal('0.0') if self.type == 'line': tax_amount = Decimal('0.0') currency = self.invoice.currency if self.invoice else None for tax in self.taxes: tax_amount += Decimal(str((tax.rate * self.amount) or '0.0')) if currency: return currency.round(tax_amount) return tax_amount return Decimal('0.0') @fields.depends('type', 'quantity', 'unit_price', 'unit', 'invoice', '_parent_invoice.currency', 'taxes', 'amount') def on_change_with_tax_unit(self): Decimal('0.0') if self.type == 'line': tax_unit = Decimal('0.0') currency = self.invoice.currency if self.invoice else None for tax in self.taxes: tax_unit += Decimal(str((tax.rate * self.unit_price) or '0.0')) if currency: return currency.round(tax_unit) return tax_unit return Decimal('0.0') @fields.depends('type', 'quantity', 'unit_price', 'unit', 'invoice', '_parent_invoice.currency', 'taxes', 'amount') def on_change_with_total_with_taxes(self): try: total_with_taxes = self.amount + self.tax_amount return total_with_taxes except Exception as e: raise ValidationError(f"Se produjo un error {e}.") @fields.depends('type', 'quantity', 'unit_price', 'unit', 'invoice', '_parent_invoice.currency', 'taxes', 'amount') def on_change_with_unit_with_tax(self): try: unit_with_tax = self.unit_price + self.tax_unit return unit_with_tax except Exception as e: raise ValidationError(f"Se produjo un error {e}.") @fields.depends('type', 'quantity', 'tax_extra_unit', 'unit_price', 'unit', 'invoice', '_parent_invoice.currency', 'taxes', 'amount') def on_change_with_total_tax_extra(self): if self.type == 'line': currency = self.invoice.currency if self.invoice else None total_tax_extra = Decimal(str(self.quantity or '0.0')) * \ (self.tax_extra_unit or Decimal('0.0')) if currency: return currency.round(total_tax_extra) return total_tax_extra return Decimal('0.0') @fields.depends('product', 'unit', '_parent_invoice.type', '_parent_invoice.party', 'party', 'invoice', 'invoice_type', '_parent_invoice.invoice_date', '_parent_invoice.accounting_date', 'company', methods=['_get_tax_rule_pattern']) def on_change_product(self): self.tax_amount = self.on_change_with_tax_amount() self.total_with_taxes = self.on_change_with_total_with_taxes() super(InvoiceLine, self).on_change_product() def get_amount_taxes(self, name): if self.type == 'line': return self.on_change_with_tax_amount() def get_unit_taxes(self, name): if self.type == 'line': return self.on_change_with_tax_unit() def get_total_with_taxes(self, name): if self.type == 'line': return self.on_change_with_total_with_taxes() def get_unit_with_tax(self, name): if self.type == 'line': return self.on_change_with_unit_with_tax() def get_total_tax_extra(self, name): if self.type == 'line': return self.on_change_with_total_tax_extra()