You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
2.0 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 Binovo IT Human Project SL <elacunza@binovo.es>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from openerp import models, fields, api
  5. from dateutil.relativedelta import relativedelta
  6. import datetime
  7. import time
  8. class AccountAnalyticAccount(models.Model):
  9. _inherit = 'account.analytic.account'
  10. recurring_rule_type = fields.Selection(
  11. selection_add=[('monthlylastday', 'Month(s) - Last day')])
  12. @api.multi
  13. def _recurring_create_invoice(self, automatic=False):
  14. invoice_ids = []
  15. current_date = time.strftime('%Y-%m-%d')
  16. if self.ids:
  17. contracts = self
  18. else:
  19. contracts = self.search(
  20. [('recurring_next_date', '<=', current_date),
  21. ('state', '=', 'open'),
  22. ('recurring_invoices', '=', True),
  23. ('type', '=', 'contract')])
  24. for contract in contracts:
  25. is_monthlylastday = False
  26. orig_next_date = contract.recurring_next_date
  27. if contract.recurring_rule_type == 'monthlylastday':
  28. is_monthlylastday = True
  29. contract.recurring_rule_type = 'monthly'
  30. try:
  31. invoice_ids.append(super(AccountAnalyticAccount, contract)
  32. ._recurring_create_invoice(automatic))
  33. finally:
  34. if is_monthlylastday:
  35. contract.recurring_rule_type = 'monthlylastday'
  36. # Note: recurring_next_day has been already incremented by super if
  37. # invoice was created. Adjust it to month's last day
  38. if is_monthlylastday \
  39. and contract.recurring_next_date != orig_next_date:
  40. next_date = datetime.datetime.strptime(
  41. contract.recurring_next_date,
  42. "%Y-%m-%d")
  43. new_date = next_date + relativedelta(day=31)
  44. contract.write(
  45. {'recurring_next_date': new_date.strftime('%Y-%m-%d')})
  46. return invoice_ids