src/Admin/Controller/HomeController.php line 59

Open in your IDE?
  1. <?php
  2. namespace App\Admin\Controller;
  3. use App\Admin\Entity\Invoice;
  4. use App\Admin\Entity\Item;
  5. use App\Admin\Entity\ItemInvoice;
  6. use App\Admin\Entity\Notification;
  7. use App\Admin\Entity\Potm;
  8. use App\Admin\Entity\Account;
  9. use App\Admin\Entity\ApiToken;
  10. use App\Admin\Entity\Reservation;
  11. use App\Admin\Entity\Settings;
  12. use App\Service\FirebaseTokenManager;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Exception;
  15. use IntlDateFormatter;
  16. use Knp\Bundle\TimeBundle\DateTimeFormatter;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  19. use Symfony\Component\Form\Extension\Core\Type\DateType;
  20. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  21. use Symfony\Component\Form\Extension\Core\Type\TextType;
  22. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  23. use Symfony\Component\HttpFoundation\Request;
  24. use \Liip\ImagineBundle\Imagine\Cache\CacheManager;
  25. use Stripe\InvoiceItem;
  26. use Symfony\Component\Serializer\Encoder\XmlEncoder;
  27. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  28. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  29. use Symfony\Component\Serializer\Serializer;
  30. use Symfony\Component\Mailer\MailerInterface;
  31. use Symfony\Component\Mime\Email;
  32. use Symfony\Contracts\Cache\CacheInterface;
  33. use Symfony\Contracts\Translation\TranslatorInterface;
  34. use YouCan\Pay\YouCanPay;
  35. class HomeController  extends AbstractController
  36. {
  37.     private $cache;
  38.     public function __construct(CacheInterface $cache)
  39.     {
  40.         $this->cache $cache;
  41.     }
  42.     function send_notificationTokens($tokens$message$key)
  43.     {
  44.         
  45.         $firebaseTokenManager = new FirebaseTokenManager($this->cache$this->getParameter('kernel.project_dir'));
  46.         $res =  $firebaseTokenManager->send_notificationTokens($tokens$message$key);
  47.         return $res;
  48.         
  49.     }
  50.     public function index(MailerInterface $mailer,CacheManager $imagineCacheManagerTranslatorInterface $translatorEntityManagerInterface $entityManager): Response
  51.     {
  52.             
  53.         $em $entityManager;
  54.         
  55.         $image $imagineCacheManager->getBrowserPath("/admin/img/admin.png"'thumb_500');
  56.         $notifMessage =  "message" ;
  57.         $notificationMessage =  [
  58.             "id"=>random_int(199999)."",
  59.             "title"=>"rfzefez",
  60.             "message"=>$notifMessage,
  61.             "icon"=>$image,
  62.             "type"=>"messages",
  63.             "cid"=> 232,
  64.             "mid"=> 23,
  65.             "created"=> "Now",
  66.             "latitude" =>2.322,
  67.             "longitude" =>3.22,
  68.             "mtype" => 33,
  69.             "image"=> null,
  70.             "original"=> "ok",
  71.             "first"=>"kkk" ,
  72.             "uid"=>  "" ,
  73.             "ufname"=> "",
  74.             "ulname"=> "",
  75.             "uimage"=>"",
  76.             "uemail"=> "",
  77.             'click_action' => 'FLUTTER_NOTIFICATION_CLICK',
  78.         ];  
  79.         $settings $em->getRepository(Settings::class)->findOneBy(array());
  80.         //$dd = $this->send_notificationTokens($token,$notificationMessage,$settings->getFirebasekey());
  81.     
  82.         $this->checkPotm($entityManager$imagineCacheManager$translator);
  83.         $start = new \DateTime;
  84.         $start->setDate($start->format('Y'), $start->format('n'), 1); // Normalize the day to 1
  85.         $start->setTime(000); // Normalize time to midnight
  86.         $start->sub(new \DateInterval('P12M'));
  87.         $interval = new \DateInterval('P1M');
  88.         $recurrences 12;
  89.         $date_list  = array();
  90.         foreach (new \DatePeriod($start$interval$recurrencestrue) as $date) {
  91.             $date_list[] =  $date// attempting to make it more clear to read here
  92.         }
  93.         $date_list array_reverse($date_list);
  94.         $new = new \DateTime;
  95.         $year_user $new->format("Y");
  96.         $month_user $new->format("m");
  97.         $_selected_user 0;
  98.         if (isset($_GET["date_user"])) {
  99.             $year_user $date_list[$_GET["date_user"]]->format("Y");
  100.             $month_user $date_list[$_GET["date_user"]]->format("m");
  101.             $_selected_user $_GET["date_user"];
  102.         }
  103.         $result_ $em->getRepository("App\Auth\Entity\User")
  104.             ->createQueryBuilder('visiteur')
  105.             ->select('DAY(visiteur.created) AS date_, count(visiteur) as count_')
  106.             ->where("YEAR(visiteur.created) = " $year_user"MONTH(visiteur.created) = " $month_user)
  107.             ->groupBy('date_')
  108.             ->getQuery()
  109.             ->getResult();
  110.         $year_matche $new->format("Y");
  111.         $month_matche $new->format("m");
  112.         $_selected_matche 0;
  113.         if (isset($_GET["date_matche"])) {
  114.             $year_matche $date_list[$_GET["date_matche"]]->format("Y");
  115.             $month_matche $date_list[$_GET["date_matche"]]->format("m");
  116.             $_selected_matche $_GET["date_matche"];
  117.         }
  118.         $result_matches $em->getRepository("App\Admin\Entity\Matche")
  119.             ->createQueryBuilder('matche')
  120.             ->select('DAY(matche.created) AS date_, count(matche) as count_')
  121.             ->where("YEAR(matche.created) = " $year_matche"MONTH(matche.created) = " $month_matche)
  122.             ->groupBy('date_')
  123.             ->getQuery()
  124.             ->getResult();
  125.         $year_reservation $new->format("Y");
  126.         $month_reservation $new->format("m");
  127.         $_selected_reservation 0;
  128.         if (isset($_GET["date_reservation"])) {
  129.             $year_reservation $date_list[$_GET["date_reservation"]]->format("Y");
  130.             $month_reservation $date_list[$_GET["date_reservation"]]->format("m");
  131.             $_selected_reservation $_GET["date_reservation"];
  132.         }
  133.         $result_reservations $em->getRepository("App\Admin\Entity\Reservation")
  134.             ->createQueryBuilder('reservation')
  135.             ->select('DAY(reservation.date) AS date_, sum(reservation.total) as total_')
  136.             ->where("YEAR(reservation.date) = " $year_matche"MONTH(reservation.date) = " $month_matche)
  137.             ->groupBy('date_')
  138.             ->getQuery()
  139.             ->getResult();
  140.         for ($i 1$i <= cal_days_in_month(CAL_GREGORIAN$month_user$year_user); $i++) {
  141.             $line_exist false;
  142.             foreach ($result_ as $key => $value) {
  143.                 if ($value["date_"] == $i) {
  144.                     $line_exist true;
  145.                     $result_users[$i] = $value;
  146.                 }
  147.             }
  148.             if (!$line_exist == true) {
  149.                 $new_ = array();
  150.                 $new_["date_"] = $i;
  151.                 $new_["count_"] = 0;
  152.                 $result_users[$i] = $new_;
  153.             }
  154.         }
  155.         for ($i 1$i <= cal_days_in_month(CAL_GREGORIAN$month_matche$year_matche); $i++) {
  156.             $line_exist false;
  157.             foreach ($result_matches as $key => $value) {
  158.                 if ($value["date_"] == $i) {
  159.                     $line_exist true;
  160.                     $result_matches_final[$i] = $value;
  161.                 }
  162.             }
  163.             if (!$line_exist == true) {
  164.                 $new_ = array();
  165.                 $new_["date_"] = $i;
  166.                 $new_["count_"] = 0;
  167.                 $result_matches_final[$i] = $new_;
  168.             }
  169.         }
  170.         for ($i 1$i <= cal_days_in_month(CAL_GREGORIAN$month_reservation$year_reservation); $i++) {
  171.             $line_exist false;
  172.             foreach ($result_reservations as $key => $value) {
  173.                 if ($value["date_"] == $i) {
  174.                     $line_exist true;
  175.                     $result_reservations_final[$i] = $value;
  176.                 }
  177.             }
  178.             if (!$line_exist == true) {
  179.                 $new_ = array();
  180.                 $new_["date_"] = $i;
  181.                 $new_["total_"] = 0;
  182.                 $result_reservations_final[$i] = $new_;
  183.             }
  184.         }
  185.         $messages $em->getRepository('App\Admin\Entity\Support')->count(array());
  186.         $users $em->getRepository('App\Auth\Entity\User')->count(array());
  187.         $matches $em->getRepository('App\Admin\Entity\Matche')->count(array());
  188.         $comments $em->getRepository('App\Admin\Entity\Comment')->count(array());
  189.         $ratings $em->getRepository('App\Admin\Entity\MatcheReview')->count(array());
  190.         $terrans $em->getRepository('App\Admin\Entity\Terrain')->count(array());
  191.         $videos $em->getRepository('App\Admin\Entity\Video')->count(array());
  192.         $invations $em->getRepository('App\Admin\Entity\MatcheInvitation')->count(array());
  193.         $videos_views $em->getRepository('App\Admin\Entity\Video')->countViews();
  194.         $videos_likes $em->getRepository('App\Admin\Entity\Video')->countLikes();
  195.         $penidng $em->getRepository('App\Admin\Entity\Reservation')->count([
  196.             "status" => "pending"
  197.         ]);
  198.         $paid $em->getRepository('App\Admin\Entity\Reservation')->count([
  199.             "status" => "paid"
  200.         ]);
  201.         $checking $em->getRepository('App\Admin\Entity\Reservation')->count([
  202.             "status" => "checking"
  203.         ]);
  204.         $canceled $em->getRepository('App\Admin\Entity\Reservation')->count([
  205.             "status" => "canceled"
  206.         ]);
  207.         $total_in_app $em->getRepository("App\Admin\Entity\Reservation")
  208.             ->createQueryBuilder('reservation')
  209.             ->select('SUM(reservation.total) as total ')
  210.             ->where("reservation.status like 'paid' ")
  211.             ->andWhere("reservation.type like 'creditcard' or  reservation.type like 'virement' ")
  212.             ->getQuery()
  213.             ->getSingleScalarResult();
  214.         $total_out_app $em->getRepository("App\Admin\Entity\Reservation")
  215.             ->createQueryBuilder('reservation')
  216.             ->select('SUM(reservation.total) as total ')
  217.             ->where("reservation.status like 'paid' ")
  218.             ->andWhere("reservation.type like 'manual'  ")
  219.             ->getQuery()
  220.             ->getSingleScalarResult();
  221.         $total_cc $em->getRepository("App\Admin\Entity\Reservation")
  222.             ->createQueryBuilder('reservation')
  223.             ->select('SUM(reservation.total) as total ')
  224.             ->where("reservation.status like 'paid' ")
  225.             ->andWhere("reservation.type like 'creditcard'  ")
  226.             ->getQuery()
  227.             ->getSingleScalarResult();
  228.         $total_vir $em->getRepository("App\Admin\Entity\Reservation")
  229.             ->createQueryBuilder('reservation')
  230.             ->select('SUM(reservation.total) as total ')
  231.             ->where("reservation.status like 'paid' ")
  232.             ->andWhere("reservation.type like 'virement'  ")
  233.             ->getQuery()
  234.             ->getSingleScalarResult();
  235.         return $this->render("Admin/Home/index.html.twig", [
  236.             "users" => $users,
  237.             "matches" => $matches,
  238.             "terrans" => $terrans,
  239.             "videos" => $videos,
  240.             "invations" => $invations,
  241.             "videos_views" => $videos_views,
  242.             "videos_likes" => $videos_likes,
  243.             "comments" => $comments,
  244.             "ratings" => $ratings,
  245.             "messages" => $messages,
  246.             "result_users" => $result_users,
  247.             "result_matches_final" => $result_matches_final,
  248.             "result_reservations_final" => $result_reservations_final,
  249.             "date_list" => $date_list,
  250.             "_selected_user" => $_selected_user,
  251.             "_selected_matche" => $_selected_matche,
  252.             "_selected_reservation" => $_selected_reservation,
  253.             "ratings" => $ratings,
  254.             "penidng" => $penidng,
  255.             "paid" => $paid,
  256.             "checking" => $checking,
  257.             "canceled" => $canceled,
  258.             "total_cc" => $total_cc,
  259.             "total_vir" => $total_vir,
  260.             "total_in_app" => $total_in_app,
  261.             "total_out_app" => $total_out_app,
  262.         ]);
  263.     }
  264.     public function checkPotm($em$imagineCacheManager$translator)
  265.     {
  266.         $datef = new \DateTime();
  267.         $datef->modify("first day of previous month");
  268.         $datel = new \DateTime();
  269.         $datel->modify("last day of previous month");
  270.         $repository $em->getRepository('App\Admin\Entity\Potm');
  271.         $query $repository->createQueryBuilder('m')
  272.             ->where("m.date > :first")
  273.             ->andWhere("m.date < :last")
  274.             ->setParameter("first"$datef)
  275.             ->setParameter("last"$datel)
  276.             ->setMaxResults(1)
  277.             ->getQuery();
  278.         $item $query->getOneOrNullResult();
  279.         if ($item == null) {
  280.             $repository $em->getRepository('App\Auth\Entity\User');
  281.             $query $repository->createQueryBuilder('p')
  282.                 ->select("p")
  283.                 ->addSelect("(count(matche.id) * 0.5 + (SUM(matchePlayer.goals)*2)  + SUM(matchePlayer.assists) ) as note  ")
  284.                 ->where("matche.started > :first")
  285.                 ->andWhere("matche.started < :last")
  286.                 ->setParameter("first"$datef)
  287.                 ->setParameter("last"$datel)
  288.                 ->leftJoin('p.matchePlayers''matchePlayer')
  289.                 ->leftJoin('matchePlayer.matche''matche')
  290.                 ->groupBy("p")
  291.                 ->orderBy("note""DESC")
  292.                 ->setMaxResults(1)
  293.                 ->getQuery();
  294.             $user $query->getOneOrNullResult();
  295.             if ($user != null) {
  296.                 $potm = new Potm();
  297.                 $potm->setUser($user[0]);
  298.                 $potm->setDate($datel);
  299.                 $em->persist($potm);
  300.                 $em->flush();
  301.                 $user_obj $potm->getUser();
  302.                 $notification  = new Notification();
  303.                 $notification->setContent("notification_selected_as_potm");
  304.                 $notification->setUser($potm->getUser());
  305.                 $notification->setSender("");
  306.                 $image $imagineCacheManager->getBrowserPath("/admin/img/admin.png"'thumb_500');
  307.                 $notification->setImage($image);
  308.                 $notification->setPositive(null);
  309.                 $notification->setNegative(null);
  310.                 $notification->setType("profile_follow");
  311.                 $now = new \DateTime();
  312.                 $notification->setExpired($now->modify("+10 years"));
  313.                 $params =  array("id" => $user_obj->getId());
  314.                 $notification->setParameters(json_encode($params));
  315.                 $em->persist($notification);
  316.                 $em->flush();
  317.                 $notificationTitle $translator->trans('notification_selected_as_potm_title'locale: ($user_obj->getLocal() == null) ? "ar" $user_obj->getLocal());
  318.                 $notificationMsg $translator->trans('notification_selected_as_potm'locale: ($user_obj->getLocal() == null) ? "ar" $user_obj->getLocal());
  319.                 $notificationMessage =  [
  320.                     "id" => random_int(199999)."",
  321.                     "title" => $notificationTitle,
  322.                     "message" => $notificationMsg,
  323.                     "icon" => $image,
  324.                     "status" => "done",
  325.                     "type" => "matche",
  326.                     'click_action' => 'FLUTTER_NOTIFICATION_CLICK',
  327.                 ];
  328.                 $settings $em->getRepository('App\Admin\Entity\Settings')->findOneBy(array());
  329.                 $this->send_notificationTokens($user_obj->getTokens(), $notificationMessage$settings->getFirebasekey());
  330.             }
  331.         }
  332.     }
  333.     public function api_first(Request $request$latitude$longitude$local$tokenCacheManager $imagineCacheManagerDateTimeFormatter $dateTimeFormatterTranslatorInterface $translator): Response
  334.     {
  335.         if ($token != $this->getParameter('token_app')) {
  336.             throw new NotFoundHttpException("Page not found");
  337.         }
  338.         $em $this->getDoctrine()->getManager();
  339.         if (date('d') < 2) {
  340.             $this->checkPotm($em$imagineCacheManager$translator);
  341.         }
  342.         $obj = [];
  343.             $query $em->getRepository('App\Admin\Entity\Video')->createQueryBuilder('p')
  344.                         ->where('p.enabled = true')
  345.                         ->setMaxResults(15// Example: Fetching 15 records
  346.                         ->addSelect('RAND() as HIDDEN rand'// MySQL specific: add random ordering
  347.                         ->orderBy('rand')
  348.                         ->getQuery();
  349.             $videos_list $query->getResult();
  350.         $list = array();
  351.         foreach ($videos_list as $key => $video) {
  352.             $v["id"] = $video->getId();
  353.             $v["likes"] = $video->getLikes();
  354.             $v["views"] = $video->getViews();
  355.             $v["duration"] = $video->getDuration();
  356.             $now = new \DateTime();
  357.             $v["created"] = $dateTimeFormatter->formatDiff($video->getCreated(), $now$local);
  358.             $v["comments"] = $video->getComments()->count();
  359.             $v["image"] = $imagineCacheManager->getBrowserPath($video->getMedia()->getPath(), 'video_thumb');
  360.             $v["video"] = $request->getSchemeAndHttpHost() . "/" $request->getBasePath() . $video->getVideo()->getPath();
  361.             #$v["video"]="https://assets.mixkit.co/videos/preview/mixkit-young-woman-missing-a-bowling-shot-49115-large.mp4";
  362.             $u["id"] = $video->getUser()->getId();
  363.             $u["fname"] = $video->getUser()->getFname();
  364.             $u["lname"] = $video->getUser()->getLname();
  365.             $u["image"] = $imagineCacheManager->getBrowserPath($video->getUser()->getMedia()->getPath(), 'thumb_500');
  366.             $v["user"] = $u;
  367.             $list[] = $v;
  368.         }
  369.         $obj["videos"] = $list;
  370.         $repository $em->getRepository('App\Admin\Entity\Matche');
  371.         $query_builder $repository->createQueryBuilder('m');
  372.         if ($latitude != && $longitude != 0) {
  373.             $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')
  374.                 ->leftJoin('m.organizer''org')
  375.                 ->leftJoin('org.media''orgm')
  376.                 ->where("m.enabled = true""m.started > :now")
  377.                 ->setParameter('lat'$latitude)
  378.                 ->setParameter('lng'$longitude)
  379.                 ->setParameter('now', new \DateTime());
  380.         } else {
  381.             $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")
  382.                 ->leftJoin('m.organizer''org')
  383.                 ->leftJoin('org.media''orgm')
  384.                 ->where("m.enabled = true""m.started > :now")
  385.                 ->setParameter('now', new \DateTime());
  386.         }
  387.         if ($latitude != && $longitude != 0) {
  388.             $query_builder->orderBy('distance'"desc");
  389.         }
  390.         $query_builder->addOrderBy('m.started'"asc");
  391.         $query_builder->addOrderBy('m.id''ASC');
  392.         $query_builder->setFirstResult(0);
  393.         $query_builder->setMaxResults(10);
  394.         $query =  $query_builder->getQuery();
  395.         $matches_list $query->getResult();
  396.         $list = array();
  397.         foreach ($matches_list as  $matche) {
  398.             $ma null;
  399.             $ma["id"] = $matche["id"];
  400.             $ma["title"] = $matche["title"];
  401.             $ma["description"] = $matche["description"];
  402.             $ma["fees"] = $matche["fees"];
  403.             $ma["latitude"] = $matche["latitude"];
  404.             $ma["longitude"] = $matche["longitude"];
  405.             $ma["duration"] = $matche["duration"];
  406.             $ma["terrain"] = $matche["terrainType"];
  407.             $ma["type"] = $matche["type"];
  408.             $ma["amount"] = $matche["amount"];
  409.             $ma["playersnumber"] = $matche["playersnumber"];
  410.             $ma["minAge"] = $matche["minAge"];
  411.             $ma["maxAge"] = $matche["maxAge"];
  412.             $ma["places"] = $matche["places"];
  413.             $ma["resultHome"] = $matche["resultHome"];
  414.             $ma["resultAway"] = $matche["resultAway"];
  415.             $ma["locked"] = $matche[0]->isLocked();
  416.             $ma["gone"] = $matche[0]->isGone();
  417.             $ma["expired"] = $matche[0]->isExpired();
  418.             $ma["end"] = $matche[0]->isEnd();
  419.             $ma["paid"] = $matche[0]->isHasPaid();
  420.             $ma["created"] = $matche["created"]->format("d/m/Y H:i");
  421.             $formatter = new IntlDateFormatter(($local == "ar") ? "ar_ma" "fr_fr"IntlDateFormatter::LONGIntlDateFormatter::LONG);
  422.             $formatter->setPattern('dd/MM/yyyy  HH:mm');
  423.             $ma["started"] = $formatter->format($matche["started"]);
  424.             $ma["time"] = $dateTimeFormatter->formatDiff($matche["started"], new \DateTime(), $local);
  425.             $ma["date"] = $dateTimeFormatter->formatDiff($matche["started"], new \DateTime(), $local);
  426.             $ma["day"] = $matche["started"]->format("d");
  427.             $ma["month"] = $matche["started"]->format("F");
  428.             if (!array_key_exists("distance"$matche)) $matche["distance"] = null;
  429.             $ma["distance"] = ($matche["distance"] === null) ? null round(abs($matche["distance"]), 2);
  430.             $positionsList = array();
  431.             foreach ($matche[0]->getPositions() as $key => $value) {
  432.                 $p null;
  433.                 $p["id"] = $value->getId();
  434.                 $p["position"] = $value->getPosition();
  435.                 $p["quantity"] = $value->getQuantity();
  436.                 $p["side"] = $value->getSide();
  437.                 $positionsList[] = $p;
  438.             }
  439.             $ma["positions"] = $positionsList;
  440.             $playersList = array();
  441.             foreach ($matche[0]->getPlayers() as $key => $value) {
  442.                 $p null;
  443.                 $p["id"] = $value->getId();
  444.                 $p["position"] = $value->getPosition();
  445.                 $p["side"] = $value->getSide();
  446.                 $p["type"] = $value->getType();
  447.                 $p["progress"] = $value->getProgress();
  448.                 $p["present"] = $value->getPresent();
  449.                 $p["goals"] = $value->getGoals();
  450.                 $p["assists"] = $value->getAssists();
  451.                 $p["user"]["id"] = $value->getUser()->getId();
  452.                 $p["user"]["fname"] = $value->getUser()->getFname();
  453.                 $p["user"]["lname"] = $value->getUser()->getLname();
  454.                 $p["user"]["phone"] = $value->getUser()->getPhone();
  455.                 $p["user"]["image"] = $imagineCacheManager->getBrowserPath($value->getUser()->getMedia()->getPath(), 'thumb_500');
  456.                 $playersList[] = $p;
  457.             }
  458.             $ma["players"] = $playersList;
  459.             $organizer  null;
  460.             $organizer["id"] = $matche[0]->getOrganizer()->getId();
  461.             $organizer["fname"] = $matche[0]->getOrganizer()->getFname();
  462.             $organizer["lname"] = $matche[0]->getOrganizer()->getLname();
  463.             $organizer["phone"] = $matche[0]->getOrganizer()->getPhone();
  464.             $organizer["image"] = $imagineCacheManager->getBrowserPath($matche[0]->getOrganizer()->getMedia()->getPath(), 'thumb_500');
  465.             $ma["organizer"] =  $organizer;
  466.             $motm  null;
  467.             if ($matche[0]->getMotm() != null) {
  468.                 $motm["id"] = $matche[0]->getMotm()->getId();
  469.                 $motm["fname"] = $matche[0]->getMotm()->getFname();
  470.                 $motm["lname"] = $matche[0]->getMotm()->getLname();
  471.                 $motm["phone"] = $matche[0]->getMotm()->getPhone();
  472.                 $motm["image"] = $imagineCacheManager->getBrowserPath($matche[0]->getMotm()->getMedia()->getPath(), 'thumb_500');
  473.             }
  474.             $ma["motm"] =  $motm;
  475.             if ($matche[0]->getConversation() != null) {
  476.                 $ma["conversation"] =  $matche[0]->getConversation()->getId();
  477.             }
  478.             $list[] = $ma;
  479.         }
  480.         $datef = new \DateTime();
  481.         $datef->modify("first day of previous month");
  482.         $datel = new \DateTime();
  483.         $datel->modify("last day of previous month");
  484.         $repository $em->getRepository('App\Admin\Entity\Potm');
  485.         $query $repository->createQueryBuilder('m')
  486.             ->where("m.date > :first")
  487.             ->andWhere("m.date < :last")
  488.             ->setParameter("first"$datef)
  489.             ->setParameter("last"$datel)
  490.             ->setMaxResults(1)
  491.             ->getQuery();
  492.         $potm $query->getOneOrNullResult();
  493.         if ($potm != null) {
  494.             $p["id"] = $potm->getUser()->getId();
  495.             $p["fname"] = $potm->getUser()->getFname();
  496.             $p["lname"] = $potm->getUser()->getLname();
  497.             $p["image"] = $imagineCacheManager->getBrowserPath($potm->getUser()->getMedia()->getPath(), 'thumb_500');
  498.             $obj["potm"]["player"] = $p;
  499.             $formatter = new IntlDateFormatter(($local == "ar") ? "ar_ma" "fr_fr"IntlDateFormatter::LONGIntlDateFormatter::LONG);
  500.             $formatter->setPattern('MMMM');
  501.             $obj["potm"]["month"] = $formatter->format($datef);
  502.         }
  503.         $setting $em->getRepository('App\Admin\Entity\Settings')->findOneBy(array());
  504.         $obj["matches"] = $list;
  505.         $obj["bankname"] = $setting->getBankname();
  506.         $obj["bankaccount"] = $setting->getBankaccount();
  507.         $obj["banknumber"] =  $setting->getBanknumber();
  508.         
  509.         header('Content-Type: application/json');
  510.         $encoders = array(new XmlEncoder(), new JsonEncoder());
  511.         $normalizers = array(new ObjectNormalizer());
  512.         $serializer = new Serializer($normalizers$encoders);
  513.         $jsonContent $serializer->serialize($obj'json');
  514.         return new Response($jsonContent);
  515.     }
  516.     public function api_stats(
  517.         EntityManagerInterface $em
  518.         Request $request
  519.         $token
  520.     ): Response
  521.     {
  522.         $parameters json_decode($request->getContent(), false);
  523.         $id $parameters->id;
  524.         $token $parameters->token;
  525.         $duration $parameters->duration;
  526.         $account $em->getRepository(Account::class)->findOneBy(array("id" => $id"enabled" => true));
  527.         $users 0;
  528.         $terrains 0;
  529.         $pending 0;
  530.         $paid 0;
  531.         $checking 0;
  532.         $canceled 0;
  533.         if ($account) {
  534.             $apiToken $em->getRepository(ApiToken::class)->findOneBy(["token" => $token"account" => $account]);
  535.             $account_id $account->getParentId() != null
  536.                 $account->getParentId()
  537.                 : $account->getId();
  538.             if ($apiToken) {
  539.                 // $users = $em->getRepository('App\Auth\Entity\User')->count(array());
  540.                 $terrains $em->getRepository('App\Admin\Entity\Terrain')->count(['account' => $account_id]);
  541.                 $query_builder $em->getRepository('App\Admin\Entity\Terrain')->createQueryBuilder('t');
  542.                 $query_builder
  543.                     ->innerJoin('t.account''account')
  544.                     ->andWhere('account.id = :account_id OR account.id = :parent_id')
  545.                     ->setParameter('account_id'$account->getId())
  546.                     ->setParameter('parent_id'$account->getParentId());
  547.                 $results $query_builder->getQuery()->getResult();
  548.                 $terrains count($results);
  549.                 $query_builder $em->getRepository('App\Admin\Entity\Reservation')->createQueryBuilder('r')
  550.                     ->innerJoin('r.account''account')
  551.                     ->andWhere("r.status = 'paid'")
  552.                     ->andWhere('account.id = :account_id OR account.id = :parent_id')
  553.                     ->setParameter('account_id'$account->getId())   
  554.                     ->setParameter('parent_id'$account->getParentId());
  555.                 $results $query_builder->getQuery()->getResult();
  556.                 $paid count($results);
  557.                 $query_builder $em->getRepository('App\Admin\Entity\Reservation')
  558.                     ->createQueryBuilder('r')
  559.                     ->innerJoin('r.account''account')
  560.                     ->andWhere("r.status = 'canceled'")
  561.                     ->andWhere('account.id = :account_id OR account.id = :parent_id')
  562.                     ->setParameter('account_id'$account->getId())     
  563.                     ->setParameter('parent_id'$account->getParentId());
  564.                 $results $query_builder->getQuery()->getResult();
  565.                 $canceled count($results);
  566.                 $total_in_app $em->getRepository("App\Admin\Entity\Reservation")
  567.                     ->createQueryBuilder('reservation')
  568.                     ->select('SUM(reservation.total) as total ')
  569.                     ->innerJoin('reservation.account''account')
  570.                     ->where("reservation.status like 'paid' ")
  571.                     ->andWhere("reservation.type like 'creditcard' or  reservation.type like 'virement' ")
  572.                     ->andWhere('account.id = :account_id OR account.id = :parent_id')
  573.                     ->setParameter('account_id'$account->getId())     
  574.                     ->setParameter('parent_id'$account->getParentId())
  575.                     ->getQuery()
  576.                     ->getSingleScalarResult();
  577.     
  578.                 $total_out_app $em->getRepository("App\Admin\Entity\Reservation")
  579.                     ->createQueryBuilder('reservation')
  580.                     ->select('SUM(reservation.total) as total ')
  581.                     ->innerJoin('reservation.account''account')
  582.                     ->where("reservation.status like 'paid' ")
  583.                     ->andWhere("reservation.type like 'manual'  ")
  584.                     ->andWhere('account.id = :account_id OR account.id = :parent_id')
  585.                     ->setParameter('account_id'$account->getId())     
  586.                     ->setParameter('parent_id'$account->getParentId())
  587.                     ->getQuery()
  588.                     ->getSingleScalarResult();
  589.                 $graphFirstDate =  new \DateTime("30 days ago");
  590.                 $graphEndDate =  new \DateTime("tomorrow");
  591.                 if ($duration == "this.week") {
  592.                     $graphFirstDate =  new \DateTime("7 days ago");
  593.                 } else if ($duration == "this.month") {
  594.                     $graphFirstDate =  new \DateTime("first day of this month");
  595.                     $graphEndDate =  new \DateTime("tomorrow");
  596.                 } else if ($duration == "last.month") {
  597.                     $graphFirstDate =  new \DateTime("first day of last month");
  598.                     $graphEndDate =  new \DateTime("last day of last month");
  599.                 }
  600.                 $interval \DateInterval::createFromDateString('+1 day');
  601.                 $period = new \DatePeriod($graphFirstDate$interval$graphEndDate);
  602.                 $new = new \DateTime;
  603.                 $year_graph $graphFirstDate->format("Y");
  604.                 $month_graph $graphFirstDate->format("m");
  605.                 $result_ $em->getRepository("App\Admin\Entity\Reservation")
  606.                     ->createQueryBuilder('reservation')
  607.                     ->select('DAY(reservation.date) AS date, sum(reservation.total) as earning')
  608.                     ->innerJoin('reservation.account''account')
  609.                     ->where("YEAR(reservation.date) = " $year_graph"MONTH(reservation.date) = " $month_graph)
  610.                     ->andWhere("reservation.status like :paid  ")
  611.                     ->andWhere('account.id = :account_id OR account.id = :parent_id')
  612.                     ->setParameter('account_id'$account->getId())     
  613.                     ->setParameter('parent_id'$account->getParentId())
  614.                     ->setParameter("paid""paid")
  615.                     ->groupBy('date')
  616.                     ->getQuery()
  617.                     ->getResult();
  618.                 $graph = [];
  619.                 foreach ($period as $dt) {
  620.                     $line_exist false;
  621.                     foreach ($result_ as $key => $value) {
  622.                         if ($value["date"] == $dt->format("d")) {
  623.                             $line_exist true;
  624.                             $item = ["name" => $dt->format("d"), "value" => $value["earning"]];
  625.                             $graph[] = $item;
  626.                         }
  627.                     }
  628.                     if (!$line_exist == true) {
  629.                         $item = ["name" => $dt->format("d"), "value" => 0];
  630.                         $graph[] = $item;
  631.                     }
  632.                 }
  633.             }
  634.         }
  635.         return $this->json([
  636.             'terrains' => $terrains,
  637.             'paid' => $paid,
  638.             'canceled' => $canceled,
  639.             'total_in_app' => $total_in_app,
  640.             'total_out_app' => $total_out_app,
  641.             'graph' => $graph
  642.         ], Response::HTTP_OK);
  643.     }
  644. }