How can I trigger an action when a Sales Order status changes to “Complete” in InvenTree (using a plugin)? #9503
Replies: 3 comments 12 replies
-
What do you mean by "when an admin completes the order"? In which interface is the order completed and why are you using an admin for it? |
Beta Was this translation helpful? Give feedback.
-
@SchrodingersGat @matmair I’ve been experimenting with my code to integrate the aforementioned actions, such as pdf downloading, by completing an order with the "Issue Order" button. However, it still seems that the event isn't being triggered as expected. Below is the relevant portion of the code I’m working with. Could you let me know if I’m using the event mixin correctly, or if there’s a better approach? # Define which events this plugin should respond to
def wants_process_event(self, event):
return event == SalesOrderEvents.COMPLETED
# This method will be called when the event occurs
def process_event(self, event, *args, **kwargs):
"""
Process the sales order completed event.
"""
try:
# Get the sales order from the kwargs
sales_order = kwargs.get('order', None)
if not sales_order:
logger.error("No sales order found in event kwargs")
return False
logger.info(f"Processing completed sales order: {sales_order.reference}")
# Call the complete order workflow
pk = sales_order.pk
# 1. Generate and upload PDF
pdf_response = self.download_pdf(None, pk)
if pdf_response.status_code != 200:
logger.error(f"Failed to generate PDF for order {pk}")
return False
upload_response = self.upload_pdf(None, pk)
if upload_response.status_code != 201:
logger.error(f"Failed to upload PDF for order {pk}")
return False
# 2. Send to ebo
eboek_response = self.send_to_ebo(None, pk)
if eboek_response.status_code != 201:
logger.error(f"Failed to send to ebo for order {pk}")
return False
logger.info(f"Successfully processed completed order {sales_order.reference}")
return True
except Exception as e:
logger.error(f"Error processing completed order event: {str(e)}")
return False |
Beta Was this translation helpful? Give feedback.
-
Ok so the event is triggered like this: trigger_event(SalesOrderEvents.COMPLETED, id=self.pk) So, to start with, your code should read: sales_order_id = kwargs.get('id', None)
sales_order = SalesOrder.objects.get(id=sales_order_id) (instead of) # Get the sales order from the kwargs
sales_order = kwargs.get('order', None) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm building a plugin for InvenTree and need to execute an action (like downloading a PDF) as soon as a Sales Order is marked as Complete. I've tried using EventsMixin and checked models.py, but so far I haven't been able to capture the event. Nothing seems to happen when an admin completes the order.
In the admin interface, I only see the default message:
“The Sales Order ‘SO…’ was changed successfully.”
So my question is: What’s a way to detect when a Sales Order status changes to “Complete”?
I'm running InvenTree on a VPS. Any pointers or example code would be appreciated!
Beta Was this translation helpful? Give feedback.
All reactions