#!/usr/bin/perl

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

use lib "/home/libs/modules" ;
use lib "/home/libs/html" ;
    
use CGI::Carp qw(fatalsToBrowser);
use JSON::Repair ':all';

require cfg ;

# my $referer = $ENV{HTTP_REFERER} ; exit unless $referer =~ m/itvadmin/iog ;

&today;

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

@ARGV = split(/\\*\&/, $ENV{'QUERY_STRING'}); foreach $ARG (@ARGV) { ($par,$val) = split(/\=/,$ARG) ; $param{$par} = $val ; }

# https://itvadmin.co.za/cgi-bin/scripts/get/get_db_cal_events.pl?start=2024-02-01 00:00:00&end=2024-02-28 00:00:00&debug=1
	
our $debug = $param{debug} ; # our $username = 'rory' if $debug == 1 ;

$param{start} =~ s/T/ /g ; my $param_start = substr($param{start},0,10);
$param{end}   =~ s/T/ /g ; my $param_end   = substr($param{end},0,10);

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

use Date::Calc qw(:all);

$json = '[' ;

&db_open_ro ;
	our $db_ignore_open_close = 1 ; # do one open and one close instead of repeating it
	&check_session ;
	&get_db_calendar_events ;
	&get_db_event_quotes ;
	# &get_db_leave ;
	$db_ignore_open_close = 0 ; # do one open and one close instead of repeating it
&db_close_conn ;

chop $json if length $json > 1 ; # $json  = substr($json,0,-1) ;    
$json .= ']' ;

print $json ;

exit ;

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

