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.2 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright (C) 2017 - Today: GRAP (http://www.grap.coop)
  3. # @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. from openerp import api, fields, models
  6. import openerp.addons.decimal_precision as dp
  7. class PosOrderLine(models.Model):
  8. _inherit = 'pos.order.line'
  9. # Columns Section
  10. margin = fields.Float(
  11. 'Margin', compute='_compute_multi_margin', store=True,
  12. multi='multi_margin',
  13. digits_compute=dp.get_precision('Product Price'))
  14. purchase_price = fields.Float(
  15. 'Cost Price', digits_compute=dp.get_precision('Product Price'))
  16. margin_percent = fields.Float(
  17. 'Margin (%)', compute='_compute_multi_margin', store=True,
  18. multi='multi_margin',
  19. digits_compute=dp.get_precision('Product Price'))
  20. # Onchange Section
  21. @api.multi
  22. def onchange_product_id(
  23. self, pricelist, product_id, qty=0, partner_id=False):
  24. product_obj = self.env['product.product']
  25. res = super(PosOrderLine, self).onchange_product_id(
  26. pricelist, product_id, qty=qty, partner_id=partner_id)
  27. if product_id:
  28. product = product_obj.browse(product_id)
  29. res['value']['purchase_price'] = product.standard_price
  30. return res
  31. # Compute Section
  32. @api.multi
  33. @api.depends('purchase_price', 'qty', 'price_subtotal')
  34. def _compute_multi_margin(self):
  35. for line in self:
  36. tmp_margin = line.price_subtotal - (line.purchase_price * line.qty)
  37. line.update({
  38. 'margin': tmp_margin,
  39. 'margin_percent': (
  40. tmp_margin / line.price_subtotal * 100.0 if
  41. line.price_subtotal else 0.0),
  42. })
  43. # Overload Section. necessary, to manage pos order line creation from
  44. # create_from_ui, because onchange section is not raised
  45. @api.model
  46. def create(self, vals):
  47. if not vals.get('purchase_price', False):
  48. product_obj = self.env['product.product']
  49. product = product_obj.browse(vals.get('product_id'))
  50. vals['purchase_price'] = product.standard_price
  51. return super(PosOrderLine, self).create(vals)