Sfoglia il codice sorgente

[Technique] Administration : refactoring DistributionController #1173

feature/souke
Guillaume Bourgeois 1 anno fa
parent
commit
09805df0cc
9 ha cambiato i file con 208 aggiunte e 234 eliminazioni
  1. +21
    -203
      backend/controllers/DistributionController.php
  2. +1
    -1
      backend/web/js/vuejs/distribution-index.js
  3. +0
    -10
      common/logic/Distribution/Distribution/Service/DistributionReportGridCsvGenerator.php
  4. +164
    -0
      common/logic/Distribution/Distribution/Service/DistributionReportGridPdfGenerator.php
  5. +0
    -12
      common/logic/Distribution/Distribution/Service/DistributionReportPdfGenerator.php
  6. +2
    -2
      common/logic/Distribution/Distribution/Wrapper/DistributionContainer.php
  7. +2
    -2
      common/logic/Distribution/Distribution/Wrapper/DistributionManager.php
  8. +15
    -1
      common/logic/Distribution/ProductDistribution/Service/ProductDistributionBuilder.php
  9. +3
    -3
      common/logic/Product/Product/Repository/ProductRepository.php

+ 21
- 203
backend/controllers/DistributionController.php Vedi File

@@ -282,7 +282,7 @@ class DistributionController extends BackendController
$productOrderArray[$product['id']] = [
'quantity' => 0,
'unit' => $product['unit'],
'price' => number_format($product['price'], 3),
'price' => number_format($product['price'], 5),
'price_with_tax' => Price::getPriceWithTax($product['price'], $product['taxRate']['value']),
];
}
@@ -373,11 +373,12 @@ class DistributionController extends BackendController
$productOrderArray[$product['id']] = [
'quantity' => 0,
'unit' => $product['unit'],
//'price' => Price::getPriceWithTax($product['price'], $product['taxRate']['value']),
'price' => number_format($product['price'], 5),
'price_with_tax' => Price::getPriceWithTax($product['price'], $product['taxRate']['value']),
];
}
return [
'id_point_sale' => $pointSaleDefault ? $pointSaleDefault->id : false,
'id_point_sale' => $pointSaleDefault ? $pointSaleDefault->id : 0,
'id_user' => 0,
'username' => '',
'comment' => '',
@@ -478,9 +479,9 @@ class DistributionController extends BackendController
foreach ($order->productOrder as $productOrder) {
if ($productOrder->id_product == $product['id']) {
if ($productOrder->invoice_price) {
$invoicePrice = number_format($productOrder->invoice_price, 3);
$invoicePrice = number_format($productOrder->invoice_price, 5);
} else {
$invoicePrice = number_format($productOrder->price, 3);
$invoicePrice = number_format($productOrder->price, 5);
}
$quantity = $productOrder->quantity;
}
@@ -573,225 +574,44 @@ class DistributionController extends BackendController
$distributionManager->generateDistributionReportCsv($distribution);
}
}

return null;
}

