Browse Source

[Administration] Export : étiquettes avancées #1416

feature/souke
Guillaume Bourgeois 1 year ago
parent
commit
2475c6227c
8 changed files with 109 additions and 44 deletions
  1. +2
    -1
      backend/controllers/DistributionController.php
  2. +1
    -1
      backend/controllers/FeatureAdminController.php
  3. +3
    -1
      backend/views/distribution/shopping-cart-labels.php
  4. +8
    -2
      backend/views/producer/update.php
  5. +66
    -38
      common/logic/Distribution/Distribution/Export/DistributionShoppingCartLabelsPdfGenerator.php
  6. +1
    -1
      common/logic/Feature/FeatureProducer/Repository/FeatureProducerRepositoryQuery.php
  7. +2
    -0
      common/logic/Producer/Producer/Model/Producer.php
  8. +26
    -0
      console/migrations/m231113_131131_add_column_producer_export_shopping_cart_labels_format.php

+ 2
- 1
backend/controllers/DistributionController.php View File

return $distributionModule->getExportManager()->getGenerator($name)->generate($distribution); return $distributionModule->getExportManager()->getGenerator($name)->generate($distribution);
} }
catch(ErrorException $exception) { catch(ErrorException $exception) {
$this->setFlash('error', "Une erreur est survenue lors de la génération de l'export.");
//$this->setFlash('error', "Une erreur est survenue lors de la génération de l'export.");
$this->setFlash('error', $exception->getMessage());
return $this->redirectReferer(); return $this->redirectReferer();
} }
} }

+ 1
- 1
backend/controllers/FeatureAdminController.php View File

return Ajax::responseSuccess($messageResponse); return Ajax::responseSuccess($messageResponse);
} }


