Skip to content

Commit

Permalink
[FIX] purchase_duplicate_check: Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
geomer198 committed Jan 17, 2025
1 parent ffcc004 commit 1dda2e5
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 96 deletions.
1 change: 1 addition & 0 deletions purchase_duplicate_check/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from . import purchase_order
from . import purchase_order_line
from . import res_config_settings
110 changes: 27 additions & 83 deletions purchase_duplicate_check/models/purchase_order.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from odoo import _, fields, models
from odoo import models


class PurchaseOrder(models.Model):
Expand All @@ -11,103 +11,47 @@ def _prepare_pending_orders_message(self, product_id):
:param product_id: product.product record id
:return str: message
"""
message = ""
for order in self:
product_line = order.order_line.filtered(
lambda line: line.product_id.id == product_id
)
if not product_line:
continue

product_qty = sum(product_line.mapped("product_qty"))
order_date = order.create_date.date()
message_parts = []
order_lines = self.env["purchase.order.line"].search(
[("product_id", "=", product_id), ("order_id", "in", self.ids)]
)
for line in order_lines:
order = line.order_id
order_href = (
f"<a href='/web#id={order.id}&model={order._name}'>{order.name}</a>"
)
type_ = order.state in ["draft", "sent"] and "RFQ" or "PO"
message += (
f"{type_}: {order_href} date: {order_date} Qty: {product_qty}<br/>"
message_parts.append(
f"{type_}: {order_href} date: {order.create_date.date()} Qty: {line.product_qty}<br/>" # noqa
)
return message
return "".join(message_parts)

def _check_pending_order(self):
if (
not self.env["ir.config_parameter"]
def _is_activity_enabled(self) -> bool:
"""Check if activity for repeating orders is enabled"""
return (
self.env["ir.config_parameter"]
.sudo()
.get_param(
"purchase_duplicate_check.create_activity_repeating_orders", False
"purchase_duplicate_check.allow_create_activity_repeating_orders", False
)
)

def _check_pending_order(self):
"""Check for pending orders and trigger confirmation wizard if needed."""
if self._is_activity_enabled() and not self._context.get(
"skip_rfq_confirmation"
):
return
if not self._context.get("skip_rfq_confirmation"):
return (
self.env["confirmation.wizard"]
.with_context(skip_rfq_confirmation=True)
.confirm_pending_order(self)
)
return

def button_confirm(self):
action = self._check_pending_order()
if action is not None:
return action
return super().button_confirm()


class PurchaseOrderLine(models.Model):
_inherit = "purchase.order.line"

pending_order_ids = fields.Many2many(
"purchase.order",
string="Pending Orders",
compute="_compute_pending_order_ids",
)

def _compute_pending_order_ids(self):
purchase_order_obj = self.env["purchase.order"]
for rec in self:
if rec.product_type != "product":
rec.pending_order_ids = False
continue
rfq_orders = purchase_order_obj.search(
[
("order_line.product_id", "=", rec.product_id.id),
("id", "!=", rec.order_id._origin.id),
"|",
("state", "in", ["draft", "sent"]),
"&",
"&",
("state", "not in", ["draft", "sent"]),
("picking_ids.picking_type_id.code", "=", "incoming"),
("picking_ids.state", "not in", ["done", "cancel"]),
]
)
rec.pending_order_ids = rfq_orders

def _get_order_confirm_message(self):
"""Get order confirmation message for pending orders"""
message = ""
for line in self:
pending_orders = line.pending_order_ids
if not pending_orders:
continue
product_line_msg = pending_orders._prepare_pending_orders_message(
line.product_id.id
)
message += f"""
Product <b>{line.product_id.name}</b><br/>
{product_line_msg}<br/>
"""
return message
"""
Confirm the purchase order.
def action_open_pending_orders(self):
"""Action open pending purchase orders"""
self.ensure_one()
return {
"name": _("Pending Orders"),
"views": [[False, "tree"], [False, "form"]],
"res_model": "purchase.order",
"type": "ir.actions.act_window",
"domain": [("id", "in", self.pending_order_ids.ids)],
"context": {"create": False},
}
:return: action or super
"""
action = self._check_pending_order()
return action or super().button_confirm()
60 changes: 60 additions & 0 deletions purchase_duplicate_check/models/purchase_order_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from odoo import _, fields, models


class PurchaseOrderLine(models.Model):
_inherit = "purchase.order.line"

pending_order_ids = fields.Many2many(
"purchase.order",
string="Pending Orders",
compute="_compute_pending_order_ids",
)

def _compute_pending_order_ids(self):
purchase_order_obj = self.env["purchase.order"]
for rec in self:
if rec.product_type != "product":
rec.pending_order_ids = False
continue
rfq_orders = purchase_order_obj.search(
[
("order_line.product_id", "=", rec.product_id.id),
("id", "!=", rec.order_id._origin.id),
"|",
("state", "in", ["draft", "sent"]),
"&",
"&",
("state", "not in", ["draft", "sent"]),
("picking_ids.picking_type_id.code", "=", "incoming"),
("picking_ids.state", "not in", ["done", "cancel"]),
]
)
rec.pending_order_ids = rfq_orders

def _get_order_confirm_message(self):
"""Get order confirmation message for pending orders"""
message = ""
for line in self:
pending_orders = line.pending_order_ids
if not pending_orders:
continue

Check warning on line 40 in purchase_duplicate_check/models/purchase_order_line.py

View check run for this annotation

Codecov / codecov/patch

purchase_duplicate_check/models/purchase_order_line.py#L40

Added line #L40 was not covered by tests
product_line_msg = pending_orders._prepare_pending_orders_message(
line.product_id.id
)
message += f"""
Product <b>{line.product_id.name}</b><br/>
{product_line_msg}<br/>
"""
return message

def action_open_pending_orders(self):
"""Action open pending purchase orders"""
self.ensure_one()
return {

Check warning on line 53 in purchase_duplicate_check/models/purchase_order_line.py

View check run for this annotation

Codecov / codecov/patch

purchase_duplicate_check/models/purchase_order_line.py#L52-L53

Added lines #L52 - L53 were not covered by tests
"name": _("Pending Orders"),
"views": [[False, "tree"], [False, "form"]],
"res_model": "purchase.order",
"type": "ir.actions.act_window",
"domain": [("id", "in", self.pending_order_ids.ids)],
"context": {"create": False},
}
10 changes: 5 additions & 5 deletions purchase_duplicate_check/models/res_config_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
class ResConfigSettings(models.TransientModel):
_inherit = "res.config.settings"

create_activity_repeating_orders = fields.Boolean(
config_parameter="purchase_duplicate_check.create_activity_repeating_orders"
allow_create_activity_repeating_orders = fields.Boolean(
config_parameter="purchase_duplicate_check.allow_create_activity_repeating_orders"
)
repeating_orders_activity_type_id = fields.Many2one(
comodel_name="mail.activity.type",
config_parameter="purchase_duplicate_check.repeating_orders_activity_type_id",
string="Activity",
)

@api.onchange("create_activity_repeating_orders")
def _onchange_create_activity_repeating_orders(self):
if not self.create_activity_repeating_orders:
@api.onchange("allow_create_activity_repeating_orders")
def _onchange_allow_create_activity_repeating_orders(self):
if not self.allow_create_activity_repeating_orders:
self.repeating_orders_activity_type_id = False

Check warning on line 19 in purchase_duplicate_check/models/res_config_settings.py

View check run for this annotation

Codecov / codecov/patch

purchase_duplicate_check/models/res_config_settings.py#L19

Added line #L19 was not covered by tests
2 changes: 1 addition & 1 deletion purchase_duplicate_check/tests/test_purchase_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def setUp(self):

self.activity_type_email = self.env.ref("mail.mail_activity_data_email")
self.env["ir.config_parameter"].sudo().set_param(
"purchase_duplicate_check.create_activity_repeating_orders", True
"purchase_duplicate_check.allow_create_activity_repeating_orders", True
)
self.env["ir.config_parameter"].sudo().set_param(
"purchase_duplicate_check.repeating_orders_activity_type_id",
Expand Down
6 changes: 3 additions & 3 deletions purchase_duplicate_check/views/res_config_settings_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@
<div name="purchase_setting_container" position="inside">
<div
class="col-12 col-lg-6 o_setting_box"
id="create_activity_repeating_orders"
id="allow_create_activity_repeating_orders"
>
<div class="o_setting_left_pane">
<field name="create_activity_repeating_orders" />
<field name="allow_create_activity_repeating_orders" />
</div>
<div class="o_setting_right_pane">
<span
class="o_form_label"
>Create Activity for Repeating Orders</span>
<div
class="content-group"
attrs="{'invisible': [('create_activity_repeating_orders', '=', False)]}"
attrs="{'invisible': [('allow_create_activity_repeating_orders', '=', False)]}"
>
<div class="row mt16">
<label
Expand Down
15 changes: 11 additions & 4 deletions purchase_duplicate_check/wizard/confirmation_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,26 @@ def confirm_pending_order(self, order):

def _create_po_activity(self, activity_type_id):
res_ids = literal_eval(self.res_ids) if self.res_ids else []
message = self.message

records = self.env[self.res_model].browse(res_ids)
model_id = self.env["ir.model"]._get_id(records._name)
activity_type = self.env["mail.activity.type"].browse(activity_type_id)
user_id = activity_type.default_user_id.id or self.env.user.id
activity_type_id = activity_type.id
activity_vals_list = []
for record in records:
self.env["mail.activity"].create(
activity_vals_list.append(
{
"user_id": activity_type.default_user_id.id or self.env.user.id,
"activity_type_id": activity_type.id,
"user_id": user_id,
"activity_type_id": activity_type_id,
"res_id": record.id,
"res_model_id": model_id,
"note": self.message,
"note": message,
}
)
if activity_vals_list:
self.env["mail.activity"].create(activity_vals_list)

def action_confirm(self):
action_type_id = (
Expand Down

0 comments on commit 1dda2e5

Please sign in to comment.