Mohammed Yusuf Shaikh | a3e69cf | 2022-02-08 01:00:37 +0530 | [diff] [blame] | 1 | import json |
| 2 | from datetime import date, datetime |
| 3 | |
| 4 | import frappe |
| 5 | from frappe import _ |
| 6 | |
| 7 | |
| 8 | @frappe.whitelist() |
| 9 | def transaction_processing(data, from_doctype, to_doctype): |
| 10 | if isinstance(data, str): |
| 11 | deserialized_data = json.loads(data) |
Mohammed Yusuf Shaikh | a3e69cf | 2022-02-08 01:00:37 +0530 | [diff] [blame] | 12 | else: |
| 13 | deserialized_data = data |
| 14 | |
| 15 | length_of_data = len(deserialized_data) |
| 16 | |
| 17 | if length_of_data > 10: |
| 18 | frappe.msgprint( |
| 19 | _("Started a background job to create {1} {0}").format(to_doctype, length_of_data) |
| 20 | ) |
| 21 | frappe.enqueue( |
| 22 | job, |
| 23 | deserialized_data=deserialized_data, |
| 24 | from_doctype=from_doctype, |
| 25 | to_doctype=to_doctype, |
| 26 | ) |
| 27 | else: |
| 28 | job(deserialized_data, from_doctype, to_doctype) |
| 29 | |
| 30 | |
| 31 | def job(deserialized_data, from_doctype, to_doctype): |
Dany Robert | 9105515 | 2022-10-03 10:59:53 +0530 | [diff] [blame] | 32 | fail_count = 0 |
Mohammed Yusuf Shaikh | a3e69cf | 2022-02-08 01:00:37 +0530 | [diff] [blame] | 33 | for d in deserialized_data: |
Mohammed Yusuf Shaikh | a3e69cf | 2022-02-08 01:00:37 +0530 | [diff] [blame] | 34 | try: |
Mohammed Yusuf Shaikh | a3e69cf | 2022-02-08 01:00:37 +0530 | [diff] [blame] | 35 | doc_name = d.get("name") |
| 36 | frappe.db.savepoint("before_creation_state") |
| 37 | task(doc_name, from_doctype, to_doctype) |
Mohammed Yusuf Shaikh | a3e69cf | 2022-02-08 01:00:37 +0530 | [diff] [blame] | 38 | except Exception as e: |
| 39 | frappe.db.rollback(save_point="before_creation_state") |
Dany Robert | 9105515 | 2022-10-03 10:59:53 +0530 | [diff] [blame] | 40 | fail_count += 1 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 41 | update_logger( |
Dany Robert | 9105515 | 2022-10-03 10:59:53 +0530 | [diff] [blame] | 42 | doc_name, |
| 43 | str(frappe.get_traceback()), |
| 44 | from_doctype, |
| 45 | to_doctype, |
| 46 | status="Failed", |
| 47 | log_date=str(date.today()), |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 48 | ) |
Dany Robert | 9105515 | 2022-10-03 10:59:53 +0530 | [diff] [blame] | 49 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 50 | update_logger( |
| 51 | doc_name, None, from_doctype, to_doctype, status="Success", log_date=str(date.today()) |
| 52 | ) |
Mohammed Yusuf Shaikh | a3e69cf | 2022-02-08 01:00:37 +0530 | [diff] [blame] | 53 | |
Dany Robert | 9105515 | 2022-10-03 10:59:53 +0530 | [diff] [blame] | 54 | show_job_status(fail_count, len(deserialized_data), to_doctype) |
Mohammed Yusuf Shaikh | a3e69cf | 2022-02-08 01:00:37 +0530 | [diff] [blame] | 55 | |
| 56 | |
| 57 | def task(doc_name, from_doctype, to_doctype): |
| 58 | from erpnext.accounts.doctype.payment_entry import payment_entry |
| 59 | from erpnext.accounts.doctype.purchase_invoice import purchase_invoice |
| 60 | from erpnext.accounts.doctype.sales_invoice import sales_invoice |
| 61 | from erpnext.buying.doctype.purchase_order import purchase_order |
| 62 | from erpnext.buying.doctype.supplier_quotation import supplier_quotation |
| 63 | from erpnext.selling.doctype.quotation import quotation |
| 64 | from erpnext.selling.doctype.sales_order import sales_order |
| 65 | from erpnext.stock.doctype.delivery_note import delivery_note |
| 66 | from erpnext.stock.doctype.purchase_receipt import purchase_receipt |
| 67 | |
| 68 | mapper = { |
| 69 | "Sales Order": { |
| 70 | "Sales Invoice": sales_order.make_sales_invoice, |
| 71 | "Delivery Note": sales_order.make_delivery_note, |
Solufy Solution | f1acc5f | 2023-04-25 19:16:30 +0530 | [diff] [blame] | 72 | "Payment Entry": payment_entry.get_payment_entry, |
Mohammed Yusuf Shaikh | a3e69cf | 2022-02-08 01:00:37 +0530 | [diff] [blame] | 73 | }, |
| 74 | "Sales Invoice": { |
| 75 | "Delivery Note": sales_invoice.make_delivery_note, |
Deepesh Garg | 2dfe849 | 2022-11-17 15:53:56 +0530 | [diff] [blame] | 76 | "Payment Entry": payment_entry.get_payment_entry, |
Mohammed Yusuf Shaikh | a3e69cf | 2022-02-08 01:00:37 +0530 | [diff] [blame] | 77 | }, |
| 78 | "Delivery Note": { |
| 79 | "Sales Invoice": delivery_note.make_sales_invoice, |
| 80 | "Packing Slip": delivery_note.make_packing_slip, |
| 81 | }, |
| 82 | "Quotation": { |
| 83 | "Sales Order": quotation.make_sales_order, |
| 84 | "Sales Invoice": quotation.make_sales_invoice, |
| 85 | }, |
| 86 | "Supplier Quotation": { |
| 87 | "Purchase Order": supplier_quotation.make_purchase_order, |
| 88 | "Purchase Invoice": supplier_quotation.make_purchase_invoice, |
Mohammed Yusuf Shaikh | a3e69cf | 2022-02-08 01:00:37 +0530 | [diff] [blame] | 89 | }, |
| 90 | "Purchase Order": { |
| 91 | "Purchase Invoice": purchase_order.make_purchase_invoice, |
| 92 | "Purchase Receipt": purchase_order.make_purchase_receipt, |
Solufy Solution | f1acc5f | 2023-04-25 19:16:30 +0530 | [diff] [blame] | 93 | "Payment Entry": payment_entry.get_payment_entry, |
Mohammed Yusuf Shaikh | a3e69cf | 2022-02-08 01:00:37 +0530 | [diff] [blame] | 94 | }, |
Dany Robert | 9105515 | 2022-10-03 10:59:53 +0530 | [diff] [blame] | 95 | "Purchase Invoice": { |
Mohammed Yusuf Shaikh | a3e69cf | 2022-02-08 01:00:37 +0530 | [diff] [blame] | 96 | "Purchase Receipt": purchase_invoice.make_purchase_receipt, |
Deepesh Garg | 2dfe849 | 2022-11-17 15:53:56 +0530 | [diff] [blame] | 97 | "Payment Entry": payment_entry.get_payment_entry, |
Mohammed Yusuf Shaikh | a3e69cf | 2022-02-08 01:00:37 +0530 | [diff] [blame] | 98 | }, |
| 99 | "Purchase Receipt": {"Purchase Invoice": purchase_receipt.make_purchase_invoice}, |
| 100 | } |
Solufy Solution | f1acc5f | 2023-04-25 19:16:30 +0530 | [diff] [blame] | 101 | if to_doctype in ["Payment Entry"]: |
Mohammed Yusuf Shaikh | a3e69cf | 2022-02-08 01:00:37 +0530 | [diff] [blame] | 102 | obj = mapper[from_doctype][to_doctype](from_doctype, doc_name) |
| 103 | else: |
| 104 | obj = mapper[from_doctype][to_doctype](doc_name) |
| 105 | |
| 106 | obj.flags.ignore_validate = True |
HarryPaulo | 22290c2 | 2023-04-25 10:43:53 -0300 | [diff] [blame] | 107 | obj.set_title_field() |
Mohammed Yusuf Shaikh | a3e69cf | 2022-02-08 01:00:37 +0530 | [diff] [blame] | 108 | obj.insert(ignore_mandatory=True) |
| 109 | |
| 110 | |
| 111 | def check_logger_doc_exists(log_date): |
| 112 | return frappe.db.exists("Bulk Transaction Log", log_date) |
| 113 | |
| 114 | |
| 115 | def get_logger_doc(log_date): |
| 116 | return frappe.get_doc("Bulk Transaction Log", log_date) |
| 117 | |
| 118 | |
| 119 | def create_logger_doc(): |
| 120 | log_doc = frappe.new_doc("Bulk Transaction Log") |
| 121 | log_doc.set_new_name(set_name=str(date.today())) |
| 122 | log_doc.log_date = date.today() |
| 123 | |
| 124 | return log_doc |
| 125 | |
| 126 | |
| 127 | def append_data_to_logger(log_doc, doc_name, error, from_doctype, to_doctype, status, restarted): |
| 128 | row = log_doc.append("logger_data", {}) |
| 129 | row.transaction_name = doc_name |
| 130 | row.date = date.today() |
| 131 | now = datetime.now() |
| 132 | row.time = now.strftime("%H:%M:%S") |
| 133 | row.transaction_status = status |
| 134 | row.error_description = str(error) |
| 135 | row.from_doctype = from_doctype |
| 136 | row.to_doctype = to_doctype |
| 137 | row.retried = restarted |
| 138 | |
| 139 | |
| 140 | def update_logger(doc_name, e, from_doctype, to_doctype, status, log_date=None, restarted=0): |
| 141 | if not check_logger_doc_exists(log_date): |
| 142 | log_doc = create_logger_doc() |
| 143 | append_data_to_logger(log_doc, doc_name, e, from_doctype, to_doctype, status, restarted) |
| 144 | log_doc.insert() |
| 145 | else: |
| 146 | log_doc = get_logger_doc(log_date) |
| 147 | if record_exists(log_doc, doc_name, status): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 148 | append_data_to_logger(log_doc, doc_name, e, from_doctype, to_doctype, status, restarted) |
Mohammed Yusuf Shaikh | a3e69cf | 2022-02-08 01:00:37 +0530 | [diff] [blame] | 149 | log_doc.save() |
| 150 | |
| 151 | |
Dany Robert | 9105515 | 2022-10-03 10:59:53 +0530 | [diff] [blame] | 152 | def show_job_status(fail_count, deserialized_data_count, to_doctype): |
| 153 | if not fail_count: |
Mohammed Yusuf Shaikh | a3e69cf | 2022-02-08 01:00:37 +0530 | [diff] [blame] | 154 | frappe.msgprint( |
Deepesh Garg | 2dfe849 | 2022-11-17 15:53:56 +0530 | [diff] [blame] | 155 | _("Creation of <b><a href='/app/{0}'>{1}(s)</a></b> successful").format( |
| 156 | to_doctype.lower().replace(" ", "-"), to_doctype |
| 157 | ), |
Mohammed Yusuf Shaikh | a3e69cf | 2022-02-08 01:00:37 +0530 | [diff] [blame] | 158 | title="Successful", |
| 159 | indicator="green", |
| 160 | ) |
Dany Robert | 9105515 | 2022-10-03 10:59:53 +0530 | [diff] [blame] | 161 | elif fail_count != 0 and fail_count < deserialized_data_count: |
Mohammed Yusuf Shaikh | a3e69cf | 2022-02-08 01:00:37 +0530 | [diff] [blame] | 162 | frappe.msgprint( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 163 | _( |
| 164 | """Creation of {0} partially successful. |
| 165 | Check <b><a href="/app/bulk-transaction-log">Bulk Transaction Log</a></b>""" |
| 166 | ).format(to_doctype), |
Mohammed Yusuf Shaikh | a3e69cf | 2022-02-08 01:00:37 +0530 | [diff] [blame] | 167 | title="Partially successful", |
| 168 | indicator="orange", |
| 169 | ) |
Dany Robert | 9105515 | 2022-10-03 10:59:53 +0530 | [diff] [blame] | 170 | else: |
Mohammed Yusuf Shaikh | a3e69cf | 2022-02-08 01:00:37 +0530 | [diff] [blame] | 171 | frappe.msgprint( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 172 | _( |
| 173 | """Creation of {0} failed. |
| 174 | Check <b><a href="/app/bulk-transaction-log">Bulk Transaction Log</a></b>""" |
| 175 | ).format(to_doctype), |
Mohammed Yusuf Shaikh | a3e69cf | 2022-02-08 01:00:37 +0530 | [diff] [blame] | 176 | title="Failed", |
| 177 | indicator="red", |
| 178 | ) |
| 179 | |
| 180 | |
| 181 | def record_exists(log_doc, doc_name, status): |
Mohammed Yusuf Shaikh | a3e69cf | 2022-02-08 01:00:37 +0530 | [diff] [blame] | 182 | record = mark_retrired_transaction(log_doc, doc_name) |
Mohammed Yusuf Shaikh | a3e69cf | 2022-02-08 01:00:37 +0530 | [diff] [blame] | 183 | if record and status == "Failed": |
| 184 | return False |
| 185 | elif record and status == "Success": |
| 186 | return True |
| 187 | else: |
| 188 | return True |
| 189 | |
| 190 | |
| 191 | def mark_retrired_transaction(log_doc, doc_name): |
| 192 | record = 0 |
| 193 | for d in log_doc.get("logger_data"): |
| 194 | if d.transaction_name == doc_name and d.transaction_status == "Failed": |
ruthra kumar | 416bd40 | 2023-11-07 17:38:07 +0530 | [diff] [blame] | 195 | frappe.db.set_value("Bulk Transaction Log Detail", d.name, "retried", 1) |
Mohammed Yusuf Shaikh | a3e69cf | 2022-02-08 01:00:37 +0530 | [diff] [blame] | 196 | record = record + 1 |
| 197 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 198 | return record |