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.

477 lines
17 KiB

  1. /* Copyright 2018 GRAP - Sylvain LE GAL
  2. Copyright 2018 Tecnativa - David Vidal
  3. Copyright 2019 Druidoo - Ivan Todorovich
  4. License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). */
  5. odoo.define('pos_order_mgmt.widgets', function (require) {
  6. "use strict";
  7. var core = require('web.core');
  8. var _t = core._t;
  9. var PosBaseWidget = require('point_of_sale.BaseWidget');
  10. var screens = require('point_of_sale.screens');
  11. var gui = require('point_of_sale.gui');
  12. var chrome = require('point_of_sale.chrome');
  13. var pos = require('point_of_sale.models');
  14. var QWeb = core.qweb;
  15. var ScreenWidget = screens.ScreenWidget;
  16. var DomCache = screens.DomCache;
  17. screens.ReceiptScreenWidget.include({
  18. render_receipt: function () {
  19. if (!this.pos.reloaded_order) {
  20. return this._super();
  21. }
  22. var order = this.pos.reloaded_order;
  23. this.$('.pos-receipt-container').html(QWeb.render('PosTicket', {
  24. widget: this,
  25. pos: this.pos,
  26. order: order,
  27. receipt: order.export_for_printing(),
  28. orderlines: order.get_orderlines(),
  29. paymentlines: order.get_paymentlines(),
  30. }));
  31. this.pos.from_loaded_order = true;
  32. },
  33. click_next: function () {
  34. if (!this.pos.from_loaded_order) {
  35. return this._super();
  36. }
  37. this.pos.from_loaded_order = false;
  38. // When reprinting a loaded order we temporarily set it as the
  39. // active one. When we get out from the printing screen, we set
  40. // it back to the one that was active
  41. if (this.pos.current_order) {
  42. this.pos.set_order(this.pos.current_order);
  43. this.pos.current_order = false;
  44. }
  45. return this.gui.show_screen(this.gui.startup_screen);
  46. },
  47. });
  48. var OrderListScreenWidget = ScreenWidget.extend({
  49. template: 'OrderListScreenWidget',
  50. init: function (parent, options) {
  51. this._super(parent, options);
  52. this.order_cache = new DomCache();
  53. this.orders = [];
  54. this.unknown_products = [];
  55. this.search_query = false;
  56. this.perform_search();
  57. },
  58. auto_back: true,
  59. show: function () {
  60. var self = this;
  61. var previous_screen = false;
  62. if (this.pos.get_order()) {
  63. previous_screen = this.pos.get_order().get_screen_data(
  64. 'previous-screen');
  65. }
  66. if (previous_screen === 'receipt') {
  67. this.gui.screen_instances.receipt.click_next();
  68. this.gui.show_screen('orderlist');
  69. }
  70. this._super();
  71. this.renderElement();
  72. this.old_order = this.pos.get_order();
  73. this.$('.back').click(function () {
  74. return self.gui.show_screen(self.gui.startup_screen);
  75. });
  76. if (this.pos.config.iface_vkeyboard &&
  77. this.chrome.widget.keyboard) {
  78. this.chrome.widget.keyboard.connect(
  79. this.$('.searchbox input'));
  80. }
  81. var search_timeout = null;
  82. this.$('.searchbox input').on('keyup', function () {
  83. self.search_query = this.value;
  84. clearTimeout(search_timeout);
  85. search_timeout = setTimeout(function () {
  86. self.perform_search();
  87. }, 70);
  88. });
  89. this.$('.searchbox .search-clear').click(function () {
  90. self.clear_search();
  91. });
  92. this.perform_search();
  93. },
  94. render_list: function () {
  95. var self = this;
  96. var orders = this.orders;
  97. var contents = this.$el[0].querySelector('.order-list-contents');
  98. contents.innerHTML = "";
  99. for (
  100. var i = 0, len = Math.min(orders.length, 1000); i < len; i++
  101. ) {
  102. var order = orders[i];
  103. var orderline = this.order_cache.get_node(
  104. order.id || order.uid);
  105. if (!orderline) {
  106. var orderline_html = QWeb.render('OrderLine', {
  107. widget: this,
  108. order: order,
  109. });
  110. orderline = document.createElement('tbody');
  111. orderline.innerHTML = orderline_html;
  112. orderline = orderline.childNodes[1];
  113. this.order_cache.cache_node(
  114. order.id || order.uid, orderline);
  115. }
  116. if (order === this.old_order) {
  117. orderline.classList.add('highlight');
  118. } else {
  119. orderline.classList.remove('highlight');
  120. }
  121. contents.appendChild(orderline);
  122. }
  123. // FIXME: Everytime the list is rendered we need to reassing the
  124. // button events.
  125. this.$('.order-list-return').off('click');
  126. this.$('.order-list-reprint').off('click');
  127. this.$('.order-list-copy').off('click');
  128. this.$('.order-list-reprint').click(function (event) {
  129. self.order_list_actions(event, 'print');
  130. });
  131. this.$('.order-list-copy').click(function (event) {
  132. self.order_list_actions(event, 'copy');
  133. });
  134. this.$('.order-list-return').click(function (event) {
  135. self.order_list_actions(event, 'return');
  136. });
  137. },
  138. order_list_actions: function (event, action) {
  139. var self = this;
  140. var dataset = event.target.parentNode.dataset;
  141. self.load_order_data(parseInt(dataset.orderId, 10))
  142. .then(function (order_data) {
  143. self.order_action(order_data, action);
  144. });
  145. },
  146. order_action: function (order_data, action) {
  147. if (this.old_order !== null) {
  148. this.gui.back();
  149. }
  150. var order = this.load_order_from_data(order_data, action);
  151. if (!order) {
  152. // The load of the order failed. (products not found, ...
  153. // We cancel the action
  154. return;
  155. }
  156. this['action_' + action](order_data, order);
  157. },
  158. action_print: function (order_data, order) {
  159. // We store temporarily the current order so we can safely compute
  160. // taxes based on fiscal position
  161. this.pos.current_order = this.pos.get_order();
  162. this.pos.set_order(order);
  163. this.pos.reloaded_order = order;
  164. var skip_screen_state = this.pos.config.iface_print_skip_screen;
  165. // Disable temporarily skip screen if set
  166. this.pos.config.iface_print_skip_screen = false;
  167. this.gui.show_screen('receipt');
  168. this.pos.reloaded_order = false;
  169. // Set skip screen to whatever previous state
  170. this.pos.config.iface_print_skip_screen = skip_screen_state;
  171. // If it's invoiced, we also print the invoice
  172. if (order_data.to_invoice) {
  173. this.pos.chrome.do_action('point_of_sale.pos_invoice_report', {
  174. additional_context: { active_ids: [order_data.id] }
  175. })
  176. }
  177. // Destroy the order so it's removed from localStorage
  178. // Otherwise it will stay there and reappear on browser refresh
  179. order.destroy();
  180. },
  181. action_copy: function (order_data, order) {
  182. order.trigger('change');
  183. this.pos.get('orders').add(order);
  184. this.pos.set('selectedOrder', order);
  185. return order;
  186. },
  187. action_return: function (order_data, order) {
  188. order.trigger('change');
  189. this.pos.get('orders').add(order);
  190. this.pos.set('selectedOrder', order);
  191. return order;
  192. },
  193. _prepare_order_from_order_data: function (order_data, action) {
  194. var self = this;
  195. var order = new pos.Order({}, {
  196. pos: this.pos,
  197. });
  198. // Get Customer
  199. if (order_data.partner_id) {
  200. order.set_client(
  201. this.pos.db.get_partner_by_id(order_data.partner_id));
  202. }
  203. // Get fiscal position
  204. if (order_data.fiscal_position && this.pos.fiscal_positions) {
  205. var fiscal_positions = this.pos.fiscal_positions;
  206. order.fiscal_position = fiscal_positions.filter(function (p) {
  207. return p.id === order_data.fiscal_position;
  208. })[0];
  209. order.trigger('change');
  210. }
  211. // Get order lines
  212. self._prepare_orderlines_from_order_data(
  213. order, order_data, action);
  214. // Get Name
  215. if (['print'].indexOf(action) !== -1) {
  216. order.name = order_data.pos_reference;
  217. } else if (['return'].indexOf(action) !== -1) {
  218. order.name = _t("Refund ") + order.uid;
  219. }
  220. // Get to invoice
  221. if (['return', 'copy'].indexOf(action) !== -1) {
  222. // If previous order was invoiced, we need a refund too
  223. order.set_to_invoice(order_data.to_invoice);
  224. }
  225. // Get returned Order
  226. if (['print'].indexOf(action) !== -1) {
  227. // Get the same value as the original
  228. order.returned_order_id = order_data.returned_order_id;
  229. order.returned_order_reference =
  230. order_data.returned_order_reference;
  231. } else if (['return'].indexOf(action) !== -1) {
  232. order.returned_order_id = order_data.id;
  233. order.returned_order_reference = order_data.pos_reference;
  234. }
  235. // Get Date
  236. if (['print'].indexOf(action) !== -1) {
  237. order.formatted_validation_date =
  238. moment(order_data.date_order).format('YYYY-MM-DD HH:mm:ss');
  239. }
  240. // Get Payment lines
  241. if (['print'].indexOf(action) !== -1) {
  242. var paymentLines = order_data.statement_ids || [];
  243. _.each(paymentLines, function (paymentLine) {
  244. var line = paymentLine;
  245. // In case of local data
  246. if (line.length === 3) {
  247. line = line[2];
  248. }
  249. _.each(self.pos.cashregisters, function (cashregister) {
  250. if (cashregister.journal.id === line.journal_id) {
  251. if (line.amount > 0) {
  252. // If it is not change
  253. order.add_paymentline(cashregister);
  254. order.selected_paymentline.set_amount(
  255. line.amount);
  256. }
  257. }
  258. });
  259. });
  260. }
  261. return order;
  262. },
  263. _prepare_orderlines_from_order_data: function (
  264. order, order_data, action) {
  265. var orderLines = order_data.line_ids || order_data.lines || [];
  266. var self = this;
  267. _.each(orderLines, function (orderLine) {
  268. var line = orderLine;
  269. // In case of local data
  270. if (line.length === 3) {
  271. line = line[2];
  272. }
  273. var product = self.pos.db.get_product_by_id(line.product_id);
  274. // Check if product are available in pos
  275. if (_.isUndefined(product)) {
  276. self.unknown_products.push(String(line.product_id));
  277. } else {
  278. // Create a new order line
  279. order.add_product(product,
  280. self._prepare_product_options_from_orderline_data(
  281. order, line, action));
  282. }
  283. });
  284. },
  285. _prepare_product_options_from_orderline_data: function (
  286. order, line, action) {
  287. var qty = line.qty;
  288. if (['return'].indexOf(action) !== -1) {
  289. // Invert line quantities
  290. qty *= -1;
  291. }
  292. return {
  293. price: line.price_unit,
  294. quantity: qty,
  295. discount: line.discount,
  296. merge: false,
  297. }
  298. },
  299. load_order_data: function (order_id) {
  300. var self = this;
  301. return this._rpc({
  302. model: 'pos.order',
  303. method: 'load_done_order_for_pos',
  304. args: [order_id],
  305. }).fail(function (error) {
  306. if (parseInt(error.code, 10) === 200) {
  307. // Business Logic Error, not a connection problem
  308. self.gui.show_popup(
  309. 'error-traceback', {
  310. 'title': error.data.message,
  311. 'body': error.data.debug,
  312. });
  313. } else {
  314. self.gui.show_popup('error', {
  315. 'title': _t('Connection error'),
  316. 'body': _t(
  317. 'Can not execute this action because the POS' +
  318. ' is currently offline'),
  319. });
  320. }
  321. });
  322. },
  323. load_order_from_data: function (order_data, action) {
  324. var self = this;
  325. this.unknown_products = [];
  326. var order = self._prepare_order_from_order_data(
  327. order_data, action);
  328. // Forbid POS Order loading if some products are unknown
  329. if (self.unknown_products.length > 0) {
  330. self.gui.show_popup('error-traceback', {
  331. 'title': _t('Unknown Products'),
  332. 'body': _t('Unable to load some order lines because the ' +
  333. 'products are not available in the POS cache.\n\n' +
  334. 'Please check that lines :\n\n * ') +
  335. self.unknown_products.join("; \n *"),
  336. });
  337. return false;
  338. }
  339. return order;
  340. },
  341. // Search Part
  342. search_done_orders: function (query) {
  343. var self = this;
  344. return this._rpc({
  345. model: 'pos.order',
  346. method: 'search_done_orders_for_pos',
  347. args: [query || '', this.pos.pos_session.id],
  348. }).then(function (result) {
  349. self.orders = result;
  350. // Get the date in local time
  351. _.each(self.orders, function (order) {
  352. if (order.date_order) {
  353. order.date_order = moment.utc(order.date_order)
  354. .local().format('YYYY-MM-DD HH:mm:ss');
  355. }
  356. });
  357. }).fail(function (error, event) {
  358. if (parseInt(error.code, 10) === 200) {
  359. // Business Logic Error, not a connection problem
  360. self.gui.show_popup(
  361. 'error-traceback', {
  362. 'title': error.data.message,
  363. 'body': error.data.debug,
  364. }
  365. );
  366. } else {
  367. self.gui.show_popup('error', {
  368. 'title': _t('Connection error'),
  369. 'body': _t(
  370. 'Can not execute this action because the POS' +
  371. ' is currently offline'),
  372. });
  373. }
  374. event.preventDefault();
  375. });
  376. },
  377. perform_search: function () {
  378. var self = this;
  379. return this.search_done_orders(self.search_query)
  380. .done(function () {
  381. self.render_list();
  382. });
  383. },
  384. clear_search: function () {
  385. var self = this;
  386. self.$('.searchbox input')[0].value = '';
  387. self.$('.searchbox input').focus();
  388. self.search_query = false;
  389. self.perform_search();
  390. },
  391. });
  392. gui.define_screen({
  393. name: 'orderlist',
  394. widget: OrderListScreenWidget,
  395. });
  396. var ListOrderButtonWidget = PosBaseWidget.extend({
  397. template: 'ListOrderButtonWidget',
  398. init: function (parent, options) {
  399. var opts = options || {};
  400. this._super(parent, opts);
  401. this.action = opts.action;
  402. this.label = opts.label;
  403. },
  404. button_click: function () {
  405. this.gui.show_screen('orderlist');
  406. },
  407. renderElement: function () {
  408. var self = this;
  409. this._super();
  410. this.$el.click(function () {
  411. self.button_click();
  412. });
  413. },
  414. });
  415. var widgets = chrome.Chrome.prototype.widgets;
  416. widgets.push({
  417. 'name': 'list_orders',
  418. 'widget': ListOrderButtonWidget,
  419. 'prepend': '.pos-rightheader',
  420. 'args': {
  421. 'label': 'All Orders',
  422. },
  423. });
  424. return {
  425. ListOrderButtonWidget: ListOrderButtonWidget,
  426. OrderListScreenWidget: OrderListScreenWidget,
  427. };
  428. });