Files
test/sf_demand_plan/models/sale_order.py
2025-06-27 17:44:32 +08:00

74 lines
2.9 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_id = fields.Many2one(comodel_name="sf.demand.plan",
string="需求计划", readonly=True)
demand_plan_count = fields.Integer(
"需求计划生成计数",
compute='_compute_demand_plan_count',
)
@api.depends('demand_plan_id.line_ids.status')
def _compute_purchase_request_count(self):
for so in self:
pr_ids = self.env['purchase.request'].sudo().search([('origin', 'like', so.name)])
if pr_ids:
so.purchase_request_purchase_order_count = len(pr_ids)
else:
so.purchase_request_purchase_order_count = 0
@api.depends('demand_plan_id.line_ids')
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.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})
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)
ret.order_id.demand_plan_id = demand_plan_info.id
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',
}