From 72112ebe6c03d87be640c3d4352e8444f86a6e37 Mon Sep 17 00:00:00 2001 From: Ramon Gutierrez Date: Wed, 31 Jan 2018 05:59:48 +0800 Subject: [PATCH] Initial commit for ticketing system --- config/acl.yaml | 19 ++ config/menu.yaml | 8 + config/routes/ticket.yaml | 40 +++ src/Controller/TicketController.php | 405 ++++++++++++++++++++++++++++ src/Entity/Ticket.php | 52 +++- src/Ramcar/TicketStatus.php | 31 +++ src/Ramcar/TicketType.php | 35 +++ templates/customer/form.html.twig | 84 +++++- templates/ticket/form.html.twig | 188 +++++++++++++ templates/ticket/list.html.twig | 173 ++++++++++++ 10 files changed, 1020 insertions(+), 15 deletions(-) create mode 100644 config/routes/ticket.yaml create mode 100644 src/Controller/TicketController.php create mode 100644 src/Ramcar/TicketStatus.php create mode 100644 src/Ramcar/TicketType.php create mode 100644 templates/ticket/form.html.twig create mode 100644 templates/ticket/list.html.twig diff --git a/config/acl.yaml b/config/acl.yaml index 38e9228d..50b26ed4 100644 --- a/config/acl.yaml +++ b/config/acl.yaml @@ -187,3 +187,22 @@ access_keys: label: Processing - id: jo_assign.list label: Assigning + + - id: support + label: Customer Support Access + acls: + - id: support.menu + label: Menu + - id: ticket + label: Ticket Access + acls: + - id: ticket.menu + label: Menu + - id: ticket.list + label: List + - id: ticket.add + label: Add + - id: ticket.update + label: Update + - id: ticket.delete + label: Delete diff --git a/config/menu.yaml b/config/menu.yaml index f7f1477b..4828b709 100644 --- a/config/menu.yaml +++ b/config/menu.yaml @@ -94,3 +94,11 @@ main_menu: label: Assigning parent: joborder + - id: support + acl: support.menu + label: Customer Support + icon: flaticon-support + - id: ticket_list + acl: ticket.list + label: Tickets + parent: support diff --git a/config/routes/ticket.yaml b/config/routes/ticket.yaml new file mode 100644 index 00000000..a1d6bdff --- /dev/null +++ b/config/routes/ticket.yaml @@ -0,0 +1,40 @@ +# ticket + +ticket_list: + path: /tickets + controller: App\Controller\TicketController::index + +ticket_rows: + path: /tickets/rows + controller: App\Controller\TicketController::rows + methods: [POST] + +ticket_create: + path: /tickets/create/{customer_id} + controller: App\Controller\TicketController::addForm + methods: [GET] + defaults: + customer_id: false + +ticket_create_submit: + path: /tickets/create/{customer_id} + controller: App\Controller\TicketController::addSubmit + methods: [POST] + defaults: + customer_id: false + +ticket_update: + path: /tickets/{id} + controller: App\Controller\TicketController::updateForm + methods: [GET] + +ticket_update_submit: + path: /tickets/{id} + controller: App\Controller\TicketController::updateSubmit + methods: [POST] + +ticket_delete: + path: /tickets/{id} + controller: App\Controller\TicketController::destroy + methods: [DELETE] + diff --git a/src/Controller/TicketController.php b/src/Controller/TicketController.php new file mode 100644 index 00000000..0ae6e2eb --- /dev/null +++ b/src/Controller/TicketController.php @@ -0,0 +1,405 @@ +denyAccessUnlessGranted('ticket.list', null, 'No access.'); + + $params = $this->initParameters('ticket_list'); + + return $this->render('ticket/list.html.twig', $params); + } + + public function rows(Request $req) + { + $this->denyAccessUnlessGranted('ticket.list', null, 'No access.'); + + // get query builder + $qb = $this->getDoctrine() + ->getRepository(Ticket::class) + ->createQueryBuilder('q'); + + // get datatable params + $datatable = $req->request->get('datatable'); + + // count total records + $tquery = $qb->select('COUNT(q)') + ->leftJoin('q.created_by', 'u'); + + // add filters to count query + $this->setQueryFilters($datatable, $tquery, $qb); + + $total = $tquery->getQuery() + ->getSingleScalarResult(); + + // get current page number + $page = $datatable['pagination']['page'] ?? 1; + + $perpage = $datatable['pagination']['perpage']; + $offset = ($page - 1) * $perpage; + + // add metadata + $meta = [ + 'page' => $page, + 'perpage' => $perpage, + 'pages' => ceil($total / $perpage), + 'total' => $total, + 'sort' => 'asc', + 'field' => 'id' + ]; + + // build query + $query = $qb->select('q') + ->addSelect($qb->expr()->concat('u.first_name', $qb->expr()->literal(' '), 'u.last_name') . ' as user_full_name'); + + // add filters to query + $this->setQueryFilters($datatable, $query, $qb); + + // check if sorting is present, otherwise use default + if (isset($datatable['sort']['field']) && !empty($datatable['sort']['field'])) { + $prefix = ''; + + if (!in_array($datatable['sort']['field'], ['user_full_name'])) + $prefix = 'q.'; + + $order = $datatable['sort']['sort'] ?? 'asc'; + $query->orderBy($prefix . $datatable['sort']['field'], $order); + } else { + $query->orderBy('q.id', 'asc'); + } + + // get rows for this page + $obj_rows = $query->setFirstResult($offset) + ->setMaxResults($perpage) + ->getQuery() + ->getResult(); + + // process rows + $rows = []; + foreach ($obj_rows as $orow) { + // add row data + $row['id'] = $orow[0]->getID(); + $row['date_create'] = $orow[0]->getDateCreate()->format("d M Y g:i A"); + $row['first_name'] = $orow[0]->getFirstName(); + $row['last_name'] = $orow[0]->getLastName(); + $row['contact_num'] = $orow[0]->getContactNumber(); + $row['status'] = $orow[0]->getStatusText(); + $row['ticket_type'] = $orow[0]->getTicketTypeText(); + $row['user_full_name'] = $orow['user_full_name']; + + // add row metadata + $row['meta'] = [ + 'update_url' => '', + 'delete_url' => '' + ]; + + // add crud urls + if ($this->isGranted('ticket.update')) + $row['meta']['update_url'] = $this->generateUrl('ticket_update', ['id' => $row['id']]); + if ($this->isGranted('ticket.delete')) + $row['meta']['delete_url'] = $this->generateUrl('ticket_delete', ['id' => $row['id']]); + + $rows[] = $row; + } + + // response + return $this->json([ + 'meta' => $meta, + 'data' => $rows + ]); + } + + public function addForm($customer_id) + { + $this->denyAccessUnlessGranted('ticket.add', null, 'No access.'); + + $params = $this->initParameters('ticket_list'); + $params['obj'] = new Ticket(); + $params['mode'] = 'create'; + $params['customer'] = false; + + // get customer data + if ($customer_id) { + $em = $this->getDoctrine()->getManager(); + $customer = $em->getRepository(Customer::class)->find($customer_id); + + // make sure this row exists + if (empty($customer)) + throw $this->createNotFoundException('This customer does not exist'); + + // add to view + $params['customer'] = $customer; + } + + // get parent associations + $params['ticket_types'] = TicketType::getCollection(); + $params['statuses'] = TicketStatus::getCollection(); + $params['other_ticket_type'] = TicketType::OTHER; + + // response + return $this->render('ticket/form.html.twig', $params); + } + + public function addSubmit(Request $req, ValidatorInterface $validator, $customer_id) + { + $this->denyAccessUnlessGranted('ticket.add', null, 'No access.'); + + $em = $this->getDoctrine()->getManager(); + + // get customer data + if ($customer_id) { + $customer = $em->getRepository(Customer::class)->find($customer_id); + + // make sure this row exists + if (empty($customer)) + throw $this->createNotFoundException('This customer does not exist'); + + // set autopopulated fields + $first_name = $customer->getFirstName(); + $last_name = $customer->getLastName(); + $contact_num = false; + + $mobile_numbers = $customer->getMobileNumbers(); + + if (!empty($mobile_numbers)) { + $contact_num = $mobile_numbers[0]->getID(); + } + } else { + // get values directly from form + $first_name = $req->request->get('first_name'); + $last_name = $req->request->get('last_name'); + $contact_num = $req->request->get('contact_num'); + } + + // create new row + $obj = new Ticket(); + + // get ticket type + $ticket_type = $req->request->get('ticket_type'); + $other_ticket_type = false; + + if ($ticket_type == TicketType::OTHER) { + $other_ticket_type = $req->request->get('other_ticket_type'); + } + + // set and save values + $obj->setFirstName($first_name) + ->setLastName($last_name) + ->setContactNumber($contact_num) + ->setStatus($req->request->get('status')) + ->setTicketType($ticket_type) + ->setOtherTicketType($other_ticket_type) + ->setDetails($req->request->get('details')) + ->setDateCreate(new DateTime()) + ->setCreatedBy($this->getUser()); + + // if assigned to customer, set association + if ($customer_id) { + $obj->setCustomer($customer); + } + + // initialize error list + $error_array = []; + + // validate + $errors = $validator->validate($obj); + + // custom validation for other ticket type + if ($ticket_type == TicketType::OTHER && empty($other_ticket_type)) { + $error_array['other_ticket_type'] = 'Ticket type not specified.'; + } + + // add errors to list + foreach ($errors as $error) { + $error_array[$error->getPropertyPath()] = $error->getMessage(); + } + + // check if any errors were found + if (!empty($error_array)) { + // return validation failure response + return $this->json([ + 'success' => false, + 'errors' => $error_array + ], 422); + } else { + // validated! save the entity + $em->persist($obj); + $em->flush(); + + // return successful response + return $this->json([ + 'success' => 'Changes have been saved!' + ]); + } + } + + public function updateForm($id) + { + $this->denyAccessUnlessGranted('ticket.update', null, 'No access.'); + + $params = $this->initParameters('ticket_list'); + $params['mode'] = 'update'; + + // get row data + $em = $this->getDoctrine()->getManager(); + $obj = $em->getRepository(Ticket::class)->find($id); + + // make sure this row exists + if (empty($obj)) + throw $this->createNotFoundException('The item does not exist'); + + $em = $this->getDoctrine()->getManager(); + + // get parent associations + $params['customer'] = $obj->getCustomer(); + $params['ticket_types'] = TicketType::getCollection(); + $params['statuses'] = TicketStatus::getCollection(); + $params['other_ticket_type'] = TicketType::OTHER; + + $params['obj'] = $obj; + + // response + return $this->render('ticket/form.html.twig', $params); + } + + public function updateSubmit(Request $req, ValidatorInterface $validator, $id) + { + $this->denyAccessUnlessGranted('ticket.update', null, 'No access.'); + + // get row data + $em = $this->getDoctrine()->getManager(); + $obj = $em->getRepository(Ticket::class)->find($id); + + // make sure this row exists + if (empty($obj)) + throw $this->createNotFoundException('The item does not exist'); + + // get customer data + if (!empty($obj->getCustomer())) { + $customer = $obj->getCustomer(); + + // make sure this row exists + if (empty($customer)) + throw $this->createNotFoundException('This customer does not exist'); + + // set autopopulated fields + $first_name = $customer->getFirstName(); + $last_name = $customer->getLastName(); + $contact_num = false; + + $mobile_numbers = $customer->getMobileNumbers(); + + if (!empty($mobile_numbers)) { + $contact_num = $mobile_numbers[0]->getID(); + } + } else { + // get values directly from form + $first_name = $req->request->get('first_name'); + $last_name = $req->request->get('last_name'); + $contact_num = $req->request->get('contact_num'); + } + + // get ticket type + $ticket_type = $req->request->get('ticket_type'); + $other_ticket_type = false; + + if ($ticket_type == TicketType::OTHER) { + $other_ticket_type = $req->request->get('other_ticket_type'); + } + + // set and save values + $obj->setFirstName($first_name) + ->setLastName($last_name) + ->setContactNumber($contact_num) + ->setStatus($req->request->get('status')) + ->setTicketType($ticket_type) + ->setOtherTicketType($other_ticket_type) + ->setDetails($req->request->get('details')); + + // initialize error list + $error_array = []; + + // validate + $errors = $validator->validate($obj); + + // custom validation for other ticket type + if ($ticket_type == TicketType::OTHER && empty($other_ticket_type)) { + $error_array['other_ticket_type'] = 'Ticket type not specified.'; + } + + // add errors to list + foreach ($errors as $error) { + $error_array[$error->getPropertyPath()] = $error->getMessage(); + } + + // check if any errors were found + if (!empty($error_array)) { + // return validation failure response + return $this->json([ + 'success' => false, + 'errors' => $error_array + ], 422); + } else { + // validated! save the entity + $em->flush(); + + // return successful response + return $this->json([ + 'success' => 'Changes have been saved!' + ]); + } + } + + public function destroy($id) + { + $this->denyAccessUnlessGranted('ticket.delete', null, 'No access.'); + + $params = $this->initParameters('ticket_list'); + + // get row data + $em = $this->getDoctrine()->getManager(); + $obj = $em->getRepository(Ticket::class)->find($id); + + if (empty($obj)) + throw $this->createNotFoundException('The item does not exist'); + + // delete this row + $em->remove($obj); + $em->flush(); + + // response + $response = new Response(); + $response->setStatusCode(Response::HTTP_OK); + $response->send(); + } + + // check if datatable filter is present and append to query + protected function setQueryFilters($datatable, &$query, $qb) { + if (isset($datatable['query']['data-rows-search']) && !empty($datatable['query']['data-rows-search'])) { + $query->where($qb->expr()->concat('u.first_name', $qb->expr()->literal(' '), 'u.last_name') . ' LIKE :filter') + ->orWhere('q.status LIKE :filter') + ->orWhere('q.ticket_type LIKE :filter') + ->orWhere('q.other_ticket_type LIKE :filter') + ->orWhere('q.first_name LIKE :filter') + ->orWhere('q.last_name LIKE :filter') + ->orWhere('q.contact_num LIKE :filter') + ->setParameter('filter', '%' . $datatable['query']['data-rows-search'] . '%'); + } + } +} diff --git a/src/Entity/Ticket.php b/src/Entity/Ticket.php index bdd070d0..0f8d448c 100644 --- a/src/Entity/Ticket.php +++ b/src/Entity/Ticket.php @@ -2,10 +2,14 @@ namespace App\Entity; +use App\Ramcar\TicketType; +use App\Ramcar\TicketStatus; use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\ArrayCollection; use Symfony\Component\Validator\Constraints as Assert; +use DateTime; + /** * @ORM\Entity * @ORM\Table(name="ticket") @@ -29,36 +33,41 @@ class Ticket // status of the ticket /** * @ORM\Column(type="string", length=15) + * @Assert\NotBlank() */ protected $status; // ticket type /** * @ORM\Column(type="string", length=15) + * @Assert\NotBlank() */ - protected $type; + protected $ticket_type; // user defined ticket type /** * @ORM\Column(type="string", length=80, nullable=true) */ - protected $other_type; + protected $other_ticket_type; // first name of ticket owner /** - * @ORM\Column(type="string", length=80, nullable=true) + * @ORM\Column(type="string", length=80) + * @Assert\NotBlank() */ protected $first_name; // last name of ticket owner /** - * @ORM\Column(type="string", length=80, nullable=true) + * @ORM\Column(type="string", length=80) + * @Assert\NotBlank() */ protected $last_name; // contact number of ticket owner /** - * @ORM\Column(type="string", length=20, nullable=true) + * @ORM\Column(type="string", length=20) + * @Assert\NotBlank() */ protected $contact_num; @@ -79,7 +88,6 @@ class Ticket /** * @ORM\ManyToOne(targetEntity="Customer", inversedBy="tickets") * @ORM\JoinColumn(name="customer_id", referencedColumnName="id", nullable=true) - * Assert\NotBlank() */ protected $customer; @@ -115,26 +123,42 @@ class Ticket return $this->status; } - public function setType($type) + public function getStatusText() { - $this->type = $type; + $statuses = TicketStatus::getCollection(); + return $statuses[$this->status] ?? ""; + } + + public function setTicketType($ticket_type) + { + $this->ticket_type = $ticket_type; return $this; } - public function getType() + public function getTicketType() { - return $this->type; + return $this->ticket_type; } - public function setOtherType($other_type) + public function setOtherTicketType($other_ticket_type) { - $this->other_type = $other_type; + $this->other_ticket_type = $other_ticket_type; return $this; } - public function getOtherType() + public function getOtherTicketType() { - return $this->other_type; + return $this->other_ticket_type; + } + + public function getTicketTypeText() + { + if ($this->ticket_type == TicketType::OTHER) { + return $this->other_ticket_type; + } else { + $types = TicketType::getCollection(); + return $types[$this->ticket_type] ?? ""; + } } public function setFirstName($first_name) diff --git a/src/Ramcar/TicketStatus.php b/src/Ramcar/TicketStatus.php new file mode 100644 index 00000000..9a129b5c --- /dev/null +++ b/src/Ramcar/TicketStatus.php @@ -0,0 +1,31 @@ + 'Filed', + 'acknowledged' => 'Acknowledged', + 'closed' => 'Closed', + ]; + + + static public function getCollection() + { + return self::COLLECTION; + } + + static public function validate($type_text) + { + if (isset(self::COLLECTION[$type_text])) + return true; + + return false; + } + +} diff --git a/src/Ramcar/TicketType.php b/src/Ramcar/TicketType.php new file mode 100644 index 00000000..a49916ce --- /dev/null +++ b/src/Ramcar/TicketType.php @@ -0,0 +1,35 @@ + 'Complaint', + 'question' => 'Question', + 'orders' => 'Orders', + 'billing' => 'Billing', + 'other' => 'Other', + ]; + + + static public function getCollection() + { + return self::COLLECTION; + } + + static public function validate($type_text) + { + if (isset(self::COLLECTION[$type_text])) + return true; + + return false; + } + +} diff --git a/templates/customer/form.html.twig b/templates/customer/form.html.twig index 15616462..6fd8c5a4 100644 --- a/templates/customer/form.html.twig +++ b/templates/customer/form.html.twig @@ -44,6 +44,11 @@ + {% if mode == 'update' %} + + {% endif %}
@@ -110,12 +115,26 @@
+ {% if mode == 'update' %} +
+
+
+
+
+
+
+ {% endif %}
-
+
+ {% if mode == 'update' %} + Create Ticket + {% endif %} +
+
Cancel
@@ -473,6 +492,7 @@ var numberIds = []; var mfgVehicles = []; var vehicleRows = []; + var ticketRows = []; {% for number in obj.getMobileNumbers() %} nrow = { @@ -513,6 +533,18 @@ vehicleRows.push(vrow); {% endfor %} + {% for ticket in obj.getTickets %} + trow = { + id: "{{ ticket.getID }}", + date_create: "{{ ticket.getDateCreate|date('d M Y - h:i A') }}", + ticket_type: "{{ ticket.getTicketTypeText }}", + status: "{{ ticket.getStatusText }}", + edit_url: "{{ url('ticket_update', {'id': ticket.getID}) }}" + }; + + ticketRows.push(trow); + {% endfor %} + // add a mobile number to the table $("#btn-add-mobile-number").click(function() { var id = $("#mobile-number").val(); @@ -875,6 +907,7 @@ { field: 'date_registered', title: 'Date Registered', + width: 200, template: function (row, index, datatable) { return row.date_registered + ''; } @@ -882,6 +915,7 @@ { field: 'date_confirmed', title: 'Date Confirmed', + width: 200, template: function (row, index, datatable) { return row.date_confirmed + ''; } @@ -1047,6 +1081,54 @@ }; var vehicleTable = $("#data-vehicles").mDatatable(vehicleOptions); + + // tickets data table + var ticketOptions = { + data: { + type: 'local', + source: ticketRows, + saveState: { + cookie: false, + webstorage: false + } + }, + layout: { + scroll: true + }, + columns: [ + { + field: 'id', + title: 'ID', + width: 30 + }, + { + field: 'date_create', + title: 'Date Created', + width: 200 + }, + { + field: 'ticket_type', + title: 'Ticket Type' + }, + { + field: 'status', + title: 'Status' + }, + { + field: 'Actions', + width: 70, + title: 'Actions', + sortable: false, + overflow: 'visible', + template: function (row, index, datatable) { + return ''; + }, + } + ], + pagination: false + }; + + var numberTable = $("#data-tickets").mDatatable(ticketOptions); }); {% endblock %} diff --git a/templates/ticket/form.html.twig b/templates/ticket/form.html.twig new file mode 100644 index 00000000..c452d9dc --- /dev/null +++ b/templates/ticket/form.html.twig @@ -0,0 +1,188 @@ +{% extends 'base.html.twig' %} + +{% block body %} + +
+
+
+

Tickets

+
+
+
+ +
+ +
+
+
+
+
+
+ + + +

+ {% if mode == 'update' %} + Edit Ticket + {{ obj.getID }} + {% else %} + New Ticket + {% if customer %} + for {{ customer.getFirstName ~ " " ~ customer.getLastName }} + {% endif %} + {% endif %} +

+
+
+
+
+
+
+
+ + + +
+
+ + + +
+
+ + + +
+
+
+
+ + + +
+
+ + + +
+
+ + + +
+
+
+
+ + + +
+
+
+
+
+
+
+ + Cancel +
+
+
+
+
+
+
+
+
+{% endblock %} + +{% block scripts %} + +{% endblock %} diff --git a/templates/ticket/list.html.twig b/templates/ticket/list.html.twig new file mode 100644 index 00000000..df1f952b --- /dev/null +++ b/templates/ticket/list.html.twig @@ -0,0 +1,173 @@ +{% extends 'base.html.twig' %} + +{% block body %} + +
+
+
+

+ Tickets +

+
+
+
+ +
+ +
+
+
+
+
+
+
+
+
+
+ + + + +
+
+
+
+ +
+
+ +
+ +
+
+
+
+
+{% endblock %} + +{% block scripts %} + +{% endblock %}