formatter.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. sap.ui.define([
  2. "sap/ui/model/type/Currency"
  3. ], function (Currency) {
  4. "use strict";
  5. return {
  6. /**
  7. * Rounds the currency value to 2 digits
  8. *
  9. * @public
  10. * @param {string} sValue value to be formatted
  11. * @returns {string} formatted currency value with 2 digits
  12. */
  13. currencyValue: function (sValue) {
  14. if (!sValue) {
  15. return "";
  16. }
  17. return parseFloat(sValue).toFixed(2);
  18. },
  19. /**
  20. * Rounds the currency value to 2 digits
  21. *
  22. * @public
  23. * @param {number} iQuantity product quantity
  24. * @param {number} fPrice product price
  25. * @param {string} sCurrencyCode currency code for the price
  26. * @returns {string} formatted currency value with 2 digits
  27. */
  28. calculateItemTotal: function (iQuantity, fPrice, sCurrencyCode) {
  29. var oCurrency = new Currency({showMeasure: false});
  30. var fTotal = iQuantity * fPrice;
  31. return oCurrency.formatValue([fTotal.toFixed(2), sCurrencyCode], "string");
  32. },
  33. /**
  34. * Converts a binary string into an image format suitable for the src attribute
  35. *
  36. * @public
  37. * @param {string} vData a binary string representing the image data
  38. * @returns {string} formatted string with image metadata based on the input or a default image when the input is empty
  39. */
  40. handleBinaryContent: function(vData){
  41. if (vData) {
  42. var sMetaData1 = 'data:image/jpeg;base64,';
  43. var sMetaData2 = vData.substr(104); // stripping the first 104 bytes from the binary data when using base64 encoding.
  44. return sMetaData1 + sMetaData2;
  45. } else {
  46. return "../images/Employee.png";
  47. }
  48. },
  49. /**
  50. * Provides a text to indicate the delivery status based on shipped and required dates
  51. *
  52. * @public
  53. * @param {object} oRequiredDate required date of the order
  54. * @param {object} oShippedDate shipped date of the order
  55. * @returns {string} delivery status text from the resource bundle
  56. */
  57. deliveryText: function (oRequiredDate, oShippedDate) {
  58. var oResourceBundle = this.getModel("i18n").getResourceBundle();
  59. if (oShippedDate === null) {
  60. return "None";
  61. }
  62. // delivery is urgent (takes more than 7 days)
  63. if (oRequiredDate - oShippedDate > 0 && oRequiredDate - oShippedDate <= 432000000) {
  64. return oResourceBundle.getText("formatterDeliveryUrgent");
  65. } else if (oRequiredDate < oShippedDate) { //d elivery is too late
  66. return oResourceBundle.getText("formatterDeliveryTooLate");
  67. } else { // delivery is in time
  68. return oResourceBundle.getText("formatterDeliveryInTime");
  69. }
  70. },
  71. /**
  72. * Provides a semantic state to indicate the delivery status based on shipped and required dates
  73. *
  74. * @public
  75. * @param {object} oRequiredDate required date of the order
  76. * @param {object} oShippedDate shipped date of the order
  77. * @returns {string} semantic state of the order
  78. */
  79. deliveryState: function (oRequiredDate, oShippedDate) {
  80. if (oShippedDate === null) {
  81. return "None";
  82. }
  83. // delivery is urgent (takes more than 7 days)
  84. if (oRequiredDate - oShippedDate > 0 && oRequiredDate - oShippedDate <= 432000000) {
  85. return "Warning";
  86. } else if (oRequiredDate < oShippedDate) { // delivery is too late
  87. return "Error";
  88. } else { // delivery is in time
  89. return "Success";
  90. }
  91. }
  92. };
  93. });