104 lines
4.4 KiB
Python
104 lines
4.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
from odoo import fields, models, api
|
|
|
|
|
|
class StockMove(models.Model):
|
|
_inherit = 'stock.move'
|
|
|
|
# Field to link inventory move to a purchase order
|
|
purchase_order_id = fields.Many2one(
|
|
'purchase.order',
|
|
string='Linked Purchase Order',
|
|
ondelete='set null',
|
|
help='Purchase order linked to this subcontracting inventory move'
|
|
)
|
|
|
|
# Computed field to show count of linked purchase orders for smart button
|
|
linked_purchase_order_count = fields.Integer(
|
|
string='Linked Purchase Orders Count',
|
|
compute='_compute_linked_purchase_order_count'
|
|
)
|
|
|
|
@api.depends('purchase_order_id')
|
|
def _compute_linked_purchase_order_count(self):
|
|
for move in self:
|
|
move.linked_purchase_order_count = 1 if move.purchase_order_id else 0
|
|
|
|
def action_view_linked_purchase_order(self):
|
|
"""Action to view the linked purchase order from the inventory move"""
|
|
self.ensure_one()
|
|
if self.purchase_order_id:
|
|
return {
|
|
'name': 'Linked Purchase Order',
|
|
'type': 'ir.actions.act_window',
|
|
'view_mode': 'form',
|
|
'res_model': 'purchase.order',
|
|
'res_id': self.purchase_order_id.id,
|
|
'target': 'current',
|
|
}
|
|
return {
|
|
'name': 'Create Purchase Order',
|
|
'type': 'ir.actions.act_window',
|
|
'view_mode': 'form',
|
|
'res_model': 'purchase.order',
|
|
'context': {
|
|
'default_order_line': [(0, 0, {
|
|
'product_id': self.product_id.id,
|
|
'product_qty': self.product_uom_qty,
|
|
'product_uom': self.product_uom.id,
|
|
})],
|
|
},
|
|
'target': 'current',
|
|
}
|
|
|
|
def _action_confirm(self, merge=True, merge_into=False):
|
|
"""Override to handle our custom subcontracting flow"""
|
|
# Call the super method first to maintain standard functionality
|
|
result = super()._action_confirm(merge=merge, merge_into=merge_into)
|
|
|
|
# For moves that are subcontracting related but not linked to purchase orders yet
|
|
# we want to allow them to exist in the system without creating manufacturing orders immediately
|
|
for move in self:
|
|
if move.is_subcontract and not move.purchase_order_id:
|
|
# This is a move created first without a purchase order - allow it to exist
|
|
# We don't want to trigger the standard subcontracting flow here
|
|
# Prevent creating MOs for moves that are not linked to a purchase order yet
|
|
continue
|
|
|
|
return result
|
|
|
|
def _action_assign(self):
|
|
"""Override to handle custom subcontracting flow"""
|
|
# For moves that are subcontracting but not linked to purchase orders yet,
|
|
# we may want to handle them differently
|
|
subcontract_moves_without_po = self.filtered(lambda m: m.is_subcontract and not m.purchase_order_id)
|
|
other_moves = self - subcontract_moves_without_po
|
|
|
|
# Process other moves normally
|
|
if other_moves:
|
|
super(StockMove, other_moves)._action_assign()
|
|
|
|
# For subcontract moves without PO, we can decide how to handle them
|
|
# For now, just call the super method
|
|
if subcontract_moves_without_po:
|
|
super(StockMove, subcontract_moves_without_po)._action_assign()
|
|
|
|
return True
|
|
|
|
def write(self, values):
|
|
"""Override write to handle purchase order linking"""
|
|
# If we're linking a purchase order to a move, update the reverse link
|
|
if 'purchase_order_id' in values:
|
|
purchase_order = self.env['purchase.order'].browse(values['purchase_order_id'])
|
|
if purchase_order.exists():
|
|
# Add this move to the purchase order's subcontracting moves
|
|
for move in self:
|
|
if move.is_subcontract and move not in purchase_order.subcontracting_move_ids:
|
|
# Add to the existing list of moves
|
|
current_moves = purchase_order.subcontracting_move_ids.ids
|
|
current_moves.append(move.id)
|
|
purchase_order.write({'subcontracting_move_ids': [(6, 0, current_moves)]})
|
|
|
|
return super().write(values) |