<?php
namespace App\Admin\Controller;
use App\Admin\Entity\Invoice;
use App\Admin\Entity\Item;
use App\Admin\Entity\ItemInvoice;
use App\Admin\Entity\Notification;
use App\Admin\Entity\Potm;
use App\Admin\Entity\Account;
use App\Admin\Entity\ApiToken;
use App\Admin\Entity\Reservation;
use App\Admin\Entity\Settings;
use App\Service\FirebaseTokenManager;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use IntlDateFormatter;
use Knp\Bundle\TimeBundle\DateTimeFormatter;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\Request;
use \Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Stripe\InvoiceItem;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use YouCan\Pay\YouCanPay;
class HomeController extends AbstractController
{
private $cache;
public function __construct(CacheInterface $cache)
{
$this->cache = $cache;
}
function send_notificationTokens($tokens, $message, $key)
{
$firebaseTokenManager = new FirebaseTokenManager($this->cache, $this->getParameter('kernel.project_dir'));
$res = $firebaseTokenManager->send_notificationTokens($tokens, $message, $key);
return $res;
}
public function index(MailerInterface $mailer,CacheManager $imagineCacheManager, TranslatorInterface $translator, EntityManagerInterface $entityManager): Response
{
$em = $entityManager;
$image = $imagineCacheManager->getBrowserPath("/admin/img/admin.png", 'thumb_500');
$notifMessage = "message" ;
$notificationMessage = [
"id"=>random_int(1, 99999)."",
"title"=>"rfzefez",
"message"=>$notifMessage,
"icon"=>$image,
"type"=>"messages",
"cid"=> 232,
"mid"=> 23,
"created"=> "Now",
"latitude" =>2.322,
"longitude" =>3.22,
"mtype" => 33,
"image"=> null,
"original"=> "ok",
"first"=>"kkk" ,
"uid"=> "" ,
"ufname"=> "",
"ulname"=> "",
"uimage"=>"",
"uemail"=> "",
'click_action' => 'FLUTTER_NOTIFICATION_CLICK',
];
$settings = $em->getRepository(Settings::class)->findOneBy(array());
//$dd = $this->send_notificationTokens($token,$notificationMessage,$settings->getFirebasekey());
$this->checkPotm($entityManager, $imagineCacheManager, $translator);
$start = new \DateTime;
$start->setDate($start->format('Y'), $start->format('n'), 1); // Normalize the day to 1
$start->setTime(0, 0, 0); // Normalize time to midnight
$start->sub(new \DateInterval('P12M'));
$interval = new \DateInterval('P1M');
$recurrences = 12;
$date_list = array();
foreach (new \DatePeriod($start, $interval, $recurrences, true) as $date) {
$date_list[] = $date; // attempting to make it more clear to read here
}
$date_list = array_reverse($date_list);
$new = new \DateTime;
$year_user = $new->format("Y");
$month_user = $new->format("m");
$_selected_user = 0;
if (isset($_GET["date_user"])) {
$year_user = $date_list[$_GET["date_user"]]->format("Y");
$month_user = $date_list[$_GET["date_user"]]->format("m");
$_selected_user = $_GET["date_user"];
}
$result_ = $em->getRepository("App\Auth\Entity\User")
->createQueryBuilder('visiteur')
->select('DAY(visiteur.created) AS date_, count(visiteur) as count_')
->where("YEAR(visiteur.created) = " . $year_user, "MONTH(visiteur.created) = " . $month_user)
->groupBy('date_')
->getQuery()
->getResult();
$year_matche = $new->format("Y");
$month_matche = $new->format("m");
$_selected_matche = 0;
if (isset($_GET["date_matche"])) {
$year_matche = $date_list[$_GET["date_matche"]]->format("Y");
$month_matche = $date_list[$_GET["date_matche"]]->format("m");
$_selected_matche = $_GET["date_matche"];
}
$result_matches = $em->getRepository("App\Admin\Entity\Matche")
->createQueryBuilder('matche')
->select('DAY(matche.created) AS date_, count(matche) as count_')
->where("YEAR(matche.created) = " . $year_matche, "MONTH(matche.created) = " . $month_matche)
->groupBy('date_')
->getQuery()
->getResult();
$year_reservation = $new->format("Y");
$month_reservation = $new->format("m");
$_selected_reservation = 0;
if (isset($_GET["date_reservation"])) {
$year_reservation = $date_list[$_GET["date_reservation"]]->format("Y");
$month_reservation = $date_list[$_GET["date_reservation"]]->format("m");
$_selected_reservation = $_GET["date_reservation"];
}
$result_reservations = $em->getRepository("App\Admin\Entity\Reservation")
->createQueryBuilder('reservation')
->select('DAY(reservation.date) AS date_, sum(reservation.total) as total_')
->where("YEAR(reservation.date) = " . $year_matche, "MONTH(reservation.date) = " . $month_matche)
->groupBy('date_')
->getQuery()
->getResult();
for ($i = 1; $i <= cal_days_in_month(CAL_GREGORIAN, $month_user, $year_user); $i++) {
$line_exist = false;
foreach ($result_ as $key => $value) {
if ($value["date_"] == $i) {
$line_exist = true;
$result_users[$i] = $value;
}
}
if (!$line_exist == true) {
$new_ = array();
$new_["date_"] = $i;
$new_["count_"] = 0;
$result_users[$i] = $new_;
}
}
for ($i = 1; $i <= cal_days_in_month(CAL_GREGORIAN, $month_matche, $year_matche); $i++) {
$line_exist = false;
foreach ($result_matches as $key => $value) {
if ($value["date_"] == $i) {
$line_exist = true;
$result_matches_final[$i] = $value;
}
}
if (!$line_exist == true) {
$new_ = array();
$new_["date_"] = $i;
$new_["count_"] = 0;
$result_matches_final[$i] = $new_;
}
}
for ($i = 1; $i <= cal_days_in_month(CAL_GREGORIAN, $month_reservation, $year_reservation); $i++) {
$line_exist = false;
foreach ($result_reservations as $key => $value) {
if ($value["date_"] == $i) {
$line_exist = true;
$result_reservations_final[$i] = $value;
}
}
if (!$line_exist == true) {
$new_ = array();
$new_["date_"] = $i;
$new_["total_"] = 0;
$result_reservations_final[$i] = $new_;
}
}
$messages = $em->getRepository('App\Admin\Entity\Support')->count(array());
$users = $em->getRepository('App\Auth\Entity\User')->count(array());
$matches = $em->getRepository('App\Admin\Entity\Matche')->count(array());
$comments = $em->getRepository('App\Admin\Entity\Comment')->count(array());
$ratings = $em->getRepository('App\Admin\Entity\MatcheReview')->count(array());
$terrans = $em->getRepository('App\Admin\Entity\Terrain')->count(array());
$videos = $em->getRepository('App\Admin\Entity\Video')->count(array());
$invations = $em->getRepository('App\Admin\Entity\MatcheInvitation')->count(array());
$videos_views = $em->getRepository('App\Admin\Entity\Video')->countViews();
$videos_likes = $em->getRepository('App\Admin\Entity\Video')->countLikes();
$penidng = $em->getRepository('App\Admin\Entity\Reservation')->count([
"status" => "pending"
]);
$paid = $em->getRepository('App\Admin\Entity\Reservation')->count([
"status" => "paid"
]);
$checking = $em->getRepository('App\Admin\Entity\Reservation')->count([
"status" => "checking"
]);
$canceled = $em->getRepository('App\Admin\Entity\Reservation')->count([
"status" => "canceled"
]);
$total_in_app = $em->getRepository("App\Admin\Entity\Reservation")
->createQueryBuilder('reservation')
->select('SUM(reservation.total) as total ')
->where("reservation.status like 'paid' ")
->andWhere("reservation.type like 'creditcard' or reservation.type like 'virement' ")
->getQuery()
->getSingleScalarResult();
$total_out_app = $em->getRepository("App\Admin\Entity\Reservation")
->createQueryBuilder('reservation')
->select('SUM(reservation.total) as total ')
->where("reservation.status like 'paid' ")
->andWhere("reservation.type like 'manual' ")
->getQuery()
->getSingleScalarResult();
$total_cc = $em->getRepository("App\Admin\Entity\Reservation")
->createQueryBuilder('reservation')
->select('SUM(reservation.total) as total ')
->where("reservation.status like 'paid' ")
->andWhere("reservation.type like 'creditcard' ")
->getQuery()
->getSingleScalarResult();
$total_vir = $em->getRepository("App\Admin\Entity\Reservation")
->createQueryBuilder('reservation')
->select('SUM(reservation.total) as total ')
->where("reservation.status like 'paid' ")
->andWhere("reservation.type like 'virement' ")
->getQuery()
->getSingleScalarResult();
return $this->render("Admin/Home/index.html.twig", [
"users" => $users,
"matches" => $matches,
"terrans" => $terrans,
"videos" => $videos,
"invations" => $invations,
"videos_views" => $videos_views,
"videos_likes" => $videos_likes,
"comments" => $comments,
"ratings" => $ratings,
"messages" => $messages,
"result_users" => $result_users,
"result_matches_final" => $result_matches_final,
"result_reservations_final" => $result_reservations_final,
"date_list" => $date_list,
"_selected_user" => $_selected_user,
"_selected_matche" => $_selected_matche,
"_selected_reservation" => $_selected_reservation,
"ratings" => $ratings,
"penidng" => $penidng,
"paid" => $paid,
"checking" => $checking,
"canceled" => $canceled,
"total_cc" => $total_cc,
"total_vir" => $total_vir,
"total_in_app" => $total_in_app,
"total_out_app" => $total_out_app,
]);
}
public function checkPotm($em, $imagineCacheManager, $translator)
{
$datef = new \DateTime();
$datef->modify("first day of previous month");
$datel = new \DateTime();
$datel->modify("last day of previous month");
$repository = $em->getRepository('App\Admin\Entity\Potm');
$query = $repository->createQueryBuilder('m')
->where("m.date > :first")
->andWhere("m.date < :last")
->setParameter("first", $datef)
->setParameter("last", $datel)
->setMaxResults(1)
->getQuery();
$item = $query->getOneOrNullResult();
if ($item == null) {
$repository = $em->getRepository('App\Auth\Entity\User');
$query = $repository->createQueryBuilder('p')
->select("p")
->addSelect("(count(matche.id) * 0.5 + (SUM(matchePlayer.goals)*2) + SUM(matchePlayer.assists) ) as note ")
->where("matche.started > :first")
->andWhere("matche.started < :last")
->setParameter("first", $datef)
->setParameter("last", $datel)
->leftJoin('p.matchePlayers', 'matchePlayer')
->leftJoin('matchePlayer.matche', 'matche')
->groupBy("p")
->orderBy("note", "DESC")
->setMaxResults(1)
->getQuery();
$user = $query->getOneOrNullResult();
if ($user != null) {
$potm = new Potm();
$potm->setUser($user[0]);
$potm->setDate($datel);
$em->persist($potm);
$em->flush();
$user_obj = $potm->getUser();
$notification = new Notification();
$notification->setContent("notification_selected_as_potm");
$notification->setUser($potm->getUser());
$notification->setSender("");
$image = $imagineCacheManager->getBrowserPath("/admin/img/admin.png", 'thumb_500');
$notification->setImage($image);
$notification->setPositive(null);
$notification->setNegative(null);
$notification->setType("profile_follow");
$now = new \DateTime();
$notification->setExpired($now->modify("+10 years"));
$params = array("id" => $user_obj->getId());
$notification->setParameters(json_encode($params));
$em->persist($notification);
$em->flush();
$notificationTitle = $translator->trans('notification_selected_as_potm_title', locale: ($user_obj->getLocal() == null) ? "ar" : $user_obj->getLocal());
$notificationMsg = $translator->trans('notification_selected_as_potm', locale: ($user_obj->getLocal() == null) ? "ar" : $user_obj->getLocal());
$notificationMessage = [
"id" => random_int(1, 99999)."",
"title" => $notificationTitle,
"message" => $notificationMsg,
"icon" => $image,
"status" => "done",
"type" => "matche",
'click_action' => 'FLUTTER_NOTIFICATION_CLICK',
];
$settings = $em->getRepository('App\Admin\Entity\Settings')->findOneBy(array());
$this->send_notificationTokens($user_obj->getTokens(), $notificationMessage, $settings->getFirebasekey());
}
}
}
public function api_first(Request $request, $latitude, $longitude, $local, $token, CacheManager $imagineCacheManager, DateTimeFormatter $dateTimeFormatter, TranslatorInterface $translator): Response
{
if ($token != $this->getParameter('token_app')) {
throw new NotFoundHttpException("Page not found");
}
$em = $this->getDoctrine()->getManager();
if (date('d') < 2) {
$this->checkPotm($em, $imagineCacheManager, $translator);
}
$obj = [];
$query = $em->getRepository('App\Admin\Entity\Video')->createQueryBuilder('p')
->where('p.enabled = true')
->setMaxResults(15) // Example: Fetching 15 records
->addSelect('RAND() as HIDDEN rand') // MySQL specific: add random ordering
->orderBy('rand')
->getQuery();
$videos_list = $query->getResult();
$list = array();
foreach ($videos_list as $key => $video) {
$v["id"] = $video->getId();
$v["likes"] = $video->getLikes();
$v["views"] = $video->getViews();
$v["duration"] = $video->getDuration();
$now = new \DateTime();
$v["created"] = $dateTimeFormatter->formatDiff($video->getCreated(), $now, $local);
$v["comments"] = $video->getComments()->count();
$v["image"] = $imagineCacheManager->getBrowserPath($video->getMedia()->getPath(), 'video_thumb');
$v["video"] = $request->getSchemeAndHttpHost() . "/" . $request->getBasePath() . $video->getVideo()->getPath();
#$v["video"]="https://assets.mixkit.co/videos/preview/mixkit-young-woman-missing-a-bowling-shot-49115-large.mp4";
$u["id"] = $video->getUser()->getId();
$u["fname"] = $video->getUser()->getFname();
$u["lname"] = $video->getUser()->getLname();
$u["image"] = $imagineCacheManager->getBrowserPath($video->getUser()->getMedia()->getPath(), 'thumb_500');
$v["user"] = $u;
$list[] = $v;
}
$obj["videos"] = $list;
$repository = $em->getRepository('App\Admin\Entity\Matche');
$query_builder = $repository->createQueryBuilder('m');
if ($latitude != 0 && $longitude != 0) {
$query_builder->addSelect('org,m.id,m.title,m.places,m.description,m.fees,m.started,m.latitude,m.longitude,m.duration,m.terrainType,m.type,m.amount,m.playersnumber,m.minAge,m.maxAge,m.resultHome,m.resultAway,m.created, - ((ACOS(SIN(:lat * PI() / 180) * SIN(m.latitude * PI() / 180) + COS(:lat * PI() / 180) * COS(m.latitude * PI() / 180) * COS((:lng - m.longitude) * PI() / 180)) * 180 / PI()) * 60 * 1.1515 * 1.609344) as distance')
->leftJoin('m.organizer', 'org')
->leftJoin('org.media', 'orgm')
->where("m.enabled = true", "m.started > :now")
->setParameter('lat', $latitude)
->setParameter('lng', $longitude)
->setParameter('now', new \DateTime());
} else {
$query_builder->addSelect("org,m.id,m.title,m.places,m.description,m.fees,m.started,m.latitude,m.longitude,m.duration,m.terrainType,m.type,m.amount,m.playersnumber,m.minAge,m.maxAge,m.resultHome,m.resultAway,m.created")
->leftJoin('m.organizer', 'org')
->leftJoin('org.media', 'orgm')
->where("m.enabled = true", "m.started > :now")
->setParameter('now', new \DateTime());
}
if ($latitude != 0 && $longitude != 0) {
$query_builder->orderBy('distance', "desc");
}
$query_builder->addOrderBy('m.started', "asc");
$query_builder->addOrderBy('m.id', 'ASC');
$query_builder->setFirstResult(0);
$query_builder->setMaxResults(10);
$query = $query_builder->getQuery();
$matches_list = $query->getResult();
$list = array();
foreach ($matches_list as $matche) {
$ma = null;
$ma["id"] = $matche["id"];
$ma["title"] = $matche["title"];
$ma["description"] = $matche["description"];
$ma["fees"] = $matche["fees"];
$ma["latitude"] = $matche["latitude"];
$ma["longitude"] = $matche["longitude"];
$ma["duration"] = $matche["duration"];
$ma["terrain"] = $matche["terrainType"];
$ma["type"] = $matche["type"];
$ma["amount"] = $matche["amount"];
$ma["playersnumber"] = $matche["playersnumber"];
$ma["minAge"] = $matche["minAge"];
$ma["maxAge"] = $matche["maxAge"];
$ma["places"] = $matche["places"];
$ma["resultHome"] = $matche["resultHome"];
$ma["resultAway"] = $matche["resultAway"];
$ma["locked"] = $matche[0]->isLocked();
$ma["gone"] = $matche[0]->isGone();
$ma["expired"] = $matche[0]->isExpired();
$ma["end"] = $matche[0]->isEnd();
$ma["paid"] = $matche[0]->isHasPaid();
$ma["created"] = $matche["created"]->format("d/m/Y H:i");
$formatter = new IntlDateFormatter(($local == "ar") ? "ar_ma" : "fr_fr", IntlDateFormatter::LONG, IntlDateFormatter::LONG);
$formatter->setPattern('dd/MM/yyyy HH:mm');
$ma["started"] = $formatter->format($matche["started"]);
$ma["time"] = $dateTimeFormatter->formatDiff($matche["started"], new \DateTime(), $local);
$ma["date"] = $dateTimeFormatter->formatDiff($matche["started"], new \DateTime(), $local);
$ma["day"] = $matche["started"]->format("d");
$ma["month"] = $matche["started"]->format("F");
if (!array_key_exists("distance", $matche)) $matche["distance"] = null;
$ma["distance"] = ($matche["distance"] === null) ? null : round(abs($matche["distance"]), 2);
$positionsList = array();
foreach ($matche[0]->getPositions() as $key => $value) {
$p = null;
$p["id"] = $value->getId();
$p["position"] = $value->getPosition();
$p["quantity"] = $value->getQuantity();
$p["side"] = $value->getSide();
$positionsList[] = $p;
}
$ma["positions"] = $positionsList;
$playersList = array();
foreach ($matche[0]->getPlayers() as $key => $value) {
$p = null;
$p["id"] = $value->getId();
$p["position"] = $value->getPosition();
$p["side"] = $value->getSide();
$p["type"] = $value->getType();
$p["progress"] = $value->getProgress();
$p["present"] = $value->getPresent();
$p["goals"] = $value->getGoals();
$p["assists"] = $value->getAssists();
$p["user"]["id"] = $value->getUser()->getId();
$p["user"]["fname"] = $value->getUser()->getFname();
$p["user"]["lname"] = $value->getUser()->getLname();
$p["user"]["phone"] = $value->getUser()->getPhone();
$p["user"]["image"] = $imagineCacheManager->getBrowserPath($value->getUser()->getMedia()->getPath(), 'thumb_500');
$playersList[] = $p;
}
$ma["players"] = $playersList;
$organizer = null;
$organizer["id"] = $matche[0]->getOrganizer()->getId();
$organizer["fname"] = $matche[0]->getOrganizer()->getFname();
$organizer["lname"] = $matche[0]->getOrganizer()->getLname();
$organizer["phone"] = $matche[0]->getOrganizer()->getPhone();
$organizer["image"] = $imagineCacheManager->getBrowserPath($matche[0]->getOrganizer()->getMedia()->getPath(), 'thumb_500');
$ma["organizer"] = $organizer;
$motm = null;
if ($matche[0]->getMotm() != null) {
$motm["id"] = $matche[0]->getMotm()->getId();
$motm["fname"] = $matche[0]->getMotm()->getFname();
$motm["lname"] = $matche[0]->getMotm()->getLname();
$motm["phone"] = $matche[0]->getMotm()->getPhone();
$motm["image"] = $imagineCacheManager->getBrowserPath($matche[0]->getMotm()->getMedia()->getPath(), 'thumb_500');
}
$ma["motm"] = $motm;
if ($matche[0]->getConversation() != null) {
$ma["conversation"] = $matche[0]->getConversation()->getId();
}
$list[] = $ma;
}
$datef = new \DateTime();
$datef->modify("first day of previous month");
$datel = new \DateTime();
$datel->modify("last day of previous month");
$repository = $em->getRepository('App\Admin\Entity\Potm');
$query = $repository->createQueryBuilder('m')
->where("m.date > :first")
->andWhere("m.date < :last")
->setParameter("first", $datef)
->setParameter("last", $datel)
->setMaxResults(1)
->getQuery();
$potm = $query->getOneOrNullResult();
if ($potm != null) {
$p["id"] = $potm->getUser()->getId();
$p["fname"] = $potm->getUser()->getFname();
$p["lname"] = $potm->getUser()->getLname();
$p["image"] = $imagineCacheManager->getBrowserPath($potm->getUser()->getMedia()->getPath(), 'thumb_500');
$obj["potm"]["player"] = $p;
$formatter = new IntlDateFormatter(($local == "ar") ? "ar_ma" : "fr_fr", IntlDateFormatter::LONG, IntlDateFormatter::LONG);
$formatter->setPattern('MMMM');
$obj["potm"]["month"] = $formatter->format($datef);
}
$setting = $em->getRepository('App\Admin\Entity\Settings')->findOneBy(array());
$obj["matches"] = $list;
$obj["bankname"] = $setting->getBankname();
$obj["bankaccount"] = $setting->getBankaccount();
$obj["banknumber"] = $setting->getBanknumber();
header('Content-Type: application/json');
$encoders = array(new XmlEncoder(), new JsonEncoder());
$normalizers = array(new ObjectNormalizer());
$serializer = new Serializer($normalizers, $encoders);
$jsonContent = $serializer->serialize($obj, 'json');
return new Response($jsonContent);
}
public function api_stats(
EntityManagerInterface $em,
Request $request,
$token
): Response
{
$parameters = json_decode($request->getContent(), false);
$id = $parameters->id;
$token = $parameters->token;
$duration = $parameters->duration;
$account = $em->getRepository(Account::class)->findOneBy(array("id" => $id, "enabled" => true));
$users = 0;
$terrains = 0;
$pending = 0;
$paid = 0;
$checking = 0;
$canceled = 0;
if ($account) {
$apiToken = $em->getRepository(ApiToken::class)->findOneBy(["token" => $token, "account" => $account]);
$account_id = $account->getParentId() != null
? $account->getParentId()
: $account->getId();
if ($apiToken) {
// $users = $em->getRepository('App\Auth\Entity\User')->count(array());
$terrains = $em->getRepository('App\Admin\Entity\Terrain')->count(['account' => $account_id]);
$query_builder = $em->getRepository('App\Admin\Entity\Terrain')->createQueryBuilder('t');
$query_builder
->innerJoin('t.account', 'account')
->andWhere('account.id = :account_id OR account.id = :parent_id')
->setParameter('account_id', $account->getId())
->setParameter('parent_id', $account->getParentId());
$results = $query_builder->getQuery()->getResult();
$terrains = count($results);
$query_builder = $em->getRepository('App\Admin\Entity\Reservation')->createQueryBuilder('r')
->innerJoin('r.account', 'account')
->andWhere("r.status = 'paid'")
->andWhere('account.id = :account_id OR account.id = :parent_id')
->setParameter('account_id', $account->getId())
->setParameter('parent_id', $account->getParentId());
$results = $query_builder->getQuery()->getResult();
$paid = count($results);
$query_builder = $em->getRepository('App\Admin\Entity\Reservation')
->createQueryBuilder('r')
->innerJoin('r.account', 'account')
->andWhere("r.status = 'canceled'")
->andWhere('account.id = :account_id OR account.id = :parent_id')
->setParameter('account_id', $account->getId())
->setParameter('parent_id', $account->getParentId());
$results = $query_builder->getQuery()->getResult();
$canceled = count($results);
$total_in_app = $em->getRepository("App\Admin\Entity\Reservation")
->createQueryBuilder('reservation')
->select('SUM(reservation.total) as total ')
->innerJoin('reservation.account', 'account')
->where("reservation.status like 'paid' ")
->andWhere("reservation.type like 'creditcard' or reservation.type like 'virement' ")
->andWhere('account.id = :account_id OR account.id = :parent_id')
->setParameter('account_id', $account->getId())
->setParameter('parent_id', $account->getParentId())
->getQuery()
->getSingleScalarResult();
$total_out_app = $em->getRepository("App\Admin\Entity\Reservation")
->createQueryBuilder('reservation')
->select('SUM(reservation.total) as total ')
->innerJoin('reservation.account', 'account')
->where("reservation.status like 'paid' ")
->andWhere("reservation.type like 'manual' ")
->andWhere('account.id = :account_id OR account.id = :parent_id')
->setParameter('account_id', $account->getId())
->setParameter('parent_id', $account->getParentId())
->getQuery()
->getSingleScalarResult();
$graphFirstDate = new \DateTime("30 days ago");
$graphEndDate = new \DateTime("tomorrow");
if ($duration == "this.week") {
$graphFirstDate = new \DateTime("7 days ago");
} else if ($duration == "this.month") {
$graphFirstDate = new \DateTime("first day of this month");
$graphEndDate = new \DateTime("tomorrow");
} else if ($duration == "last.month") {
$graphFirstDate = new \DateTime("first day of last month");
$graphEndDate = new \DateTime("last day of last month");
}
$interval = \DateInterval::createFromDateString('+1 day');
$period = new \DatePeriod($graphFirstDate, $interval, $graphEndDate);
$new = new \DateTime;
$year_graph = $graphFirstDate->format("Y");
$month_graph = $graphFirstDate->format("m");
$result_ = $em->getRepository("App\Admin\Entity\Reservation")
->createQueryBuilder('reservation')
->select('DAY(reservation.date) AS date, sum(reservation.total) as earning')
->innerJoin('reservation.account', 'account')
->where("YEAR(reservation.date) = " . $year_graph, "MONTH(reservation.date) = " . $month_graph)
->andWhere("reservation.status like :paid ")
->andWhere('account.id = :account_id OR account.id = :parent_id')
->setParameter('account_id', $account->getId())
->setParameter('parent_id', $account->getParentId())
->setParameter("paid", "paid")
->groupBy('date')
->getQuery()
->getResult();
$graph = [];
foreach ($period as $dt) {
$line_exist = false;
foreach ($result_ as $key => $value) {
if ($value["date"] == $dt->format("d")) {
$line_exist = true;
$item = ["name" => $dt->format("d"), "value" => $value["earning"]];
$graph[] = $item;
}
}
if (!$line_exist == true) {
$item = ["name" => $dt->format("d"), "value" => 0];
$graph[] = $item;
}
}
}
}
return $this->json([
'terrains' => $terrains,
'paid' => $paid,
'canceled' => $canceled,
'total_in_app' => $total_in_app,
'total_out_app' => $total_out_app,
'graph' => $graph
], Response::HTTP_OK);
}
}