Skip to content

Commit

Permalink
Merge pull request #16 from ThomasDaSilva/main
Browse files Browse the repository at this point in the history
Tax Rule Option + fix translation + hide password feature
  • Loading branch information
NicolasBarbey authored Oct 3, 2023
2 parents 2e11c10 + 70a32dc commit 5cc8195
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 32 deletions.
17 changes: 10 additions & 7 deletions ChronopostPickupPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,36 @@
use PDO;
use Propel\Runtime\ActiveQuery\Criteria;
use Propel\Runtime\Connection\ConnectionInterface;
use Propel\Runtime\Exception\PropelException;
use Propel\Runtime\Propel;
use Symfony\Component\DependencyInjection\Loader\Configurator\ServicesConfigurator;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpFoundation\RequestStack;
use Thelia\Core\HttpFoundation\Request;
use Thelia\Core\HttpFoundation\Session\Session;
use Thelia\Install\Database;
use Thelia\Model\ConfigQuery;
use Thelia\Model\Country;
use Thelia\Model\CountryArea;
use Thelia\Model\Lang;
use Thelia\Model\LangQuery;
use Thelia\Model\Message;
use Thelia\Model\MessageQuery;
use Thelia\Model\ModuleQuery;
use Thelia\Model\OrderPostage;
use Thelia\Model\State;
use Thelia\Module\AbstractDeliveryModuleWithState;
use Thelia\Module\BaseModule;
use Thelia\Module\Exception\DeliveryException;

class ChronopostPickupPoint extends AbstractDeliveryModuleWithState
{
/** @var string */
const DOMAIN_NAME = 'chronopostPickupPoint';
const DOMAIN_NAME = 'chronopostpickuppoint';

const CHRONOPOST_CONFIRMATION_MESSAGE_NAME = 'chronopost_pickup_point_confirmation_message_name';

const CHRONOPOST_TAX_RULE_ID = 'chronopost_pickup_point_tax_rule_id';

/**
* @param ConnectionInterface|null $con
* @throws PropelException
*/
public function postActivation(ConnectionInterface $con = null): void
{
Expand Down Expand Up @@ -110,6 +109,10 @@ public function postActivation(ConnectionInterface $con = null): void
->save()
;
}

if (!self::getConfigValue(self::CHRONOPOST_TAX_RULE_ID)) {
self::setConfigValue(self::CHRONOPOST_TAX_RULE_ID, null);
}
}

public function update($currentVersion, $newVersion, ConnectionInterface $con = null):void
Expand Down Expand Up @@ -367,7 +370,7 @@ public function getMinPostage($country, $cartWeight, $cartAmount, $deliveryType,
}
}

return $this->buildOrderPostage($minPostage, $country, $locale);
return $this->buildOrderPostage($minPostage, $country, $locale, self::getConfigValue(self::CHRONOPOST_TAX_RULE_ID));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Config/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<language>en_US</language>
<language>fr_FR</language>
</languages>
<version>2.0.4</version>
<version>2.0.5</version>
<authors>
<author>
<name>Thelia</name>
Expand Down
56 changes: 56 additions & 0 deletions Controller/ChronopostPickupPointTaxRuleController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace ChronopostPickupPoint\Controller;

use ChronopostPickupPoint\ChronopostPickupPoint;
use ChronopostPickupPoint\Form\ChronopostPickupPointTaxRuleForm;
use Symfony\Component\Routing\Annotation\Route;
use Thelia\Controller\Admin\BaseAdminController;
use Thelia\Core\Security\AccessManager;
use Thelia\Core\Security\Resource\AdminResources;
use Thelia\Core\Translation\Translator;
use Thelia\Form\Exception\FormValidationException;
use Thelia\Tools\URL;

#[Route('/admin/module/ChronopostPickupPoint/tax_rule', name: 'chronopost_pickup_point_tax_rule_')]
class ChronopostPickupPointTaxRuleController extends BaseAdminController
{
#[Route('/save', name: 'save')]
public function saveTaxRule()
{
if (null !== $response = $this->checkAuth(AdminResources::MODULE, ChronopostPickupPoint::DOMAIN_NAME, AccessManager::UPDATE)) {
return $response;
}

$taxRuleForm = $this->createForm(ChronopostPickupPointTaxRuleForm::getName());

$message = false;

$url = '/admin/module/ChronopostPickupPoint';

try {
$form = $this->validateForm($taxRuleForm);

// Get the form field values
$data = $form->getData();

ChronopostPickupPoint::setConfigValue(ChronopostPickupPoint::CHRONOPOST_TAX_RULE_ID, $data["tax_rule_id"]);

} catch (FormValidationException $ex) {
$message = $this->createStandardFormValidationErrorMessage($ex);
} catch (\Exception $ex) {
$message = $ex->getMessage();
}

if ($message !== false) {
$this->setupFormErrorContext(
Translator::getInstance()->trans('Error', [], ChronopostPickupPoint::DOMAIN_NAME),
$message,
$taxRuleForm,
$ex
);
}

return $this->generateRedirect(URL::getInstance()->absoluteUrl($url, [ 'current_tab' => 'tax_rule']));
}
}
11 changes: 6 additions & 5 deletions Form/ChronopostPickupPointConfigurationForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@
use ChronopostPickupPoint\Config\ChronopostPickupPointConst;
use ChronopostPickupPoint\Model\ChronopostPickupPointDeliveryModeQuery;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Thelia\Core\Translation\Translator;
use Thelia\Form\BaseForm;
use Thelia\Model\LangQuery;

