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) |
<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> |
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') + '/'; |
} | } | ||||
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; | |||||
} | |||||
}); | }); | ||||
}, | }, |
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 |