121 lines
3.7 KiB
PHP
121 lines
3.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
/**
|
|
* Calendar App
|
|
*
|
|
* @copyright 2021 Anna Larch <anna.larch@gmx.net>
|
|
*
|
|
* @author Anna Larch <anna.larch@gmx.net>
|
|
* @author Richard Steinmetz <richard@steinmetz.cloud>
|
|
*
|
|
* This library is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
|
* License as published by the Free Software Foundation; either
|
|
* version 3 of the License, or any later version.
|
|
*
|
|
* This library is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
*
|
|
*/
|
|
|
|
namespace OCA\Gestion\Service;
|
|
|
|
use OCA\Gestion\Db\Bdd;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
class ExportClientStatisticService {
|
|
/** @var Bdd */
|
|
private $gestionBdd;
|
|
|
|
/** @var LoggerInterface */
|
|
private $logger;
|
|
|
|
public function __construct(
|
|
Bdd $gestionBdd,
|
|
LoggerInterface $logger) {
|
|
$this->logger = $logger;
|
|
$this->gestionBdd = $gestionBdd;
|
|
}
|
|
|
|
public function getFileName(array $clientIds){
|
|
$filename = "";
|
|
$clients = $this->gestionBdd->getClientsByClientsID($clientIds);
|
|
foreach($clients as $client){
|
|
$filename .= $client['client_nom'] . '-' . $client['client_entreprise'] . '--';
|
|
}
|
|
$filename = rtrim($filename, '-');
|
|
$filename = str_replace(' ','-', $filename);
|
|
$filename = str_replace(' ','-', $filename);
|
|
return $filename;
|
|
}
|
|
|
|
public function getExportClientFileHeader($exportData): string{
|
|
$fileHeader =
|
|
'Client'.';'.
|
|
'Mois'.';'.
|
|
utf8_decode(html_entity_decode('Année')).';'.
|
|
utf8_decode(html_entity_decode('Nb défunts')).';'.
|
|
'Nb articles'.';'.
|
|
'Total HT'.';';
|
|
|
|
$produitList = $this->gestionBdd->getProduitsListAsArray();
|
|
foreach($produitList as $produit){
|
|
$fileHeader .= $produit['reference'].';';
|
|
}
|
|
$fileHeader .= "\n";
|
|
return $fileHeader;
|
|
}
|
|
|
|
public function populateExportDataIntoFileContent(array $exportData,string $fileContent): string{
|
|
foreach($exportData as $clientId => $clientData){
|
|
$clientName = $clientData["client_name"];
|
|
$clientStatPerMonth = $clientData["client_data"];
|
|
$totalPrice = 0;
|
|
if(!empty($clientStatPerMonth)){
|
|
foreach($clientStatPerMonth as $month => $stat){
|
|
$stat["client_name"] = $clientName;
|
|
$totalPrice+=$stat["total_price"];
|
|
$fileContent = $this->populateClientStatDataIntoFileContent($fileContent,$month,$stat);
|
|
}
|
|
$fileContent = $this->populateTotalPriceIntoFileContent($fileContent,$totalPrice);
|
|
}
|
|
}
|
|
return $fileContent;
|
|
}
|
|
|
|
private function populateTotalPriceIntoFileContent(string $fileContent,$price){
|
|
$fileContent = $fileContent.
|
|
''.';'.
|
|
''.';'.
|
|
''.';'.
|
|
''.';'.
|
|
''.';'.
|
|
utf8_decode(html_entity_decode("$price"))."\n";
|
|
return $fileContent;
|
|
}
|
|
|
|
private function populateClientStatDataIntoFileContent(string $fileContent,$month,array $statPerMonth){
|
|
$yearValue = $statPerMonth["year"];
|
|
$defuntCount = $statPerMonth["defunt_count"];
|
|
$productCount = $statPerMonth["produit_count"];
|
|
$totalPrice = $statPerMonth["total_price"];
|
|
|
|
$fileContent = $fileContent.
|
|
utf8_decode(html_entity_decode($statPerMonth['client_name'])).';'.
|
|
utf8_decode(html_entity_decode("$month")).';'.
|
|
utf8_decode(html_entity_decode("$yearValue")).';'.
|
|
utf8_decode(html_entity_decode("$defuntCount")).';'.
|
|
utf8_decode(html_entity_decode("$productCount")).';'.
|
|
utf8_decode(html_entity_decode("$totalPrice")).';'."\n";
|
|
|
|
return $fileContent;
|
|
|
|
}
|
|
}
|