From 4a947b4d2bb756bcd8f921cf5b1f2689d7599a2c Mon Sep 17 00:00:00 2001 From: Ronald Portier Date: Sat, 22 Dec 2018 11:09:50 +0100 Subject: [PATCH] [FIX] contract. Optimize insertion of dates in invoice and lines. --- .../models/account_analytic_account.py | 35 ++++--------------- .../tests/test_contract_sale.py | 8 +++++ 2 files changed, 15 insertions(+), 28 deletions(-) diff --git a/contract_sale_generation/models/account_analytic_account.py b/contract_sale_generation/models/account_analytic_account.py index c07d6b0d..79405fd7 100644 --- a/contract_sale_generation/models/account_analytic_account.py +++ b/contract_sale_generation/models/account_analytic_account.py @@ -6,7 +6,8 @@ # Copyright 2016-2017 LasLabs Inc. # Copyright 2017 Pesol () # Copyright 2017 Angel Moya -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +# Copyright 2018 Therp BV . +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). from odoo import api, models, fields from odoo.exceptions import ValidationError @@ -29,16 +30,7 @@ class AccountAnalyticAccount(models.Model): sale_line.product_id_change() sale_line_vals = sale_line._convert_to_write(sale_line._cache) # Insert markers - name = line.name - contract = line.analytic_account_id - if 'old_date' in self.env.context and 'next_date' in self.env.context: - lang_obj = self.env['res.lang'] - lang = lang_obj.search( - [('code', '=', contract.partner_id.lang)]) - date_format = lang.date_format or '%m/%d/%Y' - name = self._insert_markers( - line, self.env.context['old_date'], - self.env.context['next_date'], date_format) + name = self._insert_markers(line.name) sale_line_vals.update({ 'name': name, 'discount': line.discount, @@ -106,26 +98,13 @@ class AccountAnalyticAccount(models.Model): """ sales = self.env['sale.order'] for contract in self: - 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() - ctx.update({ - 'old_date': old_date, - 'next_date': new_date, - # Force company for correct evaluate domain access rules - 'force_company': contract.company_id.id, - }) + if not contract.check_dates_valid(): + continue # Re-read contract with correct company + ctx = contract.get_invoice_context() sales |= contract.with_context(ctx)._create_sale() contract.write({ - 'recurring_next_date': new_date.strftime('%Y-%m-%d') + 'recurring_next_date': fields.Date.to_string(ctx['next_date']) }) return sales diff --git a/contract_sale_generation/tests/test_contract_sale.py b/contract_sale_generation/tests/test_contract_sale.py index b64286b5..035ca8b9 100644 --- a/contract_sale_generation/tests/test_contract_sale.py +++ b/contract_sale_generation/tests/test_contract_sale.py @@ -98,3 +98,11 @@ class TestContractSale(TransactionCase): } del self.template_vals['name'] self.assertDictEqual(res, self.template_vals) + + def test_check_cron_ended_contract(self): + self.contract.recurring_next_date = '2016-02-29' + self.contract.recurring_rule_type = 'yearly' + self.contract.date_end = '2016-02-28' + sale_orders = self.contract.with_context( + cron=True).recurring_create_sale() + self.assertFalse(sale_orders)