Total.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Model\Quote\Address;
  7. /**
  8. * Class Total
  9. *
  10. * @method string getCode()
  11. *
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class Total extends \Magento\Framework\DataObject
  16. {
  17. /**
  18. * @var array
  19. */
  20. protected $totalAmounts = [];
  21. /**
  22. * @var array
  23. */
  24. protected $baseTotalAmounts = [];
  25. /**
  26. * Serializer interface instance.
  27. *
  28. * @var \Magento\Framework\Serialize\Serializer\Json
  29. */
  30. private $serializer;
  31. /**
  32. * Constructor
  33. *
  34. * @param array $data
  35. * @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
  36. */
  37. public function __construct(
  38. array $data = [],
  39. \Magento\Framework\Serialize\Serializer\Json $serializer = null
  40. ) {
  41. $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
  42. ->get(\Magento\Framework\Serialize\Serializer\Json::class);
  43. parent::__construct($data);
  44. }
  45. /**
  46. * Set total amount value
  47. *
  48. * @param string $code
  49. * @param float $amount
  50. * @return $this
  51. */
  52. public function setTotalAmount($code, $amount)
  53. {
  54. $amount = is_float($amount) ? round($amount, 4) : $amount;
  55. $this->totalAmounts[$code] = $amount;
  56. if ($code != 'subtotal') {
  57. $code = $code . '_amount';
  58. }
  59. $this->setData($code, $amount);
  60. return $this;
  61. }
  62. /**
  63. * Set total amount value in base store currency
  64. *
  65. * @param string $code
  66. * @param float $amount
  67. * @return $this
  68. */
  69. public function setBaseTotalAmount($code, $amount)
  70. {
  71. $amount = is_float($amount) ? round($amount, 4) : $amount;
  72. $this->baseTotalAmounts[$code] = $amount;
  73. if ($code != 'subtotal') {
  74. $code = $code . '_amount';
  75. }
  76. $this->setData('base_' . $code, $amount);
  77. return $this;
  78. }
  79. /**
  80. * Add amount total amount value
  81. *
  82. * @param string $code
  83. * @param float $amount
  84. * @return $this
  85. */
  86. public function addTotalAmount($code, $amount)
  87. {
  88. $amount = $this->getTotalAmount($code) + $amount;
  89. $this->setTotalAmount($code, $amount);
  90. return $this;
  91. }
  92. /**
  93. * Add amount total amount value in base store currency
  94. *
  95. * @param string $code
  96. * @param float $amount
  97. * @return $this
  98. */
  99. public function addBaseTotalAmount($code, $amount)
  100. {
  101. $amount = $this->getBaseTotalAmount($code) + $amount;
  102. $this->setBaseTotalAmount($code, $amount);
  103. return $this;
  104. }
  105. /**
  106. * Get total amount value by code
  107. *
  108. * @param string $code
  109. * @return float|int
  110. */
  111. public function getTotalAmount($code)
  112. {
  113. if (isset($this->totalAmounts[$code])) {
  114. return $this->totalAmounts[$code];
  115. }
  116. return 0;
  117. }
  118. /**
  119. * Get total amount value by code in base store currency
  120. *
  121. * @param string $code
  122. * @return float|int
  123. */
  124. public function getBaseTotalAmount($code)
  125. {
  126. if (isset($this->baseTotalAmounts[$code])) {
  127. return $this->baseTotalAmounts[$code];
  128. }
  129. return 0;
  130. }
  131. //@codeCoverageIgnoreStart
  132. /**
  133. * Get all total amount values
  134. *
  135. * @return array
  136. */
  137. public function getAllTotalAmounts()
  138. {
  139. return $this->totalAmounts;
  140. }
  141. /**
  142. * Get all total amount values in base currency
  143. *
  144. * @return array
  145. */
  146. public function getAllBaseTotalAmounts()
  147. {
  148. return $this->baseTotalAmounts;
  149. }
  150. //@codeCoverageIgnoreEnd
  151. /**
  152. * Set the full info, which is used to capture tax related information.
  153. *
  154. * If a string is used, it is assumed to be serialized.
  155. *
  156. * @param array|string $info
  157. * @return $this
  158. * @since 100.1.0
  159. */
  160. public function setFullInfo($info)
  161. {
  162. $this->setData('full_info', $info);
  163. return $this;
  164. }
  165. /**
  166. * Returns the full info, which is used to capture tax related information.
  167. *
  168. * @return array
  169. * @since 100.1.0
  170. */
  171. public function getFullInfo()
  172. {
  173. $fullInfo = $this->getData('full_info');
  174. if (is_string($fullInfo)) {
  175. $fullInfo = $this->serializer->unserialize($fullInfo);
  176. }
  177. return $fullInfo;
  178. }
  179. }