You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.6 KiB

  1. # Copyright (C) 2017 - Today: GRAP (http://www.grap.coop)
  2. # @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from odoo import api, fields, models
  5. import odoo.addons.decimal_precision as dp
  6. class PosOrderLine(models.Model):
  7. _inherit = 'pos.order.line'
  8. # Columns Section
  9. margin = fields.Float(
  10. 'Margin', compute='_compute_multi_margin', store=True,
  11. multi='multi_margin', digits=dp.get_precision('Product Price'))
  12. purchase_price = fields.Float(
  13. 'Cost Price', compute='_compute_multi_margin', store=True,
  14. multi='multi_margin', digits=dp.get_precision('Product Price'))
  15. # Compute Section
  16. @api.multi
  17. @api.depends('product_id', 'qty', 'price_subtotal')
  18. def _compute_multi_margin(self):
  19. for line in self.filtered('product_id'):
  20. purchase_price = self._get_purchase_price(line)
  21. line.purchase_price = purchase_price
  22. line.margin = line.price_subtotal - (purchase_price * line.qty)
  23. @api.model
  24. def _get_purchase_price(self, line):
  25. # We call _get_purchase_price from sale_margin module, to reuse
  26. # computation that handles multi currencies context and UoM
  27. SaleOrderLine = self.env['sale.order.line']
  28. # if used in combination with module which does add the uom_id to line
  29. uom = hasattr(line, 'uom_id') and line.uom_id\
  30. or line.product_id.uom_id
  31. return SaleOrderLine._get_purchase_price(
  32. line.order_id.pricelist_id, line.product_id, uom,
  33. line.order_id.date_order)['purchase_price']