src/Controller/UserDashboardController.php line 31

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Model\FiltersModel;
  5. use App\Manager\UserManager;
  6. use App\Manager\PeriodManager;
  7. use App\Manager\HierarchyManager;
  8. use App\Repository\UserRepository;
  9. use App\Repository\OrderRepository;
  10. use App\Repository\PeriodRepository;
  11. use App\Repository\CreditRepository;
  12. use App\Repository\ManualPaymentRepository;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use App\Repository\CommissionRepository;
  15. use App\Model\MultiplePaymentPeriodsModel;
  16. use App\Manager\IncentiveStatementManager;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\RequestStack;
  19. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\Routing\Annotation\Route;
  22. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  23. use Symfony\Contracts\Cache\CacheInterface;
  24. #[Route('/dashboard'name'dashboard.')]
  25. class UserDashboardController extends AbstractController
  26. {
  27.     #[Route('/home'name'index')]
  28.     public function index(UserManager $userManagerCommissionRepository $commissionRepositoryPeriodManager $periodManager): Response
  29.     {
  30.         $user $this->getUser();
  31.         if (!$user) {
  32.             throw new AccessDeniedHttpException();
  33.         }
  34.         $manager $userManager->getManager($user);
  35.         $plan $userManager->getPlan($user);
  36.         $title $user->getTitle();
  37.         $team $userManager->getUserTeam($user);
  38.         $messages $userManager->getUserMessages($user);
  39.         $filters = new FiltersModel();
  40.         $ytdPeriod $periodManager->getYtdPeriod();
  41.         $currentPeriod $periodManager->getCurrentPeriod();
  42.         $filters->user $this->getUser();
  43.         $filters->startDate $ytdPeriod->getStartDate();
  44.         $filters->endDate $currentPeriod->getEndDate();
  45.         $filters->groupByMonth true;
  46.         $commissions $commissionRepository->findByPeriod($filters'%b-%Y');
  47.         // Prepare arrays for chart
  48.         $chartLabels = [];
  49.         $chartAmounts = [];
  50.         $chartTotalAmount 0;
  51.         foreach($commissions as $commission){
  52.             $chartLabels[] = $commission['monthYear'];
  53.             $chartAmounts[] = $commission['totalAmount'];
  54.             $chartTotalAmount += $commission['totalAmount'];
  55.         }
  56.         return $this->render('user_dashboard/home/index.html.twig', [
  57.             'manager' => $manager,
  58.             'plan' => $plan,
  59.             'title' => $title,
  60.             'team' => $team,
  61.             'messages' => $messages,
  62.             'chartLabels' => json_encode($chartLabels),
  63.             'chartAmounts' => json_encode($chartAmounts),
  64.             'chartTotalAmount' => $chartTotalAmount,
  65.         ]);
  66.     }
  67.     #[Route('/my_incentives/{currentPeriodName?}'name'my_incentives')]
  68.     public function myIncentives(IncentiveStatementManager $incentiveStatementManager$currentPeriodName null): Response
  69.     {
  70.         set_time_limit(0);
  71.         $incentiveStatementManager->setCurrentPeriodName($currentPeriodName);
  72.         $incentiveStatementManager->loggedUser $this->getUser();
  73.         $incentiveStatementManager->user $this->getUser();
  74.         $incentiveStatementManager->init();
  75.         return $this->render('user_dashboard/my_incentives/index.html.twig', [
  76.             'currentPeriodName' => $incentiveStatementManager->currentPeriodName,
  77.             'hierarchy' => $incentiveStatementManager->hierarchy,
  78.             'tree' => $incentiveStatementManager->finalTree,
  79.             'incentiveStatementManager' => $incentiveStatementManager,
  80.         ]);
  81.     }
  82.     #[Route('/my_incentives/print/{currentPeriodName?}'name'my_incentives_print')]
  83.     public function myIncentivesPrint(
  84.         IncentiveStatementManager $incentiveStatementManager,
  85.         UserRepository $userRepository,
  86.         PeriodRepository $periodRepository,
  87.         CommissionRepository $commissionRepository,
  88.         CreditRepository $creditRepository,
  89.         ManualPaymentRepository $manualPaymentRepository,
  90.         HierarchyManager $hierarchyManager,
  91.         RequestStack $requestStack,
  92.         CacheInterface $cache,
  93.         Request $request,
  94.         $currentPeriodName null
  95.     ): Response
  96.     {
  97.         set_time_limit(0);
  98.         $loggedUser $this->getUser();
  99.         // Get the filtered user ID from request (if viewing specific user's hierarchy)
  100.         $filteredUserId $request->query->get('user');
  101.         // Determine which user to use as the base for hierarchy
  102.         $baseUser $loggedUser;
  103.         if ($filteredUserId) {
  104.             $requestedUser $userRepository->find($filteredUserId);
  105.             if ($requestedUser) {
  106.                 // Verify the logged user has access to this user's hierarchy
  107.                 $tempManager = new IncentiveStatementManager(
  108.                     $periodRepository,
  109.                     $commissionRepository,
  110.                     $creditRepository,
  111.                     $manualPaymentRepository,
  112.                     $userRepository,
  113.                     $hierarchyManager,
  114.                     $requestStack,
  115.                     $cache
  116.                 );
  117.                 $tempManager->setCurrentPeriodName($currentPeriodName);
  118.                 $tempManager->loggedUser $loggedUser;
  119.                 $tempManager->user $loggedUser;
  120.                 $tempManager->setPeriods();
  121.                 $tempManager->getHierarchy();
  122.                 // Check if the requested user is in the logged user's accessible hierarchy
  123.                 if ($tempManager->userInHierarchy($requestedUser->getId())) {
  124.                     $baseUser $requestedUser;
  125.                 }
  126.             }
  127.         }
  128.         // Initialize the base manager to get hierarchy starting from the base user
  129.         $incentiveStatementManager->setCurrentPeriodName($currentPeriodName);
  130.         // For building hierarchy: use baseUser as loggedUser so hierarchy starts from them
  131.         $incentiveStatementManager->loggedUser $baseUser;
  132.         $incentiveStatementManager->user $baseUser;
  133.         $incentiveStatementManager->init();
  134.         // Collect all users in the hierarchy that the logged user has access to
  135.         $printableUsers = [];
  136.         // Add users from hierarchy (the hierarchy already includes access control)
  137.         foreach ($incentiveStatementManager->hierarchy as $hierarchyNode) {
  138.             $userId $hierarchyNode['user_id'];
  139.             $user $userRepository->find($userId);
  140.             if ($user) {
  141.                 // Create a new IncentiveStatementManager for each user
  142.                 $userManager = new IncentiveStatementManager(
  143.                     $periodRepository,
  144.                     $commissionRepository,
  145.                     $creditRepository,
  146.                     $manualPaymentRepository,
  147.                     $userRepository,
  148.                     $hierarchyManager,
  149.                     $requestStack,
  150.                     $cache
  151.                 );
  152.                 // Set the current period name first
  153.                 $userManager->setCurrentPeriodName($currentPeriodName);
  154.                 // Set the logged user and the user we're generating the statement for
  155.                 $userManager->loggedUser $loggedUser;
  156.                 $userManager->user $user;
  157.                 // Initialize periods first - this is critical
  158.                 $userManager->setPeriods();
  159.                 // Check if periods were initialized successfully
  160.                 if ($userManager->currentPeriod === null) {
  161.                     continue; // Skip this user if period initialization failed
  162.                 }
  163.                 // Now initialize data structures and get incentive data (with caching)
  164.                 $userManager->initializeData();
  165.                 $userManager->getIncentiveData();
  166.                 $userManager->getGuaranteeRollover();
  167.                 $printableUsers[] = [
  168.                     'user' => $user,
  169.                     'manager' => $userManager
  170.                 ];
  171.             }
  172.         }
  173.         return $this->render('user_dashboard/my_incentives/print.html.twig', [
  174.             'currentPeriodName' => $incentiveStatementManager->currentPeriodName,
  175.             'printableUsers' => $printableUsers,
  176.         ]);
  177.     }
  178. }