Skip to content

Commit

Permalink
[IMP] purchase: Extract code from _compute_amount to allow inheritance
Browse files Browse the repository at this point in the history
Purpose
=======

Currently the discount mechanism is not implemented in the purchase module.
This limitation is could be avoided if the _compute_amount arguments could
be overwritten.

While waiting the feature to be implemented in master, we simply introduce
a hook method to allow an easy inheritance of the argument values.

Closes #11235

PS: Also move compute methods under the fields definition to follow
    the coding guidelines
  • Loading branch information
tivisse committed Jun 12, 2018
1 parent da8d4b5 commit ce1f1b6
Showing 1 changed file with 51 additions and 30 deletions.
81 changes: 51 additions & 30 deletions addons/purchase/models/purchase.py
Expand Up @@ -402,36 +402,6 @@ class PurchaseOrderLine(models.Model):
_description = 'Purchase Order Line'
_order = 'order_id, sequence, id'

@api.depends('product_qty', 'price_unit', 'taxes_id')
def _compute_amount(self):
for line in self:
taxes = line.taxes_id.compute_all(line.price_unit, line.order_id.currency_id, line.product_qty, product=line.product_id, partner=line.order_id.partner_id)
line.update({
'price_tax': sum(t.get('amount', 0.0) for t in taxes.get('taxes', [])),
'price_total': taxes['total_included'],
'price_subtotal': taxes['total_excluded'],
})

@api.multi
def _compute_tax_id(self):
for line in self:
fpos = line.order_id.fiscal_position_id or line.order_id.partner_id.property_account_position_id
# If company_id is set, always filter taxes by the company
taxes = line.product_id.supplier_taxes_id.filtered(lambda r: not line.company_id or r.company_id == line.company_id)
line.taxes_id = fpos.map_tax(taxes, line.product_id, line.order_id.partner_id) if fpos else taxes

@api.depends('invoice_lines.invoice_id.state', 'invoice_lines.quantity')
def _compute_qty_invoiced(self):
for line in self:
qty = 0.0
for inv_line in line.invoice_lines:
if inv_line.invoice_id.state not in ['cancel']:
if inv_line.invoice_id.type == 'in_invoice':
qty += inv_line.uom_id._compute_quantity(inv_line.quantity, line.product_uom)
elif inv_line.invoice_id.type == 'in_refund':
qty -= inv_line.uom_id._compute_quantity(inv_line.quantity, line.product_uom)
line.qty_invoiced = qty

name = fields.Text(string='Description', required=True)
sequence = fields.Integer(string='Sequence', default=10)
product_qty = fields.Float(string='Quantity', digits=dp.get_precision('Product Unit of Measure'), required=True)
Expand Down Expand Up @@ -465,6 +435,57 @@ def _compute_qty_invoiced(self):
currency_id = fields.Many2one(related='order_id.currency_id', store=True, string='Currency', readonly=True)
date_order = fields.Datetime(related='order_id.date_order', string='Order Date', readonly=True)

@api.depends('product_qty', 'price_unit', 'taxes_id')
def _compute_amount(self):
for line in self:
vals = line._prepare_compute_all_values()
taxes = line.taxes_id.compute_all(
vals['price_unit'],
vals['currency_id'],
vals['product_qty'],
vals['product'],
vals['partner'])
line.update({
'price_tax': sum(t.get('amount', 0.0) for t in taxes.get('taxes', [])),
'price_total': taxes['total_included'],
'price_subtotal': taxes['total_excluded'],
})

def _prepare_compute_all_values(self):
# Hook method to returns the different argument values for the
# compute_all method, due to the fact that discounts mechanism
# is not implemented yet on the purchase orders.
# This method should disappear as soon as this feature is
# also introduced like in the sales module.
self.ensure_one()
return {
'price_unit': self.price_unit,
'currency_id': self.order_id.currency_id,
'product_qty': self.product_qty,
'product': self.product_id,
'partner': self.order_id.partner_id,
}

@api.multi
def _compute_tax_id(self):
for line in self:
fpos = line.order_id.fiscal_position_id or line.order_id.partner_id.property_account_position_id
# If company_id is set, always filter taxes by the company
taxes = line.product_id.supplier_taxes_id.filtered(lambda r: not line.company_id or r.company_id == line.company_id)
line.taxes_id = fpos.map_tax(taxes, line.product_id, line.order_id.partner_id) if fpos else taxes

@api.depends('invoice_lines.invoice_id.state', 'invoice_lines.quantity')
def _compute_qty_invoiced(self):
for line in self:
qty = 0.0
for inv_line in line.invoice_lines:
if inv_line.invoice_id.state not in ['cancel']:
if inv_line.invoice_id.type == 'in_invoice':
qty += inv_line.uom_id._compute_quantity(inv_line.quantity, line.product_uom)
elif inv_line.invoice_id.type == 'in_refund':
qty -= inv_line.uom_id._compute_quantity(inv_line.quantity, line.product_uom)
line.qty_invoiced = qty

@api.model
def create(self, values):
line = super(PurchaseOrderLine, self).create(values)
Expand Down

0 comments on commit ce1f1b6

Please sign in to comment.