Skip to content

Commit f0d2293

Browse files
committed
[Add] 4049583 Last purchase price
[FIX] records IDs & LPO logic Revert "[FIX] README.md: Fixing typos" This reverts commit 4c650f3. [CR] Fixed changes in code review
1 parent 4c650f3 commit f0d2293

File tree

8 files changed

+118
-2
lines changed

8 files changed

+118
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Odoo tutorials
22

33
This repository hosts the code for the bases of the modules used in the
4-
[official Odoo tutorials](https://www.odoo.com/documentation/latest/developer/tutorials.html).
4+
[official Odoo tutorials](https://www.odoo.com/documentation/lastest/developer/tutorials.html).
55

66
It has 3 branches for each Odoo version: one for the bases, one for the
77
[Discover the JS framework](https://www.odoo.com/documentation/latest/developer/tutorials/discover_js_framework.html)
88
tutorial's solutions, and one for the
9-
[Master the Odoo web framework](https://www.odoo.com/documentation/latest/developer/tutorials/master_odoo_web_framework.html)
9+
[Master the Odoo web framework](https://www.odoo.com/documentation/lastest/developer/tutorials/master_odoo_web_framework.html)
1010
tutorial's solutions. For example, `17.0`, `17.0-discover-js-framework-solutions` and
1111
`17.0-master-odoo-web-framework-solutions`.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import models
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "Last Purchase Price in Products and Pricelists",
3+
"version": "18.0.1.0.0",
4+
"summary": "Shows last purchase price on product page and adds it to pricelist options.",
5+
"category": "Purchase/Purchase",
6+
"author": "Odoo PS",
7+
"website": "www.odoo.com",
8+
"depends": ["purchase"],
9+
"data": [
10+
"views/product_template_views.xml",
11+
"views/pricelist_item_views.xml",
12+
],
13+
"license": "OEEL-1",
14+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from . import pricelist_item
2+
from . import product_template
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from odoo import models, fields, api
2+
3+
4+
class ProductTemplate(models.Model):
5+
_inherit = "product.template"
6+
7+
last_purchase_price = fields.Float(
8+
string="Last Purchase Price",
9+
compute="_compute_last_purchase_price",
10+
)
11+
12+
@api.depends("product_variant_ids")
13+
def _compute_last_purchase_price(self):
14+
for template in self:
15+
product = template.product_variant_ids[:1]
16+
last_po_line = self.env["purchase.order.line"].search(
17+
[
18+
("product_id", "=", product.id),
19+
("order_id.state", "in", ["purchase", "done"]),
20+
],
21+
order="date_order desc",
22+
limit=1,
23+
)
24+
template.last_purchase_price = (
25+
last_po_line.price_unit if last_po_line else 0.0
26+
)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<odoo>
2+
<!-- Extend the product.pricelist.item form view to add the selection for base pricing -->
3+
<record id="view_product_pricelist_item_form_inherit_product_last_purchase_price"
4+
model="ir.ui.view">
5+
<field name="name">view.product.pricelist.item.form.inherit.product.last.purchase.price</field>
6+
<field name="model">product.pricelist.item</field>
7+
<field name="inherit_id" ref="product.product_pricelist_item_form_view" />
8+
<field name="arch" type="xml">
9+
<xpath expr="//field[@name='base']" position="attributes">
10+
<attribute name="widget">selection</attribute>
11+
<attribute name="help">Choose what the pricing rule should be based on: <ul>
12+
<li>Sales Price: Uses the standard sales price from the product.</li>
13+
<li>Last Purchase Price: Uses the most recent purchase price paid for the
14+
product.</li>
15+
</ul>
16+
</attribute>
17+
</xpath>
18+
</field>
19+
</record>
20+
</odoo>
21+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<odoo>
2+
<!-- Extend the product.template form view to add the last_purchase_price field -->
3+
<record id="view_product_template_form_inherit_product_last_purchase_price" model="ir.ui.view">
4+
<field name="name">view.product.template.form.inherit.product.last.purchase.price</field>
5+
<field name="model">product.template</field>
6+
<field name="inherit_id" ref="product.product_template_form_view" />
7+
<field name="arch" type="xml">
8+
<xpath expr="//sheet//group" position="inside">
9+
<group>
10+
<field name="last_purchase_price" />
11+
</group>
12+
</xpath>
13+
</field>
14+
</record>
15+
</odoo>
16+

0 commit comments

Comments
 (0)