account_analytic_account.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. # For copyright and license notices, see __openerp__.py file in module root
  4. # directory
  5. ##############################################################################
  6. from dateutil.relativedelta import relativedelta
  7. from datetime import datetime
  8. import logging
  9. import time
  10. from openerp import models, fields, api
  11. from openerp.addons.decimal_precision import decimal_precision as dp
  12. class account_analytic_account(models.Model):
  13. _inherit = "account.analytic.account"
  14. recurring_date_due = fields.Date("Fecha de vencimiento")
  15. recurring_day = fields.Char("Día Vencimiento")
  16. recurring_total_amount = fields.Float(
  17. 'Monto',
  18. compute='_compute_recurring_total_amount',
  19. # we dont make storable so it is compatible with contract
  20. # discount
  21. # store=True,
  22. digits=dp.get_precision('Account'),
  23. )
  24. _defaults = {
  25. 'recurring_date_due': lambda *a: time.strftime('%Y-%m-%d')
  26. }
  27. @api.one
  28. @api.depends(
  29. 'recurring_invoice_line_ids.quantity',
  30. 'recurring_invoice_line_ids.price_unit',
  31. )
  32. def _compute_recurring_total_amount(self):
  33. self.recurring_total_amount = sum(
  34. self.recurring_invoice_line_ids.mapped('price_subtotal'))
  35. @api.multi
  36. def recurring_create_invoice(self):
  37. current_date = time.strftime('%Y-%m-%d')
  38. if(self.recurring_date_due, '<=', current_date):
  39. value = super(account_analytic_account, self).recurring_create_invoice()
  40. date_due=self.recurring_date_due
  41. for each in value:
  42. invoice = self.env['account.invoice'].search([('id','=',each)])
  43. invoice.write({'date_due': date_due})
  44. self.recurring_date_due = (datetime.strptime(self.recurring_date_due,'%Y-%m-%d') + relativedelta(months=1)).strftime('%Y-%m-%d')
  45. return value
  46. else:
  47. raise Warning(_("Ya no puede generar cuotas para este contrato."))
  48. @api.model
  49. def recurring_create_invoice_giganet(self):
  50. value = super(account_analytic_account, self)._cron_recurring_create_invoice()
  51. for each in value:
  52. invoice = self.env['account.invoice'].search([('id','=',each)])
  53. contract = self.env['account.analytic.account'].search([('code','=',invoice.origin)])
  54. contract.recurring_date_due = (datetime.strptime(self.recurring_date_due,'%Y-%m-%d') + relativedelta(months=1)).strftime('%Y-%m-%d')
  55. self.env.cr.execute('update account_analytic_account set recurring_date_due = %s where id = %s', (contract.recurring_date_due, contract.id))
  56. self.env.cr.execute('update account_invoice set date_due = %s where id = %s', (date_due, invoice.id))
  57. return value