ソースを参照

[backend] Distributions : export en CSV

refactoring
コミット
2d97c2ef85
2個のファイルの変更143行の追加47行の削除
  1. +136
    -44
      backend/controllers/DistributionController.php
  2. +7
    -3
      backend/views/distribution/index.php

+ 136
- 44
backend/controllers/DistributionController.php ファイルの表示

@@ -332,7 +332,7 @@ class DistributionController extends BackendController
* @param integer $idProducer
* @return PDF|null
*/
public function actionReport($date = '', $save = false, $idProducer = 0)
public function actionReport($date = '', $save = false, $idProducer = 0, $type = "pdf")
{
if (!Yii::$app->user->isGuest) {
$idProducer = Producer::getId() ;
@@ -360,59 +360,151 @@ class DistributionController extends BackendController
}

// produits
$productsArray = Product::searchAll() ;
$productsArray = Product::find()
->joinWith(['productDistribution' => function($q) use ($distribution) {
$q->where(['id_distribution' => $distribution->id]);
}])
->where([
'id_producer' => Producer::getId(),
])
->orderBy('order ASC')
->all();
if($type == 'pdf') {
// get your HTML raw content without any layouts or scripts
$content = $this->renderPartial('report', [
'date' => $date,
'distribution' => $distribution,
'selectedProductsArray' => $selectedProductsArray,
'pointsSaleArray' => $pointsSaleArray,
'productsArray' => $productsArray,
'ordersArray' => $ordersArray
]);

// get your HTML raw content without any layouts or scripts
$content = $this->renderPartial('report', [
'date' => $date,
'distribution' => $distribution,
'selectedProductsArray' => $selectedProductsArray,
'pointsSaleArray' => $pointsSaleArray,
'productsArray' => $productsArray,
'ordersArray' => $ordersArray
]);
$dateStr = date('d/m/Y', strtotime($date));

$dateStr = date('d/m/Y', strtotime($date));
if ($save) {
$destination = Pdf::DEST_FILE;
} else {
$destination = Pdf::DEST_BROWSER;
}

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' => Pdf::ORIENT_PORTRAIT,
// 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' => '@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css',
// any css to be embedded if required
//'cssInline' => '.kv-heading-1{font-size:18px}',
// set mPDF properties on the fly
//'options' => ['title' => 'Krajee Report Title'],
// call mPDF methods on the fly
'methods' => [
'SetHeader' => ['Commandes du ' . $dateStr],
'SetFooter' => ['{PAGENO}'],
]
]);

$pdf = new Pdf([
// set to use core fonts only
'mode' => Pdf::MODE_UTF8,
// A4 paper format
'format' => Pdf::FORMAT_A4,
// portrait orientation
'orientation' => Pdf::ORIENT_PORTRAIT,
// 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' => '@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css',
// any css to be embedded if required
//'cssInline' => '.kv-heading-1{font-size:18px}',
// 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();
}
elseif($type == 'csv') {
$datas = [];
// produits en colonne
$productsNameArray = [''] ;
$productsIndexArray = [] ;
$cpt = 1 ;
foreach ($productsArray as $product) {
$quantity = Order::getProductQuantity($product->id, $ordersArray);
if($quantity) {
$productsNameArray[] = $product->name ;
$productsIndexArray[$product->id] = $cpt ++ ;
}
}
$datas[] = $productsNameArray ;
// points de vente
foreach ($pointsSaleArray as $pointSale) {
if (count($pointSale->orders)) {
// listing commandes
$datas[] = ['> '.$pointSale->name] ;
foreach($pointSale->orders as $order) {
$orderLine = [$order->getStrUser()] ;
foreach($order->productOrder as $productOrder) {
$orderLine[$productsIndexArray[$productOrder->id_product]] = $productOrder->quantity ;
}
$datas[] = $this->_lineOrderReportCSV($orderLine, $cpt) ;
}
// total point de vente
$totalsPointSaleArray = ['Total'] ;
foreach ($productsArray as $product) {
$quantity = Order::getProductQuantity($product->id, $pointSale->orders);
if($quantity) {
$index = $productsIndexArray[$product->id] ;
if(!isset($totalsPointSaleArray[$index])) {
$totalsPointSaleArray[$index] = 0 ;
}
$totalsPointSaleArray[$index] += $quantity ;
}
}
$datas[] = $this->_lineOrderReportCSV($totalsPointSaleArray, $cpt) ;
$datas[] = [] ;
}
}
// global
$totalsGlobalArray = ['> Totaux'] ;
foreach ($productsArray as $product) {
$quantity = Order::getProductQuantity($product->id, $ordersArray);
if($quantity) {
$index = $productsIndexArray[$product->id] ;
if(!isset($totalsGlobalArray[$index])) {
$totalsGlobalArray[$index] = 0 ;
}
$totalsGlobalArray[$index] += $quantity ;
}
}

// return the pdf output as per the destination setting
return $pdf->render();
$datas[] = $this->_lineOrderReportCSV($totalsGlobalArray, $cpt) ;
CSV::downloadSendHeaders('Commandes_'.$date.'.csv');
echo CSV::array2csv($datas);
die();
}
}
return null ;
}
public function _lineOrderReportCSV($orderLine, $cptMax)
{
$line = [] ;
for($i = 0; $i <= $cptMax ; $i++) {
if(isset($orderLine[$i]) && $orderLine[$i]) {
$line[] = $orderLine[$i] ;
}
else {
$line[] = '' ;
}
}

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

+ 7
- 3
backend/views/distribution/index.php ファイルの表示

@@ -184,9 +184,13 @@ $this->setPageTitle('Distributions') ;
<span class="info-box-icon bg-yellow"><i class="fa fa-download"></i></span>
<div class="info-box-content">
<span class="info-box-text">
{{ countOrders }} Commande<span v-if="countOrders > 1">s</span><br /><br />
<a href="#" class="btn btn-default" disabled="disabled" v-if="countOrders == 0">Télécharger (PDF)</a>
<a :href="distribution.url_report" class="btn btn-default" v-else>Télécharger (PDF)</a>
{{ countOrders }} Commande<span v-if="countOrders > 1">s</span><br />
<a href="#" class="btn btn-xs btn-default" disabled="disabled" v-if="countOrders == 0">Télécharger (PDF)</a>
<a :href="distribution.url_report" class="btn btn-default">Télécharger (PDF)</a><br />
<a href="#" class="btn btn-xs btn-default" disabled="disabled" v-if="countOrders == 0">Télécharger (CSV)</a>
<a :href="distribution.url_report+'&type=csv'" class="btn btn-default">Télécharger (CSV)</a>
</span>
</div>
</div>

読み込み中…
キャンセル
保存