tax rule update and delation
This commit is contained in:
113
core/lib/Thelia/Action/TaxRule.php
Normal file
113
core/lib/Thelia/Action/TaxRule.php
Normal file
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/*************************************************************************************/
|
||||
|
||||
namespace Thelia\Action;
|
||||
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Thelia\Core\Event\Tax\TaxRuleEvent;
|
||||
use Thelia\Core\Event\TheliaEvents;
|
||||
use Thelia\Model\TaxRule as TaxRuleModel;
|
||||
use Thelia\Model\TaxRuleQuery;
|
||||
|
||||
class TaxRule extends BaseAction implements EventSubscriberInterface
|
||||
{
|
||||
/**
|
||||
* @param TaxRuleEvent $event
|
||||
*/
|
||||
public function create(TaxRuleEvent $event)
|
||||
{
|
||||
$product = new TaxRuleModel();
|
||||
|
||||
$product
|
||||
->setDispatcher($this->getDispatcher())
|
||||
|
||||
->setRef($event->getRef())
|
||||
->setTitle($event->getTitle())
|
||||
->setLocale($event->getLocale())
|
||||
->setVisible($event->getVisible())
|
||||
|
||||
// Set the default tax rule to this product
|
||||
->setTaxRule(TaxRuleQuery::create()->findOneByIsDefault(true))
|
||||
|
||||
//public function create($defaultCategoryId, $basePrice, $priceCurrencyId, $taxRuleId, $baseWeight) {
|
||||
|
||||
->create(
|
||||
$event->getDefaultCategory(),
|
||||
$event->getBasePrice(),
|
||||
$event->getCurrencyId(),
|
||||
$event->getTaxRuleId(),
|
||||
$event->getBaseWeight()
|
||||
);
|
||||
;
|
||||
|
||||
$event->setTaxRule($product);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TaxRuleEvent $event
|
||||
*/
|
||||
public function update(TaxRuleEvent $event)
|
||||
{
|
||||
if (null !== $taxRule = TaxRuleQuery::create()->findPk($event->getId())) {
|
||||
|
||||
$taxRule
|
||||
->setLocale($event->getLocale())
|
||||
->setTitle($event->getTitle())
|
||||
->setDescription($event->getDescription())
|
||||
->save()
|
||||
;
|
||||
|
||||
|
||||
|
||||
$event->setTaxRule($taxRule);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TaxRuleEvent $event
|
||||
*/
|
||||
public function delete(TaxRuleEvent $event)
|
||||
{
|
||||
if (null !== $taxRule = TaxRuleQuery::create()->findPk($event->getId())) {
|
||||
|
||||
$taxRule
|
||||
->delete()
|
||||
;
|
||||
|
||||
$event->setTaxRule($taxRule);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return array(
|
||||
TheliaEvents::TAX_RULE_CREATE => array("create", 128),
|
||||
TheliaEvents::TAX_RULE_UPDATE => array("update", 128),
|
||||
TheliaEvents::TAX_RULE_DELETE => array("delete", 128),
|
||||
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -106,6 +106,11 @@
|
||||
<tag name="kernel.event_subscriber"/>
|
||||
</service>
|
||||
|
||||
<service id="thelia.action.taxrule" class="Thelia\Action\TaxRule">
|
||||
<argument type="service" id="service_container"/>
|
||||
<tag name="kernel.event_subscriber"/>
|
||||
</service>
|
||||
|
||||
<service id="thelia.action.content" class="Thelia\Action\Content">
|
||||
<argument type="service" id="service_container"/>
|
||||
<tag name="kernel.event_subscriber"/>
|
||||
|
||||
@@ -783,11 +783,19 @@
|
||||
<default key="_controller">Thelia\Controller\Admin\TaxRuleController::defaultAction</default>
|
||||
</route>
|
||||
|
||||
<route id="admin.configuration.taxes-rules.update" path="/admin/configuration/taxes_rules/update/{tax_rule_id}/{country_isoalpha3}" methods="get">
|
||||
<route id="admin.configuration.taxes-rules.update" path="/admin/configuration/taxes_rules/update/{tax_rule_id}">
|
||||
<default key="_controller">Thelia\Controller\Admin\TaxRuleController::updateAction</default>
|
||||
<requirement key="tax_rule_id">\d+</requirement>
|
||||
</route>
|
||||
|
||||
<route id="admin.categories.save" path="/admin/configuration/taxes_rules/save">
|
||||
<default key="_controller">Thelia\Controller\Admin\TaxRuleController::processUpdateAction</default>
|
||||
</route>
|
||||
|
||||
<route id="admin.categories.save" path="/admin/configuration/taxes_rules/delete">
|
||||
<default key="_controller">Thelia\Controller\Admin\TaxRuleController::deleteAction</default>
|
||||
</route>
|
||||
|
||||
<!-- end tax rules management -->
|
||||
|
||||
|
||||
|
||||
@@ -71,9 +71,14 @@ class TaxRuleController extends AbstractCrudController
|
||||
|
||||
protected function getUpdateEvent($formData)
|
||||
{
|
||||
$event = new TaxRuleEvent();
|
||||
$event = new TaxRuleEvent(
|
||||
TaxRuleQuery::create()->findPk($formData['id'])
|
||||
);
|
||||
|
||||
/* @todo fill event */
|
||||
$event->setLocale($formData['locale']);
|
||||
$event->setId($formData['id']);
|
||||
$event->setTitle($formData['title']);
|
||||
$event->setDescription($formData['description']);
|
||||
|
||||
return $event;
|
||||
}
|
||||
@@ -82,7 +87,9 @@ class TaxRuleController extends AbstractCrudController
|
||||
{
|
||||
$event = new TaxRuleEvent();
|
||||
|
||||
/* @todo fill event */
|
||||
$event->setId(
|
||||
$this->getRequest()->get('tax_rule_id', 0)
|
||||
);
|
||||
|
||||
return $event;
|
||||
}
|
||||
@@ -128,10 +135,17 @@ class TaxRuleController extends AbstractCrudController
|
||||
}
|
||||
|
||||
protected function getViewArguments()
|
||||
{
|
||||
return array(
|
||||
'tab' => $this->getRequest()->get('tab', 'data'),
|
||||
'country' => $this->getRequest()->get('country', CountryQuery::create()->findOneByByDefault(1)->getIsoalpha3()),
|
||||
);
|
||||
}
|
||||
|
||||
protected function getRouteArguments()
|
||||
{
|
||||
return array(
|
||||
'tax_rule_id' => $this->getRequest()->get('tax_rule_id'),
|
||||
'country_isoalpha3' => $this->getRequest()->get('country_isoalpha3'),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -146,14 +160,8 @@ class TaxRuleController extends AbstractCrudController
|
||||
|
||||
protected function renderEditionTemplate()
|
||||
{
|
||||
/* check the country exists */
|
||||
$country = CountryQuery::create()->findOneByIsoalpha3($this->getRequest()->get('country_isoalpha3'));
|
||||
if(null === $country) {
|
||||
$this->redirectToListTemplate();
|
||||
}
|
||||
|
||||
// We always return to the feature edition form
|
||||
return $this->render('tax-rule-edit', $this->getViewArguments());
|
||||
return $this->render('tax-rule-edit', array_merge($this->getViewArguments(), $this->getRouteArguments()));
|
||||
}
|
||||
|
||||
protected function redirectToEditionTemplate()
|
||||
@@ -161,15 +169,15 @@ class TaxRuleController extends AbstractCrudController
|
||||
// We always return to the feature edition form
|
||||
$this->redirectToRoute(
|
||||
"admin.configuration.taxes-rules.update",
|
||||
$this->getViewArguments()
|
||||
$this->getViewArguments(),
|
||||
$this->getRouteArguments()
|
||||
);
|
||||
}
|
||||
|
||||
protected function redirectToListTemplate()
|
||||
{
|
||||
$this->redirectToRoute(
|
||||
"admin.configuration.taxes-rules.list",
|
||||
array()
|
||||
"admin.configuration.taxes-rules.list"
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -29,6 +29,11 @@ class TaxRuleEvent extends ActionEvent
|
||||
{
|
||||
protected $taxRule = null;
|
||||
|
||||
protected $locale;
|
||||
protected $id;
|
||||
protected $title;
|
||||
protected $description;
|
||||
|
||||
public function __construct(TaxRule $taxRule = null)
|
||||
{
|
||||
$this->taxRule = $taxRule;
|
||||
@@ -50,4 +55,44 @@ class TaxRuleEvent extends ActionEvent
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setDescription($description)
|
||||
{
|
||||
$this->description = $description;
|
||||
}
|
||||
|
||||
public function getDescription()
|
||||
{
|
||||
return $this->description;
|
||||
}
|
||||
|
||||
public function setId($id)
|
||||
{
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function setTitle($title)
|
||||
{
|
||||
$this->title = $title;
|
||||
}
|
||||
|
||||
public function getTitle()
|
||||
{
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
public function setLocale($locale)
|
||||
{
|
||||
$this->locale = $locale;
|
||||
}
|
||||
|
||||
public function getLocale()
|
||||
{
|
||||
return $this->locale;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,31 +25,27 @@ namespace Thelia\Form;
|
||||
use Symfony\Component\Validator\Constraints;
|
||||
use Symfony\Component\Validator\Constraints\NotBlank;
|
||||
use Thelia\Core\Translation\Translator;
|
||||
use Thelia\Model\CountryQuery;
|
||||
|
||||
class TaxRuleCreationForm extends BaseForm
|
||||
{
|
||||
protected function buildForm()
|
||||
protected function buildForm($change_mode = false)
|
||||
{
|
||||
$this->formBuilder
|
||||
->add("title" , "text" , array(
|
||||
->add("locale", "text", array(
|
||||
"constraints" => array(new NotBlank())
|
||||
))
|
||||
->add("country", "text", array(
|
||||
"constraints" => array(
|
||||
new NotBlank()
|
||||
new Constraints\Callback(
|
||||
array(
|
||||
"methods" => array(
|
||||
array($this, "verifyCountry"),
|
||||
),
|
||||
)
|
||||
),
|
||||
),
|
||||
"label" => Translator::getInstance()->trans("Title *"),
|
||||
"label_attr" => array(
|
||||
"for" => "title"
|
||||
))
|
||||
)
|
||||
->add("locale" , "text" , array(
|
||||
"constraints" => array(
|
||||
new NotBlank()
|
||||
))
|
||||
)
|
||||
->add("feature_id", "hidden", array(
|
||||
"constraints" => array(
|
||||
new NotBlank()
|
||||
))
|
||||
)
|
||||
))
|
||||
;
|
||||
}
|
||||
|
||||
@@ -57,4 +53,14 @@ class TaxRuleCreationForm extends BaseForm
|
||||
{
|
||||
return "thelia_tax_rule_creation";
|
||||
}
|
||||
|
||||
public function verifyCountry($value, ExecutionContextInterface $context)
|
||||
{
|
||||
$country = CountryQuery::create()
|
||||
->findOneByIsoalpha3($value);
|
||||
|
||||
if (null === $country) {
|
||||
$context->addViolation("Country ISOALPHA3 not found");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,21 +23,50 @@
|
||||
namespace Thelia\Form;
|
||||
|
||||
use Symfony\Component\Validator\Constraints;
|
||||
use Symfony\Component\Validator\Constraints\GreaterThan;
|
||||
use Symfony\Component\Validator\ExecutionContextInterface;
|
||||
use Thelia\Model\TaxRuleQuery;
|
||||
|
||||
class TaxRuleModificationForm extends FeatureCreationForm
|
||||
class TaxRuleModificationForm extends TaxRuleCreationForm
|
||||
{
|
||||
use StandardDescriptionFieldsTrait;
|
||||
|
||||
protected function buildForm()
|
||||
{
|
||||
parent::buildForm(true);
|
||||
|
||||
$this->formBuilder
|
||||
->add("id", "hidden", array(
|
||||
"required" => true,
|
||||
"constraints" => array(
|
||||
new GreaterThan(
|
||||
array('value' => 0)
|
||||
new Constraints\NotBlank(),
|
||||
new Constraints\Callback(
|
||||
array(
|
||||
"methods" => array(
|
||||
array($this, "verifyTaxRuleId"),
|
||||
),
|
||||
)
|
||||
),
|
||||
)
|
||||
))
|
||||
->add("tab", "text", array(
|
||||
"constraints" => array(
|
||||
new Constraints\Choice(
|
||||
array(
|
||||
'choices' => array('data', 'taxes'),
|
||||
)
|
||||
)
|
||||
),
|
||||
))
|
||||
->add("country", "text", array(
|
||||
"constraints" => array(
|
||||
new Constraints\Callback(
|
||||
array(
|
||||
"methods" => array(
|
||||
array($this, "verifyCountry"),
|
||||
),
|
||||
)
|
||||
),
|
||||
)
|
||||
))
|
||||
;
|
||||
|
||||
@@ -49,4 +78,14 @@ class TaxRuleModificationForm extends FeatureCreationForm
|
||||
{
|
||||
return "thelia_tax_rule_modification";
|
||||
}
|
||||
|
||||
public function verifyTaxRuleId($value, ExecutionContextInterface $context)
|
||||
{
|
||||
$taxRule = TaxRuleQuery::create()
|
||||
->findPk($value);
|
||||
|
||||
if (null === $taxRule) {
|
||||
$context->addViolation("Tax rule ID not found");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user