public function actionReportGrid($date = '', $save = false, $idProducer = 0, $type = "pdf")
public function actionReportGrid(string $date = '', bool $save = false, int $idProducer = 0)
{
$producerManager = $this->getProducerManager();
$productDistribution = $this->getProductDistributionManager();
$orderManager = $this->getOrderManager();
$productCategoryManager = $this->getProductCategoryManager();

if (!\Yii::$app->user->isGuest) {
$idProducer = GlobalParam::getCurrentProducerId();
if (!$idProducer) {
$idProducer = $this->getProducerCurrent()->id;
}

$distribution = Distribution::searchOne([
'id_producer' => $idProducer
], [
'conditions' => 'date LIKE :date',
'params' => [':date' => $date]
]);
$distributionManager = $this->getDistributionManager();
$producerManager = $this->getProducerManager();
$producerCurrent = $producerManager->findOneProducerById($idProducer);
$this->getLogic()->setProducerContext($producerCurrent);
$distribution = $distributionManager->findOneDistribution($date);

if ($distribution) {
$ordersArray = Order::searchAll(
[
'distribution.date' => $date,
'distribution.id_producer' => $idProducer
],
[
'orderby' => 'user.lastname ASC, user.name ASC, comment_point_sale ASC',
'conditions' => 'date_delete IS NULL'
]
);

$selectedProductsArray = $productDistribution->findProductDistributionsByDistribution($distribution);
$pointsSaleArray = PointSale::searchAll([
'point_sale.id_producer' => $idProducer
]);

foreach ($pointsSaleArray as $pointSale) {
$orderManager->initPointSaleOrders($pointSale, $ordersArray);
}

$ordersByPage = 22;
$nbPages = ceil(count($ordersArray) / $ordersByPage);
$ordersArrayPaged = [];

foreach ($pointsSaleArray as $pointSale) {
$index = 0;
$indexPage = 0;

foreach ($pointSale->orders as $order) {
if (!isset($ordersArrayPaged[$pointSale->id])) {
$ordersArrayPaged[$pointSale->id] = [];
}
if (!isset($ordersArrayPaged[$pointSale->id][$indexPage])) {
$ordersArrayPaged[$pointSale->id][$indexPage] = [];
}
$ordersArrayPaged[$pointSale->id][$indexPage][] = $order;

$index++;

if ($index == $ordersByPage) {
$index = 0;
$indexPage++;
}
}
}

// catégories
$categoriesArray = $productCategoryManager->findProductCategories();
array_unshift($categoriesArray, null);

// produits
$productsArray = Product::find()
->joinWith([
'productDistribution' => function ($q) use ($distribution) {
$q->where(['id_distribution' => $distribution->id]);
}
])
->where([
'id_producer' => $idProducer,
])
->orderBy('order ASC')
->all();


$viewPdf = 'report-grid';
$orientationPdf = Pdf::ORIENT_PORTRAIT;
$producer = GlobalParam::getCurrentProducer();
if ($producer->slug == 'bourlinguepacotille') {
$viewPdf = 'report-bourlingue';
$orientationPdf = Pdf::ORIENT_LANDSCAPE;
}

// get your HTML raw content without any layouts or scripts
$content = $this->renderPartial($viewPdf, [
'date' => $date,
'distribution' => $distribution,
'selectedProductsArray' => $selectedProductsArray,
'pointsSaleArray' => $pointsSaleArray,
'categoriesArray' => $categoriesArray,
'productsArray' => $productsArray,
'ordersArray' => $ordersArrayPaged,
'producer' => $producerManager->findOneProducerById($idProducer)
]);

$dateStr = date('d/m/Y', strtotime($date));

if ($save) {
$destination = Pdf::DEST_FILE;
} else {
$destination = Pdf::DEST_BROWSER;
}

$pdf = new Pdf([
// set to use core fonts only
'mode' => Pdf::MODE_UTF8,
// A4 paper format
'format' => Pdf::FORMAT_A4,
// portrait orientation
'orientation' => $orientationPdf,
// stream to browser inline
'destination' => $destination,
'filename' => \Yii::getAlias(
'@app/web/pdf/Commandes-' . $date . '-' . $idProducer . '.pdf'
),
// your html content input
'content' => $content,
// format content from your own css file if needed or use the
// enhanced bootstrap css built by Krajee for mPDF formatting
//'cssFile' => Yii::getAlias('@web/css/distribution/report.css'),
// any css to be embedded if required
'cssInline' => '
table {
border-spacing : 0px ;
border-collapse : collapse ;
width: 100% ;
}
table tr th,
table tr td {
padding: 0px ;
margin: 0px ;
border: solid 1px #e0e0e0 ;
padding: 3px ;
vertical-align : top;
page-break-inside: avoid !important;
}

table tr th {
font-size: 10px ;
}
table tr td {
font-size: 10px ;
}
table thead tr {
line-height: 220px;
text-align:left;
}
.th-user,
.td-nb-products {
/* width: 35px ; */
text-align: center ;
}
.th-user {
padding: 10px ;
}
.category-name {
font-weight: bold ;
}
',
// set mPDF properties on the fly
//'options' => ['title' => 'Krajee Report Title'],
// call mPDF methods on the fly
'methods' => [
'SetHeader' => ['Commandes du ' . $dateStr],
'SetFooter' => ['{PAGENO}'],
]
]);

// return the pdf output as per the destination setting
return $pdf->render();
return $distributionManager->generateDistributionReportGridPdf($distribution, $save);
}
}

public function actionAjaxProcessProductQuantityMax($idDistribution, $idProduct, $quantityMax)
{
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

$productDistributionManager = $this->getProductDistributionManager();
$productDistribution = $this->getProductDistribution($idProduct, $idDistribution);

$productDistribution->quantity_max = ($quantityMax == -1) ? null : (float) abs($quantityMax);
$productDistributionManager->update($productDistribution);

$productDistributionManager->updateProductDistributionQuantityMax($productDistribution, $quantityMax);
return ['success'];
}

public function actionAjaxProcessActiveProduct($idDistribution, $idProduct, $active)
public function actionAjaxProcessActiveProduct(int $idDistribution, int $idProduct, int $active)
{
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

$productDistributionManager = $this->getProductDistributionManager();
$productDistribution = $this->getProductDistribution($idProduct, $idDistribution);

$productDistribution->active = $active;
$productDistributionManager->update($productDistribution);

$productDistributionManager->updateProductDistributionActive($productDistribution, $active);
return ['success'];
}

public function actionAjaxProcessActivePointSale($idDistribution, $idPointSale, $delivery)
public function actionAjaxProcessActivePointSale(int $idDistribution, int $idPointSale, int $delivery)
{
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

@@ -829,12 +649,10 @@ class DistributionController extends BackendController
* @param boolean $active
* @return array
*/
public function actionAjaxProcessActiveDistribution($idDistribution = 0, $date = '', $active)
public function actionAjaxProcessActiveDistribution(int $idDistribution = 0, string $date = '', bool $active = false)
{
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

$distributionManager = $this->getDistributionManager();
$producerCurrent = $this->getProducerCurrent();

if ($idDistribution) {
$distribution = $distributionManager->findOneDistributionById($idDistribution);
@@ -846,7 +664,7 @@ class DistributionController extends BackendController
$distribution = $distributionManager->createDistributionIfNotExist($date);
}

if ($distribution) {
if (isset($distribution) && $distribution) {
$distributionManager->activeDistribution($distribution, $active);
return ['success'];
}

+ 1
- 1
backend/web/js/vuejs/distribution-index.js Vedi File

@@ -997,7 +997,7 @@ Vue.component('order-form',{
price = getPrice(priceWithTax, taxRateValue);
}
else {
price = priceValue.toFixed(3);
price = priceValue.toFixed(5);
priceWithTax = getPriceWithTax(price, taxRateValue);
}


+ 0
- 10
common/logic/Distribution/Distribution/Service/DistributionReportGridCsvGenerator.php Vedi File

@@ -1,10 +0,0 @@
<?php

namespace common\logic\Distribution\Distribution\Service;

use common\logic\AbstractGenerator;

class DistributionReportGridCsvGenerator extends AbstractGenerator
{

}

+ 164
- 0
common/logic/Distribution/Distribution/Service/DistributionReportGridPdfGenerator.php Vedi File

@@ -0,0 +1,164 @@
<?php

namespace common\logic\Distribution\Distribution\Service;

use common\logic\AbstractGenerator;
use common\logic\Distribution\Distribution\Model\Distribution;
use common\logic\Distribution\ProductDistribution\Repository\ProductDistributionRepository;
use common\logic\Order\Order\Repository\OrderRepository;
use common\logic\Order\Order\Service\OrderBuilder;
use common\logic\PointSale\PointSale\Repository\PointSaleRepository;
use common\logic\Product\Product\Repository\ProductRepository;
use common\logic\Product\ProductCategory\Repository\ProductCategoryRepository;
use kartik\mpdf\Pdf;

class DistributionReportGridPdfGenerator extends AbstractGenerator
{
protected OrderRepository $orderRepository;
protected OrderBuilder $orderBuilder;
protected ProductDistributionRepository $productDistributionRepository;
protected PointSaleRepository $pointSaleRepository;
protected ProductCategoryRepository $productCategoryRepository;
protected ProductRepository $productRepository;

public function loadDependencies(): void
{
$this->orderRepository = $this->loadService(OrderRepository::class);
$this->orderBuilder = $this->loadService(OrderBuilder::class);
$this->productDistributionRepository = $this->loadService(ProductDistributionRepository::class);
$this->pointSaleRepository = $this->loadService(PointSaleRepository::class);
$this->productCategoryRepository = $this->loadService(ProductCategoryRepository::class);
$this->productRepository = $this->loadService(ProductRepository::class);
}

public function generateDistributionReportGridPdf(Distribution $distribution, bool $save = false)
{
$producer = $this->getProducerContext();
$ordersArray = $this->orderRepository->findOrdersByDistribution($distribution);
$selectedProductsArray = $this->productDistributionRepository->findProductDistributionsByDistribution($distribution);
$pointsSaleArray = $this->pointSaleRepository->findPointSales();
foreach ($pointsSaleArray as $pointSale) {
$this->orderBuilder->initPointSaleOrders($pointSale, $ordersArray);
}

$ordersByPage = 22;
$ordersArrayPaged = [];

foreach ($pointsSaleArray as $pointSale) {
$index = 0;
$indexPage = 0;

foreach ($pointSale->orders as $order) {
if (!isset($ordersArrayPaged[$pointSale->id])) {
$ordersArrayPaged[$pointSale->id] = [];
}
if (!isset($ordersArrayPaged[$pointSale->id][$indexPage])) {
$ordersArrayPaged[$pointSale->id][$indexPage] = [];
}
$ordersArrayPaged[$pointSale->id][$indexPage][] = $order;

$index++;

if ($index == $ordersByPage) {
$index = 0;
$indexPage++;
}
}
}

$categoriesArray = $this->productCategoryRepository->findProductCategories();
array_unshift($categoriesArray, null);
$productsArray = $this->productRepository->findProductsByDistribution($distribution);

$viewPdf = '@backend/views/distribution/report-grid';
$orientationPdf = Pdf::ORIENT_PORTRAIT;

if ($producer->slug == 'bourlinguepacotille') {
$viewPdf = '@backend/views/distribution/report-bourlingue';
$orientationPdf = Pdf::ORIENT_LANDSCAPE;
}

// get your HTML raw content without any layouts or scripts
$content = \Yii::$app->getView()->render($viewPdf, [
'date' => $distribution->date,
'distribution' => $distribution,
'selectedProductsArray' => $selectedProductsArray,
'pointsSaleArray' => $pointsSaleArray,
'categoriesArray' => $categoriesArray,
'productsArray' => $productsArray,
'ordersArray' => $ordersArrayPaged,
'producer' => $producer
]);

$dateStr = date('d/m/Y', strtotime($distribution->date));

if ($save) {
$destination = Pdf::DEST_FILE;
} else {
$destination = Pdf::DEST_BROWSER;
}

$pdf = new Pdf([
'mode' => Pdf::MODE_UTF8,
'format' => Pdf::FORMAT_A4,
'orientation' => $orientationPdf,
'destination' => $destination,
'filename' => \Yii::getAlias(
'@app/web/pdf/Commandes-' . $distribution->date . '-' . $producer->id . '.pdf'
),
'content' => $content,
//'cssFile' => Yii::getAlias('@web/css/distribution/report.css'),
'cssInline' => '
table {
border-spacing : 0px ;
border-collapse : collapse ;
width: 100% ;
}
table tr th,
table tr td {
padding: 0px ;
margin: 0px ;
border: solid 1px #e0e0e0 ;
padding: 3px ;
vertical-align : top;
page-break-inside: avoid !important;
}

table tr th {
font-size: 10px ;
}
table tr td {
font-size: 10px ;
}
table thead tr {
line-height: 220px;
text-align:left;
}
.th-user,
.td-nb-products {
/* width: 35px ; */
text-align: center ;
}
.th-user {
padding: 10px ;
}
.category-name {
font-weight: bold ;
}
',
'methods' => [
'SetHeader' => ['Commandes du ' . $dateStr],
'SetFooter' => ['{PAGENO}'],
]
]);

return $pdf->render();
}
}

+ 0
- 12
common/logic/Distribution/Distribution/Service/DistributionReportPdfGenerator.php Vedi File

@@ -67,23 +67,15 @@ class DistributionReportPdfGenerator extends AbstractGenerator
}

$pdf = new Pdf([
// set to use core fonts only
'mode' => Pdf::MODE_UTF8,
// A4 paper format
'format' => Pdf::FORMAT_A4,
// portrait orientation
'orientation' => $orientationPdf,
// stream to browser inline
'destination' => $destination,
'filename' => \Yii::getAlias(
'@app/web/pdf/Commandes-' . $distribution->date . '-' . $this->getProducerContextId() . '.pdf'
),
// your html content input
'content' => $content,
// format content from your own css file if needed or use the
// enhanced bootstrap css built by Krajee for mPDF formatting
//'cssFile' => Yii::getAlias('@web/css/distribution/report.css'),
// any css to be embedded if required
'cssInline' => '
table {
border-spacing : 0px ;
@@ -110,16 +102,12 @@ class DistributionReportPdfGenerator extends AbstractGenerator
font-size: 13px ;
}
',
// set mPDF properties on the fly
//'options' => ['title' => 'Krajee Report Title'],
// call mPDF methods on the fly
'methods' => [
'SetHeader' => ['Commandes du ' . $dateStr],
'SetFooter' => ['{PAGENO}'],
]
]);

// return the pdf output as per the destination setting
return $pdf->render();
}
}

+ 2
- 2
common/logic/Distribution/Distribution/Wrapper/DistributionContainer.php Vedi File

@@ -7,7 +7,7 @@ use common\logic\Distribution\Distribution\Repository\DistributionRepository;
use common\logic\Distribution\Distribution\Service\DistributionBuilder;
use common\logic\Distribution\Distribution\Service\DistributionDefinition;
use common\logic\Distribution\Distribution\Service\DistributionReportCsvGenerator;
use common\logic\Distribution\Distribution\Service\DistributionReportGridCsvGenerator;
use common\logic\Distribution\Distribution\Service\DistributionReportGridPdfGenerator;
use common\logic\Distribution\Distribution\Service\DistributionReportPdfGenerator;
use common\logic\Distribution\Distribution\Service\DistributionSolver;

@@ -21,7 +21,7 @@ class DistributionContainer extends AbstractContainer
DistributionRepository::class,
DistributionBuilder::class,
DistributionReportCsvGenerator::class,
DistributionReportGridCsvGenerator::class,
DistributionReportGridPdfGenerator::class,
DistributionReportPdfGenerator::class,
];
}

