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.

366 lines
14 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. odoo.define('pos_barcode_tare.screens', function (require) {
  2. "use strict";
  3. var core = require('web.core');
  4. var gui = require('point_of_sale.gui');
  5. var models = require('point_of_sale.models');
  6. var screens = require('point_of_sale.screens');
  7. var utils = require('web.utils');
  8. var field_utils = require('web.field_utils');
  9. var QWeb = core.qweb;
  10. var _t = core._t;
  11. var round_pr = utils.round_precision;
  12. var round_di = utils.round_decimals;
  13. var tare_barcode_type = "tare";
  14. // Define functions used to do unit operation.
  15. // Get unit search for unit based on unit name.
  16. var get_unit = function (pos, unit_name) {
  17. return pos.units.filter(
  18. function (u) {
  19. return u.name === unit_name;
  20. })[0];
  21. };
  22. // Convert mass using the reference UOM as pivot unit.
  23. var convert_mass = function (mass, from_unit, to_unit) {
  24. // There is no conversion from one category to another.
  25. if (from_unit.category_id[0] !== to_unit.category_id[0]) {
  26. throw new Error(_.str.sprintf(
  27. _t("We can not cast a weight in %s into %s."),
  28. from_unit.name, to_unit.name));
  29. }
  30. // No need to convert as weights are measured in same unit.
  31. if (from_unit.id === to_unit.id) {
  32. return mass;
  33. }
  34. // Converts "from_unit" to reference unit of measure.
  35. var result = mass;
  36. if (from_unit.uom_type === "bigger") {
  37. result /= from_unit.factor;
  38. } else {
  39. result *= from_unit.factor_inv;
  40. }
  41. // Converts reference unit of measure to "to_unit".
  42. if (to_unit.uom_type === "bigger") {
  43. result *= to_unit.factor;
  44. } else {
  45. result /= to_unit.factor_inv;
  46. }
  47. if (to_unit.rounding) {
  48. // Return the rounded result if needed.
  49. return round_pr(result || 0, to_unit.rounding);
  50. }
  51. return result || 0;
  52. };
  53. // Format the tare value.
  54. var format_tare = function (pos, qty, unit) {
  55. if (unit.rounding) {
  56. var q = round_pr(qty, unit.rounding);
  57. var decimals = pos.dp['Product Unit of Measure'];
  58. return field_utils.format.float(
  59. round_di(q, decimals),
  60. {type: 'float', digits: [69, decimals]});
  61. }
  62. return qty.toFixed(0);
  63. };
  64. // This configures read action for tare barcode. A tare barcode contains a
  65. // fake product ID and the weight to be subtracted from the product in the
  66. // latest order line.
  67. screens.ScreenWidget.include(
  68. {
  69. barcode_tare_action: function (code) {
  70. try {
  71. var order = this.pos.get_order();
  72. var selected_order_line = order.get_selected_orderline();
  73. var tare_weight = code.value;
  74. selected_order_line.set_tare(tare_weight);
  75. } catch (error) {
  76. var title = _t("We can not apply this tare barcode.");
  77. var popup = {title: title, body: error.message};
  78. this.gui.show_popup('error', popup);
  79. }
  80. },
  81. // Setup the callback action for the "weight" barcodes.
  82. show: function () {
  83. this._super();
  84. this.pos.barcode_reader.set_action_callback(
  85. 'tare',
  86. _.bind(this.barcode_tare_action, this));
  87. },
  88. });
  89. // This create a new button on top of action widget. This button links to
  90. // the barcode label printing screen defined below.
  91. var TareScreenButton = screens.ActionButtonWidget.extend({
  92. template: 'TareScreenButton',
  93. button_click: function () {
  94. this.gui.show_screen('tare');
  95. },
  96. });
  97. screens.define_action_button({
  98. 'name': 'tareScreenButton',
  99. 'widget': TareScreenButton,
  100. });
  101. // This is a new screen that reads weight from the electronic scale and
  102. // create a barcode label encoding the weight. The screen shows a preview
  103. // of the label. The user is expected to check if the preview matches what's
  104. // measured on the scale. The barcode image is generated by the report
  105. // module.
  106. var TareScreenWidget = screens.ScreenWidget.extend({
  107. template: 'TareScreenWidget',
  108. next_screen: 'products',
  109. previous_screen: 'products',
  110. default_tare_value: 0.0,
  111. weight_barcode_prefix: null,
  112. show: function () {
  113. this._super();
  114. // Fetch the unit of measure used to save the tare
  115. this.kg_unit = get_unit(this.pos, "kg");
  116. // Fetch the barcode prefix from POS barcode parser rules.
  117. this.weight_barcode_prefix = this.get_barcode_prefix();
  118. // Setup the proxy
  119. var queue = this.pos.proxy_queue;
  120. // The pooling of the scale starts here.
  121. var self = this;
  122. queue.schedule(function () {
  123. return self.pos.proxy.scale_read().then(function (weight) {
  124. try {
  125. self.set_weight(weight);
  126. } catch (error) {
  127. var title = _t("Failed to read weight from scale.");
  128. var popup = {title: title, body: error.message};
  129. self.gui.show_popup('error', popup);
  130. }
  131. });
  132. }, {duration:150, repeat: true});
  133. // Shows a barcode whose weight might be zero, but this is preferred
  134. // for UI/UX reasons.
  135. this.render_receipt();
  136. this.lock_screen(true);
  137. },
  138. get_barcode_prefix: function () {
  139. var barcode_pattern = this.get_barcode_pattern();
  140. return barcode_pattern.substr(0, 2);
  141. },
  142. get_barcode_pattern: function () {
  143. var rules = this.get_barcode_rules();
  144. var rule = rules.filter(
  145. function (r) {
  146. // We select the first (smallest sequence ID) barcode rule
  147. // with the expected type.
  148. return r.type === tare_barcode_type;
  149. })[0];
  150. return rule.pattern;
  151. },
  152. get_barcode_rules: function () {
  153. return this.pos.barcode_reader.barcode_parser.nomenclature.rules;
  154. },
  155. set_weight: function (scale_measure) {
  156. var weight = scale_measure.weight;
  157. var unit = get_unit(this.pos, scale_measure.unit);
  158. if (typeof unit === 'undefined') {
  159. throw new Error(_.str.sprintf(
  160. _t("The scale sent a measure in %s unit. This unit of "+
  161. "measure (UOM) in not found in the point of sale. You " +
  162. "may need to create a new UOM named %s. The UOM name is "+
  163. "case sensitive."), scale_measure.unit,
  164. scale_measure.unit));
  165. }
  166. if (weight > 0) {
  167. this.weight_in_kg = convert_mass(weight, unit, this.kg_unit);
  168. this.render_receipt();
  169. this.lock_screen(false);
  170. }
  171. },
  172. get_weight: function () {
  173. if (typeof this.weight_in_kg === 'undefined') {
  174. return this.default_tare_value;
  175. }
  176. return this.weight_in_kg;
  177. },
  178. barcode_data: function (weight) {
  179. // We use EAN13 barcode, it looks like 07 00000 12345 x. First there
  180. // is the prefix, here 07, that is used to decide which type of
  181. // barcode we're dealing with. A weight barcode has then two groups
  182. // of five digits. The first group encodes the product id. Here the
  183. // product id is 00000. The second group encodes the weight in
  184. // grams. Here the weight is 12.345kg. The last digit of the barcode
  185. // is a checksum, here symbolized by x.
  186. var padding_size = 5;
  187. var void_product_id = '0'.repeat(padding_size);
  188. var weight_in_gram = weight * 1e3;
  189. if (weight_in_gram >= Math.pow(10, padding_size)) {
  190. throw new RangeError(_t("Maximum tare weight is 99.999kg"));
  191. }
  192. // Weight has to be padded with zeros.
  193. var weight_with_padding = '0'.repeat(padding_size) + weight_in_gram;
  194. var padded_weight = weight_with_padding.substr(
  195. weight_with_padding.length - padding_size);
  196. // Builds the barcode using a placeholder checksum.
  197. var barcode = this.weight_barcode_prefix
  198. .concat(void_product_id, padded_weight)
  199. .concat(0);
  200. // Compute checksum
  201. var barcode_parser = this.pos.barcode_reader.barcode_parser;
  202. var checksum = barcode_parser.ean_checksum(barcode);
  203. // Replace checksum placeholder by the actual checksum.
  204. return barcode.substr(0, 12).concat(checksum);
  205. },
  206. get_barcode_data: function () {
  207. return this.barcode_data(this.get_weight());
  208. },
  209. lock_screen: function (locked) {
  210. this._locked = locked;
  211. if (locked) {
  212. this.$('.print-label').addClass('disabled');
  213. } else {
  214. this.$('.print-label').removeClass('disabled');
  215. }
  216. },
  217. print_web: function () {
  218. window.print();
  219. // TODO check this
  220. this.pos.get_order()._printed = true;
  221. },
  222. print: function () {
  223. // See comment in print function of ReceiptScreenWidget
  224. this.lock_screen(true);
  225. var self = this;
  226. setTimeout(function () {
  227. self.lock_screen(false);
  228. }, 1000);
  229. this.print_web();
  230. this.click_back();
  231. },
  232. click_back: function () {
  233. this.close();
  234. this.gui.show_screen(this.previous_screen);
  235. },
  236. renderElement: function () {
  237. this._super();
  238. var self = this;
  239. this.$('.back').click(function () {
  240. self.click_back();
  241. });
  242. this.$('.print-label').click(function () {
  243. if (!self._locked) {
  244. self.print();
  245. }
  246. });
  247. },
  248. render_receipt: function () {
  249. this.$('.pos-tare-label-container').html(
  250. QWeb.render('PosTareLabel', {widget:this}));
  251. },
  252. close: function () {
  253. this._super();
  254. delete this.weight;
  255. this.pos.proxy_queue.clear();
  256. },
  257. get_tare_str: function () {
  258. return format_tare(this.pos, this.get_weight(),
  259. get_unit(this.pos, "kg"));
  260. },
  261. });
  262. gui.define_screen({name:'tare', widget: TareScreenWidget});
  263. // Update Orderline model
  264. var _super_ = models.Orderline.prototype;
  265. var OrderLineWithTare = models.Orderline.extend({
  266. initialize: function (session, attributes) {
  267. this.tareQuantity = 0;
  268. this.tareQuantityStr = '0';
  269. return _super_.initialize.call(this, session, attributes);
  270. },
  271. init_from_JSON: function (json) {
  272. _super_.init_from_JSON.call(this, json);
  273. this.tareQuantity = json.tareQuantity ||0;
  274. this.tareQuantityStr = json.tareQuantityStr ||'0';
  275. },
  276. set_tare: function (quantity) {
  277. this.order.assert_editable();
  278. // Prevent to apply multiple times a tare to the same product.
  279. if (this.get_tare() > 0) {
  280. throw new RangeError(_.str.sprintf(
  281. _t("The tare (%s) is already set for the " +
  282. "product \"%s\". We can not re-apply a tare to this " +
  283. "product."),
  284. this.get_tare_str_with_unit(), this.product.display_name));
  285. }
  286. // We convert the tare that is always measured in kilogrammes into
  287. // the unit of measure for this order line.
  288. var kg = get_unit(this.pos, "kg");
  289. var tare = parseFloat(quantity) || 0;
  290. var unit = this.get_unit();
  291. var tare_in_product_uom = convert_mass(tare, kg, unit);
  292. var tare_in_product_uom_string = format_tare(this.pos,
  293. tare_in_product_uom, unit);
  294. var net_quantity = this.get_quantity() - tare_in_product_uom;
  295. // This method fails when the net weight is negative.
  296. if (net_quantity <= 0) {
  297. throw new RangeError(_.str.sprintf(
  298. _t("The tare weight is %s %s, it's greater or equal to " +
  299. "the product weight %s. We can not apply this tare."),
  300. tare_in_product_uom_string, unit.name,
  301. this.get_quantity_str_with_unit()));
  302. }
  303. // Update tare value.
  304. this.tareQuantity = tare_in_product_uom;
  305. this.tareQuantityStr = tare_in_product_uom_string;
  306. // Update the quantity with the new weight net of tare quantity.
  307. this.set_quantity(net_quantity);
  308. this.trigger('change', this);
  309. },
  310. get_tare: function () {
  311. return this.tareQuantity;
  312. },
  313. get_tare_str: function () {
  314. return this.tareQuantityStr;
  315. },
  316. get_tare_str_with_unit: function () {
  317. var unit = this.get_unit();
  318. return this.tareQuantityStr + ' ' + unit.name;
  319. },
  320. export_as_JSON: function () {
  321. var json = _super_.export_as_JSON.call(this);
  322. json.tareQuantity = this.get_tare();
  323. json.tareQuantityStr = this.get_tare_str();
  324. return json;
  325. },
  326. clone: function () {
  327. var orderline = _super_.clone.call(this);
  328. orderline.tareQuantity = this.tareQuantity;
  329. orderline.tareQuantityStr = this.tareQuantityStr;
  330. return orderline;
  331. },
  332. export_for_printing: function () {
  333. var result = _super_.export_for_printing.call(this);
  334. result.tare_quantity = this.get_tare();
  335. return result;
  336. },
  337. });
  338. models.Orderline = OrderLineWithTare;
  339. return {TareScreenWidget: TareScreenWidget,
  340. OrderLineWithTare: OrderLineWithTare,
  341. get_unit: get_unit};
  342. });