#!/usr/bin/perl

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

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

# https://itvadmin.co.za/cgi-bin/scripts/get/get_delete_attach.pl?documents/1&document_2-ITV_System1.xlsx&documents&1&document_2
# https://itvadmin.co.za/cgi-bin/scripts/get/get_delete_attach.pl?costings/1&costing_signed_quote-Overstrand_Herald_page_9_article.pdf&costings&1&costing_signed_quote

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

@ARGV = split(/\\*\&/, $ENV{'QUERY_STRING'});

our $delete_id	= $ARGV[0] ;
our $table		= $ARGV[1] ;
$table = 'event_quotes' unless $table ;

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

use CGI::Carp qw(fatalsToBrowser);

&delete ;

sub delete {

	return unless $delete_id ;
	
	if ($table eq 'event_quotes') {
		
		&db_min_ro($table,'1,sss_quote_nr',"id='$delete_id'",'','') ;
		
		my $sss_delete_id = $db{$table}{1}{sss_quote_nr} ;  
		my $aisa_delete_id = $delete_id ;

		&db_min_delete('event_quotes',"`id`='$aisa_delete_id'") ;
		&db_min_delete('event_quotes_min',"`id`='$aisa_delete_id'") ;
		&common_min_delete_files("$htmlpath/uploads/uploads/$aisa_delete_id") ;
		&common_min_delete_files("$htmlpath/pdf/event_details","Event_Details-$aisa_delete_id.pdf") ;

		if ($sss_delete_id) {

			&db_switch_conn('sss') ;
				&db_min_ro($table,'1,id',"id = '$sss_delete_id' AND aisa_quote_nr='$aisa_delete_id'",'','') ;
				if ($db{$table}{1}{id}) {
					&db_min_delete($table,"id='$sss_delete_id'") ;
					&common_min_delete_files("$htmlpath_sss/uploads/uploads/$sss_delete_id") ;			
					&common_min_delete_files("$htmlpath_sss/pdf/event_details","Event_Details-$sss_delete_id.pdf") ;
				}
			&db_switch_conn('aisa') ;
		}		
		
	} elsif ($table eq 'analytics_event_bookings') {
		&db_min_delete('credits_used',"analytics_event_id='$delete_id'") ;
		&db_min_delete($table,"`id`='$delete_id'") ;
	} else {
		&db_min_delete($table,"`id`='$delete_id'") ;
	}
	
} #------------------------------------------------------------------------------------------

use common ;

1;