Skip to content

Commit

Permalink
SEnd invoice before it was cancelled
Browse files Browse the repository at this point in the history
  • Loading branch information
maniamartial committed Jul 2, 2024
1 parent 8148b80 commit 623a11c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 16 deletions.
10 changes: 5 additions & 5 deletions burundi_compliance/burundi_compliance/api_classes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def authenticate_with_retry(self):
except ValueError as e:
frappe.msgprint("Received non-JSON response from OBR server")
frappe.log_error(f"Error decoding JSON response: {str(e)}", "OBRAPIBase Authentication Error")
self.enqueue_retry_task()
time.sleep(10)
# self.enqueue_retry_task()
# time.sleep(10)
return False

if result.get("success"):
Expand All @@ -43,8 +43,8 @@ def authenticate_with_retry(self):
frappe.msgprint("Authentication Problem with OBR server, Job queued")
error_message = f"Error during authentication: {str(e)}"
frappe.log_error(error_message, "OBRAPIBase Authentication Error")
self.enqueue_retry_task()
time.sleep(10)
# self.enqueue_retry_task()
# time.sleep(10)
return False


Expand Down Expand Up @@ -106,7 +106,7 @@ def wait_for_internet(self, delay=5):
def enqueue_retry_task(self):
job_id = frappe.enqueue(
"burundi_compliance.burundi_compliance.utils.background_jobs.retry_authentication",
queue="short",
queue="default",
timeout=5,
is_async=True,
at_front=True,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def _handle_response(self, response, data):
success = response.get("success")
if success:
self._create_integration_request(response, data, status="Completed")
self.update_invoice(data)
else:
self._create_integration_request(response, data, status="Failed")
return response
Expand All @@ -51,10 +52,13 @@ def cancel_invoice(self, data) -> dict:
frappe.log_error(f"Error during API request: {str(e)}")

def get_invoice(self, data):
invoice_identifier= data.get('invoice_identifier')
get_invoices=frappe.get_all('Sales Invoice', filters={'custom_invoice_identifier': invoice_identifier}, fields=['name'])
for get_invoice in get_invoices:
doc=frappe.get_doc('Sales Invoice', get_invoice.get('name'))
invoice_identifier= data.get('invoice_signature')
_invoices=frappe.get_all('Sales Invoice', filters={'custom_invoice_identifier': invoice_identifier}, fields=['name'])
for _invoice in _invoices:
doc=frappe.get_doc('Sales Invoice', _invoice.get('name'))
return doc


def update_invoice(self, data):
doc=self.get_invoice(data)
frappe.db.set_value('Sales Invoice', doc.name, 'custom_ebms_invoice_cancelled',1)
frappe.db.commit()
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def retry_cancel_invoice(invoice_data, doc):
try:
invoice_canceller = InvoiceCanceller(token)
response = invoice_canceller.cancel_invoice(invoice_data)
frappe.db.set_value(doc.doctype, doc.name, 'custom_ebms_invoice_cancelled', 1)
# frappe.db.set_value(doc.doctype, doc.name, 'custom_ebms_invoice_cancelled', 1)
frappe.publish_realtime("msgprint", f"Invoice cancelled successful!{response}", user=frappe.session.user)
return
except Exception as e:
Expand All @@ -133,7 +133,7 @@ def retry_cancel_invoice(invoice_data, doc):
frappe.msgprint(f"Error sending emails: {str(e)}")

def enqueue_cancel_invoice(invoice_data, doc):
frappe.enqueue('burundi_compliance.burundi_compliance.utils.background_jobs.retry_cancel_invoice', invoice_data=invoice_data,doc=doc, queue='long', is_async=True)
frappe.enqueue('burundi_compliance.burundi_compliance.utils.background_jobs.retry_cancel_invoice', invoice_data=invoice_data,doc=doc,queue='short',timeout=10, is_async=True)


#######################################################################################################
Expand Down
31 changes: 30 additions & 1 deletion burundi_compliance/burundi_compliance/utils/schedular.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,36 @@ def check_and_send_pending_cancelled_sales_invoices():
for sales_invoice in cancelled_sales_invoices:
try:
sales_invoice_doc = frappe.get_doc("Sales Invoice", sales_invoice.name)
cancel_invoice(sales_invoice_doc)
integration_request=frappe.db.get_doc("Integration Request", {"reference_doctype": "Sales Invoice", "reference_docname": sales_invoice.name, "service": "eBMS Invoice Cancellation"})
if integration_request:
cancel_invoice(sales_invoice_doc)
except Exception as e:
frappe.log_error(frappe.get_traceback(), "Error in sending sales invoice {0}".format(sales_invoice.name))
continue

#Cancelled Pending Sales Invoices
def check_and_send_submitted_invoice_which_were_cancelled():
#We should get data from integration request
'''
Check and send pending sales invoices
'''
integration_requests=frappe.get_all("Integration Request", filters={"status": "Failed","integration_request_service":"eBMS Invoice", "reference_doctype": "Sales Invoice"}, fields=["reference_docname"])
for integration_request in integration_requests:
try:
sales_invoice_doc = frappe.get_doc("Sales Invoice", integration_request.reference_docname)
if sales_invoice_doc.docstatus == 2 and sales_invoice_doc.custom_submitted_to_obr == 0 and sales_invoice_doc.custom_ebms_invoice_cancelled == 0:
submit_invoice_request(sales_invoice_doc)
except Exception as e:
frappe.log_error(frappe.get_traceback(), "Error in sending sales invoice {0}".format(integration_request.reference_docname))
continue

def check_and_send_pending_cancelled_invoice_from_integration_request():
integration_requests=frappe.get_all("Integration Request", filters={"status": "Failed","integration_request_service":"eBMS Invoice Cancellation", "reference_doctype": "Sales Invoice"}, fields=["reference_docname"])
for integration_request in integration_requests:
try:
sales_invoice_doc = frappe.get_doc("Sales Invoice", integration_request.reference_docname)
if sales_invoice_doc.docstatus == 2 and sales_invoice_doc.custom_submitted_to_obr == 1 and sales_invoice_doc.custom_ebms_invoice_cancelled == 0:
cancel_invoice(sales_invoice_doc)
except Exception as e:
frappe.log_error(frappe.get_traceback(), "Error in sending sales invoice {0}".format(integration_request.reference_docname))
continue
7 changes: 4 additions & 3 deletions burundi_compliance/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,10 @@

"cron":{
"*/5 * * * *":["burundi_compliance.burundi_compliance.utils.schedular.check_and_send_pending_sales_invoices"],
"* * * * *":["burundi_compliance.burundi_compliance.utils.schedular.check_and_send_pending_stock_ledger_entry"],
"*/9 * * * *":["burundi_compliance.burundi_compliance.utils.schedular.check_and_send_pending_cancelled_sales_invoices"],

"*/2 * * * *":["burundi_compliance.burundi_compliance.utils.schedular.check_and_send_pending_stock_ledger_entry"],
"*/7 * * * *":["burundi_compliance.burundi_compliance.utils.schedular.check_and_send_pending_cancelled_sales_invoices"],
"*/6 * * * *":["burundi_compliance.burundi_compliance.utils.schedular.check_and_send_submitted_invoice_which_were_cancelled"],
"0 0 * * *":["burundi_compliance.burundi_compliance.utils.schedular.check_and_send_pending_cancelled_invoice_from_integration_request"]
},

}
Expand Down

0 comments on commit 623a11c

Please sign in to comment.