<?php
/**
* Created by PhpStorm.
* User: parcel
* Date: 1/3/19
* Time: 9:02 AM
*/
namespace App\Api;
use App\Controller\SendSms;
use App\Entity\CollectedParcel;
use App\Entity\Delivery;
use App\Entity\DeliveryParcel;
use App\Entity\DeliveryVehicle;
use App\Entity\Parcel;
use App\Entity\Sms;
use App\Entity\Station;
use App\Entity\UserStation;
use App\Entity\Vehicle;
use App\Entity\WayBill;
use App\Service\ActionMatrix;
use DateTime;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\ObjectManager;
use FOS\RestBundle\Controller\AbstractFOSRestController;
use JMS\Serializer\SerializationContext;
use JMS\Serializer\SerializerBuilder;
use Psr\Log\LoggerInterface;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use FOS\RestBundle\Controller\Annotations as Rest;
use Symfony\Component\Messenger\MessageBusInterface;
class DeliveryController extends AbstractFOSRestController {
private LoggerInterface $logger;
private ActionMatrix $actionMatrix;
private ObjectManager $em;
private MessageBusInterface $messageBus;
public function __construct(LoggerInterface $logger, ManagerRegistry $managerRegistry, MessageBusInterface $messageBus){
$this->logger = $logger;
$this->em = $managerRegistry->getManager();
$this->messageBus = $messageBus;
}
/**
* @Rest\Get("/stations", name="get_all_stations")
*/
public function getStations(Request $request): JsonResponse
{
$em = $this->em;
/** @var $stations Station[] */
$stations = $em->getRepository(Station::class)->findAllStations(1);
return new JsonResponse($stations, Response::HTTP_OK);
}
/**
* @Rest\Get("/deliveries/receive_init/{station_id}", name="initialize_receive_totals")
*/
public function receiveInitDelivery($station_id): JsonResponse
{
$em = $this->em;
$deliveries = $em->getRepository(Delivery::class)->findBy([
'destination' => $station_id,
'isReceivedFully' => false,
'isVerified' => true
]);
$data = [
'deliveries_total' => count($deliveries),
'deliveries' => $deliveries
];
return new JsonResponse($data, Response::HTTP_OK);
}
/**
* @Rest\Get("/deliveries/receive/{station_id}", name="deliveries_on_route")
*/
public function onRouteDeliveries($station_id): JsonResponse
{
$em = $this->em;
$deliveries = $em->getRepository(Delivery::class)->findBy([
'destination' => $station_id,
'isReceivedFully' => false,
'isVerified' => true,
'isCancelled' => false,
'isLoaded' => true
]);
return new JsonResponse($deliveries, Response::HTTP_OK);
}
/**
* @Rest\Patch("/deliveries/receive/{delivery_id}", name="receive_delivery")
*/
public function receiveDelivery($delivery_id): JsonResponse
{
$em = $this->em;
/** @var Delivery $delivery */
$delivery = $em->getRepository(Delivery::class)->findOneBy([
'id' => $delivery_id
]);
if($delivery->getisReceived()){
$data = [
'error' => [
'message' => "Delivery was received by: ".$delivery->getReceivedBy()->getPerson()->getFirstName().' '.$delivery->getReceivedBy()->getPerson()->getSecondName()
]
];
return new JsonResponse($data,Response::HTTP_CONFLICT);
}
try {
$delivery->setIsReceived(true);
$delivery->setReceivedAt(new \DateTime());
$delivery->setReceivedBy($this->getUser());
$em->flush();
$data = [
'id' => $delivery->getId(),
'parcel_count' => $delivery->getParcelCount(),
'destination' => $delivery->getDestination()->getId(),
'origin_station' => $delivery->getOriginStation()->getId(),
'is_cancelled' => $delivery->getisCancelled(),
'is_received' => $delivery->getisReceived()
];
return new JsonResponse($data, Response::HTTP_OK);
}catch (Exception $e){
return new JsonResponse($delivery, Response::HTTP_EXPECTATION_FAILED);
}
}
/**
* @Rest\Patch("/deliveries/receive/waybill/{waybill}/{station}", name="receive_waybill")
* @param $waybill
* @throws \Exception
*/
public function receiveSingleWaybill($waybill, $station): JsonResponse
{
$em = $this->em;
/** @var WayBill $waybill */
$waybill = $em->getRepository(WayBill::class)->findOneBy([
'id' => $waybill,
'toStation' => $station
]);
if(!$waybill){
$data = [
'message' => "WAYBILL NOT FOUND"
];
return new JsonResponse($data, Response::HTTP_NOT_FOUND);
}
// if($waybill->getIsReceived()){
if(0==1){
$data = [
'message' => "WAYBILL ALREADY RECEIVED"
];
return new JsonResponse($data, Response::HTTP_ALREADY_REPORTED);
}
$waybill->setReceivedBy($this->getUser());
$waybill->setIsReceived(true);
$waybill->setReceivedAt(new \DateTime());
$this->actionMatrix = new ActionMatrix($this->messageBus,$this->em,$waybill);
$this->actionMatrix->receivingAction($this->getUser());
$data = [
"message" => "PARCEL RECEIVED"
];
return new JsonResponse($data, Response::HTTP_OK);
}
/**
* @Rest\Patch("/deliveries/receive/parcel/{parcel_id}", name="receive_delivery_parcel")
*/
public function receiveParcel($parcel_id): JsonResponse
{
$em = $this->em;
/** @var DeliveryParcel $deliveryParcel */
$deliveryParcel = $em->getRepository(DeliveryParcel::class)->findOneBy([
'id' => $parcel_id
]);
if($deliveryParcel->getisReceived()){
$data = [
'id' => $deliveryParcel->getId(),
'parcel' => $deliveryParcel->getParcel()->getId(),
'delivery' => $deliveryParcel->getDelivery()->getId(),
'is_cancelled' => $deliveryParcel->getisCancelled(),
'cancelled_because' => $deliveryParcel->getCancelledBecause()
];
return new JsonResponse($data, Response::HTTP_CONFLICT);
}
/** @var UserStation $userStation */
$userStation = $em->getRepository(UserStation::class)->findOneBy([
'user' => $this->getUser()
],['id' =>'DESC']);
try {
$deliveryParcel->setIsReceived(true);
$deliveryParcel->setReceivedAt(new \DateTime());
$deliveryParcel->setReceivedBy($this->getUser());
$deliveryParcel->getParcel()->getWaybill()->setIsReceived(true);
$deliveryParcel->getParcel()->getWaybill()->setReceivedBy($this->getUser());
$delivery = $em->getRepository(Delivery::class)->findOneBy([
'id' => $deliveryParcel->getDelivery()
]);
if(($delivery->getReceivedParcels() + 1) === $delivery->getParcelCount()){
$delivery->setIsReceivedFully(true);
}
$delivery->setReceivedParcels($delivery->getReceivedParcels() + 1);
if($deliveryParcel->getParcel()->getWaybill()->getToStation() === $userStation->getStation()) {
$this->actionMatrix = new ActionMatrix($this->messageBus,$this->em,$deliveryParcel->getParcel()->getWaybill());
$this->actionMatrix->receivingAction($this->getUser());
}
$data = [
'id' => $deliveryParcel->getId(),
'parcel' => $deliveryParcel->getParcel()->getId(),
'delivery' => $deliveryParcel->getDelivery()->getId(),
'is_cancelled' => $deliveryParcel->getisCancelled(),
'cancelled_because' => $deliveryParcel->getCancelledBecause()
];
return new JsonResponse($data, Response::HTTP_OK);
}catch (Exception $e){
$data = [
'id' => $deliveryParcel->getId(),
'parcel' => $deliveryParcel->getParcel()->getId(),
'delivery' => $deliveryParcel->getDelivery()->getId(),
'is_cancelled' => $deliveryParcel->getisCancelled(),
'cancelled_because' => $deliveryParcel->getCancelledBecause()
];
return new JsonResponse($data, Response::HTTP_EXPECTATION_FAILED);
}
}
/**
* @Rest\Get("/deliveries/collect/{waybill_id}", name="getOneWaybill_for_Collection")
*/
public function getWayBillForCollection($waybill_id): JsonResponse
{
$em = $this->em;
/** @var UserStation $userStation */
$userStation = $em->getRepository(UserStation::class)->findOneBy([
'user' => $this->getUser()
],['id' => 'DESC']);
$parcel = $em->getRepository(DeliveryParcel::class)->unCollectedParcel(
$waybill_id
);
if(!$parcel) {
$data = [
'error' => [
'message' => "Parcel Not found or already Collected"
]
];
return new JsonResponse($data, Response::HTTP_CONFLICT);
}
$waybill = $em->getRepository(WayBill::class)->findOneBy([
'id' => $waybill_id
]);
return new JsonResponse($waybill, Response::HTTP_OK);
}
/**
* @Rest\Get("/deliveries/incoming/waybill/{station_id}/{waybill}", name="search_delivery_by_waybill")
* @param $station_id
* @param $waybill
* @param Request $request
* @return JsonResponse|Response
*/
public function searchDeliveryByWaybill($station_id, $waybill, Request $request): JsonResponse|Response
{
$em = $this->em;
$context = new SerializationContext();
$context->setSerializeNull(true);
$serializer = SerializerBuilder::create()->build();
$resultWaybill = $em->getRepository(DeliveryParcel::class)->findWaybill($station_id, $waybill);
if(!$resultWaybill){
return new JsonResponse(null, Response::HTTP_NOT_FOUND);
}
$delivery_id = $resultWaybill['delivery_id'];
$delivery = $em->getRepository(Delivery::class)->findIncomingDelivery($station_id, $delivery_id);
if($delivery){
$incomingParcels = $em->getRepository(DeliveryParcel::class)->commonDeliveryIncomingParcels($delivery_id);
/** @var DeliveryVehicle $deliveryVehicle */
$deliveryVehicle = $em->getRepository(DeliveryVehicle::class)->findOneBy([
'delivery' => $delivery_id
],['id' => 'DESC']);
if($deliveryVehicle){
$delivery['delivery_vehicle'] = $deliveryVehicle->getVehicle()->getRegNumber();
}
if($incomingParcels){
$delivery['parcels'] = $incomingParcels;
}
}else{
return new JsonResponse(null, Response::HTTP_NOT_FOUND);
}
$data = $serializer->serialize($delivery, 'json', $context);
return new Response($data, Response::HTTP_OK);
}
/**
* @Rest\Post("/deliveries/collect/{waybill_id}", name="update_waybill_as_collected")
*/
public function collectWaybill($waybill_id, Request $request) {
$em = $this->em;
$collectorName = $request->request->get('collector_name');
$collectorID = $request->request->get('collector_id');
$waybill = $em->getRepository(WayBill::class)->findOneBy([
'id' => $waybill_id
]);
if($waybill) {
$waybill->setIsCollected(true);
$collectedParcel = new CollectedParcel();
$collectedParcel->setCreatedBy($this->getUser());
$collectedParcel->setCreatedAt(new \DateTime());
$collectedParcel->setWaybill($waybill);
$collectedParcel->setCollectorName($collectorName);
$collectedParcel->setNationalId($collectorID);
$waybill->setCollectedParcel($collectedParcel);
try {
$em->persist($collectedParcel);
$em->flush();
$data = [
'id' => $waybill->getId(),
'sender_name' => $waybill->getSenderName(),
'receiver_name' => $waybill->getReceiverName(),
'sender_phone_number' => $waybill->getSenderPhoneNumber(),
'receiver_phone_number' => $waybill->getReceiverPhoneNumber(),
'from_station' => $waybill->getFromStation()->getId(),
'to_station' => $waybill->getToStation()->getId(),
'transaction' => $waybill->getTransaction()->getId(),
'is_collected' => $waybill->getIsCollected()
];
return new JsonResponse($data, Response::HTTP_CREATED);
}catch (Exception $e){
return new JsonResponse($waybill, Response::HTTP_EXPECTATION_FAILED);
}
}
}
/**
* @Rest\Get("/deliveries/parcels/{way_bill}/{id}/", name="getOneParcel_for_delivery")
*/
public function getParcel($way_bill, $id): JsonResponse
{
$em = $this->em;
/** @var UserStation $userStation */
$userStation = $em->getRepository(UserStation::class)->findOneBy([
'user' => $this->getUser()
],['id' => 'DESC']);
$deliveryParcel = $em->getRepository(DeliveryParcel::class)->findOneBy([
'parcel' => $id,
'isCancelled' => false
],['id' => 'DESC']);
$waybill = $em->getRepository(WayBill::class)->findOneBy([
'id' => $way_bill,
'fromStation' => $userStation->getStation()
]);
if(!$waybill) {
$data = [
'parcel' => null,
'parcels' => null,
'waybill' => null
];
return new JsonResponse($data, Response::HTTP_NOT_FOUND);
}
$parcel = $em->getRepository(Parcel::class)->findOneBy([
'number' => $id,
'waybill' => $waybill
]);
$parcels = $em->getRepository(Parcel::class)->findBy([
'waybill' => $waybill
]);
$data = [
'parcel' => $parcel,
'parcels' => $parcels,
'waybill' => $waybill
];
return new JsonResponse($data, Response::HTTP_OK);
}
/**
* @Rest\Patch("/deliveries/verify/{delivery}", name="verify_delivery_to_vehicle")
*/
public function verifyDelivery($delivery) {
$em = $this->em;
$deliveryVehicle = $em->getRepository(DeliveryVehicle::class)->findOneBy([
'delivery' => $delivery,
'isCancelled' => false
]);
$deliveryParcels = $em->getRepository(DeliveryParcel::class)->findBy([
'delivery' => $delivery,
'isCancelled' => false
]);
if($deliveryVehicle){
/** @var Delivery $delivery */
$delivery = $em->getRepository(Delivery::class)->findOneBy([
'id' => $deliveryVehicle->getDelivery()
]);
if($deliveryVehicle) {
$deliveryVehicle->setIsVerified(true);
$deliveryVehicle->setVerifiedBy($this->getUser());
$deliveryVehicle->setVerifiedAt(new DateTime());
$delivery->setIsVerified(true);
$delivery->setParcelCount(count($deliveryParcels));
try {
$em->flush();
$data = [
'id' => $delivery->getId(),
'parcel_count' => $delivery->getParcelCount(),
'destination' => $delivery->getDestination()->getId(),
'origin_station' => $delivery->getOriginStation()->getId(),
'is_cancelled' => $delivery->getisCancelled(),
];
$this->sendLoadingSms($delivery, $this->logger);
return new JsonResponse($data, Response::HTTP_OK);
}catch (Exception $e){
return new JsonResponse($delivery, Response::HTTP_EXPECTATION_FAILED);
}
}
}else{
$data = [
'error' => [
'message' => "This parcel is not loaded"
]
];
return new JsonResponse($data, Response::HTTP_CONFLICT);
}
}
/**
* @Rest\Post("/deliveries/load/{delivery_id}/{vehicle_id}", name="load_delivery_to_vehicle")
*/
public function loadDelivery($delivery_id, $vehicle_id): JsonResponse
{
$em = $this->em;
$delivery = $em->getRepository(Delivery::class)->findOneBy([
'id' => $delivery_id
]);
if($vehicle_id == 1796){
return new JsonResponse(['message' => 'No vehicle'], Response::HTTP_EXPECTATION_FAILED);
}
$vehicle = $em->getRepository(Vehicle::class)->findOneBy([
'id' => $vehicle_id
]);
/** @var DeliveryVehicle $deliveryVehicle */
$deliveryVehicle = $em->getRepository(DeliveryVehicle::class)->findOneBy([
'delivery' => $delivery,
'isCancelled' => false
]);
if($deliveryVehicle){
$deliveryVehicle->setIsCancelled(true);
$deliveryVehicle->setCancelledBy($this->getUser());
$deliveryVehicle->setCancelledAt(new DateTime());
}
$newDeliveryVehicle = new DeliveryVehicle();
$newDeliveryVehicle->setVehicle($vehicle);
$newDeliveryVehicle->setDelivery($delivery);
$newDeliveryVehicle->setCreatedAt(new DateTime());
$newDeliveryVehicle->setIsCancelled(false);
$newDeliveryVehicle->setIsVerified(false);
$newDeliveryVehicle->setCreatedBy($this->getUser());
$data = [];
try {
$em->persist($newDeliveryVehicle);
$delivery->setIsLoaded(true);
$delivery->setLoadedAt(new DateTime());
$delivery->setLoadedBy($this->getUser());
$em->flush();
/** @var DeliveryParcel[] $parcels */
$parcels = $delivery->getParcels();
foreach ($parcels as $index => $parcel) {
$waybill = $parcel->getParcel()->getWaybill();
$this->actionMatrix = new ActionMatrix($this->messageBus,$this->em,$waybill);
$this->actionMatrix->loadingAction($this->getUser());
}
$em->flush();
$data = [
'id' => $newDeliveryVehicle->getId(),
'delivery' => $newDeliveryVehicle->getDelivery()->getId(),
'vehicle' => $newDeliveryVehicle->getVehicle()->getId(),
'is_verified' => $newDeliveryVehicle->getisVerified(),
'is_cancelled' => $newDeliveryVehicle->getisCancelled()
];
return new JsonResponse($data, Response::HTTP_CREATED);
}catch (Exception $e){
return new JsonResponse($data, Response::HTTP_EXPECTATION_FAILED);
}
}
/**
* @Rest\Delete("/deliveries/delete/{delivery_parcel_id}", name="delete_parcel_from_delivery")
*/
public function deleteParcelFromDelivery($delivery_parcel_id) {
$em = $this->getDoctrine()->getManager();
/** @var DeliveryParcel $deliveryParcel */
$deliveryParcel = $em->getRepository(DeliveryParcel::class)->findOneBy([
'id' => $delivery_parcel_id
]);
if($deliveryParcel) {
$delivery = $em->getRepository(Delivery::class)->findOneBy([
'id' => $deliveryParcel->getDelivery()
]);
$delivery->setParcelCount($delivery->getParcelCount() - 1);
$em->remove($deliveryParcel);
// $deliveryParcel->getDelivery()->setParcelCount($deliveryParcel->getDelivery()->getParcelCount() - 1);
try {
$em->flush();
return new JsonResponse(null, Response::HTTP_NO_CONTENT);
}catch (Exception $e){
return new JsonResponse($deliveryParcel, Response::HTTP_EXPECTATION_FAILED);
}
}
}
/**
* @Rest\Get("/deliveries/parcels/{id}", name="get_delivery_parcels")
* @param Delivery $delivery
*/
public function deliveryParcels(Delivery $delivery){
$em = $this->em;
$deliveryParcels = $em->getRepository(DeliveryParcel::class)->deliveryParcels($delivery);
if(!$deliveryParcels)
return new JsonResponse(null, Response::HTTP_NO_CONTENT);
return new JsonResponse($deliveryParcels, Response::HTTP_OK);
}
/**
* @Rest\Delete("/deliveries/parcels/{id}", name="delete_delivery_parcel")
* @param $id
*/
public function deleteDeliveryParcel($id){
$em = $this->em;
$deliveryParcel = $em->getRepository(DeliveryParcel::class)->findOneBy([
'id' => $id
]);
if($deliveryParcel){
$em->remove($deliveryParcel);
$em->flush();
return new JsonResponse(null, Response::HTTP_NO_CONTENT);
}
return new JsonResponse(null, Response::HTTP_NO_CONTENT);
}
/**
* @Rest\Post("/deliveries/parcels", name="add_parcel_delivery")
* @param Request $request
*/
public function addParcelDelivery(Request $request){
$em = $this->em;
$arrayParcels = json_decode($request->getContent(), true);
$delivery = $em->getRepository(Delivery::class)->findOneBy([
'id' => $arrayParcels['delivery']
]);
$parcel = $em->getRepository(Parcel::class)->findOneBy([
'id' => $arrayParcels['parcel']
]);
$parcels = $em->getRepository(DeliveryParcel::class)->findBy([
'delivery' => $delivery
]);
$delivery->setParcelCount(count($parcels));
if($delivery && $parcel){
$deliveryParcel = $em->getRepository(DeliveryParcel::class)->findOneBy([
'parcel' => $parcel,
'isCancelled' => false
]);
$newDParcel = new DeliveryParcel();
$newDParcel->setParcel($parcel);
$newDParcel->setDelivery($delivery);
$newDParcel->setCreatedAt(new \DateTime());
$newDParcel->setCreatedBy($this->getUser());
$newDParcel->setIsCancelled(false);
$newDParcel->setIsReceived(false);
if($deliveryParcel){
$newDParcel->setCancelledAt(new \DateTime());
$newDParcel->setCancelledBy($this->getUser());
$newDParcel->setIsCancelled(true);
$newDParcel->setCancelledBecause("Another parcel is added to delivery:".$deliveryParcel->getDelivery()->getId());
}
try {
// $count = $delivery->getParcelCount()? 0: $delivery->getParcelCount();
$count = $delivery->getParcelCount();
$delivery->setParcelCount(($count+1));
$em->persist($newDParcel);
$em->flush();
$data = [
'id' => $newDParcel->getId(),
'parcel' => $newDParcel->getParcel()->getId(),
'delivery' => $newDParcel->getDelivery()->getId(),
'is_cancelled' => $newDParcel->getisCancelled(),
'cancelled_because' => $newDParcel->getCancelledBecause()
];
return new JsonResponse($data, Response::HTTP_CREATED);
}catch (Exception $e) {
$data = [
'error' => [
'message' => "Error occurred: could not add parcel to this delivery ".$e->getMessage()
]
];
//
return new JsonResponse($data, Response::HTTP_EXPECTATION_FAILED);
}
}
}
/**
* @Rest\Get("/deliveries", name="all_un_verified_deliveries")
*/
public function getDeliveries(): JsonResponse
{
$em = $this->em;
$deliveries = $em->getRepository(Delivery::class)->findBy([
'isReceived' => false,
'isLoaded' => false,
'isCancelled' => false
]);
return new JsonResponse($deliveries, Response::HTTP_OK);
}
/**
* @Rest\Get("/deliveries/{station_id}", name="all_un_verified_station_deliveries")
* @param $station_id
*/
public function getStationDeliveries($station_id) {
$em = $this->em;
$deliveries = $em->getRepository(Delivery::class)->findDeliveries($station_id);
$newDeliveries = array();
foreach ($deliveries as $index => $delivery) {
$deliveryParcels = $em->getRepository(DeliveryParcel::class)->getDeliveryParcels($delivery['id']);
$deliveryVehicle = $em->getRepository(DeliveryVehicle::class)->findDeliveryVehicle($delivery['id']);
$deliveries[$index]['parcels'] = $deliveryParcels;
$deliveries[$index]['vehicle'] = $deliveryVehicle;
}
// $deliveryVehicles = $em->getRepository(DeliveryVehicle::class)->findDeliveryVehicles($station_id);
$data = [
"deliveries" => $deliveries
];
return new JsonResponse($data, Response::HTTP_OK);
}
/**
* @Rest\Post("/deliveries/{station_id}", name="create_delivery")
*/
public function createDelivery($station_id, Request $request) {
$em = $this->em;
$station = $em->getRepository(Station::class)->findOneBy([
'id' => $station_id
]);
$userStation = $em->getRepository(UserStation::class)->findOneBy([
'user' => $this->getUser()
], ['id' => 'DESC']);
if($station === $userStation->getStation()) {
$data = [
'error' => [
'message' => "Cannot create Delivery for ".$userStation->getStation()->getStationName()
]
];
return new JsonResponse($data, Response::HTTP_OK);
}
$delivery = new Delivery();
$delivery->setCreatedAt(new \DateTime());
$delivery->setIsCancelled(false);
$delivery->setCreatedBy($this->getUser());
$delivery->setIsReceived(false);
$delivery->setIsLoaded(false);
$delivery->setDestination($station);
$delivery->setParcelCount(0);
$delivery->setIsVerified(false);
$delivery->setIsReceivedFully(false);
$delivery->setIsReceived(false);
$delivery->setReceivedParcels(0);
$userStation = $em->getRepository(UserStation::class)->findOneBy([
'user' => $this->getUser()
], ['id' => 'DESC']);
$delivery->setOriginStation($userStation->getStation());
try {
$em->persist($delivery);
$em->flush();
return new JsonResponse($delivery->getId(), Response::HTTP_CREATED);
}catch (Exception $e){
return new JsonResponse($delivery, Response::HTTP_EXPECTATION_FAILED);
}
}
/**
* @Rest\Get("/deliveries/{id}/{delivery_id}", name="get_delivery")
*/
public function getDelivery(Station $station, $delivery_id) {
$em = $this->em;
$delivery = $em->getRepository(Delivery::class)->findOneBy([
'id' => $delivery_id
]);
return new JsonResponse($delivery, Response::HTTP_OK);
}
/**
* @Rest\Get("/deliveries/incoming/{station_id}/{delivery_id}", name="get_one_incoming_delivery")
* @param $station_id
* @param $delivery_id
* @return JsonResponse
*/
public function getIncomingDelivery($station_id, $delivery_id): JsonResponse
{
$em = $this->em;
$delivery = $em->getRepository(Delivery::class)->findIncomingDelivery($station_id, $delivery_id);
if($delivery){
$incomingParcels = $em->getRepository(DeliveryParcel::class)->commonDeliveryIncomingParcels($delivery_id);
/** @var DeliveryVehicle $deliveryVehicle */
$deliveryVehicle = $em->getRepository(DeliveryVehicle::class)->findOneBy([
'delivery' => $delivery_id
],['id' => 'DESC']);
if($deliveryVehicle){
$delivery['delivery_vehicle'] = $deliveryVehicle->getVehicle()->getRegNumber();
}
if($incomingParcels){
$parcels = [
'parcels' => $incomingParcels
];
$delivery['parcels'] = $incomingParcels;
}
}else{
return new JsonResponse(null, Response::HTTP_NOT_FOUND);
}
return new JsonResponse($delivery, Response::HTTP_OK);
}
private function sendLoadingSms(Delivery $delivery): void
{
$em = $this->em;
$deliveryParcels = $em->getRepository(DeliveryParcel::class)->findBy([
'isCancelled' => false,
'delivery'=>$delivery
]);
foreach ($deliveryParcels as $index => $deliveryParcel) {
$waybill = $deliveryParcel->getParcel()->getWaybill();
$this->actionMatrix = new ActionMatrix($this->messageBus,$this->em,$waybill);
$this->actionMatrix->loadingAction($this->getUser());
}
}
/**
* @Rest\Get("/delivery/sms_test", name="test_sms")
*/
public function sendTestSMs(): JsonResponse
{
$em = $this->em;
/** @var WayBill $waybill */
$waybill = $em->getRepository(WayBill::class)->findOneBy([
'id' => 10001,
]);
$data = $this->sendSms($waybill, $em, $this->logger);
return new JsonResponse($data, Response::HTTP_OK);
}
/**
* @Rest\Get("/delivery/sms_failed", name="failed_smses")
*/
public function getFailedSms(){
$em = $this->em;
$sql = "SELECT * FROM sms where date(created_at) >= cast('2023-10-05' as date) and amount = 0";
$stmt = $em->getConnection()->prepare($sql);
// $stmt->bindValue('cap',$cap);
// $stmt->bindValue('year',$year);
// $stmt->bindValue('month',$month);
$stmt->execute();
$smses = $stmt->executeQuery()->fetchAllAssociative();
// ];
foreach ($smses as $index => $sms) {
/** @var Sms $realSms */
$realSms = $em->getRepository(Sms::class)->findOneBy([
'id' => $sms['id']
]);
$smsi = new SendSms();
// $smsi->sendOutSms($sms['recepient'],$sms['sms']);
$data = $this->sendSms($realSms->getTransaction()->getWayBill(), $em, $this->logger);
}
$view = $this->view($smses, Response::HTTP_OK);
//
return $view;
// return new JsonResponse($smses, Response::HTTP_OK);
}
}