98 lines
3.9 KiB
Python
98 lines
3.9 KiB
Python
from odoo import api, fields, models, _
|
|
from odoo.exceptions import UserError
|
|
from odoo.tools.misc import clean_context
|
|
from markupsafe import Markup
|
|
|
|
class ApprovalRequest(models.Model):
|
|
_inherit = 'approval.request'
|
|
|
|
picking_ids = fields.Many2many('stock.picking',compute='_compute_picking_ids', string='Transfers')
|
|
picking_count = fields.Integer(compute='_compute_picking_count')
|
|
wh_move_created = fields.Boolean("WH Move Created", copy=False)
|
|
request_status = fields.Selection(selection_add=[('wh_sent', 'WH Sent')])
|
|
|
|
def _compute_picking_ids(self):
|
|
for request in self:
|
|
request.picking_ids = self.env['stock.picking'].search([('origin', '=', request.name)]) if request.name else False
|
|
|
|
@api.depends('picking_ids')
|
|
def _compute_picking_count(self):
|
|
for request in self:
|
|
request.picking_count = len(request.picking_ids)
|
|
|
|
def _compute_request_status(self):
|
|
super()._compute_request_status()
|
|
for request in self:
|
|
if request.request_status == 'approved' and request.wh_move_created:
|
|
request.request_status = 'wh_sent'
|
|
|
|
def action_create_wh_prep_move(self):
|
|
self.ensure_one()
|
|
if self.picking_count > 0:
|
|
return
|
|
|
|
op_type_name = "Brigjend Katamso: WH to Prep (Send)"
|
|
dest_loc_name = "Physical Locations/Inter-warehouse transit"
|
|
|
|
# Find Operation Type
|
|
picking_type = self.env['stock.picking.type'].search([('name', '=', op_type_name)], limit=1)
|
|
if not picking_type:
|
|
# Fallback: search by display_name if name isn't exact
|
|
picking_types = self.env['stock.picking.type'].search([])
|
|
for pt in picking_types:
|
|
if op_type_name in pt.display_name:
|
|
picking_type = pt
|
|
break
|
|
|
|
if not picking_type:
|
|
raise UserError(_("Operation Type '%s' not found. Please contact administrator.") % op_type_name)
|
|
|
|
# Find Destination Location
|
|
dest_loc = self.env['stock.location'].search([('complete_name', '=', dest_loc_name)], limit=1)
|
|
if not dest_loc:
|
|
raise UserError(_("Destination Location '%s' not found. Please contact administrator.") % dest_loc_name)
|
|
|
|
if not self.product_line_ids:
|
|
raise UserError(_("You must select products to create a transfer."))
|
|
|
|
picking_vals = {
|
|
'picking_type_id': picking_type.id,
|
|
'location_id': picking_type.default_location_src_id.id,
|
|
'location_dest_id': dest_loc.id,
|
|
'origin': self.name,
|
|
'partner_id': self.partner_id.id,
|
|
'move_ids': [],
|
|
}
|
|
|
|
for line in self.product_line_ids:
|
|
move_vals = {
|
|
'description_picking': line.description,
|
|
'product_id': line.product_id.id,
|
|
'product_uom_qty': line.quantity,
|
|
'product_uom': line.product_uom_id.id,
|
|
'location_id': picking_type.default_location_src_id.id,
|
|
'location_dest_id': dest_loc.id,
|
|
}
|
|
picking_vals['move_ids'].append((0, 0, move_vals))
|
|
|
|
picking = self.env['stock.picking'].create(picking_vals)
|
|
picking.action_confirm() # Confirm the picking to reserve stock if possible
|
|
|
|
self.wh_move_created = True
|
|
|
|
msg = Markup(_("Created Inventory Transfer: <a href='#' data-oe-model='stock.picking' data-oe-id='%d'>%s</a>")) % (picking.id, picking.name)
|
|
self.message_post(body=msg)
|
|
|
|
def action_open_wh_prep_move(self):
|
|
self.ensure_one()
|
|
action = {
|
|
'name': _('Transfers'),
|
|
'type': 'ir.actions.act_window',
|
|
'res_model': 'stock.picking',
|
|
'view_mode': 'list,form',
|
|
'domain': [('origin', '=', self.name)],
|
|
'context': clean_context(self.env.context),
|
|
}
|
|
return action
|
|
|