From 53a67d7c7684e4a6a4a9420ce891c3107dceeea9 Mon Sep 17 00:00:00 2001 From: guanhuan Date: Mon, 26 May 2025 15:27:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8B=E8=BE=BE=E7=94=9F=E4=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../models/sf_production_demand_plan.py | 61 ++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/sf_demand_plan/models/sf_production_demand_plan.py b/sf_demand_plan/models/sf_production_demand_plan.py index 556a6d98..d1f29bc3 100644 --- a/sf_demand_plan/models/sf_production_demand_plan.py +++ b/sf_demand_plan/models/sf_production_demand_plan.py @@ -2,6 +2,7 @@ from odoo import models, fields, api, _ from odoo.exceptions import ValidationError from odoo.tools import float_compare +from datetime import datetime, timedelta class sf_production_plan(models.Model): @@ -243,4 +244,62 @@ class sf_production_plan(models.Model): raise ValidationError("计划开工日期必须大于或等于今天。") def release_production_order(self): - pass + pro_plan_list = self.env['sf.production.plan'].search( + [('product_id', '=', self.product_id.id), ('state', '=', 'draft')]) + sf_production_line = self.env['sf.production.line'].sudo().search( + [('name', '=', '1#CNC自动生产线')], limit=1) + current_datetime = datetime.now() + timedelta(minutes=3) + current_hour = current_datetime.hour + current_datetime.minute / 60 + date_planned_start = None + production_lines = sf_production_line.mrp_workcenter_ids.filtered(lambda b: "自动生产线" in b.name) + if production_lines: + if not production_lines.deal_with_workcenter_calendar(current_datetime): + attendance_list = production_lines.resource_calendar_id.attendance_ids + # 获取所有工作日规则并按星期几分组 + attendance_by_day = {} + for attendance in attendance_list: + if attendance.dayofweek not in attendance_by_day: + attendance_by_day[attendance.dayofweek] = [] + attendance_by_day[attendance.dayofweek].append(attendance) + + for day_offset in range(0, 8): + check_date = current_datetime + timedelta(days=day_offset) + # 日期为星期几 + check_day = production_lines.get_current_day_of_week(check_date) + if check_day in attendance_by_day: + day_attendances = attendance_by_day[check_day] + if day_offset == 0: + for attendance in day_attendances: + if current_hour < attendance.hour_to: + # 找到下一个有效时间段 + if current_hour < attendance.hour_from: + # 使用开始时间 + date_planned_start = check_date.replace( + hour=int(attendance.hour_from), + minute=int((attendance.hour_from % 1) * 60), + second=0, + microsecond=0 + ) + else: + continue + break + else: + # 不是今天,使用第一个工作时间段 + attendance = day_attendances[0] + date_planned_start = check_date.replace( + hour=int(attendance.hour_from), + minute=int((attendance.hour_from % 1) * 60), + second=0, + microsecond=0 + ) + + if date_planned_start: + break + else: + date_planned_start = current_datetime + + if date_planned_start: + pro_plan_list.production_line_id = sf_production_line.id + pro_plan_list.date_planned_start = date_planned_start + for pro_plan in pro_plan_list: + pro_plan.do_production_schedule()