/**
  * Auto setup communications.
  *
  * Fires after a membership setup is completed.
  *
  * Related Action Hooks:
  * - ms_controller_membership_setup_completed
  *
  * @since  1.0.0
  * @param MS_Model_Membership $membership
  */
 public function auto_setup_communications($membership)
 {
     /*
      * Note: We intentionally set the parameter to 0. This
      * function should set up default messages when first membership is
      * created. It should not override default messages with membership-
      * specific ones.
      */
     $comms = MS_Model_Communication::get_communications(0);
     foreach ($comms as $comm) {
         $comm->enabled = true;
         $comm->save();
     }
     do_action('ms_controller_communication_auto_setup_communications_after', $membership, $this);
 }
 /**
  * Check membership status.
  *
  * Execute actions when time/period condition are met.
  * E.g. change membership status, add communication to queue, create invoices.
  *
  * This check is called via a cron job.
  *
  * @since  1.0.0
  * @internal  Used by Cron
  * @see MS_Model_Plugin::check_membership_status()
  */
 public function check_membership_status()
 {
     do_action('ms_model_relationship_check_membership_status_before', $this);
     /**
      * Use `define( 'MS_LOCK_SUBSCRIPTIONS', true );` in wp-config.php to prevent
      * Membership2 from sending *any* emails to users.
      * Also any currently enqueued message is removed from the queue
      *
      * @since  1.0.0
      */
     if (MS_Plugin::get_modifier('MS_LOCK_SUBSCRIPTIONS')) {
         return false;
     }
     $membership = $this->get_membership();
     $remaining_days = $this->get_remaining_period();
     $remaining_trial_days = $this->get_remaining_trial_period();
     $comms = MS_Model_Communication::get_communications($membership);
     $invoice_before_days = 5;
     //@todo create a setting to configure this period.
     $deactivate_expired_after_days = 30;
     //@todo create a setting to configure this period.
     $deactivate_pending_after_days = 30;
     //@todo create a setting to configure this period.
     $deactivate_trial_expired_after_days = 5;
     //@todo create a setting to configure this period.
     //@todo: Add a flag to subscriptions with sent communications. Then improve the conditions below to prevent multiple emails.
     do_action('ms_check_membership_status-' . $this->status, $this, $remaining_days, $remaining_trial_days);
     // Update the Subscription status.
     $next_status = $this->calculate_status(null);
     switch ($next_status) {
         case self::STATUS_TRIAL:
             if (MS_Model_Addon::is_enabled(MS_Model_Addon::ADDON_TRIAL) && MS_Model_Addon::is_enabled(MS_Model_Addon::ADDON_AUTO_MSGS_PLUS)) {
                 // Send trial end communication.
                 $comm = $comms[MS_Model_Communication::COMM_TYPE_BEFORE_TRIAL_FINISHES];
                 if ($comm->enabled) {
                     $days = MS_Helper_Period::get_period_in_days($comm->period['period_unit'], $comm->period['period_type']);
                     //@todo: This will send out the reminder multiple times on the reminder-day (4 times or more often)
                     if ($days == $remaining_trial_days) {
                         $comm->add_to_queue($this->id);
                         MS_Model_Event::save_event(MS_Model_Event::TYPE_MS_BEFORE_TRIAL_FINISHES, $this);
                     }
                 }
             }
             // Check for card expiration
             $gateway = $this->get_gateway();
             $gateway->check_card_expiration($this);
             break;
         case self::STATUS_TRIAL_EXPIRED:
             if (MS_Model_Addon::is_enabled(MS_Model_Addon::ADDON_TRIAL)) {
                 // Mark the trial period as completed. $this->save() is below.
                 $this->trial_period_completed = true;
                 // Request payment to the gateway (for gateways that allows it).
                 $gateway = $this->get_gateway();
                 /*
                  * The subscription will be either automatically activated
                  * or set to pending.
                  *
                  * Important: Set trial_period_completed=true before calling
                  * request_payment()!
                  */
                 if ($gateway->request_payment($this)) {
                     $next_status = self::STATUS_ACTIVE;
                     $this->status = $next_status;
                     $this->config_period();
                     // Needed because of status change.
                 }
                 // Check for card expiration
                 $gateway->check_card_expiration($this);
                 // Deactivate expired memberships after a period of time.
                 if ($deactivate_trial_expired_after_days < -$remaining_trial_days) {
                     $this->deactivate_membership();
                 }
             }
             break;
         case self::STATUS_ACTIVE:
         case self::STATUS_EXPIRED:
         case self::STATUS_CANCELED:
             /*
              * Make sure the expire date has a correct value, in case the user
              * changed the payment_type of the parent membership after this
              * subscription was created.
              */
             if ($this->payment_type != $membership->payment_type) {
                 $this->payment_type = $membership->payment_type;
                 switch ($this->payment_type) {
                     case MS_Model_Membership::PAYMENT_TYPE_PERMANENT:
                         $this->expire_date = false;
                         break;
                     default:
                         // Either keep the current expire date (if valid) or
                         // calculate a new expire date, based on current date.
                         if (!$this->expire_date) {
                             $this->expire_date = $this->calc_expire_date(MS_Helper_Period::current_date());
                         }
                         break;
                 }
                 // Recalculate the days until the subscription expires.
                 $remaining_days = $this->get_remaining_period();
                 // Recalculate the new Subscription status.
                 $next_status = $this->calculate_status();
             }
             /*
              * Only "Recurring" memberships will ever try to automatically
              * renew the subscription. All other types will expire when the
              * end date is reached.
              */
             $auto_renew = $membership->payment_type == MS_Model_Membership::PAYMENT_TYPE_RECURRING;
             $deactivate = false;
             $invoice = null;
             if ($auto_renew && $membership->pay_cycle_repetitions > 0) {
                 /*
                  * The membership has a payment-repetition limit.
                  * When this limit is reached then we do not auto-renew the
                  * subscription but expire it.
                  */
                 $payments = $this->get_payments();
                 if (count($payments) >= $membership->pay_cycle_repetitions) {
                     $auto_renew = false;
                 }
             }
             if ($auto_renew) {
                 if ($remaining_days < $invoice_before_days) {
                     // Create a new invoice.
                     $invoice = $this->get_next_invoice();
                 } else {
                     $invoice = $this->get_current_invoice();
                 }
             } else {
                 $invoice = $this->get_current_invoice();
             }
             // Advanced communications Add-on.
             if (MS_Model_Addon::is_enabled(MS_Model_Addon::ADDON_AUTO_MSGS_PLUS)) {
                 // Before finishes communication.
                 $comm = $comms[MS_Model_Communication::COMM_TYPE_BEFORE_FINISHES];
                 $days = MS_Helper_Period::get_period_in_days($comm->period['period_unit'], $comm->period['period_type']);
                 if ($days == $remaining_days) {
                     $comm->add_to_queue($this->id);
                     MS_Model_Event::save_event(MS_Model_Event::TYPE_MS_BEFORE_FINISHES, $this);
                 }
                 // After finishes communication.
                 $comm = $comms[MS_Model_Communication::COMM_TYPE_AFTER_FINISHES];
                 $days = MS_Helper_Period::get_period_in_days($comm->period['period_unit'], $comm->period['period_type']);
                 if ($remaining_days < 0 && $days == abs($remaining_days)) {
                     $comm->add_to_queue($this->id);
                     MS_Model_Event::save_event(MS_Model_Event::TYPE_MS_AFTER_FINISHES, $this);
                 }
                 // Before payment due.
                 $comm = $comms[MS_Model_Communication::COMM_TYPE_BEFORE_PAYMENT_DUE];
                 $days = MS_Helper_Period::get_period_in_days($comm->period['period_unit'], $comm->period['period_type']);
                 $invoice_days = MS_Helper_Period::subtract_dates($invoice->due_date, MS_Helper_Period::current_date());
                 if (MS_Model_Invoice::STATUS_BILLED == $invoice->status && $days == $invoice_days) {
                     $comm->add_to_queue($this->id);
                     MS_Model_Event::save_event(MS_Model_Event::TYPE_PAYMENT_BEFORE_DUE, $this);
                 }
                 // After payment due event
                 $comm = $comms[MS_Model_Communication::COMM_TYPE_AFTER_PAYMENT_DUE];
                 $days = MS_Helper_Period::get_period_in_days($comm->period['period_unit'], $comm->period['period_type']);
                 $invoice_days = MS_Helper_Period::subtract_dates($invoice->due_date, MS_Helper_Period::current_date());
                 if (MS_Model_Invoice::STATUS_BILLED == $invoice->status && $days == $invoice_days) {
                     $comm->add_to_queue($this->id);
                     MS_Model_Event::save_event(MS_Model_Event::TYPE_PAYMENT_AFTER_DUE, $this);
                 }
             }
             // -- End of advanced communications Add-on
             // Subscription ended. See if we can renew it.
             if ($remaining_days <= 0) {
                 if ($auto_renew) {
                     /*
                      * The membership can be renewed. Try to renew it
                      * automatically by requesting the next payment from the
                      * payment gateway (only works if gateway supports this)
                      */
                     $gateway = $this->get_gateway();
                     $gateway->check_card_expiration($this);
                     $gateway->request_payment($this);
                     // Check if the payment was successful.
                     $remaining_days = $this->get_remaining_period();
                 }
                 /*
                  * User did not renew the membership. Give him some time to
                  * react before restricting his access.
                  */
                 if ($deactivate_expired_after_days < -$remaining_days) {
                     $deactivate = true;
                 }
             }
             $next_status = $this->calculate_status(null);
             /*
              * When the subscription expires the first time then create a
              * new event that triggers the "Expired" email.
              */
             if (self::STATUS_EXPIRED == $next_status && $next_status != $this->status) {
                 MS_Model_Event::save_event(MS_Model_Event::TYPE_MS_EXPIRED, $this);
             } elseif ($deactivate) {
                 $this->deactivate_membership();
                 $next_status = $this->status;
                 // Move membership to configured membership.
                 $membership = $this->get_membership();
                 $new_membership = MS_Factory::load('MS_Model_Membership', $membership->on_end_membership_id);
                 if ($new_membership->is_valid()) {
                     $member = MS_Factory::load('MS_Model_Member', $this->user_id);
                     $new_subscription = $member->add_membership($membership->on_end_membership_id, $this->gateway_id);
                     MS_Model_Event::save_event(MS_Model_Event::TYPE_MS_MOVED, $new_subscription);
                     /*
                      * If the new membership is paid we want that the user
                      * confirms the payment in his account. So we set it
                      * to "Pending" first.
                      */
                     if (!$new_membership->is_free()) {
                         $new_subscription->status = self::STATUS_PENDING;
                     }
                 }
             }
             break;
         case self::STATUS_DEACTIVATED:
             /*
              * A subscription was finally deactivated.
              * Lets check if the member has any other active subscriptions,
              * or (if not) his account should be deactivated.
              *
              * First get a list of all subscriptions that do not have status
              * Pending / Deactivated.
              */
             $subscriptions = self::get_subscriptions(array('user_id' => $this->user_id));
             // Check if there is a subscription that keeps the user active.
             $deactivate = true;
             foreach ($subscriptions as $item) {
                 if ($item->id == $this->id) {
                     continue;
                 }
                 $deactivate = false;
             }
             if ($deactivate) {
                 $member = $this->get_member();
                 $member->is_member = false;
                 $member->save();
             }
             break;
         case self::STATUS_PENDING:
         default:
             // Do nothing.
             break;
     }
     // Save the new status.
     $this->status = $next_status;
     $this->save();
     // Save the changed email queue.
     foreach ($comms as $comm) {
         $comm->save();
     }
     do_action('ms_model_relationship_check_membership_status_after', $this);
 }