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

@@ -123,9 +123,8 @@ class ReportController extends BackendController

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

$posts = Yii::$app->request->post();
$isDownload = $posts['isDownload'] == 1;
$resArray = [];
$conditionUsers = $this->_generateConditionSqlReport($posts, 'users', 'id_user');
$conditionPointsSale = $this->_generateConditionSqlReport($posts, 'pointsSale', 'id_point_sale');
@@ -147,24 +146,40 @@ class ReportController extends BackendController

$totalGlobal = 0;
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) {
$resArray[] = [
'name' => $line['name'],
'quantity' => $line['quantity'],
'quantity' => $quantity,
'total' => $total,
];
$totalGlobal += $line['total'];
}
}

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

$resArray[] = [
'name' => '',
'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)

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

@@ -141,7 +141,7 @@ $this->addBreadcrumb('Rapports') ;
<tr>
<th>Produit</th>
<th>Quantité</th>
<th>Total</th>
<th>Total HT</th>
</tr>
</thead>
<tbody>
@@ -156,7 +156,8 @@ $this->addBreadcrumb('Rapports') ;
Aucune donnée disponible pour ces critères.
</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>

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

@@ -61,6 +61,18 @@ $(document).ready(function () {
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 = {
getBaseUrl: function () {
return $('meta[name=baseurl]').attr('content') + '/';

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

@@ -99,7 +99,7 @@ if($(selector).length) {
}
this.reportChange();
},
generateReport: function () {
generateReport: function (isDownload) {
var app = this;
app.showLoading = true;

@@ -110,6 +110,7 @@ if($(selector).length) {
idsUsersArray.push(app.usersArray[i].user_id);
}
}

var idsPointsSaleArray = [];
for (var i = 0; i < app.pointsSaleArray.length; i++) {
if (app.pointsSaleArray[i].checked) {
@@ -126,15 +127,22 @@ if($(selector).length) {
}
}

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

axios.post("ajax-report", data)
.then(function (response) {
app.tableReport = response.data;
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

@@ -58,7 +58,7 @@ class OrderRepositoryQuery extends AbstractRepositoryQuery

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

Loading…
Cancel
Save