diff --git a/burundi_compliance/burundi_compliance/api_classes/base.py b/burundi_compliance/burundi_compliance/api_classes/base.py index a10e9f5..f8f7081 100644 --- a/burundi_compliance/burundi_compliance/api_classes/base.py +++ b/burundi_compliance/burundi_compliance/api_classes/base.py @@ -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"): @@ -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 @@ -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, diff --git a/burundi_compliance/burundi_compliance/api_classes/cancel_invoice.py b/burundi_compliance/burundi_compliance/api_classes/cancel_invoice.py index 8a7d651..49677fa 100644 --- a/burundi_compliance/burundi_compliance/api_classes/cancel_invoice.py +++ b/burundi_compliance/burundi_compliance/api_classes/cancel_invoice.py @@ -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 @@ -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 - \ No newline at end of file + 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() \ No newline at end of file diff --git a/burundi_compliance/burundi_compliance/utils/background_jobs.py b/burundi_compliance/burundi_compliance/utils/background_jobs.py index dcb3249..f2fea2e 100644 --- a/burundi_compliance/burundi_compliance/utils/background_jobs.py +++ b/burundi_compliance/burundi_compliance/utils/background_jobs.py @@ -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: @@ -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) ####################################################################################################### diff --git a/burundi_compliance/burundi_compliance/utils/schedular.py b/burundi_compliance/burundi_compliance/utils/schedular.py index 09e166d..a8862d2 100644 --- a/burundi_compliance/burundi_compliance/utils/schedular.py +++ b/burundi_compliance/burundi_compliance/utils/schedular.py @@ -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 diff --git a/burundi_compliance/hooks.py b/burundi_compliance/hooks.py index 107861d..24c1acc 100644 --- a/burundi_compliance/hooks.py +++ b/burundi_compliance/hooks.py @@ -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"] }, }