from odoo import models, fields, api, _ from odoo.exceptions import UserError class SfPurchaseOrder(models.Model): _inherit = 'purchase.order' # 收料入库明细 storage_list_ids = fields.One2many('storage.list', 'purchase_order_id', string='收料入库明细') # 计算字段:总收料数量 total_received_quantity = fields.Float(string='到货数量',compute='_compute_total_received_quantity',store=True) purchase_order_line_id = fields.Many2one('purchase.order.line', string='采购订单行') @api.depends('order_line.arrival_quantity','order_line.product_qty') def _compute_total_received_quantity(self): for order in self: total_arrival_quantity = sum(line.arrival_quantity for line in order.order_line) total_product_qty=sum(line.product_qty for line in order.order_line) order.total_received_quantity = f'{total_arrival_quantity}/{total_product_qty}' def open_arrival_inform(self): self.ensure_one() # 方式一:把数据写入普通模型(如果要持久化关联关系等场景) # 先清空旧数据(如果需要),避免重复 self.env["storage.list"].search([ ("purchase_order_id", "=", self.id) ]).unlink() pickings = self.env['stock.picking'].search([ ('origin', '=', self.name), # ('picking_type_code', '=', 'incoming'), ('state', '!=', 'cancel') ]) relate_vals_list = [] for picking in pickings: for move_line in picking.move_ids_without_package: relate_vals = { "purchase_order_id": self.id, "product_id": move_line.product_id.id, "picking_id": picking.id, "ordered_quantity": move_line.product_uom_qty, # "current_arrival_quantity": move_line.product_uom_qty, # 默认到货数量等于需求数量 "product_code": move_line.product_id.default_code, "product_remark": move_line.name, "part_number": move_line.part_number, "state": picking.state, } relate_vals_list.append(relate_vals) if relate_vals_list: self.env["storage.list"].create(relate_vals_list) action = { 'name': _("选择到货通知"), 'type':'ir.actions.act_window', 'view_mode':'form', 'views': [(self.env.ref('sf_purchase_arrival_inform.storage_list_wrapper_form').id, 'form')], 'res_model':'purchase.order', 'target':'new', 'res_id': self.id, 'context': { 'dialog_size': 'large', }, } return action def confirm_arrival_inform(self): """确认到货通知""" # 直接调用storage_list的批量确认方法 if self.storage_list_ids: self.storage_list_ids.button_confirm() # 关闭弹窗 return {'type': 'ir.actions.act_window_close'} #询价单确认时加二次确认逻辑 def button_confirm(self): """采购订单确认,弹出确认向导""" # 无订单行时直接确认 if not self.order_line: return super().button_confirm() # 直接创建向导记录并打开 wizard = self.env['purchase.confirm.wizard'].create({ 'purchase_order_id': self.id }) return { 'name': _('询价单订单确认'), 'type': 'ir.actions.act_window', 'res_model': 'purchase.confirm.wizard', 'res_id': wizard.id, 'view_mode': 'form', 'target': 'new', } def _execute_original_confirm(self): """执行原始的确认逻辑""" res = super().button_confirm() return res class SfPurchaseOrderLine(models.Model): _inherit = 'purchase.order.line' # 到货数量(实际存储字段) arrival_quantity = fields.Float(string='到货数量', default=0.0, help='累计到货数量')