quote.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. /**
  6. * @api
  7. */
  8. define([
  9. 'ko',
  10. 'underscore'
  11. ], function (ko, _) {
  12. 'use strict';
  13. /**
  14. * Get totals data from the extension attributes.
  15. * @param {*} data
  16. * @returns {*}
  17. */
  18. var proceedTotalsData = function (data) {
  19. if (_.isObject(data) && _.isObject(data['extension_attributes'])) {
  20. _.each(data['extension_attributes'], function (element, index) {
  21. data[index] = element;
  22. });
  23. }
  24. return data;
  25. },
  26. billingAddress = ko.observable(null),
  27. shippingAddress = ko.observable(null),
  28. shippingMethod = ko.observable(null),
  29. paymentMethod = ko.observable(null),
  30. quoteData = window.checkoutConfig.quoteData,
  31. basePriceFormat = window.checkoutConfig.basePriceFormat,
  32. priceFormat = window.checkoutConfig.priceFormat,
  33. storeCode = window.checkoutConfig.storeCode,
  34. totalsData = proceedTotalsData(window.checkoutConfig.totalsData),
  35. totals = ko.observable(totalsData),
  36. collectedTotals = ko.observable({});
  37. return {
  38. totals: totals,
  39. shippingAddress: shippingAddress,
  40. shippingMethod: shippingMethod,
  41. billingAddress: billingAddress,
  42. paymentMethod: paymentMethod,
  43. guestEmail: null,
  44. /**
  45. * @return {*}
  46. */
  47. getQuoteId: function () {
  48. return quoteData['entity_id'];
  49. },
  50. /**
  51. * @return {Boolean}
  52. */
  53. isVirtual: function () {
  54. return !!Number(quoteData['is_virtual']);
  55. },
  56. /**
  57. * @return {*}
  58. */
  59. getPriceFormat: function () {
  60. return priceFormat;
  61. },
  62. /**
  63. * @return {*}
  64. */
  65. getBasePriceFormat: function () {
  66. return basePriceFormat;
  67. },
  68. /**
  69. * @return {*}
  70. */
  71. getItems: function () {
  72. return window.checkoutConfig.quoteItemData;
  73. },
  74. /**
  75. *
  76. * @return {*}
  77. */
  78. getTotals: function () {
  79. return totals;
  80. },
  81. /**
  82. * @param {Object} data
  83. */
  84. setTotals: function (data) {
  85. data = proceedTotalsData(data);
  86. totals(data);
  87. this.setCollectedTotals('subtotal_with_discount', parseFloat(data['subtotal_with_discount']));
  88. },
  89. /**
  90. * @param {*} paymentMethodCode
  91. */
  92. setPaymentMethod: function (paymentMethodCode) {
  93. paymentMethod(paymentMethodCode);
  94. },
  95. /**
  96. * @return {*}
  97. */
  98. getPaymentMethod: function () {
  99. return paymentMethod;
  100. },
  101. /**
  102. * @return {*}
  103. */
  104. getStoreCode: function () {
  105. return storeCode;
  106. },
  107. /**
  108. * @param {String} code
  109. * @param {*} value
  110. */
  111. setCollectedTotals: function (code, value) {
  112. var colTotals = collectedTotals();
  113. colTotals[code] = value;
  114. collectedTotals(colTotals);
  115. },
  116. /**
  117. * @return {Number}
  118. */
  119. getCalculatedTotal: function () {
  120. var total = 0.; //eslint-disable-line no-floating-decimal
  121. _.each(collectedTotals(), function (value) {
  122. total += value;
  123. });
  124. return total;
  125. }
  126. };
  127. });