247 lines
6.7 KiB
PHP
247 lines
6.7 KiB
PHP
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Ramcar\CrudException;
|
|
|
|
use App\Service\CustomerHandlerInterface;
|
|
use App\Entity\Customer;
|
|
use App\Entity\CustomerVehicle;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
|
|
|
use Catalyst\MenuBundle\Annotation\Menu;
|
|
|
|
class CustomerController extends Controller
|
|
{
|
|
|
|
/**
|
|
* @Menu(selected="customer_list")
|
|
*/
|
|
public function index(CustomerHandlerInterface $cust_handler)
|
|
{
|
|
$this->denyAccessUnlessGranted('customer.list', null, 'No access.');
|
|
|
|
$params = $cust_handler->initializeCustomerIndexForm();
|
|
|
|
$template = $params['template'];
|
|
|
|
return $this->render($template);
|
|
}
|
|
|
|
public function rows(Request $req, CustomerHandlerInterface $cust_handler)
|
|
{
|
|
$this->denyAccessUnlessGranted('customer.list', null, 'No access.');
|
|
|
|
$params = $cust_handler->getCustomers($req);
|
|
|
|
$meta = $params['meta'];
|
|
$rows = $params['rows'];
|
|
|
|
// process rows
|
|
foreach ($rows as $key => $data) {
|
|
// add crud urls
|
|
$cust_id = $rows[$key]['id'];
|
|
|
|
if ($this->isGranted('customer.update'))
|
|
$rows[$key]['meta']['update_url'] = $this->generateUrl('customer_update', ['id' => $cust_id]);
|
|
if ($this->isGranted('customer.delete'))
|
|
$rows[$key]['meta']['delete_url'] = $this->generateUrl('customer_delete', ['id' => $cust_id]);
|
|
}
|
|
|
|
// response
|
|
return $this->json([
|
|
'meta' => $meta,
|
|
'data' => $rows
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @Menu(selected="customer_list")
|
|
*/
|
|
public function addForm(CustomerHandlerInterface $cust_handler)
|
|
{
|
|
$this->denyAccessUnlessGranted('customer.add', null, 'No access.');
|
|
|
|
$params = $cust_handler->initializeAddCustomerForm();
|
|
|
|
$template = $params['template'];
|
|
|
|
// response
|
|
return $this->render($template, $params);
|
|
}
|
|
|
|
public function addSubmit(Request $req, CustomerHandlerInterface $cust_handler)
|
|
{
|
|
$this->denyAccessUnlessGranted('customer.add', null, 'No access.');
|
|
|
|
$result = $cust_handler->addCustomer($req);
|
|
|
|
if (isset($result['id']))
|
|
{
|
|
$id = $result['id'];
|
|
|
|
// return successful response
|
|
return $this->json([
|
|
'success' => 'Changes have been saved!',
|
|
'id' => $id
|
|
]);
|
|
}
|
|
else
|
|
{
|
|
$error_array = $result['error_array'];
|
|
$nerror_array = $result['nerror_array'];
|
|
$verror_array = $result['verror_array'];
|
|
|
|
// return validation failure response
|
|
return $this->json([
|
|
'success' => false,
|
|
'errors' => $error_array,
|
|
'nerrors' => $nerror_array,
|
|
'verrors' => $verror_array
|
|
], 422);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @Menu(selected="customer_list")
|
|
*/
|
|
public function updateForm($id, CustomerHandlerInterface $cust_handler)
|
|
{
|
|
$this->denyAccessUnlessGranted('customer.update', null, 'No access.');
|
|
|
|
$params = $cust_handler->initializeUpdateCustomerForm($id);
|
|
|
|
$template = $params['template'];
|
|
|
|
// response
|
|
return $this->render($template, $params);
|
|
}
|
|
|
|
public function updateSubmit(Request $req, CustomerHandlerInterface $cust_handler, $id)
|
|
{
|
|
$this->denyAccessUnlessGranted('customer.update', null, 'No access.');
|
|
|
|
try
|
|
{
|
|
$result = $cust_handler->updateCustomer($req, $id);
|
|
}
|
|
catch (CrudException $e)
|
|
{
|
|
throw new CrudException($e->getMessage());
|
|
}
|
|
|
|
if (isset($result['id']))
|
|
{
|
|
$id = $result['id'];
|
|
|
|
// return successful response
|
|
return $this->json([
|
|
'success' => 'Changes have been saved!',
|
|
'id' => $id
|
|
]);
|
|
}
|
|
else
|
|
{
|
|
$error_array = $result['error_array'];
|
|
$nerror_array = $result['nerror_array'];
|
|
$verror_array = $result['verror_array'];
|
|
|
|
// return validation failure response
|
|
return $this->json([
|
|
'success' => false,
|
|
'errors' => $error_array,
|
|
'nerrors' => $nerror_array,
|
|
'verrors' => $verror_array
|
|
], 422);
|
|
}
|
|
}
|
|
|
|
public function destroy($id, CustomerHandlerInterface $cust_handler)
|
|
{
|
|
$this->denyAccessUnlessGranted('customer.delete', null, 'No access.');
|
|
|
|
try
|
|
{
|
|
$cust_handler->deleteCustomer($id);
|
|
}
|
|
catch (NotFoundHttpException $e)
|
|
{
|
|
throw $this->createNotFoundException($e->getMessage());
|
|
}
|
|
|
|
// response
|
|
$response = new Response();
|
|
$response->setStatusCode(Response::HTTP_OK);
|
|
$response->send();
|
|
}
|
|
|
|
public function getCustomerVehicles(Request $req, CustomerHandlerInterface $cust_handler)
|
|
{
|
|
if (!(($this->isGranted('jo_onestep.form')) ||
|
|
($this->isGranted('jo_walkin.form')) ||
|
|
($this->isGranted('jo_in.list'))))
|
|
{
|
|
$exception = $this->createAccessDeniedException('No access.');
|
|
throw $exception;
|
|
}
|
|
|
|
$results = $cust_handler->getCustomerVehicles($req);
|
|
|
|
$vehicles = $results['vehicles'];
|
|
$has_more_pages = $results['has_more_pages'];
|
|
|
|
// response
|
|
return $this->json([
|
|
'success' => true,
|
|
'results' => $vehicles,
|
|
'pagination' => [
|
|
'more' => $has_more_pages
|
|
]
|
|
]);
|
|
}
|
|
|
|
public function getCustomerVehicleInfo(Request $req, CustomerHandlerInterface $cust_handler)
|
|
{
|
|
$this->denyAccessUnlessGranted('jo_in.list', null, 'No access.');
|
|
|
|
$result = $cust_handler->getCustomerVehicleInfo($req);
|
|
|
|
if ($result == null)
|
|
{
|
|
return $this->json([
|
|
'success' => false,
|
|
'error' => 'The item does not exist'
|
|
]);
|
|
}
|
|
else
|
|
{
|
|
// response
|
|
return $this->json([
|
|
'success' => true,
|
|
'data' => $result
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function editCustomerVehicleWarranty($id, Request $req, EntityManagerInterface $em)
|
|
{
|
|
$cv = $em->getRepository(CustomerVehicle::class)->find($id);
|
|
|
|
$serial = $req->request->get('warr_serial');
|
|
$cv->setWarrantyCode($serial);
|
|
|
|
$em->flush();
|
|
|
|
return $this->json([
|
|
'success' => true,
|
|
'data' => [
|
|
'warranty_code' => $serial,
|
|
],
|
|
]);
|
|
}
|
|
}
|