diff --git a/src/Controller/TicketController.php b/src/Controller/TicketController.php index 0f9f3cd9..7c18ee6f 100644 --- a/src/Controller/TicketController.php +++ b/src/Controller/TicketController.php @@ -9,8 +9,11 @@ use App\Ramcar\SourceOfAwareness; use App\Entity\Ticket; use App\Entity\Customer; use App\Entity\JobOrder; +use App\Entity\TicketType as NewTicketType; +use App\Entity\SubTicketType; use Doctrine\ORM\Query; +use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Validator\Validator\ValidatorInterface; @@ -128,7 +131,7 @@ class TicketController extends Controller /** * @Menu(selected="ticket_list") */ - public function addForm(Request $req, $customer_id, $job_order_id) + public function addForm(Request $req, EntityManagerInterface $em, $customer_id, $job_order_id) { $this->denyAccessUnlessGranted('ticket.add', null, 'No access.'); @@ -140,7 +143,6 @@ class TicketController extends Controller // get customer data if ($customer_id) { - $em = $this->getDoctrine()->getManager(); $customer = $em->getRepository(Customer::class)->find($customer_id); // make sure this row exists @@ -176,6 +178,8 @@ class TicketController extends Controller $params['redirect_url'] = $this->generateUrl('ticket_list'); $params['soa_types'] = SourceOfAwareness::getCollection(); + $params['sets'] = $this->generateFormSets($em); + // set redirect url if ($customer) { @@ -236,12 +240,16 @@ class TicketController extends Controller $obj = new Ticket(); // get ticket type - $ticket_type = $req->request->get('ticket_type'); - $other_ticket_type = ''; + //$ticket_type = $req->request->get('ticket_type'); + //$other_ticket_type = ''; - if ($ticket_type == TicketType::OTHER) { - $other_ticket_type = $req->request->get('other_ticket_type'); - } + //if ($ticket_type == TicketType::OTHER) { + // $other_ticket_type = $req->request->get('other_ticket_type'); + //} + + // get the "new" ticket type + $ttype_id = $req->request->get('new_ticket_type'); + $sub_ttype_id = $req->request->get('sub_ticket_type'); // get source of awareness if any $soa_type = $req->request->get('source_of_awareness'); @@ -254,8 +262,8 @@ class TicketController extends Controller ->setLastName($last_name) ->setContactNumber($contact_num) ->setStatus($req->request->get('status')) - ->setTicketType($ticket_type) - ->setOtherTicketType($other_ticket_type) + //->setTicketType($ticket_type) + //->setOtherTicketType($other_ticket_type) ->setDetails($req->request->get('details')) ->setPlateNumber($req->request->get('plate_number')) ->setDateCreate(new DateTime()) @@ -280,9 +288,21 @@ class TicketController extends Controller $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.'; - } + //if ($ticket_type == TicketType::OTHER && empty($other_ticket_type)) { + // $error_array['other_ticket_type'] = 'Ticket type not specified.'; + //} + // get ticket and sub ticket type objects + $ttype = $em->getRepository(NewTicketType::class)->find($ttype_id); + if (empty($ttype)) + $error_array['new_ticket_type'] = 'Ticket type not specified.'; + + $sub_ttype = $em->getRepository(SubTicketType::class)->find($sub_ttype_id); + if (empty($sub_ttype)) + $error_array['sub_ticket_type'] = 'Sub ticket type not specified.'; + + // set the ticket type and sub ticket type for ticket + $obj->setNewTicketType($ttype) + ->setSubTicketType($sub_ttype); // add errors to list foreach ($errors as $error) { @@ -311,22 +331,19 @@ class TicketController extends Controller /** * @Menu(selected="ticket_list") */ - public function updateForm(Request $req, $id) + public function updateForm(Request $req, $id, EntityManagerInterface $em) { $this->denyAccessUnlessGranted('ticket.update', null, 'No access.'); $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(); - $customer = $obj->getCustomer(); $job_order = $obj->getJobOrder(); @@ -339,6 +356,8 @@ class TicketController extends Controller $params['redirect_url'] = $this->generateUrl('ticket_list'); $params['soa_types'] = SourceOfAwareness::getCollection(); + $params['sets'] = $this->generateFormSets($em); + // set redirect url if ($customer) { @@ -428,12 +447,16 @@ class TicketController extends Controller } // get ticket type - $ticket_type = $req->request->get('ticket_type'); - $other_ticket_type = ''; + //$ticket_type = $req->request->get('ticket_type'); + //$other_ticket_type = ''; - if ($ticket_type == TicketType::OTHER) { - $other_ticket_type = $req->request->get('other_ticket_type'); - } + //if ($ticket_type == TicketType::OTHER) { + // $other_ticket_type = $req->request->get('other_ticket_type'); + //} + + // get the "new" ticket type + $ttype_id = $req->request->get('new_ticket_type'); + $sub_ttype_id = $req->request->get('sub_ticket_type'); // get source of awareness if any $soa_type = $req->request->get('source_of_awareness'); @@ -446,8 +469,8 @@ class TicketController extends Controller ->setLastName($last_name) ->setContactNumber($contact_num) ->setStatus($req->request->get('status')) - ->setTicketType($ticket_type) - ->setOtherTicketType($other_ticket_type) + //->setTicketType($ticket_type) + //->setOtherTicketType($other_ticket_type) ->setDetails($req->request->get('details')) ->setPlateNumber($req->request->get('plate_number')) ->setSourceOfAwareness($soa_type) @@ -460,9 +483,21 @@ class TicketController extends Controller $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.'; - } + //if ($ticket_type == TicketType::OTHER && empty($other_ticket_type)) { + // $error_array['other_ticket_type'] = 'Ticket type not specified.'; + //} + // get ticket and sub ticket type objects + $ttype = $em->getRepository(NewTicketType::class)->find($ttype_id); + if (empty($ttype)) + $error_array['new_ticket_type'] = 'Ticket type not specified.'; + + $sub_ttype = $em->getRepository(SubTicketType::class)->find($sub_ttype_id); + if (empty($sub_ttype)) + $error_array['sub_ticket_type'] = 'Sub ticket type not specified.'; + + // set the ticket type and sub ticket type for ticket + $obj->setNewTicketType($ttype) + ->setSubTicketType($sub_ttype); // add errors to list foreach ($errors as $error) { @@ -508,6 +543,35 @@ class TicketController extends Controller $response->send(); } + protected function generateFormSets(EntityManagerInterface $em) + { + // get ticket types + $ttypes = $em->getRepository(NewTicketType::class)->findBy([], ['name' => 'asc']); + $ttypes_set = []; + foreach ($ttypes as $ttype) + { + $ttypes_set[$ttype->getID()] = $ttype->getName(); + } + + // get sub ticket types + $sub_ttypes = $em->getRepository(SubTicketType::class)->findBy([], ['name' => 'asc']); + $sub_ttypes_set = []; + foreach ($sub_ttypes as $sub_ttype) + { + $sub_ttypes_set[] = [ + 'id' => $sub_ttype->getID(), + 'name' => $sub_ttype->getName(), + 'ticket_type_id' => $sub_ttype->getTicketType()->getID(), + ]; + } + + return [ + 'new_ticket_types' => $ttypes_set, + 'sub_ticket_types' => $sub_ttypes_set, + ]; + } + + // 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'])) { diff --git a/src/Entity/Ticket.php b/src/Entity/Ticket.php index a3218c47..c8efee8f 100644 --- a/src/Entity/Ticket.php +++ b/src/Entity/Ticket.php @@ -2,7 +2,7 @@ namespace App\Entity; -use App\Ramcar\TicketType; +use App\Ramcar\TicketType as LegacyTicketType; use App\Ramcar\TicketStatus; use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\ArrayCollection; @@ -44,8 +44,7 @@ class Ticket // ticket type /** - * @ORM\Column(type="string", length=15) - * @Assert\NotBlank() + * @ORM\Column(type="string", length=15, nullable=true) */ protected $ticket_type; @@ -120,6 +119,20 @@ class Ticket */ protected $remarks; + // new ticket type + /** + * @ORM\ManyToOne(targetEntity="TicketType", inversedBy="tickets") + * @ORM\JoinColumn(name="ticket_type_id", referencedColumnName="id", nullable=true) + */ + protected $new_ticket_type; + + // sub ticket type + /** + * @ORM\ManyToOne(targetEntity="SubTicketType", inversedBy="subtickets") + * @ORM\JoinColumn(name="subticket_type_id", referencedColumnName="id", nullable=true) + */ + protected $subticket_type; + public function __construct() { $this->date_create = new DateTime(); @@ -182,10 +195,10 @@ class Ticket public function getTicketTypeText() { - if ($this->ticket_type == TicketType::OTHER) { + if ($this->ticket_type == LegacyTicketType::OTHER) { return $this->other_ticket_type; } else { - $types = TicketType::getCollection(); + $types = LegacyTicketType::getCollection(); return $types[$this->ticket_type] ?? ""; } } @@ -300,4 +313,25 @@ class Ticket return $this->remarks; } + public function setNewTicketType(TicketType $new_ticket_type = null) + { + $this->new_ticket_type = $new_ticket_type; + return $this; + } + + public function getNewTicketType() + { + return $this->new_ticket_type; + } + + public function setSubTicketType(SubTicketType $subticket_type = null) + { + $this->subticket_type = $subticket_type; + return $this; + } + + public function getSubTicketType() + { + return $this->subticket_type; + } } diff --git a/templates/ticket/form.html.twig b/templates/ticket/form.html.twig index 2196e790..da0bca86 100644 --- a/templates/ticket/form.html.twig +++ b/templates/ticket/form.html.twig @@ -1,321 +1,378 @@ {% extends 'base.html.twig' %} {% block body %} - -