$usps->setWeight(10, 5);
     $usps->setDestZip("20008");
     $usps->setOrigZip("10022");
 } else {
     //echo "###";
     $usps->setDestZip($destination_zip);
     $usps->setOrigZip($origin_zip);
     $usps->setWeight($shipping_pounds, $shipping_ounces);
 }
 $usps->setServer($uspsserver);
 $usps->setUserName($uspsid);
 $uspspaswd = "";
 $usps->setPass($uspspaswd);
 $usps->setService("All");
 //$usps->setContainer("Flat Rate Box");
 $usps->setCountry("USA");
 $usps->setMachinable("true");
 $usps->setSize("REGULAR");
 $uspsprice = array();
 $uspsprice = $usps->getPrice();
 $special_chars = "<sup>®</sup>";
 //echo "errrr".$uspsprice->error->description;
 if ($uspsprice->error->description == "") {
     foreach ($uspsprice->list as $eachItem) {
         // echo "\n item";echo $eachItem->mailservice."*****". $eachItem->rate;
         if ($eachItem->mailservice != "" && $eachItem->rate != "") {
             $eachItemServicename = str_replace($special_chars, "", $eachItem->mailservice);
             if (in_array($eachItemServicename, $uspsRequired_Service)) {
                 //echo "\n entered if 2....................";
                 $resultShippingArray[$product_option_id][] = array("service_name" => $eachItemServicename, "rate" => $eachItem->rate);
             }
 public static function getShippingPriceProduct($productid = 0, $productOptionId = 0, $userId = "", $destination_country = "", $destination_state = "", $destination_zip = "")
 {
     global $tableprefix;
     $adminCurrency = GetAdminCurrency();
     if (isCartEmpty($userId)) {
         clsGenerateShippingControl::getNoShippingControl($product_option_id);
         return 0;
     }
     if (get_magic_quotes_gpc()) {
         $_POST = array_map('stripslashes_deep', $_POST);
         $_GET = array_map('stripslashes_deep', $_GET);
         $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
     }
     if ($destination_country == "" && $destination_state == "" && $destination_zip == "") {
         $userAddress = clsGenerateShippingControl::getuserAddress($userId);
         if (count($userAddress) > 0) {
             $destination_state = $userAddress['state'];
             $destination_country = $userAddress['country'];
             $destination_zip = $userAddress['zip'];
         }
     }
     $uspsRequired_Service = array("Express Mail", "Express Mail Hold For Pickup", "Express Mail Sunday/Holiday Delivery", "Priority Mail", "Parcel Post", "Priority Mail Large Flat Rate Box", "\nPriority Mail Medium Flat Rate Box", "Priority Mail Small Flat Rate Box", "Priority Mail Flat Rate Envelope", "Media Mail", "Library Mail");
     //===================Getting Currency Type=========================
     $currency_attributes = displayCurrencyType();
     $flatrateEnabled = getSettingsValue('enable_flatrate');
     /*display the active template*/
     //set shipping gateway and doing basic settings
     include_once 'fedex/fedex.php';
     $sql = "select * from " . $tableprefix . "fedexsettings ";
     $rs = mysql_query($sql) or die(mysql_error());
     if (mysql_num_rows($rs) > 0) {
         $serviceName = array();
         $row = mysql_fetch_array($rs);
         $fedexaccno = stripslashes($row['FedexAccno']);
         $fedexmeterno = stripslashes($row['FedexMeterno']);
         $weightunits = $row['FedexWeight'];
         $server = $row['FedexServer'];
         $dropoff = stripslashes($row['FedexDropOff']);
         $packageTypes = stripslashes($row['FedexPackage']);
         $sName = $row['FedexService'];
         $currency = $row['FedexCurrency'];
         if ($_SESSION["sessShippingMethod"] == "FDXE") {
             $sarry = $fedexService;
             $shippingTitle = "Fedex Express Shipping";
         } else {
             $sarry = $fedexGroundService;
             $shippingTitle = "Fedex Express Ground Shipping";
         }
     }
     // end num rows check
     switch ($server) {
         case "test":
             $fedexserver = "https://gatewaybeta.fedex.com/GatewayDC";
             break;
         case "production":
             // change this to actual server when go on live
             $fedexserver = "https://gatewaybeta.fedex.com/GatewayDC";
             break;
         default:
             $fedexserver = "https://gatewaybeta.fedex.com/GatewayDC";
             break;
     }
     //// check enabled shipping is usps or not
     include_once "usps/xmlparser.php";
     include_once 'usps/usps.php';
     $shippingTitle = "USPS";
     $sql = "select * from " . $tableprefix . "usps_settings ";
     $rs = mysql_query($sql) or die(mysql_error());
     if (mysql_num_rows($rs) > 0) {
         $row = mysql_fetch_array($rs);
         $uspsid = $row['username'];
         $server = $row['server'];
     }
     // end num rows check
     switch ($server) {
         case "test":
             $uspsserver = "http://testing.shippingapis.com/ShippingAPITest.dll";
             break;
         case "production":
             //change this line when usps is on live
             $uspsserver = "http://Production.ShippingAPIs.com/ShippingAPI.dll";
             break;
         default:
             $uspsserver = "http://testing.shippingapis.com/ShippingAPITest.dll";
             break;
     }
     // end switch
     require_once "ups.php";
     include_once "shipping.php";
     include_once 'http_client.php';
     $shippingTitle = "UPS";
     // Shipping setting for UPS Shipping
     $select_shipping_settings = "SELECT * FROM " . $tableprefix . "shipping_settings";
     $result_shipping_settings = mysql_query($select_shipping_settings);
     if (mysql_num_rows($result_shipping_settings) > 0) {
         $rs_ship = mysql_fetch_array($result_shipping_settings);
         $ups_weight = $rs_ship['nShippingweight'];
         $ups_rate = $rs_ship['ups_rate'];
         $ups_container = $rs_ship['container_type'];
         $ups_type = $rs_ship['address_quote_method'];
     }
     $upsrate = new Ups();
     // select product details
     $select_cart_details = "SELECT prd.product_id,prd.product_name,po.product_option_id,po.product_length,po.product_width,po.product_height,crt.quantity,\n                       po.product_shipping_weight,art.country AS origin_country,art.state AS origin_state,art.zip AS origin_zip,art.artist_id,\n                       art.defaultflatrate,art.additionalflatrate, prd.eUps, prd.eUsps , prd.eFedex , prd.fFlatRateShippingAmount, prd.fAdditionalItemCost\n                       FROM " . $tableprefix . "cart crt\n                       INNER JOIN " . $tableprefix . "product_options po ON crt.product_option_id = po.product_option_id\n                       INNER JOIN " . $tableprefix . "products prd ON prd.product_id = po.product_id\n                       INNER JOIN " . $tableprefix . "artists art ON art.artist_id = prd.product_artist_id\n                       WHERE crt.user_id = '" . mysql_real_escape_string($userId) . "' AND po.product_option_id = '" . mysql_real_escape_string($productOptionId) . "' ORDER BY crt.cart_id";
     $result_cart_details = mysql_query($select_cart_details) or die(mysql_error());
     $_SESSION["SHIPPING_DETAILS"] = array();
     $_SESSION["SHIPPING_TOTAL"] = 0;
     $productArray = array();
     $resultShippingArray = array();
     if (mysql_num_rows($result_cart_details) == 0) {
         //as no products there is no point in staying in shipping options
         clsGenerateShippingControl::getNoShippingControl($product_option_id);
         return 0;
     } else {
         //Now we need to take shipping rates a products
         $cart_details_row = mysql_fetch_array($result_cart_details);
         $shipping_weight = trim($cart_details_row['product_shipping_weight']);
         $origin_country = $cart_details_row['origin_country'];
         $origin_state = $cart_details_row['origin_state'];
         $origin_zip = $cart_details_row['origin_zip'];
         $product_name = $cart_details_row['product_name'];
         $artist_id = $cart_details_row['artist_id'];
         $product_id = $cart_details_row['product_id'];
         $product_option_id = $cart_details_row['product_option_id'];
         $optionsStr = "";
         $optionVal = getProductOptionsName($product_option_id);
         if ($optionVal != "") {
             $optionsStr = "<br>" . $optionVal;
         }
         $productArray[$product_option_id] = $product_name . $optionsStr;
         $quantity = trim($cart_details_row['quantity']);
         $product_length = $cart_details_row['product_length'];
         $product_height = $cart_details_row['product_height'];
         $product_width = $cart_details_row['product_width'];
         // Take product shippings
         $productShippingArray = array();
         if ($cart_details_row['eUps'] == 'Y') {
             $productShippingArray[] = 'UPS';
         }
         if ($cart_details_row['eFedex'] == 'Y') {
             $productShippingArray[] = 'FDXG';
         }
         if ($cart_details_row['eUsps'] == 'Y') {
             $productShippingArray[] = 'USPS';
         }
         if ($cart_details_row['fFlatRateShippingAmount'] > 0) {
             $productShippingArray[] = 'Flat Rate';
             $FlatRateShippingAmount = $cart_details_row['fFlatRateShippingAmount'];
             $AdditionalItemCost = $cart_details_row['fAdditionalItemCost'];
         }
         /*  echo '<br>' . $origin_state;
             echo '<br>' . $origin_country;
             echo '<br>' . $origin_zip;
             echo '<br>' . $destination_state;
             echo '<br>' . $destination_country;
             echo '<br>' . $destination_zip;*/
         //print_r($productShippingArray);
         foreach ($productShippingArray as $key => $shippingMethod) {
             //  $shippingMethod = $value; // set product shipping
             //set for ups only
             $shippingMethod = trim($shippingMethod);
             //======================Getting rates for each product from shipping gateways-Santhosh=================================
             switch ($shippingMethod) {
                 case "FDXE":
                 case "FDXG":
                     $shippingMode = $shippingMethod;
                     if (is_array($sarry)) {
                         // check service is an array or not
                         foreach ($sarry as $serviceNames) {
                             $fedex_shipping_weight = $shipping_weight * $quantity;
                             list($service, $serviceName) = split('-', $serviceNames);
                             $fedex = new Fedex();
                             $fedex->setServer($fedexserver);
                             $fedex->setAccountNumber($fedexaccno);
                             //Get your own - this will not work...
                             $fedex->setMeterNumber($fedexmeterno);
                             //Get your own - this will not work...
                             $fedex->setCarrierCode($shippingMode);
                             $fedex->setDropoffType($dropoff);
                             $fedex->setService($service, $serviceName);
                             $fedex->setPackaging($packageTypes);
                             $fedex->setWeightUnits($weightunits);
                             $fedex->setWeight($fedex_shipping_weight);
                             $fedex->setOriginStateOrProvinceCode($origin_state);
                             $fedex->setOriginPostalCode($origin_zip);
                             $fedex->setOriginCountryCode($origin_country);
                             $fedex->setDestStateOrProvinceCode($destination_state);
                             $fedex->setDestPostalCode($destination_zip);
                             $fedex->setDestCountryCode($destination_country);
                             $fedex->setPayorType("SENDER");
                             $fedexprice = $fedex->getPrice();
                             $fedexrate[] = $fedexprice->price->rate;
                             $fedexerror[] = $fedexprice->error->description;
                             if ($fedexprice->error->description == "" && $fedexprice->serviceName != "" && $fedexprice->price->rate != "") {
                                 $resultShippingArray[$product_option_id][] = array("service_name" => $fedexprice->serviceName, "rate" => $fedexprice->price->rate);
                             }
                         }
                         // end foreach
                     }
                     // end array check
                     break;
                 case "USPS":
                     $usps_shipping_weight = $shipping_weight * $quantity;
                     if ($usps_shipping_weight < 1) {
                         $usps_shipping_weight = 1;
                     }
                     $usps_shipping_weightSplit = explode(".", $usps_shipping_weight);
                     $shipping_pounds = $usps_shipping_weightSplit[0];
                     $ouncelength = count_digit($usps_shipping_weightSplit[1]);
                     if ($ouncelength == 0) {
                         $shipping_ounces = 0;
                     }
                     if ($ouncelength == 1) {
                         $shipping_ounces = $usps_shipping_weightSplit[1] / 10 * 16;
                     }
                     if ($ouncelength == 2) {
                         $shipping_ounces = $usps_shipping_weightSplit[1] / 100 * 16;
                     }
                     if ($ouncelength == 3) {
                         $shipping_ounces = $usps_shipping_weightSplit[1] / 1000 * 16;
                     }
                     if ($ouncelength == 4) {
                         $shipping_ounces = $usps_shipping_weightSplit[1] / 10000 * 16;
                     }
                     $usps = new USPS();
                     if ($server == "test") {
                         $usps->setWeight(10, 5);
                         $usps->setDestZip("20008");
                         $usps->setOrigZip("10022");
                     } else {
                         $usps->setDestZip($destination_zip);
                         $usps->setOrigZip($origin_zip);
                         $usps->setWeight($shipping_pounds, $shipping_ounces);
                     }
                     $usps->setServer($uspsserver);
                     $usps->setUserName($uspsid);
                     $uspspaswd = "";
                     $usps->setPass($uspspaswd);
                     $usps->setService("All");
                     //$usps->setContainer("Flat Rate Box");
                     $usps->setCountry("USA");
                     $usps->setMachinable("true");
                     $usps->setSize("REGULAR");
                     $uspsprice = array();
                     $uspsprice = $usps->getPrice();
                     $special_chars = "&lt;sup&gt;&amp;reg;&lt;/sup&gt;";
                     if ($uspsprice->error->description == "") {
                         foreach ($uspsprice->list as $eachItem) {
                             if ($eachItem->mailservice != "" && $eachItem->rate != "") {
                                 $eachItemServicename = str_replace($special_chars, "", $eachItem->mailservice);
                                 if (in_array($eachItemServicename, $uspsRequired_Service)) {
                                     $resultShippingArray[$product_option_id][] = array("service_name" => $eachItemServicename, "rate" => $eachItem->rate);
                                 }
                             }
                         }
                     }
                     break;
                 case "UPS":
                     $ups_types = clsGenerateShippingControl::getupsShippingTypes();
                     $ups_shipping_weight = $shipping_weight * $quantity;
                     $ups_shipping_weight = $ups_shipping_weight < 0.1 ? 0.1 : $ups_shipping_weight;
                     foreach ($ups_types as $types => $ups) {
                         $upsrate->upsProduct($types);
                         // See upsProduct() function for codes
                         $upsrate->origin($origin_zip, $origin_country);
                         // Use ISO country codes!
                         $upsrate->dest($destination_zip, $destination_country);
                         // Use ISO country codes!
                         $upsrate->rate($ups_rate);
                         // See the rate() function for codes
                         $upsrate->container($ups_container);
                         // See the container() function for codes
                         $upsrate->weight(number_format($ups_shipping_weight, 2, ".", ""));
                         $upsrate->rescom($ups_type);
                         // See the rescom() function for codes
                         $quote = $upsrate->getQuote();
                         if ($quote != "error" && $ups != "" && $quote != "") {
                             $resultShippingArray[$product_option_id][] = array("service_name" => $ups, "rate" => $quote);
                         }
                         // end price empty check
                     }
                     //end foreach
                     break;
                 case "Flat Rate":
                     $flat_rate_name = "Flat Rate";
                     $first_Item_charge = $FlatRateShippingAmount;
                     $additional_Item_charge = $AdditionalItemCost;
                     $rate = $first_Item_charge + $additional_Item_charge * ($quantity - 1);
                     $resultShippingArray[$product_option_id][] = array("service_name" => stripslashes($flat_rate_name), "rate" => $rate);
                 default:
             }
             // if the rate array is empty for other shipping Options check whether
             // admin has enabled the flat rate shipping so that customer can still
             // buy with the flat option.
             $Selected_Shipping_options = $shippingTitle;
             if (empty($selectedShippingOption)) {
                 $selectedShippingOption = $shippingTitle;
             }
             $shipping_options_availabe = count($resultShippingArray[$product_option_id]);
         }
         // end while loop
     }
     return $resultShippingArray;
 }