'admin/store/settings/quotes/methods/auspost', 'access' => user_access('configure quotes'), 'title' => t('Australia Post'), 'callback' => 'drupal_get_form', 'callback arguments' => array('uc_auspost_admin_settings'), 'type' => MENU_LOCAL_TASK, ); } drupal_add_css(drupal_get_path('module', 'uc_auspost') .'/uc_auspost.css'); return $items; } /****************************************************************************** * Workflow-ng Hooks * ******************************************************************************/ /** * Implementation of hook_configuration(). * * Connect the auspost action and event. */ function uc_auspost_configuration(){ $enabled = variable_get('uc_quote_enabled', array('auspost' => true)); $configurations = array( 'uc_auspost_get_quote' => array( '#label' => t('Shipping quote from Australia Post eDRC'), '#event' => 'get_quote_from_auspost', '#module' => 'uc_auspost', '#active' => $enabled['auspost'], ), ); $action = workflow_ng_use_action('uc_quote_action_get_quote', array( '#label' => t('Fetch a shipping quote'), )); $configurations['uc_auspost_get_quote'] = workflow_ng_configure($configurations['uc_auspost_get_quote'], $action); return $configurations; } /****************************************************************************** * Übercart Hooks * ******************************************************************************/ /** * Implementation of ubercart's hook_shipping_method(). * */ function uc_auspost_shipping_method(){ $methods = array(); // $enabled = variable_get('uc_quote_enabled', array('auspost' => true, 'order rate' => false)); // $weight = variable_get('uc_quote_method_weight', array('auspost' => 0, 'order rate' => 1)); $enabled = variable_get('uc_quote_enabled', array('auspost' => true)); $weight = variable_get('uc_quote_method_weight', array('auspost' => 0)); $methods['auspost'] = array( 'id' => 'auspost', 'module' => 'uc_auspost', 'title' => t('Australia Post eDRC Quote'), 'enabled' => $enabled['auspost'], 'quote' => array( 'type' => 'order', 'callback' => 'uc_auspost_quote', 'accessorials' => _uc_auspost_services() ), 'weight' => $weight['auspost'], ); return $methods; } /****************************************************************************** * Menu Callbacks * ******************************************************************************/ function uc_auspost_admin_settings(){ $form = array(); $form['uc_auspost_services'] = array('#type' => 'checkboxes', '#title' => t('Australia Post Services'), '#default_value' => variable_get('uc_auspost_services', _uc_auspost_services()), '#options' => _uc_auspost_services(), '#description' => t('Select the Australia Post services that are available to customers.'), ); $form['uc_auspost_markup'] = array('#type' => 'textfield', '#title' => t('Shipping Rate Markup'), '#default_value' => variable_get('uc_auspost_markup', '0%'), '#description' => t('Markup shipping rate quote by dollar amount, percentage, or multiplier.'), ); $form['uc_auspost_uri'] = array('#type' => 'textfield', '#title' => t('eDRC URI'), '#default_value' => 'http://drc.edeliver.com.au/ratecalc.asp', '#description' => t('Location of the rate calculator. Shouldn\'t need to be changed'), ); return system_settings_form($form); } /****************************************************************************** * Module Functions * ******************************************************************************/ /** * Standard callback to return a shipping rate via the auspost method. * * @param $products * The order's products. * @param $details * Other order details including a shipping address. * @return * A JSON object containing the shipping quote for the order. */ function uc_auspost_quote($products, $details){ $origin = variable_get('uc_quote_store_default_address', new stdClass()); $country = uc_get_country_data(array('country_id' => $details['country'])); $method = uc_auspost_shipping_method(); $accessorial_keys = array_flip(_uc_auspost_services()); $pickup_postcode = $origin->postal_code; $destination_postcode = $details['postal_code']; $destination_country = $country[0]['country_iso_code_2']; switch ($destination_country) { case 'AU': $type = 'domestic'; break; default: $type = 'international'; break; } foreach (_uc_auspost_services(null,$type) as $accessorial) { $rate = 0; $service_type = _uc_auspost_drc_code($accessorial_keys[$accessorial]); foreach ($products as $product){ if ($product->shippable == 1) { $quote = _uc_auspost_fetch_quote($product,$service_type,$pickup_postcode,$destination_postcode,$destination_country); //if ($quote['err_msg'] == 'OK') { $rate += $quote['charge']; //} } } $quotes[] = array('rate' => $rate, 'format' => uc_currency_format($rate), 'option_label' => _uc_get_option_label($accessorial_keys[$accessorial],$quote['days'])); } return $quotes; } function _uc_auspost_fetch_quote($product,$service_type,$pickup_postcode,$destination_postcode,$destination_country) { $response = file('http://drc.edeliver.com.au/ratecalc.asp?Pickup_Postcode='.$pickup_postcode.'&Destination_Postcode='.$destination_postcode.'&Country='.$destination_country.'&Weight='._uc_auspost_weight_to_grams($product->weight,$product->weight_units).'&Service_Type='.$service_type.'&Length='._uc_auspost_length_to_mm($product->length,$product->length_units).'&Width='._uc_auspost_length_to_mm($product->width,$product->length_units).'&Height='._uc_auspost_length_to_mm($product->height,$product->length_units).'&Quantity='.$product->qty); $charge = split('=',$response[0]); $ship_time = split('=',$response[1]); $return_msg = split('=',$response[2]); $response = array( $charge[0] => $charge[1], $ship_time[0] => $ship_time[1], $return_msg[0] => $return_msg[1] ); return $response; } function _uc_auspost_services($id = null,$type = null){ $domestic = array( '0' => t('Standard Delivery'), '1' => t('Express Post Delivery') ); $international = array( '2' => t('International Air Delivery'), '3' => t('International Surface Delivery'), '4' => t('Express Courier International Document'), '5' => t('Express Courier International Merchandise'), '6' => t('Express Post International') ); switch (strtolower($type)) { case 'domestic': $return = $domestic; break; case 'international': $return = $international; break; case null: $return = array_merge($domestic,$international); break; } if (is_null($id)) { return $return; } else { return $return[$id]; } } /* * Return the appropriate logo for the associated accessorial * * @param $id * ID of the accessorial */ function _uc_get_logo_path($id) { $logo = array( '0' => 'standard_logo.png', '1' => 'express_logo.png', '2' => 'airmail_logo.png', '3' => 'seamail_logo.png', '4' => 'eci_logo.png', '5' => 'eci_logo.png', '6' => 'epi_international_logo.png' ); // If an incorrect accessorial ID is somehow requested // return the standard logo. if (!array_key_exists($id,$logo)) { return $logo[0]; } else { return $logo[$id]; } } /* * Return the relevant option label image * * @param $accessorial_id * ID of the accessorial item to return the option label for */ function _uc_get_option_label($accessorial_id,$days = null) { $services = _uc_auspost_services(); if (!is_null($days)) { if ($days >= 2) { $text = t('Days'); } else { $text = t('Day'); } $days_text = '(' . $days . ' ' . $text . ')'; } else { $days_text =''; } $string = ' ' . $days_text; return $string; } /* * Convert weight units to Grams as required by the eDRC * * @param $weight * Weight to be converted * @param * Current unit of weight of $weight */ function _uc_auspost_weight_to_grams($weight,$unit) { switch (strtolower($unit)){ case 'oz': $weight = $weight * 28.3495231; break; case 'lb': $weight = $weight * 453.59237; break; case 'g': $weight = $weight; break; case 'kg': $weight = $weight * 1000; break; } if ($weight) { return $weight; } else { drupal_set_message(t('No weight passed to the _weight_to_grams function of uc_auspost'),error); } } /* * Convert length measurement units to millimetres as required by the eDRC * * @param $measurement * Measurement to be converted * @param $unit * Current unit of measurement of $measurement */ function _uc_auspost_length_to_mm($measurement,$unit) { switch (strtolower($unit)){ case 'cm': $measurement = $measurement * 10; break; case 'in': $measurement = $measurement * 25.4; break; case 'mm': $measurement = $measurement; break; } if ($measurement) { return $measurement; } else { drupal_set_message(t('No measurement passed to the _length_to_mm function of uc_auspost'),error); } } /* * Return the appropriate DRC Service code for a specified accessorial ID * * @param $accessorial_id * ID of the accessorial for which to retrieve the DRC Service Code */ function _uc_auspost_drc_code($accessorial_id) { $service_codes = array( '0' => 'STANDARD', '1' => 'EXPRESS', '2' => 'AIR', '3' => 'SEA', '4' => 'ECI_D', '5' => 'ECI_M', '6' => 'EPI' ); return $service_codes[$accessorial_id]; } function array_remval($val,&$arr){ $i=array_search($val,$arr); if($i===false)return false; $arr=array_merge(array_slice($arr, 0,$i), array_slice($arr, $i+1)); return true; }