You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
3.0 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright (C) 2017 Creu Blanca
  3. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
  4. from odoo import api, fields, models
  5. class CashInvoiceIn(models.TransientModel):
  6. _name = 'cash.invoice.in'
  7. _inherit = 'cash.box.out'
  8. def _default_value(self, default_function):
  9. active_model = self.env.context.get('active_model', False)
  10. if active_model:
  11. active_ids = self.env.context.get('active_ids', False)
  12. return default_function(active_model, active_ids)
  13. return None
  14. def _default_company(self):
  15. return self._default_value(self.default_company)
  16. def _default_currency(self):
  17. return self._default_value(self.default_currency)
  18. def _default_journals(self):
  19. return self._default_value(self.default_journals)
  20. def _default_journal(self):
  21. journals = self._default_journals()
  22. if journals and len(journals.ids) > 0:
  23. return self.env['account.journal'].browse(
  24. journals.ids[0]
  25. ).ensure_one()
  26. def _default_journal_count(self):
  27. return len(self._default_journals().ids)
  28. invoice_id = fields.Many2one(
  29. 'account.invoice',
  30. string='Invoice',
  31. required=True
  32. )
  33. name = fields.Char(
  34. related='invoice_id.number'
  35. )
  36. company_id = fields.Many2one(
  37. 'res.company',
  38. default=_default_company,
  39. required=True,
  40. readonly=True
  41. )
  42. currency_id = fields.Many2one(
  43. 'res.currency',
  44. default=_default_currency,
  45. required=True,
  46. readonly=True
  47. )
  48. journal_ids = fields.Many2many(
  49. 'account.journal',
  50. default=_default_journals,
  51. required=True,
  52. readonly=True
  53. )
  54. journal_id = fields.Many2one(
  55. 'account.journal',
  56. required=True,
  57. default=_default_journal
  58. )
  59. journal_count = fields.Integer(
  60. default=_default_journal_count,
  61. readonly=True
  62. )
  63. def default_company(self, active_model, active_ids):
  64. return self.env[active_model].browse(active_ids)[0].company_id
  65. def default_currency(self, active_model, active_ids):
  66. return self.default_company(active_model, active_ids).currency_id
  67. def default_journals(self, active_model, active_ids):
  68. return self.env[active_model].browse(active_ids)[0].journal_id
  69. @api.onchange('journal_ids')
  70. def compute_journal_count(self):
  71. self.journal_count = len(self.journal_ids.ids)
  72. @api.onchange('invoice_id')
  73. def _onchange_invoice(self):
  74. self.amount = self.invoice_id.residual
  75. @api.multi
  76. def _calculate_values_for_statement_line(self, record):
  77. res = super(CashInvoiceIn, self)._calculate_values_for_statement_line(
  78. record
  79. )
  80. res['invoice_id'] = self.invoice_id.id
  81. res['account_id'] = self.invoice_id.account_id.id
  82. res['ref'] = self.invoice_id.number
  83. res['partner_id'] = self.invoice_id.partner_id.id
  84. return res