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.

97 lines
3.5 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # Copyright (C) 2014 Akretion (<http://www.akretion.com>).
  6. # @author Sylvain LE GAL (https://twitter.com/legalsylvain)
  7. # @author Sylvain Calador (sylvain.calador@akretion.com).
  8. #
  9. # This program is free software: you can redistribute it and/or modify
  10. # it under the terms of the GNU Affero General Public License as
  11. # published by the Free Software Foundation, either version 3 of the
  12. # License, or (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU Affero General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU Affero General Public License
  20. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. #
  22. ##############################################################################
  23. from openerp import models, api
  24. class PosOrder(models.Model):
  25. _inherit = 'pos.order'
  26. # Overload Section
  27. @api.model
  28. def _order_fields(self, ui_order):
  29. res = super(PosOrder, self)._order_fields(ui_order)
  30. if 'order_id' in ui_order:
  31. res['order_id'] = ui_order['order_id']
  32. return res
  33. @api.model
  34. def create_from_ui(self, orders):
  35. """
  36. Remove from the 'orders' list all orders where amount_return is < 0
  37. (because that means they are not paid, but just in draft state).
  38. * call a specific function for the draft orders list
  39. * call the parent create_from_ui() for the remaining orders"""
  40. draft_orders = []
  41. for tmp_order in orders:
  42. if tmp_order['data']['amount_return'] < 0\
  43. and abs(tmp_order['data']['amount_return']) > 0.000001:
  44. draft_orders.append(tmp_order)
  45. orders.remove(tmp_order)
  46. # Save Draft Orders
  47. self._create_draft_order_from_ui(draft_orders)
  48. # Save Paid Orders
  49. return super(PosOrder, self).create_from_ui(orders)
  50. # Custom Section
  51. @api.model
  52. def search_read_orders(self, query):
  53. condition = [
  54. ('state', '=', 'draft'),
  55. ('statement_ids', '=', False),
  56. '|',
  57. ('name', 'ilike', query),
  58. ('partner_id', 'ilike', query)
  59. ]
  60. fields = ['name', 'partner_id', 'amount_total']
  61. return self.search_read(condition, fields, limit=10)
  62. @api.model
  63. def load_order(self, order_id):
  64. order = self.browse(order_id)
  65. condition = [('order_id', '=', order_id)]
  66. fields = ['product_id', 'price_unit', 'qty', 'discount']
  67. orderlines = self.lines.search_read(condition, fields)
  68. return {
  69. 'id': order.id,
  70. 'name': order.pos_reference,
  71. 'partner_id': order.partner_id.id,
  72. 'orderlines': orderlines,
  73. }
  74. @api.model
  75. def _create_draft_order_from_ui(self, orders):
  76. for order_tmp in orders:
  77. order_data = order_tmp['data']
  78. statements_data = order_data['statement_ids']
  79. order_data.pop('statement_ids')
  80. # create Order
  81. order = self.create(self._order_fields(order_data))
  82. # Create payment
  83. for statement_data in statements_data:
  84. self.add_payment(
  85. order.id, self._payment_fields(statement_data[2]))