blob: a59600158376e1cda1cfb7ba989184f0399755fa [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 kumarebd74a42023-11-09 16:38:34 +05306from frappe.utils import 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()
33def retry_failed_transactions(date: str | None):
34 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:
41 frappe.msgprint("There are no Failed transactions")
42 return
43
44 for log in failed_docs:
45 try:
46 frappe.db.savepoint("before_creation_state")
47 task(log.transaction_name, log.from_doctype, log.to_doctype)
48 except Exception as e:
49 frappe.db.rollback(save_point="before_creation_state")
50 update_log(log.name, "Failed", 1)
51 else:
52 update_log(log.name, "Success", 1)
53
54
55def update_log(log_name, status, retried):
56 frappe.db.set_value("Bulk Transaction Log Detail", log_name, "transaction_status", status)
57 frappe.db.set_value("Bulk Transaction Log Detail", log_name, "retried", retried)
58
59
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053060def job(deserialized_data, from_doctype, to_doctype):
Dany Robert91055152022-10-03 10:59:53 +053061 fail_count = 0
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053062 for d in deserialized_data:
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053063 try:
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053064 doc_name = d.get("name")
65 frappe.db.savepoint("before_creation_state")
66 task(doc_name, from_doctype, to_doctype)
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053067 except Exception as e:
68 frappe.db.rollback(save_point="before_creation_state")
Dany Robert91055152022-10-03 10:59:53 +053069 fail_count += 1
ruthra kumarebd74a42023-11-09 16:38:34 +053070 create_log(
Dany Robert91055152022-10-03 10:59:53 +053071 doc_name,
72 str(frappe.get_traceback()),
73 from_doctype,
74 to_doctype,
75 status="Failed",
76 log_date=str(date.today()),
Ankush Menat494bd9e2022-03-28 18:52:46 +053077 )
Dany Robert91055152022-10-03 10:59:53 +053078 else:
ruthra kumarebd74a42023-11-09 16:38:34 +053079 create_log(
Ankush Menat494bd9e2022-03-28 18:52:46 +053080 doc_name, None, from_doctype, to_doctype, status="Success", log_date=str(date.today())
81 )
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053082
Dany Robert91055152022-10-03 10:59:53 +053083 show_job_status(fail_count, len(deserialized_data), to_doctype)
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053084
85
86def task(doc_name, from_doctype, to_doctype):
87 from erpnext.accounts.doctype.payment_entry import payment_entry
88 from erpnext.accounts.doctype.purchase_invoice import purchase_invoice
89 from erpnext.accounts.doctype.sales_invoice import sales_invoice
90 from erpnext.buying.doctype.purchase_order import purchase_order
91 from erpnext.buying.doctype.supplier_quotation import supplier_quotation
92 from erpnext.selling.doctype.quotation import quotation
93 from erpnext.selling.doctype.sales_order import sales_order
94 from erpnext.stock.doctype.delivery_note import delivery_note
95 from erpnext.stock.doctype.purchase_receipt import purchase_receipt
96
97 mapper = {
98 "Sales Order": {
99 "Sales Invoice": sales_order.make_sales_invoice,
100 "Delivery Note": sales_order.make_delivery_note,
Solufy Solutionf1acc5f2023-04-25 19:16:30 +0530101 "Payment Entry": payment_entry.get_payment_entry,
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530102 },
103 "Sales Invoice": {
104 "Delivery Note": sales_invoice.make_delivery_note,
Deepesh Garg2dfe8492022-11-17 15:53:56 +0530105 "Payment Entry": payment_entry.get_payment_entry,
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530106 },
107 "Delivery Note": {
108 "Sales Invoice": delivery_note.make_sales_invoice,
109 "Packing Slip": delivery_note.make_packing_slip,
110 },
111 "Quotation": {
112 "Sales Order": quotation.make_sales_order,
113 "Sales Invoice": quotation.make_sales_invoice,
114 },
115 "Supplier Quotation": {
116 "Purchase Order": supplier_quotation.make_purchase_order,
117 "Purchase Invoice": supplier_quotation.make_purchase_invoice,
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530118 },
119 "Purchase Order": {
120 "Purchase Invoice": purchase_order.make_purchase_invoice,
121 "Purchase Receipt": purchase_order.make_purchase_receipt,
Solufy Solutionf1acc5f2023-04-25 19:16:30 +0530122 "Payment Entry": payment_entry.get_payment_entry,
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530123 },
Dany Robert91055152022-10-03 10:59:53 +0530124 "Purchase Invoice": {
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530125 "Purchase Receipt": purchase_invoice.make_purchase_receipt,
Deepesh Garg2dfe8492022-11-17 15:53:56 +0530126 "Payment Entry": payment_entry.get_payment_entry,
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530127 },
128 "Purchase Receipt": {"Purchase Invoice": purchase_receipt.make_purchase_invoice},
129 }
Solufy Solutionf1acc5f2023-04-25 19:16:30 +0530130 if to_doctype in ["Payment Entry"]:
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530131 obj = mapper[from_doctype][to_doctype](from_doctype, doc_name)
132 else:
133 obj = mapper[from_doctype][to_doctype](doc_name)
134
135 obj.flags.ignore_validate = True
HarryPaulo22290c22023-04-25 10:43:53 -0300136 obj.set_title_field()
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530137 obj.insert(ignore_mandatory=True)
138
139
ruthra kumarebd74a42023-11-09 16:38:34 +0530140def create_log(doc_name, e, from_doctype, to_doctype, status, log_date=None, restarted=0):
141 transaction_log = frappe.new_doc("Bulk Transaction Log Detail")
142 transaction_log.transaction_name = doc_name
143 transaction_log.date = today()
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530144 now = datetime.now()
ruthra kumarebd74a42023-11-09 16:38:34 +0530145 transaction_log.time = now.strftime("%H:%M:%S")
146 transaction_log.transaction_status = status
147 transaction_log.error_description = str(e)
148 transaction_log.from_doctype = from_doctype
149 transaction_log.to_doctype = to_doctype
150 transaction_log.retried = restarted
151 transaction_log.save()
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530152
153
Dany Robert91055152022-10-03 10:59:53 +0530154def show_job_status(fail_count, deserialized_data_count, to_doctype):
155 if not fail_count:
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530156 frappe.msgprint(
Deepesh Garg2dfe8492022-11-17 15:53:56 +0530157 _("Creation of <b><a href='/app/{0}'>{1}(s)</a></b> successful").format(
158 to_doctype.lower().replace(" ", "-"), to_doctype
159 ),
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530160 title="Successful",
161 indicator="green",
162 )
Dany Robert91055152022-10-03 10:59:53 +0530163 elif fail_count != 0 and fail_count < deserialized_data_count:
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530164 frappe.msgprint(
Ankush Menat494bd9e2022-03-28 18:52:46 +0530165 _(
166 """Creation of {0} partially successful.
167 Check <b><a href="/app/bulk-transaction-log">Bulk Transaction Log</a></b>"""
168 ).format(to_doctype),
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530169 title="Partially successful",
170 indicator="orange",
171 )
Dany Robert91055152022-10-03 10:59:53 +0530172 else:
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530173 frappe.msgprint(
Ankush Menat494bd9e2022-03-28 18:52:46 +0530174 _(
175 """Creation of {0} failed.
176 Check <b><a href="/app/bulk-transaction-log">Bulk Transaction Log</a></b>"""
177 ).format(to_doctype),
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530178 title="Failed",
179 indicator="red",
180 )