It looks like you're working with a Python function that retrieves the price of an item from a database using Frappe Framework, which is often used with ERPNext. However, there are a couple of improvements that you could consider for this code snippet to make it more robust and functional. Below is a revised version with some explanations:
User
لدي Child Table باسم Other Services يحتوي على حقل Service وهو امتداد من Item وحقل باسم service_price اريد عند ما اختار ال service يقوم بتعبئة ال service_price
Assistant
لتحقيق ذلك، يمكنك استخدام "Client-Side Scripting" في Frappe و ERPNext لاستدعاء سعر الخدمة تلقائيًا عند اختيار خدمة في "Child Table". إليك كيفية القيام بذلك باستخدام JavaScript:
1. إعداد الـ Child Table
User
Field not permitted in query: default_price
Assistant
إذا كنت قد تلقيت الخطأ "Field not permitted in query: default_price", فهذا يعني أن الحقل default_price ليس موجودًا في نموذج "Item" الذي تستخدمه. في ERPNext، الحقول المستخدمة في "Item" قد تختلف حسب التكوين الخاص بك، لذا يجب التحقق من الحقول المتاحة.
خطوات التحقق والإصلاح:
تحقق من جدول "Item":
اذهب إلى DocType "Item" في ERPNext.
تحقق من الحقول المتاحة في النموذج. ابحث عن الحقل الذي يحتوي على السعر. قد يكون مختلفاً مثل أو .
User
السعر موجود في دوكتايب Item Price
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
def get_package_price(self):
try:
package_price_data = frappe.db.get_list(
"Item Price",
fields=['price_list_rate'],
filters={'item_code': self.package},
limit=1
)
print("*" * 5, package_price_data)
# Check if we got any results
if package_price_data:
self.package_price = package_price_data[0].price_list_rate # Extracting the price
else:
self.package_price = None # Handle the case where no price was found
print("No price found for the specified package.")
except Exception as e:
print("An error occurred while fetching package price:", str(e))
self.package_price = None # Ensure package_price is reset on error
Key Changes and Explanations:
Error Handling: Wrapped the database call in a try-except block to handle potential exceptions that may arise during database access. This helps prevent the application from crashing and allows you to manage the error gracefully.
Data Check: Added a check to ensure that the package_price_data is not empty before trying to access its contents. If it's empty, we set self.package_price to None and print a corresponding message.
Accessing Price Data: Changed how the price is accessed. Since get_list returns a list of dictionaries, we need to ensure we extract the price_list_rate correctly from the first dictionary.
Self-Contained Function: Assumed that this function is part of a class, allowing the use of self. Make sure the variable self.package is defined and contains a valid item code when this function is called.
Additional Considerations:
Logging vs Printing: For better practice, especially in production code, consider using a logging framework instead of print statements. This allows for better control over log levels and formatting.
Data Validation: Depending on your application context, you might also want to validate that self.package is in an expected format before making the database call.
تأكد من أن لديك Child Table باسم "Other Services" يحتوي على الحقول التالية:
Service: حقل نوع "Link" يشير إلى "Item".
service_price: حقل نوع "Currency" أو "Float".
2. إضافة Script فـ DocType الرئيسي
اهتم بإضافة سكربت JavaScript إلى DocType الرئيسي الذي يحتوي على Child Table "Other Services". يمكنك القيام بذلك كما يلي:
frappe.ui.form.on('Your Parent Doctype', {
onload: function(frm) {
// بعد تحميل النموذج
},
refresh: function(frm) {
// عند تحديث النموذج
},
other_services_add: function(frm, cdt, cdn) {
// حدث عند إضافة صف جديد إلى Child Table
let row = locals[cdt][cdn]; // الحصول على الصف
row.service_price = 0; // تأكد أن السعر يبدأ بـ 0
}
});
frappe.ui.form.on('Other Services', {
service: function(frm, cdt, cdn) {
let row = locals[cdt][cdn]; // الحصول على الصف المحدد
if (row.service) {
// استعلام السعر من جدول Item
frappe.db.get_value('Item', row.service, 'default_price', (r) => {
if (r && r.default_price) {
row.service_price = r.default_price;
frm.refresh_field('other_services'); // تحديث الحقول لعرض القيمة الجديدة
} else {
row.service_price = 0; // إذا لم يتم العثور على سعر
}
});
} else {
row.service_price = 0; // إذا لم يتم تحديد الخدمة
}
}
});
3. ملاحظات على السكربت
تغيير اسم الـ DocType: تأكد من تغيير "Your Parent Doctype" إلى اسم الـ DocType الرئيسي الذي يحتوي على Child Table.
حقل السعر: هنا تم استخدام default_price كاسم الحقل. تأكد من استخدام اسم الحقل الصحيح من جدول Item الذي يحتوي على سعر الخدمة.
تحديث الحقول: استخدم frm.refresh_field('other_services'); لتحديث الحقول بحيث تعكس القيمة الجديدة المعبئة.
4. تفعيل السكربت
بعد كتابة السكربت، تأكد من حفظه وتفعيل "Client-Side Script" لفئة الـ DocType المناسبة من أجل تفعيل التغييرات.
هذا السكربت يجب أن يقوم بتعبئة حقل service_price تلقائيًا عند اختيار الخدمة المناسبة في Child Table "Other Services".
standard_rate
price_list_rate
تعديل السكربت:
بناءً على اسم الحقل الذي يعمل بشكل صحيح، عدل السكربت كما يلي:
frappe.ui.form.on('Your Parent Doctype', {
other_services_add: function(frm, cdt, cdn) {
let row = locals[cdt][cdn];
row.service_price = 0; // تأكد أن السعر يبدأ بـ 0
}
});
frappe.ui.form.on('Other Services', {
service: function(frm, cdt, cdn) {
let row = locals[cdt][cdn];
if (row.service) {
// استعلام السعر من جدول Item
frappe.db.get_value('Item', row.service, 'standard_rate', (r) => { // إذا كان اسم الحقل هو standard_rate
if (r && r.standard_rate) {
row.service_price = r.standard_rate;
frm.refresh_field('other_services');
} else {
row.service_price = 0; // إذا لم يتم العثور على سعر
}
});
} else {
row.service_price = 0; // إذا لم يتم تحديد الخدمة
}
}
});
توضيحات حول السكربت:
قد تحتاج لتغيير 'standard_rate' إلى اسم الحقل الفعلي الذي يجسد السعر في جدول "Item".
تأكد من أن المستخدم الذي يستخدم السكربت لديه الإذن للوصول إلى الحقول الضرورية، إذ قد تسبب القيود في الأذونات أيضًا في عدم النجاح في استعلام القيم.
اختبار الوظيفة:
بعد إجراء التعديلات، اذهب إلى نموذج الـ DocType الرئيسي، وقم بإضافة صفوف جديدة في Child Table حيث يتم اختيار الخدمات، وتأكد من أن حقل service_price يتم تعبئته بشكل صحيح بناءً على الخدمة المختارة.
إذا كان السعر موجودًا في DocType "Item Price" بدلاً من DocType "Item"، فإنك بحاجة إلى إجراء استعلام على "Item Price" لجلب السعر المناسب للخدمة المحددة عند اختيارها من "Child Table".
الخطوات اللازمة لتحقيق ذلك:
استعلام السعر من "Item Price": عليك استخدام الحقل المناسب من DocType "Item Price" لجلب السعر. غالبًا ما يكون لديك price_list_rate أو حقل آخر مرتبط بأسعار العناصر.
السكربت المعدل:
إليك السكربت المعدل لجلب السعر من "Item Price":
frappe.ui.form.on('Your Parent Doctype', {
other_services_add: function(frm, cdt, cdn) {
let row = locals[cdt][cdn];
row.service_price = 0; // تأكد أن السعر يبدأ بـ 0
}
});
frappe.ui.form.on('Other Services', {
service: function(frm, cdt, cdn) {
let row = locals[cdt][cdn];
if (row.service) {
// استعلام السعر من جدول Item Price
frappe.db.get_list('Item Price', {
fields: ['price_list_rate'],
filters: {
item_code: row.service, // أو استخدم item_code إذا كان هو الحقل المناسب
price_list: 'Standard' // استبدل هذا بالنوع المناسب إذا كان لديك قائمة أسعار محددة
},
limit: 1 // لاسترداد سعر واحد
}).then((data) => {
if (data.length > 0) {
row.service_price = data[0].price_list_rate; // الحصول على السعر
} else {
row.service_price = 0; // إذا لم يتم العثور على سعر
}
frm.refresh_field('other_services'); // تحديث الحقول لعرض القيمة الجديدة
}).catch((error) => {
console.error("حدث خطأ أثناء جلب السعر:", error);
row.service_price = 0; // تأكد من تعيين السعر إلى 0 في حالة وجود خطأ
frm.refresh_field('other_services');
});
} else {
row.service_price = 0; // إذا لم يتم تحديد الخدمة
frm.refresh_field('other_services'); // تحديث الحقول لعرض القيمة الجديدة
}
}
});
توضيحات حول السكربت:
استعلام مع get_list: يتم استخدام frappe.db.get_list() لجلب السعر من "Item Price" بناءً على الكود الخاص بالعنصر (service).
تصفية النتائج: تأكد من استخدام حقل item_code بشكل صحيح بناءً على كيفية تخزين الكودات في "Item Price".
قائمة الأسعار: تأكد من استخدام price_list الصحيح مثل Standard أو أي اسم لقائمة الأسعار لديك.
تحديث الحقول: يتم تحديث حقل service_price في الصف المحدد وrefresh على Child Table بعد تعيين السعر.
اختبار السكربت:
بعد إجراء التعديلات، يمكنك اختبار الأمر بإضافة صفوف جديدة إلى "Other Services" وملاحظة ما إذا كان حقل service_price يتم تعبئته بشكل صحيح استنادًا إلى الخدمة المختارة.