<?php
namespace App\Controller\Api;
use App\Entity\Customer;
use App\Entity\KitPayments;
use App\Services\Api\KitPaymentsManager;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class KitPaymentsController extends ApiController
{
/** @var KitPaymentsManager $kitPaymentsManager */
protected $kitPaymentsManager;
/**
* @param KitPaymentsManager $kitPaymentsManager
*/
public function __construct(EntityManagerInterface $em, ParameterBagInterface $parameterBag, KitPaymentsManager $kitPaymentsManager)
{
parent::__construct($em, $parameterBag);
$this->kitPaymentsManager = $kitPaymentsManager;
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/kit_payments/{kitPayment}/update", name="api_kit_payments_add", methods={"POST"})
*/
public function updatePayment(KitPayments $kitPayment, Request $request)
{
$request = $this->transformJsonBody($request);
$data = $this->getDataFromRequest($request);
$response = $this->kitPaymentsManager->updatePayment($kitPayment, $data);
return $this->response($data);
$this->setStatusCode($response["code"]);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/kit_payments/{date}/list", name="api_kit_payments_list", methods={"POST"})
*/
public function listPayments($date, Request $request)
{
/** @var Customer $customer */
$customer = $this->getUser();
$data = $this->kitPaymentsManager->listByMonthlyPayment($customer, new DateTime($date));
return $this->response($data);
}
protected function getDataFromRequest(Request $request)
{
return [
'amount'=> $request->get('amount') !== 'null' ? $request->get('amount') : '0.0' ,
'datePaid' => $request->get('datePaid'),
'note' => $request->get('note'),
'paid' => $request->get('paid'),
];
}
}