<?php
namespace App\Controller\Api;
use App\Entity\Customer;
use App\Entity\OtherPayments;
use App\Services\Api\OtherPaymentsManager;
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 OtherPaymentsController extends ApiController
{
/** @var OtherPaymentsManager $otherPaymentsManager */
protected $otherPaymentsManager;
/**
* @param OtherPaymentsManager $otherPaymentsManager
*/
public function __construct(EntityManagerInterface $em, ParameterBagInterface $parameterBag, OtherPaymentsManager $otherPaymentsManager)
{
parent::__construct($em, $parameterBag);
$this->otherPaymentsManager = $otherPaymentsManager;
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/other_payments/{otherPayment}/update", name="api_other_payments_add", methods={"POST"})
*/
public function updatePayment(OtherPayments $otherPayment, Request $request)
{
$request = $this->transformJsonBody($request);
$data = $this->getDataFromRequest($request);
$response = $this->otherPaymentsManager->updatePayment($otherPayment, $data);
return $this->response($data);
$this->setStatusCode($response["code"]);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/other_payments/{date}/list", name="api_other_payments_list", methods={"POST"})
*/
public function listPayments($date, Request $request)
{
/** @var Customer $customer */
$customer = $this->getUser();
$data = $this->otherPaymentsManager->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'),
];
}
}