Skip to content

Commit

Permalink
[WIP][IMP] purchase_request
Browse files Browse the repository at this point in the history
  • Loading branch information
augusto-weiss committed Nov 10, 2022
1 parent 832f41f commit 5b12b09
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 28 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
25 changes: 22 additions & 3 deletions purchase_request/models/purchase_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl-3.0)

from odoo import _, api, exceptions, fields, models
from odoo.tools import float_is_zero


class PurchaseOrder(models.Model):
Expand Down Expand Up @@ -63,10 +64,28 @@ def _purchase_request_line_check(self):
for po in self:
for line in po.order_line:
for request_line in line.purchase_request_lines:
if request_line.sudo().purchase_state == "done":
if request_line.sudo().purchase_state == "done" and not float_is_zero(
line.product_qty, precision_rounding=line.product_uom.rounding
):
raise exceptions.UserError(
_("Purchase Request %s has already been completed")
% request_line.request_id.name
_(
"Purchase Request %s has already been completed for product: %s"
)
% (
request_line.request_id.name,
request_line.product_id.name,
)
)
if (
request_line.sudo().purchased_qty
+ line.product_uom._compute_quantity(
line.product_qty, request_line.sudo().product_uom_id
)
> request_line.sudo().product_qty
):
raise exceptions.Warning(
_("Product %s exceed the Purchase Request %s quantities")
% (line.product_id.name, request_line.request_id.name)
)
return True

Expand Down
74 changes: 53 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,20 @@ class PurchaseRequestLine(models.Model):
copy=False,
)
purchase_state = fields.Selection(
compute="_compute_purchase_state",
[
("draft", "RFQ"),
("sent", "RFQ Sent"),
("to approve", "To Approve"),
("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 +301,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 +325,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 +410,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
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 @@ -285,7 +285,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 5b12b09

Please sign in to comment.