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.

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