<?php
/**
* @package HikaShop for Joomla!
* @version 1.4.3
* @author hikashop.com
* @copyright (C) 2010 HIKARI SOFTWARE. All rights reserved.
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/
defined('_JEXEC') or die('Restricted access');
?>
<?php
class plgHikashopshippingManual extends JPlugin
{
function onShippingDisplay(&$order,&$dbrates,&$usable_rates,&$messages){
if(bccomp($order->weight,0,5)<=0) return true;
if(empty($dbrates)){
$messages['no_rates'] = JText::_('NO_SHIPPING_METHOD_FOUND');
}else{
$rates = array();
foreach($dbrates as $k => $rate){
if($rate->shipping_type=='manual' && !empty($rate->shipping_published)){
$rates[]=$rate;
}
}
if(empty($rates)){
$messages['no_rates'] = JText::_('NO_SHIPPING_METHOD_FOUND');
}else{
if(bccomp($order->total->prices[0]->price_value_with_tax,0,5)){
$notUsable = array();
foreach($rates as $k => $rate){
if(@$rate->shipping_params->shipping_min_price>$order->total->prices[0]->price_value_with_tax){
$notUsable[]=$k;
continue;
}
if(!empty($rate->shipping_params->shipping_max_price) && bccomp($rate->shipping_params->shipping_max_price,0,5) && @$rate->shipping_params->shipping_max_price<$order->total->prices[0]->price_value_with_tax){
$notUsable[]=$k;
continue;
}
if(isset($rate->shipping_params->shipping_percentage) && bccomp($rate->shipping_params->shipping_percentage,0,3)){
$rate->shipping_price = $rate->shipping_price + $order->total->prices[0]->price_value_with_tax*$rate->shipping_params->shipping_percentage/100;
}
}
foreach($notUsable as $item){
unset($rates[$item]);
}
}
if(empty($rates)){
$messages['order_total_too_low'] = JText::_('ORDER_TOTAL_TOO_LOW_FOR_SHIPPING_METHODS');
}else{
$notUsable = array();
$zoneClass=hikashop::get('class.zone');
$zones = $zoneClass->getOrderZones($order);
foreach($rates as $k => $rate){
if(!empty($rate->shipping_zone_namekey)){
if(!in_array($rate->shipping_zone_namekey,$zones)){
$notUsable[]=$k;
}
}
}
foreach($notUsable as $item){
unset($rates[$item]);
}
if(empty($rates)){
$messages['no_shipping_to_your_zone'] = JText::_('NO_SHIPPING_TO_YOUR_ZONE');
}else{
$volumeClass=hikashop::get('helper.volume');
$notUsable = array();
foreach($rates as $k => $rate){
if(bccomp($rate->shipping_params->shipping_max_volume,0,3)){
$rates[$k]->shipping_params->shipping_max_volume_orig = $rates[$k]->shipping_params->shipping_max_volume;
$rates[$k]->shipping_params->shipping_max_volume=$volumeClass->convert($rate->shipping_params->shipping_max_volume,@$rate->shipping_params->shipping_size_unit);
if($rates[$k]->shipping_params->shipping_max_volume<$order->volume){
$notUsable[]=$k;
}
}
if(bccomp((float)@$rate->shipping_params->shipping_min_volume,0,3)){
$rates[$k]->shipping_params->shipping_min_volume_orig = $rates[$k]->shipping_params->shipping_min_volume;
$rates[$k]->shipping_params->shipping_min_volume=$volumeClass->convert($rate->shipping_params->shipping_min_volume,@$rate->shipping_params->shipping_size_unit);
if($rates[$k]->shipping_params->shipping_min_volume>$order->volume){
$notUsable[]=$k;
}
}
}
foreach($notUsable as $item){
unset($rates[$item]);
}
if(empty($rates)){
$messages['items_volume_over_limit'] = JText::_('ITEMS_VOLUME_TOO_BIG_FOR_SHIPPING_METHODS');
}else{
$notUsable = array();
$weightClass=hikashop::get('helper.weight');
foreach($rates as $k => $rate){
if(bccomp($rate->shipping_params->shipping_max_weight,0,3)){
$rates[$k]->shipping_params->shipping_max_weight_orig = $rates[$k]->shipping_params->shipping_max_weight;
$rates[$k]->shipping_params->shipping_max_weight=$weightClass->convert($rate->shipping_params->shipping_max_weight,@$rate->shipping_params->shipping_weight_unit);
if($rates[$k]->shipping_params->shipping_max_weight<$order->weight){
$notUsable[]=$k;
}
}
if(bccomp((float)@$rate->shipping_params->shipping_min_weight,0,3)){
$rates[$k]->shipping_params->shipping_min_weight_orig = $rates[$k]->shipping_params->shipping_min_weight;
$rates[$k]->shipping_params->shipping_min_weight=$weightClass->convert($rate->shipping_params->shipping_min_weight,@$rate->shipping_params->shipping_weight_unit);
if($rates[$k]->shipping_params->shipping_min_weight>$order->weight){
$notUsable[]=$k;
}
}
}
foreach($notUsable as $item){
unset($rates[$item]);
}
if(empty($rates)){
$messages['items_weight_over_limit'] = JText::_('ITEMS_WEIGHT_TOO_BIG_FOR_SHIPPING_METHODS');
}else{
foreach($rates as $rate){
$usable_rates[]=$rate;
}
}
}
}
}
}
}
return true;
}
function onShippingConfiguration(&$elements){
$this->manual = JRequest::getCmd('name','manual');
if(JRequest::getCmd('subtask','')=='shipping_edit'){
$this->view = 'edit';
$this->currency = hikashop::get('type.currency');
$this->weight = hikashop::get('type.weight');
$this->volume = hikashop::get('type.volume');
$this->categoryType = hikashop::get('type.categorysub');
$this->categoryType->type = 'tax';
$this->categoryType->field = 'category_id';
$bar = & JToolBar::getInstance('toolbar');
JToolBarHelper::save();
JToolBarHelper::apply();
$bar->appendButton( 'Link', 'cancel', JText::_('HIKA_CANCEL'), hikashop::completeLink('plugins&plugin_type=shipping&task=edit&name='.$this->manual) );
JToolBarHelper::divider();
$bar->appendButton( 'Pophelp','shipping-manual-form');
hikashop::setTitle(JText::_('HIKASHOP_SHIPPING_METHOD'),'plugin','plugins&plugin_type=shipping&task=edit&name='.$this->manual.'&subtask=shipping_edit&shipping_id='.JRequest::getInt('shipping_id',0));
}else{
$this->dbrates =& $elements;
$this->noForm=true;
if(!empty($this->dbrates)){
$db = JFactory::getDBO();
$zones = array();
foreach($this->dbrates as $rate){
if(!empty($rate->shipping_zone_namekey)){
$zones[$rate->shipping_zone_namekey]=$db->Quote($rate->shipping_zone_namekey);
}
}
if(!empty($zones)){
$query = 'SELECT * FROM '.hikashop::table('zone').' WHERE zone_namekey IN ('.implode(',',$zones).');';
$db->setQuery($query);
$zones = $db->loadObjectList();
if(!empty($zones)){
foreach($this->dbrates as $k => $rate){
if(!empty($rate->shipping_zone_namekey)){
foreach($zones as $zone){
if($zone->zone_namekey==$rate->shipping_zone_namekey){
foreach(get_object_vars($zone) as $key => $val){
$this->dbrates[$k]->$key=$val;
}
break;
}
}
}
}
}
}
}
$bar = & JToolBar::getInstance('toolbar');
$bar->appendButton( 'Link','new',JText::_('NEW'),hikashop::completeLink('plugins&plugin_type=shipping&task=edit&name='.$this->manual.'&subtask=shipping_edit'));
JToolBarHelper::cancel();
JToolBarHelper::divider();
$bar->appendButton( 'Pophelp','shipping-manual-listing');
hikashop::setTitle(JText::_('HIKASHOP_SHIPPING_METHOD'),'plugin','plugins&plugin_type=shipping&task=edit&name='.$this->manual);
$this->toggleClass = hikashop::get('helper.toggle');
$this->currencyHelper = hikashop::get('class.currency');
jimport('joomla.html.pagination');
$this->pagination = new JPagination( count($this->dbrates), 0, false );
$this->order = null;
$this->order->ordering = true;
$this->order->orderUp = 'orderup';
$this->order->orderDown = 'orderdown';
$this->order->reverse = false;
$app =& JFactory::getApplication();
$app->setUserState( HIKASHOP_COMPONENT.'.shipping_plugin_type', $this->manual);
}
}
function onShippingConfigurationSave(&$elements){
return true;
}
function onShippingSave(&$cart,&$methods,&$shipping_id){
$usable_mehtods = array();
$errors = array();
$this->onShippingDisplay($cart,$methods,$usable_mehtods,$errors);
$shipping_id = (int) $shipping_id;
foreach($usable_mehtods as $k => $usable_method){
if($usable_method->shipping_id==$shipping_id){
return $usable_method;
}
}
return false;
}
function onAfterOrderConfirm(&$order,&$methods,$method_id){
return true;
}
}