Esempio n. 1
0
 /**
  * Method to get rates from the USPS shipping API
  *
  * @param array $address
  * @return array rates array
  */
 private function getRates($address)
 {
     $rates = array();
     $status = true;
     $shipping_status = false;
     //first check if shippable items are in cart
     JModelLegacy::addIncludePath(JPATH_SITE . '/components/com_j2store/models');
     $model = JModelLegacy::getInstance('Mycart', 'J2StoreModel');
     $products = $model->getDataNew();
     foreach ($products as $product) {
         if ($product['shipping']) {
             $shipping_status = true;
         }
     }
     if ($shipping_status === false) {
         return $rates;
     }
     $currencyObject = J2StoreFactory::getCurrencyObject();
     $store_address = J2StoreHelperCart::getStoreAddress();
     $domestic_services = $this->params->get('domestic_services');
     $inernational_services = $this->params->get('international_services');
     $quote_data = array();
     $method_data = array();
     $cart_weight_total = J2StoreHelperCart::getWeight();
     $weightObject = J2StoreFactory::getWeightObject();
     $weight = $weightObject->convert($cart_weight_total, $store_address->config_weight_class_id, $this->params->get('usps_weight_class_id'));
     $weight = $weight < 0.1 ? 0.1 : $weight;
     $pounds = floor($weight);
     $ounces = round(16 * ($weight - $pounds), 2);
     // max 5 digits
     $postcode = str_replace(' ', '', $address['postal_code']);
     //get country data
     $countryObject = $this->getCountry($address['country_id']);
     if ($countryObject->country_isocode_2 == 'US') {
         $xml = '<RateV4Request USERID="' . $this->usps_username . '">';
         $xml .= '	<Package ID="1">';
         $xml .= '		<Service>ALL</Service>';
         $xml .= '		<ZipOrigination>' . substr($this->params->get('usps_postcode'), 0, 5) . '</ZipOrigination>';
         $xml .= '		<ZipDestination>' . substr($postcode, 0, 5) . '</ZipDestination>';
         $xml .= '		<Pounds>' . $pounds . '</Pounds>';
         $xml .= '		<Ounces>' . $ounces . '</Ounces>';
         // Prevent common size mismatch error from USPS (Size cannot be Regular if Container is Rectangular for some reason)
         if ($this->params->get('usps_container') == 'RECTANGULAR' && $this->params->get('usps_size') == 'REGULAR') {
             $this->params->set('usps_container', 'VARIABLE');
         }
         $xml .= '		<Container>' . $this->params->get('usps_container') . '</Container>';
         $xml .= '		<Size>' . $this->params->get('usps_size') . '</Size>';
         $xml .= '		<Width>' . $this->params->get('usps_width') . '</Width>';
         $xml .= '		<Length>' . $this->params->get('usps_length') . '</Length>';
         $xml .= '		<Height>' . $this->params->get('usps_height') . '</Height>';
         // Calculate girth based on usps calculation
         $xml .= '		<Girth>' . round((double) $this->params->get('usps_length') + (double) $this->params->get('usps_width') * 2 + (double) $this->params->get('usps_height') * 2, 1) . '</Girth>';
         $xml .= '		<Machinable>' . ($this->params->get('usps_machinable') ? 'true' : 'false') . '</Machinable>';
         $xml .= '	</Package>';
         $xml .= '</RateV4Request>';
         $request = 'API=RateV4&XML=' . urlencode($xml);
     } else {
         $countries = $this->getCountries();
         if (isset($countries[$countryObject->country_isocode_2])) {
             $xml = '<IntlRateV2Request USERID="' . $this->usps_username . '">';
             $xml .= '	<Package ID="1">';
             $xml .= '		<Pounds>' . $pounds . '</Pounds>';
             $xml .= '		<Ounces>' . $ounces . '</Ounces>';
             $xml .= '		<MailType>All</MailType>';
             $xml .= '		<GXG>';
             $xml .= '		  <POBoxFlag>N</POBoxFlag>';
             $xml .= '		  <GiftFlag>N</GiftFlag>';
             $xml .= '		</GXG>';
             $xml .= '		<ValueOfContents>' . J2StoreHelperCart::getSubTotal() . '</ValueOfContents>';
             $xml .= '		<Country>' . $countries[$countryObject->country_isocode_2] . '</Country>';
             // Intl only supports RECT and NONRECT
             if ($this->params->get('usps_container') == 'VARIABLE') {
                 $this->params->set('usps_container', 'NONRECTANGULAR');
             }
             $xml .= '		<Container>' . $this->params->get('usps_container') . '</Container>';
             $xml .= '		<Size>' . $this->params->get('usps_size') . '</Size>';
             $xml .= '		<Width>' . $this->params->get('usps_width') . '</Width>';
             $xml .= '		<Length>' . $this->params->get('usps_length') . '</Length>';
             $xml .= '		<Height>' . $this->params->get('usps_height') . '</Height>';
             $xml .= '		<Girth>' . $this->params->get('usps_girth') . '</Girth>';
             $xml .= '		<CommercialFlag>N</CommercialFlag>';
             $xml .= '	</Package>';
             $xml .= '</IntlRateV2Request>';
             $request = 'API=IntlRateV2&XML=' . urlencode($xml);
         } else {
             $status = false;
         }
     }
     if ($status) {
         $result = $this->_sendRequest($request);
         $handling = $this->params->get('handling', '0');
         if ($result) {
             if ($this->params->get('show_debug')) {
                 $this->_log("USPS DATA SENT: " . urldecode($request));
                 $this->_log("USPS DATA RECV: " . $result);
             }
             $dom = new DOMDocument('1.0', 'UTF-8');
             $dom->loadXml($result);
             $rate_response = $dom->getElementsByTagName('RateV4Response')->item(0);
             $intl_rate_response = $dom->getElementsByTagName('IntlRateV2Response')->item(0);
             $error = $dom->getElementsByTagName('Error')->item(0);
             $firstclasses = array('First-Class Mail Parcel', 'First-Class Mail Large Envelope', 'First-Class Mail Letter', 'First-Class Mail Postcards');
             if ($rate_response || $intl_rate_response) {
                 if ($countryObject->country_isocode_2 == 'US') {
                     $allowed = array(0, 1, 2, 3, 4, 5, 6, 7, 12, 13, 16, 17, 18, 19, 22, 23, 25, 27, 28, 30, 31, 55, 56, 62, 63);
                     $package = $rate_response->getElementsByTagName('Package')->item(0);
                     $postages = $package->getElementsByTagName('Postage');
                     if ($postages->length) {
                         foreach ($postages as $postage) {
                             $classid = $postage->getAttribute('CLASSID');
                             if (in_array($classid, $allowed)) {
                                 if ($classid == '0') {
                                     $mailservice = $postage->getElementsByTagName('MailService')->item(0)->nodeValue;
                                     foreach ($firstclasses as $k => $firstclass) {
                                         if ($firstclass == $mailservice) {
                                             $classid = $classid . $k;
                                             break;
                                         }
                                     }
                                     if (in_array('usps_domestic_' . $classid, $domestic_services)) {
                                         $cost = $postage->getElementsByTagName('Rate')->item(0)->nodeValue;
                                         $price = $currencyObject->convert($cost, 'USD', $store_address->config_currency);
                                         $rate = array();
                                         $rate['name'] = $postage->getElementsByTagName('MailService')->item(0)->nodeValue;
                                         $rate['code'] = 'usps.' . $classid;
                                         $rate['price'] = $price;
                                         $rate['extra'] = (double) $handling;
                                         $rate['total'] = $price;
                                         $rate['tax'] = "0.00";
                                         $rate['element'] = $this->_element;
                                         $rates[] = $rate;
                                     }
                                 } elseif (in_array('usps_domestic_' . $classid, $domestic_services)) {
                                     $cost = $postage->getElementsByTagName('Rate')->item(0)->nodeValue;
                                     $price = $currencyObject->convert($cost, 'USD', $store_address->config_currency);
                                     $rate = array();
                                     $rate['name'] = $postage->getElementsByTagName('MailService')->item(0)->nodeValue;
                                     $rate['code'] = 'usps.' . $classid;
                                     $rate['price'] = $price;
                                     $rate['extra'] = (double) $handling;
                                     $rate['total'] = $price;
                                     $rate['tax'] = "0.00";
                                     $rate['element'] = $this->_element;
                                     $rates[] = $rate;
                                 }
                             }
                         }
                     } else {
                         $error = $package->getElementsByTagName('Error')->item(0);
                         $method_data = array('code' => 'usps', 'title' => JText::_('usps_error_text'), 'quote' => $quote_data, 'error' => $error->getElementsByTagName('Description')->item(0)->nodeValue);
                     }
                 } else {
                     $allowed = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27);
                     $package = $intl_rate_response->getElementsByTagName('Package')->item(0);
                     $services = $package->getElementsByTagName('Service');
                     if (isset($inernational_services) && !empty($inernational_services)) {
                         foreach ($services as $service) {
                             $id = $service->getAttribute('ID');
                             if (in_array($id, $allowed) && in_array('usps_international_' . $id, $inernational_services)) {
                                 $title = $service->getElementsByTagName('SvcDescription')->item(0)->nodeValue;
                                 if ($this->params->get('usps_display_time')) {
                                     $title .= ' (' . JText::_('usps_text_eta') . ' ' . $service->getElementsByTagName('SvcCommitments')->item(0)->nodeValue . ')';
                                 }
                                 $cost = $service->getElementsByTagName('Postage')->item(0)->nodeValue;
                                 $price = $currencyObject->convert($cost, 'USD', $store_address->config_currency);
                                 $rate = array();
                                 $rate['name'] = $title;
                                 $rate['code'] = 'usps.' . $id;
                                 $rate['price'] = $price;
                                 $rate['extra'] = (double) $handling;
                                 $rate['total'] = $price;
                                 $rate['tax'] = "0.00";
                                 $rate['element'] = $this->_element;
                                 $rates[] = $rate;
                             }
                         }
                     }
                 }
             } elseif ($error) {
                 $method_data = array('code' => 'usps', 'title' => JText::_('usps_error_text'), 'quote' => $quote_data, 'error' => $error->getElementsByTagName('Description')->item(0)->nodeValue);
             }
         }
     }
     if (count($rates)) {
         //if the shipping is taxable, calculate it here.
         $tax_class_id = $this->params->get('usps_tax_class_id', '');
         if ($tax_class_id) {
             $j2tax = new J2StoreTax();
             $newRates = array();
             foreach ($rates as $rate) {
                 $newRate = array();
                 $newRate['name'] = JText::_($rate['name']);
                 $newRate['code'] = $rate['code'];
                 $newRate['price'] = $rate['price'];
                 $newRate['extra'] = $rate['extra'];
                 $shipping_method_tax_total = $j2tax->getTax($newRate['price'] + $newRate['extra'], $tax_class_id);
                 $newRate['tax'] = round($shipping_method_tax_total, 2);
                 $newRate['total'] = $rate['total'] + $newRate['tax'];
                 $newRate['element'] = $rate['element'];
                 $newRates[] = $newRate;
             }
             unset($rates);
             $rates = $newRates;
         }
     }
     return $rates;
 }
