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.4 KiB

  1. # © 2015 ACSONE SA/NV (<http://acsone.eu>)
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  3. from odoo import _, api, fields, models
  4. from odoo.exceptions import UserError
  5. class WizardPosMoveReason(models.TransientModel):
  6. _name = 'wizard.pos.move.reason'
  7. _description = 'PoS Move Reasons Wizard'
  8. def _default_move_type(self):
  9. return self.env.context.get('default_move_type', 'expense')
  10. def _default_session_id(self):
  11. return self.env.context.get('active_id', False)
  12. _MOVE_TYPE_SELECTION = [
  13. ('income', 'Put Money In'),
  14. ('expense', 'Take Money Out'),
  15. ]
  16. move_type = fields.Selection(
  17. selection=_MOVE_TYPE_SELECTION, string='Move type',
  18. default=_default_move_type)
  19. move_reason_id = fields.Many2one(
  20. comodel_name='pos.move.reason', string='Move Reason', required=True)
  21. journal_id = fields.Many2one(
  22. comodel_name='account.journal', string="Journal",
  23. domain="[('id', 'in', journal_ids)]", required=True)
  24. session_id = fields.Many2one(
  25. comodel_name='pos.session', string="Current Session",
  26. default=_default_session_id, required=True, readonly=True)
  27. statement_id = fields.Many2one(
  28. comodel_name='account.bank.statement', string='Bank Statement',
  29. compute='_compute_statement_id')
  30. journal_ids = fields.Many2many(
  31. comodel_name='account.journal', related='move_reason_id.journal_ids')
  32. name = fields.Char(string='Reason', required=True)
  33. amount = fields.Float(string='Amount', required=True)
  34. @api.onchange('move_type')
  35. def onchange_move_type(self):
  36. if self.move_type == 'income':
  37. return {'domain': {
  38. 'move_reason_id': [('is_income_reason', '=', True)]}}
  39. else:
  40. return {'domain': {
  41. 'move_reason_id': [('is_expense_reason', '=', True)]}}
  42. @api.onchange('move_reason_id')
  43. def onchange_reason(self):
  44. if len(self.journal_ids) == 1:
  45. self.journal_id = self.journal_ids[0].id
  46. self.name = self.move_reason_id.name
  47. @api.constrains('amount')
  48. def _check_amount(self):
  49. for wizard in self.filtered(lambda x: x.amount <= 0):
  50. raise UserError(_("Invalid Amount"))
  51. @api.depends('journal_id', 'session_id')
  52. def _compute_statement_id(self):
  53. for wizard in self:
  54. if wizard.session_id and wizard.journal_id:
  55. statements = wizard.session_id.statement_ids.filtered(
  56. lambda x: x.journal_id == wizard.journal_id)
  57. wizard.statement_id = statements and statements[0]
  58. @api.multi
  59. def apply(self):
  60. self.ensure_one()
  61. AccountBankStatementLine = self.env['account.bank.statement.line']
  62. AccountBankStatementLine.create(self._prepare_statement_line())
  63. @api.multi
  64. def _prepare_statement_line(self):
  65. self.ensure_one()
  66. if self.move_type == 'income':
  67. account = self.move_reason_id.income_account_id
  68. amount = self.amount
  69. else:
  70. account = self.move_reason_id.expense_account_id
  71. amount = - self.amount
  72. return {
  73. 'date': fields.Date.context_today(self),
  74. 'statement_id': self.statement_id.id,
  75. 'journal_id': self.journal_id.id,
  76. 'amount': amount,
  77. 'account_id': account.id,
  78. 'name': self.name,
  79. 'ref': self.session_id.name,
  80. }