77
88class PurchaseBillLineMatch (models .Model ):
99 _inherit = "purchase.bill.line.match"
10+ _order = "state, product_id, aml_id, pol_id"
1011
1112 reference_description = fields .Char (
1213 string = "Description" ,
@@ -19,25 +20,34 @@ class PurchaseBillLineMatch(models.Model):
1920 readonly = True ,
2021 )
2122
23+ date_order = fields .Datetime (related = "pol_id.order_id.date_order" , readonly = True )
24+
2225 @api .depends ("pol_id" , "product_id" , "display_name" )
2326 def _compute_reference_description (self ):
27+ lang = self .env .user .lang
2428 for rec in self :
25- if rec .pol_id and rec . product_id :
29+ if rec .pol_id :
2630 pol_name = rec .pol_id .name or ""
27- product_name = rec .product_id .display_name or ""
28- if pol_name .startswith (product_name ):
29- remaining = pol_name [len (product_name ) :].strip ()
30- if remaining :
31- rec .reference_description = f"{ product_name } - { remaining } "
32- else :
33- rec .reference_description = product_name
34- else :
35- rec .reference_description = pol_name
36- elif rec .pol_id :
37- rec .reference_description = rec .pol_id .name
31+ # pol.name format: "[ref] Product Name\nExtra description"
32+ # Use only the extra description if present, otherwise translated product name
33+ parts = pol_name .split ("\n " , 1 )
34+ extra = parts [1 ].strip () if len (parts ) > 1 else ""
35+ rec .reference_description = extra or rec .pol_id .product_id .with_context (lang = lang ).display_name
3836 else :
3937 rec .reference_description = rec .display_name
4038
39+ def action_match_lines (self ):
40+ """When opened from an existing draft bill and only PO lines are selected
41+ (no aml_id), add the POLs to the current bill instead of creating a new one.
42+ """
43+ account_move_id = self .env .context .get ("default_account_move_id" )
44+ if account_move_id and not self .aml_id and self .pol_id :
45+ bill = self .env ["account.move" ].browse (account_move_id )
46+ if bill .exists () and bill .state == "draft" :
47+ bill ._add_purchase_order_lines (self .pol_id )
48+ return bill ._get_records_action ()
49+ return super ().action_match_lines ()
50+
4151 def _compute_product_uom_qty (self ):
4252 # Only apply the incompatibility filter when the view was opened
4353 # via the purchase matching action (context flag set by the action).
0 commit comments