Skip to Content
Menu
This question has been flagged

Hi, I'm attempting to prevent users from canceling a confirmed sale order if there is an invoice confirmed to the client. My current configuration for invoices is "Ordered quantities", not delivered like the majority.

I'm thinking of creating a automated action or something similar that triggers when the user tries to cancel the order.

The logic would be simple, if there is a confirmed invoice, you first need to cancel in order to cancel the sale order.


I don't know where to start or how to do it. Anyone has an idea or has done it before.

I would really appreciate any insights.



Avatar
Discard
Best Answer

Hi,

You can use the following code in the action.

sale = self.env["sale.order"].search([('name', '=', self.name)]) delivery = self.env["stock.picking"].search([('origin', '=', self.name)])
invoice = self.env["account.move"].search([('invoice_origin', '=', self.name)])
print(invoice.read())
delivery.state = 'draft'
sale.state = "draft"
invoice.state = "draft"
print('delivery', delivery)
print('delivery_o',sale)
for picking in delivery:
for move in picking.move_lines:
move.state = 'draft'
move._do_unreserve()
move.unlink()
picking.unlink()

Hope it helps

Avatar
Discard
Author

Hi Cybrosys, thank you for your feedback. I'm encountering the following error:

Down I attached more info on my response.

This is how the code is displayed.
Automated action
Model: Sales Order
Trigger: On creation & Update
Looking forward to hearing from you soon!

Thanks again.

Author Best Answer

Hi Cybrosys, thank you for your feedback. I'm encountering the following error:

This is how the code is displayed.

Automated action

Model: Sales Order

Trigger: On creation & Update

Looking forward to hearing from you soon!

Thanks again.

Avatar
Discard

instead of self.env in the above code, just use env

also please cross and check verify, whether it is a stable solution as the above code uses the name field in the search

Author

Hi Niyas, that solved the big problem.
It appears there's a syntax error but I'm not getting why "sale =" is the problem.

SyntaxError : invalid syntax at line 1
sale = env["sale.order"].search([('name', '=', self.name)]) delivery = env["stock.picking"].search([('origin', '=', self.name)])

If I change it t "sale.order ="
Then the following syntax error appears:
Syntax errors detected :

Line 1:
sale.order = env["sale.order"].search([('name', '=', self.name)]) delivery = env["stock.picking"].search([('origin', '=', self.name)])
……………………………………………………………..^ (self.name)])
SyntaxError: invalid syntax

Related Posts Replies Views Activity
0
Jan 24
464
2
Sep 15
3066
2
Dec 23
527
3
Oct 22
3627
1
Oct 22
2822