|
| 1 | +from odoo import models, fields |
| 2 | + |
| 3 | + |
| 4 | +class ProductPricelistItem(models.Model): |
| 5 | + _inherit = "product.pricelist.item" |
| 6 | + |
| 7 | + base = fields.Selection( |
| 8 | + selection_add=[ |
| 9 | + ("last_purchase_price", "Last Purchase Price"), |
| 10 | + ], |
| 11 | + default="list_price", |
| 12 | + ondelete={"last_purchase_price": "set default"}, |
| 13 | + help="Choose what the pricing rule should be based on:\n" |
| 14 | + "- Sales Price: Uses the standard sales price from the product.\n" |
| 15 | + "- Last Purchase Price: Uses the most recent purchase price paid for the product.", |
| 16 | + ) |
| 17 | + |
| 18 | + |
| 19 | +def _compute_base_price(self, product, quantity, uom=None, date=None, currency=None): |
| 20 | + self.ensure_one() # this method is called per item |
| 21 | + if self.base == "last_purchase_price": |
| 22 | + # Use product's template's last purchase price |
| 23 | + price = product.product_tmpl_id.last_purchase_price |
| 24 | + if currency and product.currency_id != currency: |
| 25 | + price = product.currency_id._convert( |
| 26 | + price, |
| 27 | + currency, |
| 28 | + product.company_id or self.env.company, |
| 29 | + date or fields.Date.today(), |
| 30 | + ) |
| 31 | + else: |
| 32 | + # Fallback to original method |
| 33 | + return super(ProductPricelistItem, self)._compute_base_price( |
| 34 | + product, quantity, uom=uom, date=date, currency=currency |
| 35 | + ) |
| 36 | + return price or 0.0 # Ensure we return a float |
0 commit comments