108 lines
4.7 KiB
Python
108 lines
4.7 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
from collections import defaultdict
|
|
|
|
from odoo import api, fields, models, _
|
|
from odoo.tools import OrderedSet
|
|
|
|
|
|
# _get_surface_technics_purchase_ids
|
|
class PurchaseOrder(models.Model):
|
|
_inherit = 'purchase.order'
|
|
|
|
production_count = fields.Integer(
|
|
"关联制造订单",
|
|
compute='_compute_workorder_count',
|
|
)
|
|
|
|
def action_view_production(self):
|
|
origins = [order.name for order in self.picking_ids]
|
|
production_id = self.env['mrp.production'].search([('origin', 'in', origins)])
|
|
if not production_id:
|
|
return
|
|
action = {
|
|
'res_model': 'mrp.production',
|
|
'type': 'ir.actions.act_window',
|
|
}
|
|
if len(production_id) == 1:
|
|
action.update({
|
|
'view_mode': 'form',
|
|
'res_id': production_id.id,
|
|
})
|
|
else:
|
|
action.update({
|
|
'name': _("制造订单列表"),
|
|
'domain': [('id', 'in', production_id.ids)],
|
|
'view_mode': 'tree,form',
|
|
})
|
|
return action
|
|
|
|
def _compute_workorder_count(self):
|
|
for purchase in self:
|
|
origins = [order.name for order in purchase.picking_ids]
|
|
production_id = self.env['mrp.production'].search([('origin', 'in', origins)])
|
|
purchase.production_count = len(production_id)
|
|
|
|
def button_confirm(self):
|
|
super().button_confirm()
|
|
workorders = self.env['mrp.workorder'].search([('purchase_id', '=', self.id), ('state', '!=', 'cancel')])
|
|
for workorder in workorders:
|
|
if workorder.routing_type == '表面工艺' and workorder.is_subcontract is True:
|
|
move_out = workorder.move_subcontract_workorder_ids[1]
|
|
for mo in move_out:
|
|
if mo.state != 'done':
|
|
mo.write({'state': 'assigned', 'production_id': False})
|
|
if not mo.move_line_ids:
|
|
self.env['stock.move.line'].create(mo.get_move_line(workorder.production_id, workorder))
|
|
return True
|
|
|
|
origin_sale_id = fields.Many2one('sale.order', string='销售订单号', store=True, compute='_compute_origin_sale_id')
|
|
origin_sale_ids = fields.Many2many('sale.order', string='销售订单号(多个)', store=True,
|
|
compute='_compute_origin_sale_id')
|
|
|
|
@api.depends('origin')
|
|
def _compute_origin_sale_id(self):
|
|
for purchase in self:
|
|
if not purchase.origin:
|
|
continue
|
|
elif 'MO' in purchase.origin:
|
|
mp_name_list = [name.strip() for name in purchase['origin'].split(',')]
|
|
os_ids = list({mp_id.sale_order_id.id for mp_id in self.env['mrp.production'].sudo().search([
|
|
('name', 'in', mp_name_list)])})
|
|
if len(os_ids) == 1:
|
|
purchase.origin_sale_id = os_ids[0]
|
|
elif len(os_ids) >= 2:
|
|
purchase.origin_sale_ids = os_ids
|
|
elif 'S' in purchase.origin:
|
|
os_name_list = [name.strip() for name in purchase['origin'].split(',')]
|
|
os_ids = self.env['sale.order'].sudo().search([('name', 'in', os_name_list)])
|
|
if len(os_ids) == 1:
|
|
purchase.origin_sale_id = os_ids.id
|
|
elif len(os_ids) >= 2:
|
|
purchase.origin_sale_ids = os_ids.ids
|
|
elif 'IN' in purchase.origin:
|
|
sp_name_list = [name.strip() for name in purchase['origin'].split(',')]
|
|
os_ids = list({sp_id.sale_order_id.id for sp_id in self.env['stock.picking'].sudo().search([
|
|
('name', 'in', sp_name_list)])})
|
|
if len(os_ids) == 1:
|
|
purchase.origin_sale_id = os_ids[0]
|
|
elif len(os_ids) >= 2:
|
|
purchase.origin_sale_ids = os_ids
|
|
|
|
|
|
class PurchaseOrderLine(models.Model):
|
|
_inherit = 'purchase.order.line'
|
|
part_number = fields.Char('零件图号', related='product_id.part_number', readonly=True)
|
|
related_product = fields.Many2one('product.product', compute='_compute_related_product', string='关联产品',
|
|
help='经此产品工艺加工成的成品')
|
|
|
|
@api.depends('order_id.origin')
|
|
def _compute_related_product(self):
|
|
for record in self:
|
|
if record.product_id.detailed_type:
|
|
production_id = self.env['mrp.production'].search([('name', '=', record.order_id.origin)])
|
|
record.related_product = production_id.product_id if production_id else False
|
|
else:
|
|
record.related_product = False
|