sub get_db_calendar_events {

unless ($param{start} && $param{end}) { return ; } 

&db_min_ro('users','id,name,user_type','','','') ;
foreach (keys %{$db{users}}) { 
	$user_name{$_}   = $db{users}{$_}{name} ; 
	$casual_type{$_} = uc substr($db{users}{$_}{user_type},-1,1) ; 
}

&db_min_ro('events','*',"((`startdate` >= '$param_start 00:00:00' AND `startdate` <= '$param_end 23:59:59') OR ('$param_start 23:59:59' >= `startdate` AND '$param_start 00:00:00' <= `enddate`))",'','') ;

# &db_min_ro('events','*',"enddate >= '$param{start}' AND startdate <= '$param{end}'",'','') ;

foreach my $id (keys %{$db{events}}) {

	# &common_debug("get_db_leave - $db{events}{$id}{category} -> $db{events}{$id}{title}");

    my $start = $db{events}{$id}{startdate} ; $start =~ s/ /T/g ;
    my $end   = $db{events}{$id}{enddate}   ; $end   =~ s/ /T/g ;
    my $cat   = $db{events}{$id}{category} ;  $uccat = uc $cat ;
    my $qnr   = $db{events}{$id}{event_quote_nr} ;
    my $uid   = $db{events}{$id}{user_id} ;
	my $title = ucfirst $cat ; $title =~ s/\_/ /g ;
	
	my $date_start = substr($start,0,10);
	my $date_end   = substr($end,0,10);
    
	if ($cat eq 'leave') { 
		$color = '#ff00ff' ; 
		my $leave_type = ucfirst $db{events}{$id}{type} ; 
		$title = $leave_type . ' Leave' ; 
	} elsif ($cat eq 'weekend_work') { 
		$start = &cal_set_time($db{events}{$id}{startdate},-24); $start =~ s/ /T/g ;
		$color = 'purple' ; 
	} elsif ($cat eq 'public_holiday') { 
		$start = substr($db{events}{$id}{startdate},0,10) . 'T00:00:00';
		$end   = substr($db{events}{$id}{enddate},0,10) . 'T23:59:59';
		$color = 'red' ; 
	} elsif ($cat eq 'available') { 
		$color = '#00FF00' ; 
	} elsif ($cat eq 'unavailable') { 
		$color = '#ff4136' ; # red
	} elsif ($cat eq 'other') { 
		$color = '#e3c728' ; # yellow
	# } elsif (substr($usertype,0,6) eq 'casual') { 
	} elsif ($is_operator) { 
		$color = '#efad1d' ; # orange
	} else { 
		$color = '#3498DB' ;
	}
		
	if ($qnr) {
		# $total_casuals_working{$date_start}{$date_end}{$casual_type{$uid}}{$qnr}++ if $cat eq 'available' ;	# count casuals subscribed for the event
		# $subscribed_names{$date_start}{$date_end}{$casual_type{$uid}}{$qnr} .= "$user_name{$uid}, " if $cat eq 'available' ; 
		$total_casuals_working{$date_start}{$date_end}{$qnr}++ if $cat eq 'available' ;	# count casuals subscribed for the event
		$subscribed_names{$date_start}{$date_end}{$qnr} .= "$user_name{$uid}, " if $cat eq 'available' ; 
	}
		
	# if (substr($usertype,0,6) eq 'casual') { 
	if ($is_operator && $operator_level < 2 && !$is_temp_calibrator) { 
		# next unless $cat eq 'available' or $cat eq 'unavailable' ; 
		if ($qnr) {
			# $show_available{$casual_type{$uid}}{$qnr}{$uid} =  $uccat . ' : ' . $db{events}{$id}{title} ;
			# $color_available{$casual_type{$uid}}{$qnr}{$uid} = $color ;
			# print "1. show_available  [casual_type =$casual_type{$uid}] [$date_start->$date_end, $qnr, $uid][if ($show_available{$date_start}{$date_end}{$operator{$_}}{$quote_nr}{$userid} and $in_the_past!=1 and $casual_type{$userid} eq $operator{$_})]\n" if $debug ;
			# $show_available{$date_start}{$date_end}{$casual_type{$uid}}{$qnr}{$uid} =  $uccat . ' : ' . $db{events}{$id}{title} ;
			# $show_available{$date_start}{$date_end}{$casual_type{$uid}}{$qnr}{$uid} =  $uccat ;
			# $color_available{$date_start}{$date_end}{$casual_type{$uid}}{$qnr}{$uid} = $color ;
			
			$show_available{$date_start}{$date_end}{$qnr}{$uid}  = $uccat ;
			$color_available{$date_start}{$date_end}{$qnr}{$uid} = $color ;
		}
		next;
	}
	
	if ($cat eq 'available' || $cat eq 'unavailable') { # skip events display
		next ; 
	} 
		
	my ($s_date,$s_time) = split(/\ /,$db{events}{$id}{startdate}) ; my ($s_ccyy,$s_mm,$s_dd) = split(/\-/,$s_date) ; my $s_txt = Date_to_Text($s_ccyy,$s_mm,$s_dd);  my ($s_hr,$s_mn,$s_sc) = split(/\:/,$s_time) ;
	my ($e_date,$e_time) = split(/\ /,$db{events}{$id}{enddate})   ; my ($e_ccyy,$e_mm,$e_dd) = split(/\-/,$e_date) ; my $e_txt = Date_to_Text($e_ccyy,$e_mm,$e_dd);  my ($e_hr,$e_mn,$e_sc) = split(/\:/,$e_time) ;
	
	my $disp_title = qq($title [$user_name{$db{events}{$id}{user_id}}] $s_txt - $e_txt) ;
	
	if (($cat eq 'public_holiday') or ($cat eq 'other')) { 
		my $notes = $db{events}{$id}{notes} ; $notes =~ s/"/'/g ;
		$disp_title = qq($title : $db{events}{$id}{notes}) ; 
	}
		
	$disp_title = &fix_json($disp_title) ;
	
	$json_ele = qq~{
                "id": "$id",
                "title": "$disp_title",
                "start": "$start",
                "end": "$end",
                "color": "$color",
                "url": "javascript:dlgMdl('/cgi-bin/scripts/dialog/events.pl?$id','$title','','medium-dialog');"
              },~ ;
	
	my $json_rep = repair_json($json_ele);
	
	$json .= qq~$json_rep,~ ;
		
    # $json .= qq~{
                # "id": "$id",
                # "title": "$disp_title",
                # "start": "$start",
                # "end": "$end",
                # "color": "$color",
                # "url": "javascript:dlgMdl('/cgi-bin/scripts/dialog/events.pl?$id','$title','','medium-dialog');"
              # },~ ;
    }
   
 # [				
    # {
        # title: 'Conference',
        # start: '2016-01-11',
        # end: '2016-01-13'
    # },			
    
# ]

# events: [
# {
    # title: 'Business Lunch',
    # start: '2016-01-03T13:00:00',
    # constraint: 'businessHours'
# },
# {
    # title: 'Meeting',
    # start: '2016-01-13T11:00:00',
    # constraint: 'availableForMeeting', // defined below
    # color: '#257e4a'
# },

# // areas where "Meeting" must be dropped
# {
    # id: 'availableForMeeting',
    # start: '2016-01-11T10:00:00',
    # end: '2016-01-11T16:00:00',
    # rendering: 'background'
# },
# {
    # id: 'availableForMeeting',
    # start: '2016-01-13T10:00:00',
    # end: '2016-01-13T16:00:00',
    # rendering: 'background'
# },

# // red areas where no events can be dropped
# {
    # start: '2016-01-24',
    # end: '2016-01-28',
    # overlap: false,
    # rendering: 'background',
    # color: '#ff9f89'
# },
# {
    # start: '2016-01-06',
    # end: '2016-01-08',
    # overlap: false,
    # rendering: 'background',
    # color: '#ff9f89'
# }

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

sub get_db_event_quotes {

unless ($param{'start'} && $param{'end'}) { return ; } 

&db_min_ro('event_systems','id,system_type',"`system_type`='fixed'",'','') ;

my $table = 'event_quotes' ;

# if ($is_schools_manager) {
	# @report_sql_or = () ;
	# foreach $_orig_id (keys %{$glob_orgids{$userid}}) {
		# push @report_sql_or, "organisation_ids LIKE '%$_orig_id%'"
	# }
	# my $sql_or = join(' OR ',@report_sql_or) ;
	# push @report_sql, "($sql_or)" ; 
# }

if ($is_installation_partner) {
	@report_sql_or = () ;
	foreach $_cust_id (keys %{$glob_custids{$userid}}) {
		push @report_sql_or, "quote_to = '$_cust_id'"
	}
	foreach $_reg_id (keys %{$glob_regids{$userid}}) {
		push @report_sql_or, "region_id = '$_reg_id'"
	}
	my $sql_or = join(' OR ',@report_sql_or) ;
	push @report_sql, "($sql_or)" if $sql_or ;  
	push @report_sql, "quote_accepted='1'" if $is_installation_partner ;  
} elsif ($is_schools_manager || ($is_operator && $operator_level > 1 && !$is_temp_calibrator)) {
	@report_sql_or = () ;
	foreach $_reg_id (keys %{$glob_regids{$userid}}) {
		push @report_sql_or, "region_id = '$_reg_id'"
	}
	my $sql_or = join(' OR ',@report_sql_or) ;
	push @report_sql, "($sql_or)" if $sql_or ;  
	push @report_sql, "quote_created='1'"  if $is_schools_manager ;  
	push @report_sql, "quote_accepted='1'" if $is_operator ;
# } elsif (substr($usertype,0,6) eq 'casual') { 
# } elsif ($is_operator && $operator_level < 2) { 
} elsif ($is_operator && $operator_level < 2 && !$is_temp_calibrator) { 
	# push @report_sql, "(operator_ids='$userid' OR operator_ids LIKE '%$userid,%' OR operator_ids LIKE '%,$userid%')" ; 
	push @report_sql, "(operator_ids LIKE '%$userid%')" ; 
	push @report_sql, "quote_accepted='1'" ; 	
} else {
	push @report_sql, "quote_accepted='1'" ; 
}

my $srch_where_sql = join(' AND ', @report_sql) ; my $sql_where = ($srch_where_sql) ? "AND $srch_where_sql" : '' ;

# &db_min_ro($table,'*',"date_to >= '$param{start}' AND date_from <= '$param{end}' $sql_where",'','') ;
&db_min_ro($table,'*',"((date_to >= '$param{start}' AND date_to <= '$param{end}') OR (date_from >= '$param{start}' AND date_from <= '$param{end}')) $sql_where",'','') ;

# &db_min_ro($table,'*',"date_to >= '$param{start}' AND date_from <= '$param{end}' AND quote_accepted = 1",'','') ;

# print "get_db_event_quotes  [SELECT * FROM event_quotes WHERE ((date_to >= '$param{start}' AND date_to <= '$param{end}') OR (date_from >= '$param{start}' AND date_from <= '$param{end}')) $sql_where]\n" if $debug ;

@colors = ('#00FF00','#6666FF','#FF33FF','#00FFFF','#2dc8ed','#FF6600','#28b62c') ;

foreach my $id (keys %{$db{$table}}) {
    my $start            = $db{$table}{$id}{date_from} ; $start =~ s/ /T/g ;
    my $end              = $db{$table}{$id}{date_to} ;   $end   =~ s/ /T/g ;
					     
	my $ref              = $db{$table}{$id}{ref} ;
	my $quote_nr         = $db{$table}{$id}{quote_nr} ;
	my $type             = $db{$table}{$id}{type} ; 
	my $notes            = $db{$table}{$id}{notes} ; $notes =~ s/\r\n|\n/ - /g; $title =~ s/\t/ /g;
	# my $operator_ids   = $db{$table}{$id}{operator_ids} ; 
	my @operator_ids     = split(/\,/,$db{$table}{$id}{operator_ids});
	   # $operator{1}    = $db{$table}{$id}{casual_worker} ; 
	   $nr_of_casuals{1} = $db{$table}{$id}{nr_of_casuals} ; 
	   # $casual_worker{2} = $db{$table}{$id}{casual_worker_2} ; 
	   # $nr_of_casuals{2} = $db{$table}{$id}{nr_of_casuals_2} ; 
	   
	# if (substr($usertype,0,6) eq 'casual') { 
	if ($is_operator && $operator_level < 2 && !$is_temp_calibrator) { 
		my $valid = 0 ;
		foreach $_opid (@operator_ids) { 
			# print "$_opid == $userid \n" if $debug ;
			if ($_opid == $userid) { $valid = 1 ; }
		}
		next unless $valid ;
	}
	   
	# print "1. ok_for_casual [$quote_nr] [operator=$db{$table}{$id}{casual_worker}] [nr_of_casuals=$db{$table}{$id}{nr_of_casuals}]  [casual_worker_2=$db{$table}{$id}{casual_worker_2}]  [nr_of_casuals_2=$db{$table}{$id}{nr_of_casuals_2}] \n" if $debug ;
	   
	# &common_debug("get_db_event_quotes - $ref -> $start -> $end");
	
	# print "get_db_event_quotes  [$ref -> $start -> $end]\n" if $debug ;
	   
	my $date_start = substr($start,0,10);
	my $date_end   = substr($end,0,10);
	
	my $ttl_operator_grps  = 0 ; for (1 .. 2) { $ttl_operator_grps++ ; }
	
	my $cnow     = $now_ccyymmdd . $now_hour . $now_min . $now_sec ;
	my $cstart   = $start ; $cstart =~ s/T//g ; $cstart =~ s/-//g ; $cstart =~ s/://g ;
	my $cend     = $end   ; $cend   =~ s/T//g ; $cend   =~ s/-//g ; $cend   =~ s/://g ;
	my $in_the_past = 0 ;
	 
	if ($cstart < $cnow && $cend < $cnow) { 
		$color = '#999999' ; 
		$in_the_past = 1 ;
	} else { 
		$color = '#FF66FF' ; 
	}
	
	if ($db{$table}{$id}{quote_cancelled}) { 
		$color = '#ff4136' ; # red
	} elsif ($db{$table}{$id}{quote_completed}) { 
		$color = '#51b529' ; # green
	} elsif ($db{$table}{$id}{quote_accepted}) { 
		$color = '#51cbf2' ; # blue
		# $tt_txt = 'Accepted' ;
		my @fixed_system_overall_statuses = split(/\;/,$db{$table}{$id}{fixed_system_overall_status}) ;
		my @event_system_ids              = split(/\;/,$db{$table}{$id}{event_system_id_multiple}) ;
		my $event_sys_cnt                 = 0 ; my $got_some_fixed = 0 ; # my $event_sys_test_status = 0 ; # my $color = '#164473' ; # Dark Blue
		
		print "1. quote_accepted  [fixed_system_overall_status=$db{$table}{$id}{fixed_system_overall_status}]  [event_system_id_multiple=$db{$table}{$id}{event_system_id_multiple}] \n" if $debug ;
		
		foreach my $sys_id (@event_system_ids) {			
			if ($db{event_systems}{$sys_id}{system_type} eq 'fixed') {
				
				print "1. quote_accepted  fixed [event_id=$sys_id]  [event_system_id_multiple=$db{$table}{$id}{event_system_id_multiple}] \n" if $debug ;
				
				# $tt_txt = 'Accepted and tests show All Clear for the Fixed Systems' if $tt_txt eq 'Accepted' ;
				$color = '#164473' unless $got_some_fixed ; # Dark Blue
				$got_some_fixed = 1 ;
				if ($fixed_system_overall_statuses[$event_sys_cnt] == 3) { # Not Clear
					$color  = '#5d09d3' ; # Dark Purple
					# $tt_txt = 'Accepted but Not Clear on some Fixed Systems' ;
				} elsif ($fixed_system_overall_statuses[$event_sys_cnt] == 2) { # Sound Issue
					$color  = '#5d092c' ;
					# $tt_txt = 'Accepted but Sound Issue found on some Fixed Systems' ;
				}
			}
			$event_sys_cnt++;						
		}
		# if ($got_some_fixed) { $style  = "style='background-color:#$fixed_status_color;border-color:#$fixed_status_color'";  }
	} elsif ($db{$table}{$id}{quote_rejected}) { 
		$color = '#ca0ad3' ; # purple
	} elsif ($db{$table}{$id}{quote_created}) { 
		$color = '#f6e305' ; # yellow
	} elsif ($db{$table}{$id}{quote_pending}) { 
		$color = '#eda52d' ; # orange
	} 
		
	# my $jstitle = 'EVENT: ' . $quote_nr ;
	my $jstitle = 'EVENT: ' . $ref . ' [' . $quote_nr . ']' ;
	
	# if (substr($usertype,0,6) eq 'casual') {
	if ($is_operator && $operator_level < 2 && !$is_temp_calibrator) {
		
		# %ok_for_casual = () ;
		$ok_for_casual = 0 ;
		
		print "1. ok_for_casual  [$ok_for_casual] [ttl_operator_grps=$ttl_operator_grps]\n" if $debug ;

		for (1 .. $ttl_operator_grps) {
			
			print "2. ok_for_casual  [$ok_for_casual]  [$_] [$operator{$_}]\n" if $debug ;
			
			# next unless $operator{$_} ;
			
			# print "2. ok_for_casual  [$date_start->$date_end, $quote_nr, $userid][show_available:$show_available{$date_start}{$date_end}{$quote_nr}{$userid} and in_the_past:$in_the_past!=1 and casual_type:$casual_type{$userid} eq operator:$operator{$_})]\n" if $debug ;
			# print "2. ok_for_casual  [in_the_past=$in_the_past]\n" if $debug ;
			
			# if ($show_available{$date_start}{$date_end}{$operator{$_}}{$quote_nr}{$userid} and $in_the_past!=1 and $casual_type{$userid} eq $operator{$_}) {		
			if ($show_available{$date_start}{$date_end}{$quote_nr}{$userid} and $in_the_past!=1) {		
				$jstitle .= ' [' . $show_available{$date_start}{$date_end}{$quote_nr}{$userid} . ']' ;
				$color    = $color_available{$date_start}{$date_end}{$quote_nr}{$userid} ;
				$ok_for_casual = 1 ;
				print "3. ok_for_casual [$ok_for_casual] [$show_available{$date_start}{$date_end}{$quote_nr}{$userid}] [$color] $date_start->$date_end, $quote_nr, $userid \n" if $debug ;
			# } elsif ($in_the_past!=1 and $casual_type{$userid} eq $operator{$_}) {
			} elsif ($in_the_past!=1) {
				$ok_for_casual = 1 ;
			} else {
				$color = '#efad1d' ;
			}
			
			print "4. ok_for_casual [$ok_for_casual] $operator{$_} = $ok_for_casual{$operator{$_}} \n" if $debug ;
			last if $ok_for_casual ;			
		}
		
		print "5. ok_for_casual [$ok_for_casual] \n" if $debug ;
		next unless $ok_for_casual ;
	}
	
	# if ($ref and $show_available{$date_start}{$date_end}{$quote_nr}{$userid} eq '') {
		# $jstitle .= " [$ref]"
	# }
		
	$jstitle =~ s/\'//g ; $jstitle =~ s/ +/ /; $jstitle =~ s/\r\n|\n/ - /g; $jstitle =~ s/\t/ /g;
	
	my $link = qq~javascript:editEventQuote('$quote_nr');~;
	
	# for (1 .. $ttl_operator_grps) {
		# $operators_subscribed{$_} = sprintf("%0.0f",$total_casuals_working{$date_start}{$date_end}{$operator{$_}}{$quote_nr}) ;
		
		# my $snames = $subscribed_names{$date_start}{$date_end}{$operator{$_}}{$quote_nr} ;		
		# chop $snames ; chop $snames ;		
		# $operator_names{$_} = $snames if $snames ;
	# }
	
	@disp_operators = () ; $add_operators = '' ;
	
	# unless (substr($usertype,0,6) eq 'casual') { 
	if ($glod_user_level > 1) {
	
		foreach $_opid (@operator_ids) { 
			push @disp_operators, $user_name{$_opid} ;
		}
		
		my $operator_names_list = join(", ", @disp_operators) ;
		$add_operators = ($operator_names_list) ? ' [' . $operator_names_list . ']' : '' ;
		
		# for (1 .. $ttl_operator_grps) {
			# # $jstitle   .= " [$operators_subscribed{$_}/$nr_of_casuals{$_} casual $operator{$_} subscribed]" if $nr_of_casuals{$_} > 0 ; 
			# # $add_operators .= " [$operator_names{$_} ($operator{$_})]" if $operator_names{$_} and $operator{$_} ; 
			# $add_operators .= " [$operators_subscribed{$_}/$nr_of_casuals{$_} casual $operator{$_} subscribed]" if $operator_names{$_} and $operator{$_} ; 
		# }
	}
	
	# if (substr($usertype,0,6) eq 'casual') { 
		# $link = qq~javascript:dlgMdl('/cgi-bin/scripts/dialog/events.pl?event&$quote_nr','$jstitle','','medium-dialog');~; 
	# } 
	
	for (1 .. $ttl_operator_grps) {
		# if ($operators_subscribed{$_} >= $nr_of_casuals{$_} and $in_the_past!=1 and $show_available{$date_start}{$date_end}{$quote_nr}{$userid} eq '' and $casual_type{$userid} eq $operator{$_}) {
		if ($operators_subscribed{$_} >= $nr_of_casuals{$_} and $in_the_past!=1 and $show_available{$date_start}{$date_end}{$quote_nr}{$userid} eq '') {
			# $color    = '#ff4136' ; # red
			# $jstitle .= " [fully SUBSCRIBED for casual $operator{$_}]" ; 
			$jstitle .= " [fully SUBSCRIBED for operators]" ; 
		}
	}
	
	# "title": "CASUALS : $add_operators",
	# "title": "$ref [$quote_nr] : $add_operators",
	
	# if ($add_operators) {
		# # $added_color = ($in_the_past) ? '#999999' : '#00FF00' ;
		# # $added_color = ($in_the_past) ? '#999999' : '#35e2e2' ;
		# $added_color = ($in_the_past) ? '#999999' : $color ;
		# $json .= qq~{
					# "id": "$id",
					# "title": "$ref []",
					# "start": "$start",
					# "end": "$end",
					# "color": "$added_color",
					# "url": "javascript:dlgMdl('/cgi-bin/scripts/dialog/events.pl?event&$quote_nr','$jstitle','','medium-dialog');"
				  # },~ ;
	# # } elsif ($add_operators eq '' and $operator{$_} eq '') {
	# } else {
		# $color = ($color) ? $color : '#efad1d' ;
		
		# my $cal_title = () ? $ref : ;
		
		# $added_color = ($in_the_past) ? '#999999' : $color ;
		
		$json .= qq~{
					"id": "$id",
					"title": "$ref$add_operators",
					"start": "$start",
					"end": "$end",
					"color": "$color",
					"url": "javascript:dlgMdl('/cgi-bin/scripts/dialog/events.pl?event&$quote_nr','$jstitle','','medium-dialog');"
				  },~ ;
	# }
	
    # $json .= qq~{
                # "id": "$id",
                # "title": "$jstitle",
                # "start": "$start",
                # "end": "$end",
                # "color": "$color",
                # "url": "$link"
              # },~ ;
    }

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

# sub get_db_leave {

# unless (($param{start}) and ($param{end})) { return ; } 

# &db_min_ro('users','*','','','') ;
# foreach my $id (keys %{$db{users}}) { $user_name{$id} = $db{users}{$id}{name} ; }

# # &db_min_ro('events','*',"`startdate` >= '$param{start} 00:00:00' AND `startdate` <= '$param{end} 23:59:59'",'','') ;
# &db_min_ro('events','*',"((`startdate` >= '$param{start} 00:00:00' AND `startdate` <= '$param{end} 23:59:59') OR ('$param{start} 23:59:59' >= `startdate` AND '$param{start} 00:00:00' <= `enddate`))",'','') ;

# foreach my $id (keys %{$db{events}}) {

	# &common_debug("get_db_leave - $db{events}{$id}{category} -> $db{events}{$id}{title}");

	# unless (($db{events}{$id}{category} eq 'leave') or 
			# ($db{events}{$id}{category} eq 'weekend_work') or 
			# ($db{events}{$id}{category} eq 'public_holiday') or 
			# ($db{events}{$id}{category} eq 'other')) { next; } # don't display birthdays on calendar
	
	# # if (($username ne 'rory') and ($db{events}{$id}{category} eq 'weekend_work')) { next ; }

    # my $start = $db{events}{$id}{startdate} ; $start =~ s/ /T/g ;
    # my $end   = $db{events}{$id}{enddate}   ; $end   =~ s/ /T/g ;
	# # my $title = $db{events}{$id}{title} ;
	# my $title = ucfirst $db{events}{$id}{category} ; $title =~ s/\_/ /g ;
    
	# # if ($db{events}{$id}{category} eq 'leave') { $color = '#338e3c' ; my $leave_type = ucfirst $db{events}{$id}{type} ; $title = $leave_type . ' Leave' ; } else { $color = '#3498DB' ; }
    
	# if ($db{events}{$id}{category} eq 'leave') { 
		# $color = '#ff00ff' ; 
		# my $leave_type = ucfirst $db{events}{$id}{type} ; 
		# $title = $leave_type . ' Leave' ; 
		# } 
	# elsif ($db{events}{$id}{category} eq 'weekend_work') { 
		# $start = &cal_set_time($db{events}{$id}{startdate},-24); $start =~ s/ /T/g ;
		# # $color = '#fbc808' ; 
		# $color = 'purple' ; 
		# } 
	# elsif ($db{events}{$id}{category} eq 'public_holiday') { 
		# $start = substr($db{events}{$id}{startdate},0,10) . 'T00:00:00';
		# $end   = substr($db{events}{$id}{enddate},0,10) . 'T23:59:59';
		# $color = 'red' ; 
		# } 
	# elsif ($db{events}{$id}{category} eq 'other') { 
		# $color = '#e3c728' ; # yellow
		# } 
	# else
		# { 
		# $color = '#3498DB' ; 
		# # $color = '#ff00ff' ; 
		# # $color = '#fbc808' ; 
		# }
		
	# my ($s_date,$s_time) = split(/\ /,$db{events}{$id}{startdate}) ; my ($s_ccyy,$s_mm,$s_dd) = split(/\-/,$s_date) ; my $s_txt = Date_to_Text($s_ccyy,$s_mm,$s_dd);  my ($s_hr,$s_mn,$s_sc) = split(/\:/,$s_time) ;
	# my ($e_date,$e_time) = split(/\ /,$db{events}{$id}{enddate})   ; my ($e_ccyy,$e_mm,$e_dd) = split(/\-/,$e_date) ; my $e_txt = Date_to_Text($e_ccyy,$e_mm,$e_dd);  my ($e_hr,$e_mn,$e_sc) = split(/\:/,$e_time) ;
	
	# my $disp_title = qq($title [$user_name{$db{events}{$id}{user_id}}] $s_txt - $e_txt) ;
	
	# if (($db{events}{$id}{category} eq 'public_holiday') or ($db{events}{$id}{category} eq 'other')) { 
		# my $notes = $db{events}{$id}{notes} ; $notes =~ s/"/'/g ;
		# $disp_title = qq($title : $db{events}{$id}{notes}) ; }
		
	# $disp_title = &fix_json($disp_title) ;
	
	# # "title": "$title [$user_name{$db{events}{$id}{user_id}}] $s_hr:$s_mn on $s_txt - $e_hr:$e_mn on $e_txt ",
		
    # $json .= qq({
                # "id": "$id",
                # "title": "$disp_title",
                # "start": "$start",
                # "end": "$end",
                # "color": "$color",
                # "url": "javascript:dlgMdl('/cgi-bin/scripts/dialog/events.pl?$id','$title','','medium-dialog');"
              # },) ;
    # }
   
# } #------------------------------------------------------------------------------------------

sub fix_json {
	
my ($json) = @_ ;

$json =~ s/\r\n|\n/ - /g; 
$json =~ s/\t/ /g; 
$json =~ s/"/'/g; 
$json =~ s/\&/\&amp;/g;

return ($json) ;
	
} #------------------------------------------------------------------------------------------

sub cal_set_time {

my ($datetime,$hr_diff) = @_ ; # 2015-10-06 17:35:39

unless ($hr_diff) { $hr_diff = 1 ; }

my ($date,$time) = split(/ /,$datetime) ;

my $dy = substr($date,0,4) ;
my $dm = substr($date,5,2) ;
my $dd = substr($date,8,2) ;

my ($th,$tm,$ts) = split(/:/,$time) ;

my ($syear,$smonth,$sday,$shour,$smin,$ssec) = Add_Delta_DHMS($dy,$dm,$dd,$th,$tm,$ts,0,$hr_diff,0,0); # add 1 hour

$smonth  = sprintf("%02s", $smonth) ;
$sday    = sprintf("%02s", $sday) ;
$shour   = sprintf("%02s", $shour) ;
$smin    = sprintf("%02s", $smin) ;
$ssec    = sprintf("%02s", $ssec) ;

return("$syear-$smonth-$sday $shour:$smin:$ssec");

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

# use db ;
# use db_min ;
use common ;
use session ;
use today ;

1;