コード例 #1
0
ファイル: upscalculator.php プロジェクト: notzen/exponent-cms
 public function getRates($items)
 {
     global $order;
     // Require the main ups class and upsRate
     include_once BASE . 'external/ups-php/classes/class.ups.php';
     include_once BASE . 'external/ups-php/classes/class.upsRate.php';
     $upsConnect = new ups($this->configdata['accessnumber'], $this->configdata['username'], $this->configdata['password']);
     $upsConnect->setTemplatePath(BASE . 'external/ups-php/xml/');
     $upsConnect->setTestingMode($this->configdata['testmode']);
     // Change this to 0 for production
     $upsRate = new upsRate($upsConnect);
     $upsRate->request(array('Shop' => true));
     // set the address we will be shipping from.  this should be in the config data
     $upsRate->shipper($this->configdata['shipfrom']);
     // get the current shippingmethod and format the address for ups
     $currentmethod = $order->getCurrentShippingMethod();
     $upsRate->shipTo($this->formatAddress($currentmethod));
     // set the standard box sizes.
     $box_width = empty($this->configdata['default_width']) ? 0 : $this->configdata['default_width'];
     $box_height = empty($this->configdata['default_height']) ? 0 : $this->configdata['default_height'];
     $box_length = empty($this->configdata['default_length']) ? 0 : $this->configdata['default_length'];
     $box_volume = $box_height * $box_width * $box_length;
     // set some starting/default values
     $weight = 0;
     $volume = 0;
     $count = 0;
     $package_items = array();
     // loop each product in this shipment and create the packages
     $has_giftcard = false;
     foreach ($items->orderitem as $item) {
         for ($i = 0; $i < $item->quantity; $i++) {
             if (empty($item->product->no_shipping) && $item->product->requiresShipping == true) {
                 if ($item->product_type != 'giftcard') {
                     $lbs = empty($item->product->weight) ? $this->configdata['default_max_weight'] : $item->product->weight;
                     $width = empty($item->product->width) ? $this->configdata['default_width'] : $item->product->width;
                     $height = empty($item->product->height) ? $this->configdata['default_height'] : $item->product->height;
                     $length = empty($item->product->length) ? $this->configdata['default_length'] : $item->product->length;
                     $package_items[$count]->volume = $width * $length * $height;
                     $package_items[$count]->weight = $lbs;
                     $package_items[$count]->w = $width;
                     $package_items[$count]->h = $height;
                     $package_items[$count]->l = $length;
                     $package_items[$count]->name = $item->product->title;
                     $count += 1;
                 } else {
                     $has_giftcard = true;
                 }
             }
         }
     }
     // kludge for the giftcard shipping
     if (count($package_items) == 0 && $has_giftcard) {
         $rates = array("03" => array("id" => "03", "title" => "UPS Ground", "cost" => 5.0), "02" => array("id" => "02", "title" => "UPS Second Day Air", "cost" => 10.0), "01" => array("id" => "01", "title" => "UPS Next Day Air", "cost" => 20.0));
         return $rates;
     }
     // sort the items by volume
     $package_items = expSorter::sort(array('array' => $package_items, 'sortby' => 'volume', 'order' => 'DESC'));
     // loop over all the items and try to put them into packages in a semi-intelligent manner
     // we have sorted the list of items from biggest to smallest.  Items with a volume larger than
     // our standard box will generate a package with the dimensions set to the size of the item.
     // otherwise we just keep stuffing items in the current package until we can't find anymore that will
     // fit.  Once that happens we close that package and start a new one...repeating until we are out of items
     $space_left = $box_volume;
     $total_weight = 0;
     while (!empty($package_items)) {
         $no_more_room = true;
         $used = array();
         foreach ($package_items as $idx => $pi) {
             if ($pi->volume > $box_volume) {
                 #                    echo $pi->name."is too big for standard box <br>";
                 #                    eDebug('created OVERSIZED package with weight of '.$pi->weight);
                 #                    eDebug('dimensions: height: '.$pi->h." width: ".$pi->w." length: ".$pi->l);
                 #                    echo "<hr>";
                 $weight = $pi->weight > 1 ? $pi->weight : 1;
                 $upsRate->package(array('description' => 'shipment', 'weight' => $weight, 'code' => '02', 'length' => $pi->l, 'width' => $pi->w, 'height' => $pi->h));
                 $used[] = $idx;
                 $no_more_room = false;
             } elseif ($pi->volume <= $space_left) {
                 $space_left = $space_left - $pi->volume;
                 $total_weight += $pi->weight;
                 #                    echo "Adding ".$pi->name."<br>";
                 #                    echo "Space left in current box: ".$space_left."<br>";
                 $no_more_room = false;
                 $used[] = $idx;
             }
         }
         // remove the used items from the array so they wont be there on the next go around.
         foreach ($used as $idx) {
             unset($package_items[$idx]);
         }
         // if there is no more room left on the current package or we are out of items then
         // add the package to the shippment.
         if ($no_more_room || empty($package_items) && $total_weight > 0) {
             $total_weight = $total_weight > 1 ? $total_weight : 1;
             #                eDebug('created standard sized package with weight of '.$total_weight);
             #                echo "<hr>";
             $upsRate->package(array('description' => 'shipment', 'weight' => $total_weight, 'code' => '02', 'length' => $box_length, 'width' => $box_width, 'height' => $box_height));
             $space_left = $box_volume;
             $total_weight = 0;
         }
     }
     $upsRate->shipment(array('description' => 'my description', 'serviceType' => '03'));
     $rateFromUPS = $upsRate->sendRateRequest();
     $handling = empty($has_giftcard) ? 0 : 5;
     if ($rateFromUPS['RatingServiceSelectionResponse']['Response']['ResponseStatusCode']['VALUE'] == 1) {
         $rates = array();
         $available_methods = $this->availableMethods();
         foreach ($rateFromUPS['RatingServiceSelectionResponse']['RatedShipment'] as $rate) {
             if (array_key_exists($rate['Service']['Code']['VALUE'], $available_methods)) {
                 $rates[$rate['Service']['Code']['VALUE']] = $rate['TotalCharges']['MonetaryValue']['VALUE'];
                 $rates[$rate['Service']['Code']['VALUE']] = array('id' => $rate['Service']['Code']['VALUE'], 'title' => $this->shippingmethods[$rate['Service']['Code']['VALUE']], 'cost' => $rate['TotalCharges']['MonetaryValue']['VALUE'] + $handling);
             }
         }
         return $rates;
     } else {
         return $rateFromUPS['RatingServiceSelectionResponse']['Response']['Error']['ErrorDescription']['VALUE'];
     }
 }
