changed asset logics

This commit is contained in:
Ali 2025-11-05 15:24:41 +04:00
parent b284108c0c
commit 27ea136f37
1 changed files with 34 additions and 96 deletions

View File

@ -241,9 +241,6 @@ function render_tax_validity_fields(frm, type) {
const $col2 = $('<div class="col-sm-4">').appendTo($row); const $col2 = $('<div class="col-sm-4">').appendTo($row);
const $col3 = $('<div class="col-sm-4">').appendTo($row); const $col3 = $('<div class="col-sm-4">').appendTo($row);
// Флаг для отслеживания первоначальной загрузки
frm[`_initial_load_${type}`] = true;
// Проверяем, есть ли уже сохраненные данные в памяти (приоритет) // Проверяем, есть ли уже сохраненные данные в памяти (приоритет)
let initial_date_from = tax_article.date_from; let initial_date_from = tax_article.date_from;
let initial_duration = tax_article.duration; 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; 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({ const date_from_field = frappe.ui.form.make_control({
parent: $col1, parent: $col1,
df: { df: {
@ -263,57 +260,40 @@ function render_tax_validity_fields(frm, type) {
label: __('Date From'), label: __('Date From'),
fieldname: `${type}_date_from_dynamic`, fieldname: `${type}_date_from_dynamic`,
change: function() { change: function() {
if (frm[`_initial_load_${type}`]) { handle_date_from_change(frm, type);
return;
}
handle_field_change(frm, type, 'date_from');
} }
}, },
render_input: true render_input: true
}); });
date_from_field.set_value(initial_date_from); date_from_field.set_value(initial_date_from);
// Создаем Duration поле // Создаем Duration поле (скрытое и read only)
const duration_field = frappe.ui.form.make_control({ const duration_field = frappe.ui.form.make_control({
parent: $col2, parent: $col2,
df: { df: {
fieldtype: 'Int', fieldtype: 'Int',
label: __('Duration (Years)'), label: __('Duration (Years)'),
fieldname: `${type}_duration_dynamic`, fieldname: `${type}_duration_dynamic`,
change: function() { hidden: 1,
if (frm[`_initial_load_${type}`]) { read_only: 1
return;
}
handle_field_change(frm, type, 'duration');
}
}, },
render_input: true render_input: true
}); });
duration_field.set_value(initial_duration); duration_field.set_value(initial_duration);
// Создаем Date To поле // Создаем Date To поле (read only)
const date_to_field = frappe.ui.form.make_control({ const date_to_field = frappe.ui.form.make_control({
parent: $col3, parent: $col3,
df: { df: {
fieldtype: 'Date', fieldtype: 'Date',
label: __('Date To'), label: __('Date To'),
fieldname: `${type}_date_to_dynamic`, fieldname: `${type}_date_to_dynamic`,
change: function() { read_only: 1
if (frm[`_initial_load_${type}`]) {
return;
}
handle_field_change(frm, type, 'date_to');
}
}, },
render_input: true render_input: true
}); });
date_to_field.set_value(initial_date_to); date_to_field.set_value(initial_date_to);
// Снимаем флаг первоначальной загрузки после небольшой задержки
setTimeout(() => {
frm[`_initial_load_${type}`] = false;
}, 300);
// Добавляем информационное сообщение // Добавляем информационное сообщение
$('<div class="text-muted small" style="margin-top: 10px; padding-left: 15px;">') $('<div class="text-muted small" style="margin-top: 10px; padding-left: 15px;">')
.text(__('Changes will be saved to Tax Article when you save this Asset')) .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) { function handle_date_from_change(frm, type) {
// Предотвращаем рекурсивные вызовы
if (frm[`_updating_${type}`]) {
return;
}
if (!frm._tax_validity_controls || !frm._tax_validity_controls[type]) { if (!frm._tax_validity_controls || !frm._tax_validity_controls[type]) {
return; return;
} }
frm[`_updating_${type}`] = true; const controls = frm._tax_validity_controls[type];
try { const date_from = controls.date_from.get_value();
const controls = frm._tax_validity_controls[type]; const duration = controls.duration.get_value();
let date_from = controls.date_from.get_value(); if (!date_from || !duration) {
let date_to = controls.date_to.get_value(); return;
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);
} }
// Рассчитываем 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) { function save_tax_validity_data(frm) {