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.

107 lines
3.6 KiB

  1. /* Copyright 2020 Solvos Consultoría Informática
  2. License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  3. */
  4. odoo.define('pos_order_return_traceability.widgets', function (require) {
  5. "use strict";
  6. var core = require('web.core');
  7. var _t = core._t;
  8. var screens = require('point_of_sale.screens');
  9. var order_mgmt_widgets = require('pos_order_mgmt.widgets');
  10. var QWeb = core.qweb;
  11. screens.ActionpadWidget.include({
  12. renderElement: function() {
  13. var self = this;
  14. this._super();
  15. var button_pay_click_handler = $._data(
  16. this.$el.find('.pay')[0], 'events').click[0].handler;
  17. var button_pay = this.$('.pay');
  18. button_pay.off('click');
  19. button_pay.click(function(){
  20. var order = self.pos.get_order();
  21. if (self.check_return_order(order)) {
  22. button_pay_click_handler();
  23. }
  24. });
  25. },
  26. check_return_order: function (order) {
  27. var self = this;
  28. if (!order.returned_order_id) {
  29. return true;
  30. }
  31. var lines = order.get_orderlines();
  32. var qty_incorrect_lines = [], no_return_lines = [];
  33. for (var i = 0; i < lines.length; i++) {
  34. var qty_line = lines[i].get_quantity();
  35. var product = lines[i].get_product();
  36. if (!lines[i].returned_line_id && (qty_line < 0) &&
  37. !product.pos_allow_negative_qty) {
  38. // Prevent new lines without return associated
  39. no_return_lines.push(product.display_name);
  40. } else if (lines[i].returned_line_id && (
  41. (qty_line > 0) ||
  42. ((-1)*lines[i].quantity_returnable > qty_line)
  43. )) {
  44. // Maximum quantity allowed exceeded
  45. qty_incorrect_lines.push(product.display_name + ': ' +
  46. lines[i].get_quantity_str() + ' < ' +
  47. (-1)*lines[i].quantity_returnable);
  48. }
  49. }
  50. if ((qty_incorrect_lines.length + no_return_lines.length) > 0) {
  51. var error_message = _t('Please check the following line(s):');
  52. if (qty_incorrect_lines.length > 0) {
  53. error_message += "\n\n";
  54. error_message += _t('* Invalid quantity line(s): ') +
  55. qty_incorrect_lines.join(', ');
  56. }
  57. if (no_return_lines.length > 0) {
  58. error_message += "\n\n";
  59. error_message += _t('* Non-returnable line(s): ') +
  60. no_return_lines.join(', ');
  61. }
  62. self.gui.show_popup(
  63. 'error-traceback', {
  64. 'title': _t('Return lines error(s)'),
  65. 'body': error_message,
  66. });
  67. return false;
  68. }
  69. return true;
  70. }
  71. });
  72. order_mgmt_widgets.OrderListScreenWidget.include({
  73. _prepare_product_options_from_orderline_data: function (
  74. order, line, action) {
  75. var self = this;
  76. var ret = this._super(order, line, action);
  77. if (['return'].indexOf(action) !== -1) {
  78. ret.quantity = (-1) * line.qty_returnable;
  79. ret.extras = {
  80. returned_line_id: line.id,
  81. quantity_returnable: line.qty_returnable,
  82. }
  83. }
  84. return ret;
  85. },
  86. });
  87. });