Hi all,
i am currently making a doctype called “SPK” and i need when i create SPK it will automatically create additional salary on other site. I already develope the code, but still doesn’t create the additional salary automatically. the SPK is on site 156 while the additional salary is on port 8001, maybe anyone could help me. below i give my code too. thanks
import frappe
from frappe.model.document import Document
import datetime
import requests
from requests.exceptions import RequestException
from frappe.utils import nowdate
from frappe.model.workflow import get_workflow_name
from frappe.workflow.doctype.workflow_action.workflow_action import get_doc_workflow_state
class SuratPerintahKerja(Document):
def on_update(self):
if not get_workflow_name(self.get(“doctype”)):
return
check_additional_salary(self)
def before_cancel(self):
self.check_role_and_set_workflow()
check_additional_salary(self,cancel=True)
def on_trash(self):
check_additional_salary(self)
def check_role_and_set_workflow(self):
self.workflow_state = "Cancelled"
workflow_name = get_workflow_name(self.get("doctype"))
allow_edit_role = frappe.get_value("Workflow Document State", filters={'doc_status': 2, 'parent': workflow_name}, fieldname='allow_edit')
roles = frappe.get_roles(frappe.session.user)
if allow_edit_role not in roles:
frappe.throw("Current User doesn't have Role {} to Cancel this Document".format(allow_edit_role))
def check_additional_salary(self, cancel=False):
list_additional_salary = frappe.db.get_list(“Additional Salary”, {“ref_doctype”:self.doctype, “ref_docname”:self.name, “docstatus”:[“!=”,2]}, “employee”)
list_additional_salary = [d[‘employee’] for d in list_additional_salary]
list_employee = []
if cancel == False:
for m in self.team_members:
list_employee.append(m.employee_member)
for m in self.production_members:
list_employee.append(m.employee)
create_delete_additional_salary_on_hr_site(self.team_members, list_employee, self.doctype, self.name, list_additional_salary, self.project_name)
def create_delete_additional_salary_on_hr_site(members, list_employee, doctype, name, list_additional_salary, project):
create_add_sal = set(list_employee) - set(list_additional_salary)
delete_add_sal = set(list_additional_salary) - set(list_employee)
for member in create_add_sal:
create_additional_salary_on_hr_site(member, doctype, name, project)
for member in delete_add_sal:
create_additional_salary_on_hr_site(member, doctype, name, project)
def create_additional_salary_on_hr_site(employee, doctype, name, project):
try:
url = “http://192.168.1.156:8001/api/resource/Additional Salary”
data = {
"employee": employee,
"salary_component": frappe.db.get_value("Payroll Settings", "Payroll Settings", "salary_component_bonus"),
"payroll_date": nowdate(),
"type": "Earning" ,
"remarks": project,
"amount": 0,
"ref_doctype": doctype,
"ref_docname": name
}
headers = {
"Content-Type": "application/json",
"Authorization": "token 39cc02b1141e1d4:4f3413fccb40877"
}
response = requests.post(url, data=data.json, headers=headers)
frappe.log_error(f"Response status code: {response.status_code}")
response.raise_for_status()
frappe.msgprint(f"Response from API: {response.text}")
return {"status": "success", "message": "Successfully created additional Salary"}
except RequestException as e:
frappe.log_error(frappe.get_traceback(), "Error Creating Additional Salary")
frappe.msgprint(f"Error: {str(e)}")
return {"status": "error", "message": str(e)}