diff --git a/jey_erp/public/js/asset.js b/jey_erp/public/js/asset.js index b8da1d7..d8d8125 100644 --- a/jey_erp/public/js/asset.js +++ b/jey_erp/public/js/asset.js @@ -241,9 +241,6 @@ function render_tax_validity_fields(frm, type) { const $col2 = $('
').appendTo($row); const $col3 = $('
').appendTo($row); - // Флаг для отслеживания первоначальной загрузки - frm[`_initial_load_${type}`] = true; - // Проверяем, есть ли уже сохраненные данные в памяти (приоритет) let initial_date_from = tax_article.date_from; let initial_duration = tax_article.duration; @@ -255,7 +252,7 @@ function render_tax_validity_fields(frm, type) { initial_date_to = frm._tax_validity_data[type].date_to || initial_date_to; } - // Создаем Date From поле + // Создаем Date From поле (редактируемое) const date_from_field = frappe.ui.form.make_control({ parent: $col1, df: { @@ -263,57 +260,40 @@ function render_tax_validity_fields(frm, type) { label: __('Date From'), fieldname: `${type}_date_from_dynamic`, change: function() { - if (frm[`_initial_load_${type}`]) { - return; - } - handle_field_change(frm, type, 'date_from'); + handle_date_from_change(frm, type); } }, render_input: true }); date_from_field.set_value(initial_date_from); - // Создаем Duration поле + // Создаем Duration поле (скрытое и read only) const duration_field = frappe.ui.form.make_control({ parent: $col2, df: { fieldtype: 'Int', label: __('Duration (Years)'), fieldname: `${type}_duration_dynamic`, - change: function() { - if (frm[`_initial_load_${type}`]) { - return; - } - handle_field_change(frm, type, 'duration'); - } + hidden: 1, + read_only: 1 }, render_input: true }); duration_field.set_value(initial_duration); - // Создаем Date To поле + // Создаем Date To поле (read only) const date_to_field = frappe.ui.form.make_control({ parent: $col3, df: { fieldtype: 'Date', label: __('Date To'), fieldname: `${type}_date_to_dynamic`, - change: function() { - if (frm[`_initial_load_${type}`]) { - return; - } - handle_field_change(frm, type, 'date_to'); - } + read_only: 1 }, render_input: true }); date_to_field.set_value(initial_date_to); - // Снимаем флаг первоначальной загрузки после небольшой задержки - setTimeout(() => { - frm[`_initial_load_${type}`] = false; - }, 300); - // Добавляем информационное сообщение $('
') .text(__('Changes will be saved to Tax Article when you save this Asset')) @@ -346,81 +326,39 @@ function render_tax_validity_fields(frm, type) { }); } -function handle_field_change(frm, type, field_type) { - // Предотвращаем рекурсивные вызовы - if (frm[`_updating_${type}`]) { - return; - } - +function handle_date_from_change(frm, type) { if (!frm._tax_validity_controls || !frm._tax_validity_controls[type]) { return; } - frm[`_updating_${type}`] = true; + const controls = frm._tax_validity_controls[type]; - try { - const controls = frm._tax_validity_controls[type]; - - let date_from = controls.date_from.get_value(); - let date_to = controls.date_to.get_value(); - let duration = controls.duration.get_value(); - - // Автоматический расчет - if (field_type === 'date_from') { - if (date_from && duration) { - let from_date = frappe.datetime.str_to_obj(date_from); - let to_date = new Date(from_date); - to_date.setFullYear(to_date.getFullYear() + duration); - date_to = frappe.datetime.obj_to_str(to_date); - controls.date_to.set_value(date_to); - } - } else if (field_type === 'duration') { - if (date_from && duration) { - let from_date = frappe.datetime.str_to_obj(date_from); - let to_date = new Date(from_date); - to_date.setFullYear(to_date.getFullYear() + duration); - date_to = frappe.datetime.obj_to_str(to_date); - controls.date_to.set_value(date_to); - } - } else if (field_type === 'date_to') { - if (date_from && date_to) { - let from_date = frappe.datetime.str_to_obj(date_from); - let to_date_obj = frappe.datetime.str_to_obj(date_to); - - if (to_date_obj < from_date) { - frappe.msgprint({ - title: __('Invalid Date Range'), - indicator: 'red', - message: __('Date To cannot be earlier than Date From') - }); - controls.date_to.set_value(''); - frm[`_updating_${type}`] = false; - return; - } - - let years = (to_date_obj - from_date) / (365.25 * 24 * 60 * 60 * 1000); - duration = Math.round(years); - controls.duration.set_value(duration); - } - } - - // Сохраняем локальные переменные (уже пересчитанные значения) - if (!frm._tax_validity_data) { - frm._tax_validity_data = {}; - } - frm._tax_validity_data[type] = { - date_from: date_from, - date_to: date_to, - duration: duration - }; - - frm.dirty(); - - } finally { - setTimeout(() => { - frm[`_updating_${type}`] = false; - }, 50); + const date_from = controls.date_from.get_value(); + const duration = controls.duration.get_value(); + + if (!date_from || !duration) { + return; } + + // Рассчитываем date_to = date_from + duration + let from_date = frappe.datetime.str_to_obj(date_from); + let to_date = new Date(from_date); + to_date.setFullYear(to_date.getFullYear() + duration); + const calculated_date_to = frappe.datetime.obj_to_str(to_date); + + controls.date_to.set_value(calculated_date_to); + + // Сохраняем в памяти + if (!frm._tax_validity_data) { + frm._tax_validity_data = {}; + } + frm._tax_validity_data[type] = { + date_from: date_from, + date_to: calculated_date_to, + duration: duration + }; + + frm.dirty(); } function save_tax_validity_data(frm) {