/**
  * Save Subscription
  *
  * @return  void
  */
 public function saveTask()
 {
     // Check for request forgeries
     Request::checkToken();
     $id = Request::getInt('id', 0);
     $subscription = new Subscription($this->database);
     if (!$subscription->load($id)) {
         App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller, false), Lang::txt('COM_SERVICES_SUBSCRIPTION_NOT_FOUND'), 'error');
         return;
     }
     // get service
     $service = new Service($this->database);
     if (!$service->loadService('', $subscription->serviceid)) {
         App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller, false), Lang::txt('COM_SERVICES_SERVICE_NOT_FOUND') . ' ' . $subscription->serviceid, 'error');
         return;
     }
     $author = User::getInstance($subscription->uid);
     $subscription->notes = rtrim(stripslashes(Request::getVar('notes', '')));
     $action = Request::getVar('action', '');
     $message = Request::getVar('message', '');
     $statusmsg = '';
     $email = 0;
     switch ($action) {
         case 'refund':
             $received_refund = Request::getInt('received_refund', 0);
             $newunits = Request::getInt('newunits', 0);
             $pending = $subscription->pendingpayment - $received_refund;
             $pendingunits = $subscription->pendingunits - $newunits;
             $subscription->pendingpayment = $pending <= 0 ? 0 : $pending;
             $subscription->pendingunits = $pendingunits <= 0 ? 0 : $pendingunits;
             $email = 0;
             $statusmsg .= Lang::txt('Refund has been processed.');
             break;
         case 'activate':
             $received_payment = Request::getInt('received_payment', 0);
             $newunits = Request::getInt('newunits', 0);
             $pending = $subscription->pendingpayment - $received_payment;
             $pendingunits = $subscription->pendingunits - $newunits;
             $subscription->pendingpayment = $pending <= 0 ? 0 : $pending;
             $subscription->pendingunits = $pendingunits <= 0 ? 0 : $pendingunits;
             $subscription->totalpaid = $subscription->totalpaid + $received_payment;
             $oldunits = $subscription->units;
             $months = $newunits * $service->unitsize;
             $newexpire = $oldunits > 0 && intval($subscription->expires) != 0 ? Date::of(strtotime($subscription->expires . "+" . $months . "months"))->format("Y-m-d") : Date::of(strtotime("+" . $months . "months"))->format("Y-m-d");
             $subscription->expires = $newunits ? $newexpire : $subscription->expires;
             $subscription->status = 1;
             $subscription->units = $subscription->units + $newunits;
             $email = ($received_payment > 0 or $newunits > 0) ? 1 : 0;
             $statusmsg .= Lang::txt('COM_SERVICES_SUBSCRIPTION_ACTIVATED');
             if ($newunits > 0) {
                 $statusmsg .= ' ' . Lang::txt('for') . ' ' . $newunits . ' ';
                 $statusmsg .= $oldunits > 0 ? Lang::txt('additional') . ' ' : '';
                 $statusmsg .= Lang::txt('month(s)');
             }
             break;
         case 'message':
             $statusmsg .= Lang::txt('Your message has been sent.');
             break;
         case 'cancelsub':
             $refund = 0;
             $unitsleft = $subscription->getRemaining('unit', $subscription, $service->maxunits, $service->unitsize);
             // get cost per unit (to compute required refund)
             $refund = $subscription->totalpaid > 0 && $unitsleft > 0 && $subscription->totalpaid - $unitsleft * $unitcost > 0 ? $unitsleft * $prevunitcost : 0;
             $subscription->status = 2;
             $subscription->pendingpayment = $refund;
             $subscription->pendingunits = $refund > 0 ? $unitsleft : 0;
             $email = 1;
             $statusmsg .= Lang::txt('COM_SERVICES_SUBSCRIPTION_CANCELLED');
             break;
     }
     if ($action && $action != 'message' || $message) {
         $subscription->notes .= '------------------------------' . "\r\n";
         $subscription->notes .= Lang::txt('COM_SERVICES_SUBSCRIPTION_STATUS_UPDATED') . ', ' . Date::toSql() . "\r\n";
         $subscription->notes .= $statusmsg ? $statusmsg . "\r\n" : '';
         $subscription->notes .= $message ? $message . "\r\n" : '';
         $subscription->notes .= '------------------------------' . "\r\n";
     }
     if (!$subscription->check()) {
         $this->setError($subscription->getError());
         $this->editTask($subscription);
         return;
     }
     if (!$subscription->store()) {
         $this->setError($subscription->getError());
         $this->editTask($subscription);
         return;
     }
     if ($email || $message) {
         // E-mail "from" info
         $from = array('email' => Config::get('mailfrom'), 'name' => Config::get('sitename') . ' ' . Lang::txt('COM_SERVICES_SUBSCRIPTIONS'));
         // start email message
         $subject = Lang::txt('COM_SERVICES_EMAIL_SUBJECT', $subscription->code);
         $emailbody = $subject . ':' . "\r\n";
         $emailbody .= Lang::txt('COM_SERVICES_SUBSCRIPTION_SERVICE') . ' - ' . $service->title . "\r\n";
         $emailbody .= '----------------------------------------------------------' . "\r\n";
         $emailbody .= $action != 'message' && $statusmsg ? $statusmsg : '';
         if ($message) {
             $emailbody .= "\r\n";
             $emailbody .= $message;
         }
         if (!Event::trigger('xmessage.onSendMessage', array('subscriptions_message', $subject, $emailbody, $from, array($subscription->uid), $this->_option))) {
             \Notify::error(Lang::txt('COM_SERVICES_ERROR_FAILED_TO_MESSAGE'));
         }
     }
     App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller, false), Lang::txt('COM_SERVICES_SUBSCRIPTION_SAVED') . ($statusmsg ? ' ' . $statusmsg : ''));
 }
Exemple #2
0
 /**
  * Check if a user has employer authorization
  *
  * @param      integer $admin If user is admin or not
  * @return     void
  */
 protected function _authorizeEmployer($admin = 0)
 {
     $emp = 0;
     $employer = new Employer($this->database);
     if ($admin) {
         $adminemp = $employer->isEmployer(User::get('id'), 1);
         if (!$adminemp) {
             // will require setup only once
             $subscription = new Subscription($this->database);
             $subscription->status = 1;
             $subscription->uid = 1;
             $subscription->units = 72;
             $subscription->serviceid = 1;
             $subscription->expires = Date::of(strtotime("+ 72 months"))->toSql();
             $subscription->added = Date::toSql();
             if (!$subscription->store()) {
                 throw new Exception($subscription->getError(), 500);
             }
             if (!$subscription->id) {
                 $subscription->checkin();
             }
             // make sure we have dummy admin employer account
             $live_site = rtrim(Request::base(), '/');
             $employer->uid = 1;
             $employer->subscriptionid = $subscription->id;
             $employer->companyName = Config::get('sitename');
             $employer->companyLocation = '';
             $employer->companyWebsite = $live_site;
             // save employer information
             if (!$employer->store()) {
                 throw new Exception($employer->getError(), 500);
             }
         }
     } else {
         $emp = $employer->isEmployer(User::get('id'));
     }
     $this->_emp = $emp;
 }