from odoo import fields, models, api from odoo.exceptions import UserError, ValidationError class ShelfLocationWizard(models.TransientModel): _name = 'sf.shelf.location.wizard' _description = '货位变更' name = fields.Char('') lot_id = fields.Many2one('stock.lot', string="序列号", readonly=True) current_location_id = fields.Many2one('stock.location', string='所属库区', readonly=True) current_shelf_id = fields.Many2one('sf.shelf', string='当前货架', readonly=True) current_barcode_id = fields.Many2one('sf.shelf.location', string='当前货位编码', readonly=True) current_name = fields.Char('当前货位名称', readonly=True) current_product_id = fields.Many2one('product.product', string='产品', readonly=True) current_product_sn_ids = fields.Many2many('sf.shelf.location.lot', 'shelf_location_wizard', string='产品批次号', readonly=True) destination_location_id = fields.Many2one('stock.location', string='目标库区', compute='_compute_destination_name') destination_shelf_id = fields.Many2one('sf.shelf', string='目标货架', compute='_compute_destination_name') destination_barcode_id = fields.Many2one('sf.shelf.location', string='目标货位编码', required=True, domain="[('product_id', 'in', (False, current_product_id))]") destination_name = fields.Char('目标货位名称', compute='_compute_destination_name') destination_product_sn_ids = fields.Many2many('sf.shelf.location.lot', 'shelf_location_wizard', string='批次号', domain="[('shelf_location_id', '=', current_barcode_id)]") def return_domain(self): val = [('location_status', '=', '空闲')] if self.current_product_id: val = ['|', ('location_status', '=', '空闲'), ('product_id', '=', self.current_product_id)] if self.destination_shelf_id: val.append(('shelf_id', '=', self.destination_shelf_id)) return "%s" % val @api.depends('destination_barcode_id') def _compute_destination_name(self): if self.destination_barcode_id: self.destination_name = self.destination_barcode_id.name self.destination_location_id = self.destination_barcode_id.location_id.id self.destination_shelf_id = self.destination_barcode_id.shelf_id.id else: self.destination_name = '' self.destination_location_id = False self.destination_shelf_id = False # # @api.onchange('destination_barcode_id') # def _onchange_destination_shelf_id(self): # if self.destination_barcode_id: # self.destination_shelf_id = self.destination_barcode_id.shelf_id.id def create_stock_moves(self, lot_id, num): # 创建产品货位变更的库存移动记录 stock_move_id = self.env['stock.move'].sudo().create({ 'name': 'HWBG/%s' % self.id, 'product_id': self.current_product_id.id, 'location_id': self.current_location_id.id, 'location_dest_id': self.destination_location_id.id, 'product_uom_qty': num, 'state': 'done' }) # 创建移动历史记录 stock_move_line_id = self.env['stock.move.line'].sudo().create({ 'product_id': self.current_product_id.id, 'lot_id': lot_id.id, 'move_id': stock_move_id.id, 'current_location_id': self.current_barcode_id.id, 'destination_location_id': self.destination_barcode_id.id, 'install_tool_time': fields.Datetime.now(), 'qty_done': num, 'state': 'done' }) return stock_move_id, stock_move_line_id def confirm_the_change(self): if self.destination_barcode_id: stocks = [] if self.lot_id: self.current_barcode_id.product_sn_id = False self.destination_barcode_id.product_sn_id = self.lot_id.id stocks = self.create_stock_moves(self.lot_id, 1) elif self.current_product_sn_ids: for current_product_sn_id in self.current_product_sn_ids: stocks = self.create_stock_moves(current_product_sn_id.lot_id, current_product_sn_id.qty_num) current_product_sn_id.write({ 'qty_num': 0 }) else: raise ValidationError('没有需要变更的批次/序列号!') self.env['stock.move.line'].sudo().put_shelf_location(stocks[-1]) else: raise ValidationError('请选择目标货位编码!') # 关闭弹出窗口 return {'type': 'ir.actions.act_window_close'}