<?php
/**
* @filename        AustraliaPost.php
* @description     This class is for AustraliaPost (http://drc.edeliver.com.au)
* @version         1.0
* @package         MSHShippingLibrary.shippingClasses
* @author          Mohammad Sajjad Hossain - info@sajjadhossain.com
*
* @link            http://sajjadhossain.com
* @created on      March 16, 2011
* @Dependencies
* @license
***/
class AustraliaPost extends Carrier
{
    private $isDomestic = false;
    private $domesticServices = array('STANDARD', 'EXPRESS');
    private $internationaServices = array('AIR', 'SEA');
    private $serviceType = '';

    public function AustraliaPost()
    {
        parent::Carrier();
        $this->setRequestUrl('http://drc.edeliver.com.au/ratecalc.asp');
        $this->setCacheTableSuffix('auspost');
    }

    public function calculate()
    {
        $this->logMessage('Validating Information for Australia Post');
        $this->validateServiceSelected();
        $this->validateFromZip();
        $this->validateFromCountyCode();
        $this->validateToZip();
        $this->validateToCountyCode();
        $this->item->validate(array('weight', 'length', 'height', 'width'));

        if($this->isValid())
        {
            $weight = (int) ceil($this->item->getWeightInKilogram() * 1000);    //converting weight to gram
            $length = (int) ceil($this->item->getLengthInCentimeter() * 10);    //converting length to milimeter
            $width = (int) ceil($this->item->getWidthInCentimeter() * 10);      //converting width to milimeter
            $height = (int) ceil($this->item->getHeightInCentimeter() * 10);    //converting height to milimeter

            $this->isDomestic = $this->getFromCountryCode() == 'AU' && ($this->getFromCountryCode() == $this->getToCountryCode());

            $services = $this->isDomestic ? $this->domesticServices : $this->internationaServices;

            foreach($services as $service)
            {
                if(in_array($service, $this->getServices()))
                {
                    $this->serviceType = $service;
                    $this->setParam('Service_Type', $this->serviceType);
                    $this->setParam('Pickup_Postcode', $this->getFromZip());
                    $this->setParam('Destination_Postcode', $this->getToZip());
                    $this->setParam('Country', $this->getToCountryCode());
                    $this->setParam('Weight', $weight);
                    $this->setParam('Length', $length);
                    $this->setParam('Width', $width);
                    $this->setParam('Height', $height);
                    $this->setParam('Quantity', $this->item->getQuantity());
                    $this->makeAPICall();
                }
            }
        }

        return $this->response;
    }

    protected function getSearchString()
    {
        $this->searchString = $this->getFromZip() . ':'
                                . $this->getToCountryCode() . ':'
                                . $this->getToZip() . ':'
                                . $this->item->getQuantity() . ':'
                                . $this->item->getWeightInPound() . ':'
                                . $this->item->getLengthInInch() . ':'
                                . $this->item->getWidthInInch() . ':'
                                . $this->item->getHeightInInch() . ':'
                                . $this->getWeightUnit() . ':'
                                . $this->getLengthUnit() . ':'
                                . $this->serviceType;

        return $this->searchString;
    }

    protected function prepareResponse()
    {
        if(strstr($this->response->getRawResponse(), 'ADODB.Command'))
        {
            $this->response->setError('A technical error occured at AustraliaPost end');
            $this->response->resetRates();
        }
        else
        {
            $parts = explode("\n", $this->response->getRawResponse());

            list($key, $error) = explode('=', $parts[2]);

            if(trim($error) == 'OK')
            {
                list($key, $charge) = explode('=', $parts[0]);

                $this->response->setRate($this->getServiceName($this->serviceType), $charge);

                $this->insertResponseToDB();
            }
            else
            {
                $this->response->resetRates();
                $this->response->setError($error);
            }
        }
    }

    private function getServiceName($code)
    {
        switch($code)
        {
            case 'STANDARD':
                return 'Standard Postage';
            case 'EXPRESS':
                return 'Express Postage';
            case 'AIR':
                return 'Air Mail';
            case 'SEA':
                return 'Sea Mail';
        }
    }
}
//End of file AustraliaPost.php