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.

39 lines
1.3 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 PosOrder(models.Model):
  7. _inherit = 'pos.order'
  8. # Columns Section
  9. margin = fields.Float(
  10. string='Margin',
  11. compute='_compute_margin',
  12. store=True,
  13. digits=dp.get_precision('Product Price'),
  14. help="It gives profitability by calculating the difference between"
  15. " the Unit Price and the cost price.")
  16. margin_percent = fields.Float(
  17. string='Margin (%)',
  18. compute='_compute_margin',
  19. store=True,
  20. digits=dp.get_precision('Product Price'),
  21. )
  22. # Compute Section
  23. @api.multi
  24. @api.depends('lines.margin', 'lines.price_subtotal')
  25. def _compute_margin(self):
  26. for order in self:
  27. tmp_margin = sum(order.mapped('lines.margin'))
  28. tmp_price_subtotal = sum(order.mapped('lines.price_subtotal'))
  29. order.update({
  30. 'margin': tmp_margin,
  31. 'margin_percent': tmp_price_subtotal and (
  32. tmp_margin / tmp_price_subtotal * 100) or 0.0,
  33. })