blob: 3538c24aeba1eee7b76a2ba8475b880abafa56f8 [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()
David Arnoldffd38362024-01-23 17:42:56 +010010def transaction_processing(data, from_doctype, to_doctype, args=None):
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053011 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
David Arnoldffd38362024-01-23 17:42:56 +010016 if isinstance(args, str):
17 args = frappe._dict(json.loads(args))
18
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053019 length_of_data = len(deserialized_data)
20
ruthra kumar15dc5c72024-01-05 14:32:05 +053021 frappe.msgprint(
22 _("Started a background job to create {1} {0}").format(to_doctype, length_of_data)
23 )
24 frappe.enqueue(
25 job,
26 deserialized_data=deserialized_data,
27 from_doctype=from_doctype,
28 to_doctype=to_doctype,
David Arnoldffd38362024-01-23 17:42:56 +010029 args=args,
ruthra kumar15dc5c72024-01-05 14:32:05 +053030 )
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053031
32
ruthra kumar0aa16362023-11-10 12:20:30 +053033@frappe.whitelist()
ruthra kumarfb06ad72023-11-20 13:26:02 +053034def retry(date: str | None = None):
35 if not date:
36 date = today()
37
ruthra kumar0aa16362023-11-10 12:20:30 +053038 if date:
39 failed_docs = frappe.db.get_all(
40 "Bulk Transaction Log Detail",
41 filters={"date": date, "transaction_status": "Failed", "retried": 0},
42 fields=["name", "transaction_name", "from_doctype", "to_doctype"],
43 )
44 if not failed_docs:
ruthra kumarc3202882023-11-10 13:45:52 +053045 frappe.msgprint(_("There are no Failed transactions"))
46 else:
47 job = frappe.enqueue(
48 retry_failed_transactions,
49 failed_docs=failed_docs,
50 )
51 frappe.msgprint(
52 _("Job: {0} has been triggered for processing failed transactions").format(
53 get_link_to_form("RQ Job", job.id)
54 )
55 )
ruthra kumar0aa16362023-11-10 12:20:30 +053056
ruthra kumarc3202882023-11-10 13:45:52 +053057
58def retry_failed_transactions(failed_docs: list | None):
59 if failed_docs:
ruthra kumar0aa16362023-11-10 12:20:30 +053060 for log in failed_docs:
61 try:
62 frappe.db.savepoint("before_creation_state")
63 task(log.transaction_name, log.from_doctype, log.to_doctype)
64 except Exception as e:
65 frappe.db.rollback(save_point="before_creation_state")
Ankush Menat510fdf72024-01-01 13:10:03 +053066 update_log(log.name, "Failed", 1, str(frappe.get_traceback(with_context=True)))
ruthra kumar0aa16362023-11-10 12:20:30 +053067 else:
68 update_log(log.name, "Success", 1)
69
70
ruthra kumara52a1b42023-11-11 05:20:27 +053071def update_log(log_name, status, retried, err=None):
ruthra kumar0aa16362023-11-10 12:20:30 +053072 frappe.db.set_value("Bulk Transaction Log Detail", log_name, "transaction_status", status)
73 frappe.db.set_value("Bulk Transaction Log Detail", log_name, "retried", retried)
ruthra kumara52a1b42023-11-11 05:20:27 +053074 if err:
75 frappe.db.set_value("Bulk Transaction Log Detail", log_name, "error_description", err)
ruthra kumar0aa16362023-11-10 12:20:30 +053076
77
David Arnoldffd38362024-01-23 17:42:56 +010078def job(deserialized_data, from_doctype, to_doctype, args):
Dany Robert91055152022-10-03 10:59:53 +053079 fail_count = 0
David Arnoldffd38362024-01-23 17:42:56 +010080
81 if args:
82 # currently: flag-based transport to `task`
83 frappe.flags.args = args
84
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053085 for d in deserialized_data:
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053086 try:
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053087 doc_name = d.get("name")
88 frappe.db.savepoint("before_creation_state")
89 task(doc_name, from_doctype, to_doctype)
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053090 except Exception as e:
91 frappe.db.rollback(save_point="before_creation_state")
Dany Robert91055152022-10-03 10:59:53 +053092 fail_count += 1
ruthra kumarebd74a42023-11-09 16:38:34 +053093 create_log(
Dany Robert91055152022-10-03 10:59:53 +053094 doc_name,
Ankush Menat510fdf72024-01-01 13:10:03 +053095 str(frappe.get_traceback(with_context=True)),
Dany Robert91055152022-10-03 10:59:53 +053096 from_doctype,
97 to_doctype,
98 status="Failed",
99 log_date=str(date.today()),
Ankush Menat494bd9e2022-03-28 18:52:46 +0530100 )
Dany Robert91055152022-10-03 10:59:53 +0530101 else:
ruthra kumarebd74a42023-11-09 16:38:34 +0530102 create_log(
Ankush Menat494bd9e2022-03-28 18:52:46 +0530103 doc_name, None, from_doctype, to_doctype, status="Success", log_date=str(date.today())
104 )
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530105
Dany Robert91055152022-10-03 10:59:53 +0530106 show_job_status(fail_count, len(deserialized_data), to_doctype)
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530107
108
109def task(doc_name, from_doctype, to_doctype):
110 from erpnext.accounts.doctype.payment_entry import payment_entry
111 from erpnext.accounts.doctype.purchase_invoice import purchase_invoice
112 from erpnext.accounts.doctype.sales_invoice import sales_invoice
113 from erpnext.buying.doctype.purchase_order import purchase_order
114 from erpnext.buying.doctype.supplier_quotation import supplier_quotation
115 from erpnext.selling.doctype.quotation import quotation
116 from erpnext.selling.doctype.sales_order import sales_order
117 from erpnext.stock.doctype.delivery_note import delivery_note
118 from erpnext.stock.doctype.purchase_receipt import purchase_receipt
119
120 mapper = {
121 "Sales Order": {
122 "Sales Invoice": sales_order.make_sales_invoice,
123 "Delivery Note": sales_order.make_delivery_note,
Solufy Solutionf1acc5f2023-04-25 19:16:30 +0530124 "Payment Entry": payment_entry.get_payment_entry,
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530125 },
126 "Sales Invoice": {
127 "Delivery Note": sales_invoice.make_delivery_note,
Deepesh Garg2dfe8492022-11-17 15:53:56 +0530128 "Payment Entry": payment_entry.get_payment_entry,
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530129 },
130 "Delivery Note": {
131 "Sales Invoice": delivery_note.make_sales_invoice,
132 "Packing Slip": delivery_note.make_packing_slip,
133 },
134 "Quotation": {
135 "Sales Order": quotation.make_sales_order,
136 "Sales Invoice": quotation.make_sales_invoice,
137 },
138 "Supplier Quotation": {
139 "Purchase Order": supplier_quotation.make_purchase_order,
140 "Purchase Invoice": supplier_quotation.make_purchase_invoice,
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530141 },
142 "Purchase Order": {
143 "Purchase Invoice": purchase_order.make_purchase_invoice,
144 "Purchase Receipt": purchase_order.make_purchase_receipt,
Solufy Solutionf1acc5f2023-04-25 19:16:30 +0530145 "Payment Entry": payment_entry.get_payment_entry,
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530146 },
Dany Robert91055152022-10-03 10:59:53 +0530147 "Purchase Invoice": {
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530148 "Purchase Receipt": purchase_invoice.make_purchase_receipt,
Deepesh Garg2dfe8492022-11-17 15:53:56 +0530149 "Payment Entry": payment_entry.get_payment_entry,
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530150 },
151 "Purchase Receipt": {"Purchase Invoice": purchase_receipt.make_purchase_invoice},
152 }
David Arnold426c2452023-11-14 11:13:05 +0100153 frappe.flags.bulk_transaction = True
Solufy Solutionf1acc5f2023-04-25 19:16:30 +0530154 if to_doctype in ["Payment Entry"]:
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530155 obj = mapper[from_doctype][to_doctype](from_doctype, doc_name)
156 else:
157 obj = mapper[from_doctype][to_doctype](doc_name)
158
David Arnold9a705162024-01-30 12:59:26 +0100159 if obj:
160 obj.flags.ignore_validate = True
161 obj.set_title_field()
162 obj.insert(ignore_mandatory=True)
163
164 del obj
David Arnold426c2452023-11-14 11:13:05 +0100165 del frappe.flags.bulk_transaction
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530166
167
ruthra kumarebd74a42023-11-09 16:38:34 +0530168def create_log(doc_name, e, from_doctype, to_doctype, status, log_date=None, restarted=0):
169 transaction_log = frappe.new_doc("Bulk Transaction Log Detail")
170 transaction_log.transaction_name = doc_name
171 transaction_log.date = today()
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530172 now = datetime.now()
ruthra kumarebd74a42023-11-09 16:38:34 +0530173 transaction_log.time = now.strftime("%H:%M:%S")
174 transaction_log.transaction_status = status
175 transaction_log.error_description = str(e)
176 transaction_log.from_doctype = from_doctype
177 transaction_log.to_doctype = to_doctype
178 transaction_log.retried = restarted
179 transaction_log.save()
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530180
181
Dany Robert91055152022-10-03 10:59:53 +0530182def show_job_status(fail_count, deserialized_data_count, to_doctype):
183 if not fail_count:
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530184 frappe.msgprint(
Deepesh Garg2dfe8492022-11-17 15:53:56 +0530185 _("Creation of <b><a href='/app/{0}'>{1}(s)</a></b> successful").format(
186 to_doctype.lower().replace(" ", "-"), to_doctype
187 ),
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530188 title="Successful",
189 indicator="green",
190 )
Dany Robert91055152022-10-03 10:59:53 +0530191 elif fail_count != 0 and fail_count < deserialized_data_count:
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530192 frappe.msgprint(
Ankush Menat494bd9e2022-03-28 18:52:46 +0530193 _(
194 """Creation of {0} partially successful.
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="Partially successful",
198 indicator="orange",
199 )
Dany Robert91055152022-10-03 10:59:53 +0530200 else:
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530201 frappe.msgprint(
Ankush Menat494bd9e2022-03-28 18:52:46 +0530202 _(
203 """Creation of {0} failed.
204 Check <b><a href="/app/bulk-transaction-log">Bulk Transaction Log</a></b>"""
205 ).format(to_doctype),
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530206 title="Failed",
207 indicator="red",
208 )