+ 2
- 2
common/logic/Distribution/Distribution/Wrapper/DistributionManager.php Vedi File

@@ -7,7 +7,7 @@ use common\logic\Distribution\Distribution\Repository\DistributionRepository;
use common\logic\Distribution\Distribution\Service\DistributionBuilder;
use common\logic\Distribution\Distribution\Service\DistributionDefinition;
use common\logic\Distribution\Distribution\Service\DistributionReportCsvGenerator;
use common\logic\Distribution\Distribution\Service\DistributionReportGridCsvGenerator;
use common\logic\Distribution\Distribution\Service\DistributionReportGridPdfGenerator;
use common\logic\Distribution\Distribution\Service\DistributionReportPdfGenerator;
use common\logic\Distribution\Distribution\Service\DistributionSolver;

@@ -17,7 +17,7 @@ use common\logic\Distribution\Distribution\Service\DistributionSolver;
* @mixin DistributionRepository
* @mixin DistributionBuilder
* @mixin DistributionReportCsvGenerator
* @mixin DistributionReportGridCsvGenerator
* @mixin DistributionReportGridPdfGenerator
* @mixin DistributionReportPdfGenerator
*/
class DistributionManager extends AbstractManager

+ 15
- 1
common/logic/Distribution/ProductDistribution/Service/ProductDistributionBuilder.php Vedi File

