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