Example #1
0
 /**
  * Imports a subscription for a given user, using data coming from a CSV file
  *
  * @param   int     $userid     Joomla user_id of the imported user
  *
  * @return  bool    True if successful
  */
 protected function importSubscription($userid)
 {
     static $levelCache = array();
     JLoader::import('joomla.application.component.helper');
     if (!class_exists('AkeebasubsHelperFormat')) {
         require_once JPATH_ROOT . '/administrator/components/com_akeebasubs/helpers/format.php';
     }
     if (!$levelCache) {
         $levelCache = F0FModel::getTmpInstance('Levels', 'AkeebasubsModel')->createTitleLookup();
     }
     $app = JFactory::getApplication();
     $level = $levelCache[strtoupper($this->getCsvData('subscription_level'))];
     $publish_up = AkeebasubsHelperFormat::checkDateFormat($this->getCsvData('publish_up'));
     if (!$publish_up) {
         return false;
     }
     // Publish down
     if ($this->getCsvData('publish_down')) {
         $publish_down = AkeebasubsHelperFormat::checkDateFormat($this->getCsvData('publish_down'));
         if (!$publish_down) {
             return false;
         }
     } else {
         $temp = strtotime('+' . $level->duration . ' days', $publish_up->toUnix());
         $publish_down = new JDate($temp);
     }
     // Created on
     if ($this->getCsvData('created_on')) {
         $created_on = AkeebasubsHelperFormat::checkDateFormat($this->getCsvData('created_on'));
         if (!$created_on) {
             return false;
         }
     } else {
         $created_on = clone $publish_up;
     }
     $sub = clone F0FModel::getTmpInstance('Subscriptions', 'AkeebasubsModel')->getTable();
     $randomString = JUserHelper::genRandomPassword();
     if (version_compare(JVERSION, '3.2', 'ge')) {
         $hash = JApplication::getHash($randomString);
     } else {
         $hash = JFactory::getApplication()->getHash($randomString);
     }
     $bind['user_id'] = $userid;
     $bind['akeebasubs_level_id'] = $level->akeebasubs_level_id;
     $bind['publish_up'] = $publish_up->toSql();
     $bind['publish_down'] = $publish_down->toSql();
     $bind['enabled'] = $this->getCsvData('enabled', 1);
     $bind['processor'] = $this->getCsvData('processor', 'import');
     $bind['processor_key'] = $this->getCsvData('processor_key', md5(microtime() . $hash));
     $bind['state'] = $this->getCsvData('status', 'C');
     $bind['net_amount'] = $this->getCsvData('net_amount', 0);
     $bind['tax_amount'] = $this->getCsvData('tax_amount', 0);
     $bind['gross_amount'] = $this->getCsvData('gross_amount', $bind['net_amount'] + $bind['tax_amount']);
     $bind['recurring_amount'] = $this->getCsvData('recurring_amount', $bind['gross_amount']);
     $bind['tax_percent'] = $this->getCsvData('tax_percent', 100 * $bind['tax_amount'] / $bind['net_amount']);
     $bind['created_on'] = $created_on->toSql();
     $bind['prediscount_amount'] = $this->getCsvData('prediscount_amount', $bind['gross_amount']);
     $bind['discount_amount'] = $this->getCsvData('discount_amount', 0);
     $bind['contact_flag'] = $this->getCsvData('contact_flag', 0);
     return $sub->save($bind);
 }