|
@@ -0,0 +1,69 @@
|
|
|
+# -*- coding: utf-8 -*-
|
|
|
+from openerp import models, fields, api
|
|
|
+from dateutil.relativedelta import relativedelta
|
|
|
+import datetime
|
|
|
+import time
|
|
|
+
|
|
|
+class AccountAnalyticAccountDate(models.Model):
|
|
|
+ _inherit = 'account.analytic.account'
|
|
|
+
|
|
|
+ date_invoice = fields.Date('Fecha de la factura')
|
|
|
+
|
|
|
+ @api.model
|
|
|
+ def _prepare_invoice_data(self, contract):
|
|
|
+ date_vals = super(AccountAnalyticAccountDate, self).\
|
|
|
+ _prepare_invoice_data(
|
|
|
+ contract)
|
|
|
+ if contract.date_invoice:
|
|
|
+ date_vals['date_invoice'] = contract.date_invoice
|
|
|
+ return date_vals
|
|
|
+
|
|
|
+ def _recurring_create_invoice(self, cr, uid, ids, automatic=False, context=None):
|
|
|
+ context = context or {}
|
|
|
+ invoice_ids = []
|
|
|
+ current_date = time.strftime('%Y-%m-%d')
|
|
|
+ if ids:
|
|
|
+ contract_ids = ids
|
|
|
+ else:
|
|
|
+ contract_ids = self.search(cr, uid, [('recurring_next_date','<=', current_date), ('state','=', 'open'), ('recurring_invoices','=', True), ('type', '=', 'contract')])
|
|
|
+ if contract_ids:
|
|
|
+ cr.execute('SELECT company_id, array_agg(id) as ids FROM account_analytic_account WHERE id IN %s GROUP BY company_id', (tuple(contract_ids),))
|
|
|
+ for company_id, ids in cr.fetchall():
|
|
|
+ context_contract = dict(context, company_id=company_id, force_company=company_id)
|
|
|
+ for contract in self.browse(cr, uid, ids, context=context_contract):
|
|
|
+ try:
|
|
|
+ invoice_values = self._prepare_invoice(cr, uid, contract, context=context_contract)
|
|
|
+ invoice_ids.append(self.pool['account.invoice'].create(cr, uid, invoice_values, context=context))
|
|
|
+ next_date = datetime.datetime.strptime(contract.recurring_next_date or current_date, "%Y-%m-%d")
|
|
|
+ next_invoice_date = datetime.datetime.strptime(contract.date_invoice or current_date, "%Y-%m-%d")
|
|
|
+ interval = contract.recurring_interval
|
|
|
+ if contract.recurring_rule_type == 'daily':
|
|
|
+ new_date = next_date+relativedelta(days=+interval)
|
|
|
+ elif contract.recurring_rule_type == 'weekly':
|
|
|
+ new_date = next_date+relativedelta(weeks=+interval)
|
|
|
+ elif contract.recurring_rule_type == 'monthly':
|
|
|
+ new_date = next_date+relativedelta(months=+interval)
|
|
|
+ else:
|
|
|
+ new_date = next_date+relativedelta(years=+interval)
|
|
|
+ #calcula new invoice date
|
|
|
+ if contract.recurring_rule_type == 'daily':
|
|
|
+ new_invoice_date = next_invoice_date+relativedelta(days=+interval)
|
|
|
+ elif contract.recurring_rule_type == 'weekly':
|
|
|
+ new_invoice_date = next_invoice_date+relativedelta(weeks=+interval)
|
|
|
+ elif contract.recurring_rule_type == 'monthly':
|
|
|
+ new_invoice_date = next_invoice_date+relativedelta(months=+interval)
|
|
|
+ else:
|
|
|
+ new_invoice_date = next_invoice_date+relativedelta(years=+interval)
|
|
|
+ #recurring_next_date
|
|
|
+ self.write(cr, uid, [contract.id], {'recurring_next_date': new_date.strftime('%Y-%m-%d')}, context=context)
|
|
|
+ #date invoice
|
|
|
+ self.write(cr, uid, [contract.id], {'date_invoice': new_invoice_date.strftime('%Y-%m-%d')}, context=context)
|
|
|
+ if automatic:
|
|
|
+ cr.commit()
|
|
|
+ except Exception:
|
|
|
+ if automatic:
|
|
|
+ cr.rollback()
|
|
|
+ _logger.exception('Fail to create recurring invoice for contract %s', contract.code)
|
|
|
+ else:
|
|
|
+ raise
|
|
|
+ return invoice_ids
|