diff --git a/config/acl.yaml b/config/acl.yaml index b37e7580..c14b2d30 100644 --- a/config/acl.yaml +++ b/config/acl.yaml @@ -208,3 +208,17 @@ access_keys: label: Update - id: ticket.delete label: Delete + + - id: promo + label: Promo Access + acls: + - id: promo.menu + label: Menu + - id: promo.list + label: List + - id: promo.add + label: Add + - id: promo.update + label: Update + - id: promo.delete + label: Delete diff --git a/config/menu.yaml b/config/menu.yaml index 4828b709..e54d9703 100644 --- a/config/menu.yaml +++ b/config/menu.yaml @@ -28,6 +28,10 @@ main_menu: acl: rider.list label: Riders parent: database + - id: promo_list + acl: promo.list + label: Promos + parent: database - id: battery acl: battery.menu diff --git a/config/routes/promo.yaml b/config/routes/promo.yaml new file mode 100644 index 00000000..0a52b82d --- /dev/null +++ b/config/routes/promo.yaml @@ -0,0 +1,36 @@ +# promo + +promo_list: + path: /promos + controller: App\Controller\PromoController::index + +promo_rows: + path: /promos/rows + controller: App\Controller\PromoController::rows + methods: [POST] + +promo_create: + path: /promos/create + controller: App\Controller\PromoController::addForm + methods: [GET] + +promo_create_submit: + path: /promos/create + controller: App\Controller\PromoController::addSubmit + methods: [POST] + +promo_update: + path: /promos/{id} + controller: App\Controller\PromoController::updateForm + methods: [GET] + +promo_update_submit: + path: /promos/{id} + controller: App\Controller\PromoController::updateSubmit + methods: [POST] + +promo_delete: + path: /promos/{id} + controller: App\Controller\PromoController::destroy + methods: [DELETE] + diff --git a/src/Controller/PromoController.php b/src/Controller/PromoController.php new file mode 100644 index 00000000..f747ceb9 --- /dev/null +++ b/src/Controller/PromoController.php @@ -0,0 +1,273 @@ +denyAccessUnlessGranted('promo.list', null, 'No access.'); + + $params = $this->initParameters('promo_list'); + + return $this->render('promo/list.html.twig', $params); + } + + public function rows(Request $req) + { + $this->denyAccessUnlessGranted('promo.list', null, 'No access.'); + + // get query builder + $qb = $this->getDoctrine() + ->getRepository(Promo::class) + ->createQueryBuilder('q'); + + // get datatable params + $datatable = $req->request->get('datatable'); + + // count total records + $tquery = $qb->select('COUNT(q)'); + $this->setQueryFilters($datatable, $tquery); + $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'); + $this->setQueryFilters($datatable, $query); + + // check if sorting is present, otherwise use default + if (isset($datatable['sort']['field']) && !empty($datatable['sort']['field'])) { + $order = $datatable['sort']['sort'] ?? 'asc'; + $query->orderBy('q.' . $datatable['sort']['field'], $order); + } else { + $query->orderBy('q.id', 'asc'); + } + + // get rows for this page + $obj_rows = $query->setFirstResult($offset) + ->setMaxResults($perpage) + ->getQuery() + ->getResult(); + + $discount_apply = DiscountApply::getCollection(); + + // process rows + $rows = []; + foreach ($obj_rows as $orow) { + // add row data + $row['id'] = $orow->getID(); + $row['name'] = $orow->getName(); + $row['code'] = $orow->getCode(); + $row['discount_rate'] = ($orow->getDiscountRate() * 100) . '%'; + $row['discount_apply'] = $discount_apply[$orow->getDiscountApply()]; + + // add row metadata + $row['meta'] = [ + 'update_url' => '', + 'delete_url' => '' + ]; + + // add crud urls + if ($this->isGranted('promo.update')) + $row['meta']['update_url'] = $this->generateUrl('promo_update', ['id' => $row['id']]); + if ($this->isGranted('promo.delete')) + $row['meta']['delete_url'] = $this->generateUrl('promo_delete', ['id' => $row['id']]); + + $rows[] = $row; + } + + // response + return $this->json([ + 'meta' => $meta, + 'data' => $rows + ]); + } + + protected function setObject(Promo $obj, Request $req) + { + // set and save values + $obj->setName($req->request->get('name')) + ->setCode($req->request->get('code')) + ->setDiscountRate($req->request->get('discount_rate')) + ->setDiscountApply($req->request->get('discount_apply')); + } + + protected function setQueryFilters($datatable, QueryBuilder $query) + { + if (isset($datatable['query']['data-rows-search']) && !empty($datatable['query']['data-rows-search'])) { + $query->where('q.name LIKE :filter') + ->orWhere('q.code LIKE :filter') + ->orWhere('q.discount_rate LIKE :filter') + ->orWhere('q.discount_apply LIKE :filter') + ->setParameter('filter', '%' . $datatable['query']['data-rows-search'] . '%'); + } + } + + public function addForm() + { + $this->denyAccessUnlessGranted('promo.add', null, 'No access.'); + + $params = $this->initParameters('promo_list'); + $params['obj'] = new Promo(); + $params['mode'] = 'create'; + $params['discount_apply'] = DiscountApply::getCollection(); + + // response + return $this->render('promo/form.html.twig', $params); + } + + public function addSubmit(Request $req, ValidatorInterface $validator) + { + $this->denyAccessUnlessGranted('promo.add', null, 'No access.'); + + // create new object + $em = $this->getDoctrine()->getManager(); + $obj = new Promo(); + + $this->setObject($obj, $req); + + // validate + $errors = $validator->validate($obj); + + // initialize error list + $error_array = []; + + // 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); + } + + // 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('promo.update', null, 'No access.'); + + $params = $this->initParameters('promo_list'); + + // get row data + $em = $this->getDoctrine()->getManager(); + $obj = $em->getRepository(Promo::class)->find($id); + + // make sure this row exists + if (empty($obj)) + throw $this->createNotFoundException('The item does not exist'); + + $params['obj'] = $obj; + $params['mode'] = 'update'; + $params['discount_apply'] = DiscountApply::getCollection(); + + // response + return $this->render('promo/form.html.twig', $params); + } + + public function updateSubmit(Request $req, ValidatorInterface $validator, $id) + { + $this->denyAccessUnlessGranted('promo.update', null, 'No access.'); + + // get object data + $em = $this->getDoctrine()->getManager(); + $obj = $em->getRepository(Promo::class)->find($id); + + // make sure this object exists + if (empty($obj)) + throw $this->createNotFoundException('The item does not exist'); + + $this->setObject($obj, $req); + + // validate + $errors = $validator->validate($obj); + + // initialize error list + $error_array = []; + + // 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); + } + + // validated! save the entity + $em->flush(); + + // return successful response + return $this->json([ + 'success' => 'Changes have been saved!' + ]); + } + + public function destroy($id) + { + $this->denyAccessUnlessGranted('promo.delete', null, 'No access.'); + + $params = $this->initParameters('promo_list'); + + // get objext data + $em = $this->getDoctrine()->getManager(); + $obj = $em->getRepository(Promo::class)->find($id); + + if (empty($obj)) + throw $this->createNotFoundException('The item does not exist'); + + // delete this object + $em->remove($obj); + $em->flush(); + + // response + $response = new Response(); + $response->setStatusCode(Response::HTTP_OK); + $response->send(); + } +} diff --git a/src/Entity/Promo.php b/src/Entity/Promo.php new file mode 100644 index 00000000..9f0c4efc --- /dev/null +++ b/src/Entity/Promo.php @@ -0,0 +1,102 @@ +id; + } + + public function setName($name) + { + $this->name = $name; + return $this; + } + + public function getName() + { + return $this->name; + } + + public function setCode($code) + { + $this->code = $code; + return $this; + } + + public function getCode() + { + return $this->code; + } + + public function setDiscountRate($discount_rate) + { + $this->discount_rate = $discount_rate; + return $this; + } + + public function getDiscountRate() + { + return $this->discount_rate; + } + + public function setDiscountApply($discount_apply) + { + $this->discount_apply = $discount_apply; + return $this; + } + + public function getDiscountApply() + { + return $this->discount_apply; + } +} diff --git a/src/Ramcar/DiscountApply.php b/src/Ramcar/DiscountApply.php new file mode 100644 index 00000000..ec20d065 --- /dev/null +++ b/src/Ramcar/DiscountApply.php @@ -0,0 +1,14 @@ + 'SRP', + 'opl' => 'OPL', + ]; +} diff --git a/templates/promo/form.html.twig b/templates/promo/form.html.twig new file mode 100644 index 00000000..8a1d3f87 --- /dev/null +++ b/templates/promo/form.html.twig @@ -0,0 +1,160 @@ +{% extends 'base.html.twig' %} + +{% block body %} + +
+
+
+

Promos

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

+ {% if mode == 'update' %} + Edit Promo + {{ obj.getName }} + {% else %} + New Promo + {% endif %} +

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

+ Promos +

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