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.

116 lines
3.7 KiB

  1. odoo.define('pos_tare.models', function (require) {
  2. "use strict";
  3. var core = require('web.core');
  4. var models = require('point_of_sale.models');
  5. var pos_tare_tools = require('pos_tare.tools');
  6. var _t = core._t;
  7. var _super_ = models.Orderline.prototype;
  8. var OrderLineWithTare = models.Orderline.extend({
  9. // /////////////////////////////
  10. // Overload Section
  11. // /////////////////////////////
  12. initialize: function (session, attributes) {
  13. this.tare = 0;
  14. return _super_.initialize.call(this, session, attributes);
  15. },
  16. init_from_JSON: function (json) {
  17. _super_.init_from_JSON.call(this, json);
  18. this.tare = json.tare ||0;
  19. },
  20. clone: function () {
  21. var orderline = _super_.clone.call(this);
  22. orderline.tare = this.tare;
  23. return orderline;
  24. },
  25. export_as_JSON: function () {
  26. var json = _super_.export_as_JSON.call(this);
  27. json.tare = this.get_tare();
  28. return json;
  29. },
  30. export_for_printing: function () {
  31. var result = _super_.export_for_printing.call(this);
  32. result.tare_quantity = this.get_tare();
  33. result.gross_quantity = this.get_gross_weight();
  34. return result;
  35. },
  36. // /////////////////////////////
  37. // Custom Section
  38. // /////////////////////////////
  39. set_tare: function (quantity, update_net_weight) {
  40. this.order.assert_editable();
  41. // Prevent to apply multiple times a tare to the same product.
  42. if (this.get_tare() > 0) {
  43. // This is valid because the tare is stored using product UOM.
  44. this.set_quantity(this.get_quantity() + this.get_tare());
  45. this.reset_tare();
  46. }
  47. // We convert the tare that is always measured in the same UoM into
  48. // the unit of measure for this order line.
  49. var tare_uom = this.pos.config.iface_tare_uom_id[0];
  50. var tare_unit = this.pos.units_by_id[tare_uom];
  51. var tare = parseFloat(quantity) || 0;
  52. var line_unit = this.get_unit();
  53. var tare_in_product_uom = pos_tare_tools.convert_mass(
  54. tare, tare_unit, line_unit);
  55. var tare_in_product_uom_string = pos_tare_tools.format_tare(
  56. this.pos, tare_in_product_uom, line_unit);
  57. if (update_net_weight) {
  58. var net_quantity = this.get_quantity() - tare_in_product_uom;
  59. // Update the quantity with the new weight net of tare quantity.
  60. this.set_quantity(net_quantity);
  61. }
  62. // Update tare value.
  63. this.tare = tare_in_product_uom;
  64. this.trigger('change', this);
  65. },
  66. reset_tare: function () {
  67. this.tare = 0;
  68. },
  69. get_tare: function () {
  70. return this.tare;
  71. },
  72. get_gross_weight: function () {
  73. return this.get_tare() + this.get_quantity();
  74. },
  75. get_tare_str_with_unit: function () {
  76. var unit = this.get_unit();
  77. var tare_str = pos_tare_tools.format_tare(
  78. this.pos,
  79. this.tare,
  80. this.get_unit()
  81. );
  82. return tare_str + ' ' + unit.name;
  83. },
  84. get_gross_weight_str_with_unit: function () {
  85. var unit = this.get_unit();
  86. var gross_weight_str = pos_tare_tools.format_tare(
  87. this.pos,
  88. this.get_gross_weight(),
  89. this.get_unit()
  90. );
  91. return gross_weight_str + ' ' + unit.name;
  92. },
  93. });
  94. models.Orderline = OrderLineWithTare;
  95. });