277 lines
9.3 KiB
PHP
277 lines
9.3 KiB
PHP
<?php
|
|
|
|
namespace App\Controller\ResqAPI;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Doctrine\ORM\Query;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use Catalyst\APIBundle\Controller\APIController;
|
|
use Catalyst\APIBundle\Response\APIResponse;
|
|
|
|
use App\Entity\Rider;
|
|
use App\Entity\JobOrder;
|
|
use App\Entity\RiderRating;
|
|
|
|
use App\Service\RiderTracker;
|
|
use App\Service\MobileAPIHandler;
|
|
|
|
use App\Ramcar\JOStatus;
|
|
use App\Ramcar\APIRiderStatus;
|
|
|
|
use Catalyst\APIBundle\Access\Generator as ACLGenerator;
|
|
|
|
class RiderController extends APIController
|
|
{
|
|
protected $acl_gen;
|
|
|
|
public function __construct(ACLGenerator $acl_gen)
|
|
{
|
|
$this->acl_gen = $acl_gen;
|
|
}
|
|
|
|
public function getRiderStatus(Request $req, RiderTracker $rt, EntityManagerInterface $em,
|
|
MobileAPIHandler $mah)
|
|
{
|
|
$this->denyAccessUnlessGranted('mobile_rider.status.get', null, 'No access.');
|
|
|
|
$required_params = [];
|
|
$msg = $this->checkRequiredParameters($req, $required_params);
|
|
if ($msg)
|
|
return new APIResponse(false, $msg);
|
|
|
|
// get capi user to link to mobile user
|
|
$user_id = $this->getUser()->getID();
|
|
|
|
// get mobile user
|
|
$mobile_user = $mah->findMobileUser($user_id);
|
|
|
|
if ($mobile_user == null)
|
|
return new APIResponse(false, 'No mobile user found.');
|
|
|
|
// get customer
|
|
$cust = $mobile_user->getCustomer();
|
|
if ($cust == null)
|
|
return new APIResponse(false, 'No customer information found');
|
|
|
|
$ongoing_jos = $mah->getOngoingJobOrders($cust);
|
|
|
|
$data = [];
|
|
if (count($ongoing_jos) <= 0)
|
|
{
|
|
try
|
|
{
|
|
// check if the latest fulfilled jo they have needs rider rating
|
|
$query = $em->createQuery('select jo from App\Entity\JobOrder jo where jo.customer = :cust and jo.status = :status order by jo.date_fulfill desc');
|
|
$fulfill_jo = $query->setParameters([
|
|
'cust' => $cust,
|
|
'status' => JOStatus::FULFILLED,
|
|
])
|
|
->setMaxResults(1)
|
|
->getSingleResult();
|
|
}
|
|
catch (Exception $e)
|
|
{
|
|
// no pending
|
|
$data[] = [
|
|
'status' => APIRiderStatus::NO_PENDING_JO,
|
|
];
|
|
return new APIResponse(true, 'No pending job order', $data);
|
|
}
|
|
|
|
// we got a recently fulfilled job order
|
|
if ($fulfill_jo)
|
|
{
|
|
// check if the rider has been rated
|
|
if (!$fulfill_jo->hasRiderRating())
|
|
{
|
|
$dest = $fulfill_jo->getCoordinates();
|
|
|
|
$data[] = [
|
|
'jo_id' => $fulfill_jo->getID(),
|
|
'service_type' => $fulfill_jo->getServiceType(),
|
|
'destination' => [
|
|
'long' => $dest->getLongitude(),
|
|
'lat' => $dest->getLatitude(),
|
|
],
|
|
'delivery_address' => $fulfill_jo->getDeliveryAddress(),
|
|
'delivery_instructions' => $fulfill_jo->getDeliveryInstructions(),
|
|
];
|
|
|
|
$rider = $fulfill_jo->getRider();
|
|
|
|
// default image url
|
|
$url_prefix = $req->getSchemeAndHttpHost();
|
|
$image_url = $url_prefix . '/assets/images/user.gif';
|
|
if ($rider->getImageFile() != null)
|
|
$image_url = $url_prefix . '/uploads/' . $rider->getImageFile();
|
|
|
|
$data[] = [
|
|
'status' => APIRiderStatus::RIDER_RATING
|
|
];
|
|
// default rider location to hub
|
|
$data['rider'] = [
|
|
'id' => $rider->getID(),
|
|
'name' => $rider->getFullName(),
|
|
'plate_num' => $rider->getPlateNumber(),
|
|
'contact_num' => $rider->getContactNumber(),
|
|
'image_url' => $image_url,
|
|
];
|
|
return new APIResponse(true, 'Rider status found', $data);
|
|
}
|
|
}
|
|
|
|
// no pending
|
|
$data[] = [
|
|
'status' => APIRiderStatus::NO_PENDING_JO,
|
|
];
|
|
return new APIResponse(true, 'No pending job order', $data);
|
|
}
|
|
|
|
// get first jo that's pending
|
|
$jo = $ongoing_jos[0];
|
|
$dest = $jo->getCoordinates();
|
|
|
|
$data = [
|
|
'jo_id' => $jo->getID(),
|
|
'service_type' => $jo->getServiceType(),
|
|
'destination' => [
|
|
'long' => $dest->getLongitude(),
|
|
'lat' => $dest->getLatitude(),
|
|
],
|
|
'delivery_address' => $jo->getDeliveryAddress(),
|
|
'delivery_instructions' => $jo->getDeliveryInstructions(),
|
|
];
|
|
|
|
switch ($jo->getStatus())
|
|
{
|
|
case JOStatus::PENDING:
|
|
$data['status'] = APIRiderStatus::OUTLET_ASSIGN;
|
|
$res->setData($data);
|
|
return $res->getReturnResponse();
|
|
case JOStatus::RIDER_ASSIGN:
|
|
$data['status'] = APIRiderStatus::RIDER_ASSIGN;
|
|
$res->setData($data);
|
|
return $res->getReturnResponse();
|
|
case JOStatus::ASSIGNED:
|
|
case JOStatus::IN_TRANSIT:
|
|
case JOStatus::IN_PROGRESS:
|
|
$rider = $jo->getRider();
|
|
// get rider coordinates from redis
|
|
$coord = $rt->getRiderLocation($rider->getID());
|
|
|
|
// default image url
|
|
$url_prefix = $req->getSchemeAndHttpHost();
|
|
$image_url = $url_prefix . '/assets/images/user.gif';
|
|
if ($rider->getImageFile() != null)
|
|
$image_url = $url_prefix . '/uploads/' . $rider->getImageFile();
|
|
|
|
$data['status'] = APIRiderStatus::RIDER_PICK_UP;
|
|
// TODO: fix this to actual location of rider
|
|
// default rider location to hub
|
|
$data['rider'] = [
|
|
'id' => $rider->getID(),
|
|
'name' => $rider->getFullName(),
|
|
'plate_num' => $rider->getPlateNumber(),
|
|
'contact_num' => $rider->getContactNumber(),
|
|
'image_url' => $image_url,
|
|
'location' => [
|
|
'long' => $coord->getLongitude(),
|
|
'lat' => $coord->getLatitude()
|
|
]
|
|
];
|
|
|
|
return new APIResponse(true, 'Rider status found', $data);
|
|
}
|
|
|
|
return new APIResponse(true, 'Rider status found', $data);
|
|
}
|
|
|
|
public function addRiderRating(Request $req, EntityManagerInterface $em,
|
|
MobileAPIHandler $mah)
|
|
{
|
|
$this->denyAccessUnlessGranted('mobile_rider.rating.add', null, 'No access.');
|
|
|
|
$required_params = [
|
|
'jo_id',
|
|
'rating',
|
|
];
|
|
|
|
$msg = $this->checkRequiredParameters($req, $required_params);
|
|
if ($msg)
|
|
return new APIResponse(false, $msg);
|
|
|
|
// get capi user to link to mobile user
|
|
$user_id = $this->getUser()->getID();
|
|
|
|
// get mobile user
|
|
$mobile_user = $mah->findMobileUser($user_id);
|
|
|
|
if ($mobile_user == null)
|
|
return new APIResponse(false, 'No mobile user found.');
|
|
|
|
// get customer
|
|
$cust = $mobile_user->getCustomer();
|
|
if ($cust == null)
|
|
return new APIResponse(false, 'No customer information found');
|
|
|
|
// get job order
|
|
$jo_id = $req->request->get('jo_id');
|
|
$jo = $em->getRepository(JobOrder::class)->find($jo_id);
|
|
if ($jo == null)
|
|
return new APIResponse(false, 'No job order found');
|
|
|
|
// get rider
|
|
$rider = $jo->getRider();
|
|
if ($rider == null)
|
|
return new APIResponse(false, 'No rider found');
|
|
|
|
// check that the customer owns the job order
|
|
$jo_cust = $jo->getCustomer();
|
|
if ($jo_cust->getID() != $cust->getID())
|
|
return new APIResponse(false, 'Job order was not initiated by customer;');
|
|
|
|
// TODO: check job order status, if it's complete
|
|
|
|
// add rider rating
|
|
$rating_num = $req->request->get('rating', -1);
|
|
|
|
// if rating is -1
|
|
if ($rating_num == -1)
|
|
{
|
|
$jo->setHasRiderRating();
|
|
$em->flush();
|
|
|
|
return new APIResponse(false, 'No rider rating', $data);
|
|
}
|
|
|
|
|
|
$rating = new RiderRating();
|
|
$rating->setRider($rider)
|
|
->setCustomer($cust)
|
|
->setJobOrder($jo)
|
|
->setRating($rating_num);
|
|
|
|
// rider rating comment
|
|
$comment = $req->request->get('comment');
|
|
if (!empty($comment))
|
|
$rating->setComment($comment);
|
|
|
|
// mark jo as rider rated already
|
|
$jo->setHasRiderRating();
|
|
|
|
$em->persist($rating);
|
|
$em->flush();
|
|
|
|
$data = [
|
|
'rating' => $rating->getRating(),
|
|
'comment' => $rating->getComment(),
|
|
];
|
|
|
|
// TODO: set average rating in rider entity
|
|
|
|
return new APIResponse(true, 'Rider rating added', $data);
|
|
}
|
|
}
|