From 14f3cc27750fc6cae5442f22847e84c7a2e74ae1 Mon Sep 17 00:00:00 2001 From: Ronald Portier Date: Thu, 6 Jun 2019 01:32:08 +0200 Subject: [PATCH] [10.0][IMP] contract. Enable automatic invoice generation before invoice date. (#282) * [IMP] contract. Enable automatic invoice generation before invoice date. * [IMP] contract pregenerate - improvements after review. --- contract/__manifest__.py | 3 +- contract/models/__init__.py | 3 +- contract/models/account_analytic_account.py | 34 +++++++++++++++------ contract/models/res_company.py | 14 +++++++++ contract/views/res_company.xml | 14 +++++++++ 5 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 contract/models/res_company.py create mode 100644 contract/views/res_company.xml diff --git a/contract/__manifest__.py b/contract/__manifest__.py index 54250c1c..70ff7890 100644 --- a/contract/__manifest__.py +++ b/contract/__manifest__.py @@ -5,7 +5,7 @@ # Copyright 2016-2017 Tecnativa - Carlos Dauden # Copyright 2017 Tecnativa - Vicent Cubells # Copyright 2016-2017 LasLabs Inc. -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). { 'name': 'Contracts Management - Recurring', @@ -29,6 +29,7 @@ 'views/account_analytic_contract_view.xml', 'views/account_invoice_view.xml', 'views/res_partner_view.xml', + 'views/res_company.xml', ], 'installable': True, } diff --git a/contract/models/__init__.py b/contract/models/__init__.py index 7d711125..34080941 100644 --- a/contract/models/__init__.py +++ b/contract/models/__init__.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). from . import account_analytic_contract from . import account_analytic_account @@ -7,3 +7,4 @@ from . import account_analytic_contract_line from . import account_analytic_invoice_line from . import account_invoice from . import res_partner +from . import res_company diff --git a/contract/models/account_analytic_account.py b/contract/models/account_analytic_account.py index 3eabbbf5..b604d1eb 100644 --- a/contract/models/account_analytic_account.py +++ b/contract/models/account_analytic_account.py @@ -4,15 +4,17 @@ # Copyright 2015 Pedro M. Baeza # Copyright 2016-2017 Carlos Dauden # Copyright 2016-2017 LasLabs Inc. -# Copyright 2018 Therp BV . +# Copyright 2018-2019 Therp BV . # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). # pylint: disable=no-member +from datetime import datetime from dateutil.relativedelta import relativedelta from odoo import api, fields, models from odoo.exceptions import ValidationError from odoo.tools.translate import _ +from odoo.tools import DEFAULT_SERVER_DATE_FORMAT class AccountAnalyticAccount(models.Model): @@ -341,14 +343,28 @@ class AccountAnalyticAccount(models.Model): @api.model def cron_recurring_create_invoice(self, limit=None): - today = fields.Date.today() - contracts = self.with_context(cron=True).search([ - ('recurring_invoices', '=', True), - ('recurring_next_date', '<=', today), - '|', - ('date_end', '=', False), - ('date_end', '>=', today), - ]) + """Generate invoices. + + Invoices can be generated a specified number of days before the + invoice date, to allow the user to check the invoices before + confirmation, or to send out the invoices to the customers before + the invoice date. + """ + today = datetime.today() + company_model = self.env['res.company'] + for company in company_model.search([]): + days_before = company.contract_pregenerate_days or 0 + cutoffdate = ( + today + relativedelta(days=days_before) + ).strftime(DEFAULT_SERVER_DATE_FORMAT) + contracts = self.with_context(cron=True).search([ + ('company_id', '=', company.id), + ('recurring_invoices', '=', True), + ('recurring_next_date', '<=', cutoffdate), + '|', + ('date_end', '=', False), + ('date_end', '>=', cutoffdate), + ]) return contracts.recurring_create_invoice(limit) @api.multi diff --git a/contract/models/res_company.py b/contract/models/res_company.py new file mode 100644 index 00000000..d2b94153 --- /dev/null +++ b/contract/models/res_company.py @@ -0,0 +1,14 @@ +# -*- coding: utf-8 -*- +# Copyright 2019 - Therp BV . +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from odoo import fields, models + + +class ResCompany(models.Model): + _inherit = 'res.company' + + contract_pregenerate_days = fields.Integer( + string='Days to prepare invoices', + help="Days before next invoice date to generate draft invoices.\n" + "This allows users to check the invoices before confirmation,\n" + " and to send invoices to customers, before the invoice date.") diff --git a/contract/views/res_company.xml b/contract/views/res_company.xml new file mode 100644 index 00000000..bbfa1de8 --- /dev/null +++ b/contract/views/res_company.xml @@ -0,0 +1,14 @@ + + + + + + res.company + + + + + + + +