Skip to content

Commit

Permalink
Merge pull request #3611 from ONE-F-M/staging
Browse files Browse the repository at this point in the history
Sprint 129 - Staging to Test-Production(V15.1.1)
  • Loading branch information
pjamsheer authored Sep 4, 2024
2 parents 7dc2126 + 539f425 commit 6407584
Show file tree
Hide file tree
Showing 12 changed files with 303 additions and 22 deletions.
70 changes: 65 additions & 5 deletions one_fm/fixtures/workflow.json
Original file line number Diff line number Diff line change
Expand Up @@ -2282,7 +2282,7 @@
"doctype": "Workflow",
"document_type": "Work Permit",
"is_active": 1,
"modified": "2021-09-12 15:27:39.765866",
"modified": "2024-09-03 14:39:58.624320",
"name": "Work Permit",
"override_status": 0,
"send_email_alert": 0,
Expand Down Expand Up @@ -2436,6 +2436,21 @@
"update_field": null,
"update_value": null,
"workflow_builder_id": null
},
{
"allow_edit": "GRD Operator",
"avoid_status_override": 0,
"doc_status": "0",
"is_optional_state": 0,
"message": null,
"next_action_email_template": null,
"parent": "Work Permit",
"parentfield": "states",
"parenttype": "Workflow",
"state": "Cancelled",
"update_field": "",
"update_value": "",
"workflow_builder_id": null
}
],
"transitions": [
Expand Down Expand Up @@ -2683,7 +2698,7 @@
"doctype": "Workflow",
"document_type": "Medical Insurance",
"is_active": 1,
"modified": "2021-11-18 01:43:30.455952",
"modified": "2024-09-03 15:14:07.388701",
"name": "Medical Insurance",
"override_status": 0,
"send_email_alert": 0,
Expand Down Expand Up @@ -2717,6 +2732,21 @@
"update_field": null,
"update_value": null,
"workflow_builder_id": null
},
{
"allow_edit": "GRD Operator",
"avoid_status_override": 0,
"doc_status": "0",
"is_optional_state": 0,
"message": null,
"next_action_email_template": null,
"parent": "Medical Insurance",
"parentfield": "states",
"parenttype": "Workflow",
"state": "Cancelled",
"update_field": null,
"update_value": null,
"workflow_builder_id": null
}
],
"transitions": [
Expand Down Expand Up @@ -2748,7 +2778,7 @@
"doctype": "Workflow",
"document_type": "MOI Residency Jawazat",
"is_active": 1,
"modified": "2021-11-18 01:43:23.247401",
"modified": "2024-09-03 15:14:22.859191",
"name": "MOI",
"override_status": 0,
"send_email_alert": 0,
Expand Down Expand Up @@ -2782,6 +2812,21 @@
"update_field": null,
"update_value": null,
"workflow_builder_id": null
},
{
"allow_edit": "GRD Operator",
"avoid_status_override": 0,
"doc_status": "0",
"is_optional_state": 0,
"message": null,
"next_action_email_template": null,
"parent": "MOI",
"parentfield": "states",
"parenttype": "Workflow",
"state": "Cancelled",
"update_field": null,
"update_value": null,
"workflow_builder_id": null
}
],
"transitions": [
Expand Down Expand Up @@ -2813,7 +2858,7 @@
"doctype": "Workflow",
"document_type": "PACI",
"is_active": 1,
"modified": "2021-10-26 15:49:10.692911",
"modified": "2024-09-03 15:13:45.330821",
"name": "PACI",
"override_status": 0,
"send_email_alert": 0,
Expand Down Expand Up @@ -2862,6 +2907,21 @@
"update_field": null,
"update_value": null,
"workflow_builder_id": null
},
{
"allow_edit": "GRD Operator",
"avoid_status_override": 0,
"doc_status": "0",
"is_optional_state": 0,
"message": null,
"next_action_email_template": null,
"parent": "PACI",
"parentfield": "states",
"parenttype": "Workflow",
"state": "Cancelled",
"update_field": null,
"update_value": null,
"workflow_builder_id": null
}
],
"transitions": [
Expand Down Expand Up @@ -9715,4 +9775,4 @@
"workflow_name": "Onboard Subcontract Employee",
"workflow_state_field": "workflow_state"
}
]
]
16 changes: 14 additions & 2 deletions one_fm/grd/doctype/medical_insurance/medical_insurance.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from __future__ import unicode_literals
import frappe
from frappe.model.document import Document
from frappe.utils import today, get_url
from frappe.utils import today, get_url,getdate
from frappe import _
from datetime import date
from frappe.core.doctype.communication.email import make
Expand All @@ -16,6 +16,18 @@
from one_fm.utils import is_scheduler_emails_enabled

class MedicalInsurance(Document):
def before_insert(self):
self.cancel_existing()