Esempio n. 2
0
 /**
  *
  * @param $element
  * @param $values
  */
 function onJ2StoreGetShippingRates($element, $order)
 {
     // Check if this is the right plugin
     if (!$this->_isMe($element)) {
         return null;
     }
     $vars = array();
     $this->includeJ2StoreTables();
     $this->includeCustomTables();
     $this->includeCustomModel('ShippingMethods');
     $this->includeCustomModel('ShippingRates');
     //set the address
     $order->setAddress();
     $geozones_taxes = array();
     //	$geozones_taxes = $order->getBillingGeoZones();
     $geozones = $order->getShippingGeoZones();
     $gz_array = array();
     foreach ($geozones as $geozone) {
         $gz_array[] = $geozone->geozone_id;
     }
     if (!isset($order->order_subtotal)) {
         $subtotal = J2StoreHelperCart::getSubTotal();
     } else {
         $subtotal = $order->order_subtotal;
     }
     $rates = array();
     $model = JModelLegacy::getInstance('ShippingMethods', 'J2StoreModel');
     $model->setState('filter_enabled', '1');
     $model->setState('filter_subtotal', $subtotal);
     if ($methods = $model->getList()) {
         foreach ($methods as $method) {
             //check if there is an override
             if ($method->address_override == 'store') {
                 //there is an override.
                 //so set the shipping address to store and get the geozones afresh
                 $order->setAddress('store');
             } else {
                 $order->setAddress();
             }
             $geozones = $order->getShippingGeoZones();
             $gz_array = array();
             foreach ($geozones as $geozone) {
                 $gz_array[] = $geozone->geozone_id;
             }
             // filter the list of methods according to geozone
             $ratemodel = JModelLegacy::getInstance('ShippingRates', 'J2StoreModel');
             $ratemodel->setState('filter_shippingmethod', $method->shipping_method_id);
             $ratemodel->setState('filter_geozones', $gz_array);
             if ($ratesexist = $ratemodel->getList()) {
                 $total = $this->getTotal($method->shipping_method_id, $geozones, $order->getItems(), $geozones_taxes);
                 if ($total) {
                     $total->shipping_method_type = $method->shipping_method_type;
                     $rates[] = $total;
                 }
             }
         }
     }
     $i = 0;
     foreach ($rates as $rate) {
         $vars[$i]['element'] = $this->_element;
         $vars[$i]['name'] = addslashes(JText::_($rate->shipping_method_name));
         $vars[$i]['type'] = $rate->shipping_method_type;
         $vars[$i]['code'] = $rate->shipping_rate_id;
         $vars[$i]['price'] = $rate->shipping_rate_price;
         $vars[$i]['tax'] = round($rate->shipping_tax_total, 2);
         $vars[$i]['extra'] = $rate->shipping_rate_handling;
         $vars[$i]['total'] = $rate->shipping_rate_price + $rate->shipping_rate_handling + round($rate->shipping_tax_total, 2);
         $i++;
     }
     //print_r($vars);
     return $vars;
 }
