예제 #1
0
 /**
  * Creates a form to delete a Discount entity.
  *
  * @param Discount $discount The Discount entity
  *
  * @return Form The form
  */
 private function createDeleteForm(Discount $discount)
 {
     return $this->createFormBuilder()->setAction($this->generateUrl('admin_discount_delete', array('id' => $discount->getId())))->setMethod(Request::METHOD_DELETE)->getForm();
 }
예제 #2
0
 /**
  * @Route("/provider/saveStatus", name="equipment-saveStatus")
  */
 public function saveStatusAction(Request $request)
 {
     $id = $request->get('id');
     $text = $request->get('text');
     $errors = array();
     $discountType = $this->IsParamValidInt($errors, $request->get('discountType'), "discount type");
     $percent = $this->IsParamValidInt($errors, $request->get('percent'), "percent");
     $duration = $this->IsParamValidInt($errors, $request->get('duration'), "duration");
     //$discountType = (integer)$discountTypeStr;
     //$percent = (integer)$request->get('percent');
     //$duration = (integer)$request->get('duration');
     $equipment = $this->getDoctrineRepo('AppBundle:Equipment')->find($id);
     // security check
     if ($this->getUser()->getId() !== $equipment->getUser()->getId()) {
         //return new Response($status = Response::HTTP_FORBIDDEN);
         $errors[count($errors)] = "Access denied.";
     }
     if (count($errors) == 0 && $discountType != -1 && $discountType != 0 && $equipment->getActiveDiscount() != null) {
         $errors[count($errors)] = "There already is active discount!";
     } else {
         if (count($errors) == 0 && $discountType != -1 && $discountType != 0) {
             if ($discountType != 1 && $discountType != 2) {
                 $errors[count($errors)] = "Unknown discount selected.";
             }
             if ($percent < -1 || $percent > 6) {
                 $errors[count($errors)] = "Unknown percent selected.";
             }
             if ($discountType == 1 && ($duration < -1 || $duration > 5) || $discountType == 2 && ($duration < -1 || $duration > 24)) {
                 $errors[count($errors)] = "Unknown duration selected.";
             }
             if ($percent == -1 || $percent == 0) {
                 $errors[count($errors)] = "Please select discount percent.";
             }
             if ($duration == -1 || $duration == 0) {
                 $errors[count($errors)] = "Please select discount duration.";
             }
         }
     }
     if (count($errors) > 0) {
         $status = JsonResponse::HTTP_INTERNAL_SERVER_ERROR;
         $resp = new JsonResponse($errors, $status);
         return $resp;
     }
     $result = "OK";
     $status = JsonResponse::HTTP_OK;
     try {
         $equipment->setStatus($text);
         $em = $this->getDoctrine()->getManager();
         $em->persist($equipment);
         $em->flush();
         $activeDiscount = $equipment->getActiveDiscount();
         if ($discountType != -1 && $discountType != 0 && $activeDiscount == null) {
             $discount = new Discount();
             $discount->setType($discountType);
             $discount->setPercent($percent);
             $discount->setDuration($duration);
             $discount->setEquipment($equipment);
             $now = new DateTime();
             $endDate = new DateTime();
             $discount->setCreatedAt($now);
             $inverval = "";
             if ($discountType == 1) {
                 $inverval = "P" . $duration * 7 . "D";
             } else {
                 if ($discountType == 2) {
                     $inverval = "PT" . $duration . "H";
                 }
             }
             $endDate->add(new DateInterval($inverval));
             $discount->setExpiresAt($endDate);
             $em->persist($discount);
             $em->flush();
         } else {
             if (($discountType == -1 || $discountType == 0) && $activeDiscount != null) {
                 $now = new DateTime();
                 $activeDiscount->setExpiresAt($now);
                 $em->persist($activeDiscount);
                 $em->flush();
             }
         }
     } catch (Exception $ex) {
         $result = "Error.";
         $status = JsonResponse::HTTP_INTERNAL_SERVER_ERROR;
     }
     $resp = new JsonResponse($result, $status);
     return $resp;
 }