<?php
namespace App\Api;
use App\Entity\DailyAccount;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\ObjectManager;
use FOS\RestBundle\Controller\Annotations as Rest;
use JMS\Serializer\SerializationContext;
use JMS\Serializer\SerializerBuilder;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class DailyAccountController extends AbstractController {
private ObjectManager $em;
private LoggerInterface $logger;
public function __construct(ManagerRegistry $managerRegistry, LoggerInterface $logger){
$this->em = $managerRegistry->getManager();
$this->logger = $logger;
}
#[Rest\Route('/daily_account',name: 'app_api_dailyaccount')]
public function getDailyAccount(Request $request): JsonResponse
{
$serializer = SerializerBuilder::create()->build();
$context = new SerializationContext();
$context->setSerializeNull(true);
$dailyAccount = $this->em->getRepository(DailyAccount::class)->myDailyAccount($this->getUser()->getId());
if($request->getMethod() == 'POST'){
$dAccount = $this->em->getRepository(DailyAccount::class)->findOneBy([
'id' => $dailyAccount['id']
]);
if($dAccount){
$this->logger->debug("DAILY ACCOUNT", $dailyAccount);
$dAccount->setClosedBy($this->getUser());
$dAccount->setIsClosed(true);
$dAccount->setDrawerCash($request->get('amount'));
$this->em->flush();
}
return new JsonResponse("", Response::HTTP_OK);
}
if($dailyAccount){
// $data = $serializer->serialize($dailyAccount, 'json', $context);
return new JsonResponse($dailyAccount, Response::HTTP_OK);
}
// mpesa_amount
// cash_amount
// opening time
// expenses
$data = [
"message" => "No active daily account"
];
return new JsonResponse($data, Response::HTTP_NOT_FOUND);
}
}