Converter.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Model\Calculation\Rate;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Framework\Locale\FormatInterface;
  9. /**
  10. * Tax Rate Model converter.
  11. *
  12. * Converts a Tax Rate Model to a Data Object or vice versa.
  13. */
  14. class Converter
  15. {
  16. /**
  17. * @var \Magento\Tax\Api\Data\TaxRateInterfaceFactory
  18. */
  19. protected $taxRateDataObjectFactory;
  20. /**
  21. * @var \Magento\Tax\Api\Data\TaxRateTitleInterfaceFactory
  22. */
  23. protected $taxRateTitleDataObjectFactory;
  24. /**
  25. * @var FormatInterface|null
  26. */
  27. private $format;
  28. /**
  29. * @param \Magento\Tax\Api\Data\TaxRateInterfaceFactory $taxRateDataObjectFactory
  30. * @param \Magento\Tax\Api\Data\TaxRateTitleInterfaceFactory $taxRateTitleDataObjectFactory
  31. * @param FormatInterface|null $format
  32. */
  33. public function __construct(
  34. \Magento\Tax\Api\Data\TaxRateInterfaceFactory $taxRateDataObjectFactory,
  35. \Magento\Tax\Api\Data\TaxRateTitleInterfaceFactory $taxRateTitleDataObjectFactory,
  36. FormatInterface $format = null
  37. ) {
  38. $this->taxRateDataObjectFactory = $taxRateDataObjectFactory;
  39. $this->taxRateTitleDataObjectFactory = $taxRateTitleDataObjectFactory;
  40. $this->format = $format ?: ObjectManager::getInstance()->get(FormatInterface::class);
  41. }
  42. /**
  43. * Convert a tax rate data object to an array of associated titles
  44. *
  45. * @param \Magento\Tax\Api\Data\TaxRateInterface $taxRate
  46. * @return array
  47. */
  48. public function createTitleArrayFromServiceObject(\Magento\Tax\Api\Data\TaxRateInterface $taxRate)
  49. {
  50. $titles = $taxRate->getTitles();
  51. $titleData = [];
  52. if ($titles) {
  53. foreach ($titles as $title) {
  54. $titleData[$title->getStoreId()] = $title->getValue();
  55. }
  56. }
  57. return $titleData;
  58. }
  59. /**
  60. * Extract tax rate data in a format which is
  61. *
  62. * @param \Magento\Tax\Api\Data\TaxRateInterface $taxRate
  63. * @param Boolean $returnNumericLogic
  64. * @return array
  65. * @SuppressWarnings(PHPMD.NPathComplexity)
  66. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  67. */
  68. public function createArrayFromServiceObject(
  69. \Magento\Tax\Api\Data\TaxRateInterface $taxRate,
  70. $returnNumericLogic = false
  71. ) {
  72. $taxRateFormData = [
  73. 'tax_calculation_rate_id' => $taxRate->getId(),
  74. 'tax_country_id' => $taxRate->getTaxCountryId(),
  75. 'tax_region_id' => $taxRate->getTaxRegionId(),
  76. 'tax_postcode' => $taxRate->getTaxPostcode(),
  77. 'code' => $taxRate->getCode(),
  78. 'rate' => $taxRate->getRate(),
  79. 'zip_is_range' => $returnNumericLogic ? 0 : false,
  80. ];
  81. if ($taxRateFormData['tax_region_id'] === '0') {
  82. $taxRateFormData['tax_region_id'] = '';
  83. }
  84. if ($taxRate->getZipFrom() && $taxRate->getZipTo()) {
  85. $taxRateFormData['zip_is_range'] = $returnNumericLogic ? 1 : true;
  86. $taxRateFormData['zip_from'] = $taxRate->getZipFrom();
  87. $taxRateFormData['zip_to'] = $taxRate->getZipTo();
  88. }
  89. if ($returnNumericLogic) {
  90. //format for the ajax on multiple sites titles
  91. $titleArray=($this->createTitleArrayFromServiceObject($taxRate));
  92. if (is_array($titleArray)) {
  93. foreach ($titleArray as $storeId => $title) {
  94. $taxRateFormData['title[' . $storeId . ']']=$title;
  95. }
  96. }
  97. } else {
  98. //format for the form array on multiple sites titles
  99. $titleArray=($this->createTitleArrayFromServiceObject($taxRate));
  100. if (is_array($titleArray)) {
  101. $titleData = [];
  102. foreach ($titleArray as $storeId => $title) {
  103. $titleData[] = [$storeId => $title];
  104. }
  105. if (count($titleArray)>0) {
  106. $taxRateFormData['title'] = $titleData;
  107. }
  108. }
  109. }
  110. return $taxRateFormData;
  111. }
  112. /**
  113. * Convert an array to a tax rate data object
  114. *
  115. * @param array $formData
  116. * @return \Magento\Tax\Api\Data\TaxRateInterface
  117. */
  118. public function populateTaxRateData($formData)
  119. {
  120. $taxRate = $this->taxRateDataObjectFactory->create();
  121. $taxRate->setId($this->extractFormData($formData, 'tax_calculation_rate_id'))
  122. ->setTaxCountryId($this->extractFormData($formData, 'tax_country_id'))
  123. ->setTaxRegionId($this->extractFormData($formData, 'tax_region_id'))
  124. ->setTaxPostcode($this->extractFormData($formData, 'tax_postcode'))
  125. ->setCode($this->extractFormData($formData, 'code'))
  126. ->setRate($this->format->getNumber($this->extractFormData($formData, 'rate')));
  127. if (isset($formData['zip_is_range']) && $formData['zip_is_range']) {
  128. $taxRate->setZipFrom($this->extractFormData($formData, 'zip_from'))
  129. ->setZipTo($this->extractFormData($formData, 'zip_to'))->setZipIsRange(1);
  130. }
  131. if (isset($formData['title'])) {
  132. $titles = [];
  133. foreach ($formData['title'] as $storeId => $value) {
  134. $titles[] = $this->taxRateTitleDataObjectFactory->create()->setStoreId($storeId)->setValue($value);
  135. }
  136. $taxRate->setTitles($titles);
  137. }
  138. return $taxRate;
  139. }
  140. /**
  141. * Determines if an array value is set in the form data array and returns it.
  142. *
  143. * @param array $formData the form to get data from
  144. * @param string $fieldName the key
  145. * @return null|string
  146. */
  147. protected function extractFormData($formData, $fieldName)
  148. {
  149. if (isset($formData[$fieldName])) {
  150. return $formData[$fieldName];
  151. }
  152. return null;
  153. }
  154. }