diff --git a/autoinscribe/autoinscribe/doctype/autoinscribe_integration/autoinscribe_integration.py b/autoinscribe/autoinscribe/doctype/autoinscribe_integration/autoinscribe_integration.py index 62dbee8..7bf83af 100644 --- a/autoinscribe/autoinscribe/doctype/autoinscribe_integration/autoinscribe_integration.py +++ b/autoinscribe/autoinscribe/doctype/autoinscribe_integration/autoinscribe_integration.py @@ -113,4 +113,16 @@ def create_address(self, address): doc.insert() return doc else: - return None \ No newline at end of file + return None + +@frappe.whitelist() +def extract_text_from_img(img_url): + doc = frappe.get_single("AutoInscribe Integration") + return doc.extract_text_from_img(img_url) + + +@frappe.whitelist() +def create_address(address): + doc = frappe.get_single("AutoInscribe Integration") + return doc.create_address(address) + diff --git a/autoinscribe/hooks.py b/autoinscribe/hooks.py index 279c287..5cc20e6 100644 --- a/autoinscribe/hooks.py +++ b/autoinscribe/hooks.py @@ -29,6 +29,10 @@ # include js in doctype views # doctype_js = {"doctype" : "public/js/doctype.js"} + +doctype_js = {"Lead" : "public/js/lead.js", "Contact": "public/js/contact.js"} + + # doctype_list_js = {"doctype" : "public/js/doctype_list.js"} # doctype_tree_js = {"doctype" : "public/js/doctype_tree.js"} # doctype_calendar_js = {"doctype" : "public/js/doctype_calendar.js"} diff --git a/autoinscribe/inscribe/api.py b/autoinscribe/inscribe/api.py deleted file mode 100644 index c6adb96..0000000 --- a/autoinscribe/inscribe/api.py +++ /dev/null @@ -1,15 +0,0 @@ -'''Responsible for calling methods in doctype class "AutoInscribe Integration"''' - -import frappe - -doc = frappe.get_single("AutoInscribe Integration") - -@frappe.whitelist() -def extract_text_from_img(img_url): - return doc.extract_text_from_img(img_url) - - -@frappe.whitelist() -def create_address(address): - return doc.create_address(address) - diff --git a/autoinscribe/public/js/contact.js b/autoinscribe/public/js/contact.js new file mode 100644 index 0000000..f2631d0 --- /dev/null +++ b/autoinscribe/public/js/contact.js @@ -0,0 +1,88 @@ +frappe.ui.form.on("Contact", { + custom_upload_image(frm) { + if (frm.selected_doc.custom_upload_image) { + frappe.call({ + method: + "autoinscribe.autoinscribe.doctype.autoinscribe_integration.autoinscribe_integration.extract_text_from_img", + args: { + img_url: + window.location.href.split("/app")[0] + + frm.selected_doc.custom_upload_image, + }, + callback(res) { + const resArr = res.message.split("\n"); + + const first_name = resArr[0].split(":")[1].trim(); + const middle_name = resArr[1].split(":")[1].trim(); + const last_name = resArr[2].split(":")[1].trim(); + first_name !== "NULL" && frm.set_value("first_name", first_name); + middle_name !== "NULL" && frm.set_value("middle_name", middle_name); + last_name !== "NULL" && frm.set_value("last_name", last_name); + const company_name = resArr[8].split(":")[1].trim(); + company_name !== "NULL" && + frm.set_value("company_name", company_name); + const designation = resArr[5].split(":")[1].trim(); + designation !== "NULL" && frm.set_value("designation", designation); + const email_ids = resArr[7].split(":")[1].trim().split(","); + email_ids.forEach((email) => { + email.trim() !== "NULL" && + frm.add_child("email_ids", { + email_id: email.trim(), + }); + }); + frm.refresh_field("email_ids"); + const contact_numbers = resArr[6].split(":")[1].trim().split(","); + contact_numbers.forEach((phone_no) => { + phone_no.trim() !== "NULL" && + frm.add_child("phone_nos", { + phone: phone_no.trim(), + }); + }); + frm.refresh_field("phone_nos"); + + let gender = resArr[3].split(":")[1].trim(); + gender = gender.charAt(0).toUpperCase() + gender.slice(1); + frappe.db.exists("Gender", gender).then((gender_exists) => { + if (gender_exists) { + frm.set_value("gender", gender); + } + }); + frm.refresh_field("gender"); + let salutation = resArr[4].split(":")[1].trim(); + salutation = + salutation.charAt(0).toUpperCase() + + salutation.slice( + 1, + salutation[salutation.length - 1] === "." + ? salutation.length - 1 + : salutation.length + ); + frappe.db + .exists("Salutation", salutation) + .then((salutation_exists) => { + if (salutation_exists) { + frm.set_value("salutation", salutation); + } + }); + frm.refresh_field("salutation"); + + const address = resArr[10].split(":")[1].trim(); + if (address !== "NULL") { + frappe.call({ + method: + "autoinscribe.autoinscribe.doctype.autoinscribe_integration.autoinscribe_integration.create_address", + args: { + address: address, + }, + callback(res) { + if (res.message) { + frm.set_value("address", `${address}-Office`); + } + }, + }); + } + }, + }); + } + }, +}); diff --git a/autoinscribe/public/js/lead.js b/autoinscribe/public/js/lead.js new file mode 100644 index 0000000..fc40a43 --- /dev/null +++ b/autoinscribe/public/js/lead.js @@ -0,0 +1,97 @@ +frappe.ui.form.on("Lead", { + custom_upload_business_card(frm) { + if (frm.selected_doc.custom_upload_business_card) { + frappe.call({ + method: + "autoinscribe.autoinscribe.doctype.autoinscribe_integration.autoinscribe_integration.extract_text_from_img", + args: { + img_url: + window.location.href.split("/app")[0] + + frm.selected_doc.custom_upload_business_card, + }, + callback(res) { + const resArr = res.message.split("\n"); + + const first_name = resArr[0].split(":")[1].trim(); + const middle_name = resArr[1].split(":")[1].trim(); + const last_name = resArr[2].split(":")[1].trim(); + first_name !== "NULL" && frm.set_value("first_name", first_name); + middle_name !== "NULL" && frm.set_value("middle_name", middle_name); + last_name !== "NULL" && frm.set_value("last_name", last_name); + const company_name = resArr[8].split(":")[1].trim(); + company_name !== "NULL" && + frm.set_value("company_name", company_name); + const designation = resArr[5].split(":")[1].trim(); + designation !== "NULL" && frm.set_value("job_title", designation); + const email_ids = resArr[7].split(":")[1].trim().split(","); + frm.set_value("email_id", email_ids[0] || ""); + + const mobile_no = resArr[11].split(":")[1].trim(); + mobile_no !== "NULL" && frm.set_value("mobile_no", mobile_no); + const phone = resArr[12].split(":")[1].trim(); + phone !== "NULL" && frm.set_value("phone", phone); + + const possibleGenders = [ + "Male", + "Female", + "Prefer not to say", + "Non-Conforming", + "Genderqueer", + "Transgender", + "Other", + ]; + let gender = resArr[3].split(":")[1].trim(); + gender = gender.charAt(0).toUpperCase() + gender.slice(1); + gender !== "NULL" && + possibleGenders.includes(gender) && + frm.set_value("gender", gender); + frm.refresh_field("gender"); + + const possibleSalutations = [ + "Mr", + "Ms", + "Mx", + "Dr", + "Mrs", + "Madam", + "Miss", + "Master", + "Prof", + ]; + let salutation = resArr[4].split(":")[1].trim(); + salutation = + salutation.charAt(0).toUpperCase() + + salutation.slice( + 1, + salutation[salutation.length - 1] === "." + ? salutation.length - 1 + : salutation.length + ); + salutation !== "NULL" && + possibleSalutations.includes(salutation) && + frm.set_value("salutation", salutation); + frm.refresh_field("salutation"); + + let website = resArr[9].split(":")[1].trim(); + frm.set_value("website", website); + + const city = resArr[13].split(":")[1].trim(); + city !== "NULL" && frm.set_value("city", city); + + const state = resArr[14].split(":")[1].trim(); + state !== "NULL" && frm.set_value("state", state); + + const country = resArr[15].split(":")[1].trim(); + frappe.db.exists("Country", country).then((country_exists) => { + if (country_exists) { + frm.set_value("country", country); + } else { + frm.set_value("country", ""); + } + }); + frm.refresh_field("country"); + }, + }); + } + }, +});