diff --git a/one_compliance/custom/custom_field/customer_group.py b/one_compliance/custom/custom_field/customer_group.py new file mode 100644 index 00000000..cf46bd85 --- /dev/null +++ b/one_compliance/custom/custom_field/customer_group.py @@ -0,0 +1,23 @@ +def get_customer_group_custom_fields(): + ''' + Method to get custom fields for Customer Group doctype + ''' + return { + "Customer Group": [ + { + "fieldname": "hod", + "fieldtype": "Link", + "insert_after": "parent_customer_group", + "label": "HOD", + "options": "Employee", + }, + { + "fieldname": "hod_name", + "fieldtype": "Data", + "insert_after": "hod", + "label": "HOD Name", + "fetch_from": "hod.employee_name", + "read_only": 1, + } + ] + } \ No newline at end of file diff --git a/one_compliance/one_compliance/doc_events/sales_order.py b/one_compliance/one_compliance/doc_events/sales_order.py index 616f1a9b..fcff0c15 100644 --- a/one_compliance/one_compliance/doc_events/sales_order.py +++ b/one_compliance/one_compliance/doc_events/sales_order.py @@ -57,74 +57,93 @@ def get_compliance_subcategory(item_code): @frappe.whitelist() def create_project_from_sales_order(sales_order, start_date, item_code, priority, assign_to=None, expected_end_date=None, remark=None, custom_instructions=None): - """Create project from sales order with tasks based on project template""" + """Create project from sales order with tasks based on project template + """ + # Parse assignees employees = json.loads(assign_to) if assign_to else [] - + # Fetch required documents sales_order_doc = frappe.get_doc('Sales Order', sales_order) compliance_sub_category = frappe.get_doc('Compliance Sub Category', {'item_code': item_code}) - + + # Ensure project template exists if not compliance_sub_category.project_template: frappe.throw( - title=_('ALERT !!'), + title=_('ALERT !!'), msg=_(f'Project Template does not exist for {compliance_sub_category.name}') ) - + project_template_doc = frappe.get_doc('Project Template', compliance_sub_category.project_template) - head_of_department = frappe.db.get_value( - 'Employee', - {'employee': compliance_sub_category.head_of_department}, - 'user_id' - ) - - # Validate assignees - if not assign_to and not _has_template_assignees(project_template_doc): + + # Head of department (user_id) + head_of_department = None + if compliance_sub_category.head_of_department: + head_of_department = frappe.db.get_value( + 'Employee', + {'employee': compliance_sub_category.head_of_department}, + 'user_id' + ) + + # Customer group HOD (optional) + group_hod_user_id = None + customer_group = frappe.db.get_value('Customer', sales_order_doc.customer, 'customer_group') + if customer_group: + group_hod = frappe.db.get_value('Customer Group', customer_group, 'hod') + if group_hod: + group_hod_user_id = frappe.db.get_value('Employee', group_hod, 'user_id') + + # Validate assignees (either explicit assign_to or template has assignees) + if not employees and not _has_template_assignees(project_template_doc): frappe.msgprint("Project can't be created since no assignees are specified in tasks") return - - # Create project + + # Create project using helper project = _create_project( - sales_order_doc, - compliance_sub_category, + sales_order_doc, + compliance_sub_category, project_template_doc, - start_date, - expected_end_date, - priority, - remark, + start_date, + expected_end_date, + priority, + remark, custom_instructions ) - + # Assign to head of department - if compliance_sub_category.head_of_department: + if head_of_department: _assign_to_head_of_department(project.name, head_of_department, 'Project') - - # Assign to additional employees - if assign_to: + + # Assign to customer group HOD if present and different from HOD + if group_hod_user_id and group_hod_user_id != head_of_department: + create_todo('Project', project.name, group_hod_user_id, frappe.session.user, "Project {} Assigned Successfully".format(project.name)) + + # Assign to additional employees from assign_to + if employees: _assign_to_employees(employees, project.name, head_of_department, 'Project') - + frappe.msgprint(f'Project Created for {compliance_sub_category.name}.', alert=1) - + # Create tasks from template _create_tasks_from_template( - project, - project_template_doc, + project, + project_template_doc, compliance_sub_category, - start_date, - employees, + start_date, + employees, head_of_department ) - + # Create premium tasks if applicable - if sales_order_doc.get("is_premium_project") and hasattr(project_template_doc, "premium_tasks"): + if sales_order_doc.get("is_premium_project") and getattr(project_template_doc, "premium_tasks", None): _create_premium_tasks( - project, - project_template_doc, + project, + project_template_doc, compliance_sub_category, - start_date, - employees, + start_date, + employees, head_of_department ) - + frappe.db.commit() @@ -649,4 +668,4 @@ def set_compliance_fields(doc, method): ) if subcat: item.custom_compliance_category = subcat.compliance_category - item.custom_compliance_subcategory = subcat.name \ No newline at end of file + item.custom_compliance_subcategory = subcat.name diff --git a/one_compliance/one_compliance/doctype/compliance_agreement/compliance_agreement.py b/one_compliance/one_compliance/doctype/compliance_agreement/compliance_agreement.py index 76b11e35..ba2b0628 100644 --- a/one_compliance/one_compliance/doctype/compliance_agreement/compliance_agreement.py +++ b/one_compliance/one_compliance/doctype/compliance_agreement/compliance_agreement.py @@ -647,6 +647,14 @@ def create_project_from_template(sales_order, project_template, customer, compan compliance_date = getdate(compliance_date) project_template_doc = frappe.get_doc("Project Template", project_template) sub_category_doc = frappe.get_doc('Compliance Sub Category', compliance_sub_category) + + customer_group = frappe.db.get_value("Customer", customer, "customer_group") + group_hod_user_id = None + if customer_group: + group_hod = frappe.db.get_value("Customer Group", customer_group, "hod") + if group_hod: + group_hod_user_id = frappe.db.get_value("Employee", group_hod, "user_id") + repeat_on = frappe.db.get_value('Compliance Sub Category', compliance_sub_category, 'repeat_on') project_based_on_prior_phase = frappe.db.get_value('Compliance Sub Category', compliance_sub_category, 'project_based_on_prior_phase') previous_month_date = add_months(getdate(compliance_date), -1) @@ -712,6 +720,10 @@ def create_project_from_template(sales_order, project_template, customer, compan create_todo("Project", project.name, hod_user, frappe.session.user, f"Project assigned to {sub_category_doc.head_of_department}") + if group_hod_user_id and group_hod_user_id != hod_user: + create_todo("Project", project.name, group_hod_user_id, + frappe.session.user, f"Project {project.name} Assigned Successfully") + # Create Tasks from Template for template_task in project_template_doc.tasks: template_task_doc = frappe.get_doc('Task', template_task.task) @@ -769,6 +781,10 @@ def create_project_from_template(sales_order, project_template, customer, compan create_todo("Task", task_doc.name, hod_user, frappe.session.user, f"HOD notified for task: {task_doc.subject}") + if group_hod_user_id and group_hod_user_id not in assigned_users and group_hod_user_id != hod_user: + create_todo("Task", task_doc.name, group_hod_user_id, + frappe.session.user, "Task Assign to Customer Group HOD") + return project except Exception: diff --git a/one_compliance/setup.py b/one_compliance/setup.py index df9badce..40e524e1 100644 --- a/one_compliance/setup.py +++ b/one_compliance/setup.py @@ -21,6 +21,7 @@ from one_compliance.custom.custom_field.opportunity_item import get_opportunity_item_custom_fields from one_compliance.custom.custom_field.compliance_sub_category import get_compliance_sub_category_custom_fields from one_compliance.custom.custom_field.timesheet_detail import get_timesheet_detail_custom_fields +from one_compliance.custom.custom_field.customer_group import get_customer_group_custom_fields # Custom property setter method imports from one_compliance.custom.property_setter.contact_email import get_contact_email_property_setters @@ -146,6 +147,7 @@ def get_custom_fields(): custom_fields.update(get_opportunity_item_custom_fields()) custom_fields.update(get_compliance_sub_category_custom_fields()) custom_fields.update(get_timesheet_detail_custom_fields()) + custom_fields.update(get_customer_group_custom_fields()) custom_fields.update(()) return custom_fields