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.

57 lines
2.0 KiB

  1. # coding: utf-8
  2. # Copyright (C) 2015 - Today: GRAP (http://www.grap.coop)
  3. # @author: Julien WESTE
  4. # @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
  5. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  6. from openerp import _, api, models
  7. from openerp.exceptions import Warning as UserError
  8. class PosOrder(models.Model):
  9. _inherit = 'pos.order'
  10. # Overload Section
  11. @api.multi
  12. def action_paid(self):
  13. """ Merge all cash statement line of the Order"""
  14. self._merge_cash_payment()
  15. return super(PosOrder, self).action_paid()
  16. @api.multi
  17. def add_payment_v8(self, data):
  18. """Hack to call old api. TODO-V10 : remove me."""
  19. for order in self:
  20. self.pool['pos.order'].add_payment(
  21. self._cr, self._uid, order.id, data, context=self._context)
  22. return True
  23. # Private Function Section
  24. @api.multi
  25. def _merge_cash_payment(self):
  26. for order in self:
  27. cash_statements = order.statement_ids.filtered(
  28. lambda x: x.journal_id.type == 'cash')
  29. if len(cash_statements) < 2:
  30. continue
  31. main_statement = cash_statements[0]
  32. cash_total = sum(cash_statements.mapped('amount'))
  33. # Unlink all statements except one
  34. cash_statements.filtered(
  35. lambda x: x.id != main_statement.id).unlink()
  36. main_statement.write({'amount': cash_total})
  37. @api.multi
  38. def _allow_change_payments(self):
  39. """Return True if the user can change the payment of a POS, depending
  40. of the state of the current session."""
  41. closed_orders = self.filtered(
  42. lambda x: x.session_id.state == 'closed')
  43. if len(closed_orders):
  44. raise UserError(_(
  45. "You can not change payments of the POS '%s' because"
  46. " the associated session '%s' has been closed!" % (
  47. ', '.join(closed_orders.mapped('name')),
  48. ', '.join(closed_orders.mapped('session_id.name')))))