<?php
if(empty($errorMsg) && isset($_REQUEST['originId']) && !empty($_REQUEST['originId']))
{
    $originId = intval($_REQUEST['originId']);
    
    $query = "SELECT MarketCountry.CountryId, MarketCountry.MarketId
    FROM Origin
	INNER JOIN Location ON Origin.LocationCode = Location.Code
	INNER JOIN Country ON Country.Code = Location.CountryCode
	INNER JOIN MarketCountry ON Country.Id = MarketCountry.CountryId
    WHERE Origin.Id = '" . $originId . "' LIMIT 1;";
    if ($result = $mysqli->query($query))
    {
        if($result->num_rows == 0)
        {
            $errorMsg[] = 'No freight types have been found';
        }
        else
        {
	        while ($row = $result->fetch_object())
	        {
                $originCountryId = $row->CountryId;
                $originMarketId = $row->MarketId;
	        }
            $result->close();
        }
    }
    else
    {
	    print $mysqli->error;
	    exit;
    }
    
    if(isset($originCountryId) && isset($originMarketId))
    {
        $query = "SELECT FreightType.Id, FreightType.Name
        FROM FreightType
        INNER JOIN ShippingCharge ON ShippingCharge.FreightTypeId = FreightType.Id
        INNER JOIN ChargeGroup ON ShippingCharge.ChargeGroupId = ChargeGroup.Id
        WHERE ShippingCharge.WebsiteId = '" . $websiteId . "' AND ShippingCharge.Live = 1 AND ShippingCharge.Value != 0
        AND (ShippingCharge.OriginId = '" . $originId . "' OR ShippingCharge.OriginMarketId = '" . $originMarketId . "' OR ShippingCharge.OriginCountryId = '" . $originCountryId . "')
        GROUP BY FreightType.Id, FreightType.Name
        ORDER BY FreightType.Order, FreightType.Name;";
        if ($result = $mysqli->query($query))
        {
            if($result->num_rows == 0)
            {
                $errorMsg[] = 'No freight types have been found';
            }
            else
            {
                $xmlContent = $xml->addChild('FreightTypes');
	            while ($row = $result->fetch_object())
	            {
                    $xmlFreightType = $xmlContent->addChild('FreightType');
	                $xmlFreightType->addChild('Id', intval($row->Id));
                    $xmlFreightType->addChild('Name', encodeString($row->Name));
	            }
                $result->close();
            }
        }
        else
        {
	        print $mysqli->error;
	        exit;
        }
    }
    else
    {
        $query = "SELECT FreightType.Id, FreightType.Name
        FROM FreightType
        WHERE FreightType.WebsiteId = '" . $websiteId . "' AND FreightType.Live = 1
        ORDER BY FreightType.Order, FreightType.Name;";
        if ($result = $mysqli->query($query))
        {
            if($result->num_rows == 0)
            {
                $errorMsg[] = 'No freight types have been found';
            }
            else
            {
                $xmlContent = $xml->addChild('FreightTypes');
	            while ($row = $result->fetch_object())
	            {
                    $xmlFreightType = $xmlContent->addChild('FreightType');
	                $xmlFreightType->addChild('Id', intval($row->Id));
                    $xmlFreightType->addChild('Name', encodeString($row->Name));
	            }
                $result->close();
            }
        }
        else
        {
	        print $mysqli->error;
	        exit;
        }
    }
}
?>