mirror of
https://github.com/OCA/contract.git
synced 2025-02-13 17:57:24 +02:00
[IMP] contract: Module contract improvements (#107)
This commit is contained in:
committed by
Pedro M. Baeza
parent
fccb967ada
commit
684fda7613
@@ -6,3 +6,4 @@ from . import account_analytic_account
|
||||
from . import account_analytic_invoice_line
|
||||
from . import account_analytic_contract_line
|
||||
from . import account_invoice
|
||||
from . import res_partner
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# © 2004-2010 OpenERP SA
|
||||
# © 2014 Angel Moya <angel.moya@domatix.com>
|
||||
# © 2015 Pedro M. Baeza <pedro.baeza@tecnativa.com>
|
||||
# © 2016 Carlos Dauden <carlos.dauden@tecnativa.com>
|
||||
# Copyright 2004-2010 OpenERP SA
|
||||
# Copyright 2014 Angel Moya <angel.moya@domatix.com>
|
||||
# Copyright 2015 Pedro M. Baeza <pedro.baeza@tecnativa.com>
|
||||
# Copyright 2016-2017 Carlos Dauden <carlos.dauden@tecnativa.com>
|
||||
# Copyright 2016-2017 LasLabs Inc.
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
|
||||
@@ -29,7 +29,14 @@ class AccountAnalyticAccount(models.Model):
|
||||
inverse_name='analytic_account_id',
|
||||
copy=True,
|
||||
)
|
||||
date_start = fields.Date(default=fields.Date.context_today)
|
||||
date_start = fields.Date(
|
||||
string='Date Start',
|
||||
default=fields.Date.context_today,
|
||||
)
|
||||
date_end = fields.Date(
|
||||
string='Date End',
|
||||
index=True,
|
||||
)
|
||||
recurring_invoices = fields.Boolean(
|
||||
string='Generate recurring invoices automatically',
|
||||
)
|
||||
@@ -198,9 +205,19 @@ class AccountAnalyticAccount(models.Model):
|
||||
|
||||
@api.multi
|
||||
def recurring_create_invoice(self):
|
||||
"""
|
||||
Create invoices from contracts
|
||||
:return: invoices created
|
||||
"""
|
||||
invoices = self.env['account.invoice']
|
||||
for contract in self:
|
||||
old_date = fields.Date.from_string(
|
||||
contract.recurring_next_date or fields.Date.today())
|
||||
ref_date = contract.recurring_next_date or fields.Date.today()
|
||||
if (contract.date_start > ref_date or
|
||||
contract.date_end and contract.date_end < ref_date):
|
||||
raise ValidationError(
|
||||
_("You must review start and end dates!\n%s") %
|
||||
contract.name)
|
||||
old_date = fields.Date.from_string(ref_date)
|
||||
new_date = old_date + self.get_relative_delta(
|
||||
contract.recurring_rule_type, contract.recurring_interval)
|
||||
ctx = self.env.context.copy()
|
||||
@@ -211,17 +228,22 @@ class AccountAnalyticAccount(models.Model):
|
||||
'force_company': contract.company_id.id,
|
||||
})
|
||||
# Re-read contract with correct company
|
||||
contract.with_context(ctx)._create_invoice()
|
||||
invoices |= contract.with_context(ctx)._create_invoice()
|
||||
contract.write({
|
||||
'recurring_next_date': new_date.strftime('%Y-%m-%d')
|
||||
})
|
||||
return True
|
||||
return invoices
|
||||
|
||||
@api.model
|
||||
def cron_recurring_create_invoice(self):
|
||||
contracts = self.search(
|
||||
[('recurring_next_date', '<=', fields.date.today()),
|
||||
('recurring_invoices', '=', True)])
|
||||
today = fields.Date.today()
|
||||
contracts = self.search([
|
||||
('recurring_invoices', '=', True),
|
||||
('recurring_next_date', '<=', today),
|
||||
'|',
|
||||
('date_end', '=', False),
|
||||
('date_end', '>=', today),
|
||||
])
|
||||
return contracts.recurring_create_invoice()
|
||||
|
||||
@api.multi
|
||||
|
||||
46
contract/models/res_partner.py
Normal file
46
contract/models/res_partner.py
Normal file
@@ -0,0 +1,46 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright 2017 Carlos Dauden <carlos.dauden@tecnativa.com>
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
|
||||
from odoo import fields, models
|
||||
|
||||
|
||||
class ResPartner(models.Model):
|
||||
_inherit = 'res.partner'
|
||||
|
||||
contract_count = fields.Integer(
|
||||
string='Contracts',
|
||||
compute='_compute_contract_count',
|
||||
)
|
||||
|
||||
def _compute_contract_count(self):
|
||||
Contract = self.env['account.analytic.account']
|
||||
today = fields.Date.today()
|
||||
for partner in self:
|
||||
partner.contract_count = Contract.search_count([
|
||||
('recurring_invoices', '=', True),
|
||||
('partner_id', '=', partner.id),
|
||||
('date_start', '<=', today),
|
||||
'|',
|
||||
('date_end', '=', False),
|
||||
('date_end', '>=', today),
|
||||
])
|
||||
|
||||
def act_show_contract(self):
|
||||
""" This opens contract view
|
||||
@return: the contract view
|
||||
"""
|
||||
self.ensure_one()
|
||||
res = self.env['ir.actions.act_window'].for_xml_id(
|
||||
'contract', 'action_account_analytic_overdue_all')
|
||||
res.update(
|
||||
context=dict(
|
||||
self.env.context,
|
||||
search_default_recurring_invoices=True,
|
||||
search_default_not_finished=True,
|
||||
default_partner_id=self.id,
|
||||
default_recurring_invoices=True,
|
||||
),
|
||||
domain=[('partner_id', '=', self.id)],
|
||||
)
|
||||
return res
|
||||
Reference in New Issue
Block a user