blob: df21b61139a2c8d5009e4833b233b2be1ba2c319 [file] [log] [blame]
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +05301import json
2from datetime import date, datetime
3
4import frappe
5from frappe import _
ruthra kumarc3202882023-11-10 13:45:52 +05306from frappe.utils import get_link_to_form, today
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +05307
8
9@frappe.whitelist()
10def transaction_processing(data, from_doctype, to_doctype):
11 if isinstance(data, str):
12 deserialized_data = json.loads(data)
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053013 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 kumar0aa16362023-11-10 12:20:30 +053032@frappe.whitelist()
ruthra kumarfb06ad72023-11-20 13:26:02 +053033def retry(date: str | None = None):
34 if not date:
35 date = today()
36
ruthra kumar0aa16362023-11-10 12:20:30 +053037 if date:
38 failed_docs = frappe.db.get_all(
39 "Bulk Transaction Log Detail",
40 filters={"date": date, "transaction_status": "Failed", "retried": 0},
41 fields=["name", "transaction_name", "from_doctype", "to_doctype"],
42 )
43 if not failed_docs:
ruthra kumarc3202882023-11-10 13:45:52 +053044 frappe.msgprint(_("There are no Failed transactions"))
45 else:
46 job = frappe.enqueue(
47 retry_failed_transactions,
48 failed_docs=failed_docs,
49 )
50 frappe.msgprint(
51 _("Job: {0} has been triggered for processing failed transactions").format(
52 get_link_to_form("RQ Job", job.id)
53 )
54 )
ruthra kumar0aa16362023-11-10 12:20:30 +053055
ruthra kumarc3202882023-11-10 13:45:52 +053056
57def retry_failed_transactions(failed_docs: list | None):
58 if failed_docs:
ruthra kumar0aa16362023-11-10 12:20:30 +053059 for log in failed_docs:
60 try:
61 frappe.db.savepoint("before_creation_state")
62 task(log.transaction_name, log.from_doctype, log.to_doctype)
63 except Exception as e:
64 frappe.db.rollback(save_point="before_creation_state")
ruthra kumara52a1b42023-11-11 05:20:27 +053065 update_log(log.name, "Failed", 1, str(frappe.get_traceback()))
ruthra kumar0aa16362023-11-10 12:20:30 +053066 else:
67 update_log(log.name, "Success", 1)
68
69
ruthra kumara52a1b42023-11-11 05:20:27 +053070def update_log(log_name, status, retried, err=None):
ruthra kumar0aa16362023-11-10 12:20:30 +053071 frappe.db.set_value("Bulk Transaction Log Detail", log_name, "transaction_status", status)
72 frappe.db.set_value("Bulk Transaction Log Detail", log_name, "retried", retried)
ruthra kumara52a1b42023-11-11 05:20:27 +053073 if err:
74 frappe.db.set_value("Bulk Transaction Log Detail", log_name, "error_description", err)
ruthra kumar0aa16362023-11-10 12:20:30 +053075
76
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053077def job(deserialized_data, from_doctype, to_doctype):
Dany Robert91055152022-10-03 10:59:53 +053078 fail_count = 0
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053079 for d in deserialized_data:
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053080 try:
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053081 doc_name = d.get("name")
82 frappe.db.savepoint("before_creation_state")
83 task(doc_name, from_doctype, to_doctype)
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053084 except Exception as e:
85 frappe.db.rollback(save_point="before_creation_state")
Dany Robert91055152022-10-03 10:59:53 +053086 fail_count += 1
ruthra kumarebd74a42023-11-09 16:38:34 +053087 create_log(
Dany Robert91055152022-10-03 10:59:53 +053088 doc_name,
89 str(frappe.get_traceback()),
90 from_doctype,
91 to_doctype,
92 status="Failed",
93 log_date=str(date.today()),
Ankush Menat494bd9e2022-03-28 18:52:46 +053094 )
Dany Robert91055152022-10-03 10:59:53 +053095 else:
ruthra kumarebd74a42023-11-09 16:38:34 +053096 create_log(
Ankush Menat494bd9e2022-03-28 18:52:46 +053097 doc_name, None, from_doctype, to_doctype, status="Success", log_date=str(date.today())
98 )
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053099
Dany Robert91055152022-10-03 10:59:53 +0530100 show_job_status(fail_count, len(deserialized_data), to_doctype)
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530101
102
103def task(doc_name, from_doctype, to_doctype):
104 from erpnext.accounts.doctype.payment_entry import payment_entry
105 from erpnext.accounts.doctype.purchase_invoice import purchase_invoice
106 from erpnext.accounts.doctype.sales_invoice import sales_invoice
107 from erpnext.buying.doctype.purchase_order import purchase_order
108 from erpnext.buying.doctype.supplier_quotation import supplier_quotation
109 from erpnext.selling.doctype.quotation import quotation
110 from erpnext.selling.doctype.sales_order import sales_order
111 from erpnext.stock.doctype.delivery_note import delivery_note
112 from erpnext.stock.doctype.purchase_receipt import purchase_receipt
113
114 mapper = {
115 "Sales Order": {
116 "Sales Invoice": sales_order.make_sales_invoice,
117 "Delivery Note": sales_order.make_delivery_note,
Solufy Solutionf1acc5f2023-04-25 19:16:30 +0530118 "Payment Entry": payment_entry.get_payment_entry,
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530119 },
120 "Sales Invoice": {
121 "Delivery Note": sales_invoice.make_delivery_note,
Deepesh Garg2dfe8492022-11-17 15:53:56 +0530122 "Payment Entry": payment_entry.get_payment_entry,
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530123 },
124 "Delivery Note": {
125 "Sales Invoice": delivery_note.make_sales_invoice,
126 "Packing Slip": delivery_note.make_packing_slip,
127 },
128 "Quotation": {
129 "Sales Order": quotation.make_sales_order,
130 "Sales Invoice": quotation.make_sales_invoice,
131 },
132 "Supplier Quotation": {
133 "Purchase Order": supplier_quotation.make_purchase_order,
134 "Purchase Invoice": supplier_quotation.make_purchase_invoice,
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530135 },
136 "Purchase Order": {
137 "Purchase Invoice": purchase_order.make_purchase_invoice,
138 "Purchase Receipt": purchase_order.make_purchase_receipt,
Solufy Solutionf1acc5f2023-04-25 19:16:30 +0530139 "Payment Entry": payment_entry.get_payment_entry,
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530140 },
Dany Robert91055152022-10-03 10:59:53 +0530141 "Purchase Invoice": {
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530142 "Purchase Receipt": purchase_invoice.make_purchase_receipt,
Deepesh Garg2dfe8492022-11-17 15:53:56 +0530143 "Payment Entry": payment_entry.get_payment_entry,
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530144 },
145 "Purchase Receipt": {"Purchase Invoice": purchase_receipt.make_purchase_invoice},
146 }
David Arnold426c2452023-11-14 11:13:05 +0100147 frappe.flags.bulk_transaction = True
Solufy Solutionf1acc5f2023-04-25 19:16:30 +0530148 if to_doctype in ["Payment Entry"]:
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530149 obj = mapper[from_doctype][to_doctype](from_doctype, doc_name)
150 else:
151 obj = mapper[from_doctype][to_doctype](doc_name)
152
153 obj.flags.ignore_validate = True
HarryPaulo22290c22023-04-25 10:43:53 -0300154 obj.set_title_field()
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530155 obj.insert(ignore_mandatory=True)
David Arnold426c2452023-11-14 11:13:05 +0100156 del frappe.flags.bulk_transaction
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530157
158
ruthra kumarebd74a42023-11-09 16:38:34 +0530159def create_log(doc_name, e, from_doctype, to_doctype, status, log_date=None, restarted=0):
160 transaction_log = frappe.new_doc("Bulk Transaction Log Detail")
161 transaction_log.transaction_name = doc_name
162 transaction_log.date = today()
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530163 now = datetime.now()
ruthra kumarebd74a42023-11-09 16:38:34 +0530164 transaction_log.time = now.strftime("%H:%M:%S")
165 transaction_log.transaction_status = status
166 transaction_log.error_description = str(e)
167 transaction_log.from_doctype = from_doctype
168 transaction_log.to_doctype = to_doctype
169 transaction_log.retried = restarted
170 transaction_log.save()
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530171
172
Dany Robert91055152022-10-03 10:59:53 +0530173def show_job_status(fail_count, deserialized_data_count, to_doctype):
174 if not fail_count:
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530175 frappe.msgprint(
Deepesh Garg2dfe8492022-11-17 15:53:56 +0530176 _("Creation of <b><a href='/app/{0}'>{1}(s)</a></b> successful").format(
177 to_doctype.lower().replace(" ", "-"), to_doctype
178 ),
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530179 title="Successful",
180 indicator="green",
181 )
Dany Robert91055152022-10-03 10:59:53 +0530182 elif fail_count != 0 and fail_count < deserialized_data_count:
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530183 frappe.msgprint(
Ankush Menat494bd9e2022-03-28 18:52:46 +0530184 _(
185 """Creation of {0} partially successful.
186 Check <b><a href="/app/bulk-transaction-log">Bulk Transaction Log</a></b>"""
187 ).format(to_doctype),
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530188 title="Partially successful",
189 indicator="orange",
190 )
Dany Robert91055152022-10-03 10:59:53 +0530191 else:
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530192 frappe.msgprint(
Ankush Menat494bd9e2022-03-28 18:52:46 +0530193 _(
194 """Creation of {0} failed.
195 Check <b><a href="/app/bulk-transaction-log">Bulk Transaction Log</a></b>"""
196 ).format(to_doctype),
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530197 title="Failed",
198 indicator="red",
199 )