account_interest_line.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # -*- coding: utf-8 -*-
  2. from openerp import models, fields, api
  3. from openerp.exceptions import ValidationError, Warning
  4. from datetime import datetime
  5. # mapping invoice type to journal type
  6. TYPE2JOURNAL = {
  7. 'out_invoice': 'sale',
  8. 'in_invoice': 'purchase',
  9. 'out_refund': 'sale_refund',
  10. 'in_refund': 'purchase_refund',
  11. }
  12. class account_interest_line(models.Model):
  13. _name = "account.interest.line"
  14. # _inherit = "account.interest.line"
  15. origin = fields.Char(string='Source Document', help="Reference of the document that produced this invoice.")
  16. invoice_id = fields.Many2one('account.invoice', string='Invoice Reference', ondelete='cascade', index=True, required=True)
  17. invoice_line_id = fields.Many2one('account.invoice.line', string='Invoice line Reference', ondelete='cascade', index=True, required=True)
  18. date = fields.Date()
  19. name_product = fields.Text(string='Description', required=True)
  20. price_subtotal = fields.Float(string='Amount')
  21. quantity = fields.Float(string='Quantity')
  22. state = fields.Boolean()
  23. partner_id = fields.Many2one('res.partner', string='Partner')
  24. interes_invoice = fields.Many2one('account.invoice', string='Invoice Reference', ondelete='cascade', index=True, required=True)
  25. move_line_id = fields.Many2one('account.move.line', string='Seguimiento de Pago ', ondelete='cascade', index=True, required=True)
  26. @api.model
  27. def _default_journal(self):
  28. inv_type = self._context.get('type', 'out_invoice')
  29. inv_types = inv_type if isinstance(inv_type, list) else [inv_type]
  30. company_id = self._context.get('company_id', self.env.user.company_id.id)
  31. domain = [
  32. ('type', 'in', filter(None, map(TYPE2JOURNAL.get, inv_types))),
  33. ('company_id', '=', company_id),
  34. ]
  35. return self.env['account.journal'].search(domain, limit=1)
  36. journal_id = fields.Many2one('account.journal', string='Journal',
  37. required=True, readonly=True, states={'draft': [('readonly', False)]},
  38. default=_default_journal,
  39. domain="[('type', 'in', {'out_invoice': ['sale'], 'out_refund': ['sale_refund'], 'in_refund': ['purchase_refund'], 'in_invoice': ['purchase']}.get(type, [])), ('company_id', '=', company_id)]")
  40. class invoice_interes(models.Model):
  41. _inherit = "account.invoice"
  42. interes_ids = fields.One2many('account.interest.line', inverse_name="invoice_id", string="id Interes")
  43. @api.onchange('interes_ids')
  44. def onchange_update(self):
  45. for interest in self.interes_ids:
  46. if interest.state == False:
  47. interest_datos = self.env['account.interest.line'].search([('id','in', interest.ids)])
  48. print(interest_datos.price_subtotal)
  49. interest_datos.write({'state' : False})
  50. def actualizarInteres(self, cr , uid, dato, context=None):
  51. # print("*****Eliminado linea de Interes***********************************")
  52. estado=None
  53. # Obtener los datos de Interest Line (Linea de interes)
  54. interes_model = self.pool.get('account.interest.line').search(cr, uid,[('interes_invoice', 'in', dato),
  55. ('state', '=', False)],context=context)
  56. interes_datos = self.pool.get('account.interest.line').browse(cr, uid, interes_model, context=context)
  57. for interes in interes_datos:
  58. # print(interes.name_product)
  59. # Obtener los datos de invoice line (Iten de facturacion)
  60. line_model = self.pool.get('account.invoice.line').search(cr, uid, [('id', '=', interes.invoice_line_id.ids)], context=context)
  61. line_datos = self.pool.get('account.invoice.line').browse(cr, uid, line_model, context=context)
  62. for line in line_datos:
  63. # Obtener los datos de invoice para su actualizacion
  64. invoice_model = self.pool.get('account.invoice').search(cr, uid, [('id', 'in', dato)],context=context)
  65. invoice_dato = self.pool.get('account.invoice').browse(cr, uid, invoice_model, context=context)
  66. # Actualizar Invoice(facturacion)
  67. invoice_dato.write({'residual' : (invoice_dato.residual - line.price_subtotal),
  68. 'amount_total' : (invoice_dato.amount_total - line.price_subtotal),
  69. 'amount_untaxed' : (invoice_dato.amount_untaxed - line.price_subtotal)})
  70. # Actualar cuanta a pagr del cliente
  71. # partner_model = self.pool.get('res.partner').search(cr, uid,[('id', '=', invoice_dato.partner_id.ids)], context=context)
  72. # parthner_datos = self.pool.get('res.partner').browse(cr, uid, partner_model,context=context)
  73. # parthner_datos.write({'payment_amount_due' :invoice_dato.residual})
  74. # Otnere los linea de condicion de pago
  75. payment_line_model = self.pool.get('account.payment.term.line').search(cr, uid, [('payment_id', 'in', invoice_dato.payment_term.ids)
  76. ], order='id', context=context)
  77. payment_line_dato = self.pool.get('account.payment.term.line').browse(cr, uid, payment_line_model, context=context)
  78. # print(payment_line_dato)
  79. # Obtener Datos de Move Line
  80. move_line_model = self.pool.get('account.move.line').search(cr, uid, [('reconcile_ref', '=', None), ('ref', '=', invoice_dato.reference),
  81. ('credit', '=', 0)], order='id', context=context)
  82. # Obtener los registrode la lineas
  83. move_line_modelp = self.pool.get('account.move.line').search(cr, uid, [('ref', '=', invoice_dato.reference), ('credit', '=', 0)], order='id', context=context)
  84. # Retorna la cantidad de Registro de la Consulta
  85. move_count = self.pool.get('account.move.line').search_count(cr, uid, [('reconcile_ref', '=', None), ('ref', '=', invoice_dato.reference),
  86. ('credit', '=', 0)], context=context)
  87. # Selecionar todos los dactos de Move Line para su activacion
  88. model_move_line1 = self.pool.get('account.move.line').search(cr, uid,[('ref', '=', invoice_dato.reference)], context=context)
  89. datos_move_line2 = self.pool.get('account.move.line').browse(cr, uid, model_move_line1, context=context)
  90. move_line_dato = self.pool.get('account.move.line').browse(cr, uid, move_line_model, context=context)
  91. move_line_datosp = self.pool.get('account.move.line').browse(cr, uid, move_line_modelp, context=context)
  92. # print(move_line_dato)
  93. monto_cuota = invoice_dato.residual
  94. # Obterner el registro de diario de ventas
  95. model_journal = self.pool.get('account.journal').search(cr, uid,[('type', '=', 'sale')], context=context)
  96. datos_journal = self.pool.get('account.journal').browse(cr, uid, model_journal, context=context)
  97. #Activar Modificacion
  98. datos_journal.write({'entry_posted' : True})
  99. # datos_move_line2.write({'debit' : (moveline2.debit + ammount_interes)})
  100. for move_line in move_line_datosp:
  101. # obterner move
  102. move_model = self.pool.get('account.move').search(cr, uid, [('id', 'in', move_line.move_id.ids)],context=context)
  103. move_dato = self.pool.get('account.move').browse(cr, uid, move_model, context=context)
  104. for move in move_dato:
  105. estado = move.state
  106. # Obtener pagos
  107. # print('voucher')
  108. # voucher_model = self.pool.get('account.voucher').search(cr, uid, [('move_id', 'in', move.id)],context=context)
  109. # voucher_dato = self.pool.get('account.voucher').browse(cr, uid, voucher_model,context=context)
  110. # for voucher in voucher_dato:
  111. # obtener ls linea de pagos
  112. line_voucher_model = self.pool.get('account.voucher.line').search(cr, uid, [('move_line_id', 'in', move_line.ids),
  113. ('amount', '>', 0), ('reconcile', '=', False)],context=context)
  114. line_voucher_dato = self.pool.get('account.voucher.line').browse(cr, uid, line_voucher_model,context=context)
  115. for voucher_line in line_voucher_dato:
  116. monto_cuota =(monto_cuota - (voucher_line.amount_original - voucher_line.amount))
  117. move_dato.write({'state' : 'draft'})
  118. c=0
  119. montototal=0
  120. for line_move in move_line_dato:
  121. c +=1
  122. if c <= (move_count-1):
  123. move_line = self.pool.get('account.move.line').search(cr, uid, [('id', '=', line_move.ids)], context=context)
  124. move_line_browse = self.pool.get('account.move.line').browse(cr, uid, move_line, context=context)
  125. monto_cuota1= (monto_cuota*0.33)
  126. montototal +=monto_cuota1
  127. move_line_browse.write({'debit' : monto_cuota1})
  128. else:
  129. if c == move_count:
  130. move_line = self.pool.get('account.move.line').search(cr, uid, [('id', '=', line_move.ids)], context=context)
  131. move_line_browse = self.pool.get('account.move.line').browse(cr, uid, move_line, context=context)
  132. monto_cuota1= (monto_cuota - montototal)
  133. move_line_browse.write({'debit' : monto_cuota1})
  134. if estado == None:
  135. estado='draft'
  136. move_dato.write({'state' : estado})
  137. #Desativar Modificacion
  138. datos_journal.write({'entry_posted' : False})
  139. # Activar Move line
  140. datos_move_line2.write({'state' : 'valid'})
  141. # Eliminar Invoice Line ------------------------------------------------------
  142. self.pool.get('account.invoice.line').unlink(cr, uid,line.ids,context=context)
  143. # Eliminar linea de account.interest.line cuando su estado esta en fase ----------
  144. self.pool.get('account.interest.line').unlink(cr, uid, interes.ids, context=context)
  145. print("*****************************************************************")