/** * Add New offer * * @param array $requestParams - Request parameters * * @return $array */ public function add($requestParams) { $user = $this->securityContext->getToken()->getUser(); $practiceProfileId = $this->securityContext->getToken()->getSession()->getPracticeProfileId(); if (null === $practiceProfileId) { return null; } $dummyOffer = new Offer(); $dummyOffer->setPracticeProfileId($practiceProfileId); $er = $this->doctrine->getManager()->getRepository('PractoApiBundle:Offer'); if (!$this->securityContext->isGranted('READ', $dummyOffer)) { throw new AccessDeniedException("This user or role is not allowed to retrieve these offers."); } $errors = array(); if (array_key_exists('offer_code', $requestParams) && strlen("" . $requestParams["offer_code"]) > 0) { if (strlen($requestParams["offer_code"]) > 15) { @($errors['offer_code'][] = "Offer code length can't be more than 15 characters."); } else { $requestOfferCode = $requestParams["offer_code"]; if ($er->checkDuplicateOffer($practiceProfileId, $requestOfferCode)) { @($errors['offer_code'][] = "This offer code already exists."); } } } else { @($errors['offer_code'][] = "Offer code can't be empty."); } if (array_key_exists('discount', $requestParams) && strlen("" . $requestParams["discount"]) > 0) { if (!is_numeric($requestParams["discount"])) { @($errors['discount'][] = "Discount type must be a numeric value."); } else { if (round($requestParams["discount"], 2) <= 0) { @($errors['discount'][] = "This value should be greater than 0.01"); } } } else { @($errors['discount'][] = "Discount can't be empty."); } if (array_key_exists('discount_units', $requestParams) && !empty($requestParams["discount_units"])) { if (array_key_exists('discount', $requestParams) && strlen("" . $requestParams["discount"]) > 0) { if (strcmp($requestParams["discount_units"], "PERCENT") == 0 && $requestParams["discount"] > 100) { @($errors['discount'][] = "Discount percent can't be more than 100."); } } if (strcmp($requestParams["discount_units"], "PERCENT") != 0 && strcmp($requestParams["discount_units"], "NUMBER") != 0) { @($errors['discount_units'][] = "The value you selected is not a valid choice."); } } else { @($errors['discount_units'][] = "Discount unit can't be empty."); } if (array_key_exists('offer_description', $requestParams) && !empty($requestParams["offer_description"]) && strlen($requestParams["offer_description"]) > 30) { @($errors['offer_description'][] = "Offer description length can't be more than 30 characters."); } if (0 < count($errors)) { throw new ValidationError($errors); } $offer = new Offer(); $offer->setPracticeProfileId($practiceProfileId); $offer->setCreatedAt(new \DateTime('now')); $offer->setCreatedByUserId($user); if ($this->securityContext->isGranted('CREATE', $offer)) { $offer->setSoftDeleted(false); $offer->setVisible(true); $this->updateFields($offer, $requestParams, $user, $practiceProfileId); $em = $this->doctrine->getManager(); $em->persist($offer); $em->flush(); } else { throw new AccessDeniedException("This user or role is not allowed to create offer."); } return $offer; }