public function actionToggleStatusFeatureProducer(int $id, bool $status = null)
public function actionToggleStatusFeatureProducer(int $id, int $status = null)
{ {
$featureManager = $this->getFeatureModule()->getManager(); $featureManager = $this->getFeatureModule()->getManager();
$feature = $this->findModel($id); $feature = $this->findModel($id);

+ 3
- 1
backend/views/distribution/shopping-cart-labels.php View File

$i = 0; $i = 0;
foreach($ordersArray as $key => $order) { foreach($ordersArray as $key => $order) {
$index ++; $index ++;

echo $distributionShoppingCartLabelsPdfGenerator->getShoppingCartLabelAsHtml($order); echo $distributionShoppingCartLabelsPdfGenerator->getShoppingCartLabelAsHtml($order);

if($index == $shoppingCartLabelsPerColumn) { if($index == $shoppingCartLabelsPerColumn) {
$index = 0; $index = 0;
} }
if($index == 0 && $key !== array_key_last($ordersArray)) {
if(!$isSpecificFormat && $index == 0 && $key !== array_key_last($ordersArray)) {
echo '<columnbreak />'; echo '<columnbreak />';
} }
} }

+ 8
- 2
backend/views/producer/update.php View File

use common\helpers\Dropdown; use common\helpers\Dropdown;
use common\helpers\GlobalParam; use common\helpers\GlobalParam;
use common\logic\Distribution\Distribution\Module\DistributionModule; use common\logic\Distribution\Distribution\Module\DistributionModule;
use common\logic\Distribution\Distribution\Service\ExportManager;
use common\logic\Feature\Feature\Model\Feature; use common\logic\Feature\Feature\Model\Feature;
use common\logic\Feature\Feature\Module\FeatureModule; use common\logic\Feature\Feature\Module\FeatureModule;
use common\logic\User\User\Module\UserModule; use common\logic\User\User\Module\UserModule;
->dropDownList(Dropdown::noYesChoices()); ?> ->dropDownList(Dropdown::noYesChoices()); ?>
<?= $form->field($model, 'option_csv_export_by_piece') <?= $form->field($model, 'option_csv_export_by_piece')
->dropDownList(Dropdown::noYesChoices()); ?> ->dropDownList(Dropdown::noYesChoices()); ?>
<?= $form->field($model, 'export_shopping_cart_labels_number_per_column')
->dropDownList(Dropdown::numberChoices(1, 20)); ?>
<?php if($featureManager->isEnabled(Feature::ALIAS_EXPORT_SHOPPING_CART_LABELS_ADVANCED)): ?>
<?= $form->field($model, 'export_shopping_cart_labels_format')
->dropDownList($distributionExportManager->getGenerator(ExportManager::SHOPPING_CART_LABELS_PDF)->populateDropdownSpecificFormats()); ?>
<?php else: ?>
<?= $form->field($model, 'export_shopping_cart_labels_number_per_column')
->dropDownList(Dropdown::numberChoices(1, 20)); ?>
<?php endif; ?>
</div> </div>
</div> </div>



+ 66
- 38
common/logic/Distribution/Distribution/Export/DistributionShoppingCartLabelsPdfGenerator.php View File



use common\logic\AbstractGenerator; use common\logic\AbstractGenerator;
use common\logic\Distribution\Distribution\Model\Distribution; use common\logic\Distribution\Distribution\Model\Distribution;
use common\logic\Feature\Feature\Model\Feature;
use common\logic\Feature\Feature\Service\FeatureManager;
use common\logic\Order\Order\Model\Order; use common\logic\Order\Order\Model\Order;
use common\logic\Order\Order\Repository\OrderRepository; use common\logic\Order\Order\Repository\OrderRepository;
use common\logic\Order\Order\Service\OrderSolver; use common\logic\Order\Order\Service\OrderSolver;
use common\logic\Producer\Producer\Service\ProducerSolver; use common\logic\Producer\Producer\Service\ProducerSolver;
use kartik\mpdf\Pdf; use kartik\mpdf\Pdf;
use yii\base\ErrorException;
use yii\helpers\BaseStringHelper; use yii\helpers\BaseStringHelper;
use yii\helpers\Html; use yii\helpers\Html;


class DistributionShoppingCartLabelsPdfGenerator extends AbstractGenerator implements DistributionExportGeneratorInterface class DistributionShoppingCartLabelsPdfGenerator extends AbstractGenerator implements DistributionExportGeneratorInterface
{ {
const FORMAT_70_42 = '70x42';

protected ProducerSolver $producerSolver; protected ProducerSolver $producerSolver;
protected OrderRepository $orderRepository; protected OrderRepository $orderRepository;
protected OrderSolver $orderSolver; protected OrderSolver $orderSolver;
$this->orderSolver = $this->loadService(OrderSolver::class); $this->orderSolver = $this->loadService(OrderSolver::class);
} }


public function getSpecificFormatDetailsArray(): array
{
return [
self::FORMAT_70_42 => [70, 42]
];
}

public function generate(Distribution $distribution, bool $save = false) public function generate(Distribution $distribution, bool $save = false)
{ {
$isSpecificFormat = true;
$specificFormatWidth = 42;
$specificFormatHeight = 70;
$specificFormatNumberColumns = 4;
$specificFormatNumberLines = 7;
// @TODO : obligé de charger FeatureManager comme ça car on est dans un service Generator (problème de hiérarchie de services)
$featureManager = FeatureManager::getInstance();
$isSpecificFormat = false;
$exportShoppingCartLabelsFormat = $this->producerSolver->getConfig('export_shopping_cart_labels_format');
if($featureManager->isEnabled(Feature::ALIAS_EXPORT_SHOPPING_CART_LABELS_ADVANCED)) {
if($exportShoppingCartLabelsFormat) {
$isSpecificFormat = true;
[$specificFormatWidth, $specificFormatHeight] = $this->getSpecificFormatDetails($exportShoppingCartLabelsFormat);
}
else {
throw new ErrorException("Aucun format d'étiquette n'est défini dans les paramètres.");
}
}


$ordersArray = $this->orderRepository->findOrdersByDistribution($distribution); $ordersArray = $this->orderRepository->findOrdersByDistribution($distribution);
$ordersArray = $this->filterOrdersExcludedUsersAndPointSales($ordersArray); $ordersArray = $this->filterOrdersExcludedUsersAndPointSales($ordersArray);


$content = \Yii::$app->getView()->render('@backend/views/distribution/shopping-cart-labels.php', [
'distribution' => $distribution,
'ordersArray' => $ordersArray,
'isSpecificFormat' => $isSpecificFormat,
'shoppingCartLabelsPerColumn' => $this->producerSolver->getConfig('export_shopping_cart_labels_number_per_column') ?: 8
]);

if($isSpecificFormat) { if($isSpecificFormat) {
$pdf = $this->getPdf($distribution, true);
$pdf->getApi()->WriteHTML($this->getCss(true), 1);

$x = 0;
$y = 0;
$pdf->getApi()->AddPage();

foreach($ordersArray as $order) {
$pdf->getApi()->SetXY($x * $specificFormatWidth, $y * $specificFormatHeight);
$pdf->getApi()->WriteHTML($this->getShoppingCartLabelAsHtml($order));

$y ++;
if($y == $specificFormatNumberLines) {
$y = 0;
$x ++;
if($x == $specificFormatNumberColumns) {
$x = 0;
$pdf->getApi()->AddPage();
}
}
}
$pdf = $this->getPdf($distribution, $content, true, $specificFormatWidth, $specificFormatHeight);
} }
else { else {
$content = \Yii::$app->getView()->render('@backend/views/distribution/shopping-cart-labels.php', [
'distribution' => $distribution,
'ordersArray' => $ordersArray,
'shoppingCartLabelsPerColumn' => $this->producerSolver->getConfig('export_shopping_cart_labels_number_per_column') ?: 8
]);
$pdf = $this->getPdf($distribution, false, $content);
$pdf = $this->getPdf($distribution, $content, false);
$pdf->getApi()->SetColumns(4); $pdf->getApi()->SetColumns(4);
$pdf->getApi()->keepColumns = true; $pdf->getApi()->keepColumns = true;
} }
return $pdf->render(); return $pdf->render();
} }


public function getPdf(Distribution $distribution, bool $isSpecificFormat, string $content = '')
public function getPdf(Distribution $distribution, string $content, bool $isSpecificFormat, float $specificFormatWidth = 0, float $specificFormatHeight = 0)
{ {
return new Pdf([ return new Pdf([
'mode' => Pdf::MODE_UTF8, 'mode' => Pdf::MODE_UTF8,
'@app/web/pdf/Etiquettes-' . $distribution->date . '-' . $this->getProducerContextId() . '.pdf' '@app/web/pdf/Etiquettes-' . $distribution->date . '-' . $this->getProducerContextId() . '.pdf'
), ),
'content' => $content, 'content' => $content,
'cssInline' => !$isSpecificFormat ? $this->getCss() : '',
'cssInline' => $this->getCss($isSpecificFormat, $specificFormatWidth, $specificFormatHeight),
]); ]);
} }


