*/ /** * stTaxBackend actions. * * @package stTaxPlugin * @subpackage actions */ class stTaxBackendActions extends autoStTaxBackendActions { public function executeChangePriceType() { $type = $this->getRequestParameter('type'); $config = stConfig::getInstance(sfContext::getInstance(), 'stProduct'); $config->set('price_type', $type); $config->save(); return $this->redirect($this->getRequest()->getReferer()); } public function executeSave() { $tax = TaxPeer::retrieveByPK($this->getRequestParameter('id')); if (!$tax || $tax->getVat() == $this->getRequestParameter('tax[vat]')) { return parent::executeSave(); } $this->tax = $tax; } public function updateTaxFromRequest() { if ($this->hasRequestParameter('update_price') && $this->tax->getVat() != $this->getRequestParameter('tax[vat]')) { $this->tax->setUpdateResume(array('prev_value' => $this->tax->getVat(), 'type' => $this->getRequestParameter('update_price'))); } parent::updateTaxFromRequest(); } public function executeUpdatePrice() { $tax = TaxPeer::retrieveByPK($this->getRequestParameter('id')); if ($params = $tax->getUpdateResume()) { $c = new Criteria(); $c->add(ProductPeer::TAX_ID, $tax->getId()); $steps['products'] = ProductPeer::doCount($c); $c = new Criteria(); $c->add(DeliveryPeer::TAX_ID, $tax->getId()); $steps['deliveries'] = DeliveryPeer::doCount($c); $this->steps = $steps['products'] + $steps['deliveries']; stTaxProgressBar::setParam('steps', $steps); } $params['id'] = $tax->getId(); $params['value'] = $tax->getVat(); stTaxProgressBar::setParam('tax', $params); $this->params = $params; $i18n = $this->getContext()->getI18n(); if (isset($params['type'])) { $this->title = $i18n->__('Przeliczanie cen z %type% dla stawki VAT %tax%%', array( '%type%' => $i18n->__($params['type'] == 'netto' ? 'brutto na netto' : 'netto na brutto'), '%tax%' => $params['value'] )); } else { $this->title = $i18n->__('Przeliczanie cen dla stawki VAT %tax%%', array('%tax%' => $params['value'])); } $this->tax = $tax; } public function validateEdit() { $tax = $this->getTaxOrCreate(); $i18n = $this->getContext()->getI18N(); $request = $this->getRequest(); if ($tax->getUpdateResume()) { $message = $i18n->__('Wykryto zmiane stawki VAT. Kliknij').' '.$i18n->__('tutaj').', '.$i18n->__('aby dokończyć poprzednią aktualizację cen'); $request->setError('tax', $message); } if ($request->getMethod() == sfRequest::POST) { $data = $this->getRequestParameter('tax'); $config = stConfig::getInstance('stTaxBackend'); if (isset($data['is_active']) && isset($data['rates_by_country']) && $config->get('is_customer_eu_tax_enabled')) { $codes = stTax::getRequiredCountriesCodes(); foreach ($codes as $code) { if (!isset($data['rates_by_country'][$code]) || $data['rates_by_country'][$code] === "") { $request->setError('tax{rates_by_country}', $i18n->__('Proszę uzupełnić pole.')); $request->setError('tax{rates_by_country}{'.$code.'}', $i18n->__('Proszę uzupełnić pole.')); } } } if (!isset($data['is_active']) && !$tax->isNew() && $tax->countProducts() > 0) { $request->setError('tax{is_active}', $i18n->__('Nie można wyłączyć stawki VAT "%%tax%%", ponieważ ma przypisane produkty', array('%%tax%%' => $tax->getVatName()))); } } return !$request->hasErrors(); } /** * {@inheritDoc} */ public function getLabels() { $i18n = $this->getContext()->getI18N(); $labels = parent::getLabels(); $labels['tax'] = $i18n->__('Zmiana stawki VAT'); return $labels; } public function handleErrorSave() { $this->handleErrorEdit(); $this->setTemplate('edit'); return sfView::SUCCESS; } public function saveTax($tax) { $ret = parent::saveTax($tax); if ($this->hasRequestParameter('update_price')) { $con = Propel::getConnection(); $sql = sprintf('UPDATE %1$s, %2$s SET %3$s = ? WHERE %4$s = %5$s AND %6$s = ?', ProductOptionsValuePeer::TABLE_NAME, ProductPeer::TABLE_NAME, ProductOptionsValuePeer::IS_UPDATED, ProductOptionsValuePeer::PRODUCT_ID, ProductPeer::ID, ProductPeer::TAX_ID); $st = $con->prepareStatement($sql); $st->setBoolean(1, false); $st->setInt(2, $tax->getId()); $st->executeQuery(); $this->redirect('stTaxBackend/updatePrice?id='.$tax->getId()); } return $ret; } public function validateConfig() { $request = $this->getRequest(); if ($request->getMethod() == sfRequest::POST) { sfLoader::loadHelpers(array('Helper', 'stUrl')); $data = $this->getRequestParameter('config'); $i18n = $this->getContext()->getI18N(); if (isset($data['is_eu_tax_enabled'])) { if (!stConfig::getInstance('stShopInfoBackend')->get('country')) { $error = $i18n->__('Do poprawnego działania %%name%% wymagane jest ustawienie kraju siedziby firmy w %%link%%', array( '%%name%%' => $i18n->__('VAT UE dla firm'), '%%link%%' => st_link_to($i18n->__('Konfiguracji informacji o sklepie'), '@stShopInfoPlugin?action=config', array('target' => '_blank')) )); $request->setError('config{is_eu_tax_enabled}',$error); } } if (isset($data['is_customer_eu_tax_enabled'])) { $errors = array(); if (!stConfig::getInstance('stShopInfoBackend')->get('country')) { $errors[] = $i18n->__('Do poprawnego działania %%name%% wymagane jest ustawienie kraju siedziby firmy w %%link%%', array( '%%name%%' => $i18n->__('VAT UE dla konsumentów'), '%%link%%' => st_link_to($i18n->__('Konfiguracji informacji o sklepie'), '@stShopInfoPlugin?action=config', array('target' => '_blank')) )); } $required = stTax::getRequiredCountriesCodes(); $i18n = $this->getContext()->getI18N(); foreach (TaxPeer::doSelectActive() as $tax) { if ($tax->isSystemDefault()) { continue; } $rates = $tax->getRatesByCountry(); foreach ($required as $code) { if (!isset($rates[$code]) || $rates[$code] === "") { $errors[] = $i18n->__('is.customer.eu.tax.enabled.help'); break 2; } } } if ($errors) { $request->setError('config{is_customer_eu_tax_enabled}', implode('
', $errors)); } } } return !$request->hasErrors(); } /** * {@inheritDoc} */ protected function getTaxOrCreate($id = 'id') { $tax = parent::getTaxOrCreate($id); if ($tax->isSystemDefault()) { $this->hideField('rates_by_country'); } return $tax; } protected function deleteTax($tax) { if ($tax->countProducts() > 0) { $i18n = $this->getContext()->getI18N(); throw new Exception($i18n->__('Nie można usunąć stawki VAT "%%tax%%", ponieważ ma przypisane produkty', array('%%tax%%' => $tax->getVatName()))); } return parent::deleteTax($tax); } }