From 0f6951bc21257a875532cf3074665f4666907fe5 Mon Sep 17 00:00:00 2001 From: Etienne Roudeix Date: Tue, 8 Oct 2013 16:02:58 +0200 Subject: [PATCH] tax rule update and delation --- core/lib/Thelia/Action/TaxRule.php | 113 ++++++++++++++++++ core/lib/Thelia/Config/Resources/action.xml | 5 + .../Thelia/Config/Resources/routing/admin.xml | 10 +- .../Controller/Admin/TaxRuleController.php | 36 +++--- .../Thelia/Core/Event/Tax/TaxRuleEvent.php | 45 +++++++ core/lib/Thelia/Form/TaxRuleCreationForm.php | 42 ++++--- .../Thelia/Form/TaxRuleModificationForm.php | 47 +++++++- templates/admin/default/tax-rule-edit.html | 64 +++++++++- templates/admin/default/taxes-rules.html | 14 ++- 9 files changed, 331 insertions(+), 45 deletions(-) create mode 100644 core/lib/Thelia/Action/TaxRule.php diff --git a/core/lib/Thelia/Action/TaxRule.php b/core/lib/Thelia/Action/TaxRule.php new file mode 100644 index 000000000..9fda846e1 --- /dev/null +++ b/core/lib/Thelia/Action/TaxRule.php @@ -0,0 +1,113 @@ +. */ +/* */ +/*************************************************************************************/ + +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), + + ); + } +} diff --git a/core/lib/Thelia/Config/Resources/action.xml b/core/lib/Thelia/Config/Resources/action.xml index 87575f59b..67c14bce5 100755 --- a/core/lib/Thelia/Config/Resources/action.xml +++ b/core/lib/Thelia/Config/Resources/action.xml @@ -106,6 +106,11 @@ + + + + + diff --git a/core/lib/Thelia/Config/Resources/routing/admin.xml b/core/lib/Thelia/Config/Resources/routing/admin.xml index f8cd21c9b..8d60250a4 100755 --- a/core/lib/Thelia/Config/Resources/routing/admin.xml +++ b/core/lib/Thelia/Config/Resources/routing/admin.xml @@ -783,11 +783,19 @@ Thelia\Controller\Admin\TaxRuleController::defaultAction - + Thelia\Controller\Admin\TaxRuleController::updateAction \d+ + + Thelia\Controller\Admin\TaxRuleController::processUpdateAction + + + + Thelia\Controller\Admin\TaxRuleController::deleteAction + + diff --git a/core/lib/Thelia/Controller/Admin/TaxRuleController.php b/core/lib/Thelia/Controller/Admin/TaxRuleController.php index 223e73fa1..3756f686c 100644 --- a/core/lib/Thelia/Controller/Admin/TaxRuleController.php +++ b/core/lib/Thelia/Controller/Admin/TaxRuleController.php @@ -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" ); } diff --git a/core/lib/Thelia/Core/Event/Tax/TaxRuleEvent.php b/core/lib/Thelia/Core/Event/Tax/TaxRuleEvent.php index d12d81fba..93cfced8e 100644 --- a/core/lib/Thelia/Core/Event/Tax/TaxRuleEvent.php +++ b/core/lib/Thelia/Core/Event/Tax/TaxRuleEvent.php @@ -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; + } } diff --git a/core/lib/Thelia/Form/TaxRuleCreationForm.php b/core/lib/Thelia/Form/TaxRuleCreationForm.php index a54e8b0c5..4b9a66f7b 100644 --- a/core/lib/Thelia/Form/TaxRuleCreationForm.php +++ b/core/lib/Thelia/Form/TaxRuleCreationForm.php @@ -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"); + } + } } diff --git a/core/lib/Thelia/Form/TaxRuleModificationForm.php b/core/lib/Thelia/Form/TaxRuleModificationForm.php index 44cde86cd..9b80916ef 100644 --- a/core/lib/Thelia/Form/TaxRuleModificationForm.php +++ b/core/lib/Thelia/Form/TaxRuleModificationForm.php @@ -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"); + } + } } diff --git a/templates/admin/default/tax-rule-edit.html b/templates/admin/default/tax-rule-edit.html index e58b67ddf..a953ed19d 100644 --- a/templates/admin/default/tax-rule-edit.html +++ b/templates/admin/default/tax-rule-edit.html @@ -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"}}}
@@ -19,7 +20,7 @@
  • {intl l='Editing tax rule'}
  • - {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}
    @@ -32,16 +33,67 @@
    - TODO +
    + {form name="thelia.admin.taxrule.modification"} -
    + + {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 *} + + + {form_hidden_fields form=$form} + + {form_field form=$form field='success_url'} + + {/form_field} + + {form_field form=$form field='locale'} + + {/form_field} + + {if $form_error}
    {$form_error_message}
    {/if} + + {form_field form=$form field='title'} +
    + + +
    + {/form_field} + + {form_field form=$form field='description'} +
    + + + +
    + {/form_field} + +
    +
    +
    + +
    +

    {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}}

    +
    +
    +
    +
    - - {/form} + {/form} +
    @@ -55,7 +107,7 @@
    diff --git a/templates/admin/default/taxes-rules.html b/templates/admin/default/taxes-rules.html index bc6cb4ed3..387f70e08 100644 --- a/templates/admin/default/taxes-rules.html +++ b/templates/admin/default/taxes-rules.html @@ -49,11 +49,11 @@
    {loop type="auth" name="can_change" roles="ADMIN" permissions="admin.configuration.taxes-rules.change"} - + {/loop} {loop type="auth" name="can_change" roles="ADMIN" permissions="admin.configuration.taxes-rules.change"} - + {/loop}
    @@ -93,4 +93,14 @@ form_content = {$smarty.capture.tax_rule_delete_dialog nofilter} } +{/block} + +{block name="javascript-initialization"} + + + {/block} \ No newline at end of file