account_move_line.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # -*- coding: utf-8 -*-
  2. from openerp import fields, models, api, _
  3. from openerp.exceptions import Warning
  4. class account_move_line(models.Model):
  5. _inherit = 'account.move.line'
  6. tax_settlement_detail_id = fields.Many2one(
  7. 'account.tax.settlement.detail',
  8. 'Tax Settlement Detail',
  9. )
  10. tax_state = fields.Selection([
  11. ('to_settle', _('To Settle')),
  12. ('to_pay', _('To Pay')),
  13. ('paid', _('Paid')),
  14. ],
  15. _('Tax State'),
  16. compute='_get_tax_state',
  17. # store=True,
  18. )
  19. tax_settlement_move_id = fields.Many2one(
  20. 'account.move',
  21. 'Tax Settlement Move',
  22. help='Move where this tax has been settled',
  23. )
  24. # NOTO. No se porque me da un error esta funcion. Por ahora pusimos
  25. # restriccion en tax settlement
  26. # @api.multi
  27. # def write(self, vals):
  28. # """
  29. # Check that you are not writing tax_settlement_move_id to a line that
  30. # has it already setted
  31. # """
  32. # if 'tax_settlement_move_id' in vals:
  33. # if self.filtered('tax_settlement_move_id'):
  34. # raise Warning(_(
  35. # 'I seams that some lines has been already settled.\n'
  36. # '* Lines: %s') % (
  37. # self.filtered('tax_settlement_move_id').ids))
  38. # return super(account_move_line, self).write(vals)
  39. @api.one
  40. @api.depends(
  41. 'tax_code_id',
  42. # 'tax_settlement_move_id',
  43. 'tax_settlement_move_id.payable_residual',
  44. )
  45. def _get_tax_state(self):
  46. if self.tax_code_id:
  47. tax_state = 'to_settle'
  48. if self.tax_settlement_move_id:
  49. tax_state = 'to_pay'
  50. # if tax_settlement_move_id and move are the same, then
  51. # we are on the settlement move line
  52. if self.tax_settlement_move_id == self.move_id:
  53. tax_state = False
  54. elif self.tax_settlement_move_id.payable_residual == 0.0:
  55. tax_state = 'paid'
  56. self.tax_state = tax_state
  57. @api.multi
  58. def pay_tax_settlement(self):
  59. self.ensure_one()
  60. return self.tax_settlement_move_id.with_context(
  61. from_settlement=True).create_voucher('payment')
  62. @api.multi
  63. def make_tax_settlement(self):
  64. self.ensure_one()
  65. if self.tax_settlement_move_id:
  66. raise Warning(_('Line already settled'))
  67. if not self.tax_code_id:
  68. raise Warning(_(
  69. 'Settlement only alled for journal items with tax code'))
  70. # get parent tax codes (only parents)
  71. parent_tax_codes_ids = []
  72. parent_tax_code = self.tax_code_id
  73. while parent_tax_code:
  74. parent_tax_codes_ids.append(parent_tax_code.id)
  75. parent_tax_code = parent_tax_code.parent_id
  76. # parent_tax_codes_ids = self.
  77. journals = self.env['account.journal'].search([
  78. ('type', '=', 'tax_settlement'),
  79. ('tax_code_id', 'in', parent_tax_codes_ids),
  80. ])
  81. if not journals:
  82. raise Warning(_(
  83. 'No tax settlemnt journal found for tax code %s') % (
  84. self.tax_code_id.name))
  85. elif len(journals) != 1:
  86. raise Warning(_(
  87. 'Only one tax settlemnt journal must exist for tax code %s'
  88. 'We have found the journal ids %s') % (
  89. self.tax_code_id, journals.ids))
  90. else:
  91. journal = journals
  92. # check account payable
  93. if self.debit < self.credit:
  94. account = journal.default_debit_account_id
  95. tax_code = journal.default_debit_tax_code_id
  96. else:
  97. account = journal.default_credit_account_id
  98. tax_code = journal.default_credit_tax_code_id
  99. # check account type so that we can create a debt
  100. if account.type != 'payable':
  101. raise Warning(_(
  102. 'You can only pay if tax counterpart use a payable account.'
  103. 'Account id %i' % account.id))
  104. # get date, period and name
  105. date = fields.Date.context_today(self)
  106. period = self.env['account.period'].with_context(
  107. company_id=self.company_id.id).find(date)[:1]
  108. name = journal.sequence_id._next()
  109. move_vals = {
  110. 'ref': name,
  111. 'name': name,
  112. 'period_id': period.id,
  113. 'date': date,
  114. 'journal_id': journal.id,
  115. 'company_id': self.company_id.id,
  116. }
  117. move = self.env['account.move'].create(move_vals)
  118. # write move id on settled tax move line
  119. self.tax_settlement_move_id = move.id
  120. counterpart_line_vals = {
  121. 'move_id': move.id,
  122. 'partner_id': self.partner_id.id,
  123. 'name': self.name,
  124. 'debit': self.credit,
  125. 'credit': self.debit,
  126. 'account_id': self.account_id.id,
  127. 'tax_code_id': self.tax_code_id.id,
  128. 'tax_amount': self.tax_amount,
  129. 'tax_settlement_move_id': move.id,
  130. }
  131. move.line_id.create(counterpart_line_vals)
  132. # TODO ver si ref se completa con el name del journal
  133. deb_line_vals = {
  134. 'move_id': move.id,
  135. 'partner_id': journal.partner_id.commercial_partner_id.id,
  136. 'name': self.name,
  137. 'debit': self.debit,
  138. 'credit': self.credit,
  139. 'account_id': account.id,
  140. 'tax_code_id': tax_code.id,
  141. 'tax_amount': -1.0 * self.tax_amount,
  142. }
  143. move.line_id.create(deb_line_vals)
  144. return True