Skip to content

Commit

Permalink
fix: capacity planning issue in the job card
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitwaghchaure committed Feb 25, 2024
1 parent dd23ddc commit 752fecf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
29 changes: 18 additions & 11 deletions erpnext/manufacturing/doctype/job_card/job_card.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,12 @@ def validate_time_logs(self):
for row in self.sub_operations:
self.total_completed_qty += row.completed_qty

def get_overlap_for(self, args, check_next_available_slot=False):
def get_overlap_for(self, args):
time_logs = []

time_logs.extend(self.get_time_logs(args, "Job Card Time Log", check_next_available_slot))
time_logs.extend(self.get_time_logs(args, "Job Card Time Log"))

time_logs.extend(self.get_time_logs(args, "Job Card Scheduled Time", check_next_available_slot))
time_logs.extend(self.get_time_logs(args, "Job Card Scheduled Time"))

if not time_logs:
return {}
Expand All @@ -269,7 +269,7 @@ def get_overlap_for(self, args, check_next_available_slot=False):
self.workstation = workstation_time.get("workstation")
return workstation_time

return time_logs[-1]
return time_logs[0]

def has_overlap(self, production_capacity, time_logs):
overlap = False
Expand Down Expand Up @@ -308,7 +308,7 @@ def has_overlap(self, production_capacity, time_logs):
return True
return overlap

def get_time_logs(self, args, doctype, check_next_available_slot=False):
def get_time_logs(self, args, doctype):
jc = frappe.qb.DocType("Job Card")
jctl = frappe.qb.DocType(doctype)

Expand All @@ -318,9 +318,6 @@ def get_time_logs(self, args, doctype, check_next_available_slot=False):
((jctl.from_time >= args.from_time) & (jctl.to_time <= args.to_time)),
]

if check_next_available_slot:
time_conditions.append(((jctl.from_time >= args.from_time) & (jctl.to_time >= args.to_time)))

query = (
frappe.qb.from_(jctl)
.from_(jc)
Expand Down Expand Up @@ -395,18 +392,28 @@ def schedule_time_logs(self, row):

def validate_overlap_for_workstation(self, args, row):
# get the last record based on the to time from the job card
data = self.get_overlap_for(args, check_next_available_slot=True)
data = self.get_overlap_for(args)

if not self.workstation:
workstations = get_workstations(self.workstation_type)
if workstations:
# Get the first workstation
self.workstation = workstations[0]

if not data:
row.planned_start_time = args.from_time
return

if data:
if data.get("planned_start_time"):
row.planned_start_time = get_datetime(data.planned_start_time)
args.planned_start_time = get_datetime(data.planned_start_time)
else:
row.planned_start_time = get_datetime(data.to_time + get_mins_between_operations())
args.planned_start_time = get_datetime(data.to_time + get_mins_between_operations())

args.from_time = args.planned_start_time
args.to_time = add_to_date(args.planned_start_time, minutes=row.remaining_time_in_mins)

self.validate_overlap_for_workstation(args, row)

def check_workstation_time(self, row):
workstation_doc = frappe.get_cached_doc("Workstation", self.workstation)
Expand Down
11 changes: 7 additions & 4 deletions erpnext/manufacturing/doctype/work_order/work_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,6 @@ def create_job_card(self):
def prepare_data_for_job_card(self, row, index, plan_days, enable_capacity_planning):
self.set_operation_start_end_time(index, row)

original_start_time = row.planned_start_time
job_card_doc = create_job_card(
self, row, auto_create=True, enable_capacity_planning=enable_capacity_planning
)
Expand All @@ -597,11 +596,15 @@ def prepare_data_for_job_card(self, row, index, plan_days, enable_capacity_plann
row.planned_start_time = job_card_doc.scheduled_time_logs[-1].from_time
row.planned_end_time = job_card_doc.scheduled_time_logs[-1].to_time

if date_diff(row.planned_start_time, original_start_time) > plan_days:
if date_diff(row.planned_end_time, self.planned_start_date) > plan_days:
frappe.message_log.pop()
frappe.throw(
_("Unable to find the time slot in the next {0} days for the operation {1}.").format(
plan_days, row.operation
_(
"Unable to find the time slot in the next {0} days for the operation {1}. Please increase the 'Capacity Planning For (Days)' in the {2}."
).format(
plan_days,
row.operation,
get_link_to_form("Manufacturing Settings", "Manufacturing Settings"),
),
CapacityError,
)
Expand Down

0 comments on commit 752fecf

Please sign in to comment.