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