/** * {@inheritdoc} */ public function save(\Magento\Tax\Api\Data\TaxClassInterface $taxClass) { if ($taxClass->getClassId()) { $originalTaxClassModel = $this->get($taxClass->getClassId()); /* should not be allowed to switch the tax class type */ if ($originalTaxClassModel->getClassType() !== $taxClass->getClassType()) { throw new InputException(__('Updating classType is not allowed.')); } } $this->validateTaxClassData($taxClass); try { $this->taxClassResource->save($taxClass); } catch (ModelException $e) { if (strpos($e->getMessage(), (string) __('Class name and class type')) !== false) { throw new InputException(__('A class with the same name already exists for ClassType %1.', $taxClass->getClassType())); } else { throw $e; } } $this->classModelRegistry->registerTaxClass($taxClass); return $taxClass->getClassId(); }
/** * {@inheritdoc} */ public function updateTaxClass($taxClassId, TaxClassDataObject $taxClass) { if ($taxClass->getClassId()) { throw new InputException(self::CLASS_ID_NOT_ALLOWED); } $this->validateTaxClassData($taxClass); if (!$taxClassId) { throw InputException::invalidFieldValue('taxClassId', $taxClassId); } $originalTaxClassModel = $this->classModelRegistry->retrieve($taxClassId); $taxClassModel = $this->converter->createTaxClassModel($taxClass); $taxClassModel->setId($taxClassId); /* should not be allowed to switch the tax class type */ if ($originalTaxClassModel->getClassType() !== $taxClassModel->getClassType()) { throw new InputException('Updating classType is not allowed.'); } try { $taxClassModel->save(); } catch (\Exception $e) { return false; } $this->classModelRegistry->registerTaxClass($taxClassModel); return true; }