1
1
# Copyright 2024 Camptocamp SA
2
2
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
3
3
4
+ import psycopg2
5
+
6
+ from odoo import fields
7
+ from odoo .tests .common import Form
8
+
4
9
from odoo .addons .sale_stock_available_to_promise_release .tests import common
5
10
6
11
7
12
class TestSaleBlockRelease (common .Common ):
13
+ @classmethod
14
+ def setUpClass (cls ):
15
+ super ().setUpClass ()
16
+ # Ensure there is no security lead during tests
17
+ cls .env .company .security_lead = 0
18
+
8
19
def test_sale_release_not_blocked (self ):
9
20
self ._set_stock (self .line .product_id , self .line .product_uom_qty )
10
21
self .assertFalse (self .sale .block_release )
@@ -16,3 +27,92 @@ def test_sale_release_blocked(self):
16
27
self .sale .block_release = True
17
28
self .sale .action_confirm ()
18
29
self .assertTrue (self .sale .picking_ids .release_blocked )
30
+
31
+ def _create_unblock_release_wizard (
32
+ self , order_lines , date_deadline = None , from_order = None , option = "free"
33
+ ):
34
+ wiz_form = Form (
35
+ self .env ["unblock.release" ].with_context (
36
+ from_sale_order_id = from_order and from_order .id ,
37
+ active_model = order_lines ._name ,
38
+ active_ids = order_lines .ids ,
39
+ default_option = option ,
40
+ )
41
+ )
42
+ if date_deadline :
43
+ wiz_form .date_deadline = date_deadline
44
+ return wiz_form .save ()
45
+
46
+ def test_sale_order_line_unblock_release_contextual (self ):
47
+ self ._set_stock (self .line .product_id , self .line .product_uom_qty )
48
+ self .sale .block_release = True
49
+ self .sale .action_confirm ()
50
+ # Unblock deliveries through the wizard, opened from another SO
51
+ # to define default values
52
+ new_sale = self ._create_sale_order ()
53
+ new_sale .commitment_date = fields .Datetime .add (fields .Datetime .now (), days = 1 )
54
+ wiz = self ._create_unblock_release_wizard (
55
+ self .sale .order_line , from_order = new_sale
56
+ )
57
+ self .assertEqual (wiz .option , "contextual" )
58
+ self .assertEqual (wiz .date_deadline , new_sale .commitment_date )
59
+ self .assertNotEqual (wiz .order_line_ids .move_ids .date , new_sale .commitment_date )
60
+ old_picking = wiz .order_line_ids .move_ids .picking_id
61
+ wiz .validate ()
62
+ # Deliveries have been scheduled to the new date deadline
63
+ new_picking = wiz .order_line_ids .move_ids .picking_id
64
+ self .assertEqual (wiz .order_line_ids .move_ids .date , new_sale .commitment_date )
65
+ self .assertNotEqual (old_picking , new_picking )
66
+ self .assertFalse (old_picking .exists ())
67
+
68
+ def test_sale_order_line_unblock_release_free (self ):
69
+ self ._set_stock (self .line .product_id , self .line .product_uom_qty )
70
+ self .sale .block_release = True
71
+ self .sale .action_confirm ()
72
+ # Unblock deliveries through the wizard
73
+ new_date_deadline = fields .Datetime .add (fields .Datetime .now (), days = 1 )
74
+ wiz = self ._create_unblock_release_wizard (
75
+ self .sale .order_line , date_deadline = new_date_deadline
76
+ )
77
+ self .assertEqual (wiz .date_deadline , new_date_deadline )
78
+ self .assertNotEqual (wiz .order_line_ids .move_ids .date , new_date_deadline )
79
+ old_picking = wiz .order_line_ids .move_ids .picking_id
80
+ wiz .validate ()
81
+ # Deliveries have been scheduled to the new date deadline
82
+ new_picking = wiz .order_line_ids .move_ids .picking_id
83
+ self .assertEqual (wiz .order_line_ids .move_ids .date , new_date_deadline )
84
+ self .assertNotEqual (old_picking , new_picking )
85
+ self .assertFalse (old_picking .exists ())
86
+
87
+ def test_sale_order_line_unblock_release_asap (self ):
88
+ # Start with a blocked SO having a commitment date in the past
89
+ self ._set_stock (self .line .product_id , self .line .product_uom_qty )
90
+ self .sale .block_release = True
91
+ yesterday = fields .Datetime .subtract (fields .Datetime .now (), days = 1 )
92
+ self .sale .commitment_date = yesterday
93
+ self .sale .action_confirm ()
94
+ # Unblock deliveries through the wizard
95
+ today = fields .Datetime .now ()
96
+ wiz = self ._create_unblock_release_wizard (self .sale .order_line , option = "asap" )
97
+ self .assertEqual (wiz .date_deadline , today )
98
+ self .assertNotEqual (wiz .order_line_ids .move_ids .date , today )
99
+ old_picking = wiz .order_line_ids .move_ids .picking_id
100
+ wiz .validate ()
101
+ # Deliveries have been scheduled for today
102
+ new_picking = wiz .order_line_ids .move_ids .picking_id
103
+ self .assertEqual (wiz .order_line_ids .move_ids .date , today )
104
+ self .assertNotEqual (old_picking , new_picking )
105
+ self .assertFalse (old_picking .exists ())
106
+
107
+ def test_sale_order_line_unblock_release_past_date_deadline (self ):
108
+ self ._set_stock (self .line .product_id , self .line .product_uom_qty )
109
+ self .sale .block_release = True
110
+ self .sale .action_confirm ()
111
+ # Try to unblock deliveries through the wizard with a scheduled date
112
+ # in the past
113
+ new_sale = self ._create_sale_order ()
114
+ yesterday = fields .Datetime .subtract (fields .Datetime .now (), days = 1 )
115
+ with self .assertRaises (psycopg2 .errors .CheckViolation ):
116
+ self ._create_unblock_release_wizard (
117
+ self .sale .order_line , date_deadline = yesterday , from_order = new_sale
118
+ )
0 commit comments