[ADD] helpdesk_mgmt_stock_purchase_origin: Pre-fill purchase order field on helpdesk ticket from an incoming picking#968
Conversation
…eld on helpdesk ticket from an incoming picking
36627af to
6313f23
Compare
marcos-mendez
left a comment
There was a problem hiding this comment.
Automated Review -- Tests Failed
1. Root Cause
The test failure occurs because the action_view_helpdesk_tickets method in stock_picking.py attempts to access self.origin on a stock picking, but for incoming pickings created from a purchase order, self.origin may be None or not match the purchase order name exactly. This causes the search for the purchase order to return no results, and thus default_purchase_order_ids is not set in the action context.
2. Suggested Fix
In helpdesk_mgmt_stock_purchase_origin/models/stock_picking.py, line ~15, the code should check if self.origin is not None and not empty before performing the search. Also, consider using self.env['purchase.order'].search([('name', '=', self.origin)], limit=1) instead of search_read to simplify the logic and avoid unnecessary field fetching.
# File: helpdesk_mgmt_stock_purchase_origin/models/stock_picking.py
# Line ~15
if picking_type_code == "incoming" and self.origin:
origin_po = self.env["purchase.order"].search([("name", "=", self.origin)], limit=1)
if origin_po:
action["context"].update(
{
"default_purchase_order_ids": [Command.set([origin_po.id])]
}
)3. Additional Code Issues
- Redundant
search_readusage: The code usessearch_readto fetch only theidfield, which is inefficient. It should usesearchdirectly. - No fallback handling: If
self.origindoes not match a purchase order name, the code silently does nothing. It would be better to log or handle this case explicitly.
4. Test Improvements
The current test only covers the happy path. Add the following test cases:
- Test with no origin: Ensure that no
default_purchase_order_idsis set whenself.originisNoneor empty. - Test with non-matching origin: Ensure that no
default_purchase_order_idsis set whenself.origindoes not correspond to any purchase order. - Test with outgoing picking: Ensure that the code does not modify the action context for non-incoming pickings.
# In test_helpdesk_mgmt_stock_purchase_origin.py
def test_action_view_helpdesk_tickets_no_origin(self):
# Create an incoming picking with no origin
picking = self.env["stock.picking"].create({
"picking_type_id": self.env.ref("stock.picking_type_in").id,
"origin": None,
})
action = picking.action_view_helpdesk_tickets()
self.assertNotIn("default_purchase_order_ids", action["context"])
def test_action_view_helpdesk_tickets_non_matching_origin(self):
# Create an incoming picking with a non-existent origin
picking = self.env["stock.picking"].create({
"picking_type_id": self.env.ref("stock.picking_type_in").id,
"origin": "NonExistentPO",
})
action = picking.action_view_helpdesk_tickets()
self.assertNotIn("default_purchase_order_ids", action["context"])
def test_action_view_helpdesk_tickets_outgoing_picking(self):
# Ensure outgoing pickings don't affect the context
picking = self.env["stock.picking"].create({
"picking_type_id": self.env.ref("stock.picking_type_out").id,
"origin": self.po.name,
})
action = picking.action_view_helpdesk_tickets()
self.assertNotIn("default_purchase_order_ids", action["context"])These tests align with OCA's patterns using TransactionCase and cover edge cases to ensure robustness.
Reciprocal Review Request
Hi everyone! I found some test failures on this PR and left detailed feedback above. I am happy to discuss or help debug. In the meantime, if any of you get a chance, I would appreciate a look at my open PR(s):
My open PRs across OCA:
- server-tools#3554 [MIG] datetime_formatter: Migration to 18.0
- server-tools#3548 [18.0][MIG] base_kanban_stage: Migration to 18.0
- hr-attendance#262 [16.0][ADD] Hr_attendance_idsecure: iDSecure (ControliD) attendance integration
- stock-logistics-workflow#2276 [16.0][ADD] stock_move_line_devaluation
- stock-logistics-workflow#2275 [16.0][ADD] Stock move line analytic account
- stock-logistics-workflow#2268 [16.0][ADD] stock_move_line_picking_partner
- purchase-workflow#2694 [16.0][IMP]Purchase workflow added to review state & exception fix
Reviewing each other's work helps the whole community move forward. Thank you!
Environment via OCA Neural Reviewer: Minikube + K8s Job + oca-ci/py3.10-odoo16.0 | Odoo 16.0
Automated review by OCA Neural Reviewer + qwen3-coder:30b
Depends on #875