Skip to content

Commit

Permalink
Merge commit 'refs/pull/1546/head' of https://github.com/oca/purchase…
Browse files Browse the repository at this point in the history
…-workflow into 13.0-5121
  • Loading branch information
docker-odoo committed Jan 11, 2024
2 parents c112ffb + 330b3f0 commit 5c10df1
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 27 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: "3.8"
- name: Get python version
run: echo "PY=$(python -VV | sha256sum | cut -d' ' -f1)" >> $GITHUB_ENV
python-version: "3.9"
- uses: actions/cache@v1
with:
path: ~/.cache/pre-commit
Expand Down
75 changes: 54 additions & 21 deletions purchase_request/models/purchase_request_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,12 @@ class PurchaseRequestLine(models.Model):
)

purchased_qty = fields.Float(
string="Quantity in RFQ or PO",
string="Purchased Quantity",
digits="Product Unit of Measure",
compute="_compute_purchased_qty",
)
rfq_qty = fields.Float(
string="Quantity in RFQ",
digits="Product Unit of Measure",
compute="_compute_purchased_qty",
)
Expand All @@ -117,11 +122,21 @@ class PurchaseRequestLine(models.Model):
copy=False,
)
purchase_state = fields.Selection(
compute="_compute_purchase_state",
[
("draft", "RFQ"),
("sent", "RFQ Sent"),
("to approve", "To Approve"),
("approved", "Approved"),
("partially", "Partially Purchased"),
("purchase", "Purchased"),
("done", "Locked"),
("cancel", "Cancelled"),
],
string="Purchase Status",
selection=lambda self: self.env["purchase.order"]._fields["state"].selection,
compute="_compute_purchase_state",
store=True,
)

move_dest_ids = fields.One2many(
comodel_name="stock.move",
inverse_name="created_purchase_request_line_id",
Expand Down Expand Up @@ -287,14 +302,11 @@ def _compute_supplier_id(self):
@api.onchange("product_id")
def onchange_product_id(self):
if self.product_id:
name = self.product_id.name
if self.product_id.code:
name = "[{}] {}".format(self.product_id.code, name)
self.name = self.product_id.display_name
if self.product_id.description_purchase:
name += "\n" + self.product_id.description_purchase
self.product_uom_id = self.product_id.uom_id.id
self.name += " " + self.product_id.description_purchase
self.product_uom_id = self.product_id.uom_po_id.id
self.product_qty = 1
self.name = name

def do_cancel(self):
"""Actions to perform when cancelling a purchase request line."""
Expand All @@ -314,31 +326,52 @@ def write(self, vals):
def _compute_purchased_qty(self):
for rec in self:
rec.purchased_qty = 0.0
for line in rec.purchase_lines.filtered(lambda x: x.state != "cancel"):
if rec.product_uom_id and line.product_uom != rec.product_uom_id:
rec.rfq_qty = 0.0
for line in rec.purchase_lines:
if line.state in ["purchase", "done"]:
rec.purchased_qty += line.product_uom._compute_quantity(
line.product_qty, rec.product_uom_id
)
else:
rec.purchased_qty += line.product_qty
rec.rfq_qty += line.product_uom._compute_quantity(
line.product_qty, rec.product_uom_id
)

@api.depends("purchase_lines.state", "purchase_lines.order_id.state")
def _compute_purchase_state(self):
for rec in self:
temp_purchase_state = False
if rec.purchase_lines:
if any([po_line.state == "done" for po_line in rec.purchase_lines]):
temp_purchase_state = "done"
elif all([po_line.state == "cancel" for po_line in rec.purchase_lines]):
temp_purchase_state = "cancel"
qty_done = sum(
[
po_line.product_uom._compute_quantity(
po_line.product_qty, rec.product_uom_id
)
for po_line in rec.purchase_lines.filtered(
lambda po: po.state == "done"
)
]
)
temp_purchase_state = (
"done" if qty_done >= rec.product_qty else "partially"
)
elif any(
[po_line.state == "purchase" for po_line in rec.purchase_lines]
):
temp_purchase_state = "purchase"
elif any(
[po_line.state == "to approve" for po_line in rec.purchase_lines]
):
temp_purchase_state = "to approve"
qty_purchased = sum(
[
po_line.product_uom._compute_quantity(
po_line.product_qty, rec.product_uom_id
)
for po_line in rec.purchase_lines.filtered(
lambda po: po.state == "purchase"
)
]
)
temp_purchase_state = (
"purchase" if qty_purchased >= rec.product_qty else "partially"
)
elif any([po_line.state == "sent" for po_line in rec.purchase_lines]):
temp_purchase_state = "sent"
elif all(
Expand Down Expand Up @@ -378,7 +411,7 @@ def _calc_new_qty(self, request_line, po_line=None, new_pr_line=False):
rl_qty = 0.0
# Recompute quantity by adding existing running procurements.
if new_pr_line:
rl_qty = po_line.product_uom_qty
rl_qty = po_line.product_qty
else:
for prl in po_line.purchase_request_lines:
for alloc in prl.purchase_request_allocation_ids:
Expand Down
9 changes: 7 additions & 2 deletions purchase_request/tests/test_purchase_request_to_rfq.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,15 @@ def test_purchase_request_to_purchase_rfq_multiple_po(self):
item.onchange_product_id()
wiz_id.make_purchase_order()
self.assertEquals(
purchase_request_line1.purchased_qty, 1.0, "Should be a quantity of 1"
purchase_request_line1.rfq_qty, 1.0, "Quantity in RFQ should be 1"
)
self.assertEquals(
purchase_request_line2.purchased_qty, 1.0, "Should be a quantity of 1"
purchase_request_line2.rfq_qty, 1.0, "Quantity in RFQ should be 1"
)
purchase_request_line1.purchase_lines.order_id.button_confirm()
purchase_request_line1._compute_purchased_qty()
self.assertEquals(
purchase_request_line1.purchased_qty, 1.0, "Purchased quantity should be 1"
)

def test_purchase_request_to_purchase_rfq_multiple_PO_purchaseUoM(self):
Expand Down
2 changes: 2 additions & 0 deletions purchase_request/views/purchase_request_line_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<field name="date_required" />
<field name="estimated_cost" widget="monetary" />
<field name="currency_id" invisible="1" />
<field name="rfq_qty" />
<field name="purchased_qty" />
<field name="purchase_state" />
<field name="product_id" />
Expand Down Expand Up @@ -157,6 +158,7 @@
<notebook>
<page name="purchase_lines" string="Purchase Order Lines">
<group>
<field name="rfq_qty" />
<field name="purchased_qty" />
<field name="purchase_state" />
</group>
Expand Down
2 changes: 2 additions & 0 deletions purchase_request/views/purchase_request_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@
/>
<field name="cancelled" invisible="1" />
<field name="is_editable" invisible="1" />
<field name="rfq_qty" />
<field name="purchased_qty" />
<field name="purchase_state" />
</tree>
Expand Down Expand Up @@ -274,6 +275,7 @@
>
<group>
<group name="purchase_state">
<field name="rfq_qty" />
<field name="purchased_qty" />
<field name="purchase_state" />
<field name="qty_in_progress" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def make_purchase_order(self):
po_line_data["name"] = item.name
po_line = po_line_obj.create(po_line_data)
po_line_product_uom_qty = po_line.product_uom._compute_quantity(
po_line.product_uom_qty, alloc_uom
po_line.product_qty, alloc_uom
)
wizard_product_uom_qty = wizard_uom._compute_quantity(
item.product_qty, alloc_uom
Expand Down

0 comments on commit 5c10df1

Please sign in to comment.