Skip to content

Commit

Permalink
Merge pull request #28825 from MeKeyCool/refacto/19381_order_invoices…
Browse files Browse the repository at this point in the history
…_checkboxes

Order invoices configuration form: add multistore compatibility and some refactoring
  • Loading branch information
kpodemski committed Jul 29, 2022
2 parents 28e7e2d + 4602943 commit d2a3865
Show file tree
Hide file tree
Showing 8 changed files with 450 additions and 183 deletions.
6 changes: 6 additions & 0 deletions admin-dev/themes/new-theme/js/pages/invoices/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,10 @@ const {$} = window;
$(() => {
initDatePickers();
new TranslatableInput();

window.prestashop.component.initComponents(
[
'MultistoreConfigField',
],
);
});
134 changes: 87 additions & 47 deletions src/Adapter/Invoice/InvoiceOptionsConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,44 +27,60 @@
namespace PrestaShop\PrestaShop\Adapter\Invoice;

use PrestaShop\PrestaShop\Adapter\Configuration;
use PrestaShop\PrestaShop\Core\Configuration\DataConfigurationInterface;
use PrestaShop\PrestaShop\Adapter\Shop\Context;
use PrestaShop\PrestaShop\Core\Configuration\AbstractMultistoreConfiguration;
use PrestaShop\PrestaShop\Core\Feature\FeatureInterface;
use PrestaShop\PrestaShop\Core\Form\FormChoiceProviderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* Class InvoiceOptionsConfiguration is responsible for saving & loading invoice configuration.
*/
final class InvoiceOptionsConfiguration implements DataConfigurationInterface
final class InvoiceOptionsConfiguration extends AbstractMultistoreConfiguration
{
/**
* @var Configuration
* @var FormChoiceProviderInterface
*/
private $configuration;
private $invoiceModelByNameChoiceProvider;

/**
* AbstractMultistoreConfiguration constructor.
*
* @param Configuration $configuration
* @param Context $shopContext
* @param FeatureInterface $multistoreFeature
* @param FormChoiceProviderInterface $invoiceModelByNameChoiceProvider
*/
public function __construct(Configuration $configuration)
{
$this->configuration = $configuration;
public function __construct(
Configuration $configuration,
Context $shopContext,
FeatureInterface $multistoreFeature,
FormChoiceProviderInterface $invoiceModelByNameChoiceProvider
) {
parent::__construct($configuration, $shopContext, $multistoreFeature);
$this->invoiceModelByNameChoiceProvider = $invoiceModelByNameChoiceProvider;
}

/**
* {@inheritdoc}
*/
public function getConfiguration()
{
$shopConstraint = $this->getShopConstraint();

return [
'enable_invoices' => $this->configuration->getBoolean('PS_INVOICE'),
'enable_tax_breakdown' => $this->configuration->getBoolean('PS_INVOICE_TAXES_BREAKDOWN'),
'enable_product_images' => $this->configuration->getBoolean('PS_PDF_IMG_INVOICE'),
'invoice_prefix' => $this->configuration->get('PS_INVOICE_PREFIX'),
'add_current_year' => $this->configuration->getBoolean('PS_INVOICE_USE_YEAR'),
'reset_number_annually' => $this->configuration->getBoolean('PS_INVOICE_RESET'),
'year_position' => $this->configuration->getInt('PS_INVOICE_YEAR_POS'),
'invoice_number' => $this->configuration->getInt('PS_INVOICE_START_NUMBER'),
'legal_free_text' => $this->configuration->get('PS_INVOICE_LEGAL_FREE_TEXT'),
'footer_text' => $this->configuration->get('PS_INVOICE_FREE_TEXT'),
'invoice_model' => $this->configuration->get('PS_INVOICE_MODEL'),
'use_disk_cache' => $this->configuration->getBoolean('PS_PDF_USE_CACHE'),
'enable_invoices' => (bool) $this->configuration->get('PS_INVOICE', true, $shopConstraint),
'enable_tax_breakdown' => (bool) $this->configuration->get('PS_INVOICE_TAXES_BREAKDOWN', false, $shopConstraint),
'enable_product_images' => (bool) $this->configuration->get('PS_PDF_IMG_INVOICE', false, $shopConstraint),
'invoice_prefix' => $this->configuration->get('PS_INVOICE_PREFIX', ['#IN', '#FA'], $shopConstraint),
'add_current_year' => (bool) $this->configuration->get('PS_INVOICE_USE_YEAR', false, $shopConstraint),
'reset_number_annually' => (bool) $this->configuration->get('PS_INVOICE_RESET', false, $shopConstraint),
'year_position' => (int) $this->configuration->get('PS_INVOICE_YEAR_POS', 0, $shopConstraint),
'invoice_number' => (int) $this->configuration->get('PS_INVOICE_START_NUMBER', 0, $shopConstraint),
'legal_free_text' => $this->configuration->get('PS_INVOICE_LEGAL_FREE_TEXT', null, $shopConstraint),
'footer_text' => $this->configuration->get('PS_INVOICE_FREE_TEXT', null, $shopConstraint),
'invoice_model' => $this->configuration->get('PS_INVOICE_MODEL', 'invoice', $shopConstraint),
'use_disk_cache' => (bool) $this->configuration->get('PS_PDF_USE_CACHE', false, $shopConstraint),
];
}

Expand All @@ -74,41 +90,65 @@ public function getConfiguration()
public function updateConfiguration(array $configuration)
{
if ($this->validateConfiguration($configuration)) {
$this->configuration->set('PS_INVOICE', $configuration['enable_invoices']);
$this->configuration->set('PS_INVOICE_TAXES_BREAKDOWN', $configuration['enable_tax_breakdown']);
$this->configuration->set('PS_PDF_IMG_INVOICE', $configuration['enable_product_images']);
$this->configuration->set('PS_INVOICE_PREFIX', $configuration['invoice_prefix']);
$this->configuration->set('PS_INVOICE_USE_YEAR', $configuration['add_current_year']);
$this->configuration->set('PS_INVOICE_RESET', $configuration['reset_number_annually']);
$this->configuration->set('PS_INVOICE_YEAR_POS', $configuration['year_position']);
$this->configuration->set('PS_INVOICE_START_NUMBER', $configuration['invoice_number']);
$this->configuration->set('PS_INVOICE_LEGAL_FREE_TEXT', $configuration['legal_free_text']);
$this->configuration->set('PS_INVOICE_FREE_TEXT', $configuration['footer_text']);
$this->configuration->set('PS_INVOICE_MODEL', $configuration['invoice_model']);
$this->configuration->set('PS_PDF_USE_CACHE', $configuration['use_disk_cache']);
$shopConstraint = $this->getShopConstraint();

$this->updateConfigurationValue('PS_INVOICE', 'enable_invoices', $configuration, $shopConstraint);
$this->updateConfigurationValue('PS_INVOICE_TAXES_BREAKDOWN', 'enable_tax_breakdown', $configuration, $shopConstraint);
$this->updateConfigurationValue('PS_PDF_IMG_INVOICE', 'enable_product_images', $configuration, $shopConstraint);
$this->updateConfigurationValue('PS_INVOICE_PREFIX', 'invoice_prefix', $configuration, $shopConstraint);
$this->updateConfigurationValue('PS_INVOICE_USE_YEAR', 'add_current_year', $configuration, $shopConstraint);
$this->updateConfigurationValue('PS_INVOICE_RESET', 'reset_number_annually', $configuration, $shopConstraint);
$this->updateConfigurationValue('PS_INVOICE_YEAR_POS', 'year_position', $configuration, $shopConstraint);
$this->updateConfigurationValue('PS_INVOICE_START_NUMBER', 'invoice_number', $configuration, $shopConstraint);
$this->updateConfigurationValue('PS_INVOICE_LEGAL_FREE_TEXT', 'legal_free_text', $configuration, $shopConstraint);
$this->updateConfigurationValue('PS_INVOICE_FREE_TEXT', 'footer_text', $configuration, $shopConstraint);
$this->updateConfigurationValue('PS_INVOICE_MODEL', 'invoice_model', $configuration, $shopConstraint);
$this->updateConfigurationValue('PS_PDF_USE_CACHE', 'use_disk_cache', $configuration, $shopConstraint);
}

return [];
}

/**
* {@inheritdoc}
* @return OptionsResolver
*/
public function validateConfiguration(array $configuration)
protected function buildResolver(): OptionsResolver
{
return isset(
$configuration['enable_invoices'],
$configuration['enable_tax_breakdown'],
$configuration['enable_product_images'],
$configuration['invoice_prefix'],
$configuration['add_current_year'],
$configuration['reset_number_annually'],
$configuration['year_position'],
$configuration['invoice_number'],
$configuration['legal_free_text'],
$configuration['footer_text'],
$configuration['invoice_model'],
$configuration['use_disk_cache']
);
$resolver = (new OptionsResolver())
->setDefined(
[
'enable_invoices',
'enable_tax_breakdown',
'enable_product_images',
'invoice_prefix',
'add_current_year',
'reset_number_annually',
'year_position',
'invoice_number',
'legal_free_text',
'footer_text',
'invoice_model',
'use_disk_cache',
]
)
->setAllowedTypes('enable_invoices', ['bool'])
->setAllowedTypes('enable_tax_breakdown', ['bool'])
->setAllowedTypes('enable_product_images', ['bool'])
->setAllowedTypes('invoice_prefix', ['array'])
->setAllowedTypes('add_current_year', ['bool'])
->setAllowedTypes('reset_number_annually', ['bool'])
->setAllowedTypes('year_position', ['integer'])
->setAllowedValues('year_position', [0, 1])
->setAllowedTypes('invoice_number', ['integer'])
->setAllowedValues('invoice_number', function (int $value) {
return $value >= 0;
})
->setAllowedTypes('legal_free_text', ['array'])
->setAllowedTypes('footer_text', ['array'])
->setAllowedTypes('invoice_model', ['string'])
->setAllowedValues('invoice_model', array_keys($this->invoiceModelByNameChoiceProvider->getChoices()))
->setAllowedTypes('use_disk_cache', ['bool']);

return $resolver;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function processAction(Request $request)
private function processForm(FormHandlerInterface $formHandler, Request $request)
{
$form = $formHandler->getForm();
$form->submit($request->request->get($form->getName()));
$form->handleRequest($request);

if ($form->isSubmitted()) {
if ($errors = $formHandler->save($form->getData())) {
Expand Down

0 comments on commit d2a3865

Please sign in to comment.