Browse Source

[Administration] Statistiques > Rapports : export CSV

feature/rotating_product
Guillaume Bourgeois 5 months ago
parent
commit
eafa844a3b
5 changed files with 48 additions and 12 deletions
  1. +21
    -6
      backend/controllers/ReportController.php
  2. +3
    -2
      backend/views/report/index.php
  3. +12
    -0
      backend/web/js/backend.js
  4. +11
    -3
      backend/web/js/vuejs/report-index.js
  5. +1
    -1
      domain/Order/Order/OrderRepositoryQuery.php

+ 21
- 6
backend/controllers/ReportController.php View File



public function actionAjaxReport() public function actionAjaxReport()
{ {
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

$posts = Yii::$app->request->post(); $posts = Yii::$app->request->post();
$isDownload = $posts['isDownload'] == 1;
$resArray = []; $resArray = [];
$conditionUsers = $this->_generateConditionSqlReport($posts, 'users', 'id_user'); $conditionUsers = $this->_generateConditionSqlReport($posts, 'users', 'id_user');
$conditionPointsSale = $this->_generateConditionSqlReport($posts, 'pointsSale', 'id_point_sale'); $conditionPointsSale = $this->_generateConditionSqlReport($posts, 'pointsSale', 'id_point_sale');


$totalGlobal = 0; $totalGlobal = 0;
foreach ($res as $line) { foreach ($res as $line) {
$total = Price::format(round($line['total'], 2));
$roundedTotal = round($line['total'], 2);
$total = $isDownload ? $roundedTotal : Price::format($roundedTotal);

$quantity = $line['quantity'];
if((int) $quantity != $quantity) {
$quantity = round($quantity, 3);
}

if ($line['quantity'] > 0) { if ($line['quantity'] > 0) {
$resArray[] = [ $resArray[] = [
'name' => $line['name'], 'name' => $line['name'],
'quantity' => $line['quantity'],
'quantity' => $quantity,
'total' => $total, 'total' => $total,
]; ];
$totalGlobal += $line['total']; $totalGlobal += $line['total'];
} }
} }


$roundedTotalGlobal = round($totalGlobal, 2);
$totalGlobalFormat = $isDownload ? $roundedTotalGlobal : Price::format($roundedTotalGlobal).' HT';

$resArray[] = [ $resArray[] = [
'name' => '', 'name' => '',
'quantity' => '', 'quantity' => '',
'total' => '<strong>' . Price::format(round($totalGlobal, 2)) . ' HT</strong>',
'total' => $totalGlobalFormat,
]; ];


return $resArray;
if($isDownload) {
CSV::send('rapport.csv', $resArray);
}
else {
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
return $resArray;
}
} }


public function _generateConditionSqlReport($posts, $name, $fieldOrder) public function _generateConditionSqlReport($posts, $name, $fieldOrder)

+ 3
- 2
backend/views/report/index.php View File

<tr> <tr>
<th>Produit</th> <th>Produit</th>
<th>Quantité</th> <th>Quantité</th>
<th>Total</th>
<th>Total HT</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
Aucune donnée disponible pour ces critères. Aucune donnée disponible pour ces critères.
</div> </div>
</div> </div>
<a class="btn btn-primary" @click="generateReport()" v-else="!showReport"><span class="fa fa-pencil-square-o"></span> Générer</a>
<a v-else="!showReport" class="btn btn-primary" @click="generateReport(false)"><span class="fa fa-pencil-square-o"></span> Générer</a>
<a class="btn btn-primary" @click="generateReport(true)"><span class="fa fa-download"></span> Télécharger (CSV)</a>
</div> </div>
</div> </div>
</div> </div>

+ 12
- 0
backend/web/js/backend.js View File

opendistrib_tinymce_responsive(); opendistrib_tinymce_responsive();
}); });


function saveData (data, fileName) {
var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
var blob = new Blob([data], {type: "octet/stream"}),
url = window.URL.createObjectURL(blob);
a.href = url;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(url);
}

var UrlManager = { var UrlManager = {
getBaseUrl: function () { getBaseUrl: function () {
return $('meta[name=baseurl]').attr('content') + '/'; return $('meta[name=baseurl]').attr('content') + '/';

+ 11
- 3
backend/web/js/vuejs/report-index.js View File

} }
this.reportChange(); this.reportChange();
}, },
generateReport: function () {
generateReport: function (isDownload) {
var app = this; var app = this;
app.showLoading = true; app.showLoading = true;


idsUsersArray.push(app.usersArray[i].user_id); idsUsersArray.push(app.usersArray[i].user_id);
} }
} }

var idsPointsSaleArray = []; var idsPointsSaleArray = [];
for (var i = 0; i < app.pointsSaleArray.length; i++) { for (var i = 0; i < app.pointsSaleArray.length; i++) {
if (app.pointsSaleArray[i].checked) { if (app.pointsSaleArray[i].checked) {
} }
} }


data.append('isDownload', isDownload ? 1 : 0);
data.append('users', idsUsersArray); data.append('users', idsUsersArray);
data.append('pointsSale', idsPointsSaleArray); data.append('pointsSale', idsPointsSaleArray);
data.append('distributions', idsDistributionsArray); data.append('distributions', idsDistributionsArray);


axios.post("ajax-report", data) axios.post("ajax-report", data)
.then(function (response) { .then(function (response) {
app.tableReport = response.data;
app.showLoading = false; app.showLoading = false;
app.showReport = true;

if(isDownload) {
saveData(response.data, 'rapport.csv');
}
else {
app.tableReport = response.data;
app.showReport = true;
}
}); });


}, },

+ 1
- 1
domain/Order/Order/OrderRepositoryQuery.php View File



public static function getSqlFilterIsValid(): string public static function getSqlFilterIsValid(): string
{ {
return "`order`.order_status_alias = '".OrderStatus::ALIAS_ORDERED."' OR `order`.order_status_alias = '".OrderStatus::ALIAS_UPDATED."'";
return "(`order`.order_status_alias = '".OrderStatus::ALIAS_ORDERED."' OR `order`.order_status_alias = '".OrderStatus::ALIAS_UPDATED."')";
} }


public function filterIsValid(): self public function filterIsValid(): self

Loading…
Cancel
Save