#!/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 $event_cost_item_id	= $ARGV[0] ;
our $event_start_date	= $ARGV[1] ;
our $full_half_day		= $ARGV[2] ;
our $changed_id			= $ARGV[3] ;

our $table = 'event_cost_items' ;

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

use CGI::Carp qw(fatalsToBrowser);

$json = '' ;

if ($event_cost_item_id && $event_start_date && $full_half_day && $changed_id) {
	
	&db_min_ro($table,'1,valid_from,valid_to,rates,full_half_day,excl_from_expenses',"id='$event_cost_item_id' AND active='1'",'','') ;
	
	if ($db{$table}{1}{excl_from_expenses} && $changed_id =~ /_workings_event_/) {
		print $json ;
		return ;
	}
	
	$json .= "[" ;
	
	$event_start_date =~ s/\-//g ;
	
	# my @valid_from 			= split(/\;/,$db{$table}{1}{valid_from})    ;
	# my @valid_to 			= split(/\;/,$db{$table}{1}{valid_to})      ;
	my @rates	 			= split(/\;/,$db{$table}{1}{rates})         ;
	my @full_half_day_arr 	= split(/\;/,$db{$table}{1}{full_half_day}) ;
	
	my $cnt = 0 ; 
	
	foreach (@rates) {
		# if ($valid_from[$cnt] <= $event_start_date && $event_start_date <= $valid_to[$cnt] && $full_half_day_arr[$cnt] eq $full_half_day) {
		if ($full_half_day_arr[$cnt] eq $full_half_day) {
			my $rate_name = ($full_half_day) ? "rate_full" : "rate_half" ;
			$json .= qq~{"rate":"$rates[$cnt]"}~ ; 
			last ;
		}
		$cnt++ ;
		
	}

	if ($json =~ /\,/) { $json = substr($json,0,-1) ; }

	$json  =~ s/\\//g ;
	$json .= "]" ;
	
}

print $json ;

use common ;

1;