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.

118 lines
3.9 KiB

  1. # Copyright 2004-2009 Tiny SPRL (<http://tiny.be>).
  2. # Copyright 2016 ACSONE SA/NV (<http://acsone.eu>)
  3. # Copyright 2015-2019 Tecnativa - Pedro M. Baeza
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  5. from odoo import api, fields, models
  6. class PurchaseOrder(models.Model):
  7. _inherit = "purchase.order"
  8. def _add_supplier_to_product(self):
  9. """Insert a mapping of products to PO lines to be picked up
  10. in supplierinfo's create()"""
  11. self.ensure_one()
  12. po_line_map = {
  13. line.product_id.product_tmpl_id.id: line for line in self.order_line
  14. }
  15. return super(
  16. PurchaseOrder, self.with_context(po_line_map=po_line_map)
  17. )._add_supplier_to_product()
  18. class PurchaseOrderLine(models.Model):
  19. _inherit = "purchase.order.line"
  20. # adding discount to depends
  21. @api.depends("discount")
  22. def _compute_amount(self):
  23. return super()._compute_amount()
  24. def _prepare_compute_all_values(self):
  25. vals = super()._prepare_compute_all_values()
  26. vals.update({"price_unit": self._get_discounted_price_unit()})
  27. return vals
  28. discount = fields.Float(string="Discount (%)", digits="Discount")
  29. _sql_constraints = [
  30. (
  31. "discount_limit",
  32. "CHECK (discount <= 100.0)",
  33. "Discount must be lower than 100%.",
  34. )
  35. ]
  36. def _get_discounted_price_unit(self):
  37. """Inheritable method for getting the unit price after applying
  38. discount(s).
  39. :rtype: float
  40. :return: Unit price after discount(s).
  41. """
  42. self.ensure_one()
  43. if self.discount:
  44. return self.price_unit * (1 - self.discount / 100)
  45. return self.price_unit
  46. @api.onchange("product_qty", "product_uom")
  47. def _onchange_quantity(self):
  48. """
  49. Check if a discount is defined into the supplier info and if so then
  50. apply it to the current purchase order line
  51. """
  52. res = super()._onchange_quantity()
  53. if self.product_id:
  54. date = None
  55. if self.order_id.date_order:
  56. date = self.order_id.date_order.date()
  57. seller = self.product_id._select_seller(
  58. partner_id=self.partner_id,
  59. quantity=self.product_qty,
  60. date=date,
  61. uom_id=self.product_uom,
  62. )
  63. self._apply_value_from_seller(seller)
  64. return res
  65. @api.model
  66. def _apply_value_from_seller(self, seller):
  67. """Overload this function to prepare other data from seller,
  68. like in purchase_triple_discount module"""
  69. if not seller:
  70. return
  71. self.discount = seller.discount
  72. def _prepare_account_move_line(self, move=False):
  73. vals = super(PurchaseOrderLine, self)._prepare_account_move_line(move)
  74. vals["discount"] = self.discount
  75. return vals
  76. @api.model
  77. def _prepare_purchase_order_line(
  78. self, product_id, product_qty, product_uom, company_id, supplier, po
  79. ):
  80. """Apply the discount to the created purchase order"""
  81. res = super()._prepare_purchase_order_line(
  82. product_id, product_qty, product_uom, company_id, supplier, po
  83. )
  84. partner = supplier.name
  85. uom_po_qty = product_uom._compute_quantity(product_qty, product_id.uom_po_id)
  86. seller = product_id.with_company(company_id)._select_seller(
  87. partner_id=partner,
  88. quantity=uom_po_qty,
  89. date=po.date_order and po.date_order.date(),
  90. uom_id=product_id.uom_po_id,
  91. )
  92. res.update(self._prepare_purchase_order_line_from_seller(seller))
  93. return res
  94. @api.model
  95. def _prepare_purchase_order_line_from_seller(self, seller):
  96. """Overload this function to prepare other data from seller,
  97. like in purchase_triple_discount module"""
  98. if not seller:
  99. return {}
  100. return {"discount": seller.discount}