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.

144 lines
5.2 KiB

  1. # Copyright (C) 2017 - Today: GRAP (http://www.grap.coop)
  2. # @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
  3. # @author Quentin DUPONT (quentin.dupont@grap.coop)
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. from odoo import _, api, fields, models
  6. from odoo.exceptions import Warning as UserError
  7. import odoo.addons.decimal_precision as dp
  8. class AccountBankStatement(models.Model):
  9. _inherit = "account.bank.statement"
  10. # Columns Section
  11. control_balance = fields.Float(
  12. string="Controled Balance",
  13. compute="_compute_control_balance",
  14. digits_compute=dp.get_precision("Account"),
  15. )
  16. control_difference = fields.Float(
  17. string="Control Difference",
  18. compute="_compute_control_difference",
  19. digits_compute=dp.get_precision("Account"),
  20. )
  21. is_pos_control = fields.Boolean(
  22. string="Pos control Bank statement",
  23. compute="_compute_is_pos_control", store=True,
  24. )
  25. pos_session_state = fields.Char(
  26. string="Pos session state", compute="_compute_pos_session_state"
  27. )
  28. display_autosolve = fields.Boolean(
  29. string="Display autosolve", compute="_compute_display_autosolve"
  30. )
  31. # Compute Section
  32. @api.multi
  33. @api.depends("line_ids")
  34. def _compute_control_balance(self):
  35. for statement in self:
  36. statement.control_balance = sum(
  37. statement.mapped("balance_end_real")
  38. )
  39. @api.multi
  40. @api.depends("control_balance", "total_entry_encoding", "balance_end_real")
  41. def _compute_control_difference(self):
  42. for statement in self:
  43. statement.control_difference = (
  44. + statement.balance_end_real
  45. - statement.balance_start
  46. - statement.total_entry_encoding
  47. )
  48. @api.multi
  49. @api.depends("journal_id.pos_control", "pos_session_state",
  50. "balance_start")
  51. def _compute_is_pos_control(self):
  52. for statement in self:
  53. journal = statement.journal_id
  54. statement.is_pos_control = journal.pos_control
  55. @api.multi
  56. @api.depends("pos_session_id.state")
  57. def _compute_pos_session_state(self):
  58. for statement in self:
  59. statement.pos_session_state = statement.pos_session_id.state
  60. # Display button autosolve with some conditions
  61. @api.multi
  62. def _compute_display_autosolve(self):
  63. for statement in self:
  64. if not statement.journal_id.pos_control:
  65. statement.display_autosolve = False
  66. else:
  67. if statement.pos_session_id.config_id.autosolve_limit:
  68. difference_with_limit = (
  69. abs(statement.control_difference)
  70. - statement.pos_session_id.config_id.autosolve_limit
  71. )
  72. else:
  73. difference_with_limit = -1
  74. statement.display_autosolve = (
  75. statement.pos_session_state not in
  76. ["closed"]
  77. and difference_with_limit < 0
  78. and abs(round(statement.control_difference, 3)) != 0
  79. )
  80. @api.multi
  81. @api.depends("pos_session_state")
  82. def automatic_solve(self):
  83. self.WizardReason = self.env['wizard.pos.move.reason']
  84. for statement in self:
  85. pos_move_reason = statement.\
  86. pos_session_id.config_id.autosolve_pos_move_reason
  87. if pos_move_reason:
  88. cb_pos_move_reason_id = pos_move_reason.id
  89. cb_difference = statement.control_difference
  90. cb_journal_id = statement.journal_id.id
  91. if cb_difference < 0:
  92. default_move_type = "expense"
  93. else:
  94. default_move_type = "income"
  95. wizard = self.WizardReason.with_context(
  96. active_id=statement.pos_session_id.id,
  97. default_move_type=default_move_type).create({
  98. 'move_reason_id': cb_pos_move_reason_id,
  99. 'journal_id': cb_journal_id,
  100. 'statement_id': statement.id,
  101. 'amount': abs(cb_difference),
  102. 'name': 'Automatic solve',
  103. })
  104. wizard.apply()
  105. else:
  106. raise UserError(
  107. _(
  108. "We can't autosolve this difference. \nYou need to "
  109. "configure the Point Of Sale config and choose a "
  110. "pos move reason for autosolving this difference."
  111. )
  112. )
  113. def open_cashbox_starting_balance(self):
  114. return self.open_cashbox_balance('starting')
  115. def open_cashbox_ending_balance(self):
  116. return self.open_cashbox_balance('ending')
  117. def open_cashbox_balance(self, balance_moment):
  118. action = self.env.ref(
  119. "pos_multiple_control."
  120. "action_wizard_pos_update_bank_statement_balance").read()[0]
  121. action['context'] = {'balance_moment': balance_moment,
  122. 'active_id': [self.id],
  123. 'active_pos_id': [self.pos_session_id.id],
  124. 'active_model': 'pos.session'}
  125. return action