2025-01-15 10:16:19 -05:00
from odoo import models , fields , api
2025-01-15 12:10:16 -05:00
2025-01-15 10:16:19 -05:00
class PurchaseOrderLine ( models . Model ) :
2025-01-15 12:10:16 -05:00
_inherit = " purchase.order.line "
2025-01-15 10:16:19 -05:00
2025-01-15 11:59:19 -05:00
requisition_id = fields . Many2one (
2025-01-15 15:52:03 -05:00
comodel_name = " purchase.requisition " ,
string = " Agreement " ,
store = True ,
compute = " _compute_requisition_id " ,
inverse = " _inverse_requisition_id " ,
2025-01-15 12:10:16 -05:00
)
2025-01-15 15:52:03 -05:00
@api.model_create_multi
def create ( self , vals_list ) :
return super ( ) . create ( vals_list )
@api.depends ( " order_id.requisition_id " , " product_id " )
def _compute_requisition_id ( self ) :
for line in self :
2025-01-15 16:23:56 -05:00
customer = line . sale_order_id . partner_id or line . group_id . partner_id
if not customer :
sale_order = line . order_id . _get_sale_orders ( )
if len ( sale_order ) == 1 :
customer = sale_order . partner_id
2025-01-15 15:52:03 -05:00
domain = [
2025-01-15 16:23:56 -05:00
" | " ,
2025-01-15 15:52:03 -05:00
( " requisition_id.vendor_id " , " = " , line . order_id . partner_id . id ) ,
2025-01-15 16:23:56 -05:00
(
2025-01-16 08:25:32 -05:00
" requisition_id.vendor_id.commercial_partner_id " ,
2025-01-15 16:23:56 -05:00
" = " ,
line . order_id . partner_id . id ,
) ,
2025-01-15 15:52:03 -05:00
( " product_id " , " = " , line . product_id . id ) ,
]
requisition = self . order_id . requisition_id
if customer :
domain + = [
" | " ,
( " requisition_id.customer_ids " , " in " , [ customer . id ] ) ,
( " requisition_id.customer_ids " , " = " , False ) ,
]
else :
domain + = [
" | " ,
(
" requisition_id " ,
" = " ,
requisition . id ,
) ,
( " requisition_id.customer_ids " , " = " , False ) ,
]
requisition_lines = self . env [ " purchase.requisition.line " ] . search ( domain )
# If the current order's requisition_id is in the possible lines, use it
2025-01-16 08:25:32 -05:00
req_id = False
2025-01-15 15:52:03 -05:00
if line . order_id . requisition_id and requisition_lines :
2025-01-16 08:25:32 -05:00
req_id = requisition_lines . filtered (
2025-01-15 15:52:03 -05:00
lambda req_line : req_line . requisition_id
== line . order_id . requisition_id
2025-01-15 17:38:00 -05:00
) . requisition_id
2025-01-16 08:25:32 -05:00
if not req_id and requisition_lines :
req_id = requisition_lines [ 0 ] . requisition_id
line . requisition_id = req_id
# TODO: Try to guess based on the other lines on the PO if there is no linked SO line but there are other SOs in the purchase order's _get_sale_orders()
2025-01-15 15:52:03 -05:00
def _inverse_requisition_id ( self ) :
pass
2025-01-15 11:59:19 -05:00
@api.model
2025-01-15 12:10:16 -05:00
def _prepare_purchase_order_line (
self , product_id , product_qty , product_uom , company_id , supplier , po
) :
res = super ( ) . _prepare_purchase_order_line (
product_id , product_qty , product_uom , company_id , supplier , po
)
2025-01-15 11:59:19 -05:00
# Si on a une réquisition, on ne veut pas regrouper les lignes
if po . requisition_id :
2025-01-15 12:10:16 -05:00
existing_line = po . order_line . filtered (
lambda l : l . product_id == product_id
and l . requisition_id == po . requisition_id
)
2025-01-15 11:59:19 -05:00
if existing_line :
# Créer une nouvelle ligne au lieu de mettre à jour la quantité
2025-01-15 12:10:16 -05:00
res [ " product_qty " ] = product_qty
2025-01-15 11:59:19 -05:00
else :
2025-01-15 12:10:16 -05:00
res [ " product_qty " ] = product_qty
2025-01-15 11:59:19 -05:00
return res
2025-01-15 12:10:16 -05:00
def _find_candidate (
self ,
product_id ,
product_qty ,
product_uom ,
location_id ,
name ,
origin ,
company_id ,
values ,
) :
2025-01-15 11:59:19 -05:00
return super (
2025-01-15 12:10:16 -05:00
PurchaseOrderLine , self . filtered ( lambda line : not line . requisition_id )
) . _find_candidate (
product_id ,
product_qty ,
product_uom ,
location_id ,
name ,
origin ,
company_id ,
values ,
2025-01-15 11:59:19 -05:00
)