Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ExportPost.php
Go to the documentation of this file.
1 <?php
7 
10 
12 {
18  public function execute()
19  {
21  $headers = new \Magento\Framework\DataObject(
22  [
23  'code' => __('Code'),
24  'country_name' => __('Country'),
25  'region_name' => __('State'),
26  'tax_postcode' => __('Zip/Post Code'),
27  'rate' => __('Rate'),
28  'zip_is_range' => __('Zip/Post is Range'),
29  'zip_from' => __('Range From'),
30  'zip_to' => __('Range To'),
31  ]
32  );
33  $template = '"{{code}}","{{country_name}}","{{region_name}}","{{tax_postcode}}","{{rate}}"' .
34  ',"{{zip_is_range}}","{{zip_from}}","{{zip_to}}"';
35  $content = $headers->toString($template);
36 
37  $storeTaxTitleTemplate = [];
38  $taxCalculationRateTitleDict = [];
39 
40  foreach ($this->_objectManager->create(
41  \Magento\Store\Model\Store::class
42  )->getCollection()->setLoadDefault(
43  false
44  ) as $store) {
45  $storeTitle = 'title_' . $store->getId();
46  $content .= ',"' . $store->getCode() . '"';
47  $template .= ',"{{' . $storeTitle . '}}"';
48  $storeTaxTitleTemplate[$storeTitle] = null;
49  }
50  unset($store);
51 
52  $content .= "\n";
53 
54  foreach ($this->_objectManager->create(
55  \Magento\Tax\Model\Calculation\Rate\Title::class
56  )->getCollection() as $title) {
57  $rateId = $title->getTaxCalculationRateId();
58 
59  if (!array_key_exists($rateId, $taxCalculationRateTitleDict)) {
60  $taxCalculationRateTitleDict[$rateId] = $storeTaxTitleTemplate;
61  }
62 
63  $taxCalculationRateTitleDict[$rateId]['title_' . $title->getStoreId()] = $title->getValue();
64  }
65  unset($title);
66 
67  $collection = $this->_objectManager->create(
68  \Magento\Tax\Model\ResourceModel\Calculation\Rate\Collection::class
69  )->joinCountryTable()->joinRegionTable();
70 
71  while ($rate = $collection->fetchItem()) {
72  if ($rate->getTaxRegionId() == 0) {
73  $rate->setRegionName('*');
74  }
75 
76  if (array_key_exists($rate->getId(), $taxCalculationRateTitleDict)) {
77  $rate->addData($taxCalculationRateTitleDict[$rate->getId()]);
78  } else {
79  $rate->addData($storeTaxTitleTemplate);
80  }
81 
82  $content .= $rate->toString($template) . "\n";
83  }
84  return $this->fileFactory->create('tax_rates.csv', $content, DirectoryList::VAR_DIR);
85  }
86 
90  protected function _isAllowed()
91  {
92  return $this->_authorization->isAllowed(
93  'Magento_Tax::manage_tax'
94  ) || $this->_authorization->isAllowed(
95  'Magento_TaxImportExport::import_export'
96  );
97  }
98 }
$title
Definition: default.phtml:14
__()
Definition: __.php:13
_isAllowed()
Definition: ExportPost.php:90
execute()
Definition: ExportPost.php:18
Definition: ExportPost.php:11
$template
Definition: export.php:12