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