207 lines
8.9 KiB
Python
207 lines
8.9 KiB
Python
# -*- coding: utf-8 -*-
|
||
|
||
from odoo import models, fields, api, _
|
||
from odoo.tools import float_compare
|
||
from odoo.exceptions import ValidationError
|
||
|
||
|
||
class SfDemandMainPlan(models.Model):
|
||
_name = 'sf.demand.main.plan'
|
||
_description = 'sf_demand_main_plan'
|
||
|
||
def _get_machining_precision(self):
|
||
machinings = self.env['sf.machining.accuracy'].sudo().search([])
|
||
list = [(m.sync_id, m.name) for m in machinings]
|
||
return list
|
||
|
||
state = fields.Selection([
|
||
('10', '需求确认'),
|
||
('20', '待工艺设计'),
|
||
('30', '部分下达'),
|
||
('40', '已下达'),
|
||
('50', '取消'),
|
||
], string='状态', default='10', compute='_compute_state', store=True)
|
||
|
||
line_ids = fields.One2many(comodel_name='sf.production.demand.plan',
|
||
inverse_name='demand_plan_id', string="需求计划", copy=True)
|
||
|
||
sale_order_id = fields.Many2one(comodel_name="sale.order",
|
||
string="销售订单", readonly=True)
|
||
sale_order_line_id = fields.Many2one(comodel_name="sale.order.line",
|
||
string="销售订单明细", readonly=True)
|
||
|
||
product_id = fields.Many2one(
|
||
comodel_name='product.product',
|
||
related='sale_order_line_id.product_id',
|
||
string='产品', store=True, index=True)
|
||
|
||
part_name = fields.Char('零件名称', related='product_id.part_name')
|
||
part_number = fields.Char('零件图号', compute='_compute_part_number', store=True)
|
||
materials_id = fields.Char('材料', compute='_compute_materials_id', store=True)
|
||
|
||
blank_type = fields.Selection([('圆料', '圆料'), ('方料', '方料')], string='坯料分类',
|
||
related='product_id.blank_type')
|
||
embryo_long = fields.Char('坯料尺寸(mm)', compute='_compute_embryo_long', store=True)
|
||
is_incoming_material = fields.Boolean('客供料', related='sale_order_line_id.is_incoming_material', store=True)
|
||
pending_qty = fields.Float(
|
||
string="待计划",
|
||
compute='_compute_pending_qty', store=True)
|
||
planned_qty = fields.Float(
|
||
string="已计划",
|
||
compute='_compute_planned_qty', store=True)
|
||
model_id = fields.Char('模型ID', related='product_id.model_id')
|
||
customer_name = fields.Char('客户', related='sale_order_id.customer_name')
|
||
product_uom_qty = fields.Float(
|
||
string="需求数量",
|
||
related='sale_order_line_id.product_uom_qty', store=True)
|
||
deadline_of_delivery = fields.Date('客户交期', related='sale_order_line_id.delivery_end_date', store=True)
|
||
contract_date = fields.Date('合同日期', related='sale_order_id.contract_date')
|
||
contract_code = fields.Char('合同号', related='sale_order_id.contract_code', store=True)
|
||
|
||
model_process_parameters_ids = fields.Many2many('sf.production.process.parameter',
|
||
'demand_plan_process_parameter_rel',
|
||
string='表面工艺',
|
||
compute='_compute_model_process_parameters_ids'
|
||
, store=True
|
||
)
|
||
model_machining_precision = fields.Selection(selection=_get_machining_precision, string='精度',
|
||
related='product_id.model_machining_precision')
|
||
|
||
inventory_quantity_auto_apply = fields.Float(
|
||
string="成品库存",
|
||
compute='_compute_inventory_quantity_auto_apply', store=True
|
||
)
|
||
|
||
priority = fields.Selection([
|
||
('1', '紧急'),
|
||
('2', '高'),
|
||
('3', '中'),
|
||
('4', '低'),
|
||
], string='优先级', default='3')
|
||
|
||
hide_button_release_plan = fields.Boolean(
|
||
string='显示下达计划按钮',
|
||
compute='_compute_hide_button_release_plan',
|
||
default=False
|
||
)
|
||
|
||
@api.depends('product_id.part_number', 'product_id.model_name')
|
||
def _compute_part_number(self):
|
||
for line in self:
|
||
if line.product_id:
|
||
if line.product_id.part_number:
|
||
line.part_number = line.product_id.part_number
|
||
else:
|
||
if line.product_id.model_name:
|
||
line.part_number = line.product_id.model_name.rsplit('.', 1)[0]
|
||
else:
|
||
line.part_number = None
|
||
|
||
@api.depends('product_id.materials_id')
|
||
def _compute_materials_id(self):
|
||
for line in self:
|
||
if line.product_id:
|
||
line.materials_id = f"{line.product_id.materials_id.name}/{line.product_id.materials_type_id.name}"
|
||
else:
|
||
line.materials_id = None
|
||
|
||
@api.depends('product_id.model_long', 'product_id.model_width', 'product_id.model_height')
|
||
def _compute_embryo_long(self):
|
||
for line in self:
|
||
if line.product_id:
|
||
line.embryo_long = f"{round(line.product_id.model_long, 3)}*{round(line.product_id.model_width, 3)}*{round(line.product_id.model_height, 3)}"
|
||
else:
|
||
line.embryo_long = None
|
||
|
||
@api.depends('product_id.model_process_parameters_ids')
|
||
def _compute_model_process_parameters_ids(self):
|
||
for line in self:
|
||
if line.product_id and line.product_id.model_process_parameters_ids:
|
||
line.model_process_parameters_ids = [(6, 0, line.product_id.model_process_parameters_ids.ids)]
|
||
else:
|
||
line.model_process_parameters_ids = [(5, 0, 0)]
|
||
|
||
def _compute_inventory_quantity_auto_apply(self):
|
||
location_id = self.env['stock.location'].search([('name', '=', '成品存货区')], limit=1).id
|
||
product_ids = self.mapped('product_id').ids
|
||
if product_ids:
|
||
quant_data = self.env['stock.quant'].read_group(
|
||
domain=[
|
||
('product_id', 'in', product_ids),
|
||
('location_id', '=', location_id)
|
||
],
|
||
fields=['product_id', 'inventory_quantity_auto_apply'],
|
||
groupby=['product_id']
|
||
)
|
||
quantity_map = {item['product_id'][0]: item['inventory_quantity_auto_apply'] for item in quant_data}
|
||
else:
|
||
quantity_map = {}
|
||
for line in self:
|
||
if line.product_id:
|
||
line.inventory_quantity_auto_apply = quantity_map.get(line.product_id.id, 0.0)
|
||
else:
|
||
line.inventory_quantity_auto_apply = 0.0
|
||
|
||
@api.depends('product_uom_qty', 'line_ids.plan_uom_qty')
|
||
def _compute_pending_qty(self):
|
||
for line in self:
|
||
sum_plan_uom_qty = sum(line.line_ids.mapped('plan_uom_qty'))
|
||
pending_qty = line.product_uom_qty - sum_plan_uom_qty
|
||
rounding = line.product_id.uom_id.rounding or 0.01#设置默认精度
|
||
# 如果精度小于等于0,则设置为0.01
|
||
if rounding<=0:
|
||
rounding = 0.01
|
||
# 如果计划量小于等于0,则设置为0
|
||
if float_compare(pending_qty, 0, precision_rounding=rounding) == -1:
|
||
line.pending_qty = 0
|
||
else:
|
||
line.pending_qty = pending_qty
|
||
|
||
@api.depends('line_ids.plan_uom_qty')
|
||
def _compute_planned_qty(self):
|
||
for line in self:
|
||
line.planned_qty = sum(line.line_ids.mapped('plan_uom_qty'))
|
||
|
||
@api.depends('line_ids.status')
|
||
def _compute_hide_button_release_plan(self):
|
||
for line in self:
|
||
line.hide_button_release_plan = bool(line.line_ids.filtered(lambda p: p.status != '60'))
|
||
|
||
def button_release_plan(self):
|
||
pass
|
||
|
||
@api.depends('line_ids.status', 'sale_order_id.state')
|
||
def _compute_state(self):
|
||
for line in self:
|
||
#已下达的订单
|
||
status_line = line.line_ids.filtered(lambda p: p.status == '60')
|
||
if line.sale_order_id.state == 'cancel':
|
||
line.state = '50'
|
||
line.line_ids.status = '100'
|
||
elif len(line.line_ids) == len(status_line):
|
||
line.state = '40'
|
||
#排除上面全部下达的情形后,如果有已下达订单,则状态为部分
|
||
elif bool(status_line):
|
||
line.state = '30'
|
||
else:
|
||
line.state = '10'
|
||
|
||
def write(self, vals):
|
||
res = super(SfDemandMainPlan, self).write(vals)
|
||
line_ids = self.line_ids.filtered(lambda p: p.plan_uom_qty == 0 or p.plan_uom_qty < 0)
|
||
if line_ids:
|
||
raise ValidationError(f"计划量不能小于等于0")
|
||
if 'line_ids' in vals:
|
||
for line in self.line_ids:
|
||
if not line.sale_order_id:
|
||
line.sale_order_id = self.sale_order_id
|
||
if not line.sale_order_line_id:
|
||
line.sale_order_line_id = self.sale_order_line_id
|
||
return res
|
||
|
||
def name_get(self):
|
||
result = []
|
||
for plan in self:
|
||
result.append((plan.id, plan.sale_order_id.name))
|
||
return result
|