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