Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 27 additions & 17 deletions purchase_last_price_info/models/product_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,36 @@ class ProductProduct(models.Model):
string="Last Purchase Currency",
)
show_last_purchase_price_currency = fields.Boolean(
compute="_compute_show_last_purchase_price_currency",
related="show_last_purchase_price_currency_rate",
string="Show Last Purchase Price Currency (Old)",
)
show_last_purchase_price_currency_rate = fields.Boolean(
compute="_compute_show_last_purchase_price_currency_rate",
)
last_purchase_price_currency = fields.Float(
string="Last currency purchase price",
compute="_compute_last_purchase_price_currency",
related="last_purchase_price_currency_rate",
string="Last Purchase Currency Rate (Old)",
)
last_purchase_price_currency_rate = fields.Float(
string="Last Purchase Currency Rate",
compute="_compute_last_purchase_price_currency_rate",
digits=0,
)

@api.depends_context("company")
@api.depends("last_purchase_line_ids.state")
def _compute_last_purchase_line_id(self):
for item in self:
item.last_purchase_line_id = fields.first(
item.last_purchase_line_ids.sudo().filtered_domain(
[
("state", "in", ["purchase", "done"]),
("company_id", "in", self.env.companies.ids),
]
)
candidate_lines = item.last_purchase_line_ids.sudo().filtered_domain(
[
("state", "in", ["purchase", "done"]),
("company_id", "in", self.env.companies.ids),
]
)
sorted_lines = candidate_lines.sorted(
key=lambda r: (r.date_order, r.id), reverse=True
)
item.last_purchase_line_id = fields.first(sorted_lines)

@api.depends("last_purchase_line_id")
def _compute_last_purchase_line_id_info(self):
Expand All @@ -66,29 +76,29 @@ def _compute_last_purchase_line_id_info(self):
item.last_purchase_currency_id = item.last_purchase_line_id.currency_id

@api.depends("last_purchase_line_id", "last_purchase_currency_id")
def _compute_show_last_purchase_price_currency(self):
def _compute_show_last_purchase_price_currency_rate(self):
for item in self:
last_line = item.last_purchase_line_id
item.show_last_purchase_price_currency = (
item.show_last_purchase_price_currency_rate = (
last_line
and item.last_purchase_currency_id
and item.last_purchase_currency_id != last_line.company_id.currency_id
)

@api.depends(
"last_purchase_line_id",
"show_last_purchase_price_currency",
"show_last_purchase_price_currency_rate",
"last_purchase_currency_id",
"last_purchase_date",
)
def _compute_last_purchase_price_currency(self):
def _compute_last_purchase_price_currency_rate(self):
for item in self:
if item.show_last_purchase_price_currency:
if item.show_last_purchase_price_currency_rate:
rates = item.last_purchase_currency_id._get_rates(
item.last_purchase_line_id.company_id, item.last_purchase_date
)
item.last_purchase_price_currency = rates.get(
item.last_purchase_price_currency_rate = rates.get(
item.last_purchase_currency_id.id
)
else:
item.last_purchase_price_currency = 1
item.last_purchase_price_currency_rate = 1
30 changes: 20 additions & 10 deletions purchase_last_price_info/models/product_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,36 @@ class ProductTemplate(models.Model):
string="Last Purchase Currency",
)
show_last_purchase_price_currency = fields.Boolean(
related="product_variant_ids.show_last_purchase_price_currency",
related="show_last_purchase_price_currency_rate",
string="Show Last Purchase Price Currency (Old)",
)
show_last_purchase_price_currency_rate = fields.Boolean(
related="product_variant_ids.show_last_purchase_price_currency_rate",
)
last_purchase_price_currency = fields.Float(
string="Last currency purchase price",
related="product_variant_ids.last_purchase_price_currency",
related="last_purchase_price_currency_rate",
string="Last Purchase Currency Rate (Old)",
)
last_purchase_price_currency_rate = fields.Float(
string="Last Purchase Currency Rate",
related="product_variant_ids.last_purchase_price_currency_rate",
digits=0,
)

@api.depends_context("company")
@api.depends("last_purchase_line_ids.state")
def _compute_last_purchase_line_id(self):
for item in self:
item.last_purchase_line_id = fields.first(
item.last_purchase_line_ids.sudo().filtered_domain(
[
("state", "in", ["purchase", "done"]),
("company_id", "in", self.env.companies.ids),
]
)
candidate_lines = item.last_purchase_line_ids.sudo().filtered_domain(
[
("state", "in", ["purchase", "done"]),
("company_id", "in", self.env.companies.ids),
]
)
sorted_lines = candidate_lines.sorted(
key=lambda r: (r.date_order, r.id), reverse=True
)
item.last_purchase_line_id = fields.first(sorted_lines)

@api.depends("last_purchase_line_id")
def _compute_last_purchase_line_id_info(self):
Expand Down
90 changes: 72 additions & 18 deletions purchase_last_price_info/tests/test_purchase_last_price_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# Copyright 2021 Tecnativa - Víctor Martínez
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import datetime

import odoo.tests.common as common
from odoo import fields

Expand All @@ -16,6 +18,16 @@ def setUp(self):
self.purchase_model = self.env["purchase.order"]
self.purchase_line_model = self.env["purchase.order.line"]
self.product = self.env.ref("product.consu_delivery_01")
self.product1 = self.env["product.product"].create(
{
"name": "Test product",
"type": "consu",
"standard_price": 10.0,
"list_price": 20.0,
"uom_id": self.env.ref("uom.product_uom_unit").id,
"uom_po_id": self.env.ref("uom.product_uom_unit").id,
}
)
self.partner = self.env.ref("base.res_partner_1")
# Create custom rates to currency + currency_extra
self._create_currency_rate(self.currency, "2000-01-01", 1.0)
Expand Down Expand Up @@ -54,10 +66,10 @@ def test_purchase_last_price_info_demo(self):
first_purchase_line.currency_id, self.product.last_purchase_currency_id
)
self.assertEqual(self.product.last_purchase_currency_id, self.currency)
self.assertEqual(self.product.last_purchase_price_currency, 1.0)
self.assertEqual(self.product.last_purchase_price_currency_rate, 1.0)

def test_purchase_last_price_info_new_order(self):
purchase_order = self.purchase_model.create(
purchase_order1 = self.purchase_model.create(
{
"date_order": "2000-01-01",
"currency_id": self.currency_extra.id,
Expand All @@ -67,30 +79,72 @@ def test_purchase_last_price_info_new_order(self):
0,
0,
{
"product_id": self.product.id,
"product_uom": self.product.uom_id.id,
"price_unit": self.product.standard_price,
"name": self.product.name,
"product_id": self.product1.id,
"product_uom": self.product1.uom_id.id,
"price_unit": self.product1.standard_price,
"name": self.product1.name,
"date_planned": fields.Datetime.now(),
"product_qty": 1,
"sequence": 1,
},
)
],
}
)
purchase_order.button_confirm()
purchase_order2 = self.purchase_model.create(
{
"date_order": "2001-01-01",
"currency_id": self.currency_extra.id,
"partner_id": self.partner.id,
"order_line": [
(
0,
0,
{
"product_id": self.product1.id,
"product_uom": self.product1.uom_id.id,
"price_unit": self.product1.standard_price,
"name": self.product1.name,
"date_planned": fields.Datetime.now(),
"product_qty": 1,
"sequence": 9999,
},
)
],
}
)
purchase_order1.button_confirm()
purchase_order2.button_confirm()
self.assertEqual(
fields.Datetime.from_string(purchase_order.date_order).date(),
fields.Datetime.from_string(self.product.last_purchase_date).date(),
purchase_order2.date_order,
self.product1.last_purchase_date,
)
first_order_line = fields.first(
self.product1.last_purchase_line_ids.sudo().filtered_domain(
[
("state", "in", ["purchase", "done"]),
("company_id", "in", self.env.companies.ids),
]
)
)
self.assertNotEqual(
first_order_line.date_order,
self.product1.last_purchase_date,
)
expected_date = datetime.datetime(2001, 1, 1, 0, 0)
self.assertEqual(
expected_date,
self.product1.last_purchase_date,
)
first_order_line = fields.first(purchase_order.order_line)
self.assertEqual(first_order_line.price_unit, self.product.last_purchase_price)
expected_price = 10.0
self.assertEqual(expected_price, self.product1.last_purchase_price)
expected_currency = self.currency_extra
self.assertEqual(
first_order_line.currency_id,
self.product.last_purchase_currency_id,
expected_currency,
self.product1.last_purchase_currency_id,
)
self.assertEqual(self.product.last_purchase_currency_id, self.currency_extra)
self.assertEqual(self.product.last_purchase_price_currency, 2.0)
self.assertEqual(self.partner, self.product.last_purchase_supplier_id)
purchase_order.button_cancel()
self.assertEqual(purchase_order.state, "cancel")
self.assertEqual(self.product1.last_purchase_currency_id, self.currency_extra)
self.assertEqual(self.product1.last_purchase_price_currency, 2.0)
self.assertEqual(self.partner, self.product1.last_purchase_supplier_id)
purchase_order2.button_cancel()
self.assertEqual(purchase_order2.state, "cancel")
22 changes: 14 additions & 8 deletions purchase_last_price_info/views/product_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@
<field name="last_purchase_supplier_id" />
<field name="last_purchase_date" />
<field name="last_purchase_price" />
<field name="show_last_purchase_price_currency" invisible="1" />
<field
name="show_last_purchase_price_currency_rate"
invisible="1"
/>
<field
name="last_purchase_currency_id"
groups="base.group_multi_currency"
invisible="not show_last_purchase_price_currency"
invisible="not show_last_purchase_price_currency_rate"
/>
<field
name="last_purchase_price_currency"
name="last_purchase_price_currency_rate"
groups="base.group_multi_currency"
invisible="not show_last_purchase_price_currency"
invisible="not show_last_purchase_price_currency_rate"
/>
</group>
</field>
Expand All @@ -43,16 +46,19 @@
<field name="last_purchase_supplier_id" />
<field name="last_purchase_date" />
<field name="last_purchase_price" />
<field name="show_last_purchase_price_currency" invisible="1" />
<field
name="show_last_purchase_price_currency_rate"
invisible="1"
/>
<field
name="last_purchase_currency_id"
groups="base.group_multi_currency"
invisible="not show_last_purchase_price_currency"
invisible="not show_last_purchase_price_currency_rate"
/>
<field
name="last_purchase_price_currency"
name="last_purchase_price_currency_rate"
groups="base.group_multi_currency"
invisible="not show_last_purchase_price_currency"
invisible="not show_last_purchase_price_currency_rate"
/>
</group>
</field>
Expand Down
Loading