84 lines
3.7 KiB
Python
84 lines
3.7 KiB
Python
from odoo import models, fields, api, _
|
||
|
||
|
||
class ReSaleOrder(models.Model):
|
||
_inherit = 'sale.order'
|
||
|
||
mrp_production_ids = fields.Many2many(
|
||
'mrp.production',
|
||
compute='_compute_mrp_production_ids',
|
||
string='与此销售订单相关联的制造订单',
|
||
groups='mrp.group_mrp_user', store=True)
|
||
demand_plan_count = fields.Integer(
|
||
"需求计划生成计数",
|
||
compute='_compute_demand_plan_count',
|
||
)
|
||
#暂时不知道哪里用到了
|
||
@api.depends('procurement_group_id')
|
||
def _compute_purchase_request_count(self):
|
||
for record in self:
|
||
pr_ids = self.env['purchase.request'].sudo().search([('origin', 'like', record.name)])
|
||
if pr_ids:
|
||
record.purchase_request_purchase_order_count = len(pr_ids)
|
||
else:
|
||
record.purchase_request_purchase_order_count = 0
|
||
#计算需求计划生成计数
|
||
def _compute_demand_plan_count(self):
|
||
for line in self:
|
||
demand_plan = self.env['sf.production.demand.plan'].sudo().search([('sale_order_id', '=', line.id)])
|
||
line.demand_plan_count = len(demand_plan)
|
||
|
||
def sale_order_create_line(self, product, item):
|
||
ret = super(ReSaleOrder, self).sale_order_create_line(product, item)
|
||
# vals = {
|
||
# 'sale_order_id': ret.order_id.id,
|
||
# 'sale_order_line_id': ret.id,
|
||
# }
|
||
# demand_plan_info = self.env['sf.demand.main.plan'].sudo().create(vals)
|
||
# vals.update({'demand_plan_id': demand_plan_info.id, 'plan_uom_qty': ret.product_uom_qty})
|
||
# demand_plan = self.env['sf.production.demand.plan'].sudo().create(vals)
|
||
# demand_plan_info.write({'line_ids': demand_plan.ids})
|
||
# 优化方案1:使用事务确保数据一致性
|
||
with self.env.cr.savepoint():
|
||
# 1. 先创建主计划
|
||
demand_plan_info = self.env['sf.demand.main.plan'].sudo().create({
|
||
'sale_order_id': ret.order_id.id,
|
||
'sale_order_line_id': ret.id,
|
||
})
|
||
# 2. 创建明细计划时直接建立关联(利用One2many的inverse特性)
|
||
demand_plan = self.env['sf.production.demand.plan'].sudo().create({
|
||
'demand_plan_id': demand_plan_info.id,
|
||
'plan_uom_qty': ret.product_uom_qty,
|
||
'sale_order_id': ret.order_id.id,
|
||
'sale_order_line_id': ret.id,
|
||
})
|
||
# 3. 不需要手动更新line_ids,Odoo的ORM会自动处理One2many关系
|
||
if demand_plan.product_id.machining_drawings_name:
|
||
filename_url = demand_plan.product_id.machining_drawings_name.rsplit('.', 1)[0]
|
||
wizard_vals = {
|
||
'demand_plan_id': demand_plan.id,
|
||
'model_id': demand_plan.model_id,
|
||
'filename_url': filename_url,
|
||
'type': '1',
|
||
}
|
||
self.env['sf.demand.plan.print.wizard'].sudo().create(wizard_vals)
|
||
return ret
|
||
#暂时不知道哪里用到
|
||
def confirm_to_supply_method(self):
|
||
self.state = 'sale'
|
||
for line in self.order_line:
|
||
if line.product_id.auto_machining:
|
||
line.supply_method = 'automation'
|
||
#在销售订单打开需求计划列表
|
||
def action_view_demand_plan(self):
|
||
self.ensure_one()
|
||
demand_plan_ids = self.env['sf.production.demand.plan'].sudo().search([('sale_order_id', '=', self.id)]).ids
|
||
return {
|
||
'res_model': 'sf.production.demand.plan',
|
||
'type': 'ir.actions.act_window',
|
||
'name': _("需求计划"),
|
||
'domain': [('id', 'in', demand_plan_ids)],
|
||
'view_mode': 'tree',
|
||
}
|
||
|