Esempio n. 3
0
 public function getDiscountTotal()
 {
     $app = JFactory::getApplication();
     $session = JFactory::getSession();
     $discount_total = 0;
     if ($session->has('coupon', 'j2store')) {
         $coupon_info = J2StoreHelperCart::getCoupon($session->get('coupon', '', 'j2store'));
         if ($coupon_info) {
             $products = $this->getDataNew();
             if (!$coupon_info->product) {
                 $sub_total = J2StoreHelperCart::getSubTotal();
             } else {
                 $sub_total = 0;
                 foreach ($products as $product) {
                     if (in_array($product['product_id'], $coupon_info->product)) {
                         $sub_total += $product['total'];
                     }
                 }
             }
             if ($coupon_info->value_type == 'F') {
                 $coupon_info->value = min($coupon_info->value, $sub_total);
             }
             $product_array2 = array();
             foreach ($products as $product) {
                 $discount = 0;
                 if (!$coupon_info->product) {
                     $status = true;
                 } else {
                     if (in_array($product['product_id'], $coupon_info->product)) {
                         $status = true;
                     } else {
                         $status = false;
                     }
                 }
                 if ($status) {
                     if ($coupon_info->value_type == 'F') {
                         $discount = $coupon_info->value * ($product['total'] / $sub_total);
                     } elseif ($coupon_info->value_type == 'P') {
                         $discount = $product['total'] / 100 * $coupon_info->value;
                     }
                 }
                 $discount_total += $discount;
             }
             if ($coupon_info->free_shipping && $session->has('shipping_values', 'j2store')) {
                 $shipping = $session->get('shipping_values', array(), 'j2store');
                 $shipping_cost = $shipping['shipping_price'] + $shipping['shipping_extra'] + $shipping['shipping_tax'];
                 $discount_total += $shipping_cost;
             }
         }
     }
     return $discount_total;
 }
