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.

153 lines
5.1 KiB

  1. # Copyright 2020 ForgeFlow, S.L.
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  3. from odoo import api, fields, models, _
  4. from odoo.exceptions import UserError
  5. class WizardPOSBankStatementUpdateBalance(models.TransientModel):
  6. _name = "wizard.pos.update.bank.statement.balance"
  7. _description = 'POS Update Bank Statement Balance'
  8. def _default_session_id(self):
  9. return self.env.context.get('active_pos_id', False)
  10. _BALANCE_MOMENT_SELECTION = [
  11. ('bydefault', 'Default'),
  12. ('starting', 'Starting balance'),
  13. ('ending', 'Ending balance'),
  14. ]
  15. item_ids = fields.One2many(
  16. comodel_name="wizard.pos.update.bank.statement.balance.line",
  17. inverse_name="wiz_id",
  18. string="Items",
  19. )
  20. balance_moment = fields.Selection(
  21. selection=_BALANCE_MOMENT_SELECTION, string='Balance moment',
  22. default='bydefault')
  23. journal_id = fields.Many2one(
  24. comodel_name='account.journal', string="Journal",
  25. domain="[('id', 'in', journal_ids)]", required=True)
  26. session_id = fields.Many2one(
  27. comodel_name='pos.session', string="Current Session",
  28. default=_default_session_id, required=True, readonly=True)
  29. @api.model
  30. def _prepare_item(self, statement):
  31. return {
  32. "statement_id": statement.id,
  33. "name": statement.name,
  34. "journal_id": statement.journal_id.id,
  35. "balance_start": statement.balance_start,
  36. "balance_end": statement.balance_end,
  37. "total_entry_encoding": statement.total_entry_encoding,
  38. "currency_id": statement.currency_id.id,
  39. }
  40. @api.model
  41. def default_get(self, flds):
  42. res = super().default_get(flds)
  43. # Load objects
  44. session_obj = self.env["pos.session"]
  45. bank_statement_obj = self.env["account.bank.statement"]
  46. # Load context
  47. active_ids = self.env.context["active_id"] or []
  48. active_pos_id = self.env.context["active_pos_id"] or []
  49. active_model = self.env.context["active_model"] or []
  50. balance_moment = self.env.context["balance_moment"] or []
  51. # Check propagation
  52. if not active_pos_id:
  53. return res
  54. assert active_model == "pos.session", \
  55. "Bad context propagation"
  56. if len(active_pos_id) > 1:
  57. raise UserError(_('You cannot start the closing '
  58. 'balance for multiple POS sessions'))
  59. # Add bank statement lines
  60. session = session_obj.browse(active_pos_id[0])
  61. bank_statement = bank_statement_obj.browse(active_ids[0])
  62. items = []
  63. items.append([0, 0, self._prepare_item(bank_statement)])
  64. # Give values for wizard
  65. res["session_id"] = session.id
  66. res["item_ids"] = items
  67. res["balance_moment"] = balance_moment
  68. res["journal_id"] = bank_statement.journal_id.id
  69. return res
  70. @api.model
  71. def _prepare_cash_box_journal(self, item):
  72. return {
  73. 'amount': abs(item.difference),
  74. 'name': _('Out'),
  75. "journal_id": item.journal_id.id,
  76. }
  77. @api.multi
  78. def action_confirm(self):
  79. self.ensure_one()
  80. # record new values from wizard
  81. for item in self.item_ids:
  82. if item.balance_moment == 'starting':
  83. item.statement_id.balance_start = item.balance_start_real
  84. elif item.balance_moment == 'ending':
  85. item.statement_id.balance_end_real = item.balance_end_real
  86. # item.statement_id.balance_end = item.balance_end_real
  87. return True
  88. class WizardPOSBankStatementUpdateBalanceLine(models.TransientModel):
  89. _name = "wizard.pos.update.bank.statement.balance.line"
  90. _description = 'POS Update Bank Statement Balance Line'
  91. wiz_id = fields.Many2one(
  92. comodel_name='wizard.pos.update.bank.statement.balance',
  93. required=True,
  94. )
  95. statement_id = fields.Many2one(
  96. comodel_name='account.bank.statement',
  97. )
  98. name = fields.Char(
  99. related='statement_id.name'
  100. )
  101. journal_id = fields.Many2one(
  102. comodel_name='account.journal',
  103. related='statement_id.journal_id',
  104. )
  105. balance_start = fields.Monetary(
  106. string="Starting Balance",
  107. default=0.0,
  108. compute='_compute_balance_start'
  109. )
  110. balance_start_real = fields.Monetary(
  111. default=0.0
  112. )
  113. total_entry_encoding = fields.Monetary(
  114. related='statement_id.total_entry_encoding',
  115. )
  116. balance_end = fields.Monetary(
  117. string="Computed Balance",
  118. default=0.0,
  119. compute='_compute_balance_end'
  120. )
  121. balance_end_real = fields.Monetary(
  122. default=0.0
  123. )
  124. currency_id = fields.Many2one(
  125. comodel_name='res.currency',
  126. related='statement_id.currency_id'
  127. )
  128. balance_moment = fields.Selection(
  129. related='wiz_id.balance_moment',
  130. default='bydefault')
  131. def _compute_balance_start(self):
  132. for rec in self:
  133. rec.balance_start = rec.statement_id.balance_start
  134. def _compute_balance_end(self):
  135. for rec in self:
  136. rec.balance_end = rec.statement_id.balance_end