eiru_account_interest.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. # -*- coding: utf-8 -*-
  2. from openerp import models, fields, tools, api, _
  3. ## date
  4. from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT, DEFAULT_SERVER_DATE_FORMAT
  5. from pytz import timezone
  6. from datetime import datetime,timedelta
  7. ## logging
  8. import logging
  9. _logger = logging.getLogger(__name__)
  10. class EiruAccountInterest(models.Model):
  11. _inherit = 'account.interest'
  12. '''
  13. timezone
  14. '''
  15. def get_timezone(self):
  16. return timezone(self._context.get('tz') or self.env.user.tz)
  17. '''
  18. Datetime
  19. '''
  20. def get_datetime(self):
  21. return datetime.now(self.get_timezone()).strftime(DEFAULT_SERVER_DATETIME_FORMAT)
  22. '''
  23. Date
  24. '''
  25. def get_date(self):
  26. return datetime.now(self.get_timezone()).strftime(DEFAULT_SERVER_DATE_FORMAT)
  27. '''
  28. Convert Str --> Datetime
  29. '''
  30. def _convert_str_to_datetime(self, date):
  31. return datetime.strptime(date,DEFAULT_SERVER_DATE_FORMAT)
  32. '''
  33. GET Account Interest
  34. '''
  35. @api.model
  36. def get_account_interest(self, id):
  37. return [{
  38. 'id': interest.id,
  39. 'name': interest.name,
  40. 'state': interest.state,
  41. 'invoiceId': interest.invoice_id.id,
  42. 'customer': [{
  43. 'id': partner.id,
  44. 'name': partner.name,
  45. } for partner in self.env['res.partner'].browse(interest.customer_id.id)],
  46. 'currency': [{
  47. 'id': currency.id,
  48. 'symbol': currency.symbol,
  49. 'rate': currency.rate,
  50. 'thousandsSeparator': currency.thousands_separator,
  51. 'decimalSeparator': currency.decimal_separator,
  52. 'decimalPlaces': currency.decimal_places,
  53. 'position': currency.position,
  54. } for currency in self.env['res.currency'].browse(interest.currency_id.id)]
  55. }for interest in self.env['account.interest'].browse(id)]
  56. '''
  57. get Account Invoice
  58. '''
  59. @api.model
  60. def get_account_invoice_interest(self, invoice=None, partner=None):
  61. _logger.info('Verify invoice')
  62. domain = [('state', '=', 'open')]
  63. if (invoice):
  64. domain.append(('id', '=', invoice))
  65. if (partner):
  66. domain.append(('partner_id.id', '=', partner))
  67. accountInvoice = self.env['account.invoice'].search(domain)
  68. for invoice in accountInvoice:
  69. self.get_move_line_in_invoice(invoice.id)
  70. '''
  71. Get move Line.
  72. '''
  73. def get_move_line_in_invoice(self, invoice):
  74. _logger.info('Get Move Line')
  75. accountInvoice = self.env['account.invoice'].browse(invoice)
  76. dateServer = self._convert_str_to_datetime(self.get_date())
  77. domain = [('state','!=','draft'), ('credit','<=', 0), ('partner_id','=',accountInvoice.partner_id.id),('invoice','=',accountInvoice.id)]
  78. for line in self.env['account.move.line'].search(domain):
  79. expiredDays = dateServer - self._convert_str_to_datetime(line.date_maturity)
  80. if ((line.amount_residual <= 0) or (expiredDays.days <= 0)):
  81. continue
  82. ''' verify account.interest '''
  83. accountInterest = self.eiru_create_account_interest(accountInvoice.id)
  84. ''' verify account.interest.line '''
  85. interrstLine = self.eiru_create_account_interest_lines(accountInvoice.id, accountInterest.id, line.id, expiredDays.days)
  86. '''
  87. Create Account Interest
  88. '''
  89. def eiru_create_account_interest(self, invoiceId):
  90. _logger.info('Verify account interest')
  91. accountInvoice = self.env['account.invoice'].browse(invoiceId)
  92. domain = [('invoice_id', '=', accountInvoice.id),('customer_id.id', '=', accountInvoice.partner_id.id)]
  93. accountInterest = self.env['account.interest'].search(domain)
  94. dateServer = self.get_date()
  95. if (not accountInterest):
  96. accountInterest = self.env['account.interest'].create({
  97. 'invoice_id': accountInvoice.id,
  98. 'customer_id': accountInvoice.partner_id.id,
  99. 'date': dateServer,
  100. 'currency_id': accountInvoice.currency_id.id,
  101. 'reference': accountInvoice.number,
  102. })
  103. return accountInterest
  104. '''
  105. create / write
  106. '''
  107. def eiru_create_account_interest_lines(self, invoiceId, interestID, moveId, expiredDays):
  108. _logger.info('Verify account interest Line')
  109. decimal_precision = self.env['decimal.precision'].precision_get('Account')
  110. accountInvoice = self.env['account.invoice'].browse(invoiceId)
  111. accountInterest = self.env['account.interest'].browse(interestID)
  112. moveLine = self.env['account.move.line'].browse(moveId)
  113. interestConfig = self.env['account.interest.config'].search([('active', '=', True)])
  114. interestLine = self.env['account.interest.line'].search([('interest_id.id', '=', accountInterest.id),('move_line_id.id', '=',moveLine.id)])
  115. decimalPlaces = accountInvoice.currency_id.decimal_places
  116. if (not decimalPlaces):
  117. decimalPlaces = 0
  118. amount = (moveLine.amount_currency or moveLine.debit or 0.0)
  119. amount_residual = (moveLine.amount_residual_currency or moveLine.amount_residual or 0.0)
  120. line = {
  121. 'move_line_id': moveLine.id,
  122. 'amount': round(amount, decimalPlaces),
  123. 'date_maturity': moveLine.date_maturity,
  124. 'expired_days': expiredDays,
  125. 'amount_residual': round(amount_residual,decimalPlaces),
  126. 'amount_interest': round((expiredDays * (amount_residual * (interestConfig.interest_rate / 100))),decimalPlaces),
  127. 'interest_id': accountInterest.id
  128. }
  129. if (not interestLine):
  130. return interestLine.create(line)
  131. return interestLine.write(line)
  132. ''' ____ _ ___ _
  133. / ___|_ __ ___ __ _| |_ ___ |_ _|_ ____ _____ (_) ___ ___
  134. | | | '__/ _ \/ _` | __/ _ \ | || '_ \ \ / / _ \| |/ __/ _ \
  135. | |___| | | __/ (_| | || __/ | || | | \ V / (_) | | (_| __/
  136. \____|_| \___|\__,_|\__\___| |___|_| |_|\_/ \___/|_|\___\___|
  137. '''
  138. @api.model
  139. def create_account_invoice_interest(self, id, interestLine):
  140. _logger.info('Create invoice interest')
  141. accountInterest = self.env['account.interest'].browse(id)
  142. if (not accountInterest):
  143. return {
  144. 'state': False,
  145. 'message': 'Error en obtener el registro de interés'
  146. }
  147. interestConfig = self.env['account.interest.config'].search([('active', '=', True)])
  148. if (not interestConfig):
  149. return {
  150. 'state': False,
  151. 'message': 'Error en obtener Configuración de interés'
  152. }
  153. decimal_precision = self.env['decimal.precision'].precision_get('Account')
  154. dateServer = self.get_date()
  155. invoice_line = []
  156. for line in interestLine:
  157. accountInterestLine = self.env['account.interest.line'].browse(line['id'])
  158. descrip= "Deuda vencida al %s, días atrasados %d" % (accountInterestLine.date_maturity, accountInterestLine.expired_days)
  159. if (line['discount'] > 0):
  160. descrip = descrip+" Descuento %d" % (line['discount'])
  161. if (accountInterestLine):
  162. invoice_line.append([0,False, {
  163. 'name': descrip,
  164. 'account_id': interestConfig.line_account_id.id,
  165. 'quantity': 1,
  166. 'price_unit': round((accountInterestLine.amount_interest - line['discount']),decimal_precision),
  167. 'price_subtotal': round((accountInterestLine.amount_interest - line['discount']),decimal_precision),
  168. 'partner_id': accountInterest.customer_id.id,
  169. 'invoice_line_tax_id': [(6, 0,[x.id for x in interestConfig.line_tax_id])],
  170. }])
  171. invoice = {
  172. 'partner_id': accountInterest.customer_id.id,
  173. 'currency_id': accountInterest.currency_id.id,
  174. 'date_invoice': dateServer,
  175. 'journal_id': interestConfig.invoice_journal_id.id,
  176. 'account_id': interestConfig.invoice_account_id.id,
  177. 'invoice_line': invoice_line,
  178. }
  179. accountInvoice = self.env['account.invoice'].create(invoice)
  180. accountInvoice.signal_workflow('invoice_open')
  181. for line in interestLine:
  182. accountInterestLine = self.env['account.interest.line'].browse(line['id'])
  183. if (accountInterestLine):
  184. accountInterestLine.write({
  185. 'invoice': accountInvoice.id,
  186. 'reference': accountInvoice.number,
  187. 'state': 'invoiced'
  188. })
  189. return {
  190. 'state': True,
  191. 'message': 'Operación exitosa'
  192. }