from odoo import models, fields, api, _ from odoo.exceptions import UserError, ValidationError class PurchaseRequestLine(models.Model): _inherit = 'purchase.request.line' _description = '采购申请明细' supply_method = fields.Selection([ ('automation', "自动化产线加工"), ('manual', "人工线下加工"), ('purchase', "外购"), ('outsourcing', "委外加工"), ], string='供货方式', readonly=True) demand_plan_line_id = fields.Many2one(comodel_name="sf.production.demand.plan", string="需求计划明细", readonly=True) @api.depends('demand_plan_line_id') def _compute_supply_method(self): for prl in self: if prl.demand_plan_line_id: prl.supply_method = prl.demand_plan_line_id.supply_method else: prl.supply_method = None class PurchaseRequestLineMakePurchaseOrder(models.TransientModel): _inherit = "purchase.request.line.make.purchase.order" def make_purchase_order(self): res = [] purchase_obj = self.env["purchase.order"] po_line_obj = self.env["purchase.order.line"] purchase = False if len(set([item_id.line_id.supply_method for item_id in self.item_ids])) > 1: raise ValidationError('不同供货方式不可合并创建询价单!') for item in self.item_ids: line = item.line_id if item.product_qty <= 0.0: raise UserError(_("Enter a positive quantity.")) if self.purchase_order_id: purchase = self.purchase_order_id if not purchase: po_data = self._prepare_purchase_order( line.request_id.picking_type_id, line.request_id.group_id, line.company_id, line.request_id.origin, ) po_data['demand_plan_line_id'] = item.line_id.demand_plan_line_id.id # po_data.update({'related_product':line.related_product.id}) purchase = purchase_obj.create(po_data) # Look for any other PO line in the selected PO with same # product and UoM to sum quantities instead of creating a new # po line domain = self._get_order_line_search_domain(purchase, item) available_po_lines = po_line_obj.search(domain) new_pr_line = True # If Unit of Measure is not set, update from wizard. if not line.product_uom_id: line.product_uom_id = item.product_uom_id # Allocation UoM has to be the same as PR line UoM alloc_uom = line.product_uom_id wizard_uom = item.product_uom_id if available_po_lines and not item.keep_description: new_pr_line = False po_line = available_po_lines[0] po_line.purchase_request_lines = [(4, line.id)] po_line.move_dest_ids |= line.move_dest_ids po_line_product_uom_qty = po_line.product_uom._compute_quantity( po_line.product_uom_qty, alloc_uom ) wizard_product_uom_qty = wizard_uom._compute_quantity( item.product_qty, alloc_uom ) all_qty = min(po_line_product_uom_qty, wizard_product_uom_qty) self.create_allocation(po_line, line, all_qty, alloc_uom) else: po_line_data = self._prepare_purchase_order_line(purchase, item) if item.keep_description: po_line_data["name"] = item.name if line.related_product: po_line_data.update({'related_product': line.related_product.id}) po_line = po_line_obj.create(po_line_data) po_line_product_uom_qty = po_line.product_uom._compute_quantity( po_line.product_uom_qty, alloc_uom ) wizard_product_uom_qty = wizard_uom._compute_quantity( item.product_qty, alloc_uom ) all_qty = min(po_line_product_uom_qty, wizard_product_uom_qty) self.create_allocation(po_line, line, all_qty, alloc_uom) self._post_process_po_line(item, po_line, new_pr_line) res.append(purchase.id) purchase_requests = self.item_ids.mapped("request_id") purchase_requests.button_in_progress() return { "domain": [("id", "in", res)], "name": _("RFQ"), "view_mode": "tree,form", "res_model": "purchase.order", "view_id": False, "context": False, "type": "ir.actions.act_window", }