class ChronopostPickupPointConfigurationForm extends BaseForm
{
protected function buildForm()
protected function buildForm(): void
{
$config = ChronopostPickupPointConst::getConfig();

$this->formBuilder

/** Chronopost basic informations */
/** Chronopost basic information */
->add(
ChronopostPickupPointConst::CHRONOPOST_PICKUP_POINT_CODE_CLIENT,
TextType::class,
Expand All @@ -36,7 +37,7 @@ protected function buildForm()
]
)
->add(ChronopostPickupPointConst::CHRONOPOST_PICKUP_POINT_PASSWORD,
TextType::class,
PasswordType::class,
[
'required' => true,
'data' => $config[ChronopostPickupPointConst::CHRONOPOST_PICKUP_POINT_PASSWORD],
Expand Down Expand Up @@ -79,10 +80,10 @@ protected function buildForm()
;
}

/** BUILDFORM END */
/** BUILD FORM END */
}

public static function getName()
public static function getName(): string
{
return "chronopost_pickup_point_configuration_form";
}
Expand Down
49 changes: 49 additions & 0 deletions Form/ChronopostPickupPointTaxRuleForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace ChronopostPickupPoint\Form;

use ChronopostPickupPoint\ChronopostPickupPoint;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Thelia\Core\HttpFoundation\Request;
use Thelia\Core\Translation\Translator;
use Thelia\Form\BaseForm;
use Thelia\Model\TaxRuleI18nQuery;

class ChronopostPickupPointTaxRuleForm extends BaseForm
{
protected function buildForm(): void
{
$this->formBuilder
->add("tax_rule_id",
ChoiceType::class,
[
'data' => (int)ChronopostPickupPoint::getConfigValue(ChronopostPickupPoint::CHRONOPOST_TAX_RULE_ID),
'choices' => $this->getTaxRules(),
'label' => Translator::getInstance()->trans('Tax Rule', [], ChronopostPickupPoint::DOMAIN_NAME),
]
);
}

private function getTaxRules(): array
{
$res = [];

/** @var Request $request */
$request = $this->request;

$lang = $request->getSession()?->getAdminEditionLang();

$taxRules = TaxRuleI18nQuery::create()
->filterByLocale($lang->getLocale())
->find();

$res[Translator::getInstance()->trans('Default Tax rule', [], ChronopostPickupPoint::DOMAIN_NAME)] = null;

foreach ($taxRules as $taxRule)
{
$res[$taxRule->getTitle()] = $taxRule->getId();
}

return $res;
}
}
6 changes: 6 additions & 0 deletions I18n/backOffice/default/fr_FR.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

return array(
'Chronopost Tax Rule configuration' => 'Configuration des Règles de taxes Chronopost',
'Tax Rule' => 'Règles de taxes',
);
4 changes: 3 additions & 1 deletion I18n/fr_FR.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php

return array(
// 'an english string' => 'La traduction française de la chaine',
'Default Tax rule' => 'Règles de taxes par défaut',
'Tax Rule' => 'Règles de taxes',
);
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ NB1 : You need IDs provided by Chronopost to use this module.
Add it in your main thelia composer.json file

```
composer require thelia/chronopost-pickup-point-module:~1.0
composer require thelia/chronopost-pickup-point-module:~2.0.0
```

## Usage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,16 @@

{render_form_field field="chronopost_pickup_point_code"}

{render_form_field field="chronopost_pickup_point_password"}
<div class="form-group">
{form_field field="chronopost_pickup_point_password"}

<label for="{$name}" class="control-label">{$label}{if $required} <span class="required">*</span>{/if} : </label>
<input type="password" id="mdp" name="{$name}" class="form-control" value="{$data}" placeholder="Your Chronopost password" aria-describedby="pwdHelpBlock">
<button type="button" class="eye btn btn-primary btn-responsive" style="color:#FFF;background-color:#f6993c" onclick="togglePwd('mdp');">
<i class="glyphicon glyphicon-eye-open"></i>
</button>
{/form_field}
</div>
</div>
<div class="col-md-6">
<b>{intl l="Allowed shipping modes"}</b>
Expand All @@ -38,4 +47,12 @@
</div>
</form>
{/form}
</div>
</div>

<script>
function togglePwd(id)
{
var x = document.getElementById(id);
x.type = (x.type === "password")? "text" : "password";
}
</script>
Loading

0 comments on commit 5cc8195

Please sign in to comment.