src/Api/DailyAccountController.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\Api;
  3. use App\Entity\DailyAccount;
  4. use Doctrine\Persistence\ManagerRegistry;
  5. use Doctrine\Persistence\ObjectManager;
  6. use FOS\RestBundle\Controller\Annotations as Rest;
  7. use JMS\Serializer\SerializationContext;
  8. use JMS\Serializer\SerializerBuilder;
  9. use Psr\Log\LoggerInterface;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. class DailyAccountController extends AbstractController {
  15. private ObjectManager $em;
  16. private LoggerInterface $logger;
  17. public function __construct(ManagerRegistry $managerRegistry, LoggerInterface $logger){
  18. $this->em = $managerRegistry->getManager();
  19. $this->logger = $logger;
  20. }
  21. #[Rest\Route('/daily_account',name: 'app_api_dailyaccount')]
  22. public function getDailyAccount(Request $request): JsonResponse
  23. {
  24. $serializer = SerializerBuilder::create()->build();
  25. $context = new SerializationContext();
  26. $context->setSerializeNull(true);
  27. $dailyAccount = $this->em->getRepository(DailyAccount::class)->myDailyAccount($this->getUser()->getId());
  28. if($request->getMethod() == 'POST'){
  29. $dAccount = $this->em->getRepository(DailyAccount::class)->findOneBy([
  30. 'id' => $dailyAccount['id']
  31. ]);
  32. if($dAccount){
  33. $this->logger->debug("DAILY ACCOUNT", $dailyAccount);
  34. $dAccount->setClosedBy($this->getUser());
  35. $dAccount->setIsClosed(true);
  36. $dAccount->setDrawerCash($request->get('amount'));
  37. $this->em->flush();
  38. }
  39. return new JsonResponse("", Response::HTTP_OK);
  40. }
  41. if($dailyAccount){
  42. // $data = $serializer->serialize($dailyAccount, 'json', $context);
  43. return new JsonResponse($dailyAccount, Response::HTTP_OK);
  44. }
  45. // mpesa_amount
  46. // cash_amount
  47. // opening time
  48. // expenses
  49. $data = [
  50. "message" => "No active daily account"
  51. ];
  52. return new JsonResponse($data, Response::HTTP_NOT_FOUND);
  53. }
  54. }