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.

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