tax rule update and delation

This commit is contained in:
Etienne Roudeix
2013-10-08 16:02:58 +02:00
parent 942638281a
commit 0f6951bc21
9 changed files with 331 additions and 45 deletions

View 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),
);
}
}

View File

@@ -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"/>

View File

@@ -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 -->

View File

@@ -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"
);
}

View File

@@ -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;
}
}

View File

@@ -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");
}
}
}

View File

@@ -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");
}
}
}

View File

@@ -7,6 +7,7 @@
{block name="main-content"}
{assign oder_tab {$smarty.get.tab|default:"data"}}
{assign asked_country {$smarty.get.country|default:{country ask="default" attr="isoalpha3"}}}
<div class="taxes-rules edit-taxes-rules">
@@ -19,7 +20,7 @@
<li>{intl l='Editing tax rule'}</li>
</ul>
{loop type="tax-rule" name="tax-rule" id=$tax_rule_id}
{loop type="tax-rule" name="tax-rule" id=$tax_rule_id backend_context="1" lang=$edit_language_id}
<div class="row">
<div class="col-md-12 general-block-decorator clearfix">
@@ -32,16 +33,67 @@
<div class="tab-content">
<div class="tab-pane fade {if $oder_tab == 'data'}active in{/if}" id="data">
TODO
<div class="form-container">
{form name="thelia.admin.taxrule.modification"}
<form method="POST" action="{url path="/admin/configuration/taxes_rules/update/$tax_rule_id/$country_isoalpha3"}" {form_enctype form=$form} >
<form method="POST" action="{url path="/admin/configuration/taxes_rules/save"}" {form_enctype form=$form} >
{include
file = "includes/inner-form-toolbar.html"
hide_submit_buttons = false
page_url = {url path="/admin/configuration/taxes_rules/update/$tax_rule_id" tab=data}
close_url = {url path="/admin/configuration/taxes_rules"}
}
{* Be sure to get the product ID, even if the form could not be validated *}
<input type="hidden" name="tax_rule_id" value="{$ID}" />
{form_hidden_fields form=$form}
{form_field form=$form field='success_url'}
<input type="hidden" name="{$name}" value="{url path="/admin/configuration/taxes_rules"}" />
{/form_field}
{form_field form=$form field='locale'}
<input type="hidden" name="{$name}" value="{$edit_language_locale}" />
{/form_field}
{if $form_error}<div class="alert alert-danger">{$form_error_message}</div>{/if}
{form_field form=$form field='title'}
<div class="form-group {if $error}has-error{/if}">
<label for="{$label_attr.for}" class="control-label">{intl l=$label} : </label>
<input type="text" id="{$label_attr.for}" name="{$name}" required="required" title="{intl l='Title'}" placeholder="{intl l='Title'}" class="form-control" value="{if $error}{$value}{else}{if $IS_TRANSLATED == 1}{$TITLE}{/if}{/if}">
</div>
{/form_field}
{form_field form=$form field='description'}
<div class="form-group {if $error}has-error{/if}">
<label for="{$label_attr.for}" class="control-label">
{intl l=$label} :
<span class="label-help-block">{intl l="The detailed description."}</span>
</label>
<textarea name="{$name}" id="{$label_attr.for}" rows="10" class="form-control wysiwyg">{if $error}{$value}{else}{if $IS_TRANSLATED == 1}{$DESCRIPTION}{/if}{/if}</textarea>
</div>
{/form_field}
<div class="row">
<div class="col-md-12">
<div class="control-group">
<label>&nbsp;</label>
<div class="controls">
<p>{intl l='Tax rule created on %date_create. Last modification: %date_change' date_create={format_date date=$CREATE_DATE} date_change={format_date date=$UPDATE_DATE}}</p>
</div>
</div>
</div>
</div>
</form>
{/form}
{/form}
</div>
</div>
@@ -55,7 +107,7 @@
<label for="" class="label-control">{intl l="Choose a country"} :</label>
<select id="country-selector" data-toggle="selectpicker">
{loop type="country" name="country-list"}
<option value="{$ID}" {if $ISOALPHA3 == $country_isoalpha3}selected="selected"{/if}>{$TITLE}</option>
<option value="{$ID}" {if $ISOALPHA3 == $asked_country}selected="selected"{/if}>{$TITLE}</option>
{/loop}
</select>
</div>

View File

@@ -49,11 +49,11 @@
<td>
<div class="btn-group">
{loop type="auth" name="can_change" roles="ADMIN" permissions="admin.configuration.taxes-rules.change"}
<a class="btn btn-default btn-xs" title="{intl l='Change this tax rule'}" href="{url path="/admin/configuration/taxes_rules/update/$ID/{country ask="default" attr="isoalpha3"}"}"><span class="glyphicon glyphicon-edit"></span></a>
<a class="btn btn-default btn-xs" title="{intl l='Change this tax rule'}" href="{url path="/admin/configuration/taxes_rules/update/$ID"}"><span class="glyphicon glyphicon-edit"></span></a>
{/loop}
{loop type="auth" name="can_change" roles="ADMIN" permissions="admin.configuration.taxes-rules.change"}
<a class="btn btn-default btn-xs" title="{intl l='Delete this tax rule'}" href="#tax_rule_delete_dialog" data-id="{$ID}" data-toggle="modal"><span class="glyphicon glyphicon-trash"></span></a>
<a class="btn btn-default btn-xs js-delete-tax-rule" title="{intl l='Delete this tax rule'}" href="#tax_rule_delete_dialog" data-id="{$ID}" data-toggle="modal"><span class="glyphicon glyphicon-trash"></span></a>
{/loop}
</div>
</td>
@@ -93,4 +93,14 @@
form_content = {$smarty.capture.tax_rule_delete_dialog nofilter}
}
{/block}
{block name="javascript-initialization"}
<script type="text/javascript">
$(".js-delete-tax-rule").click(function(e){
$('#tax_rule_delete_id').val($(this).data('id'))
});
</script>
{/block}