_resourceConfig = $resourceConfig; $this->_taxRateConverter = $taxRateConverter; $this->_taxRateRepository = $taxRateRepository; $this->_taxClassDataObjectFactory = $taxClassDataObjectFactory; $this->_taxClassRepository = $taxClassService; $this->_ruleService = $ruleService; $this->_taxRuleDataObjectFactory = $taxRuleDataObjectFactory; $this->_taxRateDataObjectFactory = $taxRateDataObjectFactory; $this->_state = $state; parent::__construct(); } public function getObjectManager() { return \Magento\Framework\App\ObjectManager::getInstance(); } public function in_array_r($needle, $haystack, $strict = false) { foreach ($haystack as $item) { if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && $this->in_array_r($needle, $item, $strict))) { return true; } } return false; } protected function configure() { $this->setName('wowow:configure')->setDescription('Configure your site.') ->setDefinition([ new InputOption( Options::LANGUE, '-l', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, 'Langue de configuration', ['fr_FR'] ), new InputOption( Options::SCOPE, '-s', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, 'scope stores ou default', ['default'] ), new InputOption( Options::SCOPE_ID, '-i', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, 'Id du store à configurer', ['0'] ), new InputOption( Options::STORE_NAME, null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, 'Store Name', null ), new InputOption( Options::STORE_PHONE_NUMBER, null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, 'Store Phone', null ), new InputOption( Options::STORE_HOURS_OPERATION, null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, 'Store Hours', null ), new InputOption( Options::STORE_COUNTRY, null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, 'Store Country', null ), new InputOption( Options::STORE_REGION, null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, 'Store Region', null ), new InputOption( Options::STORE_CP, null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, 'Store Cp', null ), new InputOption( Options::STORE_CITY, null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, 'Store City', null ), new InputOption( Options::STORE_ADR1, null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, 'Store Adresse 1', null ), new InputOption( Options::STORE_ADR2, null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, 'Store Adresse Compl', null ), new InputOption( Options::STORE_VAT, null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, 'Store VAT', null ), new InputOption( Options::STORE_EMAIL, null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, 'Store Email', null ) ]); } protected function execute(InputInterface $input, OutputInterface $output) { $this->input = $input; $langue = $this->input->getOption(Options::LANGUE)[0]; $scope = $this->input->getOption(Options::SCOPE)[0]; $scopeId = $this->input->getOption(Options::SCOPE_ID)[0]; $storeInformation = array( array('general/store_information/name',$storeName = (isset($this->input->getOption(Options::STORE_NAME)[0])) ? $this->input->getOption(Options::STORE_NAME)[0] : null,'nom du store ? '), array('general/store_information/phone',$storePhoneNumber = (isset($this->input->getOption(Options::STORE_PHONE_NUMBER)[0])) ? $this->input->getOption(Options::STORE_PHONE_NUMBER)[0] : null,'numéro de téléphone ? '), array('general/store_information/hours',$storeHoursOperation = (isset($this->input->getOption(Options::STORE_HOURS_OPERATION)[0])) ? $this->input->getOption(Options::STORE_HOURS_OPERATION)[0] : null,'heures d\'opération ? '), array('general/store_information/country_id',$storeCountry = (isset($this->input->getOption(Options::STORE_COUNTRY)[0])) ? $this->input->getOption(Options::STORE_COUNTRY)[0] : null,'pays ? '), array('general/store_information/region_id',$storeRegion = (isset($this->input->getOption(Options::STORE_REGION)[0])) ? $this->input->getOption(Options::STORE_REGION)[0] : null,'region ? '), array('general/store_information/postcode',$storeCp = (isset($this->input->getOption(Options::STORE_CP)[0])) ? $this->input->getOption(Options::STORE_CP)[0] : null,'cp ? '), array('general/store_information/city',$storeCity = (isset($this->input->getOption(Options::STORE_CITY)[0])) ? $this->input->getOption(Options::STORE_CITY)[0] : null,'ville ? '), array('general/store_information/street_line1',$storeAdr1 = (isset($this->input->getOption(Options::STORE_ADR1)[0])) ? $this->input->getOption(Options::STORE_ADR1)[0] : null,'adresse ? '), array('general/store_information/street_line2',$storeAdr2 = (isset($this->input->getOption(Options::STORE_ADR2)[0])) ? $this->input->getOption(Options::STORE_ADR2)[0] : null,'complément d\'adresse ? '), array('general/store_information/merchant_vat_number',$storeVat = (isset($this->input->getOption(Options::STORE_VAT)[0])) ? $this->input->getOption(Options::STORE_VAT)[0] : null,'numéro de TVA ? '), array('trans_email/ident_general/email',$email = (isset($this->input->getOption(Options::STORE_EMAIL)[0])) ? $this->input->getOption(Options::STORE_EMAIL)[0] : null,'email générale ? '), array('trans_email/ident_sales/email',$email = (isset($this->input->getOption(Options::STORE_EMAIL)[0])) ? $this->input->getOption(Options::STORE_EMAIL)[0] : null,'email de vente ? '), array('trans_email/ident_support/email',$email = (isset($this->input->getOption(Options::STORE_EMAIL)[0])) ? $this->input->getOption(Options::STORE_EMAIL)[0] : null,'email de support ? '), array('trans_email/ident_custom1/email',$email = (isset($this->input->getOption(Options::STORE_EMAIL)[0])) ? $this->input->getOption(Options::STORE_EMAIL)[0] : null,'email custom 1 ? '), array('trans_email/ident_custom2/email',$email = (isset($this->input->getOption(Options::STORE_EMAIL)[0])) ? $this->input->getOption(Options::STORE_EMAIL)[0] : null,'email custom 2 ? '), array('contact/email/recipient_email',$email = (isset($this->input->getOption(Options::STORE_EMAIL)[0])) ? $this->input->getOption(Options::STORE_EMAIL)[0] : null,'email de contact ? ') ); $varEmail = array('trans_email/ident_general/email','trans_email/ident_sales/email','trans_email/ident_support/email','trans_email/ident_custom1/email','trans_email/ident_custom2/email','contact/email/recipient_email'); $emailGenerale = false; if($this->in_array_r(null,$storeInformation)){ $helper = $this->getHelper('question'); $question = new Question('Voulez vous rentrez plus d\'options ? (Y/f)','f'); $ajoutOption = $helper->ask($input,$output,$question); } foreach($storeInformation as $item){ if($item[1]){ $this->_resourceConfig->saveConfig($item[0],$item[1],$scope,$scopeId); if($item[0] == 'trans_email/ident_general/email'){ $emailGenerale = $item[1]; } }else if($ajoutOption == 'Y'){ $question = new Question($item[2],false); $valOption = $helper->ask($input,$output,$question); if(in_array($item[0],$varEmail)){ if($valOption){ if($item[0] == 'trans_email/ident_general/email'){ $emailGenerale = $valOption; } $this->_resourceConfig->saveConfig($item[0],$valOption,$scope,$scopeId); }else if($emailGenerale){ $this->_resourceConfig->saveConfig($item[0],$emailGenerale,$scope,$scopeId); } }else{ $this->_resourceConfig->saveConfig($item[0],$valOption,$scope,$scopeId); } } } if($langue == 'fr_FR'){ $this->_resourceConfig->saveConfig('general/country/default','FR', $scope,$scopeId); $this->_resourceConfig->saveConfig('general/country/destinations','FR', $scope,$scopeId); $this->_resourceConfig->saveConfig('general/locale/timezone','Europe/Paris', $scope,$scopeId); $this->_resourceConfig->saveConfig('general/locale/code',$langue, $scope,$scopeId); $this->_resourceConfig->saveConfig('general/locale/weight_unit','kgs', $scope,$scopeId); $this->_resourceConfig->saveConfig('general/locale/firstday',1,$scope,$scopeId); if(!$storeCountry) $this->_resourceConfig->saveConfig('general/store_information/country_id','FR',$scope,$scopeId); $this->_resourceConfig->saveConfig('currency/options/base','EUR',$scope,$scopeId); $this->_resourceConfig->saveConfig('currency/options/default','EUR',$scope,$scopeId); $this->_resourceConfig->saveConfig('currency/options/allow','EUR',$scope,$scopeId); $this->_resourceConfig->saveConfig('tax/defaults/country','FR',$scope,$scopeId); $this->_resourceConfig->saveConfig('tax/calculation/based_on','origin',$scope,$scopeId); $this->_resourceConfig->saveConfig('shipping/origin/country_id','FR',$scope,$scopeId); $this->_resourceConfig->saveConfig('shipping/origin/region_id',null,$scope,$scopeId); $this->_resourceConfig->saveConfig('shipping/origin/postcode',null,$scope,$scopeId); $objectManager = $this->getObjectManager(); $rateModel = $objectManager->create('Magento\Tax\Model\TaxRateCollection'); $bool20 = false; $bool5 = false; $bool10 = false; foreach($rateModel->loadData() as $item){ if(($item->getRate() == '20') && ($item->getTaxCountryId() == 'FR')){ $bool20 = true; } if(($item->getRate() == '5.5') && ($item->getTaxCountryId() == 'FR')){ $bool5 = true; } if(($item->getRate() == '10') && ($item->getTaxCountryId() == 'FR')){ $bool10 = true; } } $dataRate = array(); $this->_state->setAreaCode('adminhtml'); if(!$bool20){ array_push($dataRate,array('form_key'=> $objectManager->get('Magento\Framework\Data\Form\FormKey')->getFormKey(),'code'=>'TVA-FR-20','tax_postcode'=>'*','zip_from'=>'','zip_to'=>'','tax_region_id'=>'','tax_country_id'=>'FR','rate'=>'20')); } if(!$bool10){ array_push($dataRate,array('form_key'=> $objectManager->get('Magento\Framework\Data\Form\FormKey')->getFormKey(),'code'=>'TVA-FR-10','tax_postcode'=>'*','zip_from'=>'','zip_to'=>'','tax_region_id'=>'','tax_country_id'=>'FR','rate'=>'10')); } if(!$bool5){ array_push($dataRate,array('form_key'=> $objectManager->get('Magento\Framework\Data\Form\FormKey')->getFormKey(),'code'=>'TVA-FR-5','tax_postcode'=>'*','zip_from'=>'','zip_to'=>'','tax_region_id'=>'','tax_country_id'=>'FR','rate'=>'5.5')); } $defaultTaxClassProduct = null; foreach($dataRate as $rate) { $taxData = $this->_taxRateDataObjectFactory->create(); $taxData->setTaxCountryId($rate['tax_country_id']) ->setTaxRegionId($rate['tax_region_id']) ->setTaxPostcode($rate['tax_postcode']) ->setZipTo($rate['zip_to']) ->setZipFrom($rate['zip_from']) ->setCode($rate['code']) ->setRate($rate['rate']); $rateId = $this->_taxRateRepository->save($taxData); $rateId = $taxData->getId(); $taxClass = $this->_taxClassDataObjectFactory->create() ->setClassId(null) ->setClassType('PRODUCT') ->setClassName($rate['code']); $taxClassId = $this->_taxClassRepository->save($taxClass); if($rate['code'] == 'TVA-FR-20'){ $defaultTaxClassProduct = $taxClassId; } $objectManager = $this->getObjectManager(); $rateModel = $objectManager->create('Magento\Tax\Model\ClassModel'); $taxCustomerId = $rateModel->getCollection()->addFieldToFilter('class_type','CUSTOMER')->getFirstItem()->getId(); $taxRule = $this->_taxRuleDataObjectFactory->create(); $taxRule->setCode($rate['code']); $taxRule->setTaxRateIds(array($rateId)); $taxRule->setCustomerTaxClassIds(array($taxCustomerId)); $taxRule->setProductTaxClassIds(array($defaultTaxClassProduct)); $taxRule->setPriority("0"); $taxRule->setPosition("0"); $taxRule = $this->_ruleService->save($taxRule); } if($defaultTaxClassProduct) $this->_resourceConfig->saveConfig('tax/classes/default_product_tax_class',$defaultTaxClassProduct,$scope,$scopeId); }else{ $output->writeln('juste fr_FR pour l\'instant'); } $command = $this->getApplication()->find('cache:clean'); $returnCode = $command->run(new ArrayInput(array('command' => 'cache:clean')),$output); if(!$returnCode) $output->writeln('successfully finished'); } }