eiru_account_interest.py 11 KB

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