eiru_account_interest.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. ____ _ ___ _ _
  58. / ___| ___ _ __ ___ _ __ __ _| |_ ___ |_ _|_ __ | |_ ___ _ __ ___ ___| |_
  59. | | _ / _ \ '_ \ / _ \ '__/ _` | __/ _ \ | || '_ \| __/ _ \ '__/ _ \/ __| __|
  60. | |_| | __/ | | | __/ | | (_| | || __/ | || | | | || __/ | | __/\__ \ |_
  61. \____|\___|_| |_|\___|_| \__,_|\__\___| |___|_| |_|\__\___|_| \___||___/\__|
  62. '''
  63. @api.model
  64. def get_account_invoice_interest(self, invoice=None, partner=None):
  65. _logger.info('Verify invoice')
  66. domain = [('state', '=', 'open'),('is_interest', '=', False)]
  67. if (invoice):
  68. domain.append(('id', '=', invoice))
  69. if (partner):
  70. domain.append(('partner_id.id', '=', partner))
  71. accountInvoice = self.env['account.invoice'].search(domain)
  72. for invoice in accountInvoice:
  73. self.get_move_line_in_invoice(invoice.id)
  74. return True
  75. '''
  76. Get move Line.
  77. '''
  78. def get_move_line_in_invoice(self, invoice):
  79. _logger.info('Get Move Line')
  80. accountInvoice = self.env['account.invoice'].browse(invoice)
  81. dateServer = self._convert_str_to_datetime(self.get_date())
  82. domain = [('state','!=','draft'), ('credit','<=', 0), ('partner_id','=',accountInvoice.partner_id.id),('invoice','=',accountInvoice.id)]
  83. for line in self.env['account.move.line'].search(domain):
  84. expiredDays = dateServer - self._convert_str_to_datetime(line.date_maturity)
  85. if (line.date_interest):
  86. expiredDays = dateServer - self._convert_str_to_datetime(line.date_interest)
  87. if ((line.amount_residual <= 0) or (expiredDays.days <= 0)):
  88. continue
  89. ''' verify account.interest '''
  90. accountInterest = self.eiru_create_account_interest(accountInvoice.id)
  91. if (not accountInterest):
  92. return {'state': False}
  93. ''' verify account.interest.line '''
  94. interrstLine = self.eiru_create_account_interest_lines(accountInvoice.id, accountInterest.id, line.id, expiredDays.days)
  95. if (not interrstLine):
  96. return {'state': False}
  97. return True
  98. '''
  99. Create Account Interest
  100. '''
  101. def eiru_create_account_interest(self, invoiceId):
  102. _logger.info('Verify account interest')
  103. accountInvoice = self.env['account.invoice'].browse(invoiceId)
  104. domain = [('invoice_id', '=', accountInvoice.id),('customer_id.id', '=', accountInvoice.partner_id.id)]
  105. accountInterest = self.env['account.interest'].search(domain)
  106. dateServer = self.get_date()
  107. if (not accountInterest):
  108. accountInterest = self.env['account.interest'].create({
  109. 'invoice_id': accountInvoice.id,
  110. 'customer_id': accountInvoice.partner_id.id,
  111. 'date': dateServer,
  112. 'currency_id': accountInvoice.currency_id.id,
  113. 'reference': accountInvoice.number,
  114. })
  115. return accountInterest
  116. '''
  117. create / write
  118. '''
  119. def eiru_create_account_interest_lines(self, invoiceId, interestID, moveId, expiredDays):
  120. _logger.info('Verify account interest Line')
  121. decimal_precision = self.env['decimal.precision'].precision_get('Account')
  122. accountInvoice = self.env['account.invoice'].browse(invoiceId)
  123. accountInterest = self.env['account.interest'].browse(interestID)
  124. moveLine = self.env['account.move.line'].browse(moveId)
  125. interestConfig = self.env['account.interest.config'].search([('active', '=', True)])
  126. domain = [('interest_id.id', '=', accountInterest.id),('move_line_id.id', '=',moveLine.id),('state', '=', 'open')]
  127. interestLine = self.env['account.interest.line'].search(domain)
  128. decimalPlaces = accountInvoice.currency_id.decimal_places
  129. if (not decimalPlaces):
  130. decimalPlaces = 0
  131. amount = (moveLine.amount_currency or moveLine.debit or 0.0)
  132. amount_residual = (moveLine.amount_residual_currency or moveLine.amount_residual or 0.0)
  133. line = {
  134. 'move_line_id': moveLine.id,
  135. 'amount': round(amount, decimalPlaces),
  136. 'date_maturity': moveLine.date_maturity,
  137. 'expired_days': expiredDays,
  138. 'amount_residual': round(amount_residual,decimalPlaces),
  139. 'amount_interest': round((expiredDays * (amount_residual * (interestConfig.interest_rate / 100))),decimalPlaces),
  140. 'interest_id': accountInterest.id,
  141. }
  142. if (not interestLine):
  143. return interestLine.create(line)
  144. return interestLine.write(line)
  145. ''' ____ _ ___ _
  146. / ___|_ __ ___ __ _| |_ ___ |_ _|_ ____ _____ (_) ___ ___
  147. | | | '__/ _ \/ _` | __/ _ \ | || '_ \ \ / / _ \| |/ __/ _ \
  148. | |___| | | __/ (_| | || __/ | || | | \ V / (_) | | (_| __/
  149. \____|_| \___|\__,_|\__\___| |___|_| |_|\_/ \___/|_|\___\___|
  150. '''
  151. @api.model
  152. def create_account_invoice_interest(self, id, interestLine):
  153. _logger.info('Create invoice interest')
  154. accountInterest = self.env['account.interest'].browse(id)
  155. if (not accountInterest):
  156. return {
  157. 'state': False,
  158. 'message': 'Error en obtener el registro de interés'
  159. }
  160. interestConfig = self.env['account.interest.config'].search([('active', '=', True)])
  161. if (not interestConfig):
  162. return {
  163. 'state': False,
  164. 'message': 'Error en obtener Configuración de interés'
  165. }
  166. decimal_precision = self.env['decimal.precision'].precision_get('Account')
  167. dateServer = self.get_date()
  168. invoice_line = []
  169. for line in interestLine:
  170. accountInterestLine = self.env['account.interest.line'].browse(line['id'])
  171. descrip= "Deuda vencida al %s, días atrasados %d" % (accountInterestLine.date_maturity, accountInterestLine.expired_days)
  172. if (line['discount'] > 0):
  173. descrip = descrip+" Descuento %d" % (line['discount'])
  174. if (accountInterestLine):
  175. invoice_line.append([0,False, {
  176. 'name': descrip,
  177. 'account_id': interestConfig.line_account_id.id,
  178. 'quantity': 1,
  179. 'price_unit': round((accountInterestLine.amount_interest - line['discount']),decimal_precision),
  180. 'price_subtotal': round((accountInterestLine.amount_interest - line['discount']),decimal_precision),
  181. 'partner_id': accountInterest.customer_id.id,
  182. 'invoice_line_tax_id': [(6, 0,[x.id for x in interestConfig.line_tax_id])],
  183. }])
  184. invoice = {
  185. 'partner_id': accountInterest.customer_id.id,
  186. 'currency_id': accountInterest.currency_id.id,
  187. 'date_invoice': dateServer,
  188. 'journal_id': interestConfig.invoice_journal_id.id,
  189. 'account_id': interestConfig.invoice_account_id.id,
  190. 'invoice_line': invoice_line,
  191. 'origin': '%s/%s' % (accountInterest.name, accountInterest.reference),
  192. 'is_interest': True
  193. }
  194. ''' Create / open /invoice '''
  195. accountInvoice = self.env['account.invoice'].create(invoice)
  196. accountInvoice.signal_workflow('invoice_open')
  197. for line in interestLine:
  198. accountInterestLine = self.env['account.interest.line'].browse(line['id'])
  199. if (accountInterestLine):
  200. moveLine = self.env['account.move.line'].browse(accountInterestLine.move_line_id.id)
  201. if (moveLine):
  202. moveLine.write({'date_interest': dateServer})
  203. accountInterestLine.write({
  204. 'invoice': accountInvoice.id,
  205. 'reference': accountInvoice.number,
  206. 'state': 'invoiced'
  207. })
  208. return {
  209. 'state': True,
  210. 'message': 'Operación exitosa'
  211. }