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.

168 lines
6.2 KiB

  1. # Copyright (C) 2017 - Today: GRAP (http://www.grap.coop)
  2. # @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from odoo import _, api, fields, models
  5. from odoo.exceptions import ValidationError
  6. from odoo.exceptions import Warning as UserError
  7. class PosSession(models.Model):
  8. _inherit = "pos.session"
  9. # Columns Section
  10. statement_ids = fields.One2many(readonly=False)
  11. control_statement_ids = fields.One2many(
  12. string="Control statements",
  13. comodel_name="account.bank.statement",
  14. related="statement_ids",
  15. )
  16. summary_statement_ids = fields.One2many(
  17. string="Summary statements",
  18. comodel_name="account.bank.statement",
  19. related="statement_ids",
  20. )
  21. control_register_balance_start = fields.Float(
  22. compute='_compute_control_register_balance_start',
  23. string='Opening Balances')
  24. control_register_total_entry_encoding = fields.Float(
  25. compute='_compute_control_register_total_entry_encoding',
  26. string='Transactions')
  27. control_register_balance_end = fields.Float(
  28. compute='_compute_control_register_balance_end',
  29. string='Theoretical Closing Balances')
  30. control_register_balance = fields.Float(
  31. compute='_compute_control_register_balance',
  32. string='Real Closing Balances')
  33. control_register_difference = fields.Float(
  34. compute='_compute_control_register_difference',
  35. string='Total difference')
  36. # Compute Section
  37. @api.multi
  38. @api.depends('statement_ids.is_pos_control', 'statement_ids.balance_start')
  39. def _compute_control_register_balance_start(self):
  40. for session in self:
  41. session.control_register_balance_start = sum(
  42. session.statement_ids.filtered(
  43. lambda x: x.is_pos_control).mapped('balance_start'))
  44. @api.multi
  45. @api.depends(
  46. 'statement_ids.is_pos_control', 'statement_ids.total_entry_encoding')
  47. def _compute_control_register_total_entry_encoding(self):
  48. for session in self:
  49. session.control_register_total_entry_encoding = sum(
  50. session.statement_ids.filtered(
  51. lambda x: x.is_pos_control).mapped('total_entry_encoding'))
  52. @api.multi
  53. @api.depends(
  54. 'statement_ids.is_pos_control', 'statement_ids.balance_end_real')
  55. def _compute_control_register_balance_end(self):
  56. for session in self:
  57. session.control_register_balance_end = sum(
  58. session.statement_ids.filtered(
  59. lambda x: x.is_pos_control).mapped('balance_end'))
  60. @api.multi
  61. @api.depends(
  62. 'statement_ids.is_pos_control', 'statement_ids.control_balance')
  63. def _compute_control_register_balance(self):
  64. for session in self:
  65. session.control_register_balance = sum(
  66. session.statement_ids.filtered(
  67. lambda x: x.is_pos_control).mapped('control_balance'))
  68. @api.multi
  69. @api.depends(
  70. 'statement_ids.is_pos_control', 'statement_ids.control_difference')
  71. def _compute_control_register_difference(self):
  72. for session in self:
  73. session.control_register_difference = sum(
  74. session.statement_ids.filtered(
  75. lambda x: x.is_pos_control).mapped('control_difference'))
  76. # Model
  77. @api.multi
  78. def open_cashbox_opening(self):
  79. return super(PosSession, self).open_cashbox()
  80. @api.multi
  81. def action_pos_session_new_session(self):
  82. return self.config_id.open_new_session(False)
  83. @api.multi
  84. def wkf_action_closing_control(self):
  85. for session in self:
  86. draft_orders = session.order_ids.filtered(
  87. lambda x: x.state == "draft"
  88. )
  89. if len(draft_orders):
  90. raise UserError(
  91. _(
  92. "You can not end this session because there are some"
  93. " draft orders: \n\n- %s"
  94. )
  95. % ("\n- ".join([x.name for x in draft_orders]))
  96. )
  97. return super(PosSession, self).wkf_action_closing_control()
  98. @api.multi
  99. def action_pos_session_validate(self):
  100. for session in self:
  101. for statement in session.statement_ids:
  102. if statement.journal_id.pos_control is True:
  103. if abs(statement.control_difference) > 0.001:
  104. raise UserError(
  105. _(
  106. "You can not close this session because the "
  107. "journal %s (from %s) has a not null "
  108. "difference: %s%s \n You have to change his "
  109. "starting or ending balance"
  110. )
  111. % (statement.journal_id.name, statement.name,
  112. str(round(statement.control_difference, 3)),
  113. statement.currency_id.symbol)
  114. )
  115. return super(PosSession, self).action_pos_session_validate()
  116. # Constraints
  117. @api.multi
  118. @api.constrains('user_id', 'state')
  119. def _check_unicity(self):
  120. for session in self:
  121. domain = [
  122. ("state", "in", ["opening_control", "opened"]),
  123. ("user_id", "=", session.user_id.id),
  124. ]
  125. if self.search_count(domain) > 1:
  126. raise ValidationError(
  127. _(
  128. "You cannot create two active sessions "
  129. "with the same responsible!"
  130. )
  131. )
  132. @api.multi
  133. @api.constrains('config_id', 'state')
  134. def _check_pos_config(self):
  135. for session in self:
  136. domain = [
  137. ("state", "in", ["opening_control", "opened"]),
  138. ("config_id", "=", session.config_id.id),
  139. ]
  140. if self.search_count(domain) > 1:
  141. raise ValidationError(
  142. _(
  143. "You cannot create two active sessions related"
  144. " to the same point of sale!"
  145. )
  146. )