/**
  * Report to the referrer that they are eligible for a discount reward, but their discount code could not be
  * generated.
  *
  * @param ReferralInterface $referral   The referral that should have completed and had a discount reward generated
  * @param DiscountBuildException $e     The exception that was thrown upon failure to generate a discount code
  */
 public function report(ReferralInterface $referral, DiscountBuildException $e)
 {
     $this->_message->setTo($referral->getReferrer()->getName(), $referral->getReferrer()->email);
     $this->_message->setSubject($this->_translator->trans('ms.discount_reward.email.failure.subject'));
     $this->_message->setView('Message:Mothership:DiscountReward::discount_reward:email:create_failed', ['toName' => $referral->getReferrer()->getName(), 'message' => $this->_translator->trans('ms.discount_reward.email.failure.message'), 'error' => $e->getMessage()]);
     $this->_send();
 }
 /**
  * Inform the referrer that a discount code has been generated for them
  *
  * @param ReferralInterface $referral
  * @param Discount $discount
  */
 public function inform(ReferralInterface $referral, Discount $discount)
 {
     $this->_message->setTo($referral->getReferrer()->email, $referral->getReferrer()->getName());
     $this->_message->setSubject($this->_translator->trans('ms.discount_reward.email.success.subject'));
     $this->_message->setView('Message:Mothership:DiscountReward::discount_reward:email:discount_create', ['message' => $this->_translator->trans('ms.discount_reward.email.success.message', ['{%toName%}' => $referral->getReferrer()->getName(), '{%discountCode%}' => $discount->code]), 'discount' => $discount]);
     $this->_send();
 }
 private function _isValid(Referral\ReferralInterface $referral)
 {
     foreach ($referral->getRewardConfig()->getConstraints() as $constraint) {
         if (false === $constraint->isValid($referral)) {
             return false;
         }
     }
     return true;
 }
    /**
     * Save the referral to the database.
     *
     * @param ReferralInterface $referral
     *
     * @return ReferralInterface
     */
    public function save(ReferralInterface $referral)
    {
        $result = $this->_query->run('
			INSERT INTO
				refer_a_friend_referral
				(
					reward_config_id,
					status,
					referrer_id,
					referred_email,
					referred_name,
					created_at,
					created_by,
					updated_at,
					updated_by
				)
			VALUES
				(
					:rewardConfigID?i,
					:status?s,
					:referrerID?i,
					:referredEmail?s,
					:referredName?s,
					:createdAt?d,
					:createdBy?in,
					:createdAt?d,
					:createdBy?in
				)
		', ['rewardConfigID' => $referral->getRewardConfig()->getID(), 'status' => $referral->getStatus(), 'referrerID' => $referral->getReferrer()->id, 'referredEmail' => $referral->getReferredEmail(), 'referredName' => $referral->getReferredName(), 'createdAt' => $referral->getCreatedAt() ?: new \DateTime(), 'createdBy' => $this->_currentUser->id]);
        $referral->setID($result->id());
        return $referral;
    }
    /**
     * Add query saving the referral to the database transaction
     *
     * @param ReferralInterface $referral
     */
    private function _addToTransaction(ReferralInterface $referral)
    {
        $this->_trans->add('
			UPDATE
				refer_a_friend_referral
			SET
				status         = :status?s,
				referrer_id    = :referrerID?i,
				referred_email = :referredEmail?s,
				updated_at     = :updatedAt?d,
				updated_by     = :updatedBy?in
			WHERE
				referral_id = :id?i
		', ['status' => $referral->getStatus(), 'referrerID' => $referral->getReferrer()->id, 'referredEmail' => $referral->getReferredEmail(), 'updatedAt' => new \DateTime(), 'updatedBy' => $this->_currentUser->id, 'id' => $referral->getID()]);
    }
 /**
  * Create a discount object with a unique code from a referral.
  * If there is no valid discount type (i.e. percentage or set amount), it will throw an exception
  *
  * @param ReferralInterface $referral
  * @throws Exception\DiscountBuildException
  *
  * @return Discount
  */
 public function build(ReferralInterface $referral)
 {
     $discount = new Discount();
     $discount->code = $this->_getCode();
     $discount->emails = [$referral->getReferrer()->email];
     $rewardOptions = $referral->getRewardConfig()->getRewardOptions();
     $type = $rewardOptions->get('discount_reward_discount_type')->getValue();
     switch ($type) {
         case DiscountType::SET_AMOUNT:
             $discount->discountAmounts = [];
             foreach ($rewardOptions as $rewardOption) {
                 if ($rewardOption instanceof Config\RewardOption\SetAmount) {
                     $discount->discountAmounts[$rewardOption->getCurrency()] = $rewardOption->getValue();
                 }
             }
             break;
         case DiscountType::PERCENTAGE:
             $discount->percentage = $rewardOptions->get('discount_reward_percentage_value')->getValue();
             break;
         default:
             throw new Exception\DiscountBuildException('No valid discount type set on discount referral');
     }
     return $discount;
 }
 /**
  * {@inheritDoc}
  */
 public function isValid(ReferralInterface $referral)
 {
     return $referral->getReferredEmail() !== $referral->getReferrer()->email;
 }
 /**
  * {@inheritDoc}
  */
 public function isValid(ReferralInterface $referral, Event $event)
 {
     if (!$event instanceof OrderEvent) {
         throw new \LogicException('Event should be an instance of OrderEvent');
     }
     $orderDate = $event->getOrder()->authorship->createdAt()->format('Y-m-d');
     $referralDate = $referral->getCreatedAt()->format('Y-m-d');
     $diff = strtotime($orderDate) - strtotime($referralDate);
     $timedOut = $diff <= $this->_value;
     if (false === $timedOut) {
         $referral->setStatus(Statuses::EXPIRED);
         $this->_referralEdit->save($referral);
     }
 }
 /**
  * {@inheritDoc}
  */
 public function isValid(ReferralInterface $referral)
 {
     $existing = $this->_loader->getByEmail($referral->getReferredEmail());
     return count($existing) <= 0;
 }
 /**
  * {@inheritDoc}
  */
 public function isValid(ReferralInterface $referral)
 {
     $users = $this->_userLoader->getByEmail($referral->getReferredEmail());
     return empty($users);
 }