public function getLoyaltyCards() { if (is_null($this->_loyaltycards)) { $fcc = new LoyaltyCard_Model_Customer(); $customer_id = $this->getCustomer()->getId() | 0; $this->_loyaltycards = $fcc->findAllByOptionValue($this->getCurrentOption()->getId(), $customer_id); } return $this->_loyaltycards; }
public function validateAction() { try { $html = array(); if ($datas = Zend_Json::decode($this->getRequest()->getRawBody())) { $application_id = $this->getApplication()->getAppId(); // Récupération de l'option_value en cours $option_value = $this->getCurrentOptionValue(); // Récupération du client en cours $customer_id = $this->getSession()->getCustomerId(); // Si le client n'est pas connecté if (empty($customer_id)) { throw new Exception($this->_('You must be logged in to validate points')); } $customer_card_id = $datas['customer_card_id']; // Récupération de la carte de fidélité de l'utilisateur en cours $card = new LoyaltyCard_Model_Customer(); // Récupère la carte du client ou, à défaut, en créé une nouvelle $cards = $card->findAllByOptionValue($option_value->getId(), $customer_id); foreach ($cards as $tmp_card) { // Si la carte n'existe pas, customer_card_id == 0 if ($tmp_card->getCustomerCardId() == $customer_card_id) { $card = $tmp_card; } } // Déclaration des variables annexes $password_entered = $datas['password']; $nbr = !empty($datas['number_of_points']) ? $datas['number_of_points'] : 1; // Ou si le mot de passe est vide ou non numérique if ($card->getValueId() != $option_value->getId() or empty($password_entered) or !preg_match('/[0-9]/', $nbr)) { throw new Exception($this->_('An error occurred while validating point. Please try again later.')); } // Récupération du mot de passe $password = new LoyaltyCard_Model_Password(); $password->findByPassword($password_entered, $application_id); // Test si le mot de passe a été trouvé if (!$password->getId()) { // Ajoute une erreur à la carte en cours $card->addError(); // Calcul le nombre de tentative restante $only = 3 - $card->getNumberOfError(); // S'il reste au moins 1 tentative de saisie, on envoie un message d'erreur if ($only > 0) { $html['customer_card_id'] = $card->getCustomerCardId(); throw new Exception($this->_('Wrong password. Be carefull! %d remaining attempt%s before locking your card. Ask the store person for validating your point', $only, $only > 1 ? 's' : '')); } else { // Sinon, on ferme le clavier et on annonce que la carte est bloquée $html['close_pad'] = true; $html['card_is_locked'] = true; throw new Exception($this->_('You have exceeded the number of attempts to validate points. Your card is locked for 24h')); } } else { // S'il reste des points à valider if ($card->getNumberOfPoints() < $card->getMaxNumberOfPoints()) { // On met à jour la carte de fidélité du client $card->setNumberOfPoints($card->getNumberOfPoints() + $nbr)->setCustomerId($this->getSession()->getCustomerId())->setNumberOfError(0)->setLastError(null)->save(); // On log l'employé ayant validé le ou les points $card->createLog($password->getId(), $nbr); // On renvoie un message de validation $s = $nbr > 1 ? 's' : ''; $msg = $this->_('Point%s successfully validated', $s, $s); $html = array('success' => true, 'message' => $msg, 'close_pad' => true, 'number_of_points' => $card->getNumberOfPoints()); } else { $card->setIsUsed(1)->setUsedAt($card->formatDate(null, 'y-MM-dd HH:mm:ss'))->setValidateBy($password->getId())->save(); $html = array('success' => true, 'message' => $this->_('You just finished your card'), 'promotion_id_to_remove' => $card->getId(), 'close_pad' => true); } if ($this->getSession()->getCustomer()->canPostSocialMessage()) { $html['canPostMessage'] = true; } } } } catch (Exception $e) { $html['error'] = 1; $html['message'] = $e->getMessage(); } $this->_sendHtml($html); }