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.

62 lines
2.0 KiB

  1. odoo.define('pos_tare.tools', function (require) {
  2. "use strict";
  3. var core = require('web.core');
  4. var utils = require('web.utils');
  5. var field_utils = require('web.field_utils');
  6. var _t = core._t;
  7. var round_pr = utils.round_precision;
  8. var round_di = utils.round_decimals;
  9. // Convert mass using the reference UOM as pivot unit.
  10. var convert_mass = function (mass, from_unit, to_unit) {
  11. // There is no conversion from one category to another.
  12. if (from_unit.category_id[0] !== to_unit.category_id[0]) {
  13. throw new Error(_.str.sprintf(
  14. _t("We can not cast a weight in %s into %s."),
  15. from_unit.name, to_unit.name));
  16. }
  17. // No need to convert as weights are measured in same unit.
  18. if (from_unit.id === to_unit.id) {
  19. return mass;
  20. }
  21. // Converts "from_unit" to reference unit of measure.
  22. var result = mass;
  23. if (from_unit.uom_type === "bigger") {
  24. result /= from_unit.factor;
  25. } else {
  26. result *= from_unit.factor_inv;
  27. }
  28. // Converts reference unit of measure to "to_unit".
  29. if (to_unit.uom_type === "bigger") {
  30. result *= to_unit.factor;
  31. } else {
  32. result /= to_unit.factor_inv;
  33. }
  34. if (to_unit.rounding) {
  35. // Return the rounded result if needed.
  36. return round_pr(result || 0, to_unit.rounding);
  37. }
  38. return result || 0;
  39. };
  40. // Format the tare value.
  41. var format_tare = function (pos, qty, unit) {
  42. if (unit.rounding) {
  43. var q = round_pr(qty, unit.rounding);
  44. var decimals = pos.dp['Product Unit of Measure'];
  45. return field_utils.format.float(
  46. round_di(q, decimals),
  47. {type: 'float', digits: [69, decimals]});
  48. }
  49. return qty.toFixed(0);
  50. };
  51. return {
  52. convert_mass: convert_mass,
  53. format_tare: format_tare,
  54. };
  55. });