|
@ -103,10 +103,12 @@ class SubscriptionPackage(models.Model): |
|
|
readonly=False) |
|
|
readonly=False) |
|
|
plan_id = fields.Many2one('subscription.package.plan', |
|
|
plan_id = fields.Many2one('subscription.package.plan', |
|
|
string='Subscription Plan') |
|
|
string='Subscription Plan') |
|
|
start_date = fields.Date(string='Start Date', store=True, |
|
|
start_date = fields.Date(string='Period Start Date', store=True, |
|
|
ondelete='restrict') |
|
|
ondelete='restrict') |
|
|
next_invoice_date = fields.Date(string='Next Invoice Date', readonly=False, |
|
|
date_started = fields.Date(string='Subsciption Start date', store=True, |
|
|
store=True, |
|
|
ondelete='restrict', readonly=True) |
|
|
|
|
|
next_invoice_date = fields.Date(string='Next Invoice Date', |
|
|
|
|
|
store=False, readonly=False, |
|
|
compute="_compute_next_invoice_date") |
|
|
compute="_compute_next_invoice_date") |
|
|
company_id = fields.Many2one('res.company', string='Company', |
|
|
company_id = fields.Many2one('res.company', string='Company', |
|
|
default=lambda self: self.env.company, |
|
|
default=lambda self: self.env.company, |
|
@ -121,7 +123,7 @@ class SubscriptionPackage(models.Model): |
|
|
index=True, |
|
|
index=True, |
|
|
group_expand='_read_group_stage_ids') |
|
|
group_expand='_read_group_stage_ids') |
|
|
invoice_count = fields.Integer(string='Invoices', |
|
|
invoice_count = fields.Integer(string='Invoices', |
|
|
compute='_compute_invoice_count') |
|
|
compute='_compute_invoice_count', store=True) |
|
|
so_count = fields.Integer(string='Sales', compute='_compute_sale_count') |
|
|
so_count = fields.Integer(string='Sales', compute='_compute_sale_count') |
|
|
description = fields.Text(string='Description') |
|
|
description = fields.Text(string='Description') |
|
|
analytic_account_id = fields.Many2one('account.analytic.account', |
|
|
analytic_account_id = fields.Many2one('account.analytic.account', |
|
@ -178,6 +180,8 @@ class SubscriptionPackage(models.Model): |
|
|
if sub.start_date: |
|
|
if sub.start_date: |
|
|
sub.next_invoice_date = sub.start_date + relativedelta( |
|
|
sub.next_invoice_date = sub.start_date + relativedelta( |
|
|
days=sub.plan_id.renewal_time) |
|
|
days=sub.plan_id.renewal_time) |
|
|
|
|
|
else: |
|
|
|
|
|
sub.next_invoice_date = None |
|
|
|
|
|
|
|
|
def button_invoice_count(self): |
|
|
def button_invoice_count(self): |
|
|
""" It displays invoice based on subscription package """ |
|
|
""" It displays invoice based on subscription package """ |
|
@ -225,8 +229,10 @@ class SubscriptionPackage(models.Model): |
|
|
rec_list = [0, 0, {'product_id': rec.product_id.id, |
|
|
rec_list = [0, 0, {'product_id': rec.product_id.id, |
|
|
'quantity': rec.product_qty}] |
|
|
'quantity': rec.product_qty}] |
|
|
this_products_line.append(rec_list) |
|
|
this_products_line.append(rec_list) |
|
|
invoices = self.env['account.move'].search([('subscription_id', '=', self.id), ('state', '=', 'draft')]) |
|
|
invoices = self.env['account.move'].search( |
|
|
orders = self.env['sale.order'].search([('subscription_id', '=', self.id), ('invoice_status', '=', 'no')]) |
|
|
[('subscription_id', '=', self.id), ('state', '=', 'draft')]) |
|
|
|
|
|
orders = self.env['sale.order'].search( |
|
|
|
|
|
[('subscription_id', '=', self.id), ('invoice_status', '=', 'no')]) |
|
|
if invoices: |
|
|
if invoices: |
|
|
for invoice in invoices: |
|
|
for invoice in invoices: |
|
|
invoice.action_post() |
|
|
invoice.action_post() |
|
@ -258,18 +264,20 @@ class SubscriptionPackage(models.Model): |
|
|
|
|
|
|
|
|
def button_start_date(self): |
|
|
def button_start_date(self): |
|
|
"""Button to start subscription package""" |
|
|
"""Button to start subscription package""" |
|
|
if not self.start_date: |
|
|
stage_id = (self.env['subscription.package.stage'].search([ |
|
|
self.start_date = datetime.date.today() |
|
|
('category', '=', 'progress')], limit=1).id) |
|
|
for rec in self: |
|
|
for rec in self: |
|
|
if len(rec.env['subscription.package.stage'].search([('category', '=', 'draft')])) > 1: |
|
|
if len(rec.env['subscription.package.stage'].search( |
|
|
|
|
|
[('category', '=', 'draft')])) > 1: |
|
|
raise UserError( |
|
|
raise UserError( |
|
|
_('More than one stage is having category "Draft". ' |
|
|
_('More than one stage is having category "Draft". ' |
|
|
'Please change category of stage to "In Progress", ' |
|
|
'Please change category of stage to "In Progress", ' |
|
|
'only one stage is allowed to have category "Draft"')) |
|
|
'only one stage is allowed to have category "Draft"')) |
|
|
else: |
|
|
else: |
|
|
rec.write( |
|
|
rec.write( |
|
|
{'stage_id': (rec.env['subscription.package.stage'].search([ |
|
|
{'stage_id': stage_id, |
|
|
('category', '=', 'draft')]).id) + 1}) |
|
|
'date_started': fields.Date.today() |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
def button_sale_order(self): |
|
|
def button_sale_order(self): |
|
|
"""Button to create sale order""" |
|
|
"""Button to create sale order""" |
|
@ -279,7 +287,8 @@ class SubscriptionPackage(models.Model): |
|
|
'product_uom_qty': rec.product_qty, |
|
|
'product_uom_qty': rec.product_qty, |
|
|
'discount': rec.discount}] |
|
|
'discount': rec.discount}] |
|
|
this_products_line.append(rec_list) |
|
|
this_products_line.append(rec_list) |
|
|
orders = self.env['sale.order'].search([('subscription_id', '=', self.id), ('invoice_status', '=', 'no')]) |
|
|
orders = self.env['sale.order'].search( |
|
|
|
|
|
[('subscription_id', '=', self.id), ('invoice_status', '=', 'no')]) |
|
|
if orders: |
|
|
if orders: |
|
|
for order in orders: |
|
|
for order in orders: |
|
|
order.action_confirm() |
|
|
order.action_confirm() |
|
@ -333,51 +342,105 @@ class SubscriptionPackage(models.Model): |
|
|
sub.write(values) |
|
|
sub.write(values) |
|
|
return True |
|
|
return True |
|
|
|
|
|
|
|
|
|
|
|
def find_renew_date(self, next_invoice, date_started, end): |
|
|
|
|
|
if end == 0: |
|
|
|
|
|
end_date = next_invoice |
|
|
|
|
|
difference = (next_invoice - date_started).days / 10 |
|
|
|
|
|
renew_date = next_invoice - relativedelta( |
|
|
|
|
|
days=difference) |
|
|
|
|
|
close_date = next_invoice |
|
|
|
|
|
else: |
|
|
|
|
|
end_date = fields.Date.add(date_started, |
|
|
|
|
|
days=end) |
|
|
|
|
|
close = date_started + relativedelta(days=end) |
|
|
|
|
|
difference = (close - date_started).days / 10 |
|
|
|
|
|
renew_date = close - relativedelta( |
|
|
|
|
|
days=difference) |
|
|
|
|
|
close_date = close |
|
|
|
|
|
|
|
|
|
|
|
data = {'renew_date': renew_date, |
|
|
|
|
|
'end_date': end_date, |
|
|
|
|
|
'close_date': close_date} |
|
|
|
|
|
return data |
|
|
|
|
|
|
|
|
def close_limit_cron(self): |
|
|
def close_limit_cron(self): |
|
|
""" It Checks renew date, close date. It will send mail when renew date """ |
|
|
""" It Checks renew date, close date. It will send mail when renew |
|
|
|
|
|
date""" |
|
|
pending_subscriptions = self.env['subscription.package'].search( |
|
|
pending_subscriptions = self.env['subscription.package'].search( |
|
|
[('stage_category', '=', 'progress')]) |
|
|
[('stage_category', '=', 'progress')]) |
|
|
today_date = fields.Date.today() |
|
|
today_date = fields.Date.today() |
|
|
pending_subscription = False |
|
|
pending_subscription = False |
|
|
close_subscription = False |
|
|
|
|
|
for pending_subscription in pending_subscriptions: |
|
|
for pending_subscription in pending_subscriptions: |
|
|
pending_subscription.close_date = pending_subscription.start_date + relativedelta( |
|
|
|
|
|
days=pending_subscription.plan_id.days_to_end) |
|
|
get_dates = self.find_renew_date( |
|
|
difference = ( |
|
|
pending_subscription.next_invoice_date, |
|
|
pending_subscription.close_date - pending_subscription.start_date).days / 10 |
|
|
pending_subscription.date_started, |
|
|
renew_date = pending_subscription.close_date - relativedelta( |
|
|
pending_subscription.plan_id.days_to_end) |
|
|
days=difference) |
|
|
renew_date = get_dates['renew_date'] |
|
|
if today_date == renew_date: |
|
|
end_date = get_dates['end_date'] |
|
|
self.env.ref( |
|
|
pending_subscription.close_date = get_dates['close_date'] |
|
|
'subscription_package.mail_template_subscription_renew').send_mail( |
|
|
if (today_date == end_date) and ( |
|
|
pending_subscription.id, force_send=True) |
|
|
pending_subscription.plan_id.limit_choice != 'manual'): |
|
|
pending_subscription.write({'to_renew': True}) |
|
|
display_msg = ("<h5><i>The renewal limit has been exceeded " |
|
|
|
|
|
"today for this subscription based on the " |
|
|
|
|
|
"current subscription plan.</i></h5>") |
|
|
|
|
|
pending_subscription.message_post(body=display_msg) |
|
|
|
|
|
pending_subscription.is_closed = True |
|
|
|
|
|
reason = (self.env['subscription.package.stop'].search([ |
|
|
|
|
|
('name', '=', 'Renewal Limit Exceeded')]).id) |
|
|
|
|
|
pending_subscription.close_reason = reason |
|
|
|
|
|
pending_subscription.closed_by = self.user_id |
|
|
|
|
|
pending_subscription.close_date = fields.Date.today() |
|
|
|
|
|
stage = (self.env['subscription.package.stage'].search([ |
|
|
|
|
|
('category', '=', 'closed')]).id) |
|
|
|
|
|
values = {'stage_id': stage, 'to_renew': False} |
|
|
|
|
|
pending_subscription.write(values) |
|
|
|
|
|
|
|
|
|
|
|
if today_date == pending_subscription.next_invoice_date: |
|
|
if pending_subscription.plan_id.invoice_mode == 'draft_invoice': |
|
|
if pending_subscription.plan_id.invoice_mode == 'draft_invoice': |
|
|
this_products_line = [] |
|
|
this_products_line = [] |
|
|
for rec in pending_subscription.product_line_ids: |
|
|
for rec in pending_subscription.product_line_ids: |
|
|
rec_list = [0, 0, {'product_id': rec.product_id.id, |
|
|
rec_list = [0, 0, {'product_id': rec.product_id.id, |
|
|
'quantity': rec.product_qty}] |
|
|
'quantity': rec.product_qty, |
|
|
|
|
|
'price_unit': rec.unit_price, |
|
|
|
|
|
'discount': rec.discount}] |
|
|
this_products_line.append(rec_list) |
|
|
this_products_line.append(rec_list) |
|
|
self.env['account.move'].create( |
|
|
self.env['account.move'].create( |
|
|
{ |
|
|
{ |
|
|
'move_type': 'out_invoice', |
|
|
'move_type': 'out_invoice', |
|
|
'date': fields.Date.today(), |
|
|
'date': today_date, |
|
|
'invoice_date': fields.Date.today(), |
|
|
'invoice_date': today_date, |
|
|
'state': 'draft', |
|
|
'state': 'draft', |
|
|
|
|
|
'subscription_id': pending_subscription.id, |
|
|
'partner_id': pending_subscription.partner_invoice_id.id, |
|
|
'partner_id': pending_subscription.partner_invoice_id.id, |
|
|
'currency_id': pending_subscription.partner_invoice_id.currency_id.id, |
|
|
'currency_id': pending_subscription.partner_invoice_id.currency_id.id, |
|
|
'invoice_line_ids': this_products_line |
|
|
'invoice_line_ids': this_products_line |
|
|
}) |
|
|
}) |
|
|
|
|
|
pending_subscription._compute_invoice_count() |
|
|
|
|
|
|
|
|
pending_subscription.write({'to_renew': False, |
|
|
pending_subscription.write({'to_renew': False, |
|
|
'start_date': datetime.datetime.today()}) |
|
|
'start_date': pending_subscription.next_invoice_date}) |
|
|
close_subscriptions = self.env['subscription.package'].search( |
|
|
new_date = self.find_renew_date( |
|
|
[('stage_category', '=', 'progress'), ('to_renew', '=', True)]) |
|
|
pending_subscription.next_invoice_date, |
|
|
for close_subscription in close_subscriptions: |
|
|
pending_subscription.date_started, |
|
|
close_subscription.close_date = close_subscription.start_date + relativedelta( |
|
|
pending_subscription.plan_id.days_to_end) |
|
|
days=close_subscription.plan_id.days_to_end) |
|
|
pending_subscription.write( |
|
|
if today_date == close_subscription.close_date: |
|
|
{'close_date': new_date['close_date']}) |
|
|
close_subscription.set_close() |
|
|
if today_date == new_date['renew_date']: |
|
|
return dict(pending=pending_subscription, closed=close_subscription) |
|
|
self.env.ref( |
|
|
|
|
|
'subscription_package' |
|
|
|
|
|
'.mail_template_subscription_renew').send_mail( |
|
|
|
|
|
pending_subscription.id, force_send=True) |
|
|
|
|
|
pending_subscription.write({'to_renew': True}) |
|
|
|
|
|
|
|
|
|
|
|
if today_date == renew_date: |
|
|
|
|
|
self.env.ref( |
|
|
|
|
|
'subscription_package.mail_template_subscription_renew').send_mail( |
|
|
|
|
|
pending_subscription.id, force_send=True) |
|
|
|
|
|
pending_subscription.write({'to_renew': True}) |
|
|
|
|
|
|
|
|
|
|
|
return dict(pending=pending_subscription) |
|
|
|
|
|
|
|
|
@api.depends('product_line_ids.total_amount') |
|
|
@api.depends('product_line_ids.total_amount') |
|
|
def _compute_total_recurring_price(self): |
|
|
def _compute_total_recurring_price(self): |
|
|