blob: 7e73c2fe0b0c52573745cc59cb16c27d472547c4 [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 kumarc3202882023-11-10 13:45:52 +053033def retry(date: str | None):
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")
62 update_log(log.name, "Failed", 1)
63 else:
64 update_log(log.name, "Success", 1)
65
66
67def update_log(log_name, status, retried):
68 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)
70
71
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053072def job(deserialized_data, from_doctype, to_doctype):
Dany Robert91055152022-10-03 10:59:53 +053073 fail_count = 0
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053074 for d in deserialized_data:
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053075 try:
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053076 doc_name = d.get("name")
77 frappe.db.savepoint("before_creation_state")
78 task(doc_name, from_doctype, to_doctype)
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053079 except Exception as e:
80 frappe.db.rollback(save_point="before_creation_state")
Dany Robert91055152022-10-03 10:59:53 +053081 fail_count += 1
ruthra kumarebd74a42023-11-09 16:38:34 +053082 create_log(
Dany Robert91055152022-10-03 10:59:53 +053083 doc_name,
84 str(frappe.get_traceback()),
85 from_doctype,
86 to_doctype,
87 status="Failed",
88 log_date=str(date.today()),
Ankush Menat494bd9e2022-03-28 18:52:46 +053089 )
Dany Robert91055152022-10-03 10:59:53 +053090 else:
ruthra kumarebd74a42023-11-09 16:38:34 +053091 create_log(
Ankush Menat494bd9e2022-03-28 18:52:46 +053092 doc_name, None, from_doctype, to_doctype, status="Success", log_date=str(date.today())
93 )
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053094
Dany Robert91055152022-10-03 10:59:53 +053095 show_job_status(fail_count, len(deserialized_data), to_doctype)
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053096
97
98def task(doc_name, from_doctype, to_doctype):
99 from erpnext.accounts.doctype.payment_entry import payment_entry
100 from erpnext.accounts.doctype.purchase_invoice import purchase_invoice
101 from erpnext.accounts.doctype.sales_invoice import sales_invoice
102 from erpnext.buying.doctype.purchase_order import purchase_order
103 from erpnext.buying.doctype.supplier_quotation import supplier_quotation
104 from erpnext.selling.doctype.quotation import quotation
105 from erpnext.selling.doctype.sales_order import sales_order
106 from erpnext.stock.doctype.delivery_note import delivery_note
107 from erpnext.stock.doctype.purchase_receipt import purchase_receipt
108
109 mapper = {
110 "Sales Order": {
111 "Sales Invoice": sales_order.make_sales_invoice,
112 "Delivery Note": sales_order.make_delivery_note,
Solufy Solutionf1acc5f2023-04-25 19:16:30 +0530113 "Payment Entry": payment_entry.get_payment_entry,
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530114 },
115 "Sales Invoice": {
116 "Delivery Note": sales_invoice.make_delivery_note,
Deepesh Garg2dfe8492022-11-17 15:53:56 +0530117 "Payment Entry": payment_entry.get_payment_entry,
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530118 },
119 "Delivery Note": {
120 "Sales Invoice": delivery_note.make_sales_invoice,
121 "Packing Slip": delivery_note.make_packing_slip,
122 },
123 "Quotation": {
124 "Sales Order": quotation.make_sales_order,
125 "Sales Invoice": quotation.make_sales_invoice,
126 },
127 "Supplier Quotation": {
128 "Purchase Order": supplier_quotation.make_purchase_order,
129 "Purchase Invoice": supplier_quotation.make_purchase_invoice,
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530130 },
131 "Purchase Order": {
132 "Purchase Invoice": purchase_order.make_purchase_invoice,
133 "Purchase Receipt": purchase_order.make_purchase_receipt,
Solufy Solutionf1acc5f2023-04-25 19:16:30 +0530134 "Payment Entry": payment_entry.get_payment_entry,
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530135 },
Dany Robert91055152022-10-03 10:59:53 +0530136 "Purchase Invoice": {
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530137 "Purchase Receipt": purchase_invoice.make_purchase_receipt,
Deepesh Garg2dfe8492022-11-17 15:53:56 +0530138 "Payment Entry": payment_entry.get_payment_entry,
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530139 },
140 "Purchase Receipt": {"Purchase Invoice": purchase_receipt.make_purchase_invoice},
141 }
Solufy Solutionf1acc5f2023-04-25 19:16:30 +0530142 if to_doctype in ["Payment Entry"]:
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530143 obj = mapper[from_doctype][to_doctype](from_doctype, doc_name)
144 else:
145 obj = mapper[from_doctype][to_doctype](doc_name)
146
147 obj.flags.ignore_validate = True
HarryPaulo22290c22023-04-25 10:43:53 -0300148 obj.set_title_field()
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530149 obj.insert(ignore_mandatory=True)
150
151
ruthra kumarebd74a42023-11-09 16:38:34 +0530152def create_log(doc_name, e, from_doctype, to_doctype, status, log_date=None, restarted=0):
153 transaction_log = frappe.new_doc("Bulk Transaction Log Detail")
154 transaction_log.transaction_name = doc_name
155 transaction_log.date = today()
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530156 now = datetime.now()
ruthra kumarebd74a42023-11-09 16:38:34 +0530157 transaction_log.time = now.strftime("%H:%M:%S")
158 transaction_log.transaction_status = status
159 transaction_log.error_description = str(e)
160 transaction_log.from_doctype = from_doctype
161 transaction_log.to_doctype = to_doctype
162 transaction_log.retried = restarted
163 transaction_log.save()
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530164
165
Dany Robert91055152022-10-03 10:59:53 +0530166def show_job_status(fail_count, deserialized_data_count, to_doctype):
167 if not fail_count:
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530168 frappe.msgprint(
Deepesh Garg2dfe8492022-11-17 15:53:56 +0530169 _("Creation of <b><a href='/app/{0}'>{1}(s)</a></b> successful").format(
170 to_doctype.lower().replace(" ", "-"), to_doctype
171 ),
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530172 title="Successful",
173 indicator="green",
174 )
Dany Robert91055152022-10-03 10:59:53 +0530175 elif fail_count != 0 and fail_count < deserialized_data_count:
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530176 frappe.msgprint(
Ankush Menat494bd9e2022-03-28 18:52:46 +0530177 _(
178 """Creation of {0} partially successful.
179 Check <b><a href="/app/bulk-transaction-log">Bulk Transaction Log</a></b>"""
180 ).format(to_doctype),
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530181 title="Partially successful",
182 indicator="orange",
183 )
Dany Robert91055152022-10-03 10:59:53 +0530184 else:
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530185 frappe.msgprint(
Ankush Menat494bd9e2022-03-28 18:52:46 +0530186 _(
187 """Creation of {0} failed.
188 Check <b><a href="/app/bulk-transaction-log">Bulk Transaction Log</a></b>"""
189 ).format(to_doctype),
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530190 title="Failed",
191 indicator="red",
192 )