Example #1
0
 /**
  * Subscription confirmation
  *
  * @return     void
  */
 public function confirmTask()
 {
     // Login required
     if (User::isGuest()) {
         \Notify::warning(Lang::txt('COM_JOBS_PLEASE_LOGIN_ACCESS_EMPLOYER'));
         $this->login();
         return;
     }
     $uid = Request::getInt('uid', User::get('id'));
     if ($uid && User::get('id') != $uid && !$this->_admin) {
         // not authorized
         App::abort(403, Lang::txt('COM_JOBS_ALERTNOTAUTH'));
     }
     $uid = $uid ? $uid : User::get('id');
     // Get the member's info
     $profile = new \Hubzero\User\Profile();
     $profile->load($uid);
     // are we renewing?
     $subid = Request::getInt('subid', 0);
     $sconfig = Component::params('com_services');
     $autoapprove = $sconfig->get('autoapprove');
     // load Employer
     $employer = new Employer($this->database);
     if (!$employer->loadEmployer($uid)) {
         $employer = new Employer($this->database);
         $employer->uid = $uid;
     }
     $subid = $employer->subscriptionid ? $employer->subscriptionid : $subid;
     $employer->companyName = Request::getVar('companyName', $profile->get('organization'));
     $employer->companyLocation = Request::getVar('companyLocation', $profile->get('countryresident'));
     $employer->companyWebsite = Request::getVar('companyWebsite', $profile->get('url'));
     if (!$employer->companyName || !$employer->companyLocation) {
         $this->setError(Lang::txt('Please make sure all required fields are filled in'));
         // send to subscription page
         App::redirect(Route::url('index.php?option=com_jobs&task=subscribe'));
         return;
     }
     // do we have a subscription already?
     $subscription = new Subscription($this->database);
     if (!$subscription->load($subid)) {
         $subscription = new Subscription($this->database);
     }
     $serviceid = Request::getInt('serviceid', 0);
     // get service
     $service = new Service($this->database);
     if (!$serviceid or !$service->loadService('', $serviceid)) {
         throw new Exception(Lang::txt('COM_JOBS_ERROR_SUBSCRIPTION_CHOOSE_SERVICE'), 500);
     }
     $units = Request::getInt('units_' . $serviceid, 0);
     $contact = Request::getVar('contact', '');
     $total = $service->unitprice * $units;
     $now = Date::toSql();
     $new = 0;
     $credit = 0;
     $months = $units * $service->unitsize;
     $newexprire = Date::of(strtotime('+' . $months . ' month'))->toSql();
     // we got an order
     if ($units) {
         if ($total && !$contact) {
             // need contact info with payment
             throw new Exception(Lang::txt('COM_JOBS_ERROR_SUBSCRIPTION_NO_PHONE'), 500);
         }
         $newunitcost = $service->unitprice;
         if ($subid) {
             // get cost per unit (to compute required refund)
             $prevunitcost = $serviceid != $subscription->serviceid ? $service->getServiceCost($subscription->serviceid) : $newunitcost;
             $unitsleft = 0;
             $refund = 0;
             // we are upgrading / downgrading - or replacing cancelled subscription
             if ($serviceid != $subscription->serviceid or $subscription->status == 2) {
                 if ($prevunitcost > 0 && $subscription->status != 2) {
                     $unitsleft = $subscription->getRemaining('unit', $subscription, $service->maxunits, $service->unitsize);
                     $refund = $subscription->totalpaid > 0 && $subscription->totalpaid - $unitsleft * $prevunitcost < 0 ? $unitsleft * $prevunitcost : 0;
                     // calculate available credit - if upgrading
                     if ($newunitcost > $prevunitcost) {
                         $credit = 0;
                         // TBD
                     }
                 }
                 // cancel previous subscription & issue a refund if applicable
                 if ($subscription->status != 2) {
                     $subscription->cancelSubscription($subid, $refund, $unitsleft);
                 }
                 // enroll in new service
                 $subscription = new Subscription($this->database);
                 $new = 1;
             } else {
                 if ($subscription->expires > $now) {
                     // extending?
                     $subscription->status = $autoapprove && !$total ? 1 : $subscription->status;
                     $subscription->status = $subscription->status == 2 ? 1 : $subscription->status;
                     $subscription->units = $autoapprove && !$total ? $subscription->units + $units : $subscription->units;
                     $subscription->pendingunits = $autoapprove && !$total ? 0 : $units;
                     $subscription->pendingpayment = $autoapprove && !$total ? 0 : $units * $newunitcost;
                     $newexprire = Date::of(strtotime('+' . $subscription->units * $service->unitsize . ' month'))->toSql();
                     $subscription->expires = $newexprire;
                     $subscription->updated = $now;
                 } else {
                     // expired - treat like new
                     $new = 1;
                     $subscription->updated = $now;
                 }
             }
         } else {
             // this is a new subscription
             $new = 1;
         }
     }
     // this is a new subscription
     if ($new) {
         $subscription->added = $now;
         $subscription->status = $autoapprove && !$total ? 1 : 0;
         $subscription->units = $autoapprove && !$total ? $units : 0;
         $subscription->pendingunits = $autoapprove && !$total ? 0 : $units;
         $subscription->pendingpayment = $autoapprove && !$total ? 0 : $units * $newunitcost;
         $subscription->pendingpayment = $credit ? $subscription->pendingpayment < $credit : $subscription->pendingpayment;
         $subscription->pendingpayment = $subscription->pendingpayment < 0 ? 0 : $subscription->pendingpayment;
         $subscription->expires = $newexprire;
     }
     // save subscription information
     if ($units or $contact != $subscription->contact or !$subid or $serviceid != $subscription->serviceid) {
         $subscription->contact = $contact;
         $subscription->uid = $uid;
         $subscription->serviceid = $serviceid;
         if (!$subscription->id) {
             // get unique code
             $subscription->code = $subscription->generateCode();
         }
         if (!$subscription->check()) {
             throw new Exception($subscription->getError(), 500);
         }
         if (!$subscription->store()) {
             throw new Exception($subscription->getError(), 500);
         }
         if (!$subscription->id) {
             $subscription->checkin();
         }
     }
     // save employer information
     $employer->subscriptionid = $subscription->id;
     if (!$employer->store()) {
         throw new Exception($employer->getError(), 500);
     }
     $this->_msg = $subid ? Lang::txt('COM_JOBS_MSG_SUBSCRIPTION_PROCESSED') : Lang::txt('COM_JOBS_MSG_SUBSCRIPTION_ACCEPTED');
     if ($units) {
         $this->_msg .= $autoapprove && !$total ? ' ' . Lang::txt('COM_JOBS_MSG_SUBSCRIPTION_YOU_HAVE_ACCESS') . ' ' . $subscription->units . ' ' . Lang::txt('COM_JOBS_MONTHS') : ' ' . Lang::txt('COM_JOBS_MSG_SUBSCRIPTION_WE_WILL_CONTACT');
     }
     App::redirect(Route::url('index.php?option=com_jobs&task=dashboard'), $this->_msg);
     return;
 }