setQuery("SHOW COLUMNS FROM `#__hikashop_product` LIKE 'product_subscription_id'"); $exist = $db->loadObjectList(); if(!$exist){ $db->setQuery("ALTER TABLE `#__hikashop_product` ADD `product_subscription_id` INT NOT NULL DEFAULT 0"); $db->query(); $db->setQuery("SELECT * FROM `#__hikashop_product` WHERE `subscription_level_id` != 0 "); $exist = $db->loadObjectList(); if($exist){ // copy the values $db->setQuery("UPDATE `#__hikashop_product` SET `product_subscription_id` = `subscription_level_id`"); $db->query(); } } } function onBeforeProductUpdate(& $element, & $do){ return $this->onBeforeProductCreate($element, $do); } function onAfterOrderCreate( &$order,&$send_email){ return $this->onAfterOrderUpdate( $order,$send_email); } function onAfterOrderUpdate( &$order,&$send_email){ $path = JPATH_ADMINISTRATOR.DS.'components'.DS.'com_akeebasubs'.DS.'akeebasubs.php'; if(!JFile::exists($path)){ $app = JFactory::getApplication(); if($app->isAdmin()) $app->enqueueMessage(JText::_('The Akeeba Subscriptions component is required for the Akeebasubs plugin.')); return false; } jimport('joomla.utilities.date'); $db = JFactory::getDBO(); jimport('joomla.filesystem.file'); $path = JPATH_LIBRARIES.'/fof/include.php'; if(JFile::exists($path) && !defined('FOF_INCLUDED')) require_once $path; else $path = JPATH_LIBRARIES.'/f0f/include.php'; if(JFile::exists($path) && !defined('F0F_INCLUDED')) require_once $path; //Order informations $class = hikashop_get('class.order'); $fullOrder = $class->loadFullOrder($order->order_id, false, false); //For each products foreach($fullOrder->products as $result){ $product_id = $result->product_id; //subscription_level_id from the product $productClass = hikashop_get('class.product'); $product = $productClass->get($product_id); if(is_object($product)){ if($product->product_type == 'variant' && $product->product_subscription_id == '0'){ $productClass = hikashop_get('class.product'); $product = $productClass->get($product->product_parent_id); } $level_id = $product->product_subscription_id; }else{ $level_id = 0; } if($level_id != 0){ if(class_exists('FOFModel')) $levels = FOFModel::getTmpInstance('Levels','AkeebasubsModel')->getItemList(); elseif(class_exists('F0FModel')) $levels = F0FModel::getTmpInstance('Levels','AkeebasubsModel')->getItemList(); else return false; $userClass = hikashop_get('class.user'); $user = $userClass->get($fullOrder->order_user_id); $user_id = $user->user_cms_id; //Subscription duration foreach($levels as $level){ if($level->akeebasubs_level_id == $level_id){ $level_duration = $level->duration; } } //know if the subs is a new sub or a sub add $query = 'SELECT * FROM '.hikashop_table('akeebasubs_subscriptions',false).' WHERE state = '.$db->quote('C').' AND user_id = '.(int)$user_id.' AND akeebasubs_level_id = '.(int)$level_id.' ORDER BY akeebasubs_subscription_id DESC'; $db->setQuery($query); $old = $db->loadObject(); if(($order->order_status == 'confirmed' && @$this->old->order_status != 'shipped') || ($order->order_status == 'shipped' && @$this->old->order_status != 'confirmed')){ //if new or update subs // Initialise the user data if(class_exists('FOFModel')) $list = FOFModel::getTmpInstance('Users','AkeebasubsModel') ->user_id($user_id) ->getItemList(); elseif(class_exists('F0FModel')) $list = F0FModel::getTmpInstance('Users','AkeebasubsModel') ->user_id($user_id) ->getItemList(); else return false; if(!count($list)) { $id = 0; } else { $thisUser = array_pop($list); $id = $thisUser->akeebasubs_user_id; } //format user date from Hikashop $isbusiness = 0; if($fullOrder->billing_address->address_company != ''){ $isbusiness = 1; } $country = ''; $query = 'SELECT zone_code_2 FROM '.hikashop_table('zone').' WHERE zone_name = '.$db->quote($fullOrder->billing_address->address_country).''; $db->setQuery($query); $country = $db->loadResult(); // Supply user data from the HikaShop order information $userData = array( 'akeebasubs_user_id' => $id, 'user_id' => $user_id, 'isbusiness' => $isbusiness, // 1 if it's a business, 0 otherwise 'businessname' => $fullOrder->billing_address->address_company, // Business name, e.g. 'AkeebaBackup.com' 'occupation' => '', ##// Busienss occupation, e.g. 'IT Services' 'vatnumber' => $fullOrder->billing_address->address_vat, // VAT number, WITHOUT the country prefix, e.g. 070298898 'viesregistered' => 0, // Set to 1 if it is a VIES-registered business. It's safe to leave 0. 'taxauthority' => '', // Leave blank; bot implemented 'address1' => $fullOrder->billing_address->address_street, // Address, first line 'address2' => '', // Address, second line 'city' => $fullOrder->billing_address->address_city, // City 'state' => '', ##// Two-letter state code, US and Canada only, e.g. 'CA' for California 'zip' => $fullOrder->billing_address->address_post_code, // ZIP/Postal Code 'country' => $country, // Two letter ISO country code, e.g. 'US' for the Unites States of America 'params' => json_encode(array()) // Leave it as it is here ); $timestamp = time(); if(!$old){ //New sub $up = time(); $up = (int)$up - date("Z"); $down = time() + (86400 * $level_duration * $result->order_product_quantity); $down = (int)$down - date("Z"); } else{ //add sub after the old sub if(strtotime($old->publish_down) < time()){ $up = time(); $up = (int)$up - date("Z"); $down = time() + (86400 * $level_duration * $result->order_product_quantity); $down = (int)$down - date("Z"); }else{ $up = strtotime($old->publish_down); $down = strtotime($old->publish_down) + (86400 * $level_duration * $result->order_product_quantity); } } $publish_up = hikashop_getDate($up,'Y-m-d H:i:s'); $publish_down = hikashop_getDate($down,'Y-m-d H:i:s'); $grossAmount = ($result->order_product_price + $result->order_product_tax) * $result->order_product_quantity; //$grossAmount2 = (/*$result->order_product_price*/$price + $result->order_product_tax) * $result->order_product_quantity; $taxPercent = 0; if(!empty($result->order_product_price) && hikashop_toFloat($result->order_product_price) != 0.0) $taxPercent = round($result->order_product_tax / $result->order_product_price * 100, 2); $now = hikashop_getDate(time(),'Y-m-d H:i:s'); // Setup the subscription data $subscriptionData = array( 'akeebasubs_subscription_id' => 0, // Must be present and 0 'user_id' => $user_id, 'akeebasubs_level_id' => $level_id, // foreign key to #__akeebasubs_levels, to the akeebasubs_level_id field 'publish_up' => $publish_up, // Valid From date (when the subscription is activated, required) 'publish_down' => $publish_down, // Valid To date (when the subscription is deactivated, required) 'created_on' => $now, // $jNow->toSql(), 'notes' => '', // Optional, leave empty 'params' => '{}', // Optional, leave empty 'enabled' => 0, // Required - SET TO 0 and let Akeeba Subscriptions handle the auto-activation 'processor' => 'hikashop', // Required 'processor_key' => md5(microtime() . $order->order_id), // Must be unique per subscription and processor. You can use the HikaShop order ID here 'state' => 'C', // Required. C = Payment completed, P = Payment pending, N = Payment not processed, X = Cancelled payment // keeperprancis //'akeebasubs_coupon_id' => $coupon_id, // end of keeperprancis 'net_amount' => $result->order_product_price * $result->order_product_quantity, // Paid amount, without taxes 'tax_amount' => $result->order_product_tax * $result->order_product_quantity, // Paid tax amount 'gross_amount' => $grossAmount, // net_amount + tax_amount, stored to speed up SUM operations 'tax_percent' => $taxPercent, // Tax percentage, e.g. "19.5" for 19.5% tax. Used to avoid dangling floating point values if we did tax_amount/net_amount to determine it 'affiliate_comission' => 0, // Leave 0 //'prediscount_amount' => $grossAmount2, // In your case, it's equal to gross_amount //'discount_amount' => $disc_amount * $result->order_product_quantity, //0.00, // Leave 0 ); if(class_exists('FOFModel')){ // Save the user record FOFModel::getTmpInstance('Users','AkeebasubsModel') ->setId($id) ->getItem() ->save($userData); // Save the subscription $subscription = FOFModel::getTmpInstance('Subscriptions','AkeebasubsModel') ->getTable(); }elseif(class_exists('F0FModel')){ // Save the user record F0FModel::getTmpInstance('Users','AkeebasubsModel') ->setId($id) ->getItem() ->save($userData); // Save the subscription $subscription = F0FModel::getTmpInstance('Subscriptions','AkeebasubsModel') ->getTable(); }else{ return false; } $subscription->reset(); $subscription->akeebasubs_subscription_id = 0; //$subscription->_dontCheckPaymentID = true; $result = $subscription->save($subscriptionData); } else if($order->order_status == 'cancelled' && $old){ $up = strtotime($old->publish_up); $down = strtotime($old->publish_down); $down = $down-(86400*$level_duration*$result->order_product_quantity); $publish_up = hikashop_getDate($up,'Y-m-d H:i:s'); $publish_down = hikashop_getDate($down,'Y-m-d H:i:s'); $query = 'UPDATE '.hikashop_table('akeebasubs_subscriptions', false).' SET enabled = 0, publish_down = '.$db->quote($publish_down).' WHERE akeebasubs_subscription_id = '.$old->akeebasubs_subscription_id.' LIMIT 1'; $db->setQuery($query); $db->query(); } } } } function onProductFormDisplay(& $product, & $html){ $path = JPATH_ADMINISTRATOR.DS.'components'.DS.'com_akeebasubs'.DS.'akeebasubs.php'; if(!JFile::exists($path)){ $app = JFactory::getApplication(); if($app->isAdmin()) $app->enqueueMessage(JText::_('The Akeeba Subscriptions component is required for the Akeebasubs plugin.')); return false; } jimport('joomla.filesystem.file'); $path = JPATH_LIBRARIES.'/fof/include.php'; if(JFile::exists($path) && !defined('FOF_INCLUDED')) require_once $path; else $path = JPATH_LIBRARIES.'/f0f/include.php'; if(JFile::exists($path) && !defined('F0F_INCLUDED')) require_once $path; if(class_exists('FOFModel')) $levels = FOFModel::getTmpInstance('Levels','AkeebasubsModel')->getItemList(); elseif(class_exists('F0FModel')) $levels = F0FModel::getTmpInstance('Levels','AkeebasubsModel')->getItemList(); else return false; if(empty($product->product_subscription_id))$product->product_subscription_id = 0; $default_id = 0; foreach($levels as $level){ if($level->akeebasubs_level_id == $product->product_subscription_id){ $default_title = $level->title; $default_id = $level->akeebasubs_level_id; } } ?> JRoute::_('index.php?option=com_akeebasubs&view=subscriptions'.$url_itemid),'level'=>1,'image'=>'subscription','text'=>JText::_('SUBSCRIPTIONS'),'description'=>''); return true; } function onCheckSubscription(&$subLevel,&$result){ $db = JFactory::getDBO(); $query = 'SELECT * FROM '.hikashop_table('akeebasubs_levels',false).' WHERE akeebasubs_level_id IN ('.implode(',',$subLevel).')'; $db->setQuery($query); $result = $db->loadObjectList(); return true; } function onCheckSubscritpionPlugin(&$name){ return $this->onCheckSubscriptionPlugin($name); } function onCheckSubscriptionPlugin(&$name){ $name = 'akeebasubs'; return true; } }