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) |
| 12 | |
| 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 | |
| 32 | def job(deserialized_data, from_doctype, to_doctype): |
| 33 | failed_history = [] |
| 34 | i = 0 |
| 35 | for d in deserialized_data: |
| 36 | failed = [] |
| 37 | |
| 38 | try: |
| 39 | i += 1 |
| 40 | doc_name = d.get("name") |
| 41 | frappe.db.savepoint("before_creation_state") |
| 42 | task(doc_name, from_doctype, to_doctype) |
| 43 | |
| 44 | except Exception as e: |
| 45 | frappe.db.rollback(save_point="before_creation_state") |
| 46 | failed_history.append(e) |
| 47 | failed.append(e) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 48 | update_logger( |
| 49 | doc_name, e, from_doctype, to_doctype, status="Failed", log_date=str(date.today()) |
| 50 | ) |
Mohammed Yusuf Shaikh | a3e69cf | 2022-02-08 01:00:37 +0530 | [diff] [blame] | 51 | if not failed: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 52 | update_logger( |
| 53 | doc_name, None, from_doctype, to_doctype, status="Success", log_date=str(date.today()) |
| 54 | ) |
Mohammed Yusuf Shaikh | a3e69cf | 2022-02-08 01:00:37 +0530 | [diff] [blame] | 55 | |
| 56 | show_job_status(failed_history, deserialized_data, to_doctype) |
| 57 | |
| 58 | |
| 59 | def task(doc_name, from_doctype, to_doctype): |
| 60 | from erpnext.accounts.doctype.payment_entry import payment_entry |
| 61 | from erpnext.accounts.doctype.purchase_invoice import purchase_invoice |
| 62 | from erpnext.accounts.doctype.sales_invoice import sales_invoice |
| 63 | from erpnext.buying.doctype.purchase_order import purchase_order |
| 64 | from erpnext.buying.doctype.supplier_quotation import supplier_quotation |
| 65 | from erpnext.selling.doctype.quotation import quotation |
| 66 | from erpnext.selling.doctype.sales_order import sales_order |
| 67 | from erpnext.stock.doctype.delivery_note import delivery_note |
| 68 | from erpnext.stock.doctype.purchase_receipt import purchase_receipt |
| 69 | |
| 70 | mapper = { |
| 71 | "Sales Order": { |
| 72 | "Sales Invoice": sales_order.make_sales_invoice, |
| 73 | "Delivery Note": sales_order.make_delivery_note, |
| 74 | "Advance Payment": payment_entry.get_payment_entry, |
| 75 | }, |
| 76 | "Sales Invoice": { |
| 77 | "Delivery Note": sales_invoice.make_delivery_note, |
| 78 | "Payment": payment_entry.get_payment_entry, |
| 79 | }, |
| 80 | "Delivery Note": { |
| 81 | "Sales Invoice": delivery_note.make_sales_invoice, |
| 82 | "Packing Slip": delivery_note.make_packing_slip, |
| 83 | }, |
| 84 | "Quotation": { |
| 85 | "Sales Order": quotation.make_sales_order, |
| 86 | "Sales Invoice": quotation.make_sales_invoice, |
| 87 | }, |
| 88 | "Supplier Quotation": { |
| 89 | "Purchase Order": supplier_quotation.make_purchase_order, |
| 90 | "Purchase Invoice": supplier_quotation.make_purchase_invoice, |
| 91 | "Advance Payment": payment_entry.get_payment_entry, |
| 92 | }, |
| 93 | "Purchase Order": { |
| 94 | "Purchase Invoice": purchase_order.make_purchase_invoice, |
| 95 | "Purchase Receipt": purchase_order.make_purchase_receipt, |
| 96 | }, |
| 97 | "Purhcase Invoice": { |
| 98 | "Purchase Receipt": purchase_invoice.make_purchase_receipt, |
| 99 | "Payment": payment_entry.get_payment_entry, |
| 100 | }, |
| 101 | "Purchase Receipt": {"Purchase Invoice": purchase_receipt.make_purchase_invoice}, |
| 102 | } |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 103 | if to_doctype in ["Advance Payment", "Payment"]: |
Mohammed Yusuf Shaikh | a3e69cf | 2022-02-08 01:00:37 +0530 | [diff] [blame] | 104 | obj = mapper[from_doctype][to_doctype](from_doctype, doc_name) |
| 105 | else: |
| 106 | obj = mapper[from_doctype][to_doctype](doc_name) |
| 107 | |
| 108 | obj.flags.ignore_validate = True |
| 109 | obj.insert(ignore_mandatory=True) |
| 110 | |
| 111 | |
| 112 | def check_logger_doc_exists(log_date): |
| 113 | return frappe.db.exists("Bulk Transaction Log", log_date) |
| 114 | |
| 115 | |
| 116 | def get_logger_doc(log_date): |
| 117 | return frappe.get_doc("Bulk Transaction Log", log_date) |
| 118 | |
| 119 | |
| 120 | def create_logger_doc(): |
| 121 | log_doc = frappe.new_doc("Bulk Transaction Log") |
| 122 | log_doc.set_new_name(set_name=str(date.today())) |
| 123 | log_doc.log_date = date.today() |
| 124 | |
| 125 | return log_doc |
| 126 | |
| 127 | |
| 128 | def append_data_to_logger(log_doc, doc_name, error, from_doctype, to_doctype, status, restarted): |
| 129 | row = log_doc.append("logger_data", {}) |
| 130 | row.transaction_name = doc_name |
| 131 | row.date = date.today() |
| 132 | now = datetime.now() |
| 133 | row.time = now.strftime("%H:%M:%S") |
| 134 | row.transaction_status = status |
| 135 | row.error_description = str(error) |
| 136 | row.from_doctype = from_doctype |
| 137 | row.to_doctype = to_doctype |
| 138 | row.retried = restarted |
| 139 | |
| 140 | |
| 141 | def update_logger(doc_name, e, from_doctype, to_doctype, status, log_date=None, restarted=0): |
| 142 | if not check_logger_doc_exists(log_date): |
| 143 | log_doc = create_logger_doc() |
| 144 | append_data_to_logger(log_doc, doc_name, e, from_doctype, to_doctype, status, restarted) |
| 145 | log_doc.insert() |
| 146 | else: |
| 147 | log_doc = get_logger_doc(log_date) |
| 148 | if record_exists(log_doc, doc_name, status): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 149 | 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] | 150 | log_doc.save() |
| 151 | |
| 152 | |
| 153 | def show_job_status(failed_history, deserialized_data, to_doctype): |
| 154 | if not failed_history: |
| 155 | frappe.msgprint( |
| 156 | _("Creation of {0} successful").format(to_doctype), |
| 157 | title="Successful", |
| 158 | indicator="green", |
| 159 | ) |
| 160 | |
| 161 | if len(failed_history) != 0 and len(failed_history) < len(deserialized_data): |
| 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 | ) |
| 170 | |
| 171 | if len(failed_history) == len(deserialized_data): |
| 172 | frappe.msgprint( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 173 | _( |
| 174 | """Creation of {0} failed. |
| 175 | Check <b><a href="/app/bulk-transaction-log">Bulk Transaction Log</a></b>""" |
| 176 | ).format(to_doctype), |
Mohammed Yusuf Shaikh | a3e69cf | 2022-02-08 01:00:37 +0530 | [diff] [blame] | 177 | title="Failed", |
| 178 | indicator="red", |
| 179 | ) |
| 180 | |
| 181 | |
| 182 | def record_exists(log_doc, doc_name, status): |
| 183 | |
| 184 | record = mark_retrired_transaction(log_doc, doc_name) |
| 185 | |
| 186 | if record and status == "Failed": |
| 187 | return False |
| 188 | elif record and status == "Success": |
| 189 | return True |
| 190 | else: |
| 191 | return True |
| 192 | |
| 193 | |
| 194 | def mark_retrired_transaction(log_doc, doc_name): |
| 195 | record = 0 |
| 196 | for d in log_doc.get("logger_data"): |
| 197 | if d.transaction_name == doc_name and d.transaction_status == "Failed": |
| 198 | d.retried = 1 |
| 199 | record = record + 1 |
| 200 | |
| 201 | log_doc.save() |
| 202 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 203 | return record |