account_invoice.py 6.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # -*- coding: utf-8 -*-
  2. from __future__ import division
  3. from datetime import datetime
  4. from datetime import date, timedelta
  5. from dateutil.relativedelta import relativedelta
  6. import openerp.addons.decimal_precision as dp
  7. from openerp import models, fields, api
  8. from openerp.tools.float_utils import float_round as round, float_compare
  9. from openerp.osv import fields, osv
  10. from openerp.tools.translate import _
  11. class account_invoice(models.Model):
  12. _name = 'account.invoice'
  13. _inherit = 'account.invoice'
  14. def update_interest(self,cr,uid,value,context=None):
  15. date_ref = datetime.today().date()
  16. fecha = None
  17. interes = None
  18. # account invoice
  19. model_invoice_ids = self.pool.get('account.invoice').search(cr, uid,[('id', '=', value)], context=context)
  20. datos_invoice = self.pool.get('account.invoice').browse(cr, uid, model_invoice_ids, context=context)
  21. for account_in in datos_invoice:
  22. account_residual = account_in.residual
  23. account_total = account_in.amount_total
  24. # print("Calculando interest**********************************************")
  25. # account paymnet term line
  26. model_payment_term_line = self.pool.get('account.payment.term.line').search(cr, uid,[('payment_id', 'in', account_in.payment_term.ids)], context=context)
  27. datos_payment_term_line = self.pool.get('account.payment.term.line').browse(cr, uid, model_payment_term_line, context=context)
  28. for payment_line in datos_payment_term_line:
  29. interes = payment_line.interest_rate
  30. if interes <= 0:
  31. raise osv.except_osv('Advertencia','No fue Definida, la Condiciones de Pago')
  32. return
  33. # prodcuto prodcuto
  34. model_prodcuto = self.pool.get('product.product').search(cr, uid,[('name', '=', 'INTEREST')], context=context)
  35. datos_producto = self.pool.get('product.product').browse(cr, uid, model_prodcuto, context=context)
  36. # print("Producto")
  37. print(model_prodcuto)
  38. for productos_obj in datos_producto:
  39. print("producto")
  40. print(productos_obj.name_template)
  41. # journal
  42. model_journal = self.pool.get('account.journal').search(cr, uid,[('type', '=', 'sale')], context=context)
  43. datos_journal = self.pool.get('account.journal').browse(cr, uid, model_journal, context=context)
  44. # print(datos_journal)
  45. # account move line
  46. model_move_line = self.pool.get('account.move.line').search(cr, uid,[('reconcile_ref', '=', None),
  47. ('partner_id', 'in', account_in.partner_id.ids),
  48. ('ref', '=', account_in.reference),
  49. ('date_maturity', '<', date_ref)], context=context)
  50. datos_move_line = self.pool.get('account.move.line').browse(cr, uid, model_move_line, context=context)
  51. # print(datos_move_line)
  52. for move_line in datos_move_line:
  53. mesciere=move_line.month_interest_calculate
  54. datos_invoice = self.pool.get('account.invoice').browse(cr, uid, model_invoice_ids, context=context)
  55. for account_in in datos_invoice:
  56. account_residual = account_in.residual
  57. account_total = account_in.amount_total
  58. account_subtot = account_in.amount_untaxed
  59. ammount_interes = ((account_in.residual * interes)/100)
  60. model_move_line2 = self.pool.get('account.move.line').search(cr, uid,[('reconcile_ref', '=', None),
  61. ('partner_id', '=', account_in.partner_id.ids),
  62. ('ref', '=', account_in.reference),
  63. ('date_maturity', '<', date_ref),
  64. ('id', '=', move_line.id)], context=context)
  65. datos_move_line2 = self.pool.get('account.move.line').browse(cr, uid, model_move_line2, context=context)
  66. for moveline2 in datos_move_line2:
  67. fechavencimeito =moveline2.date_maturity
  68. if ((date_ref.month) > moveline2.month_interest_calculate):
  69. new_id=self.pool.get('account.invoice.line').create(cr, uid, { 'origin' : account_in.reference, 'create_uid' : '1', 'uos_id' : '1', 'account_id' : '113',
  70. 'sequence' : '10', 'invoice_id' : account_in.id, 'price_unit' : ammount_interes,
  71. 'price_subtotal' : ammount_interes, 'company_id' : '1', 'write_uid' : '1',
  72. 'discount' : '0.00', 'product_id' : productos_obj.id,
  73. 'partner_id' : moveline2.partner_id['id'], 'quantity' : '1',
  74. 'name' : 'Incumplimiento de pago / '+str(moveline2.date_maturity) }, context=context)
  75. self.pool.get('account.interest.line').create(cr, uid, {'origin' : account_in.reference, 'invoice_id' : account_in.id, 'price_subtotal' :ammount_interes,
  76. 'invoice_line_id' : new_id, 'name_product' : 'Incumplimiento de pago / '+str(moveline2.date_maturity),
  77. 'date' :date_ref, 'quantity' : '1', 'state' : True, 'interes_invoice' : account_in.id,
  78. 'move_line_id' : moveline2.id }, context=context)
  79. datos_invoice.write({'residual' : (account_residual+ammount_interes),
  80. 'amount_total' : (account_total+ammount_interes), 'amount_untaxed' : (account_subtot+ammount_interes)})
  81. datos_move_line2.write({'month_interest_calculate' : (date_ref.month)})
  82. datos_journal.write({'entry_posted' : True})
  83. datos_move_line2.write({'debit' : (moveline2.debit + ammount_interes)})
  84. datos_journal.write({'entry_posted' : False})
  85. # validar move_line
  86. model_move_line = self.pool.get('account.move.line').search(cr, uid,[('ref', '=', account_in.reference)], context=context)
  87. datos_move_line2 = self.pool.get('account.move.line').browse(cr, uid, model_move_line, context=context)
  88. for line_move in datos_move_line2:
  89. datos_move_line2.write({'state' : 'valid'})