diff --git a/config/acl.yaml b/config/acl.yaml index e694340c..0f858fa9 100644 --- a/config/acl.yaml +++ b/config/acl.yaml @@ -488,3 +488,17 @@ access_keys: label: Update - id: sap_csize.delete label: Delete + + - id: customer_tag + label: Customer Tags Access + acls: + - id: customer_tag.menu + label: Menu + - id: customer_tag.list + label: List + - id: customer_tag.add + label: Add + - id: customer_tag.update + label: Update + - id: customer_tag.delete + label: Delete diff --git a/config/routes/customer_tag.yaml b/config/routes/customer_tag.yaml new file mode 100644 index 00000000..96bc50ef --- /dev/null +++ b/config/routes/customer_tag.yaml @@ -0,0 +1,33 @@ +customertag_list: + path: /customer_tags + controller: App\Controller\CustomerTagController::index + +customertag_rows: + path: /customer_tags/rows + controller: App\Controller\CustomerTagController::rows + methods: [POST] + +customertag_create: + path: /customer_tags/create + controller: App\Controller\CustomerTagController::addForm + methods: [GET] + +customertag_create_submit: + path: /customer_tags/create + controller: App\Controller\CustomerTagController::addSubmit + methods: [POST] + +customertag_update: + path: /customer_tags/{id} + controller: App\Controller\CustomerTagController::updateForm + methods: [GET] + +customertag_update_submit: + path: /customer_tags/{id} + controller: App\Controller\CustomerTagController::updateSubmit + methods: [POST] + +customertag_delete: + path: /customer_tags/{id} + controller: App\Controller\CustomerTagController::destroy + methods: [DELETE] diff --git a/config/services.yaml b/config/services.yaml index 2110d8a4..fb4870ba 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -270,3 +270,8 @@ services: App\Service\WarrantyAPILogger: arguments: $em: "@doctrine.orm.entity_manager" + + # promo logger + App\Service\PromoLogger: + arguments: + $em: "@doctrine.orm.entity_manager" diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index 870eac05..409f36b8 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -39,6 +39,7 @@ use App\Service\MapTools; use App\Service\InventoryManager; use App\Service\RiderAssignmentHandlerInterface; use App\Service\WarrantyAPILogger; +use App\Service\PromoLogger; use App\Entity\MobileSession; use App\Entity\Customer; @@ -846,7 +847,7 @@ class APIController extends Controller implements LoggedController public function requestJobOrder(Request $req, InvoiceGeneratorInterface $ic, GeofenceTracker $geo, MapTools $map_tools, InventoryManager $im, MQTTClient $mclient, - RiderAssignmentHandlerInterface $rah) + RiderAssignmentHandlerInterface $rah, PromoLogger $promo_logger) { // check required parameters and api key $required_params = [ @@ -1122,6 +1123,37 @@ class APIController extends Controller implements LoggedController 'invoice' => $invoice_data ]; + // check service type + if ($jo->getServiceType() == ServiceType::BATTERY_REPLACEMENT_NEW) + { + $customer = $cv->getCustomer(); + $customer_tags = $customer->getCustomerTagObjects(); + if (!empty($customer_tags)) + { + foreach ($customer_tags as $customer_tag) + { + // TODO: not too comfy with this being hardcoded + if ($customer_tag->getID() == 'CAR_CLUB_PROMO') + { + // remove associated entity + $customer->removeCustomerTag($customer_tag); + + // log the availment of promo from customer + $created_by = $req->query->get('api_key');; + $cust_id = $jo->getCustomer()->getID(); + $cust_fname = $jo->getCustomer()->getFirstName(); + $cust_lname = $jo->getCustomer()->getLastName(); + $jo_id = $jo->getID(); + $invoice_id = $jo->getInvoice()->getID(); + // TODO: check if we store total price of invoice or just the discounted amount + $amount = $jo->getInvoice()->getTotalPrice(); + $this->promo_logger->logPromoInfo($created_by, $cust_id, $cust_fname, $cust_lname, $jo_id, + $invoice_id, $amount); + } + } + } + } + // set data $res->setData($data); @@ -2332,7 +2364,7 @@ class APIController extends Controller implements LoggedController public function newRequestJobOrder(Request $req, InvoiceGeneratorInterface $ic, GeofenceTracker $geo, MapTools $map_tools, InventoryManager $im, MQTTClient $mclient, - RiderAssignmentHandlerInterface $rah) + RiderAssignmentHandlerInterface $rah, PromoLogger $promo_logger) { // check required parameters and api key $required_params = [ @@ -2668,6 +2700,38 @@ class APIController extends Controller implements LoggedController 'invoice' => $invoice_data ]; + // need to check for customer tag/promo + // check service type + if ($jo->getServiceType() == ServiceType::BATTERY_REPLACEMENT_NEW) + { + $customer = $cv->getCustomer(); + $customer_tags = $customer->getCustomerTagObjects(); + if (!empty($customer_tags)) + { + foreach ($customer_tags as $customer_tag) + { + // TODO: not too comfy with this being hardcoded + if ($customer_tag->getID() == 'CAR_CLUB_PROMO') + { + // remove associated entity + $customer->removeCustomerTag($customer_tag); + + // log the availment of promo from customer + $created_by = $req->query->get('api_key');; + $cust_id = $jo->getCustomer()->getID(); + $cust_fname = $jo->getCustomer()->getFirstName(); + $cust_lname = $jo->getCustomer()->getLastName(); + $jo_id = $jo->getID(); + $invoice_id = $jo->getInvoice()->getID(); + // TODO: check if we store total price of invoice or just the discounted amount + $amount = $jo->getInvoice()->getTotalPrice(); + $promo_logger->logPromoInfo($created_by, $cust_id, $cust_fname, $cust_lname, $jo_id, + $invoice_id, $amount); + } + } + } + } + // set data $res->setData($data); diff --git a/src/Controller/CustomerTagController.php b/src/Controller/CustomerTagController.php new file mode 100644 index 00000000..3302bfd2 --- /dev/null +++ b/src/Controller/CustomerTagController.php @@ -0,0 +1,267 @@ +denyAccessUnlessGranted('customer_tag.list', null, 'No access.'); + + return $this->render('customer-tag/list.html.twig'); + } + + public function rows(Request $req) + { + $this->denyAccessUnlessGranted('customer_tag.list', null, 'No access.'); + + // get query builder + $qb = $this->getDoctrine() + ->getRepository(CustomerTag::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(); + + // process rows + $rows = []; + foreach ($obj_rows as $orow) { + // add row data + $row['id'] = $orow->getID(); + $row['name'] = $orow->getName(); + + // add row metadata + $row['meta'] = [ + 'update_url' => '', + 'delete_url' => '' + ]; + + // add crud urls + if ($this->isGranted('customer_tag.update')) + $row['meta']['update_url'] = $this->generateUrl('customertag_update', ['id' => $row['id']]); + if ($this->isGranted('customer_tag.delete')) + $row['meta']['delete_url'] = $this->generateUrl('customertag_delete', ['id' => $row['id']]); + + $rows[] = $row; + } + + // response + return $this->json([ + 'meta' => $meta, + 'data' => $rows + ]); + } + + 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') + ->setParameter('filter', '%' . $datatable['query']['data-rows-search'] . '%'); + } + } + + /** + * @Menu(selected="customer_tag_list") + */ + public function addForm() + { + $this->denyAccessUnlessGranted('customer_tag.add', null, 'No access.'); + + $params['obj'] = new CustomerTag(); + $params['mode'] = 'create'; + + // response + return $this->render('customer-tag/form.html.twig', $params); + } + + public function addSubmit(Request $req, ValidatorInterface $validator) + { + $this->denyAccessUnlessGranted('customer_tag.add', null, 'No access.'); + + // create new object + $em = $this->getDoctrine()->getManager(); + $obj = new CustomerTag(); + + $tag_details = $req->request->get('tag_details'); + $tag_details_json = json_decode($tag_details, true); + $obj->setID($req->request->get('id')) + ->setName($req->request->get('name')) + ->setTagDetails($tag_details_json); + + // 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); + } else { + // validated! save the entity + $em->persist($obj); + $em->flush(); + + // return successful response + return $this->json([ + 'success' => 'Changes have been saved!' + ]); + } + } + + /** + * @Menu(selected="customer_tag_list") + */ + public function updateForm($id) + { + $this->denyAccessUnlessGranted('customer_tag.update', null, 'No access.'); + + // get row data + $em = $this->getDoctrine()->getManager(); + $obj = $em->getRepository(CustomerTag::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'; + + // response + return $this->render('customer-tag/form.html.twig', $params); + } + + public function updateSubmit(Request $req, ValidatorInterface $validator, $id) + { + $this->denyAccessUnlessGranted('customer_tag.update', null, 'No access.'); + + // get object data + $em = $this->getDoctrine()->getManager(); + $obj = $em->getRepository(CustomerTag::class)->find($id); + + // make sure this object exists + if (empty($obj)) + throw $this->createNotFoundException('The item does not exist'); + + $tag_details = $req->request->get('tag_details'); + $tag_details_json = json_decode($tag_details, true); + $obj->setID($req->request->get('id')) + ->setName($req->request->get('name')) + ->setTagDetails($tag_details_json); + + // 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('customer_tag.delete', null, 'No access.'); + + // get object data + $em = $this->getDoctrine()->getManager(); + $obj = $em->getRepository(CustomerTag::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/Customer.php b/src/Entity/Customer.php index f5e8e4e9..2f593e00 100644 --- a/src/Entity/Customer.php +++ b/src/Entity/Customer.php @@ -208,12 +208,20 @@ class Customer */ protected $create_source; + // customer tags + /** + * @ORM\ManyToMany(targetEntity="CustomerTag", inversedBy="customers") + * @ORM\JoinTable(name="customer_customer_tags") + */ + protected $customer_tags; + public function __construct() { $this->numbers = new ArrayCollection(); $this->sessions = new ArrayCollection(); $this->vehicles = new ArrayCollection(); $this->job_orders = new ArrayCollection(); + $this->customer_tags = new ArrayCollection(); $this->customer_classification = CustomerClassification::REGULAR; $this->customer_notes = ''; @@ -608,4 +616,36 @@ class Customer { return $this->create_source; } + + public function addCustomerTag(CustomerTag $customer_tag) + { + $this->customer_tags[$customer_tag->getID()] = $customer_tag; + return $this; + } + + public function clearCustomerTags() + { + $this->customer_tags->clear(); + return $this; + } + + public function getCustomerTags() + { + $str_cust_tags = []; + foreach ($this->customer_tags as $cust_tag) + $str_cust_tags[] = $cust_tag->getID(); + + return $str_cust_tags; + } + + public function getCustomerTagObjects() + { + return $this->customer_tags; + } + + public function removeCustomerTag(CustomerTag $customer_tag) + { + $this->customer_tags->removeElement($customer_tag); + $customer_tag->removeCustomer($this); + } } diff --git a/src/Entity/CustomerTag.php b/src/Entity/CustomerTag.php new file mode 100644 index 00000000..4ce840ab --- /dev/null +++ b/src/Entity/CustomerTag.php @@ -0,0 +1,125 @@ +date_create = new DateTime(); + $this->customers = new ArrayCollection(); + $this->tag_details = []; + } + + public function getID() + { + return $this->id; + } + + public function setID($id) + { + $this->id = $id; + return $this; + } + + public function setName($name) + { + $this->name = $name; + return $this; + } + + public function getName() + { + return $this->name; + } + + public function getDateCreate() + { + return $this->date_create; + } + + public function addCustomer(Customer $customer) + { + $this->customers[$customer->getID()] = $customer; + + return $this; + } + + public function clearCustomers() + { + $this->customers->clear(); + return $this; + } + + public function getCustomers() + { + return $this->customers; + } + + public function removeCustomer(Customer $customer) + { + $this->customers->removeElement($customer); + } + + public function addTagDetails($id, $value) + { + $this->tag_details[$id] = $value; + return $this; + } + + public function setTagDetails($tag_details) + { + $this->tag_details = $tag_details; + return $this; + } + + public function getTagDetails($id) + { + // return null if we don't have it + if (!isset($this->tag_details[$id])) + return null; + + return $this->tag_details[$id]; + } + + public function getAllTagDetails() + { + return json_encode($this->tag_details); + } +} + diff --git a/src/Entity/PromoLog.php b/src/Entity/PromoLog.php new file mode 100644 index 00000000..604acbe3 --- /dev/null +++ b/src/Entity/PromoLog.php @@ -0,0 +1,169 @@ +date_create = new DateTime(); + } + + public function getID() + { + return $this->id; + } + + public function setDateCreate(DateTime $date_create) + { + $this->date_create = $date_create; + return $this; + } + + public function getDateCreate() + { + return $this->date_create; + } + + public function setCreatedBy($created_by) + { + $this->created_by = $created_by; + return $this; + } + + public function getCreatedBy() + { + return $this->created_by; + } + + public function setCustId($cust_id) + { + $this->cust_id = $cust_id; + return $this; + } + + public function getCustId() + { + return $this->cust_id; + } + + public function setCustFirstName($cust_first_name) + { + $this->cust_first_name = $cust_first_name; + return $this; + } + + public function getCustFirstName() + { + return $this->cust_first_name; + } + + public function setCustLastName($cust_last_name) + { + $this->cust_last_name = $cust_last_name; + return $this; + } + + public function getCustLastName() + { + return $this->cust_last_name; + } + + public function setJoId($jo_id) + { + $this->jo_id = $jo_id; + return $this; + } + + public function getJoId() + { + return $this->jo_id; + } + + public function setInvoiceId($invoice_id) + { + $this->invoice_id = $invoice_id; + return $this; + } + + public function getInvoiceId() + { + return $this->invoice_id; + } + + public function setAmount($amount) + { + $this->amount = $amount; + return $this; + } + + public function getAmount() + { + return $this->amount; + } +} + diff --git a/src/Service/CustomerHandler/ResqCustomerHandler.php b/src/Service/CustomerHandler/ResqCustomerHandler.php index 76d20054..1554a288 100644 --- a/src/Service/CustomerHandler/ResqCustomerHandler.php +++ b/src/Service/CustomerHandler/ResqCustomerHandler.php @@ -22,6 +22,7 @@ use App\Entity\Vehicle; use App\Entity\Battery; use App\Entity\VehicleManufacturer; use App\Entity\BatteryManufacturer; +use App\Entity\CustomerTag; use DateTime; @@ -151,6 +152,9 @@ class ResqCustomerHandler implements CustomerHandlerInterface $params['obj'] = new Customer(); $params['mode'] = 'create'; + // get customer tags + $params['customer_tags'] = $this->em->getRepository(CustomerTag::class)->findAll(); + // get dropdown parameters $this->fillDropdownParameters($params); @@ -307,6 +311,9 @@ class ResqCustomerHandler implements CustomerHandlerInterface if (empty($row)) throw new NotFoundHttpException('The item does not exist'); + // get customer tags + $params['customer_tags'] = $this->em->getRepository(CustomerTag::class)->findAll(); + // get dropdown parameters $this->fillDropdownParameters($params); @@ -541,6 +548,7 @@ class ResqCustomerHandler implements CustomerHandlerInterface 'flag_promo_email' => $customer->isPromoEmail(), 'flag_research_sms' => $customer->isResearchSms(), 'flag_research_email' => $customer->isResearchEmail(), + 'customer_tags' => $customer->getCustomerTags(), ], 'vehicle' => [ 'id' => $vehicle->getID(), @@ -603,13 +611,26 @@ class ResqCustomerHandler implements CustomerHandlerInterface ->setPromoEmail($req->request->get('flag_promo_email', false)) ->setDpaConsent($is_dpa_checked) ->setResearchSms($req->request->get('flag_research_sms', false)) - ->setResearchEmail($req->request->get('flag_research_email', false)); + ->setResearchEmail($req->request->get('flag_research_email', false)) + ->clearCustomerTags();; // phone numbers $obj->setPhoneMobile($req->request->get('phone_mobile')) ->setPhoneLandline($req->request->get('phone_landline')) ->setPhoneOffice($req->request->get('phone_office')) ->setPhoneFax($req->request->get('phone_fax')); + + // set car club flags + $customer_tags = $req->request->get('customer_tags'); + if (!empty($customer_tags)) + { + foreach($customer_tags as $customer_tag_id) + { + $customer_tag = $this->em->getRepository(CustomerTag::class)->find($customer_tag_id); + if (!empty($customer_tag)) + $obj->addCustomerTag($customer_tag); + } + } } protected function fillDropdownParameters(&$params) diff --git a/src/Service/InvoiceGenerator/ResqInvoiceGenerator.php b/src/Service/InvoiceGenerator/ResqInvoiceGenerator.php index 2999d622..f1be78f1 100644 --- a/src/Service/InvoiceGenerator/ResqInvoiceGenerator.php +++ b/src/Service/InvoiceGenerator/ResqInvoiceGenerator.php @@ -19,6 +19,7 @@ use App\Entity\InvoiceItem; use App\Entity\User; use App\Entity\Battery; use App\Entity\Promo; +use App\Entity\Customer; use App\Service\InvoiceGeneratorInterface; @@ -66,6 +67,15 @@ class ResqInvoiceGenerator implements InvoiceGeneratorInterface $stype = $criteria->getServiceType(); $cv = $criteria->getCustomerVehicle(); $has_coolant = $criteria->hasCoolant(); + + $cust_tag_info = []; + if ($stype == ServiceType::BATTERY_REPLACEMENT_NEW) + { + // check if criteria has entries + $entries = $criteria->getEntries(); + if (!empty($entries)) + $cust_tag_info = $this->getCustomerTagInfo($cv); + } // error_log($stype); switch ($stype) { @@ -81,7 +91,7 @@ class ResqInvoiceGenerator implements InvoiceGeneratorInterface $this->processBatteries($total, $criteria, $invoice); $this->processTradeIns($total, $criteria, $invoice); */ - $this->processDiscount($total, $criteria, $invoice); + $this->processDiscount($total, $criteria, $invoice, $cust_tag_info); break; case ServiceType::BATTERY_REPLACEMENT_WARRANTY: @@ -404,45 +414,94 @@ class ResqInvoiceGenerator implements InvoiceGeneratorInterface } } - protected function processDiscount(&$total, InvoiceCriteria $criteria, Invoice $invoice) + protected function processDiscount(&$total, InvoiceCriteria $criteria, Invoice $invoice, $cust_tag_info) { - $promos = $criteria->getPromos(); - if (count($promos) < 1) - return; - - // NOTE: only get first promo because only one is applicable anyway - $promo = $promos[0]; - - $rate = $promo->getDiscountRate(); - $apply_to = $promo->getDiscountApply(); - - switch ($apply_to) + if (empty($cust_tag_info)) { - case DiscountApply::SRP: - $discount = round($total['sell_price'] * $rate, 2); - break; - case DiscountApply::OPL: - // $discount = round($total['sell_price'] * 0.6 / 0.7 * $rate, 2); - $discount = round($total['sell_price'] * (1 - 1.5 / 0.7 * $rate), 2); - break; - } + //error_log('empty cust tag'); + $promos = $criteria->getPromos(); + if (count($promos) < 1) + return; - // if discount is higher than 0, display in invoice - if ($discount > 0) + // NOTE: only get first promo because only one is applicable anyway + $promo = $promos[0]; + + $rate = $promo->getDiscountRate(); + $apply_to = $promo->getDiscountApply(); + + switch ($apply_to) + { + case DiscountApply::SRP: + $discount = round($total['sell_price'] * $rate, 2); + break; + case DiscountApply::OPL: + // $discount = round($total['sell_price'] * 0.6 / 0.7 * $rate, 2); + $discount = round($total['sell_price'] * (1 - 1.5 / 0.7 * $rate), 2); + break; + } + + // if discount is higher than 0, display in invoice + if ($discount > 0) + { + $item = new InvoiceItem(); + $item->setInvoice($invoice) + ->setTitle('Promo discount') + ->setQuantity(1) + ->setPrice(-1 * $discount); + $invoice->addItem($item); + } + + $total['discount'] = $discount; + $total['total_price'] -= $discount; + + // process + $invoice->setPromo($promo); + } + else { - $item = new InvoiceItem(); - $item->setInvoice($invoice) - ->setTitle('Promo discount') - ->setQuantity(1) - ->setPrice(-1 * $discount); - $invoice->addItem($item); + // since only one promo can only be used, we prioritize the tag promos + // TODO: need to test this for multiple tags + $total_discount_amount = 0; + $total_amount = $total['total_price']; + foreach ($cust_tag_info as $ct_info) + { + // check discount type + $discount_type = ''; + $discount_value = 0; + $discount_amount = 0; + $discounted_total = 0; + if (isset($ct_info['discount_type'])) + $discount_type = $ct_info['discount_type']; + if (isset($ct_info['discount_value'])) + { + $discount_value = $ct_info['discount_value']; + } + + if ($discount_type == 'percent') + { + $discount = round(($discount_value / 100), 2); + $discount_amount = $total_amount * $discount; + $discounted_total = $total_amount - $discount_amount; + } + else + { + // assume fixed amount for this + $discount = $discount_value; + $discounted_total = $total_amount - $discount; + } + + $total_discount_amount += $discounted_total; + $item = new InvoiceItem(); + $item->setInvoice($invoice) + ->setTitle($ct_info['invoice_display']) + ->setQuantity(1) + ->setPrice(-1 * $total_discount_amount); + $invoice->addItem($item); + } + + $total['discount'] = $total_discount_amount; + $total['total_price'] -= $total_discount_amount; } - - $total['discount'] = $discount; - $total['total_price'] -= $discount; - - // process - $invoice->setPromo($promo); } protected function processJumpstart(&$total, $invoice) @@ -663,6 +722,7 @@ class ResqInvoiceGenerator implements InvoiceGeneratorInterface $cv = $criteria->getCustomerVehicle(); $has_coolant = $criteria->hasCoolant(); // error_log($stype); + $cust_tag_info = []; switch ($stype) { case ServiceType::JUMPSTART_TROUBLESHOOT: @@ -677,7 +737,7 @@ class ResqInvoiceGenerator implements InvoiceGeneratorInterface $this->processBatteries($total, $criteria, $invoice); $this->processTradeIns($total, $criteria, $invoice); */ - $this->processDiscount($total, $criteria, $invoice); + $this->processDiscount($total, $criteria, $invoice, $cust_tag_info); break; case ServiceType::BATTERY_REPLACEMENT_WARRANTY: @@ -719,4 +779,33 @@ class ResqInvoiceGenerator implements InvoiceGeneratorInterface return $invoice; } + + protected function getCustomerTagInfo($cv) + { + $cust_tag_info = []; + // get customer and customer tags using customer vehicle + $customer = $cv->getCustomer(); + $customer_tags = $customer->getCustomerTagObjects(); + if (!empty($customer_tags)) + { + foreach ($customer_tags as $customer_tag) + { + // TODO: can we keep the tag ids hardcoded? + // check tag details + $cust_tag_type = $customer_tag->getTagDetails('type'); + // TODO: might have to make this statement be more generic? + if (($cust_tag_type != null) && ($cust_tag_type == 'one-time-discount')) + { + $cust_tag_info[] = [ + 'type' => $cust_tag_type, + 'discount_type' => $customer_tag->getTagDetails('discount_type'), + 'discount_amount' => $customer_tag->getTagDetails('discount_amount'), + 'discount_value' => $customer_tag->getTagDetails('discount_value'), + 'invoice_display' => $customer_tag->getTagDetails('invoice_display'), + ]; + } + } + } + return $cust_tag_info; + } } diff --git a/src/Service/JobOrderHandler/ResqJobOrderHandler.php b/src/Service/JobOrderHandler/ResqJobOrderHandler.php index a47a7a98..804c0002 100644 --- a/src/Service/JobOrderHandler/ResqJobOrderHandler.php +++ b/src/Service/JobOrderHandler/ResqJobOrderHandler.php @@ -25,6 +25,7 @@ use App\Entity\Rider; use App\Entity\JORejection; use App\Entity\Warranty; use App\Entity\Customer; +use App\Entity\CustomerTag; use App\Ramcar\InvoiceCriteria; use App\Ramcar\ServiceType; @@ -50,6 +51,7 @@ use App\Service\MQTTClient; use App\Service\APNSClient; use App\Service\MapTools; use App\Service\RisingTideGateway; +use App\Service\PromoLogger; use CrEOF\Spatial\PHP\Types\Geometry\Point; @@ -71,13 +73,15 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface protected $country_code; protected $wh; protected $rt; + protected $promo_logger; protected $template_hash; public function __construct(Security $security, EntityManagerInterface $em, InvoiceGeneratorInterface $ic, ValidatorInterface $validator, TranslatorInterface $translator, RiderAssignmentHandlerInterface $rah, - string $country_code, WarrantyHandler $wh, RisingTideGateway $rt) + string $country_code, WarrantyHandler $wh, RisingTideGateway $rt, + PromoLogger $promo_logger) { $this->em = $em; $this->ic = $ic; @@ -88,6 +92,7 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface $this->country_code = $country_code; $this->wh = $wh; $this->rt = $rt; + $this->promo_logger = $promo_logger; $this->loadTemplates(); } @@ -288,10 +293,13 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface $em = $this->em; $jo = $em->getRepository(JobOrder::class)->find($id); + // flag for new job order for the customer tags + $flag_new_jo = false; if (empty($jo)) { // new job order $jo = new JobOrder(); + $flag_new_jo = true; } // find customer @@ -472,6 +480,41 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface if ($jo != null) { $data['job_order'] = $jo; + + // need to get the customer tags but only if new JO + if ($flag_new_jo) + { + // check service type + if ($jo->getServiceType() == ServiceType::BATTERY_REPLACEMENT_NEW) + { + $customer = $cust_vehicle->getCustomer(); + $customer_tags = $customer->getCustomerTagObjects(); + if (!empty($customer_tags)) + { + foreach ($customer_tags as $customer_tag) + { + // TODO: not too comfy with this being hardcoded + if ($customer_tag->getID() == 'CAR_CLUB_PROMO') + { + // remove associated entity + $customer->removeCustomerTag($customer_tag); + + // log the availment of promo from customer + $created_by = $jo->getCreatedBy()->getUsername(); + $cust_id = $jo->getCustomer()->getID(); + $cust_fname = $jo->getCustomer()->getFirstName(); + $cust_lname = $jo->getCustomer()->getLastName(); + $jo_id = $jo->getID(); + $invoice_id = $jo->getInvoice()->getID(); + // TODO: check if we store total price of invoice or just the discounted amount + $amount = $jo->getInvoice()->getTotalPrice(); + $this->promo_logger->logPromoInfo($created_by, $cust_id, $cust_fname, $cust_lname, $jo_id, + $invoice_id, $amount); + } + } + } + } + } } return $data; @@ -2685,6 +2728,9 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface $fac_hubs[$hub->getID()] = $hub->getName() . ' - ' . $hub->getBranch(); } + // list of customer tags + $params['customer_tags'] = $em->getRepository(CustomerTag::class)->findAll(); + // name values $params['service_types'] = ServiceType::getCollection(); $params['warranty_classes'] = WarrantyClass::getCollection(); diff --git a/src/Service/PromoLogger.php b/src/Service/PromoLogger.php new file mode 100644 index 00000000..f41710a9 --- /dev/null +++ b/src/Service/PromoLogger.php @@ -0,0 +1,35 @@ +em = $em; + } + + public function logPromoInfo($created_by, $cust_id, $cust_fname, $cust_lname, $jo_id, + $invoice_id, $amount) + { + $log_entry = new PromoLog(); + + $log_entry->setCreatedBy($created_by) + ->setCustId($cust_id) + ->setCustFirstName($cust_fname) + ->setCustLastName($cust_lname) + ->setJoId($jo_id) + ->setInvoiceId($invoice_id) + ->setAmount($amount); + + $this->em->persist($log_entry); + $this->em->flush(); + } +} + diff --git a/templates/customer-tag/form.html.twig b/templates/customer-tag/form.html.twig new file mode 100644 index 00000000..455bafbd --- /dev/null +++ b/templates/customer-tag/form.html.twig @@ -0,0 +1,156 @@ +{% extends 'base.html.twig' %} + +{% block body %} + +
+
+
+

