Se rendre au contenu
Menu
Cette question a été signalée

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
Ignorer
Meilleure réponse

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
Ignorer
Auteur

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.

Auteur Meilleure réponse

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
Ignorer

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

Auteur

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

Publications associées Réponses Vues Activité
0
janv. 24
1395
2
sept. 15
3901
2
déc. 23
2876
3
oct. 22
6349
1
oct. 22
4270