#!/usr/bin/perl

BEGIN { use lib '/usr/home/cfg' ; require push_inc ; }

require cfg ;

use CGI::Carp qw(fatalsToBrowser);

print "Content-type: text/html\n\n";

use LWP::UserAgent;
use HTTP::Request;
use XML::Simple ;
use DBI;
use SOAP::Lite ;

#-------------------------------------------------------------------------------

    @ARGV        = split(/\\*\&/, $ENV{'QUERY_STRING'});
our $debug       = $ARGV[0];
my  $suburb      = $ARGV[1];
my  $postalcode  = $ARGV[2];

$suburb  =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
   
# https://itvadmin.co.za/cgi-bin/scripts/admin/oneoff/dcb_postalcode_lookup.pl?1&Morningside&4001

#-------------------------------------------------------------------------------

&today;

&send_it ;
&parse_it ;
&read_it ;

# &dcd_set_parameters ;
# &dcb_send_xml ;

exit;

#-------------------------------------------------------------------------------

sub send_it {

	my $message = qq~<?xml version="1.0" encoding="utf-8"?>
	<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
	  <soap:Body>
		<PostalCode xmlns="http://tempuri.org/">
			<request>
				<PostalCode xmlns="http://schemas.datacontract.org/2004/07/OMSIntegrate.Request">$postalcode</PostalCode>
				<Suburb xmlns="http://schemas.datacontract.org/2004/07/OMSIntegrate.Request">$suburb</Suburb>
			</request>
		</PostalCode>
	  </soap:Body>
	</soap:Envelope>~ ;
	
	if ($debug) {
		print $message;
    }

	my $userAgent = LWP::UserAgent->new();
	# my $request = HTTP::Request->new(POST => 'https://dcboms.co.za/APIUAT/OMSIntegrate.svc');
	my $request = HTTP::Request->new(POST => 'https://dcboms.co.za/API/OMSIntegrate.svc');
	$request->header(SOAPAction => '"http://tempuri.org/IOMSIntegrate/PostalCode"');
	$request->content($message);
	$request->content_type("text/xml; charset=utf-8");
	my $response = $userAgent->request($request);
		
	# Check response
	my $response_code    = $response -> code ;
	my $response_content = $response -> content ;

	$returnxml = $response_content ;

	# print $returnxml ;
    
} #-------------------------------------------------------------------------------

sub parse_it {

	# use Data::Dumper;
	# print Dumper(\$returnxml);
	# exit;

	$returnxml =~ s/[\x80-\xFF]/?/g ;
	my $xml = new XML::Simple or die "Cant instantiate object XML::Simple $!" ;
	# my $xmldata = $xml->XMLin($returnxml) or die "Cant open xmldata $!" ;
	$xmldata = $xml->XMLin($returnxml, forcearray => 1) or die "Cant open xmldata $!";

	if ($debug) {
		use Data::Dumper;
		print Dumper(\$xmldata);
		# exit ;
    }

} #-------------------------------------------------------------------------------

