blob: fd8e7226485f8ab37a51c6e76dc0200953883867 [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
32def job(deserialized_data, from_doctype, to_doctype):
Dany Robert91055152022-10-03 10:59:53 +053033 fail_count = 0
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053034 for d in deserialized_data:
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053035 try:
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053036 doc_name = d.get("name")
37 frappe.db.savepoint("before_creation_state")
38 task(doc_name, from_doctype, to_doctype)
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053039 except Exception as e:
40 frappe.db.rollback(save_point="before_creation_state")
Dany Robert91055152022-10-03 10:59:53 +053041 fail_count += 1
ruthra kumarebd74a42023-11-09 16:38:34 +053042 create_log(
Dany Robert91055152022-10-03 10:59:53 +053043 doc_name,
44 str(frappe.get_traceback()),
45 from_doctype,
46 to_doctype,
47 status="Failed",
48 log_date=str(date.today()),
Ankush Menat494bd9e2022-03-28 18:52:46 +053049 )
Dany Robert91055152022-10-03 10:59:53 +053050 else:
ruthra kumarebd74a42023-11-09 16:38:34 +053051 create_log(
Ankush Menat494bd9e2022-03-28 18:52:46 +053052 doc_name, None, from_doctype, to_doctype, status="Success", log_date=str(date.today())
53 )
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053054
Dany Robert91055152022-10-03 10:59:53 +053055 show_job_status(fail_count, len(deserialized_data), to_doctype)
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053056
57
58def task(doc_name, from_doctype, to_doctype):
59 from erpnext.accounts.doctype.payment_entry import payment_entry
60 from erpnext.accounts.doctype.purchase_invoice import purchase_invoice
61 from erpnext.accounts.doctype.sales_invoice import sales_invoice
62 from erpnext.buying.doctype.purchase_order import purchase_order
63 from erpnext.buying.doctype.supplier_quotation import supplier_quotation
64 from erpnext.selling.doctype.quotation import quotation
65 from erpnext.selling.doctype.sales_order import sales_order
66 from erpnext.stock.doctype.delivery_note import delivery_note
67 from erpnext.stock.doctype.purchase_receipt import purchase_receipt
68
69 mapper = {
70 "Sales Order": {
71 "Sales Invoice": sales_order.make_sales_invoice,
72 "Delivery Note": sales_order.make_delivery_note,
Solufy Solutionf1acc5f2023-04-25 19:16:30 +053073 "Payment Entry": payment_entry.get_payment_entry,
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053074 },
75 "Sales Invoice": {
76 "Delivery Note": sales_invoice.make_delivery_note,
Deepesh Garg2dfe8492022-11-17 15:53:56 +053077 "Payment Entry": payment_entry.get_payment_entry,
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053078 },
79 "Delivery Note": {
80 "Sales Invoice": delivery_note.make_sales_invoice,
81 "Packing Slip": delivery_note.make_packing_slip,
82 },
83 "Quotation": {
84 "Sales Order": quotation.make_sales_order,
85 "Sales Invoice": quotation.make_sales_invoice,
86 },
87 "Supplier Quotation": {
88 "Purchase Order": supplier_quotation.make_purchase_order,
89 "Purchase Invoice": supplier_quotation.make_purchase_invoice,
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053090 },
91 "Purchase Order": {
92 "Purchase Invoice": purchase_order.make_purchase_invoice,
93 "Purchase Receipt": purchase_order.make_purchase_receipt,
Solufy Solutionf1acc5f2023-04-25 19:16:30 +053094 "Payment Entry": payment_entry.get_payment_entry,
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053095 },
Dany Robert91055152022-10-03 10:59:53 +053096 "Purchase Invoice": {
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053097 "Purchase Receipt": purchase_invoice.make_purchase_receipt,
Deepesh Garg2dfe8492022-11-17 15:53:56 +053098 "Payment Entry": payment_entry.get_payment_entry,
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +053099 },
100 "Purchase Receipt": {"Purchase Invoice": purchase_receipt.make_purchase_invoice},
101 }
Solufy Solutionf1acc5f2023-04-25 19:16:30 +0530102 if to_doctype in ["Payment Entry"]:
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530103 obj = mapper[from_doctype][to_doctype](from_doctype, doc_name)
104 else:
105 obj = mapper[from_doctype][to_doctype](doc_name)
106
107 obj.flags.ignore_validate = True
HarryPaulo22290c22023-04-25 10:43:53 -0300108 obj.set_title_field()
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530109 obj.insert(ignore_mandatory=True)
110
111
ruthra kumarebd74a42023-11-09 16:38:34 +0530112def create_log(doc_name, e, from_doctype, to_doctype, status, log_date=None, restarted=0):
113 transaction_log = frappe.new_doc("Bulk Transaction Log Detail")
114 transaction_log.transaction_name = doc_name
115 transaction_log.date = today()
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530116 now = datetime.now()
ruthra kumarebd74a42023-11-09 16:38:34 +0530117 transaction_log.time = now.strftime("%H:%M:%S")
118 transaction_log.transaction_status = status
119 transaction_log.error_description = str(e)
120 transaction_log.from_doctype = from_doctype
121 transaction_log.to_doctype = to_doctype
122 transaction_log.retried = restarted
123 transaction_log.save()
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530124
125
Dany Robert91055152022-10-03 10:59:53 +0530126def show_job_status(fail_count, deserialized_data_count, to_doctype):
127 if not fail_count:
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530128 frappe.msgprint(
Deepesh Garg2dfe8492022-11-17 15:53:56 +0530129 _("Creation of <b><a href='/app/{0}'>{1}(s)</a></b> successful").format(
130 to_doctype.lower().replace(" ", "-"), to_doctype
131 ),
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530132 title="Successful",
133 indicator="green",
134 )
Dany Robert91055152022-10-03 10:59:53 +0530135 elif fail_count != 0 and fail_count < deserialized_data_count:
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530136 frappe.msgprint(
Ankush Menat494bd9e2022-03-28 18:52:46 +0530137 _(
138 """Creation of {0} partially successful.
139 Check <b><a href="/app/bulk-transaction-log">Bulk Transaction Log</a></b>"""
140 ).format(to_doctype),
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530141 title="Partially successful",
142 indicator="orange",
143 )
Dany Robert91055152022-10-03 10:59:53 +0530144 else:
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530145 frappe.msgprint(
Ankush Menat494bd9e2022-03-28 18:52:46 +0530146 _(
147 """Creation of {0} failed.
148 Check <b><a href="/app/bulk-transaction-log">Bulk Transaction Log</a></b>"""
149 ).format(to_doctype),
Mohammed Yusuf Shaikha3e69cf2022-02-08 01:00:37 +0530150 title="Failed",
151 indicator="red",
152 )