def cancel_existing(self):
"""Cancel documents for that employee which were created in previous years"""
year_threshold = getdate(self.date_of_application).year or getdate().year
first_day_of_year = getdate(f'01-01-{year_threshold}') #Get the first day of the year
existing_docs = frappe.get_all(self.doctype,{'date_of_application':['<',first_day_of_year],'name':['!=',self.name],'docstatus':0,'employee':self.employee})
if existing_docs:
for one in existing_docs:
frappe.db.set_value(self.doctype,one,'workflow_state','Cancelled')
frappe.db.commit()

def validate(self):
self.set_value()
Expand Down Expand Up @@ -47,7 +59,7 @@ def valid_work_permit_exists(preparation_name):
if employee_in_preparation.preparation_record:
for employee in employee_in_preparation.preparation_record:
if employee.renewal_or_extend == 'Renewal' and employee.nationality != 'Kuwaiti':
print(employee.employee)

create_mi_record(frappe.get_doc('Work Permit',{'preparation':preparation_name,'employee':employee.employee}))

#Creating mi for transfer
Expand Down
18 changes: 16 additions & 2 deletions one_fm/grd/doctype/moi_residency_jawazat/moi_residency_jawazat.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ class MOIResidencyJawazat(Document):
company = frappe.db.get_value("Company", frappe.defaults.get_global_default('company'),
['phone_no', 'email', 'company_name_arabic'], as_dict=1)

def before_insert(self):
self.cancel_existing()

def cancel_existing(self):
"""Cancel documents for that employee which were created in previous years"""
year_threshold = getdate(self.date_of_application).year or getdate().year
first_day_of_year = getdate(f'01-01-{year_threshold}') #Get the first day of the year
existing_docs = frappe.get_all(self.doctype,{'date_of_application':['<',first_day_of_year],'name':['!=',self.name],'docstatus':0,'employee':self.employee})
if existing_docs:
for one in existing_docs:
frappe.db.set_value(self.doctype,one,'workflow_state','Cancelled')
frappe.db.commit()


def validate(self):
self.set_grd_values()
self.set_new_expiry_date()
Expand Down Expand Up @@ -84,7 +98,7 @@ def recall_create_paci(self):
paci.create_PACI_for_transfer(self.employee)

def validate_mandatory_fields_on_submit(self):
field_list = [{'Upload Payment Invoice':'invoice_attachment'},{'Upload Residency':'residency_attachment'},{'Updated Residency Expiry Date':'new_residency_expiry_date'}]
field_list = [{'Upload Payment Invoice':'invoice_attachment'},{'Updated Residency Expiry Date':'new_residency_expiry_date'}]
self.set_mendatory_fields(field_list)

def set_mendatory_fields(self,field_list):
Expand All @@ -95,7 +109,7 @@ def set_mendatory_fields(self,field_list):
mandatory_fields.append(field)

if len(mandatory_fields) > 0:
message= 'Mandatory fields required in Work Permit form<br><br><ul>'
message= 'Mandatory fields required in MOI Residency Jawazat form<br><br><ul>'
for mandatory_field in mandatory_fields:
message += '<li>' + mandatory_field +'</li>'
message += '</ul>'
Expand Down
14 changes: 14 additions & 0 deletions one_fm/grd/doctype/paci/paci.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@
from one_fm.utils import is_scheduler_emails_enabled

class PACI(Document):
def before_insert(self):
self.cancel_existing()

def cancel_existing(self):
"""Cancel documents for that employee which were created in previous years"""
year_threshold = getdate(self.date_of_application).year or getdate().year
first_day_of_year = getdate(f'01-01-{year_threshold}') #Get the first day of the year
existing_docs = frappe.get_all(self.doctype,{'date_of_application':['<',first_day_of_year],'name':['!=',self.name],'docstatus':0,'employee':self.employee})
if existing_docs:
for one in existing_docs:
frappe.db.set_value(self.doctype,one,'workflow_state','Cancelled')
frappe.db.commit()


