Thanasoft-H2F/gestion/lib/Service/Provider/Statistic/ProviderStatisticService.php

168 lines
6.4 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\Provider\Statistic;
use OCA\Gestion\Constants\BddConstant;
use OCA\Gestion\Db\Bdd;
use OCA\Gestion\Db\OrderBdd;
use OCA\Gestion\Db\ProviderRepository;
use OCA\Gestion\Helpers\FileExportHelpers;
use OCP\Files\IRootFolder;
use Psr\Log\LoggerInterface;
class ProviderStatisticService {
private $providerRepository;
private $providerStatisticService;
private $orderRepository;
private $gestionRepository;
/** @var LoggerInterface */
private $logger;
/** @var \OCP\Files\IRootFolder */
private $rootFolder;
public function __construct(
ProviderRepository $providerRepository,
LoggerInterface $logger,
OrderBdd $orderBdd,
Bdd $gestionRepository,
IRootFolder $rootFolder) {
$this->logger = $logger;
$this->providerRepository = $providerRepository;
$this->orderRepository = $orderBdd;
$this->gestionRepository = $gestionRepository;
$this->rootFolder = $rootFolder;
}
private function getProviderOrdersStatisticDataByYear($providerId,$year,$orderProducts){
$monthLists = range(1,12);
$data = [];
$provider = $this->providerRepository->getProviderById($providerId);
if($provider == null){
return [];
}
foreach($monthLists as $monthValue){
if(!isset($data[$monthValue])){
$data[$monthValue] = [];
}
$providerOrders = $this->orderRepository->getProviderOrdersByMonthAndYear($providerId,$monthValue,$year);
$providerOrderIds = [];
$statisticForeachProductPerMonth = [];
foreach($providerOrders as $currentOrder){
$providerOrderIds[] = $currentOrder['id'];
}
$productTotalPrices = 0;
foreach($orderProducts as $currentProduct){
if(!isset($statisticForeachProductPerMonth[$currentProduct->id])){
$statisticForeachProductPerMonth[$currentProduct->id] = 0;
}
$productTotalCount = $this->orderRepository->getItemCountByOrderIdListAndOrderProductId($providerOrderIds,$currentProduct->id);
$totalWithoutVat = $productTotalCount * $currentProduct->ht_amount;
$statisticForeachProductPerMonth[$currentProduct->id] += $productTotalCount;
$productTotalPrices += $totalWithoutVat;
}
$data[$monthValue] = [
'products' => $statisticForeachProductPerMonth,
'year' => $year,
'total_price' => $productTotalPrices,
'provider_name' => $provider['provider_name']
];
}
return $data;
}
private function getExportProviderStatisticFileHeader(array $orderProducts){
$fileHeader =
'FOURNISSEUR'.';'.
'MOIS'.';'.
'ANNEE'.';';
foreach($orderProducts as $currentProduct){
$fileHeader .= FileExportHelpers::FormatTextForExport($currentProduct->reference).';';
}
$fileHeader .= 'TOTAL HT'.';'."\n";
return $fileHeader;
}
private function exportProviderStatisticData($providerStatisticData,$orderProducts,$idNextCloud){
if(empty($providerStatisticData)){
return null;
}
$defaultConfig = $this->gestionRepository->getConfigurationByIdNextcloud(BddConstant::DEFAULT_ADMIN_APP_ID_NEXTCLOUD);
$racineFolder = html_entity_decode($defaultConfig['path']).'/';
$statFolder = $racineFolder.'STATISTIQUES/FOURNISSEURS/'.$providerStatisticData[1]['year'].'/';
$storage = $this->rootFolder->getUserFolder($idNextCloud);
try {
$storage->newFolder($statFolder);
}
catch(\OCP\Files\NotPermittedException $e) {
}
$fileContent = $this->getExportProviderStatisticFileHeader($orderProducts);
$fileContent = $this->populateProviderStatisticDataIntoFileContent($providerStatisticData,$fileContent);
$filename = $providerStatisticData[1]['year'].'-'.$providerStatisticData[1]['provider_name'];
$filenamePath = $statFolder."STAT-FOURNISSEUR-".$filename.'.csv';
$storage->newFile($filenamePath);
$file = $storage->get($filenamePath);
$file->putContent($fileContent);
return $filenamePath;
}
private function populateProviderStatisticDataIntoFileContent(array $providerStatisticData,$fileContent){
foreach($providerStatisticData as $monthValue => $currentMonthData){
$fileContent .= FileExportHelpers::FormatTextForExport($currentMonthData['provider_name']).';';
$fileContent .= $monthValue.';';
$fileContent .= $currentMonthData['year'].';';
foreach($currentMonthData['products'] as $currentProduct){
$fileContent .= $currentProduct.';';
}
$totalPrice = $currentMonthData['total_price'];
$fileContent .= "$totalPrice".";"."\n";
}
return $fileContent;
}
public function exportProvidersStatisticByYear($providerIds,$year,$idNextCloud){
$year = $year ?? date('Y');
$filenames = [];
$orderProducts = $this->orderRepository->getOrderProducts();
$orderProducts = json_decode($orderProducts);
foreach($providerIds as $providerId){
$providerOrdersPerMonthInAYear = $this->getProviderOrdersStatisticDataByYear($providerId,$year,$orderProducts);
$filename = $this->exportProviderStatisticData($providerOrdersPerMonthInAYear,$orderProducts,$idNextCloud);
if($filename != null){
$filenames[] = $filename;
}
}
return $filenames;
}
}