from odoo import api, fields, models, _ class PurchaseOrder(models.Model): _inherit = 'purchase.order' state = fields.Selection([ ('draft', '询价'), ('sent', '发送询价'), ('to approve', '待批准'), ("approved", "已批准"), ('purchase', '采购订单'), ('done', '完成'), ('cancel', '取消'), ('rejected', '已驳回') ], string='Status', readonly=True, index=True, copy=False, default='draft', tracking=True) def button_confirm(self): res = super(PurchaseOrder, self).button_confirm() # 取消反向调拨单 reverse_move_ids = self.env['stock.move'].search([ ('origin', '=', self.name), ('purchase_line_id', '=', False), ('state', '!=', 'done') ]) if reverse_move_ids: reverse_move_ids.picking_id.action_cancel() return res def button_cancel(self): """ 将取消的采购订单关联的库存移动撤销 """ move_ids = self.order_line.move_dest_ids.filtered(lambda move: move.state != 'done' and not move.scrapped) res =super(PurchaseOrder, self).button_cancel() if move_ids.mapped('created_purchase_request_line_id'): move_ids.write({'state': 'waiting', 'is_done': False}) return res