#!/usr/bin/perl
# Copyright 2001-2019 Leslie Richardson
# This file is part of Open Admin for Schools.
my %lex = ( 'Continue' => 'Continue',
'Error' => 'Error',
'Main' => 'Main',
'Staff' => 'Staff',
'Leave' => 'Leave',
'Hours' => 'Hours',
'Position' => 'Position',
'Add' => 'Add',
);
my $self = 'addStaffLeave.pl';
use DBI;
use CGI;
use Time::JulianDay;
eval require "../../etc/admin.conf";
if ( $@ ) {
print $lex{Error}. ": $@
\n";
die $lex{Error}. ": $@\n";
}
my $q = new CGI;
print $q->header( -charset, $charset );
my %arr = $q->Vars;
my $dsn = "DBI:$dbtype:dbname=$dbase";
my $dbh = DBI->connect($dsn,$user,$password);
$dbh->{mysql_enable_utf8} = 1;
# load staffpay vars: @pay_LeaveAreas
my $sth = $dbh->prepare("select id, datavalue from conf_system where sectionname = 'staffpay'");
$sth->execute;
if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; }
while ( my ($id, $datavalue) = $sth->fetchrow ) {
eval $datavalue;
if ( $@ ) {
print "$lex{Error}: $@
\n";
die "$lex{Error}: $@\n";
}
}
my $title = "Add $lex{Staff} $lex{Leave}";
print qq{$doctype\n
$title\n};
print qq{\n};
print qq{$chartype\n[ $lex{Main} ]\n};
print qq{$title
\n};
if ( not $arr{page} ) {
showStartPage();
} elsif ( $arr{page} == 1 ) {
delete $arr{page};
selectStaff();
} elsif ( $arr{page} == 2 ) {
delete $arr{page};
addLeave();
}
#-----------
sub addLeave {
#-----------
# foreach my $key ( sort keys %arr ) { print "K:$key V:$arr{$key}
\n"; }
my %leaves;
foreach my $key ( keys %arr ) {
my ( $idx,$leave) = split(':', $key );
if ( $idx eq 'LV' ) {
$leaves{$leave} = $arr{$key};
delete $arr{$key};
}
}
# print "Leaves:", %leaves, "
\n";
# print qq{
\n};
# foreach my $key ( sort keys %arr ) { print "K:$key V:$arr{$key}
\n"; }
# Load Staff
my (@staff, %staffname, %position);
my $sth1 = $dbh->prepare("select field_value from staff_multi
where userid = ? and field_name = 'position'");
my $sth = $dbh->prepare("select lastname, firstname, userid from staff
order by lastname, firstname");
$sth->execute;
if ($DBI::errstr){ print $DBI::errstr; die "$DBI::errstr\n";}
while ( my $ref = $sth->fetchrow_hashref ) {
my %r = %$ref;
push @staff, $r{userid};
$staffname{$r{userid}} = qq{$r{lastname}, $r{firstname}};
$sth1->execute( $r{userid} );
if ($DBI::errstr){ print $DBI::errstr; die "$DBI::errstr\n";}
my $position = $sth1->fetchrow;
$position{$r{userid}} = $position
}
my $sth = $dbh->prepare("insert into staff_leave ( hours, userid, category )
values ( ?, ?, ? )");
my $sth2 = $dbh->prepare("select count(*) from staff_leave where userid = ? and category = ?");
# Add Records
foreach my $userid ( sort keys %arr ) {
foreach my $leave ( keys %leaves ) {
my $hours = $leaves{$leave};
# check for existing record.
$sth2->execute( $userid, $leave );
if ($DBI::errstr){ print $DBI::errstr; die "$DBI::errstr\n";}
my $count = $sth2->fetchrow;
if ( $count ) {
print qq{Leave exists for $staffname{$userid} - $leave - Skipping
\n};
} else { # insert a record.
$sth->execute( $hours, $userid, $leave);
if ($DBI::errstr){ print $DBI::errstr; die "$DBI::errstr\n";}
print qq{Add - $hours Hours, $staffname{$userid} - $leave
\n};
}
} # end of leave loop
} # end of userid loop
print qq{Records Added
\n};
print qq{[ $lex{Main} ]\n};
print qq{\n};
exit;
} # end of addLeave;
#--------------
sub selectStaff {
#--------------
# foreach my $key ( sort keys %arr ) { print "K:$key V:$arr{$key}
\n"; }
print qq{\n};
print qq{Category | Hours | \n};
foreach my $key ( keys %arr ) {
print qq{
---|
$key | $arr{$key} |
\n};
}
print qq{
\n};
# Start Form
print qq{\n};
print qq{