#!/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];
our $from_to   				= $ARGV[1];
our $event_id   			= $ARGV[2];
our $logistics_dcb_waybill	= $ARGV[3];
# my  $suburb  = $ARGV[1];

# $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_create_waybill_test.pl?1

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

&today;
&db_open_upd ;
	our $db_ignore_open_close = 1 ; # do one open and one close instead of repeating it	
		&send_it ;
		&parse_it ;
		&read_it ;
	$db_ignore_open_close = 0 ; # do one open and one close instead of repeating it
&db_close_conn ;

# &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>
			<savewabill xmlns="http://tempuri.org/">
				<!-- Optional -->
				<request>
					<AccNr xmlns="http://schemas.datacontract.org/2004/07/OMSIntegrate.Request"></AccNr>
					<AddressLine1 xmlns="http://schemas.datacontract.org/2004/07/OMSIntegrate.Request"></AddressLine1>
					<AddressLine2 xmlns="http://schemas.datacontract.org/2004/07/OMSIntegrate.Request"></AddressLine2>
					<AddressLine3 xmlns="http://schemas.datacontract.org/2004/07/OMSIntegrate.Request"></AddressLine3>
					<AddressLine4 xmlns="http://schemas.datacontract.org/2004/07/OMSIntegrate.Request"></AddressLine4>
					<ContactNumber xmlns="http://schemas.datacontract.org/2004/07/OMSIntegrate.Request">+27 (79) 888 7121</ContactNumber>
					<ContactPerson xmlns="http://schemas.datacontract.org/2004/07/OMSIntegrate.Request"> CLINT SLIEDRECHT</ContactPerson>
					<CustomerReferenceNumber xmlns="http://schemas.datacontract.org/2004/07/OMSIntegrate.Request"></CustomerReferenceNumber>
					<Date xmlns="http://schemas.datacontract.org/2004/07/OMSIntegrate.Request">2024-05-31</Date>
					<DestHub xmlns="http://schemas.datacontract.org/2004/07/OMSIntegrate.Request">AMERICAN INTERNATIONAL SCHOOL CAPE TOWN</DestHub>
					<Number xmlns="http://schemas.datacontract.org/2004/07/OMSIntegrate.Request">ITVTEST123</Number>
					<OriginHub xmlns="http://schemas.datacontract.org/2004/07/OMSIntegrate.Request">ITV CPT</OriginHub>
					<PostalCode xmlns="http://schemas.datacontract.org/2004/07/OMSIntegrate.Request">1619</PostalCode>
					<Reciever xmlns="http://schemas.datacontract.org/2004/07/OMSIntegrate.Request">DEV SERVICE</Reciever>
					<RouteCode xmlns="http://schemas.datacontract.org/2004/07/OMSIntegrate.Request">JNBKMPE</RouteCode>
					<SpecialInstructions xmlns="http://schemas.datacontract.org/2004/07/OMSIntegrate.Request">HelloWOrld!!!dfsdfdfsdfasdfasdfasdfdsfasdfasdfasdf sf sdf asdf </SpecialInstructions>
					<serviceType xmlns="http://schemas.datacontract.org/2004/07/OMSIntegrate.Request">1</serviceType>
				</request>
			</savewabill>
		</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');
	$request->header(SOAPAction => '"http://tempuri.org/IOMSIntegrate/savewabill"');
	$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 {
	
	print "\n" . $xmldata->{'s:Body'}->[0]->{'savewabillResponse'}->[0]->{'savewabillResult'}->[0] ;
	
	return unless $event_id ;
	return unless $from_to ;
		
	# &db_min_ro("event_quotes","1,logistics_dcb_waybill","`id`='$event_id'","","") ;
	
	# my @waybill_nrs = split(/\-/,$db{event_quotes}{1}{logistics_waybill_nrs}) ; 
	my @dcb_waybill = split(/\-/,$logistics_dcb_waybill) ; 
	
	%i = () ;
	
	$i{logistics_dcb_waybill} = ($from_to eq 'to') ? "$dcb_waybill[0]-1" : "1-$dcb_waybill[1]"  ;
	
	# our $testing = 1 ;
	
	# &db_min_upd('event_quotes',"`id`='$event_id'") ;
	&db_min_upd('event_quotes',"`id`='5136'") ;
			
} #-------------------------------------------------------------------------------



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;