return $order->pointSale && $order->pointSale->exclude_export_shopping_cart_labels; return $order->pointSale && $order->pointSale->exclude_export_shopping_cart_labels;
} }


public function getCss(bool $isSpecificFormat = false): string
public function getCss(bool $isSpecificFormat = false, float $specificFormatWith = 0, float $specificFormatHeight = 0): string
{ {
$css = ' $css = '
@page { @page {
}'; }';


if($isSpecificFormat) { if($isSpecificFormat) {

$paddingShoppingCartLabel = 3;
$specificFormatWith = $specificFormatWith - 2 * $paddingShoppingCartLabel;
$specificFormatHeight = $specificFormatHeight - 2 * $paddingShoppingCartLabel;

$css .= ' $css .= '
.shopping-cart-label { .shopping-cart-label {
width: 42mm;
height: 70mm;
overflow: hidden;
box-sizing: border-box;
padding: '.$paddingShoppingCartLabel.'mm;
width: '.$specificFormatWith.'mm;
height: '.$specificFormatHeight.'mm;
display: block;
float: left;
}'; }';
} }
else { else {
</div> </div>
</div>'; </div>';
} }

public function getSpecificFormatDetails(string $name): array
{
$specificFormatDetailsArray = $this->getSpecificFormatDetailsArray();
if(!isset($specificFormatDetailsArray[$name])) {
throw new ErrorException("Format d'étiquette inconnu");
}
return $specificFormatDetailsArray[$name];
}

public function populateDropdownSpecificFormats(): array
{
$specificFormatsArray = [null => '--'];
foreach($this->getSpecificFormatDetailsArray() as $key => $specificFormat) {
$specificFormatsArray[$key] = $specificFormat[0].'x'.$specificFormat[1].' mm';
}
return $specificFormatsArray;
}
} }

+ 1
- 1
common/logic/Feature/FeatureProducer/Repository/FeatureProducerRepositoryQuery.php View File



public function filterByFeature(Feature $feature): self public function filterByFeature(Feature $feature): self
{ {
$this->andWhere(['id_feature' => $feature]);
$this->andWhere(['id_feature' => $feature->id]);
return $this; return $this;
} }
} }

+ 2
- 0
common/logic/Producer/Producer/Model/Producer.php View File

'option_testimony', 'option_testimony',
'contact_email', 'contact_email',
'admin_comment', 'admin_comment',
'export_shopping_cart_labels_format',
], ],
'string' 'string'
], ],
'option_invoice_only_based_on_delivery_notes' => 'Facturer uniquement sur la base des bons de livraison', 'option_invoice_only_based_on_delivery_notes' => 'Facturer uniquement sur la base des bons de livraison',
'option_document_width_logo' => 'Largeur du logo dans les documents', 'option_document_width_logo' => 'Largeur du logo dans les documents',
'export_shopping_cart_labels_number_per_column' => "Étiquettes (PDF) : nombre d'étiquettes par colonne", 'export_shopping_cart_labels_number_per_column' => "Étiquettes (PDF) : nombre d'étiquettes par colonne",
'export_shopping_cart_labels_format' => 'Étiquettes (PDF) : format',
'option_document_display_price_unit_reference' => "Afficher les prix au kilogramme", 'option_document_display_price_unit_reference' => "Afficher les prix au kilogramme",
'id_user_group_default' => "Groupe utilisateur par défaut attribué à l'inscription", 'id_user_group_default' => "Groupe utilisateur par défaut attribué à l'inscription",
'option_check_by_default_prevent_user_credit' => "Par défaut, prévenir l'utilisateur quand on crédite son compte" 'option_check_by_default_prevent_user_credit' => "Par défaut, prévenir l'utilisateur quand on crédite son compte"

+ 26
- 0
console/migrations/m231113_131131_add_column_producer_export_shopping_cart_labels_format.php View File

<?php

use yii\db\Migration;
use yii\db\Schema;

/**
* Class m231113_131131_add_column_producer_export_shopping_cart_labels_format
*/
class m231113_131131_add_column_producer_export_shopping_cart_labels_format extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->addColumn('producer', 'export_shopping_cart_labels_format', Schema::TYPE_STRING);
}

/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->dropColumn('producer', 'export_shopping_cart_labels_format');
}
}

Loading…
Cancel
Save