blob: 9678488a2614e77f3a8d4ba8acbd8d95d0b459cf [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
ruthra kumar15dc5c72024-01-05 14:32:05 +053018 frappe.msgprint(
19 _("Started a background job to create {1} {0}").format(to_doctype, length_of_data)
20 )
21 frappe.enqueue(
22 job,
23 deserialized_data=deserialized_data,
24 from_doctype=from_doctype,
25 to_doctype=to_doctype,
26 )
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053027
28
ruthra kumar0aa16362023-11-10 12:20:30 +053029@frappe.whitelist()
ruthra kumarfb06ad72023-11-20 13:26:02 +053030def retry(date: str | None = None):
31 if not date:
32 date = today()
33
ruthra kumar0aa16362023-11-10 12:20:30 +053034 if date:
35 failed_docs = frappe.db.get_all(
36 "Bulk Transaction Log Detail",
37 filters={"date": date, "transaction_status": "Failed", "retried": 0},
38 fields=["name", "transaction_name", "from_doctype", "to_doctype"],
39 )
40 if not failed_docs:
ruthra kumarc3202882023-11-10 13:45:52 +053041 frappe.msgprint(_("There are no Failed transactions"))
42 else:
43 job = frappe.enqueue(
44 retry_failed_transactions,
45 failed_docs=failed_docs,
46 )
47 frappe.msgprint(
48 _("Job: {0} has been triggered for processing failed transactions").format(
49 get_link_to_form("RQ Job", job.id)
50 )
51 )
ruthra kumar0aa16362023-11-10 12:20:30 +053052
ruthra kumarc3202882023-11-10 13:45:52 +053053
54def retry_failed_transactions(failed_docs: list | None):
55 if failed_docs:
ruthra kumar0aa16362023-11-10 12:20:30 +053056 for log in failed_docs:
57 try:
58 frappe.db.savepoint("before_creation_state")
59 task(log.transaction_name, log.from_doctype, log.to_doctype)
60 except Exception as e:
61 frappe.db.rollback(save_point="before_creation_state")
Ankush Menat510fdf72024-01-01 13:10:03 +053062 update_log(log.name, "Failed", 1, str(frappe.get_traceback(with_context=True)))
ruthra kumar0aa16362023-11-10 12:20:30 +053063 else:
64 update_log(log.name, "Success", 1)
65
66
ruthra kumara52a1b42023-11-11 05:20:27 +053067def update_log(log_name, status, retried, err=None):
ruthra kumar0aa16362023-11-10 12:20:30 +053068 frappe.db.set_value("Bulk Transaction Log Detail", log_name, "transaction_status", status)
69 frappe.db.set_value("Bulk Transaction Log Detail", log_name, "retried", retried)
ruthra kumara52a1b42023-11-11 05:20:27 +053070 if err:
71 frappe.db.set_value("Bulk Transaction Log Detail", log_name, "error_description", err)
ruthra kumar0aa16362023-11-10 12:20:30 +053072
73
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053074def job(deserialized_data, from_doctype, to_doctype):
Dany Robert91055152022-10-03 10:59:53 +053075 fail_count = 0
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053076 for d in deserialized_data:
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053077 try:
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053078 doc_name = d.get("name")
79 frappe.db.savepoint("before_creation_state")
80 task(doc_name, from_doctype, to_doctype)
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053081 except Exception as e:
82 frappe.db.rollback(save_point="before_creation_state")
Dany Robert91055152022-10-03 10:59:53 +053083 fail_count += 1
ruthra kumarebd74a42023-11-09 16:38:34 +053084 create_log(
Dany Robert91055152022-10-03 10:59:53 +053085 doc_name,
Ankush Menat510fdf72024-01-01 13:10:03 +053086 str(frappe.get_traceback(with_context=True)),
Dany Robert91055152022-10-03 10:59:53 +053087 from_doctype,
88 to_doctype,
89 status="Failed",
90 log_date=str(date.today()),
Ankush Menat494bd9e2022-03-28 18:52:46 +053091 )
Dany Robert91055152022-10-03 10:59:53 +053092 else:
ruthra kumarebd74a42023-11-09 16:38:34 +053093 create_log(
Ankush Menat494bd9e2022-03-28 18:52:46 +053094 doc_name, None, from_doctype, to_doctype, status="Success", log_date=str(date.today())
95 )
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053096
Dany Robert91055152022-10-03 10:59:53 +053097 show_job_status(fail_count, len(deserialized_data), to_doctype)
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053098
99
100def task(doc_name, from_doctype, to_doctype):
101 from erpnext.accounts.doctype.payment_entry import payment_entry
102 from erpnext.accounts.doctype.purchase_invoice import purchase_invoice
103 from erpnext.accounts.doctype.sales_invoice import sales_invoice
104 from erpnext.buying.doctype.purchase_order import purchase_order
105 from erpnext.buying.doctype.supplier_quotation import supplier_quotation
106 from erpnext.selling.doctype.quotation import quotation
107 from erpnext.selling.doctype.sales_order import sales_order
108 from erpnext.stock.doctype.delivery_note import delivery_note
109 from erpnext.stock.doctype.purchase_receipt import purchase_receipt
110
111 mapper = {
112 "Sales Order": {
113 "Sales Invoice": sales_order.make_sales_invoice,
114 "Delivery Note": sales_order.make_delivery_note,
Solufy Solutionf1acc5f2023-04-25 19:16:30 +0530115 "Payment Entry": payment_entry.get_payment_entry,
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530116 },
117 "Sales Invoice": {
118 "Delivery Note": sales_invoice.make_delivery_note,
Deepesh Garg2dfe8492022-11-17 15:53:56 +0530119 "Payment Entry": payment_entry.get_payment_entry,
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530120 },
121 "Delivery Note": {
122 "Sales Invoice": delivery_note.make_sales_invoice,
123 "Packing Slip": delivery_note.make_packing_slip,
124 },
125 "Quotation": {
126 "Sales Order": quotation.make_sales_order,
127 "Sales Invoice": quotation.make_sales_invoice,
128 },
129 "Supplier Quotation": {
130 "Purchase Order": supplier_quotation.make_purchase_order,
131 "Purchase Invoice": supplier_quotation.make_purchase_invoice,
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530132 },
133 "Purchase Order": {
134 "Purchase Invoice": purchase_order.make_purchase_invoice,
135 "Purchase Receipt": purchase_order.make_purchase_receipt,
Solufy Solutionf1acc5f2023-04-25 19:16:30 +0530136 "Payment Entry": payment_entry.get_payment_entry,
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530137 },
Dany Robert91055152022-10-03 10:59:53 +0530138 "Purchase Invoice": {
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530139 "Purchase Receipt": purchase_invoice.make_purchase_receipt,
Deepesh Garg2dfe8492022-11-17 15:53:56 +0530140 "Payment Entry": payment_entry.get_payment_entry,
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530141 },
142 "Purchase Receipt": {"Purchase Invoice": purchase_receipt.make_purchase_invoice},
143 }
David Arnold426c2452023-11-14 11:13:05 +0100144 frappe.flags.bulk_transaction = True
Solufy Solutionf1acc5f2023-04-25 19:16:30 +0530145 if to_doctype in ["Payment Entry"]:
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530146 obj = mapper[from_doctype][to_doctype](from_doctype, doc_name)
147 else:
148 obj = mapper[from_doctype][to_doctype](doc_name)
149
150 obj.flags.ignore_validate = True
HarryPaulo22290c22023-04-25 10:43:53 -0300151 obj.set_title_field()
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530152 obj.insert(ignore_mandatory=True)
David Arnold426c2452023-11-14 11:13:05 +0100153 del frappe.flags.bulk_transaction
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530154
155
ruthra kumarebd74a42023-11-09 16:38:34 +0530156def create_log(doc_name, e, from_doctype, to_doctype, status, log_date=None, restarted=0):
157 transaction_log = frappe.new_doc("Bulk Transaction Log Detail")
158 transaction_log.transaction_name = doc_name
159 transaction_log.date = today()
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530160 now = datetime.now()
ruthra kumarebd74a42023-11-09 16:38:34 +0530161 transaction_log.time = now.strftime("%H:%M:%S")
162 transaction_log.transaction_status = status
163 transaction_log.error_description = str(e)
164 transaction_log.from_doctype = from_doctype
165 transaction_log.to_doctype = to_doctype
166 transaction_log.retried = restarted
167 transaction_log.save()
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530168
169
Dany Robert91055152022-10-03 10:59:53 +0530170def show_job_status(fail_count, deserialized_data_count, to_doctype):
171 if not fail_count:
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530172 frappe.msgprint(
Deepesh Garg2dfe8492022-11-17 15:53:56 +0530173 _("Creation of <b><a href='/app/{0}'>{1}(s)</a></b> successful").format(
174 to_doctype.lower().replace(" ", "-"), to_doctype
175 ),
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530176 title="Successful",
177 indicator="green",
178 )
Dany Robert91055152022-10-03 10:59:53 +0530179 elif fail_count != 0 and fail_count < deserialized_data_count:
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530180 frappe.msgprint(
Ankush Menat494bd9e2022-03-28 18:52:46 +0530181 _(
182 """Creation of {0} partially successful.
183 Check <b><a href="/app/bulk-transaction-log">Bulk Transaction Log</a></b>"""
184 ).format(to_doctype),
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530185 title="Partially successful",
186 indicator="orange",
187 )
Dany Robert91055152022-10-03 10:59:53 +0530188 else:
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530189 frappe.msgprint(
Ankush Menat494bd9e2022-03-28 18:52:46 +0530190 _(
191 """Creation of {0} failed.
192 Check <b><a href="/app/bulk-transaction-log">Bulk Transaction Log</a></b>"""
193 ).format(to_doctype),
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530194 title="Failed",
195 indicator="red",
196 )