function step4save()
 {
     $checkout = JModelLegacy::getInstance('checkout', 'jshop');
     $checkout->checkStep(4);
     $session = JFactory::getSession();
     $jshopConfig = JSFactory::getConfig();
     JPluginHelper::importPlugin('jshoppingcheckout');
     $dispatcher = JDispatcher::getInstance();
     $dispatcher->trigger('onBeforeSaveCheckoutStep4save', array());
     $cart = JModelLegacy::getInstance('cart', 'jshop');
     $cart->load();
     $user = JFactory::getUser();
     if ($user->id) {
         $adv_user = JTable::getInstance('userShop', 'jshop');
         $adv_user->load($user->id);
     } else {
         $adv_user = JSFactory::getUserShopGuest();
     }
     if ($adv_user->delivery_adress) {
         $id_country = $adv_user->d_country;
     } else {
         $id_country = $adv_user->country;
     }
     if (!$id_country) {
         $id_country = $jshopConfig->default_country;
     }
     $sh_pr_method_id = JRequest::getInt('sh_pr_method_id');
     $shipping_method_price = JTable::getInstance('shippingMethodPrice', 'jshop');
     $shipping_method_price->load($sh_pr_method_id);
     $sh_method = JTable::getInstance('shippingMethod', 'jshop');
     $sh_method->load($shipping_method_price->shipping_method_id);
     if (!$shipping_method_price->sh_pr_method_id) {
         JError::raiseWarning("", _JSHOP_ERROR_SHIPPING);
         $this->setRedirect(SEFLink('index.php?option=com_jshopping&controller=checkout&task=step4', 0, 1, $jshopConfig->use_ssl));
         return 0;
     }
     if (!$shipping_method_price->isCorrectMethodForCountry($id_country)) {
         JError::raiseWarning("", _JSHOP_ERROR_SHIPPING);
         $this->setRedirect(SEFLink('index.php?option=com_jshopping&controller=checkout&task=step4', 0, 1, $jshopConfig->use_ssl));
         return 0;
     }
     if (!$sh_method->shipping_id) {
         JError::raiseWarning("", _JSHOP_ERROR_SHIPPING);
         $this->setRedirect(SEFLink('index.php?option=com_jshopping&controller=checkout&task=step4', 0, 1, $jshopConfig->use_ssl));
         return 0;
     }
     $prices = $shipping_method_price->calculateSum($cart);
     $cart->setShippingId($sh_method->shipping_id);
     $cart->setShippingPrId($sh_pr_method_id);
     $cart->setShippingsDatas($prices, $shipping_method_price);
     if ($jshopConfig->show_delivery_date) {
         $delivery_date = '';
         $deliverytimedays = JSFactory::getAllDeliveryTimeDays();
         $day = $deliverytimedays[$shipping_method_price->delivery_times_id];
         if ($day) {
             $delivery_date = getCalculateDeliveryDay($day);
         } else {
             if ($jshopConfig->delivery_order_depends_delivery_product) {
                 $day = $cart->getDeliveryDaysProducts();
                 if ($day) {
                     $delivery_date = getCalculateDeliveryDay($day);
                 }
             }
         }
         $cart->setDeliveryDate($delivery_date);
     }
     //update payment price
     $payment_method_id = $cart->getPaymentId();
     if ($payment_method_id) {
         $paym_method = JTable::getInstance('paymentmethod', 'jshop');
         $paym_method->load($payment_method_id);
         $cart->setDisplayItem(1, 1);
         $paym_method->setCart($cart);
         $price = $paym_method->getPrice();
         $cart->setPaymentDatas($price, $paym_method);
     }
     $adv_user->saveTypeShipping($sh_method->shipping_id);
     $dispatcher->trigger('onAfterSaveCheckoutStep4', array(&$adv_user, &$sh_method, &$shipping_method_price, &$cart));
     if ($jshopConfig->step_4_3 && !$jshopConfig->without_payment) {
         $checkout->setMaxStep(3);
         $this->setRedirect(SEFLink('index.php?option=com_jshopping&controller=checkout&task=step3', 0, 1, $jshopConfig->use_ssl));
     } else {
         $checkout->setMaxStep(5);
         $this->setRedirect(SEFLink('index.php?option=com_jshopping&controller=checkout&task=step5', 0, 1, $jshopConfig->use_ssl));
     }
 }
 public function saveShippingData($sh_pr_method_id, &$allparams, &$adv_user)
 {
     $jshopConfig = JSFactory::getConfig();
     $cart = $this->getCart();
     $shipping_method_price = JSFactory::getTable('shippingMethodPrice', 'jshop');
     $shipping_method_price->load($sh_pr_method_id);
     $sh_method = JSFactory::getTable('shippingMethod', 'jshop');
     $sh_method->load($shipping_method_price->shipping_method_id);
     if (!$shipping_method_price->sh_pr_method_id) {
         $this->setError(_JSHOP_ERROR_SHIPPING);
         return 0;
     }
     $id_country = $this->getAnyIdCountry($adv_user);
     if (!$shipping_method_price->isCorrectMethodForCountry($id_country)) {
         $this->setError(_JSHOP_ERROR_SHIPPING);
         return 0;
     }
     if (!$sh_method->shipping_id) {
         $this->setError(_JSHOP_ERROR_SHIPPING);
         return 0;
     }
     $params = $allparams[$sh_method->shipping_id];
     if (isset($params)) {
         $cart->setShippingParams($params);
     } else {
         $cart->setShippingParams('');
     }
     $shippingForm = $sh_method->getShippingForm();
     if ($shippingForm && !$shippingForm->check($params, $sh_method)) {
         $this->setError($shippingForm->getErrorMessage());
         return 0;
     }
     $prices = $shipping_method_price->calculateSum($cart);
     $cart->setShippingId($sh_method->shipping_id);
     $cart->setShippingPrId($sh_pr_method_id);
     $cart->setShippingsDatas($prices, $shipping_method_price);
     if ($jshopConfig->show_delivery_date) {
         $delivery_date = '';
         $deliverytimedays = JSFactory::getAllDeliveryTimeDays();
         $day = $deliverytimedays[$shipping_method_price->delivery_times_id];
         if ($day) {
             $delivery_date = getCalculateDeliveryDay($day);
         } else {
             if ($jshopConfig->delivery_order_depends_delivery_product) {
                 $day = $cart->getDeliveryDaysProducts();
                 if ($day) {
                     $delivery_date = getCalculateDeliveryDay($day);
                 }
             }
         }
         $cart->setDeliveryDate($delivery_date);
     }
     //update payment price
     if ($cart->getPaymentId()) {
         $paym_method = $this->getPaymentMethod();
         $cart->setDisplayItem(1, 1);
         $paym_method->setCart($cart);
         $price = $paym_method->getPrice();
         $cart->setPaymentDatas($price, $paym_method);
     }
     $adv_user->saveTypeShipping($sh_method->shipping_id);
     $this->setActiveShippingMethod($sh_method);
     $this->setActiveShippingMethodPrice($shipping_method_price);
     return 1;
 }