Esempio n. 4
0
 /**
  * Calculates the per_order coupon discount for the order
  * and the total post-tax/shipping discount
  * and sets order->order_discount
  *
  * @return unknown_type
  */
 function calculateDiscountTotals()
 {
     $this->_taxes = J2StoreHelperCart::getTaxes();
     $session = JFactory::getSession();
     $tax = new J2StoreTax();
     JModelLegacy::addIncludePath(JPATH_SITE . '/components/com_j2store/models');
     $model = JModelLegacy::getInstance('MyCart', 'J2StoreModel');
     $products = $model->getDataNew();
     if ($session->has('coupon', 'j2store')) {
         $coupon_info = J2StoreHelperCart::getCoupon($session->get('coupon', '', 'j2store'));
         if ($coupon_info) {
             $discount_total = 0;
             if (!$coupon_info->product) {
                 $sub_total = J2StoreHelperCart::getSubTotal();
             } else {
                 $sub_total = 0;
                 foreach ($products as $product) {
                     if (in_array($product['product_id'], $coupon_info->product)) {
                         $sub_total += $product['total'];
                     }
                 }
             }
             if ($coupon_info->value_type == 'F') {
                 $coupon_info->value = min($coupon_info->value, $sub_total);
             }
             //maximum value restriction. If set then we need to check
             if ($coupon_info->value_type == 'P' && !empty($coupon_info->max_value) && (double) $coupon_info->max_value > 0) {
                 //calculate the actual discount
                 require_once JPATH_SITE . '/components/com_j2store/models/mycart.php';
                 $cart_model = new J2StoreModelMyCart();
                 $actual_discount = $cart_model->getDiscountTotal();
                 //is the actual discount greater than the max value
                 if ($actual_discount > 0 && $actual_discount > (double) $coupon_info->max_value) {
                     //set the coupon to be of fixed value
                     $coupon_info->value = (double) $coupon_info->max_value;
                     $coupon_info->value_type = 'F';
                 }
             }
             foreach ($products as $product) {
                 $discount = 0;
                 if (!$coupon_info->product) {
                     $status = true;
                 } else {
                     if (in_array($product['product_id'], $coupon_info->product)) {
                         $status = true;
                     } else {
                         $status = false;
                     }
                 }
                 if ($status) {
                     if ($coupon_info->value_type == 'F') {
                         $discount = $coupon_info->value * ($product['total'] / $sub_total);
                     } elseif ($coupon_info->value_type == 'P') {
                         $discount = $product['total'] / 100 * $coupon_info->value;
                     }
                     if ($product['tax_profile_id']) {
                         $tax_rates = $tax->getRateArray($product['total'] - ($product['total'] - $discount), $product['tax_profile_id']);
                         foreach ($tax_rates as $tax_rate) {
                             //	if ($tax_rate['value_type'] == 'P') {
                             $this->_taxes[$tax_rate['taxrate_id']] -= $tax_rate['amount'];
                             //	}
                         }
                     }
                 }
                 $discount_total += $discount;
             }
         }
     }
     // store the total amount of the discount
     //set the total as equal to the order_subtotal + order_tax if its greater than the sum of the two
     $this->order_discount = $discount_total > $this->order_subtotal + $this->order_tax ? $this->order_subtotal + $this->order_tax : $discount_total;
 }