Add register functionality #540
This commit is contained in:
parent
f08d7e03aa
commit
ae9d32d434
1 changed files with 113 additions and 3 deletions
|
|
@ -4,12 +4,14 @@ namespace App\Controller\CAPI;
|
|||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpKernel\KernelInterface;
|
||||
use Doctrine\ORM\Query;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
|
||||
use Catalyst\APIBundle\Controller\APIController;
|
||||
use Catalyst\APIBundle\Response\APIResponse;
|
||||
|
||||
|
||||
use App\Entity\WarrantySerial;
|
||||
use App\Entity\Warranty;
|
||||
use App\Entity\BatteryModel;
|
||||
|
|
@ -94,6 +96,8 @@ class CustomerWarrantyController extends APIController
|
|||
if (!$res)
|
||||
return $res;
|
||||
|
||||
error_log('check warranty serial');
|
||||
|
||||
// check if warranty serial is there
|
||||
$warr_serial = $em->getRepository(WarrantySerial::class)->find($serial);
|
||||
$warr = $em->getRepository(Warranty::class)->findOneBy(['serial' => $serial]);
|
||||
|
|
@ -110,6 +114,8 @@ class CustomerWarrantyController extends APIController
|
|||
// if we have a warranty entry for the serial already
|
||||
if ($warr != null)
|
||||
{
|
||||
error_log('already have warranty.');
|
||||
|
||||
$warr_plate = $warr->getPlateNumber();
|
||||
$is_registered = true;
|
||||
|
||||
|
|
@ -206,8 +212,9 @@ class CustomerWarrantyController extends APIController
|
|||
}
|
||||
|
||||
|
||||
public function register($serial, EntityManagerInterface $em, Request $req)
|
||||
public function register($serial, EntityManagerInterface $em, Request $req, KernelInterface $kernel)
|
||||
{
|
||||
error_log('HERE - register');
|
||||
// check required parameters
|
||||
$required_params = [
|
||||
'first_name',
|
||||
|
|
@ -230,6 +237,7 @@ class CustomerWarrantyController extends APIController
|
|||
$invoice = $req->files->get('invoice');
|
||||
$warr_card = $req->files->get('warr_card');
|
||||
|
||||
error_log('handling file uploads');
|
||||
// process picture uploads
|
||||
$upload_dir = $kernel->getProjectDir() . '/public/warranty_uploads';
|
||||
$inv_filename = $this->handlePictureUpload($invoice, $upload_dir, $serial, 'invoice');
|
||||
|
|
@ -247,12 +255,19 @@ class CustomerWarrantyController extends APIController
|
|||
|
||||
error_log(print_r($data, true));
|
||||
|
||||
error_log('updating warranty');
|
||||
|
||||
// do actual registering
|
||||
$res = $this->updateWarranty($em, $req, $serial, $inv_filename, $wcard_filename);
|
||||
|
||||
// flush to db
|
||||
$em->flush();
|
||||
|
||||
$data = [];
|
||||
return $res;
|
||||
|
||||
return new APIResponse(true, 'Warranty registered.', $data);
|
||||
error_log('after updating warranty');
|
||||
|
||||
return new APIResponse(true, 'Warranty registered.');
|
||||
}
|
||||
|
||||
// TODO: move this to a service, since it's shared by all warranty updaters
|
||||
|
|
@ -286,4 +301,99 @@ class CustomerWarrantyController extends APIController
|
|||
return $serial . '/' . $filename;
|
||||
}
|
||||
|
||||
protected function updateWarranty($em, $req, $serial, $inv_filename = null, $wcard_filename = null)
|
||||
{
|
||||
error_log('warranty serial check');
|
||||
// get serial
|
||||
$warr_serial = $em->getRepository(WarrantySerial::class)->find($serial);
|
||||
if ($warr_serial == null)
|
||||
{
|
||||
return new APIResponse(false, 'Invalid warranty serial code.');
|
||||
}
|
||||
|
||||
// check if warranty exists already
|
||||
$warr = $em->getRepository(Warranty::class)->findOneBy(['serial' => $serial]);
|
||||
|
||||
// skip warranty if it already exists
|
||||
if ($warr != null)
|
||||
{
|
||||
return new APIResponse(false, 'Warranty registred to a vehicle not in your list of vehicles.');
|
||||
|
||||
/*
|
||||
// check if warranty is registered to a serial owned by customer
|
||||
$warr_plate = $warr->getPlateNumber();
|
||||
$cust = $this->session->getCustomer();
|
||||
$is_customer_warranty = $this->checkCustomerPlateNumber($warr_plate, $cust);
|
||||
|
||||
if (!$is_customer_warranty)
|
||||
{
|
||||
return new APIResponse(false, 'Warranty registred to a vehicle not in your list of vehicles.');
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
$warr = new Warranty();
|
||||
}
|
||||
|
||||
error_log('sap battery check');
|
||||
// get sap battery
|
||||
$sku = $warr_serial->getSKU();
|
||||
$sap_bty = $em->getRepository(SAPBattery::class)->find($sku);
|
||||
if ($sap_bty == null)
|
||||
{
|
||||
return new APIResponse(false, 'Could not find battery entry for warranty.');
|
||||
}
|
||||
|
||||
error_log('date check');
|
||||
// default date purchase to today
|
||||
// NOTE: might need to change this later
|
||||
$date_pur = new DateTime();
|
||||
|
||||
// get date purchase specified by customer
|
||||
$date_pur_cust = DateTime::createFromFormat('Y-m-d', $req->request->get('date_purchase'));
|
||||
if (!$date_pur_cust)
|
||||
{
|
||||
return new APIResponse(false, 'Invalid date format for date of purchase.');
|
||||
}
|
||||
|
||||
|
||||
error_log('update entity / database');
|
||||
// create or update warranty entry
|
||||
$warr->setSerial($serial)
|
||||
->setFirstName($req->request->get('first_name'))
|
||||
->setLastName($req->request->get('last_name'))
|
||||
->setEmail($req->request->get('email'))
|
||||
->setPlateNumber($req->request->get('plate_num'))
|
||||
// TODO: figure out how to compute date of purchase
|
||||
->setDatePurchase($date_pur)
|
||||
// TODO: set status
|
||||
// ->setStatus()
|
||||
// TODO: set battery model and size id
|
||||
// ->setBatterySize()
|
||||
// ->setBatteryModel()
|
||||
->setSAPBattery($sap_bty)
|
||||
// ->setMobileNumber(substr($this->session->getPhoneNumber(), 2))
|
||||
->setActivated(true)
|
||||
|
||||
// files
|
||||
->setFileInvoice($inv_filename)
|
||||
->setFileWarrantyCard($wcard_filename)
|
||||
|
||||
// new fields
|
||||
->setOdometer($req->request->get('odometer', 0))
|
||||
->setDatePurchaseCustomer($date_pur_cust)
|
||||
->setValidated(false);
|
||||
|
||||
// TODO: check for date purchase and date expire
|
||||
|
||||
$em->persist($warr);
|
||||
|
||||
// TODO: check if we need to do anyting else
|
||||
$data = [];
|
||||
|
||||
return new APIResponse(true, 'Warranty registered.', $data);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue