diff --git a/config/routes/cmb_rider_api.yaml b/config/routes/cmb_rider_api.yaml index e044872a..eb4f2186 100644 --- a/config/routes/cmb_rider_api.yaml +++ b/config/routes/cmb_rider_api.yaml @@ -119,3 +119,13 @@ cmb_rapi_jo_odometer: path: /cmbrapi/odometer controller: App\Controller\CMBRAPIController::setOdometer methods: [POST] + +cmb_rapi_jo_arrive_photos_upload: + path: /cmbrapi/uploadarrivephotos + controller: App\Controller\CMBRAPIController::uploadArrivePhotos + methods: [POST] + +cmb_rapi_jo_finish_photos_upload: + path: /cmbrapi/uploadfinishphotos + controller: App\Controller\CMBRAPIController::uploadFinishPhotos + methods: [POST] diff --git a/src/Controller/CMBRAPIController.php b/src/Controller/CMBRAPIController.php index 4d9ab33b..a1043c04 100644 --- a/src/Controller/CMBRAPIController.php +++ b/src/Controller/CMBRAPIController.php @@ -547,5 +547,50 @@ class CMBRAPIController extends Controller return $res->getReturnResponse(); } + public function uploadArrivePhotos(Request $req, RiderAPIHandlerInterface $rapi_handler) + { + $res = new APIResult(); + + $data = $rapi_handler->uploadArrivePhotos($req); + + if (isset($data['error'])) + { + $message = $data['error']; + + $res->setError(true) + ->setErrorMessage($message); + } + else + { + $res->setData($data); + } + + // response + return $res->getReturnResponse(); + } + + public function uploadFinishPhotos(Request $req, RiderAPIHandlerInterface $rapi_handler) + { + $res = new APIResult(); + + $data = $rapi_handler->uploadFinishPhotos($req); + + if (isset($data['error'])) + { + $message = $data['error']; + + $res->setError(true) + ->setErrorMessage($message); + } + else + { + $res->setData($data); + } + + // response + return $res->getReturnResponse(); + } + + } diff --git a/src/Entity/JOExtra.php b/src/Entity/JOExtra.php new file mode 100644 index 00000000..4f5d40b6 --- /dev/null +++ b/src/Entity/JOExtra.php @@ -0,0 +1,122 @@ +id; + } + + public function setBeforeSpeedImageFilename($image_filename) + { + $this->before_speed_image_filename = $image_filename; + return $this; + } + + public function getBeforeSpeedImageFilename($image_filename) + { + return $this->before_speed_image_filename; + } + + public function setBeforePlateNumImageFilename($image_filename) + { + $this->before_plate_num_image_filename = $image_filename; + return $this; + } + + public function getBeforePlateNumImageFilename($image_filename) + { + return $this->before_plate_num_image_filename; + } + + public function setBeforeBattImageFilename($image_filename) + { + $this->before_batt_image_filename = $image_filename; + return $this; + } + + public function getBeforeBattImageFilename($image_filename) + { + return $this->before_batt_image_filename; + } + public function setAfterSpeedImageFilename($image_filename) + { + $this->after_speed_image_filename = $image_filename; + return $this; + } + + public function getAfterSpeedImageFilename($image_filename) + { + return $this->after_speed_image_filename; + } + + public function setAfterPlateNumImageFilename($image_filename) + { + $this->after_plate_num_image_filename = $image_filename; + return $this; + } + + public function getAfterPlateNumImageFilename($image_filename) + { + return $this->after_plate_num_image_filename; + } + + public function setAfterBattImageFilename($image_filename) + { + $this->after_batt_image_filename = $image_filename; + return $this; + } + + public function getAfterBattImageFilename($image_filename) + { + return $this->after_batt_image_filename; + } + + +} diff --git a/src/Entity/JobOrder.php b/src/Entity/JobOrder.php index 8bcdbb77..a174fb96 100644 --- a/src/Entity/JobOrder.php +++ b/src/Entity/JobOrder.php @@ -334,6 +334,12 @@ class JobOrder */ protected $phone_mobile; + // link to JOExtra + /** + * @ORM\OneToOne(targetEntity="JOExtra") + */ + protected $jo_extra; + public function __construct() { $this->date_create = new DateTime(); @@ -962,5 +968,15 @@ class JobOrder return $this->phone_mobile; } + public function setJOExtra(JOExtra $jo_extra) + { + $this->jo_extra = $jo_extra; + return $this; + } + + public function getJOExtra() + { + return $this->jo_extra; + } } diff --git a/src/Service/RiderAPIHandler/CMBRiderAPIHandler.php b/src/Service/RiderAPIHandler/CMBRiderAPIHandler.php index a14ff160..75ccb4b7 100644 --- a/src/Service/RiderAPIHandler/CMBRiderAPIHandler.php +++ b/src/Service/RiderAPIHandler/CMBRiderAPIHandler.php @@ -31,6 +31,7 @@ use App\Entity\Battery; use App\Entity\BatteryModel; use App\Entity\BatterySize; use App\Entity\JobOrder; +use App\Entity\JOExtra; use DateTime; use DateInterval; @@ -49,12 +50,13 @@ class CMBRiderAPIHandler implements RiderAPIHandlerInterface protected $jo_handler; protected $ic; protected $session; + protected $upload_dir; public function __construct(EntityManagerInterface $em, RedisClientProvider $redis, EncoderFactoryInterface $ef, RiderCache $rcache, string $country_code, MQTTClient $mclient, WarrantyHandler $wh, JobOrderHandlerInterface $jo_handler, - InvoiceGeneratorInterface $ic) + InvoiceGeneratorInterface $ic, string $upload_dir) { $this->em = $em; $this->redis = $redis; @@ -65,6 +67,7 @@ class CMBRiderAPIHandler implements RiderAPIHandlerInterface $this->wh = $wh; $this->jo_handler = $jo_handler; $this->ic = $ic; + $this->upload_dir = $upload_dir; // one device = one session, since we have control over the devices // when a rider logs in, we just change the rider assigned to the device @@ -1313,6 +1316,86 @@ class CMBRiderAPIHandler implements RiderAPIHandlerInterface return $data; } + public function uploadArrivePhotos(Request $req) + { + $required_params = [ + 'jo_id', + ]; + $data = $this->checkActiveJO($req, $required_params, $jo); + if (isset($data['error'])) + return $data; + + $dest = $this->upload_dir; + + $speed_img_file = $req->files->get('speedomtr_img'); + $batt_img_file = $req->files->get('battery_img'); + $plate_num_img_file = $req->files->get('plate_number_img'); + + if ((empty($speed_img_file)) && + (empty($batt_img_file)) && + (empty($plate_num_img_file))) + { + $data = [ + 'error' => 'No image files received.' + ]; + return $data; + } + else + { + // save speedometer file + $orig_speed_filename = pathinfo($speed_img_file->getClientOriginalName(), PATHINFO_FILENAME); + $new_speed_filename = uniqid() . '-'. $orig_speed_filename . '.' . $speed_img_file->guessClientExtension(); + + // save battery file + $orig_batt_filename = pathinfo($batt_img_file->getClientOriginalName(), PATHINFO_FILENAME); + $new_batt_filename = uniqid() . '-' . $orig_batt_filename . '.' . $batt_img_file->guessClientExtension(); + + // save plate number file + $orig_plate_num_filename = pathinfo($plate_num_img_file->getClientOriginalName(), PATHINFO_FILENAME); + $new_plate_num_filename = uniqid() . '-' . $orig_plate_num_filename . '.' . $plate_num_img_file->guessClientExtension(); + + try + { + $speed_img_file->move($dest, $new_speed_filename); + $batt_img_file->move($dest, $new_batt_filename); + $plate_num_img_file->move($dest, $new_plate_num_filename); + } + catch (FileException $e) + { + $data = [ + 'error' => 'Error saving image files.' + ]; + return $data; + } + + // create JOExtra entity + $jo_extra = new JOExtra(); + + $jo_extra->setBeforeSpeedImageFilename($new_speed_filename); + $jo_extra->setBeforeBattImageFilename($new_batt_filename); + $jo_extra->setBeforePlateNumImageFilename($new_plate_num_filename); + + $jo->setJOExtra($jo_extra); + + $this->em->persist($jo_extra); + $this->em->flush(); + } + + return $data; + } + + public function uploadFinishPhotos(Request $req) + { + $required_params = [ + 'jo_id', + ]; + $data = $this->checkActiveJO($req, $required_params, $jo); + if (isset($data['error'])) + return $data; + + return $data; + } + protected function checkMissingParameters(Request $req, $params = []) { $missing = [];