Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie

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.



Awatar
Odrzuć
Najlepsza odpowiedź

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

Awatar
Odrzuć
Autor

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.

Autor Najlepsza odpowiedź

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.

Awatar
Odrzuć

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

Autor

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

Powiązane posty Odpowiedzi Widoki Czynność
0
sty 24
1345
2
wrz 15
3864
2
gru 23
2767
3
paź 22
6203
1
paź 22
4197