sub read_it {

	foreach my $hashref ( @{$xmldata->{'s:Body'}->[0]->{'PostalCodeResponse'}->[0]->{'PostalCodeResult'}->[0]->{'a:ArrayOfpostalCode'} } ) {
		# unless (lc ${$hashref}{'Name'}->[0] eq lc $search)     { next ; }
		# print ${$hashref}{'ContentEntityId'}->[0] ;
		
		# unless (exists $exists{${$hashref}{'a:postalCode'}->[0]}) {	
		foreach $address ( @{$hashref}{'a:postalCode'}) {	
			# print $address . "<br>" ;
			# print Dumper(\$address);
			# print Dumper(\$address{'a:city'}->[0]);
			print 'a:city=' . ${$hashref}{'a:postalCode'}->[0]->{'a:city'}->[0] . "\n" ;
			print 'a:suburb=' . ${$hashref}{'a:postalCode'}->[0]->{'a:suburb'}->[0] . "\n" ;
			print 'a:code=' . ${$hashref}{'a:postalCode'}->[0]->{'a:code'}->[0] . "\n" ;
			print 'a:routeCode=' . ${$hashref}{'a:postalCode'}->[0]->{'a:routeCode'}->[0] . "\n" ;
			# print ${$address}->[0] . "\n" ;
			# print 'a:city=' . ${$address}{'a:city'}->[0] . "<br>" ;
			# print 'a:routeCode=' . ${$address}{'a:routeCode'}->[0] . "<br>" ;
			# print 'a:suburb=' . ${$address}{'a:suburb'}->[0] . "<br>" ;
			# print 'a:code=' . ${$address}{'a:code'}->[0] . "<br>" ;
			
			# $i{dcb_id}    = ${$hashref}{'ContentEntityId'}->[0] ;
			# $i{latitude}   = ${$hashref}{'Latitude'}->[0] ;
			# $i{longitude}  = ${$hashref}{'Longitude'}->[0] ;
			# $i{country}    = ${$hashref}{'Country'}->[0] ;
			 # $i{area}      = ${$hashref}{'Area'}->[0] ;
			 
			# # unless (ref(${$hashref}{'Area'}->[0] eq 'HASH'))     { $i{area}      = ${$hashref}{'Area'}->[0] ; }
			# # unless (ref(${$hashref}{'Location'}->[0] eq 'HASH')) { $i{location}  = ${$hashref}{'Location'}->[0] ; }
			
			# $i{location}  = ${$hashref}{'Location'}->[0] unless (ref(${$hashref}{'Location'}->[0]) eq 'HASH');
			
			# $i{lastupdate} = "$now_ccyy_mm_dd $now_hour:$now_min:$now_sec" ;
			# &common_db_insert('dcb_places') ;
		}
		# last ;
	}
    
} #-------------------------------------------------------------------------------



sub dcd_set_parameters {

# use SOAP::Lite ;
   # +trace => [ transport => sub { print $_[0]->as_string } ];

$dcb_param{'uri'}       = 'https://dcboms.co.za/APIUAT/OMSIntegrate.svc' ;
$dcb_param{'proxy'}     = 'https://dcboms.co.za/APIUAT/OMSIntegrate.svc' ;
# $dcb_param{'username'}  = 'res@followme2africa.com' ;
# $dcb_param{'password'}  = 'moby602' ;

} #----------------------------------------------------------------------------------------------------

sub dcb_send_xml {

$dcb_param{'method'}  = 'PostalCode' ;

&dcb_create_soap ;

# if (lc $username eq 'groz000'){
	# print "Content-type: text/html\n\n";
	# use Data::Dumper;
    # print Dumper(\$xml_out_save_itinerary);
    # }
	
$xml_out = qq~
<request>
	<PostalCode xmlns="http://schemas.datacontract.org/2004/07/OMSIntegrate.Request"></PostalCode>
	<Suburb xmlns="http://schemas.datacontract.org/2004/07/OMSIntegrate.Request">POMONA</Suburb>
</request>
~ ;

$xml_out_dcb_soap = SOAP::Data->type('xml' => $xml_out);

$dcb_response = $dcb_soap -> call ($dcb_method => $xml_out_dcb_soap) ;

&common_debug ("dcb_send_xml : dcb_response [ $dcb_response ]") ;

} #----------------------------------------------------------------------------------------------------

sub dcb_create_soap {

$dcb_soap = SOAP::Lite
                ->proxy( $dcb_param{'proxy'} )
                ->autotype(0)
                ->outputxml(1)
                ->on_action( sub { join '/', @_ } )
                ;

$dcb_method = SOAP::Data -> name( $dcb_param{'method'} ) -> attr({ 'xmlns' => $dcb_param{'uri'} } ) ;

} #----------------------------------------------------------------------------------------------------

use common ;
use today ;

1;
