@@ -30,25 +30,32 @@ class SaleOrderLine(models.Model):
30
30
string = "Contract Template" ,
31
31
compute = "_compute_contract_template_id" ,
32
32
)
33
+ recurring_interval = fields .Integer (
34
+ default = 1 ,
35
+ string = "Invoice Every" ,
36
+ help = "Invoice every (Days/Week/Month/Year)" ,
37
+ )
33
38
recurring_rule_type = fields .Selection (related = "product_id.recurring_rule_type" )
34
39
recurring_invoicing_type = fields .Selection (
35
40
related = "product_id.recurring_invoicing_type"
36
41
)
37
- date_start = fields .Date ()
38
- date_end = fields .Date ()
39
-
42
+ date_start = fields .Date (
43
+ compute = "_compute_date_start" , readonly = False , store = True , precompute = True
44
+ )
45
+ date_end = fields .Date (
46
+ compute = "_compute_date_end" , readonly = False , store = True , precompute = True
47
+ )
40
48
contract_line_id = fields .Many2one (
41
49
comodel_name = "contract.line" ,
42
50
string = "Contract Line to replace" ,
43
- required = False ,
44
51
copy = False ,
45
52
)
46
53
is_auto_renew = fields .Boolean (
47
54
string = "Auto Renew" ,
48
55
compute = "_compute_auto_renew" ,
49
- default = False ,
50
56
store = True ,
51
57
readonly = False ,
58
+ precompute = True ,
52
59
)
53
60
auto_renew_interval = fields .Integer (
54
61
default = 1 ,
@@ -57,6 +64,7 @@ class SaleOrderLine(models.Model):
57
64
store = True ,
58
65
readonly = False ,
59
66
help = "Renew every (Days/Week/Month/Year)" ,
67
+ precompute = True ,
60
68
)
61
69
auto_renew_rule_type = fields .Selection (
62
70
[
@@ -71,6 +79,7 @@ class SaleOrderLine(models.Model):
71
79
readonly = False ,
72
80
string = "Renewal type" ,
73
81
help = "Specify Interval for automatic renewal." ,
82
+ precompute = True ,
74
83
)
75
84
contract_start_date_method = fields .Selection (
76
85
related = "product_id.contract_start_date_method"
@@ -97,47 +106,68 @@ def _compute_contract_template_id(self):
97
106
rec .order_id .company_id
98
107
).property_contract_template_id
99
108
100
- def _get_auto_renew_rule_type (self ):
101
- """monthly last day don't make sense for auto_renew_rule_type"""
102
- self .ensure_one ()
103
- if self .recurring_rule_type == "monthlylastday" :
104
- return "monthly"
105
- return self .recurring_rule_type
109
+ @api .depends ("product_id" )
110
+ def _compute_date_start (self ):
111
+ for sol in self :
112
+ if sol .contract_start_date_method == "start_this" :
113
+ sol .date_start = sol .order_id .date_order .replace (day = 1 )
114
+ elif sol .contract_start_date_method == "end_this" :
115
+ sol .date_start = (
116
+ sol .order_id .date_order
117
+ + self .get_relative_delta (
118
+ sol .recurring_rule_type , sol .product_id .default_qty
119
+ )
120
+ ).replace (day = 1 ) - relativedelta (days = 1 )
121
+ elif sol .contract_start_date_method == "start_next" :
122
+ # Dia 1 del siguiente recurring_rule_type
123
+ sol .date_start = (
124
+ sol .order_id .date_order
125
+ + self .get_relative_delta (
126
+ sol .recurring_rule_type , sol .product_id .default_qty
127
+ )
128
+ ).replace (day = 1 )
129
+ elif sol .contract_start_date_method == "end_next" :
130
+ # Last day of next recurring period
131
+ sol .date_start = (
132
+ sol .order_id .date_order
133
+ + self .get_relative_delta (
134
+ sol .recurring_rule_type , sol .product_id .default_qty + 1
135
+ )
136
+ ).replace (day = 1 ) - relativedelta (days = 1 )
137
+ else :
138
+ # Manual method
139
+ sol .date_start = False
106
140
107
- def _get_date_end (self ):
108
- self .ensure_one ()
109
- contract_start_date_method = self .product_id .contract_start_date_method
110
- date_end = False
111
- if contract_start_date_method == "manual" :
112
- contract_line_model = self .env ["contract.line" ]
113
- date_end = (
114
- self .date_start
115
- + contract_line_model .get_relative_delta (
116
- self ._get_auto_renew_rule_type (),
117
- int (self .product_uom_qty ),
141
+ @api .depends (
142
+ "is_auto_renew" ,
143
+ "date_start" ,
144
+ "auto_renew_interval" ,
145
+ "auto_renew_rule_type" ,
146
+ )
147
+ def _compute_date_end (self ):
148
+ for sol in self :
149
+ if sol .is_auto_renew and sol .date_start :
150
+ sol .date_end = self .env ["contract.line" ]._get_first_date_end (
151
+ sol .date_start ,
152
+ sol .auto_renew_rule_type ,
153
+ sol .auto_renew_interval ,
118
154
)
119
- - relativedelta (days = 1 )
120
- )
121
- return date_end
155
+ else :
156
+ sol .date_end = False
157
+
158
+ @api .model
159
+ def get_relative_delta (self , recurring_rule_type , interval ):
160
+ return self .env ["contract.recurrency.mixin" ].get_relative_delta (
161
+ recurring_rule_type , interval
162
+ )
122
163
123
164
@api .depends ("product_id" )
124
165
def _compute_auto_renew (self ):
125
- for rec in self :
126
- if rec .product_id .is_contract :
127
- rec .product_uom_qty = rec .product_id .default_qty
128
- contract_start_date_method = rec .product_id .contract_start_date_method
129
- if contract_start_date_method == "manual" :
130
- rec .date_start = rec .date_start or fields .Date .today ()
131
- rec .date_end = rec ._get_date_end ()
132
- rec .is_auto_renew = rec .product_id .is_auto_renew
133
- if rec .is_auto_renew :
134
- rec .auto_renew_interval = rec .product_id .auto_renew_interval
135
- rec .auto_renew_rule_type = rec .product_id .auto_renew_rule_type
136
-
137
- @api .onchange ("date_start" , "product_uom_qty" )
138
- def onchange_date_start (self ):
139
166
for rec in self .filtered ("product_id.is_contract" ):
140
- rec .date_end = rec ._get_date_end () if rec .date_start else False
167
+ rec .product_uom_qty = rec .product_id .default_qty
168
+ rec .is_auto_renew = rec .product_id .is_auto_renew
169
+ rec .auto_renew_interval = rec .product_id .auto_renew_interval
170
+ rec .auto_renew_rule_type = rec .product_id .auto_renew_rule_type
141
171
142
172
def _get_contract_line_qty (self ):
143
173
"""Returns the amount that will be placed in new contract lines."""
@@ -178,7 +208,7 @@ def _prepare_contract_line_values(
178
208
"date_end" : self .date_end ,
179
209
"date_start" : self .date_start or fields .Date .today (),
180
210
"recurring_next_date" : recurring_next_date ,
181
- "recurring_interval" : 1 ,
211
+ "recurring_interval" : self . recurring_interval or 1 ,
182
212
"recurring_invoicing_type" : self .recurring_invoicing_type ,
183
213
"recurring_rule_type" : self .recurring_rule_type ,
184
214
"is_auto_renew" : self .is_auto_renew ,
0 commit comments