contract.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # -*- coding: utf-8 -*-
  2. from openerp import models, fields, api
  3. from dateutil.relativedelta import relativedelta
  4. import datetime
  5. import time
  6. class AccountAnalyticAccountDate(models.Model):
  7. _inherit = 'account.analytic.account'
  8. date_invoice = fields.Date('Fecha de la factura')
  9. @api.model
  10. def _prepare_invoice_data(self, contract):
  11. date_vals = super(AccountAnalyticAccountDate, self).\
  12. _prepare_invoice_data(
  13. contract)
  14. if contract.date_invoice:
  15. date_vals['date_invoice'] = contract.date_invoice
  16. return date_vals
  17. def _recurring_create_invoice(self, cr, uid, ids, automatic=False, context=None):
  18. context = context or {}
  19. invoice_ids = []
  20. current_date = time.strftime('%Y-%m-%d')
  21. if ids:
  22. contract_ids = ids
  23. else:
  24. contract_ids = self.search(cr, uid, [('recurring_next_date','<=', current_date), ('state','=', 'open'), ('recurring_invoices','=', True), ('type', '=', 'contract')])
  25. if contract_ids:
  26. 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),))
  27. for company_id, ids in cr.fetchall():
  28. context_contract = dict(context, company_id=company_id, force_company=company_id)
  29. for contract in self.browse(cr, uid, ids, context=context_contract):
  30. try:
  31. invoice_values = self._prepare_invoice(cr, uid, contract, context=context_contract)
  32. invoice_ids.append(self.pool['account.invoice'].create(cr, uid, invoice_values, context=context))
  33. next_date = datetime.datetime.strptime(contract.recurring_next_date or current_date, "%Y-%m-%d")
  34. next_invoice_date = datetime.datetime.strptime(contract.date_invoice or current_date, "%Y-%m-%d")
  35. interval = contract.recurring_interval
  36. if contract.recurring_rule_type == 'daily':
  37. new_date = next_date+relativedelta(days=+interval)
  38. elif contract.recurring_rule_type == 'weekly':
  39. new_date = next_date+relativedelta(weeks=+interval)
  40. elif contract.recurring_rule_type == 'monthly':
  41. new_date = next_date+relativedelta(months=+interval)
  42. else:
  43. new_date = next_date+relativedelta(years=+interval)
  44. #calcula new invoice date
  45. if contract.recurring_rule_type == 'daily':
  46. new_invoice_date = next_invoice_date+relativedelta(days=+interval)
  47. elif contract.recurring_rule_type == 'weekly':
  48. new_invoice_date = next_invoice_date+relativedelta(weeks=+interval)
  49. elif contract.recurring_rule_type == 'monthly':
  50. new_invoice_date = next_invoice_date+relativedelta(months=+interval)
  51. else:
  52. new_invoice_date = next_invoice_date+relativedelta(years=+interval)
  53. #recurring_next_date
  54. self.write(cr, uid, [contract.id], {'recurring_next_date': new_date.strftime('%Y-%m-%d')}, context=context)
  55. #date invoice
  56. self.write(cr, uid, [contract.id], {'date_invoice': new_invoice_date.strftime('%Y-%m-%d')}, context=context)
  57. if automatic:
  58. cr.commit()
  59. except Exception:
  60. if automatic:
  61. cr.rollback()
  62. _logger.exception('Fail to create recurring invoice for contract %s', contract.code)
  63. else:
  64. raise
  65. return invoice_ids