eiru_account_interest.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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)]
  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. expiredDays = dateServer - self._convert_str_to_datetime(line.date_maturity)
  110. if (line.date_interest):
  111. expiredDays = dateServer - self._convert_str_to_datetime(line.date_interest)
  112. if ((line.amount_residual <= 0) or (expiredDays.days <= 0)):
  113. continue
  114. ''' verify account.interest '''
  115. accountInterest = self.eiru_create_account_interest(accountInvoice.id)
  116. if (not accountInterest):
  117. return {'state': False}
  118. ''' verify account.interest.line '''
  119. interrstLine = self.eiru_create_account_interest_lines(accountInvoice.id, accountInterest.id, line.id, expiredDays.days)
  120. if (not interrstLine):
  121. return {'state': False}
  122. return True
  123. '''
  124. Create Account Interest
  125. '''
  126. def eiru_create_account_interest(self, invoiceId):
  127. _logger.info('Verify account interest')
  128. accountInvoice = self.env['account.invoice'].browse(invoiceId)
  129. domain = [('invoice_id', '=', accountInvoice.id)]
  130. accountInterest = self.env['account.interest'].search(domain)
  131. dateServer = self.get_date()
  132. if (not accountInterest):
  133. accountInterest = self.env['account.interest'].create({
  134. 'invoice_id': accountInvoice.id,
  135. 'customer_id': accountInvoice.commercial_partner_id.id if(accountInvoice.commercial_partner_id) else accountInvoice.partner_id.id,
  136. 'date': dateServer,
  137. 'currency_id': accountInvoice.currency_id.id,
  138. 'reference': accountInvoice.number,
  139. })
  140. return accountInterest
  141. '''
  142. create / write
  143. '''
  144. def eiru_create_account_interest_lines(self, invoiceId, interestID, moveId, expiredDays):
  145. _logger.info('Verify account interest Line')
  146. decimal_precision = self.env['decimal.precision'].precision_get('Account')
  147. accountInvoice = self.env['account.invoice'].browse(invoiceId)
  148. accountInterest = self.env['account.interest'].browse(interestID)
  149. moveLine = self.env['account.move.line'].browse(moveId)
  150. interestConfig = self.env['account.interest.config'].search([('active', '=', True)])
  151. domain = [('interest_id.id', '=', accountInterest.id),('move_line_id.id', '=',moveLine.id),('state', '=', 'open')]
  152. interestLine = self.env['account.interest.line'].search(domain)
  153. decimalPlaces = accountInvoice.currency_id.decimal_places
  154. if (not decimalPlaces):
  155. decimalPlaces = 0
  156. amount = (moveLine.amount_currency or moveLine.debit or 0.0)
  157. amount_residual = (moveLine.amount_residual_currency or moveLine.amount_residual or 0.0)
  158. line = {
  159. 'move_line_id': moveLine.id,
  160. 'amount': round(amount, decimalPlaces),
  161. 'date_maturity': moveLine.date_maturity,
  162. 'expired_days': expiredDays,
  163. 'amount_residual': round(amount_residual,decimalPlaces),
  164. 'amount_interest': round((expiredDays * (amount_residual * (interestConfig.interest_rate / 100))),decimalPlaces),
  165. 'interest_id': accountInterest.id,
  166. }
  167. if (not interestLine):
  168. return interestLine.create(line)
  169. return interestLine.write(line)
  170. ''' ____ _ ___ _
  171. / ___|_ __ ___ __ _| |_ ___ |_ _|_ ____ _____ (_) ___ ___
  172. | | | '__/ _ \/ _` | __/ _ \ | || '_ \ \ / / _ \| |/ __/ _ \
  173. | |___| | | __/ (_| | || __/ | || | | \ V / (_) | | (_| __/
  174. \____|_| \___|\__,_|\__\___| |___|_| |_|\_/ \___/|_|\___\___|
  175. '''
  176. @api.model
  177. def create_account_invoice_interest(self, id, interestLine):
  178. _logger.info('Create invoice interest')
  179. accountInterest = self.env['account.interest'].browse(id)
  180. if (not accountInterest):
  181. return {
  182. 'state': False,
  183. 'message': 'Error en obtener el registro de interés'
  184. }
  185. interestConfig = self.env['account.interest.config'].search([('active', '=', True)])
  186. if (not interestConfig):
  187. return {
  188. 'state': False,
  189. 'message': 'Error en obtener Configuración de interés'
  190. }
  191. decimal_precision = self.env['decimal.precision'].precision_get('Account')
  192. dateServer = self.get_date()
  193. invoice_line = []
  194. for line in interestLine:
  195. accountInterestLine = self.env['account.interest.line'].browse(line['id'])
  196. descrip= "Deuda vencida al %s, días atrasados %d" % (accountInterestLine.date_maturity, accountInterestLine.expired_days)
  197. if (line['discount'] > 0):
  198. descrip = descrip+" Descuento %d" % (line['discount'])
  199. if (accountInterestLine):
  200. invoice_line.append([0,False, {
  201. 'name': descrip,
  202. 'account_id': interestConfig.line_account_id.id,
  203. 'quantity': 1,
  204. 'price_unit': round((accountInterestLine.amount_interest - line['discount']),decimal_precision),
  205. 'price_subtotal': round((accountInterestLine.amount_interest - line['discount']),decimal_precision),
  206. 'partner_id': accountInterest.customer_id.id,
  207. 'invoice_line_tax_id': [(6, 0,[x.id for x in interestConfig.line_tax_id])],
  208. }])
  209. invoice = {
  210. 'partner_id': accountInterest.customer_id.id,
  211. 'currency_id': accountInterest.currency_id.id,
  212. 'date_invoice': dateServer,
  213. 'journal_id': interestConfig.invoice_journal_id.id,
  214. 'account_id': interestConfig.invoice_account_id.id,
  215. 'invoice_line': invoice_line,
  216. 'origin': '%s/%s' % (accountInterest.name, accountInterest.reference),
  217. 'is_interest': True
  218. }
  219. ''' Create / open /invoice '''
  220. accountInvoice = self.env['account.invoice'].create(invoice)
  221. accountInvoice.signal_workflow('invoice_open')
  222. for line in interestLine:
  223. accountInterestLine = self.env['account.interest.line'].browse(line['id'])
  224. if (accountInterestLine):
  225. moveLine = self.env['account.move.line'].browse(accountInterestLine.move_line_id.id)
  226. if (moveLine):
  227. moveLine.write({'date_interest': dateServer})
  228. accountInterestLine.write({
  229. 'invoice': accountInvoice.id,
  230. 'reference': accountInvoice.number,
  231. 'state': 'invoiced',
  232. 'amount_dicount': round(line['discount'],decimal_precision)
  233. })
  234. return {
  235. 'state': True,
  236. 'message': 'Operación exitosa'
  237. }