-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ADD] cancel and redraft sale endpoint
- Loading branch information
Pierrick Brun
committed
Mar 3, 2021
1 parent
2ce2a2d
commit cf79a2c
Showing
4 changed files
with
81 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
# Copyright 2017 Akretion (http://www.akretion.com). | ||
# @author Sébastien BEAU <[email protected]> | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). | ||
from odoo.exceptions import UserError | ||
from odoo.osv import expression | ||
from odoo.tools.translate import _ | ||
|
||
from odoo.addons.base_rest.components.service import to_int | ||
from odoo.addons.component.core import Component | ||
|
@@ -49,6 +51,21 @@ def ask_email_invoice(self, _id): | |
self._ask_email_invoice = True | ||
return self.ask_email(_id) | ||
|
||
def cancel(self, _id): | ||
order = self._get(_id) | ||
self._cancel(order) | ||
return self._to_json(order)[0] | ||
|
||
def reset_to_cart(self, _id): | ||
order = self._get(_id) | ||
self._cancel(order, reset_to_cart=True) | ||
res = self._to_json(order)[0] | ||
return { | ||
"data": res, | ||
"set_session": {"cart_id": res["id"]}, | ||
"store_cache": {"cart": res}, | ||
} | ||
|
||
# Validator | ||
def _validator_search(self): | ||
default_search_validator = self._default_validator_search() | ||
|
@@ -59,6 +76,12 @@ def _validator_search(self): | |
def _validator_ask_email_invoice(self): | ||
return self._validator_ask_email() | ||
|
||
def _validator_cancel(self): | ||
return {} | ||
|
||
def _validator_reset_to_cart(self): | ||
return {} | ||
|
||
# The following method are 'private' and should be never never NEVER call | ||
# from the controller. | ||
# All params are trusted as they have been checked before | ||
|
@@ -102,6 +125,18 @@ def _get_invoices(self, sale): | |
domain_state = invoice_service._get_domain_state() | ||
return invoices.filtered_domain(domain_state) | ||
|
||
def _cancel(self, sale, reset_to_cart=False): | ||
for line in sale.order_line: | ||
if line.qty_delivered > 0 or line.qty_invoiced > 0: | ||
raise UserError( | ||
_("Orders that have been delivered or invoiced cannot be edited.") | ||
) | ||
sale.action_cancel() | ||
if reset_to_cart: | ||
sale.action_draft() | ||
sale.typology = "cart" | ||
return sale | ||
|
||
def _convert_invoices(self, invoices): | ||
return [self._convert_one_invoice(invoice) for invoice in invoices] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Copyright 2021 Akretion (https://www.akretion.com). | ||
# @author Pierrick Brun <[email protected]> | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). | ||
|
||
from odoo.exceptions import UserError | ||
|
||
from .test_sale import CommonSaleCase | ||
|
||
|
||
class TestSaleCancel(CommonSaleCase): | ||
def test_sale_cancel(self): | ||
self.sale.action_confirm() | ||
self.assertEqual("sale", self.sale.typology) | ||
self.assertEqual("sale", self.sale.state) | ||
|
||
self.service.dispatch("cancel", self.sale.id) | ||
|
||
self.assertEqual("cancel", self.sale.state) | ||
|
||
def test_sale_cancel_fail_if_delivered(self): | ||
self.sale.action_confirm() | ||
# deliver only 1 line | ||
self.sale.order_line[0].write({"qty_delivered": 1}) | ||
self.env["sale.order.line"].flush(records=self.sale.order_line) | ||
with self.assertRaises(UserError): | ||
self.service.dispatch("cancel", self.sale.id) | ||
self.assertEqual("sale", self.sale.typology) | ||
self.assertEqual("sale", self.sale.state) | ||
|
||
def test_sale_cancel_to_cart(self): | ||
self.sale.action_confirm() | ||
self.assertEqual("sale", self.sale.typology) | ||
self.assertEqual("sale", self.sale.state) | ||
|
||
result = self.service.dispatch("reset_to_cart", self.sale.id) | ||
|
||
session = result.get("set_session") | ||
self.assertEqual("draft", self.sale.state) | ||
self.assertEqual("cart", self.sale.typology) | ||
self.assertIsInstance(session, dict) | ||
self.assertEqual(session.get("cart_id"), self.sale.id) |