Hi,
I have created one childtable Project PS Table in Project Planning and Scheduling Doctype. Also, i have added one button "Create Task in that childtable. when we click , it should show small childtable in POP UP. It show from frappe prompt. after update the pop-up childtable, the updated data showed when i open the same row in Project PS Table childtable. But if i save the parent doctype, then its not showing in the pop-up. Below , i attached code and reference images.
frappe.ui.form.on('Project PS Table', {
create_task: function(frm, cdt, cdn) {
let row = locals[cdt][cdn]; // Get the current row in the child table
// Fetch the from_year and to_year from the parent doctype (Project Planning and Scheduling)
let fromYear = frm.doc.from_year;
let toYear = frm.doc.to_year;
let holidayDates = [];
// If 'is_parent' is checked
if (row.is_parent) {
// Show the pop-up with only Start Date and Finish Date fields
frappe.prompt([{
fieldname: 'start_date',
label: 'Start Date',
fieldtype: 'Date',
reqd: 1
},
{
fieldname: 'finish_date',
label: 'Finish Date',
fieldtype: 'Date',
reqd: 1
}
], function(values) {
let adjusted_start_date = values.start_date;
let adjusted_finish_date = values.finish_date;
// Exclude holidays for start and finish dates
adjusted_start_date = excludeHolidays(adjusted_start_date, holidayDates);
adjusted_finish_date = excludeHolidays(adjusted_finish_date, holidayDates);
// Save the updated start and finish dates back to the child table row
frappe.model.set_value(cdt, cdn, 'start_date', adjusted_start_date);
frappe.model.set_value(cdt, cdn, 'finish_date', adjusted_finish_date);
frm.refresh_fields(); // Refresh to reflect changes
}, 'Enter Dates', 'Update');
} else {
// If 'is_parent' is unchecked, show the Dependency Tasks table
let existing_dependency_tasks = row.dependency_table || [];
// Show the Dependency Tasks table in the pop-up
frappe.prompt([{
fieldname: 'dependency_tasks',
label: 'Select Dependency Tasks',
fieldtype: 'Table',
fields: [{
fieldname: 'dependency_task',
label: 'Dependency Task',
fieldtype: 'Data',
reqd: 1,
in_list_view: 1
},
{
fieldname: 'dependency_type',
label: 'Dependency Type',
fieldtype: 'Select',
options: ['SS', 'SF', 'FS', 'FF'],
reqd: 1,
in_list_view: 1
},
{
fieldname: 'lag_time',
label: 'Lag Time (days)',
fieldtype: 'Int',
default: 0,
in_list_view: 1
}
],
in_place_edit: true,
data: existing_dependency_tasks, // Populate with the previously saved data
reqd: 1
}], function(values) {
// Get the Dependency Tasks data from the pop-up
let dependency_tasks = values.dependency_tasks || [];
// Fetch holidays for fromYear and toYear
fetchHolidays(frm, fromYear, toYear, function(holidayDates) {
dependency_tasks.forEach(function(dep_task) {
let dependency_task = dep_task.dependency_task;
let dependency_type = dep_task.dependency_type;
let lag_time = dep_task.lag_time || 0;
let dep_task_row = frm.doc.project_ps_table.find(d => d.activity_name === dependency_task);
if (!dep_task_row) return;
let dep_task_start_date = dep_task_row.start_date;
let dep_task_finish_date = dep_task_row.finish_date;
let adjusted_start_date, adjusted_finish_date;
// Calculate based on Dependency Type (SS, SF, FS, FF)
if (dependency_type === 'SS') {
adjusted_start_date = dep_task_start_date;
} else if (dependency_type === 'SF') {
adjusted_finish_date = dep_task_start_date;
} else if (dependency_type === 'FS') {
adjusted_start_date = dep_task_finish_date;
} else if (dependency_type === 'FF') {
adjusted_finish_date = dep_task_finish_date;
}
// Apply lag time if defined
if (lag_time !== undefined) {
if (adjusted_start_date) {
adjusted_start_date = frappe.datetime.add_days(adjusted_start_date, lag_time);
}
if (adjusted_finish_date) {
adjusted_finish_date = frappe.datetime.add_days(adjusted_finish_date, lag_time);
}
}
// Exclude holidays for both start and finish dates
if (adjusted_start_date) {
adjusted_start_date = excludeHolidays(adjusted_start_date, holidayDates);
frappe.model.set_value(cdt, cdn, 'start_date', adjusted_start_date);
}
if (adjusted_finish_date) {
adjusted_finish_date = excludeHolidays(adjusted_finish_date, holidayDates);
frappe.model.set_value(cdt, cdn, 'finish_date', adjusted_finish_date);
}
});
// Save the updated dependency tasks back to the 'dependency_table' field
frappe.model.set_value(cdt, cdn, 'dependency_table', dependency_tasks);
frm.refresh_fields(); // Refresh to reflect changes
});
}, 'Enter Dependency Tasks', 'Update');
}
}
});
// Function to fetch holidays for the given from_year and to_year
function fetchHolidays(frm, fromYear, toYear, callback) {
let holidayDates = [];
// Fetch holidays for fromYear
frappe.call({
method: 'frappe.client.get',
args: {
doctype: 'Holiday List',
name: fromYear // from_year links to Holiday List
},
callback: function(responseFromYear) {
if (responseFromYear.message) {
let holidaysFromYear = responseFromYear.message.holidays || [];
holidayDates = holidayDates.concat(holidaysFromYear.map(holiday => frappe.datetime.str_to_obj(holiday.holiday_date)));
}
// Fetch holidays for toYear
frappe.call({
method: 'frappe.client.get',
args: {
doctype: 'Holiday List',
name: toYear // to_year links to Holiday List
},
callback: function(responseToYear) {
if (responseToYear.message) {
let holidaysToYear = responseToYear.message.holidays || [];
holidayDates = holidayDates.concat(holidaysToYear.map(holiday => frappe.datetime.str_to_obj(holiday.holiday_date)));
}
// Once both holiday lists are fetched, pass the combined holidays to the callback
callback(holidayDates);
}
});
}
});
}
// Function to exclude holidays while adjusting dates
function excludeHolidays(date, holidayDates) {
// Check if the date is a holiday
let adjusted_date = frappe.datetime.str_to_obj(date);
while (holidayDates.some(holiday => holiday.toDateString() === adjusted_date.toDateString())) {
adjusted_date = frappe.datetime.add_days(adjusted_date, 1); // Skip this day and move to next
}
return frappe.datetime.obj_to_str(adjusted_date); // Return adjusted date as string
}