Customer Tags

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

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

+
+
+
+
+ +
+
+
+ + + + Unique identifier for this customer tag +
+
+
+
+ + + +
+
+
+
+ + + + JSON format e.g. {"type":"discount", "discount_type":"percent", ...} +
+
+
+
+
+
+
+ + Back +
+
+
+
+
+
+
+
+
+{% endblock %} + + +{% block scripts %} + +{% endblock %} + diff --git a/templates/customer-tag/list.html.twig b/templates/customer-tag/list.html.twig new file mode 100644 index 00000000..7b33ae44 --- /dev/null +++ b/templates/customer-tag/list.html.twig @@ -0,0 +1,144 @@ +{% extends 'base.html.twig' %} + +{% block body %} + +
+
+
+

+ Customer Tags +

+
+
+
+ +
+ +
+
+
+
+
+
+
+
+
+
+ + + + +
+
+
+
+ +
+
+ +
+ +
+
+
+
+
+{% endblock %} + +{% block scripts %} + +{% endblock %} + diff --git a/templates/customer/form.html.twig b/templates/customer/form.html.twig index eeae4bbe..a68640df 100644 --- a/templates/customer/form.html.twig +++ b/templates/customer/form.html.twig @@ -101,7 +101,7 @@ -
+
@@ -160,6 +160,19 @@
+
+
+
+ {% for customer_tag in customer_tags %} + + {% endfor %} +
+
+
diff --git a/templates/job-order/form.html.twig b/templates/job-order/form.html.twig index ac001767..fede693a 100644 --- a/templates/job-order/form.html.twig +++ b/templates/job-order/form.html.twig @@ -201,11 +201,29 @@
{% if is_granted('customer.dpa') %} - +
{% endif %}
+
+
+
+ {% for customer_tag in customer_tags %} + + {% endfor %} +
+
+
@@ -1371,6 +1389,19 @@ $(function() { $("#flag-research-email").prop("checked", true); } + if (vdata.customer.customer_tags.length > 0) { + var checkboxes = document.getElementsByName("customer_tags[]"); + for (var x = 0; checkboxes.length > x; x++) + { + if (vdata.customer.customer_tags.indexOf(checkboxes[x].value) !== -1) { + checkboxes[x].checked = true; + } + else { + checkboxes[x].checked = false; + } + } + } + // set hidden customer id $("#cid").val(vdata.customer.id); @@ -1417,6 +1448,12 @@ $(function() { $("#flag-promo-email").prop("checked", false); $("#flag-research-sms").prop("checked", false); $("#flag-research-email").prop("checked", false); + + var checkboxes = document.getElementsByName("customer_tags[]"); + for (var x = 0; checkboxes.length > x; x++) + { + checkboxes[x].checked = false; + } } // datepicker