working on purchase_customer_requisition
This commit is contained in:
parent
99dca038d3
commit
ed848863f9
2 changed files with 89 additions and 42 deletions
|
|
@ -4,15 +4,12 @@ from odoo import models, fields, api
|
||||||
class PurchaseOrderLine(models.Model):
|
class PurchaseOrderLine(models.Model):
|
||||||
_inherit = "purchase.order.line"
|
_inherit = "purchase.order.line"
|
||||||
|
|
||||||
customer_ids = fields.Many2many(
|
|
||||||
# related='order_id.requisition_id.customer_ids',
|
|
||||||
string="Customers",
|
|
||||||
store=True,
|
|
||||||
help="Customers associated with this purchase order line",
|
|
||||||
)
|
|
||||||
|
|
||||||
requisition_id = fields.Many2one(
|
requisition_id = fields.Many2one(
|
||||||
related="order_id.requisition_id", string="Purchase Requisition", store=True
|
comodel_name="purchase.requisition",
|
||||||
|
string="Agreement",
|
||||||
|
store=True,
|
||||||
|
compute="_compute_requisition_id",
|
||||||
|
inverse="_inverse_requisition_id",
|
||||||
)
|
)
|
||||||
|
|
||||||
requisition_name = fields.Char(
|
requisition_name = fields.Char(
|
||||||
|
|
@ -20,13 +17,49 @@ class PurchaseOrderLine(models.Model):
|
||||||
string="Agreement",
|
string="Agreement",
|
||||||
)
|
)
|
||||||
|
|
||||||
def _get_product_purchase_description(self, product_lang):
|
@api.model_create_multi
|
||||||
name = super()._get_product_purchase_description(product_lang)
|
def create(self, vals_list):
|
||||||
if self.requisition_id:
|
return super().create(vals_list)
|
||||||
name = f"[REQ/{self.requisition_id.name}] {name}"
|
|
||||||
if self.customer_ids:
|
@api.depends("order_id.requisition_id", "product_id")
|
||||||
name = f"[{', '.join(self.customer_ids.mapped('name'))}] {name}"
|
def _compute_requisition_id(self):
|
||||||
return name
|
for line in self:
|
||||||
|
customer = line.sale_order_id.partner_id
|
||||||
|
domain = [
|
||||||
|
("requisition_id.vendor_id", "=", line.order_id.partner_id.id),
|
||||||
|
("product_id", "=", line.product_id.id),
|
||||||
|
]
|
||||||
|
requisition = self.order_id.requisition_id
|
||||||
|
if customer:
|
||||||
|
domain += [
|
||||||
|
"|",
|
||||||
|
("requisition_id.customer_ids", "in", [customer.id]),
|
||||||
|
("requisition_id.customer_ids", "=", False),
|
||||||
|
]
|
||||||
|
else:
|
||||||
|
domain += [
|
||||||
|
"|",
|
||||||
|
(
|
||||||
|
"requisition_id",
|
||||||
|
"=",
|
||||||
|
requisition.id,
|
||||||
|
),
|
||||||
|
("requisition_id.customer_ids", "=", False),
|
||||||
|
]
|
||||||
|
requisition_lines = self.env["purchase.requisition.line"].search(domain)
|
||||||
|
# If the current order's requisition_id is in the possible lines, use it
|
||||||
|
if line.order_id.requisition_id and requisition_lines:
|
||||||
|
line.requisition_id = requisition_lines.filtered(
|
||||||
|
lambda req_line: req_line.requisition_id
|
||||||
|
== line.order_id.requisition_id
|
||||||
|
)
|
||||||
|
if not line.requisition_id and requisition_lines:
|
||||||
|
line.requisition_id = requisition_lines[0]
|
||||||
|
else:
|
||||||
|
line.requisition_id = False
|
||||||
|
|
||||||
|
def _inverse_requisition_id(self):
|
||||||
|
pass
|
||||||
|
|
||||||
@api.model
|
@api.model
|
||||||
def _prepare_purchase_order_line(
|
def _prepare_purchase_order_line(
|
||||||
|
|
@ -48,19 +81,6 @@ class PurchaseOrderLine(models.Model):
|
||||||
res["product_qty"] = product_qty
|
res["product_qty"] = product_qty
|
||||||
return res
|
return res
|
||||||
|
|
||||||
@api.model_create_multi
|
|
||||||
def create(self, vals_list):
|
|
||||||
lines = super().create(vals_list)
|
|
||||||
for line in lines:
|
|
||||||
if line.name:
|
|
||||||
name = line.name
|
|
||||||
if line.requisition_id:
|
|
||||||
name = f"[REQ/{line.requisition_id.name}] {name}"
|
|
||||||
if line.customer_ids:
|
|
||||||
name = f"[{', '.join(line.customer_ids.mapped('name'))}] {name}"
|
|
||||||
line.name = name
|
|
||||||
return lines
|
|
||||||
|
|
||||||
def _find_candidate(
|
def _find_candidate(
|
||||||
self,
|
self,
|
||||||
product_id,
|
product_id,
|
||||||
|
|
|
||||||
|
|
@ -8,34 +8,59 @@ class TestPurchaseOrder(TransactionCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
super().setUpClass()
|
super().setUpClass()
|
||||||
|
cls.mto_route = cls.env.ref("stock.route_warehouse0_mto")
|
||||||
|
cls.buy_route = cls.env.ref("purchase_stock.route_warehouse0_buy")
|
||||||
|
cls.mto_route.active = True
|
||||||
cls.supplier = cls.env.ref("base.res_partner_18")
|
cls.supplier = cls.env.ref("base.res_partner_18")
|
||||||
cls.client_1 = cls.env.ref("base.res_partner_2")
|
cls.client_1 = cls.env.ref("base.res_partner_2")
|
||||||
cls.client_2 = cls.env.ref("base.res_partner_3")
|
cls.client_2 = cls.env.ref("base.res_partner_3")
|
||||||
cls.client_3 = cls.env.ref("base.res_partner_4")
|
cls.client_3 = cls.env.ref("base.res_partner_4")
|
||||||
cls.product_1 = cls.env.ref("product.product_delivery_01")
|
cls.product_1 = cls.env["product.product"].create(
|
||||||
cls.product_2 = cls.env.ref("product.product_delivery_02")
|
{
|
||||||
|
"name": "SuperProduct",
|
||||||
|
"is_storable": True,
|
||||||
|
"route_ids": [(6, 0, (cls.mto_route + cls.buy_route).ids)],
|
||||||
|
"seller_ids": [
|
||||||
|
(
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
{
|
||||||
|
"partner_id": cls.supplier.id,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
cls.env["stock.rule"].search(
|
cls.product_2 = cls.env["product.product"].create(
|
||||||
[
|
{
|
||||||
("action", "=", "pull"),
|
"name": "SuperProduct2",
|
||||||
(
|
"is_storable": True,
|
||||||
"location_dest_id",
|
"route_ids": [(6, 0, (cls.mto_route + cls.buy_route).ids)],
|
||||||
"=",
|
"seller_ids": [
|
||||||
cls.env.ref("stock.stock_location_customers").id,
|
(
|
||||||
),
|
0,
|
||||||
]
|
0,
|
||||||
).write({"procure_method": "mts_else_mto"})
|
{
|
||||||
|
"partner_id": cls.supplier.id,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
cls.env["product.supplierinfo"].create(
|
cls.env["product.supplierinfo"].create(
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
"partner_id": cls.supplier.id,
|
"partner_id": cls.supplier.id,
|
||||||
"product_id": cls.product_1.id,
|
"product_id": cls.product_1.id,
|
||||||
|
"product_tmpl_id": cls.product_1.product_tmpl_id.id,
|
||||||
"price": 2000,
|
"price": 2000,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"partner_id": cls.supplier.id,
|
"partner_id": cls.supplier.id,
|
||||||
"product_id": cls.product_2.id,
|
"product_id": cls.product_2.id,
|
||||||
|
"product_tmpl_id": cls.product_1.product_tmpl_id.id,
|
||||||
"price": 3000,
|
"price": 3000,
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
@ -44,7 +69,7 @@ class TestPurchaseOrder(TransactionCase):
|
||||||
cls.agreement_1 = cls.env["purchase.requisition"].create(
|
cls.agreement_1 = cls.env["purchase.requisition"].create(
|
||||||
{
|
{
|
||||||
"name": "ATRACK 123",
|
"name": "ATRACK 123",
|
||||||
"vendor_id": 1,
|
"vendor_id": cls.supplier.id,
|
||||||
"customer_ids": [Command.set([cls.client_1.id, cls.client_2.id])],
|
"customer_ids": [Command.set([cls.client_1.id, cls.client_2.id])],
|
||||||
"line_ids": [
|
"line_ids": [
|
||||||
Command.create(
|
Command.create(
|
||||||
|
|
@ -70,7 +95,7 @@ class TestPurchaseOrder(TransactionCase):
|
||||||
cls.agreement_2 = cls.env["purchase.requisition"].create(
|
cls.agreement_2 = cls.env["purchase.requisition"].create(
|
||||||
{
|
{
|
||||||
"name": "ATRACK 456",
|
"name": "ATRACK 456",
|
||||||
"vendor_id": 1,
|
"vendor_id": cls.supplier.id,
|
||||||
"customer_ids": [Command.set([cls.client_3.id])],
|
"customer_ids": [Command.set([cls.client_3.id])],
|
||||||
"line_ids": [
|
"line_ids": [
|
||||||
Command.create(
|
Command.create(
|
||||||
|
|
@ -104,4 +129,6 @@ class TestPurchaseOrder(TransactionCase):
|
||||||
sale_order.action_confirm()
|
sale_order.action_confirm()
|
||||||
|
|
||||||
purchase_line = sale_order.order_line[0].purchase_line_ids
|
purchase_line = sale_order.order_line[0].purchase_line_ids
|
||||||
|
self.assertTrue(purchase_line)
|
||||||
|
self.assertEqual(purchase_line.order_id.partner_id, self.supplier)
|
||||||
self.assertEqual(purchase_line.requisition_id, self.agreement_1)
|
self.assertEqual(purchase_line.requisition_id, self.agreement_1)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue