Browse Source

Merge branch 'dev'

prodstable
Guillaume Bourgeois 5 years ago
parent
commit
b1c37803b7
1 changed files with 150 additions and 2 deletions
  1. +150
    -2
      backend/controllers/DistributionController.php

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

'class' => AccessControl::className(), 'class' => AccessControl::className(),
'rules' => [ 'rules' => [
[ [
'actions' => ['report-cron'],
'actions' => ['report-cron', 'report-terredepains'],
'allow' => true, 'allow' => true,
'roles' => ['?'] 'roles' => ['?']
], ],
return null ; 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) { public function _totalReportCSV($label, $ordersArray, $productsArray, $productsIndexArray) {
$totalsPointSaleArray = [$label] ; $totalsPointSaleArray = [$label] ;
foreach ($productsArray as $product) { foreach ($productsArray as $product) {
return $totalsPointSaleArray ; return $totalsPointSaleArray ;
} }
public function _lineOrderReportCSV($orderLine, $cptMax)
public function _lineOrderReportCSV($orderLine, $cptMax, $showTotal = false)
{ {
$line = [] ; $line = [] ;
$cptTotal = 0 ;
for($i = 0; $i <= $cptMax ; $i++) { for($i = 0; $i <= $cptMax ; $i++) {
if(isset($orderLine[$i]) && $orderLine[$i]) { if(isset($orderLine[$i]) && $orderLine[$i]) {
$line[] = $orderLine[$i] ; $line[] = $orderLine[$i] ;
$cptTotal += $orderLine[$i] ;
} }
else { else {
$line[] = '' ; $line[] = '' ;
} }
} }
if($cptTotal > 0 && $showTotal) {
$line[] = $cptTotal ;
}


return $line ; return $line ;
} }

Loading…
Cancel
Save