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.

467 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. var qty = line.qty;
  279. if (['return'].indexOf(action) !== -1) {
  280. // Invert line quantities
  281. qty *= -1;
  282. }
  283. // Create a new order line
  284. order.add_product(product, {
  285. price: line.price_unit,
  286. quantity: qty,
  287. discount: line.discount,
  288. merge: false,
  289. });
  290. }
  291. });
  292. },
  293. load_order_data: function (order_id) {
  294. var self = this;
  295. return this._rpc({
  296. model: 'pos.order',
  297. method: 'load_done_order_for_pos',
  298. args: [order_id],
  299. }).fail(function (error) {
  300. if (parseInt(error.code, 10) === 200) {
  301. // Business Logic Error, not a connection problem
  302. self.gui.show_popup(
  303. 'error-traceback', {
  304. 'title': error.data.message,
  305. 'body': error.data.debug,
  306. });
  307. } else {
  308. self.gui.show_popup('error', {
  309. 'title': _t('Connection error'),
  310. 'body': _t(
  311. 'Can not execute this action because the POS' +
  312. ' is currently offline'),
  313. });
  314. }
  315. });
  316. },
  317. load_order_from_data: function (order_data, action) {
  318. var self = this;
  319. this.unknown_products = [];
  320. var order = self._prepare_order_from_order_data(
  321. order_data, action);
  322. // Forbid POS Order loading if some products are unknown
  323. if (self.unknown_products.length > 0) {
  324. self.gui.show_popup('error-traceback', {
  325. 'title': _t('Unknown Products'),
  326. 'body': _t('Unable to load some order lines because the ' +
  327. 'products are not available in the POS cache.\n\n' +
  328. 'Please check that lines :\n\n * ') +
  329. self.unknown_products.join("; \n *"),
  330. });
  331. return false;
  332. }
  333. return order;
  334. },
  335. // Search Part
  336. search_done_orders: function (query) {
  337. var self = this;
  338. return this._rpc({
  339. model: 'pos.order',
  340. method: 'search_done_orders_for_pos',
  341. args: [query || '', this.pos.pos_session.id],
  342. }).then(function (result) {
  343. self.orders = result;
  344. // Get the date in local time
  345. _.each(self.orders, function (order) {
  346. if (order.date_order) {
  347. order.date_order = moment.utc(order.date_order)
  348. .local().format('YYYY-MM-DD HH:mm:ss');
  349. }
  350. });
  351. }).fail(function (error, event) {
  352. if (parseInt(error.code, 10) === 200) {
  353. // Business Logic Error, not a connection problem
  354. self.gui.show_popup(
  355. 'error-traceback', {
  356. 'title': error.data.message,
  357. 'body': error.data.debug,
  358. }
  359. );
  360. } else {
  361. self.gui.show_popup('error', {
  362. 'title': _t('Connection error'),
  363. 'body': _t(
  364. 'Can not execute this action because the POS' +
  365. ' is currently offline'),
  366. });
  367. }
  368. event.preventDefault();
  369. });
  370. },
  371. perform_search: function () {
  372. var self = this;
  373. return this.search_done_orders(self.search_query)
  374. .done(function () {
  375. self.render_list();
  376. });
  377. },
  378. clear_search: function () {
  379. var self = this;
  380. self.$('.searchbox input')[0].value = '';
  381. self.$('.searchbox input').focus();
  382. self.search_query = false;
  383. self.perform_search();
  384. },
  385. });
  386. gui.define_screen({
  387. name: 'orderlist',
  388. widget: OrderListScreenWidget,
  389. });
  390. var ListOrderButtonWidget = PosBaseWidget.extend({
  391. template: 'ListOrderButtonWidget',
  392. init: function (parent, options) {
  393. var opts = options || {};
  394. this._super(parent, opts);
  395. this.action = opts.action;
  396. this.label = opts.label;
  397. },
  398. button_click: function () {
  399. this.gui.show_screen('orderlist');
  400. },
  401. renderElement: function () {
  402. var self = this;
  403. this._super();
  404. this.$el.click(function () {
  405. self.button_click();
  406. });
  407. },
  408. });
  409. var widgets = chrome.Chrome.prototype.widgets;
  410. widgets.push({
  411. 'name': 'list_orders',
  412. 'widget': ListOrderButtonWidget,
  413. 'prepend': '.pos-rightheader',
  414. 'args': {
  415. 'label': 'All Orders',
  416. },
  417. });
  418. return {
  419. ListOrderButtonWidget: ListOrderButtonWidget,
  420. OrderListScreenWidget: OrderListScreenWidget,
  421. };
  422. });