@@ -142,21 +142,24 @@ def cron_subscription_management(self):
142
142
for subscription in self .search ([]):
143
143
if subscription .in_progress :
144
144
if (
145
- subscription .recurring_next_date = = today
145
+ subscription .recurring_next_date < = today
146
146
and subscription .sale_subscription_line_ids
147
147
):
148
148
try :
149
149
subscription .generate_invoice ()
150
150
except Exception :
151
151
logger .exception ("Error on subscription invoice generate" )
152
- if not subscription .recurring_rule_boundary :
153
- if subscription .date == today :
154
- subscription .action_close_subscription ()
152
+ if (
153
+ not subscription .recurring_rule_boundary
154
+ and subscription .date <= today
155
+ ):
156
+ subscription .close_subscription ()
155
157
156
- else :
157
- if subscription .date_start == today :
158
- subscription .action_start_subscription ()
159
- subscription .generate_invoice ()
158
+ elif (
159
+ subscription .date_start <= today and subscription .stage_id .type == "pre"
160
+ ):
161
+ subscription .action_start_subscription ()
162
+ subscription .generate_invoice ()
160
163
161
164
@api .depends ("sale_subscription_line_ids" )
162
165
def _compute_total (self ):
@@ -239,7 +242,6 @@ def action_start_subscription(self):
239
242
self .stage_id = in_progress_stage
240
243
241
244
def action_close_subscription (self ):
242
- self .recurring_next_date = False
243
245
return {
244
246
"view_type" : "form" ,
245
247
"view_mode" : "form" ,
@@ -249,6 +251,15 @@ def action_close_subscription(self):
249
251
"res_id" : False ,
250
252
}
251
253
254
+ def close_subscription (self , close_reason_id = False ):
255
+ self .ensure_one ()
256
+ self .recurring_next_date = False
257
+ closed_stage = self .env ["sale.subscription.stage" ].search (
258
+ [("type" , "=" , "post" )], limit = 1
259
+ )
260
+ self .close_reason_id = close_reason_id
261
+ self .stage_id = closed_stage
262
+
252
263
def _prepare_sale_order (self , line_ids = False ):
253
264
self .ensure_one ()
254
265
return {
@@ -271,6 +282,7 @@ def _prepare_account_move(self, line_ids):
271
282
"invoice_user_id" : self .user_id .id ,
272
283
"partner_bank_id" : self .company_id .partner_id .bank_ids [:1 ].id ,
273
284
"invoice_line_ids" : line_ids ,
285
+ "subscription_id" : self .id ,
274
286
}
275
287
if self .journal_id :
276
288
values ["journal_id" ] = self .journal_id .id
@@ -294,7 +306,6 @@ def create_invoice(self):
294
306
.with_context (default_move_type = "out_invoice" , journal_type = "sale" )
295
307
.create (invoice_values )
296
308
)
297
- self .write ({"invoice_ids" : [(4 , invoice_id .id )]})
298
309
return invoice_id
299
310
300
311
def create_sale_order (self ):
@@ -464,7 +475,7 @@ def create(self, values):
464
475
values ["date_start" ] = values ["recurring_next_date" ]
465
476
values ["stage_id" ] = (
466
477
self .env ["sale.subscription.stage" ]
467
- .search ([("type" , "=" , "pre " )], order = "sequence desc" , limit = 1 )
478
+ .search ([("type" , "=" , "draft " )], order = "sequence desc" , limit = 1 )
468
479
.id
469
480
)
470
481
return super (SaleSubscription , self ).create (values )
0 commit comments