Copyright (c) 2024, Asofi and contributors For license information, please see…
- User
Copyright (c) 2024, Asofi and contributors
For license information, please see license.txt
import frappe from frappe.model.document import Document from frappe import _
class ApplicantInvitation(Document): sales_invoice_name = ""
def before_save(self): self.add_company_information() self.add_full_name() def on_submit(self): # دالة on_submit ستتبع المعاملات، وسيتم التراجع عن أي عملية فاشلة تلقائيًا self.create_user() if self.payd_from == "Employee": # إنشاء أو الحصول على كائن Customer الفردي customer_doc = self.create_customer() customer_name = customer_doc.name # استخدام الـ name الداخلي كمعرف else: frappe.logger().debug("#" * 40 + f" Company Name: {self.company_name}") # إنشاء أو الحصول على كائن Customer للشركة customer_doc = self.create_company_customer() customer_name = customer_doc.name # استخدام الـ name الداخلي كمعرف # إنشاء فاتورة المبيعات باستخدام معرف العميل الصحيح self.create_sales_invoice(customer_name) # إنشاء طلب تعليمات التحقق self.create_verification_instructions_request() def on_cancel(self): pass def add_full_name(self): middle_name = self.middle_name if self.middle_name else "" self._full_name = f"{self.first_name} {middle_name} {self.last_name}".strip() def add_company_information(self): user = frappe.get_doc("User", frappe.session.user) self.db_set("company_name", user.full_name) if user.full_name == "Administrator": self.db_set("company_email", "administrator@example.com") # استبدل بـ البريد الإلكتروني الفعلي للمسؤول إذا لزم الأمر else: self.db_set("company_email", user.email) def create_user(self): """إنشاء مستخدم إذا لم يكن موجودًا بالفعل.""" if frappe.db.exists("User", {"email": self.email}): frappe.logger().debug(f"User with email {self.email} already exists.") return frappe.get_doc("User", {"email": self.email}) else: new_doc = frappe.new_doc("User") new_doc.email = self.email new_doc.first_name = self.first_name new_doc.last_name = self.last_name new_doc.middle_name = self.middle_name new_doc.enabled = True new_doc.language = self.language new_doc.module_profile = "" new_doc.role_profile_name = "Applicant" new_doc.insert(ignore_permissions=True) frappe.logger().debug(f"User Created: {new_doc.name}") return new_doc def create_customer(self): """إنشاء Customer فردي إذا لم يكن موجودًا وإرجاع كائن Customer.""" if frappe.db.exists("Customer", {"customer_name": self._full_name}): frappe.logger().debug(f"Customer '{self._full_name}' already exists.") customer_doc = frappe.get_doc("Customer", {"customer_name": self._full_name}) else: new_doc = frappe.new_doc("Customer") new_doc.email = self.email new_doc.customer_name = self._full_name new_doc.customer_type = "Individual" new_doc.customer_group = "Applicants" new_doc.insert(ignore_permissions=True) customer_doc = new_doc frappe.logger().debug(f"Customer Created: {customer_doc.name}") return customer_doc def create_company_customer(self): """إنشاء Customer للشركة إذا لم يكن موجودًا وإرجاع كائن Customer.""" if frappe.db.exists("Customer", {"customer_name": self.company_name}): frappe.logger().debug(f"Company Customer '{self.company_name}' already exists.") customer_doc = frappe.get_doc("Customer", {"customer_name": self.company_name}) else: new_doc = frappe.new_doc("Customer") new_doc.customer_name = self.company_name new_doc.customer_type = "Company" new_doc.customer_group = "Companies" # تأكد من وجود هذه المجموعة new_doc.email = self.company_email new_doc.insert(ignore_permissions=True) customer_doc = new_doc frappe.logger().debug(f"Company Customer Created: {customer_doc.name}") return customer_doc def create_verification_instructions_request(self): """إنشاء طلب تعليمات التحقق.""" new_doc = frappe.new_doc("Verification Instructions Request") new_doc.payd_from = self.payd_from new_doc.user_id = self.email new_doc.company_submitting_application = self.company_email new_doc.language = self.language new_doc.application_status = "Job Request" # new_doc.sales_invoice = self.sales_invoice_name # فك التعليق إذا لزم الأمر new_doc.insert(ignore_permissions=True) frappe.logger().debug(f"Verification Instructions Request Created: {new_doc.name}") return new_doc def create_sales_invoice(self, customer_name): """إنشاء فاتورة مبيعات للعميل المعطي.""" if not customer_name: frappe.throw(_("Customer is not set. Cannot create Sales Invoice.")) # الحصول على كائن Customer للتحقق من وجوده customer = frappe.get_doc("Customer", customer_name) # إنشاء فاتورة المبيعات sales_invoice = frappe.new_doc("Sales Invoice") sales_invoice.customer = customer.name # استخدام حقل الـ name sales_invoice.company = self.company_name # تأكد من مطابقة هذه القيمة مع الشركة في ERPNext sales_invoice.posting_date = frappe.utils.today() # تعيين تاريخ اليوم أو حسب المتطلبات # إضافة أي حقول إلزامية أخرى هنا مثل due_date، items، الخ. # إعداد عناصر فاتورة المبيعات self.preparing_the_sales_invoice(self.other_services, sales_invoice) # حفظ فاتورة المبيعات sales_invoice.save(ignore_permissions=True) # خيارياً، يمكن اعتماد فاتورة المبيعات تلقائيًا # sales_invoice.submit() # تعيين اسم فاتورة المبيعات للكائن لأغراض الرجوع إليها لاحقاً إذا لزم الأمر self.sales_invoice_name = sales_invoice.name frappe.logger().debug(f"Sales Invoice Created: {sales_invoice.name}") def preparing_the_sales_invoice(self, list_items, sales_invoice): """إضافة عناصر إلى فاتورة المبيعات بناءً على الخدمات المقدمة.""" for i in list_items: is_stock_item = frappe.db.get_value("Item", i.service, "is_stock_item") if is_stock_item: sales_invoice.append( "items", { "item_code": i.service, "qty": 1, "uom": "Nos", }, ) elif bundle := frappe.db.exists("Product Bundle", {"new_item_code": i.service}): self.preparing(bundle, sales_invoice) if self.package: self.preparing(self.package, sales_invoice) def preparing(self, bundle_name, sales_invoice): """إضافة عناصر من حزمة منتجات إلى فاتورة المبيعات.""" bundle = frappe.get_doc("Product Bundle", bundle_name) for b_i in bundle.items: sales_invoice.append( "items", { "item_code": b_i.item_code, "qty": 1, "uom": "Nos", }, ) - Assistant
- User
استبدل التوثيق العربي بتوثيق انجليزي للكود
- Assistant
- User
I want it to be approved only if all the functions called in on_submit succeed
- Assistant
- User
Rewrite the entire code
- Assistant