diff --git a/contract_sale_generation/__manifest__.py b/contract_sale_generation/__manifest__.py
index 072fa461..285b3013 100644
--- a/contract_sale_generation/__manifest__.py
+++ b/contract_sale_generation/__manifest__.py
@@ -6,7 +6,7 @@
{
'name': 'Contracts Management - Recurring Sales',
- 'version': '10.0.1.0.0',
+ 'version': '10.0.2.0.0',
'category': 'Contract Management',
'license': 'AGPL-3',
'author': "PESOL, "
@@ -17,6 +17,7 @@
'views/account_analytic_account_view.xml',
'views/account_analytic_contract_view.xml',
'views/sale_view.xml',
+ 'data/contract_cron.xml',
],
'installable': True,
}
diff --git a/contract_sale_generation/data/contract_cron.xml b/contract_sale_generation/data/contract_cron.xml
new file mode 100644
index 00000000..d4d6f8fd
--- /dev/null
+++ b/contract_sale_generation/data/contract_cron.xml
@@ -0,0 +1,14 @@
+
+
+
+
+ Generate Recurring sales from Contracts
+ 1
+ days
+ -1
+
+
+
+
+
+
diff --git a/contract_sale_generation/models/account_analytic_account.py b/contract_sale_generation/models/account_analytic_account.py
index b29b4afd..1079d3b8 100644
--- a/contract_sale_generation/models/account_analytic_account.py
+++ b/contract_sale_generation/models/account_analytic_account.py
@@ -8,7 +8,7 @@
# Copyright 2017 Angel Moya
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
-from odoo import api, models
+from odoo import api, models, fields
from odoo.exceptions import ValidationError
from odoo.tools.translate import _
@@ -70,6 +70,12 @@ class AccountAnalyticAccount(models.Model):
if self.type == 'invoice':
return super(AccountAnalyticAccount, self)._create_invoice()
else:
+ return self.env['account.invoice']
+
+ @api.multi
+ def _create_sale(self):
+ self.ensure_one()
+ if self.type == 'sale':
sale_vals = self._prepare_sale()
sale = self.env['sale.order'].create(sale_vals)
for line in self.recurring_invoice_line_ids:
@@ -78,3 +84,48 @@ class AccountAnalyticAccount(models.Model):
if self.sale_autoconfirm:
sale.action_confirm()
return sale
+ else:
+ return self.env['sale.order']
+
+ @api.multi
+ def recurring_create_sale(self):
+ """
+ Create sales from contracts
+ :return: sales created
+ """
+ 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,
+ })
+ # Re-read contract with correct company
+ sales |= contract.with_context(ctx)._create_sale()
+ contract.write({
+ 'recurring_next_date': new_date.strftime('%Y-%m-%d')
+ })
+ return sales
+
+ @api.model
+ def cron_recurring_create_sale(self):
+ today = fields.Date.today()
+ contracts = self.search([
+ ('recurring_invoices', '=', True),
+ ('recurring_next_date', '<=', today),
+ '|',
+ ('date_end', '=', False),
+ ('date_end', '>=', today),
+ ])
+ return contracts.recurring_create_sale()
diff --git a/contract_sale_generation/tests/test_contract_sale.py b/contract_sale_generation/tests/test_contract_sale.py
index 02aacac5..88c7e6d3 100644
--- a/contract_sale_generation/tests/test_contract_sale.py
+++ b/contract_sale_generation/tests/test_contract_sale.py
@@ -60,10 +60,10 @@ class TestContractSale(TransactionCase):
self.contract.partner_id = False
with self.assertRaises(ValidationError):
- self.contract.recurring_create_invoice()
+ self.contract.recurring_create_sale()
self.contract.partner_id = self.partner.id
- self.contract.recurring_create_invoice()
+ self.contract.recurring_create_sale()
self.sale_monthly = self.env['sale.order'].search(
[('project_id', '=', self.contract.id),
('state', '=', 'draft')])
@@ -84,10 +84,10 @@ class TestContractSale(TransactionCase):
self.contract.partner_id = False
with self.assertRaises(ValidationError):
- self.contract.recurring_create_invoice()
+ self.contract.recurring_create_sale()
self.contract.partner_id = self.partner.id
- self.contract.recurring_create_invoice()
+ self.contract.recurring_create_sale()
self.sale_monthly = self.env['sale.order'].search(
[('project_id', '=', self.contract.id),
('state', '=', 'sale')])