Sfoglia il codice sorgente

[backend] Distribution : report Terre de pains

dev
parent
commit
eb3c42d201
1 ha cambiato i file con 150 aggiunte e 2 eliminazioni
  1. +150
    -2
      backend/controllers/DistributionController.php

+ 150
- 2
backend/controllers/DistributionController.php Vedi File

@@ -59,7 +59,7 @@ class DistributionController extends BackendController
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['report-cron'],
'actions' => ['report-cron', 'report-terredepains'],
'allow' => true,
'roles' => ['?']
],
@@ -561,6 +561,147 @@ class DistributionController extends BackendController
return null ;
}
/**
* Génère un export des commandes au format CSV à destination du Google Drive
* de Terre de pains.
*
* @param type $date
* @return CSV
*/
public function actionReportTerredepains($date, $key)
{
if($key == 'ef572cc148c001f0180c4a624189ed30') {
$producer = Producer::searchOne([
'producer.slug' => 'terredepains'
]) ;

$idProducer = $producer->id ;

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

$distribution = Distribution::searchOne([
'distribution.id_producer' => $idProducer
],[
'conditions' => 'date LIKE :date',
'params' => [
':date' => $date,
]
]) ;

if ($distribution) {
$selectedProductsArray = ProductDistribution::searchByDistribution($distribution->id) ;
$pointsSaleArray = PointSale::searchAll([
'point_sale.id_producer' => $idProducer
]) ;

foreach($pointsSaleArray as $pointSale) {
$pointSale->initOrders($ordersArray) ;
}

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

$datas = [];

// produits en colonne
$productsNameArray = [''] ;
$productsIndexArray = [] ;
$productsHasQuantity = [] ;
$cpt = 1 ;
foreach ($productsArray as $product) {
$productsHasQuantity[$product->id] = 0 ;
foreach(Product::$unitsArray as $unit => $dataUnit) {
$quantity = Order::getProductQuantity($product->id, $ordersArray, true, $unit);
if($quantity) {
$productsHasQuantity[$product->id] += $quantity ;
}
}
if($productsHasQuantity[$product->id] > 0) {
$theUnit = Product::strUnit($product->unit, 'wording_short', true) ;
$theUnit = ($theUnit == 'p.') ? '' : ' ('.$theUnit.')' ;
$productsNameArray[] = $product->name .$theUnit ;
$productsIndexArray[$product->id] = $cpt ++ ;
}
}
$datas[] = $productsNameArray ;

// global
$totalsGlobalArray = $this->_totalReportCSV(
'> Totaux',
$ordersArray,
$productsArray,
$productsIndexArray
) ;
$datas[] = $this->_lineOrderReportCSV($totalsGlobalArray, $cpt - 1, true) ;
$datas[] = [] ;
$datas[] = [] ;
$datas[] = [] ;
// 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($productsIndexArray as $idProduct => $indexProduct) {
$orderLine[$indexProduct] = '' ;
}

foreach($order->productOrder as $productOrder) {
if(strlen($orderLine[$productsIndexArray[$productOrder->id_product]])) {
$orderLine[$productsIndexArray[$productOrder->id_product]] .= ' + ' ;
}
$orderLine[$productsIndexArray[$productOrder->id_product]] .= $productOrder->quantity;
if($productOrder->product->unit != $productOrder->unit) {
$orderLine[$productsIndexArray[$productOrder->id_product]] .= Product::strUnit($productOrder->unit, 'wording_short', true) ;
}
}
$datas[] = $this->_lineOrderReportCSV($orderLine, $cpt - 1, true) ;
}

// total point de vente
$totalsPointSaleArray = $this->_totalReportCSV(
'Total',
$pointSale->orders,
$productsArray,
$productsIndexArray
) ;
$datas[] = $this->_lineOrderReportCSV($totalsPointSaleArray, $cpt - 1, true) ;
$datas[] = [] ;
}
}

CSV::downloadSendHeaders('Commandes_'.$date.'.csv');
echo CSV::array2csv($datas);
die();

}

return null ;
}
}
public function _totalReportCSV($label, $ordersArray, $productsArray, $productsIndexArray) {
$totalsPointSaleArray = [$label] ;
foreach ($productsArray as $product) {
@@ -587,17 +728,24 @@ class DistributionController extends BackendController
return $totalsPointSaleArray ;
}
public function _lineOrderReportCSV($orderLine, $cptMax)
public function _lineOrderReportCSV($orderLine, $cptMax, $showTotal = false)
{
$line = [] ;
$cptTotal = 0 ;
for($i = 0; $i <= $cptMax ; $i++) {
if(isset($orderLine[$i]) && $orderLine[$i]) {
$line[] = $orderLine[$i] ;
$cptTotal += $orderLine[$i] ;
}
else {
$line[] = '' ;
}
}
if($cptTotal > 0 && $showTotal) {
$line[] = $cptTotal ;
}

return $line ;
}

Loading…
Annulla
Salva