purchase_customer_requisition: prices correctly set for new lines
This commit is contained in:
parent
245610362d
commit
1122f71628
2 changed files with 48 additions and 37 deletions
|
|
@ -1,4 +1,7 @@
|
|||
from odoo import models, fields, api
|
||||
import logging
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class PurchaseOrderLine(models.Model):
|
||||
|
|
@ -12,9 +15,26 @@ class PurchaseOrderLine(models.Model):
|
|||
inverse="_inverse_requisition_id",
|
||||
)
|
||||
|
||||
requisition_line_id = fields.Many2one(
|
||||
comodel_name="purchase.requisition.line",
|
||||
string="Requisition Line",
|
||||
compute="_compute_requisition_line_id",
|
||||
)
|
||||
|
||||
@api.model_create_multi
|
||||
def create(self, vals_list):
|
||||
return super().create(vals_list)
|
||||
res = super().create(vals_list)
|
||||
for line in res.filtered("requisition_id"):
|
||||
line.price_unit = line.requisition_line_id.price_unit
|
||||
return res
|
||||
|
||||
@api.depends("requisition_id")
|
||||
def _compute_requisition_line_id(self):
|
||||
for line in self:
|
||||
candidates = line.requisition_id.line_ids.filtered(
|
||||
lambda req_line: req_line.product_id == line.product_id
|
||||
)
|
||||
line.requisition_line_id = candidates[0] if candidates else False
|
||||
|
||||
@api.depends("order_id.requisition_id", "product_id")
|
||||
def _compute_requisition_id(self):
|
||||
|
|
@ -62,31 +82,10 @@ class PurchaseOrderLine(models.Model):
|
|||
if not req_id and requisition_lines:
|
||||
req_id = requisition_lines[0].requisition_id
|
||||
line.requisition_id = req_id
|
||||
# TODO: Try to guess based on the other lines on the PO if there is no linked SO line but there are other SOs in the purchase order's _get_sale_orders()
|
||||
|
||||
def _inverse_requisition_id(self):
|
||||
pass
|
||||
|
||||
@api.model
|
||||
def _prepare_purchase_order_line(
|
||||
self, product_id, product_qty, product_uom, company_id, supplier, po
|
||||
):
|
||||
res = super()._prepare_purchase_order_line(
|
||||
product_id, product_qty, product_uom, company_id, supplier, po
|
||||
)
|
||||
# Si on a une réquisition, on ne veut pas regrouper les lignes
|
||||
if po.requisition_id:
|
||||
existing_line = po.order_line.filtered(
|
||||
lambda l: l.product_id == product_id
|
||||
and l.requisition_id == po.requisition_id
|
||||
)
|
||||
if existing_line:
|
||||
# Créer une nouvelle ligne au lieu de mettre à jour la quantité
|
||||
res["product_qty"] = product_qty
|
||||
else:
|
||||
res["product_qty"] = product_qty
|
||||
return res
|
||||
|
||||
def _find_candidate(
|
||||
self,
|
||||
product_id,
|
||||
|
|
|
|||
|
|
@ -1,3 +1,6 @@
|
|||
from odoo.addons.sale_purchase_inter_company_rules.models.purchase_order import (
|
||||
purchase_order,
|
||||
)
|
||||
from odoo.tests import TransactionCase, tagged
|
||||
from odoo import Command, fields
|
||||
from datetime import timedelta
|
||||
|
|
@ -113,6 +116,23 @@ class TestPurchaseOrder(TransactionCase):
|
|||
self.assertEqual(purchase_line.requisition_id, self.agreement_1)
|
||||
|
||||
def test_competing_sale_orders_get_two_lines(self):
|
||||
purchase_order = self._generate_2_sales_1_purchase_clients_1_3()
|
||||
self.assertEqual(len(purchase_order.order_line), 2)
|
||||
self.assertEqual(
|
||||
purchase_order.order_line[0].requisition_id,
|
||||
self.agreement_1,
|
||||
f"PO line for Partner 1 should have requisition {self.agreement_1.name}, not {purchase_order.order_line[0].requisition_id.name}",
|
||||
)
|
||||
self.assertEqual(
|
||||
purchase_order.order_line[1].requisition_id,
|
||||
self.agreement_2,
|
||||
f"PO line for Partner 2 should have requisition {self.agreement_2.name}, not {purchase_order.order_line[1].requisition_id.name}"
|
||||
f"PO line has sale order {purchase_order.order_line[1].sale_order_id} and sale line {purchase_order.order_line[1].sale_line_id}"
|
||||
f"PO has sales orders {purchase_order._get_sale_orders()}"
|
||||
f"PO has requisition {purchase_order.requisition_id}",
|
||||
)
|
||||
|
||||
def _generate_2_sales_1_purchase_clients_1_3(self):
|
||||
sale_order_1 = self.env["sale.order"].create(
|
||||
{
|
||||
"partner_id": self.client_1.id,
|
||||
|
|
@ -141,19 +161,11 @@ class TestPurchaseOrder(TransactionCase):
|
|||
}
|
||||
)
|
||||
sale_order_2.action_confirm()
|
||||
|
||||
purchase_order = sale_order_1._get_purchase_orders()[0]
|
||||
self.assertEqual(len(purchase_order.order_line), 2)
|
||||
self.assertEqual(
|
||||
purchase_order.order_line[0].requisition_id,
|
||||
self.agreement_1,
|
||||
f"PO line for Partner 1 should have requisition {self.agreement_1.name}, not {purchase_order.order_line[0].requisition_id.name}",
|
||||
)
|
||||
self.assertEqual(
|
||||
purchase_order.order_line[1].requisition_id,
|
||||
self.agreement_2,
|
||||
f"PO line for Partner 2 should have requisition {self.agreement_2.name}, not {purchase_order.order_line[1].requisition_id.name}"
|
||||
f"PO line has sale order {purchase_order.order_line[1].sale_order_id} and sale line {purchase_order.order_line[1].sale_line_id}"
|
||||
f"PO has sales orders {purchase_order._get_sale_orders()}"
|
||||
f"PO has requisition {purchase_order.requisition_id}",
|
||||
)
|
||||
return purchase_order
|
||||
|
||||
def test_lines_from_multiple_sales_get_correct_pricing(self):
|
||||
purchase_order = self._generate_2_sales_1_purchase_clients_1_3()
|
||||
|
||||
self.assertEqual(purchase_order.order_line[0].price_unit, 1000)
|
||||
self.assertEqual(purchase_order.order_line[1].price_unit, 1500)
|
||||
|
|
|
|||
Loading…
Reference in a new issue