account_move.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # -*- coding: utf-8 -*-
  2. from openerp import fields, models, api, _
  3. from openerp.exceptions import Warning
  4. import openerp.addons.decimal_precision as dp
  5. import logging
  6. _logger = logging.getLogger(__name__)
  7. class account_move(models.Model):
  8. _inherit = 'account.move'
  9. # residual_type = fields.Selection([
  10. # ('receivable', 'Receivable'),
  11. # ('payable', 'Payable'),
  12. # ],
  13. # string='Residual Type')
  14. receivable_residual = fields.Float(
  15. string=_('Receivable Balance'),
  16. digits=dp.get_precision('Account'),
  17. compute='_compute_residual',
  18. # store=True,
  19. help="Remaining receivable amount due."
  20. )
  21. payable_residual = fields.Float(
  22. string=_('Payable Balance'),
  23. digits=dp.get_precision('Account'),
  24. compute='_compute_residual',
  25. # store=True,
  26. help="Remaining payable amount due."
  27. )
  28. @api.one
  29. @api.depends(
  30. 'state',
  31. 'line_id.account_id.type',
  32. 'line_id.amount_residual',
  33. # Fixes the fact that move_id.line_id.amount_residual, being not stored and old API, doesn't trigger recomputation
  34. 'line_id.reconcile_id',
  35. # 'line_id.amount_residual_currency',
  36. # 'line_id.currency_id',
  37. 'line_id.reconcile_partial_id.line_partial_ids',
  38. # 'line_id.reconcile_partial_id.line_partial_ids.invoice.type',
  39. )
  40. def _compute_residual(self):
  41. # TODO tal vez falta agregar lo de multi currency de donde lo sacamos
  42. # de invoice
  43. payable_residual = 0.0
  44. receivable_residual = 0.0
  45. # Each partial reconciliation is considered only once for each invoice
  46. # it appears into,
  47. # and its residual amount is divided by this number of invoices
  48. partial_reconciliations_done = []
  49. for line in self.sudo().line_id:
  50. if line.account_id.type not in ('receivable', 'payable'):
  51. continue
  52. if line.reconcile_partial_id and line.reconcile_partial_id.id in partial_reconciliations_done:
  53. continue
  54. # Get the correct line residual amount
  55. line_amount = line.amount_residual_currency if line.currency_id else line.amount_residual
  56. # For partially reconciled lines, split the residual amount
  57. if line.reconcile_partial_id:
  58. r = line.reconcile_partial_id
  59. line_amount = reduce(
  60. lambda y, t: (t.credit or 0.0) - (
  61. t.debit or 0.0) + y, r.line_partial_ids, 0.0)
  62. if line.account_id.type == 'receivable':
  63. receivable_residual += line_amount
  64. else:
  65. payable_residual += line_amount
  66. self.payable_residual = max(payable_residual, 0.0)
  67. self.receivable_residual = max(receivable_residual, 0.0)
  68. @api.multi
  69. def action_create_receipt_voucher(self):
  70. return self.create_voucher('receipt')
  71. @api.multi
  72. def action_create_payment_voucher(self):
  73. return self.create_voucher('payment')
  74. @api.multi
  75. def create_voucher(self, voucher_type):
  76. # todo ver como podemos incorporar cobros, si es que tiene sentido
  77. self.ensure_one()
  78. if voucher_type == 'payment':
  79. name = _('Payment')
  80. residual_amount = self.payable_residual
  81. else:
  82. name = _('Receipt')
  83. residual_amount = self.receivable_residual
  84. view_id = self.env['ir.model.data'].xmlid_to_res_id(
  85. 'account_voucher.view_vendor_receipt_dialog_form')
  86. voucher_context = self._context.copy()
  87. voucher_context.update({
  88. 'payment_expected_currency': self.company_id.currency_id.id,
  89. 'default_partner_id': self.partner_id.id,
  90. 'default_amount': residual_amount,
  91. # for compatibilit with other modules
  92. 'default_net_amount': residual_amount,
  93. 'default_reference': self.name,
  94. 'close_after_process': True,
  95. # 'invoice_type': self.type,
  96. # 'invoice_id': self.id,
  97. 'move_line_ids': self.line_id.filtered(
  98. lambda x: x.account_id.type in [
  99. 'payable', 'receivable']).ids,
  100. 'default_type': voucher_type,
  101. 'default_company_id': self.company_id.id,
  102. 'type': 'voucher_type',
  103. })
  104. res = {
  105. 'name': name,
  106. 'view_mode': 'form',
  107. 'view_id': view_id,
  108. 'view_type': 'form',
  109. 'res_model': 'account.voucher',
  110. 'type': 'ir.actions.act_window',
  111. 'nodestroy': True,
  112. 'target': 'new',
  113. 'domain': '[]',
  114. 'context': voucher_context
  115. }
  116. return res