se adiciona __setup__ para inicializar modelos

FossilOrigin-Name: 504ad84bee1c708c2b55fdde3552d39980bf1efad3a51de8050018e4d1f387f3
This commit is contained in:
bit4bit 2021-07-09 01:03:51 +00:00
parent b3e4a088b7
commit a1a9746353
4 changed files with 37 additions and 10 deletions

View File

@ -111,13 +111,17 @@ class TaxSubTotal(model.Model):
tax_amount = fields.Many2One(Amount, name='TaxAmount')
tax_category = fields.Many2One(TaxCategory)
percent = fields.Virtual(setter='set_percent')
def set_percent(self, name, value):
self.tax_category.percent = value
# TODO(bit4bit) hacer variable
self.tax_category.tax_scheme.id = '01'
self.tax_category.tax_scheme.name = 'IVA'
percent = fields.Virtual(setter='set_category')
scheme = fields.Virtual(setter='set_category')
def set_category(self, name, value):
if name == 'percent':
self.tax_category.percent = value
# TODO(bit4bit) hacer variable
self.tax_category.tax_scheme.id = '01'
self.tax_category.tax_scheme.name = 'IVA'
elif name == 'scheme':
self.tax_category.tax_scheme.id = value
class TaxTotal(model.Model):
__name__ = 'TaxTotal'
@ -186,6 +190,8 @@ class Invoice(model.Model):
lines = fields.One2Many(InvoiceLine)
legal_monetary_total = fields.Many2One(LegalMonetaryTotal)
cufe = fields.Virtual()
@fields.on_change(['lines'])
def update_legal_monetary_total(self, name, value):
for line in self.lines:

View File

@ -50,7 +50,9 @@ class ModelBase(object, metaclass=ModelMeta):
(fun, on_change_fields) = on_change_fields_for_function()
for field in on_change_fields:
obj._on_change_fields[field].append(fun)
obj.__setup__()
return obj
def _set_attribute(self, field, name, value):
@ -141,4 +143,8 @@ class Model(ModelBase):
"""
return value
def __setup__(self):
"""
Inicializar modelo
"""

View File

@ -506,3 +506,15 @@ def test_field_amount():
assert '<Line amount="33"/>' == line.to_xml()
def test_model_setup():
class Line(facho.model.Model):
__name__ = 'Line'
amount = fields.Attribute(name='amount')
def __setup__(self):
self.amount = 23
line = Line()
assert '<Line amount="23"/>' == line.to_xml()

View File

@ -36,5 +36,8 @@ def _test_simple_invoice_cufe():
line = invoice.lines.create()
line.quantity = 1
line.price = 1_500_000
subtotal = line.taxtotal.subtotals.create()
subtotal.percent = 19.0
line_subtotal = line.taxtotal.subtotals.create()
line_subtotal.percent = 19.0
line.subtotal.scheme = '01'
assert invoice.cufe == '8bb918b19ba22a694f1da11c643b5e9de39adf60311cf179179e9b33381030bcd4c3c3f156c506ed5908f9276f5bd9b4'