بالطبع! يمكنك إضافة حقل من نوع "Check" باسم "تحديد كل العملاء" (Select All Customers) في الـ dialog، وعند تفعيله، يمكن تحديد كافة العملاء المعروضين في جدول customers_table. إليك كيفية تنفيذ ذلك:
1. إضافة حقل "تحديد كل العملاء":
يمكنك إضافة الحقل الجديد إلى الـ dialog في قسم fields.
2. إضافة منطق لتفعيل / تعطيل الاختيار لكل العملاء:
عند تغيير حالة حقل "تحديد كل العملاء"، سنقوم بتحديث حالة جميع الصفوف في الجدول customers_table.
إليك الكود المعدل:
frappe.ui.form.on('Frequency Plan', {
get_customer: function(frm) {
// التحقق من الحقول المطلوبة
if (!frm.doc.employee || !frm.doc.coverage_strategy || !frm.doc.holiday_list || !frm.doc.fiscal_year) {
frappe.msgprint(__('Please fill in all required fields: Employee, Coverage Strategy, Holiday List, and Fiscal Year.'));
return; // عدم فتح الـ Dialog إذا كانت الحقول غير مكتملة
}
// إنشاء مربع الحوار
let dialog = new frappe.ui.Dialog({
title: __('Get Customer Details'),
size: 'extra-large',
fields: [
{
fieldname: 'select_all',
fieldtype: 'Check',
label: __('Select All Customers'),
reqd: 0
},
{
fieldname: 'employee',
fieldtype: 'Link',
label: __('Employee'),
options: 'Employee',
reqd: 0,
default: frm.doc.employee,
read_only: 1
},
{
fieldtype: 'Link',
label: __('Territory'),
fieldname: 'territory',
options: 'Territory',
reqd: 1
},
{
fieldname: "business_type",
label: __("Business Type"),
fieldtype: "Select",
options: "Business Type",
options: ['', 'Pharmacies', 'Clinics'],
reqd: 0
},
{
fieldtype: 'Column Break',
fieldname: 'col0'
},
{
fieldname: 'full_name',
fieldtype: 'Data',
label: __('Full Name'),
reqd: 0,
default: frm.doc.full_name,
read_only: 1
},
{
fieldtype: 'Link',
label: __('Class'),
fieldname: '_class',
options: 'Class',
reqd: 1
},
{
fieldtype: 'Select',
label: __('Days of Week'),
fieldname: 'days_of_week',
options: [
{ "value": "Sunday", "label": "Sunday" },
{ "value": "Monday", "label": "Monday" },
{ "value": "Tuesday", "label": "Tuesday" },
{ "value": "Wednesday", "label": "Wednesday" },
{ "value": "Thursday", "label": "Thursday" },
{ "value": "Friday", "label": "Friday" },
{ "value": "Saturday", "label": "Saturday" }
],
reqd: 1
},
{
"fieldname": "section_break_l3nnz",
"fieldtype": "Section Break"
},
{
fieldtype: 'Table',
label: __('Customers'),
fieldname: 'customers_table',
fields: [
{
fieldtype: 'Check',
label: __('Select'),
fieldname: 'select',
in_list_view: 1
},
{
fieldtype: 'Link',
label: __('Customer'),
fieldname: 'customer',
options: 'Customer',
in_list_view: 1
},
{
fieldtype: 'Link',
label: __('Territory'),
fieldname: 'territory',
options: 'Territory',
in_list_view: 1
},
{
fieldtype: 'Link',
label: __('Class'),
fieldname: '_class',
options: 'Class',
in_list_view: 1
}
]
},
]
});
// إضافة الأزرار بتصميم جميل
dialog.$body.append(`
<div class="dialog-button-container">
<button class="btn btn-primary fetch-customers-btn">Fetch Customers</button>
<button class="btn btn-secondary add-frequency-plan-btn">Add Customers to Frequency Plan</button>
</div>
`);
// تخصيص على CSS
dialog.$body.find(".dialog-button-container").css({
"display": "flex",
"justify-content": "flex-end",
"gap": "15px",
"margin-top": "20px",
"margin-bottom": "20px"
});
// تخصيص الأزرار
dialog.$body.find(".fetch-customers-btn").css({
"background-color": "#007bff",
"color": "#ffffff",
"border": "none",
"border-radius": "5px",
"padding": "10px 20px",
"cursor": "pointer",
"transition": "background-color 0.3s"
}).hover(function() {
$(this).css("background-color", "#0056b3");
}, function() {
$(this).css("background-color", "#007bff");
});
dialog.$body.find(".add-frequency-plan-btn").css({
"background-color": "#6c757d",
"color": "#ffffff",
"border": "none",
"border-radius": "5px",
"padding": "10px 20px",
"cursor": "pointer",
"transition": "background-color 0.3s"
}).hover(function() {
$(this).css("background-color", "#5a6268");
}, function() {
$(this).css("background-color", "#6c757d");
});
// التحكم في تحديد كل العملاء
dialog.$body.on('change', 'input[name="select_all"]', function() {
let isChecked = $(this).is(':checked');
dialog.fields_dict.customers_table.df.data.forEach(row => {
row.select = isChecked ? 1 : 0; // تعيين القيمة للتحديد أو الإلغاء
});
dialog.fields_dict.customers_table.grid.refresh(); // تحديث الجدول
});
// إضافة المنطق لجلب العملاء
dialog.$body.on('click', '.fetch-customers-btn', function() {
let values = dialog.get_values();
if (values.territory && values._class) {
frappe.call({
method: "get_customer_details", // تأكد من تعديل هذا إلى مسار دالة السيرفر الخاص بك
doc: frm.doc,
args: {
territory: values.territory,
_class: values._class,
business_type: values.business_type
},
callback: function(r) {
if (r.message && r.message.length > 0) {
dialog.fields_dict.customers_table.df.data = [];
r.message.forEach(customer => {
dialog.fields_dict.customers_table.df.data.push({
select: 0,
customer: customer.name,
territory: customer.territory,
_class: customer._class
});
});
dialog.fields_dict.customers_table.grid.refresh();
} else {
frappe.msgprint(__("No customers found for the selected territory and class."));
}
}
});
} else {
frappe.msgprint(__("Please select Territory and Class."));
}
});
// إضافة المنطق لإضافة العملاء إلى خطة التردد
dialog.$body.on('click', '.add-frequency-plan-btn', function() {
let values = dialog.get_values();
if (values.territory && values._class && values.days_of_week) {
// استدعاء دالة السيرفر لإضافة العملاء
frappe.call({
method: "add_customers_to_frequency_plan", // تأكد من تعديل هذا إلى مسار دالة السيرفر الخاص بك
doc: frm.doc,
args: {
fiscal_year: frm.doc.fiscal_year,
selected_days: values.days_of_week,
selected_customers: dialog.fields_dict.customers_table.df.data
.filter(row => row.select) // تحديد الصفوف المحددة فقط
.map(row => row.customer) // استخراج أسماء العملاء فقط
},
callback: function(r) {
if (r.message && r.message.status === "success") {
frappe.msgprint(r.message.message);
frm.refresh_field('frequency_plan_table'); // تحديث الجدول
dialog.hide();
} else {
frappe.msgprint(__('Error occurred while adding customers.'));
}
}
});
} else {
frappe.msgprint(__('Please select Territory, Class and Days of Week.'));
}
});
// إظهار مربع الحوار
dialog.show();
}
});
النقاط الأساسية:
- إضافة حقل "تحديد كل العملاء": أضفنا حقل
Check جديد في بداية الـ dialog ليكون مسؤولًا عن تحديد كل العملاء.
- منطق تحديد/إلغاء تحديد العملاء: عند تغيير حالة حقل "تحديد كل العملاء"، يتغير الوضع الخاص بحقل
select في كل صف من جدول customers_table ليتم تحديده أو إلغاء تحديده.
- تحديث الجدول: يتم تحديث الجدول بعد تغيير حالة تحديد جميع العملاء ليعكس التغييرات الجديدة.
ستساعد هذه التعديلات على تحسين واجهة المستخدم وتجعل من السهل على المستخدمين تحديد جميع العملاء في خطوة واحدة. إذا كان لديك أي استفسار أو تحتاج إلى مزيد من المساعدة، فلا تتردد في طرحها!