Extract apply method into a mixin.

This commit refs #6154
This commit is contained in:
Javier Uribe 2018-11-22 13:45:18 +01:00
parent e3b871e2aa
commit 4f7f76afb9
6 changed files with 19 additions and 115 deletions

View File

@ -9,6 +9,6 @@ def register():
Pool.register(
sale_cost.CostType,
sale_cost.CostTemplate,
sale_cost.Cost,
sale_cost.SaleCost,
invoice.InvoiceLine,
module='sale_cost_apply_invoice', type_='model')

View File

@ -35,5 +35,5 @@ msgid "Invoice Lines"
msgstr "Líneas de Factura"
msgctxt "error:sale.cost:"
msgid "Must define Invoice Party for cost of type \"%s\" on Sale \"%s\"."
msgstr "Debe definir Tercero a facturar para el coste de tipo \"%s\" en la Venta \"%s\"."
msgid "Must define Invoice Party for cost of type \"%s\" on Document \"%s\"."
msgstr "Debe definir Tercero a facturar para el coste de tipo \"%s\" en el Documento \"%s\"."

View File

@ -1,25 +1,12 @@
# The COPYRIGHT file at the top level of this repository contains the full
# copyright notices and license terms.
from trytond.pool import PoolMeta, Pool
from trytond.pool import PoolMeta
from trytond.model import fields
from trytond.pyson import Eval
from trytond.modules.document_cost_apply_invoice.cost import (
ApplyMethodCostMixin, ApplyMethodCostDocumentMixin)
__all__ = ['CostType', 'CostTemplate', 'Cost']
class ApplyMethodCostMixin(object):
invoice_party = fields.Many2One('party.party', 'Invoice Party',
states={
'invisible': ~Eval('apply_method', '').in_(
['invoice_in', 'invoice_out'])
}, depends=['apply_method'])
@classmethod
def __setup__(cls):
super(ApplyMethodCostMixin, cls).__setup__()
cls.apply_method.selection.append(('invoice_out', 'Invoice Out'))
cls.apply_method.selection.append(('invoice_in', 'Invoice In'))
__all__ = ['CostType', 'CostTemplate', 'SaleCost']
class CostType(ApplyMethodCostMixin):
@ -43,102 +30,11 @@ class CostTemplate:
self.invoice_party = self.type_.invoice_party
class Cost(ApplyMethodCostMixin):
class SaleCost(ApplyMethodCostDocumentMixin):
__name__ = 'sale.cost'
__metaclass__ = PoolMeta
@classmethod
def __setup__(cls):
super(Cost, cls).__setup__()
cls._error_messages.update({
'invoice_no_party': (
'Must define Invoice Party for cost of type "%s" '
'on Sale "%s".')
})
def on_change_template(self):
super(Cost, self).on_change_template()
super(SaleCost, self).on_change_template()
if self.template:
self.invoice_party = self.template.invoice_party
@classmethod
def _apply_method(cls, apply_method, costs):
super(Cost, cls)._apply_method(apply_method, costs)
if apply_method in ['invoice_in', 'invoice_out']:
for cost in costs:
if not cost.invoice_party:
cls.raise_user_error('invoice_no_party', (
cost.type_.rec_name, cost.document.rec_name))
cls.get_invoice_lines(apply_method, costs)
@classmethod
def _unapply_method(cls, apply_method, costs):
InvoiceLine = Pool().get('account.invoice.line')
super(Cost, cls)._unapply_method(apply_method, costs)
if apply_method in ['invoice_in', 'invoice_out']:
lines = InvoiceLine.search([
('origin.id', 'in', [c.id for c in costs], 'sale.cost')])
if lines:
InvoiceLine.delete(lines)
def _get_tax_rule_pattern(self):
return {}
@classmethod
def get_invoice_lines(cls, apply_method, costs):
pool = Pool()
InvoiceLine = pool.get('account.invoice.line')
lines = []
for cost in costs:
_lines = cost._get_invoice_lines(apply_method)
if not _lines:
continue
lines.extend(_lines)
InvoiceLine.save(lines)
return lines
def _get_invoice_lines(self, apply_method):
pool = Pool()
InvoiceLine = pool.get('account.invoice.line')
line = InvoiceLine()
line.type = 'line'
line.invoice_type = apply_method[-2:]
line.company = self.document.company
currency = self.document.company.currency
line.currency = currency
line.party = self.invoice_party
line.product = self.type_.product
line.unit = self.type_.product.default_uom
line.account = (self.type_.product.account_expense_used
if line.invoice_type == 'in'
else self.type_.product.account_revenue_used)
line.origin = self
line.quantity = 1
taxes = []
tax_fields = {
'invoice_in': ('supplier_taxes_used', 'supplier_tax_rule'),
'invoice_out': ('customer_taxes_used', 'customer_tax_rule')
}
pattern = self._get_tax_rule_pattern()
tax_rule = getattr(line.party, tax_fields[apply_method][1])
for tax in getattr(line.product, tax_fields[apply_method][0]):
if tax_rule:
tax_ids = tax_rule.apply(tax, pattern)
if tax_ids:
taxes.extend(tax_ids)
continue
taxes.append(tax.id)
if tax_rule:
tax_ids = tax_rule.apply(None, pattern)
if tax_ids:
taxes.extend(tax_ids)
line.taxes = taxes
line.unit_price = self.amount * (-1
if line.invoice_type == 'out' else 1)
return [line]

View File

@ -13,6 +13,7 @@ except ImportError:
MODULE2PREFIX = {
'sale_cost': 'datalife',
'document_cost_apply_invoice': 'datalife'
}
@ -65,6 +66,12 @@ dependency_links = [
'branch': branch,
'series': series,
}),
('hg+https://bitbucket.org/datalife_sco/'
'trytond-document_cost_apply_invoice@%(branch)s'
'#egg=datalife_document_cost_apply_invoice-%(series)s' % {
'branch': branch,
'series': series,
}),
]
if minor_version % 2:
dependency_links.append('https://trydevpi.tryton.org/')

View File

@ -173,7 +173,7 @@ Check cost applying::
>>> sale.click('confirm') # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
UserError: Must define Invoice Party for cost of type "Invoice" on Sale "1".
UserError: Must define Invoice Party for cost of type "Invoice" on Document "1".
>>> invoice_cost.invoice_party = supplier
>>> invoice_cost.save()
>>> sale.click('confirm')

View File

@ -4,6 +4,7 @@ depends:
ir
res
sale_cost
account_invoice_line_standalone
document_cost_apply_invoice
xml:
sale_cost.xml