@@ -46,10 +46,11 @@ class ProductDistributionBuilder extends AbstractBuilder
return $this->productDistributionRepository->findOneProductDistribution($distribution, $product)
?? $this->createProductDistribution($distribution, $product);
}

public function updateProductDistribution(ProductDistribution $productDistribution): ProductDistribution
{
$this->initProductDistribution($productDistribution);
$this->saveUpdate($productDistribution);
$this->update($productDistribution);

return $productDistribution;
}
@@ -82,4 +83,17 @@ class ProductDistributionBuilder extends AbstractBuilder
return $productDistribution;
}

public function updateProductDistributionQuantityMax(ProductDistribution $productDistribution, float $quantityMax)
{
$productDistribution->quantity_max = ($quantityMax == -1) ? null : (float) abs($quantityMax);
$this->update($productDistribution);

return $productDistribution;
}

public function updateProductDistributionActive(ProductDistribution $productDistribution, int $active)
{
$productDistribution->active = $active;
$this->update($productDistribution);
}
}

+ 3
- 3
common/logic/Product/Product/Repository/ProductRepository.php Vedi File

@@ -141,7 +141,7 @@ class ProductRepository extends AbstractRepository
'user_producer' => $userProducer,
'point_sale' => $pointSale,
'quantity' => $specificPrice->from_quantity
]), 3),
]), 5),
'price_with_tax' => number_format($this->productSolver->getPriceWithTax($product, [
'user' => $user,
'user_producer' => $userProducer,
@@ -155,8 +155,8 @@ class ProductRepository extends AbstractRepository
// base price
$priceArray[] = [
'from_quantity' => 0,
'price' => $this->productSolver->getPrice($product),
'price_with_tax' => $this->productSolver->getPriceWithTax($product),
'price' => number_format($this->productSolver->getPrice($product), 5),
'price_with_tax' => number_format($this->productSolver->getPriceWithTax($product), 2),
];
}


Loading…
Annulla
Salva