コード例 #2
0
 public function getUPSresponse($cart, $method)
 {
     $vendorId = $this->vendor;
     $vendorModel = VmModel::getModel('vendor');
     $vendorFields = $vendorModel->getVendorAddressFields();
     $weight = 0;
     foreach ($cart->products as $product) {
         (double) ($product_weight = ShopFunctions::convertWeigthUnit($product->product_weight, $product->product_weight_uom, "LB"));
         $weight += $product_weight * $product->quantity;
     }
     if ($weight == 0) {
         JFactory::getApplication()->enqueueMessage("UPS Error: Product Weight not found", "error");
         $this->clear();
         $mainframe = JFactory::getApplication();
         $redirectMsg = "UPS Error: Product Weight not found";
         $mainframe->redirect(JRoute::_('index.php?option=com_virtuemart&view=user&task=editaddresscart&addrtype=BT'), $redirectMsg);
         return FALSE;
     }
     $accessNumber = trim($method->api);
     $username = trim($method->username);
     $password = trim($method->password);
     $upsConnect = new ups($accessNumber, $username, $password);
     $upsConnect->setTemplatePath(JPATH_ROOT . '/plugins/vmshipment/jibon_ups/ups/xml/');
     $upsConnect->setTestingMode($method->mood);
     // Change this to 0 for production
     $upsRate = new upsRate($upsConnect);
     $upsRate->request(array('Shop' => true));
     $upsRate->shipper(array('name' => $vendorFields['fields']['first_name']['value'] . " " . $vendorFields['fields']['last_name']['value'], 'phone' => $vendorFields['fields']['phone_1']['value'], 'shipperNumber' => '', 'address1' => $vendorFields['fields']['address_1']['value'], 'address2' => '', 'address3' => '', 'city' => $vendorFields['fields']['city']['value'], 'state' => $vendorFields['fields']['virtuemart_state_id']['state_2_code'], 'postalCode' => $vendorFields['fields']['zip']['value'], 'country' => $vendorFields['fields']['virtuemart_country_id']['country_2_code']));
     if (!is_array($cart->BT)) {
         JFactory::getApplication()->enqueueMessage("UPS Error: Please put valid shipping information !!", "error");
         return false;
     }
     if (is_array($cart->ST)) {
         $upsRate->shipTo(array('companyName' => $cart->ST['company'], 'attentionName' => $cart->ST['first_name'] . " " . $cart->ST['last_name'], 'phone' => $cart->ST['phone_1'], 'address1' => $cart->ST['address_1'], 'address2' => '', 'address3' => '', 'city' => $cart->ST['city'], 'state' => ShopFunctions::getStateByID($cart->ST['virtuemart_state_id'], "state_2_code"), 'postalCode' => $cart->ST['zip'], 'countryCode' => ShopFunctions::getCountryByID($cart->ST['virtuemart_country_id'], "country_2_code")));
     } else {
         $upsRate->shipTo(array('companyName' => $cart->BT['company'], 'attentionName' => $cart->BT['first_name'] . " " . $cart->BT['last_name'], 'phone' => $cart->BT['phone_1'], 'address1' => $cart->BT['address_1'], 'address2' => '', 'address3' => '', 'city' => $cart->BT['city'], 'state' => ShopFunctions::getStateByID($cart->BT['virtuemart_state_id'], "state_2_code"), 'postalCode' => $cart->BT['zip'], 'countryCode' => ShopFunctions::getCountryByID($cart->BT['virtuemart_country_id'], "country_2_code")));
     }
     $upsRate->package(array('description' => 'my description', 'weight' => $weight, 'code' => '02', 'length' => 0, 'width' => 0, 'height' => 0));
     $upsRate->shipment(array('description' => 'my description', 'serviceType' => '03'));
     //service type
     $upsRate->sendRateRequest();
     $this->UPSresponse = $upsRate->returnResponseArray();
     if (!empty($this->UPSresponse["RatingServiceSelectionResponse"]["Response"]["Error"]["ErrorCode"]) or empty($this->UPSresponse)) {
         $this->ups_rate = "";
         $this->ups_service_name = "";
         $this->ups_service_id = "";
         $this->status = 0;
         $this->loadPost($method->virtuemart_shipmentmethod_id);
         JFactory::getApplication()->enqueueMessage("UPS Error: " . $this->UPSresponse["RatingServiceSelectionResponse"]["Response"]["Error"]["ErrorDescription"]["VALUE"], "error");
     }
     $currency = CurrencyDisplay::getInstance();
     if ($this->UPSresponse['RatingServiceSelectionResponse']['RatedShipment']) {
         foreach ($this->UPSresponse['RatingServiceSelectionResponse']['RatedShipment'] as $rate) {
             if ($this->ups_service_id === $rate["Service"]["Code"]["VALUE"]) {
                 $this->ups_rate = $currency->convertCurrencyTo("USD", $rate["TotalCharges"]["MonetaryValue"]["VALUE"]);
                 $this->ups_service_name = $this->getServiceName($rate["Service"]["Code"]["VALUE"]);
                 $this->ups_service_id = $rate["Service"]["Code"]["VALUE"];
                 $this->save();
                 break;
             }
         }
     }
     return $this->UPSresponse;
 }