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.

104 lines
3.2 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 CashInvoiceOut(models.TransientModel):
  6. _name = 'cash.invoice.out'
  7. _inherit = 'cash.box.in'
  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())
  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. journal_ids = fields.Many2many(
  48. 'account.journal',
  49. default=_default_journals,
  50. required=True,
  51. readonly=True
  52. )
  53. journal_id = fields.Many2one(
  54. 'account.journal',
  55. required=True,
  56. default=_default_journal
  57. )
  58. journal_count = fields.Integer(
  59. default=_default_journal_count,
  60. readonly=True
  61. )
  62. def default_company(self, active_model, active_ids):
  63. return self.env[active_model].browse(active_ids)[0].company_id
  64. def default_currency(self, active_model, active_ids):
  65. return self.default_company(active_model, active_ids).currency_id
  66. def default_journals(self, active_model, active_ids):
  67. return self.env[active_model].browse(active_ids)[0].journal_id
  68. @api.onchange('journal_ids')
  69. def compute_journal_count(self):
  70. self.journal_count = len(self.journal_ids.ids)
  71. @api.onchange('journal_id')
  72. def _onchange_journal(self):
  73. self.currency_id = (self.journal_id.currency_id or
  74. self.journal_id.company_id.currency_id)
  75. @api.onchange('invoice_id')
  76. def _onchange_invoice(self):
  77. self.amount = self.invoice_id.residual
  78. @api.multi
  79. def _calculate_values_for_statement_line(self, record):
  80. res = super(CashInvoiceOut, self)._calculate_values_for_statement_line(
  81. record
  82. )
  83. res['invoice_id'] = self.invoice_id.id
  84. res['account_id'] = self.invoice_id.account_id.id
  85. res['ref'] = self.invoice_id.number
  86. res['partner_id'] = self.invoice_id.partner_id.id
  87. return res