Handle file uploads for invoice and warranty card pics #540
This commit is contained in:
parent
d7485e0058
commit
25dfdf1bbc
3 changed files with 140 additions and 65 deletions
0
public/.gitkeep
Normal file
0
public/.gitkeep
Normal file
|
|
@ -12,6 +12,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||||
use Symfony\Component\Dotenv\Dotenv;
|
use Symfony\Component\Dotenv\Dotenv;
|
||||||
use Symfony\Component\HttpFoundation\RequestStack;
|
use Symfony\Component\HttpFoundation\RequestStack;
|
||||||
|
use Symfony\Component\HttpKernel\KernelInterface;
|
||||||
|
|
||||||
use CrEOF\Spatial\PHP\Types\Geometry\Point;
|
use CrEOF\Spatial\PHP\Types\Geometry\Point;
|
||||||
|
|
||||||
|
|
@ -2751,6 +2752,31 @@ class APIController extends Controller implements LoggedController
|
||||||
return $res->getReturnResponse();
|
return $res->getReturnResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function checkCustomerPlateNumber($plate_number, $cust)
|
||||||
|
{
|
||||||
|
// strip spaces and make all caps
|
||||||
|
$plate_number = preg_replace('/\s+/', '', strtoupper($plate_number));
|
||||||
|
|
||||||
|
// if there's no customer linked to session
|
||||||
|
if ($cust != null)
|
||||||
|
{
|
||||||
|
// check all the customer vehicles
|
||||||
|
$cvs = $cust->getVehicles();
|
||||||
|
foreach ($cvs as $cv)
|
||||||
|
{
|
||||||
|
$cv_plate = preg_replace('/\s+/', '', strtoupper($cv->getPlateNumber()));
|
||||||
|
|
||||||
|
// did we find a match?
|
||||||
|
if ($cv_plate == $plate_number)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public function warrantyCheck($serial, EntityManagerInterface $em, Request $req)
|
public function warrantyCheck($serial, EntityManagerInterface $em, Request $req)
|
||||||
{
|
{
|
||||||
// check required parameters and api key
|
// check required parameters and api key
|
||||||
|
|
@ -2759,24 +2785,6 @@ class APIController extends Controller implements LoggedController
|
||||||
if ($res->isError())
|
if ($res->isError())
|
||||||
return $res->getReturnResponse();
|
return $res->getReturnResponse();
|
||||||
|
|
||||||
/*
|
|
||||||
// initialize data
|
|
||||||
$data = [
|
|
||||||
'is_valid' => false,
|
|
||||||
'is_registered' => false,
|
|
||||||
'customer' => [
|
|
||||||
'first_name' => '',
|
|
||||||
'last_name' => '',
|
|
||||||
'mobile_number' => '',
|
|
||||||
'plate_number' => '',
|
|
||||||
],
|
|
||||||
'battery' => [
|
|
||||||
'brand' => '',
|
|
||||||
'size' => '',
|
|
||||||
],
|
|
||||||
];
|
|
||||||
*/
|
|
||||||
|
|
||||||
// check if warranty serial is there
|
// check if warranty serial is there
|
||||||
$warr_serial = $em->getRepository(WarrantySerial::class)->find($serial);
|
$warr_serial = $em->getRepository(WarrantySerial::class)->find($serial);
|
||||||
$warr = $em->getRepository(Warranty::class)->findOneBy(['serial' => $serial]);
|
$warr = $em->getRepository(Warranty::class)->findOneBy(['serial' => $serial]);
|
||||||
|
|
@ -2790,29 +2798,39 @@ class APIController extends Controller implements LoggedController
|
||||||
return $res->getReturnResponse();
|
return $res->getReturnResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
// if warranty serial is there
|
// if we have a warranty entry for the serial already
|
||||||
if ($warr_serial != null)
|
if ($warr != null)
|
||||||
{
|
{
|
||||||
// if we have a warranty entry for the serial already
|
$warr_plate = $warr->getPlateNumber();
|
||||||
if ($warr != null)
|
$is_registered = true;
|
||||||
|
$is_customer_warranty = false;
|
||||||
|
|
||||||
|
// TODO: check if the warranty is registered to a car owned by the customer
|
||||||
|
$cust = $this->session->getCustomer();
|
||||||
|
|
||||||
|
$is_customer_warranty = $this->checkCustomerPlateNumber($warr_plate, $cust);
|
||||||
|
|
||||||
|
// null mobile number should be blank string instead
|
||||||
|
if ($warr->getMobileNumber() == null)
|
||||||
|
$mobile_num = '';
|
||||||
|
else
|
||||||
|
$mobile_num = $warr->getMobileNumber();
|
||||||
|
|
||||||
|
$can_edit = $is_customer_warranty;
|
||||||
|
|
||||||
|
// if customer plate number matches the one registered on the warranty
|
||||||
|
if ($is_customer_warranty)
|
||||||
{
|
{
|
||||||
$is_registered = true;
|
|
||||||
|
|
||||||
// null mobile number should be blank string instead
|
|
||||||
if ($warr->getMobileNumber() == null)
|
|
||||||
$mobile_num = '';
|
|
||||||
else
|
|
||||||
$mobile_num = $warr->getMobileNumber();
|
|
||||||
|
|
||||||
$customer = [
|
$customer = [
|
||||||
'first_name' => $warr->getFirstName(),
|
'first_name' => $warr->getFirstName(),
|
||||||
'last_name' => $warr->getLastName(),
|
'last_name' => $warr->getLastName(),
|
||||||
'mobile_number' => $mobile_num,
|
'mobile_number' => $mobile_num,
|
||||||
'plate_number' => $warr->getPlateNumber(),
|
'plate_number' => $warr_plate,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
// hide customer information if customer is not the one registered
|
||||||
$customer = [
|
$customer = [
|
||||||
'first_name' => '',
|
'first_name' => '',
|
||||||
'last_name' => '',
|
'last_name' => '',
|
||||||
|
|
@ -2820,46 +2838,80 @@ class APIController extends Controller implements LoggedController
|
||||||
'plate_number' => '',
|
'plate_number' => '',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
$sku = $warr_serial->getSKU();
|
else
|
||||||
$batt = $em->getRepository(SAPBattery::class)->find($sku);
|
{
|
||||||
// TODO: put this in a config file
|
$can_edit = true;
|
||||||
$image_url = $req->getSchemeAndHttpHost() . '/battery/generic.png';
|
$customer = [
|
||||||
if ($batt != null)
|
'first_name' => '',
|
||||||
{
|
'last_name' => '',
|
||||||
$battery = [
|
'mobile_number' => '',
|
||||||
'brand' => $batt->getBrand()->getName(),
|
'plate_number' => '',
|
||||||
'size' => $batt->getSize()->getName(),
|
|
||||||
'image_url' => $image_url,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$battery = [
|
|
||||||
'brand' => '',
|
|
||||||
'size' => '',
|
|
||||||
'image_url' => '',
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
// populate data
|
|
||||||
$data = [
|
|
||||||
'is_valid' => true,
|
|
||||||
'is_registered' => $is_registered,
|
|
||||||
'customer' => $customer,
|
|
||||||
'battery' => $battery,
|
|
||||||
'message' => [
|
|
||||||
'register_error' => 'Warranty serial code has already been registered.',
|
|
||||||
],
|
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$sku = $warr_serial->getSKU();
|
||||||
|
$batt = $em->getRepository(SAPBattery::class)->find($sku);
|
||||||
|
// TODO: put this in a config file
|
||||||
|
$image_url = $req->getSchemeAndHttpHost() . '/battery/generic.png';
|
||||||
|
if ($batt != null)
|
||||||
|
{
|
||||||
|
$battery = [
|
||||||
|
'brand' => $batt->getBrand()->getName(),
|
||||||
|
'size' => $batt->getSize()->getName(),
|
||||||
|
'image_url' => $image_url,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$battery = [
|
||||||
|
'brand' => '',
|
||||||
|
'size' => '',
|
||||||
|
'image_url' => '',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
// populate data
|
||||||
|
$data = [
|
||||||
|
'is_valid' => true,
|
||||||
|
'is_registered' => $is_registered,
|
||||||
|
'can_edit' => $can_edit,
|
||||||
|
'customer' => $customer,
|
||||||
|
'battery' => $battery,
|
||||||
|
'message' => [
|
||||||
|
'register_error' => 'Warranty serial code has already been registered.',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
$res->setData($data);
|
$res->setData($data);
|
||||||
|
|
||||||
return $res->getReturnResponse();
|
return $res->getReturnResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function warrantyRegister($serial, EntityManagerInterface $em, Request $req)
|
protected function handlePictureUpload($file, $target_dir, $serial, $name)
|
||||||
|
{
|
||||||
|
// no file sent
|
||||||
|
if ($file == null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
// create target dir if it doesn't exist
|
||||||
|
if (!file_exists($target_dir))
|
||||||
|
{
|
||||||
|
if (!mkdir($target_dir, 0744, true))
|
||||||
|
{
|
||||||
|
error_log('failed to create folder for warranty pictures');
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// move file
|
||||||
|
$filename = $name . $file->getClientOriginalExtension();
|
||||||
|
$file->move($target_dir . '/' . $serial, $filename);
|
||||||
|
|
||||||
|
return $serial . '/' . $filename;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function warrantyRegister($serial, EntityManagerInterface $em, Request $req, KernelInterface $kernel)
|
||||||
{
|
{
|
||||||
// check required parameters and api key
|
// check required parameters and api key
|
||||||
$required_params = [
|
$required_params = [
|
||||||
|
|
@ -2868,6 +2920,16 @@ class APIController extends Controller implements LoggedController
|
||||||
'email',
|
'email',
|
||||||
'plate_number',
|
'plate_number',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// handle file uploads
|
||||||
|
$invoice = $req->files->get('invoice');
|
||||||
|
$warr_card = $req->files->get('warr_card');
|
||||||
|
|
||||||
|
// process picture uploads
|
||||||
|
$upload_dir = $kernel->getProjectDir() . '/public/warranty_uploads';
|
||||||
|
$inv_filename = $this->warrantyRegister($invoice, $upload_dir, $serial, 'invoice');
|
||||||
|
$wcard_filename = $this->warrantyRegister($warr_card, $upload_dir, $serial, 'wcard');
|
||||||
|
|
||||||
$res = $this->checkParamsAndKey($req, $em, $required_params);
|
$res = $this->checkParamsAndKey($req, $em, $required_params);
|
||||||
if ($res->isError())
|
if ($res->isError())
|
||||||
return $res->getReturnResponse();
|
return $res->getReturnResponse();
|
||||||
|
|
@ -2876,14 +2938,14 @@ class APIController extends Controller implements LoggedController
|
||||||
$cust = $this->updateCustomerInfo($req, $em);
|
$cust = $this->updateCustomerInfo($req, $em);
|
||||||
|
|
||||||
// update warranty
|
// update warranty
|
||||||
$res = $this->updateWarranty($res, $em, $req, $serial);
|
$res = $this->updateWarranty($res, $em, $req, $serial, $inv_filename, $wcard_filename);
|
||||||
|
|
||||||
$em->flush();
|
$em->flush();
|
||||||
|
|
||||||
return $res->getReturnResponse();
|
return $res->getReturnResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function updateWarranty($res, $em, $req, $serial)
|
protected function updateWarranty($res, $em, $req, $serial, $inv_filename = null, $wcard_filename = null)
|
||||||
{
|
{
|
||||||
// get serial
|
// get serial
|
||||||
$warr_serial = $em->getRepository(WarrantySerial::class)->find($serial);
|
$warr_serial = $em->getRepository(WarrantySerial::class)->find($serial);
|
||||||
|
|
|
||||||
|
|
@ -141,6 +141,19 @@ class Warranty
|
||||||
*/
|
*/
|
||||||
protected $privacy_policy;
|
protected $privacy_policy;
|
||||||
|
|
||||||
|
|
||||||
|
// invoice picture
|
||||||
|
/**
|
||||||
|
* @ORM\Column(type="string", length=80)
|
||||||
|
*/
|
||||||
|
protected $file_invoice;
|
||||||
|
|
||||||
|
// warranty card picture
|
||||||
|
/**
|
||||||
|
* @ORM\Column(type="string", length=80)
|
||||||
|
*/
|
||||||
|
protected $file_warr_card;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->date_create = new DateTime();
|
$this->date_create = new DateTime();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue