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.

60 lines
2.0 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. string='Margin',
  11. compute='_compute_multi_margin',
  12. store=True,
  13. digits=dp.get_precision('Product Price'),
  14. )
  15. margin_percent = fields.Float(
  16. string='Margin (%)',
  17. compute='_compute_multi_margin',
  18. store=True,
  19. digits=dp.get_precision('Product Price'),
  20. )
  21. purchase_price = fields.Float(
  22. string='Cost Price',
  23. compute='_compute_multi_margin',
  24. store=True,
  25. digits=dp.get_precision('Product Price'),
  26. )
  27. # Compute Section
  28. @api.multi
  29. @api.depends('product_id', 'qty', 'price_subtotal')
  30. def _compute_multi_margin(self):
  31. for line in self.filtered('product_id'):
  32. purchase_price = self._get_purchase_price(line)
  33. margin = line.price_subtotal - (purchase_price * line.qty)
  34. line.update({
  35. 'purchase_price': purchase_price,
  36. 'margin': margin,
  37. 'margin_percent': line.price_subtotal and (
  38. margin / line.price_subtotal * 100.0),
  39. })
  40. @api.model
  41. def _get_purchase_price(self, line):
  42. # We call _get_purchase_price from sale_margin module, to reuse
  43. # computation that handles multi currencies context and UoM
  44. SaleOrderLine = self.env['sale.order.line']
  45. # if used in combination with module which does add the uom_id to line
  46. uom = hasattr(line, 'uom_id') and line.uom_id\
  47. or line.product_id.uom_id
  48. return SaleOrderLine._get_purchase_price(
  49. line.order_id.pricelist_id, line.product_id, uom,
  50. line.order_id.date_order)['purchase_price']