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.

465 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. if (this.pos.config.iface_print_via_proxy) {
  164. this.pos.proxy.print_receipt(QWeb.render(
  165. 'XmlReceipt', {
  166. receipt: order.export_for_printing(),
  167. widget: this,
  168. pos: this.pos,
  169. order: order,
  170. orderlines: order.get_orderlines(),
  171. paymentlines: order.get_paymentlines(),
  172. }));
  173. this.pos.set_order(this.pos.current_order);
  174. this.pos.current_order = false;
  175. } else {
  176. this.pos.reloaded_order = order;
  177. this.gui.show_screen('receipt');
  178. this.pos.reloaded_order = false;
  179. }
  180. order.destroy();
  181. },
  182. action_copy: function (order_data, order) {
  183. order.trigger('change');
  184. this.pos.get('orders').add(order);
  185. this.pos.set('selectedOrder', order);
  186. return order;
  187. },
  188. action_return: function (order_data, order) {
  189. order.trigger('change');
  190. this.pos.get('orders').add(order);
  191. this.pos.set('selectedOrder', order);
  192. return order;
  193. },
  194. _prepare_order_from_order_data: function (order_data, action) {
  195. var self = this;
  196. var order = new pos.Order({}, {
  197. pos: this.pos,
  198. });
  199. // Get Customer
  200. if (order_data.partner_id) {
  201. order.set_client(
  202. this.pos.db.get_partner_by_id(order_data.partner_id));
  203. }
  204. // Get fiscal position
  205. if (order_data.fiscal_position && this.pos.fiscal_positions) {
  206. var fiscal_positions = this.pos.fiscal_positions;
  207. order.fiscal_position = fiscal_positions.filter(function (p) {
  208. return p.id === order_data.fiscal_position;
  209. })[0];
  210. order.trigger('change');
  211. }
  212. // Get order lines
  213. self._prepare_orderlines_from_order_data(
  214. order, order_data, action);
  215. // Get Name
  216. if (['print'].indexOf(action) !== -1) {
  217. order.name = order_data.pos_reference;
  218. } else if (['return'].indexOf(action) !== -1) {
  219. order.name = _t("Refund ") + order.uid;
  220. }
  221. // Get to invoice
  222. if (['return', 'copy'].indexOf(action) !== -1) {
  223. // If previous order was invoiced, we need a refund too
  224. order.set_to_invoice(order_data.to_invoice);
  225. }
  226. // Get returned Order
  227. if (['print'].indexOf(action) !== -1) {
  228. // Get the same value as the original
  229. order.returned_order_id = order_data.returned_order_id;
  230. order.returned_order_reference =
  231. order_data.returned_order_reference;
  232. } else if (['return'].indexOf(action) !== -1) {
  233. order.returned_order_id = order_data.id;
  234. order.returned_order_reference = order_data.pos_reference;
  235. }
  236. // Get Date
  237. if (['print'].indexOf(action) !== -1) {
  238. order.formatted_validation_date =
  239. moment(order_data.date_order).format('YYYY-MM-DD HH:mm:ss');
  240. }
  241. // Get Payment lines
  242. if (['print'].indexOf(action) !== -1) {
  243. var paymentLines = order_data.statement_ids || [];
  244. _.each(paymentLines, function (paymentLine) {
  245. var line = paymentLine;
  246. // In case of local data
  247. if (line.length === 3) {
  248. line = line[2];
  249. }
  250. _.each(self.pos.cashregisters, function (cashregister) {
  251. if (cashregister.journal.id === line.journal_id) {
  252. if (line.amount > 0) {
  253. // If it is not change
  254. order.add_paymentline(cashregister);
  255. order.selected_paymentline.set_amount(
  256. line.amount);
  257. }
  258. }
  259. });
  260. });
  261. }
  262. return order;
  263. },
  264. _prepare_orderlines_from_order_data: function (
  265. order, order_data, action) {
  266. var orderLines = order_data.line_ids || order_data.lines || [];
  267. var self = this;
  268. _.each(orderLines, function (orderLine) {
  269. var line = orderLine;
  270. // In case of local data
  271. if (line.length === 3) {
  272. line = line[2];
  273. }
  274. var product = self.pos.db.get_product_by_id(line.product_id);
  275. // Check if product are available in pos
  276. if (_.isUndefined(product)) {
  277. self.unknown_products.push(String(line.product_id));
  278. } else {
  279. var qty = line.qty;
  280. if (['return'].indexOf(action) !== -1) {
  281. // Invert line quantities
  282. qty *= -1;
  283. }
  284. // Create a new order line
  285. order.add_product(product, {
  286. price: line.price_unit,
  287. quantity: qty,
  288. discount: line.discount,
  289. merge: false,
  290. });
  291. }
  292. });
  293. },
  294. load_order_data: function (order_id) {
  295. var self = this;
  296. return this._rpc({
  297. model: 'pos.order',
  298. method: 'load_done_order_for_pos',
  299. args: [order_id],
  300. }).fail(function (error) {
  301. if (parseInt(error.code, 10) === 200) {
  302. // Business Logic Error, not a connection problem
  303. self.gui.show_popup(
  304. 'error-traceback', {
  305. 'title': error.data.message,
  306. 'body': error.data.debug,
  307. });
  308. } else {
  309. self.gui.show_popup('error', {
  310. 'title': _t('Connection error'),
  311. 'body': _t(
  312. 'Can not execute this action because the POS' +
  313. ' is currently offline'),
  314. });
  315. }
  316. });
  317. },
  318. load_order_from_data: function (order_data, action) {
  319. var self = this;
  320. this.unknown_products = [];
  321. var order = self._prepare_order_from_order_data(
  322. order_data, action);
  323. // Forbid POS Order loading if some products are unknown
  324. if (self.unknown_products.length > 0) {
  325. self.gui.show_popup('error-traceback', {
  326. 'title': _t('Unknown Products'),
  327. 'body': _t('Unable to load some order lines because the ' +
  328. 'products are not available in the POS cache.\n\n' +
  329. 'Please check that lines :\n\n * ') +
  330. self.unknown_products.join("; \n *"),
  331. });
  332. return false;
  333. }
  334. return order;
  335. },
  336. // Search Part
  337. search_done_orders: function (query) {
  338. var self = this;
  339. return this._rpc({
  340. model: 'pos.order',
  341. method: 'search_done_orders_for_pos',
  342. args: [query || '', this.pos.pos_session.id],
  343. }).then(function (result) {
  344. self.orders = result;
  345. // Get the date in local time
  346. _.each(self.orders, function (order) {
  347. if (order.date_order) {
  348. order.date_order = moment.utc(order.date_order)
  349. .local().format('YYYY-MM-DD HH:mm:ss');
  350. }
  351. });
  352. }).fail(function (error, event) {
  353. if (parseInt(error.code, 10) === 200) {
  354. // Business Logic Error, not a connection problem
  355. self.gui.show_popup(
  356. 'error-traceback', {
  357. 'title': error.data.message,
  358. 'body': error.data.debug,
  359. }
  360. );
  361. } else {
  362. self.gui.show_popup('error', {
  363. 'title': _t('Connection error'),
  364. 'body': _t(
  365. 'Can not execute this action because the POS' +
  366. ' is currently offline'),
  367. });
  368. }
  369. event.preventDefault();
  370. });
  371. },
  372. perform_search: function () {
  373. var self = this;
  374. return this.search_done_orders(self.search_query)
  375. .done(function () {
  376. self.render_list();
  377. });
  378. },
  379. clear_search: function () {
  380. var self = this;
  381. self.$('.searchbox input')[0].value = '';
  382. self.$('.searchbox input').focus();
  383. self.search_query = false;
  384. self.perform_search();
  385. },
  386. });
  387. gui.define_screen({
  388. name: 'orderlist',
  389. widget: OrderListScreenWidget,
  390. });
  391. var ListOrderButtonWidget = PosBaseWidget.extend({
  392. template: 'ListOrderButtonWidget',
  393. init: function (parent, options) {
  394. var opts = options || {};
  395. this._super(parent, opts);
  396. this.action = opts.action;
  397. this.label = opts.label;
  398. },
  399. button_click: function () {
  400. this.gui.show_screen('orderlist');
  401. },
  402. renderElement: function () {
  403. var self = this;
  404. this._super();
  405. this.$el.click(function () {
  406. self.button_click();
  407. });
  408. },
  409. });
  410. var widgets = chrome.Chrome.prototype.widgets;
  411. widgets.push({
  412. 'name': 'list_orders',
  413. 'widget': ListOrderButtonWidget,
  414. 'prepend': '.pos-rightheader',
  415. 'args': {
  416. 'label': 'All Orders',
  417. },
  418. });
  419. return {
  420. ListOrderButtonWidget: ListOrderButtonWidget,
  421. OrderListScreenWidget: OrderListScreenWidget,
  422. };
  423. });