def validate(self):
self.set_grd_values()
self.set_new_expiry_date()
Expand Down
15 changes: 15 additions & 0 deletions one_fm/grd/doctype/preparation/preparation.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,21 @@ var caclulate_renewal_extension_cost_total = function(frm, child) {

//Set renewal for all employee to facilitate process
frappe.ui.form.on("Preparation", {
refresh : frm=>{
if(frm.doc.docstatus==1){
if(!frappe.user.has_role("HR Manager")){
cur_frm.fields_dict.preparation_record.grid.update_docfield_property("renewal_or_extend", "allow_on_submit", 0);
cur_frm.fields_dict.preparation_record.grid.update_docfield_property("no_of_years", "allow_on_submit", 0);
cur_frm.fields_dict.preparation_record.grid.update_docfield_property("work_permit_amount", "allow_on_submit", 0);
cur_frm.fields_dict.preparation_record.grid.update_docfield_property("medical_insurance_amount", "allow_on_submit", 0);
cur_frm.fields_dict.preparation_record.grid.update_docfield_property("residency_stamp_amount", "allow_on_submit", 0);
cur_frm.fields_dict.preparation_record.grid.update_docfield_property("civil_id_amount", "allow_on_submit", 0);

}


}
},
set_renewal_for_all: function(frm) {
frappe.call({
doc: frm.doc,
Expand Down
100 changes: 100 additions & 0 deletions one_fm/grd/doctype/preparation/preparation.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,44 @@
from one_fm.processor import sendemail

class Preparation(Document):

def on_update_after_submit(self):
self.compare_preparation_record()

def compare_preparation_record(self):
"""Compare the data of preparation record child table before it was saved with the most updated version
and flag changes
"""
old_preparation_record = {}
new_preparation_record = {}
old_doc = self.get_doc_before_save()
for each in old_doc.preparation_record:
old_preparation_record[each.name] = each
old_row_ids = [i.name for i in old_doc.preparation_record ]
for one in self.preparation_record:
new_preparation_record[one.name] = one
new_row_ids = [i.name for i in self.preparation_record ]

for ind in old_row_ids: #Find Removed rows
if ind not in new_row_ids:
# self.fetch_linked_records(existing_preparation_record.get(ind)) # Delete for this employee
cancel_delete_doc("PACI",self.name,old_preparation_record.get(ind))
cancel_delete_doc("Work Permit",self.name,old_preparation_record.get(ind))
cancel_delete_doc("MOI Residency Jawazat",self.name,old_preparation_record.get(ind))
cancel_delete_doc("Medical Insurance",self.name,old_preparation_record.get(ind))
else:
if old_preparation_record.get(ind).get('renewal_or_extend')!=new_preparation_record.get(ind).get('renewal_or_extend'):
handle_renewal_changes(old_preparation_record.get(ind),new_preparation_record.get(ind),self.name) #Create or Delete based on choices
for each in new_row_ids: #Find New rows added
if each not in old_row_ids:
handle_creation_of_grd_docs(new_preparation_record.get(each),self.name)







def validate(self):
self.set_grd_values()
self.set_hr_values()
Expand Down Expand Up @@ -239,3 +277,65 @@ def get_grd_renewal_extension_cost(renewal_or_extend, no_of_years=False):
result = frappe.db.sql(query, as_dict=True)
if result and len(result) > 0:
return result[0]

def handle_creation_of_grd_docs(row,source):
"""
Handle the creation of grd documents for new rows just added after the submission of a preparation document
Args:
row (dict): dict containing employee information
"""
try:
employee_doc = frappe.get_doc("Employee",row.employee)
work_permit.create_wp_renewal(employee_doc,row.renewal_or_extend,source)
frappe.db.commit() #because Medical Insurance depends on the work permit
medical_insurance.create_mi_record(frappe.get_doc('Work Permit',{'preparation':source,'employee':employee_doc.employee}))
moi_residency_jawazat.create_moi_record(employee_doc,row.renewal_or_extend,preparation_name=source)
paci.create_PACI(employee_doc,row.renewal_or_extend,source)
except:
frappe.log_error(title=f"Error Creating New GRD documents for {row.employee} </b>",message=frappe.get_traceback())


def handle_renewal_changes(old_,new_,source):
"""
Handle the changes in renewal field of a row in the preparation record table
Args:
old (dict): a dict containing details of the old row
new (dict): a dict containing details of the new row
"""
if old_.renewal_or_extend == "Renewal" and new_.renewal_or_extend in ['Extend 1 month','Extend 2 months','Extend 3 months']:
cancel_delete_doc("PACI",source,new_)
cancel_delete_doc("Medical Insurance",source,new_)
cancel_delete_doc("Work Permit",source,new_)
elif new_.renewal_or_extend == "Cancellation":
cancel_delete_doc("MOI Residency Jawazat",source,new_)
cancel_delete_doc("PACI",source,new_)
cancel_delete_doc("Medical Insurance",source,new_)
cancel_delete_doc("Work Permit",source,new_)
elif new_.renewal_or_extend == "Renewal" and old_.renewal_or_extend != "Renewal":
handle_creation_of_grd_docs(new_,source)
#Create for all



def cancel_delete_doc(doctype,source,row):
"""
Loop through a list of records, cancel and delete them
Args:
doctype (str): a doctype
records (dict): a dict of records
"""
records = frappe.get_all(doctype,{'preparation':source,'employee':row.employee},['docstatus','name'])

for each in records:
try:
doc = frappe.get_doc(doctype,each.name)
doc.flags.ignore_links = 1
doc.flags.ignore_permissions = 1
doc.save()
if each.docstatus == 1:
doc.cancel()
frappe.delete_doc(doctype,each.name,force= True)
except:
frappe.log_error(title=f"Error Cancelling and Deleting <b>{doctype} {each.name} </b>",message=frappe.get_traceback())

continue
Loading

0 comments on commit 6407584

Please sign in to comment.