mirror of
https://github.com/OCA/contract.git
synced 2025-02-13 17:57:24 +02:00
[FIX] contract. Optimize insertion of dates in invoice and lines.
This commit is contained in:
committed by
Denis Roussel
parent
f127c1dccd
commit
f6537a9fea
@@ -6,7 +6,8 @@
|
||||
# Copyright 2016-2017 LasLabs Inc.
|
||||
# Copyright 2017 Pesol (<http://pesol.es>)
|
||||
# Copyright 2017 Angel Moya <angel.moya@pesol.es>
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
# Copyright 2018 Therp BV <https://therp.nl>.
|
||||
# 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
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user