__HTML__
return $return_html;
}
################################################################
### Get the HTML user table show in upper left corner
################################################################
sub get_account_table_with_update_link{
my $user_key = shift;
my $link = shift;
my %user_info = get_account_info($user_key);
my $return_html = <<__HTML__;
__HTML__
return $return_html;
}
################################################################
### Get the HTML user table show in upper left corner
################################################################
sub get_account_table{
my $user_key = shift;
my %user_info = get_account_info($user_key);
my $return_html = <<__HTML__;
__HTML__
return $return_html;
}
############################################
# Get adjustment tabel cell for payment screen
############################################
sub get_fees_table_cell_paypal_string_and_total{
my $player_key = shift;
my $style = shift;
my $return_cell = "";
my $paypal_string = "";
my $total = 0;
my $total_fee = 0;
my $total_paid = 0;
my %return_hash = ();
my %total_due = 0;
my %adjustment_info = ();
my $return_string = "";
my $check_id = "";
my $plus_minus = "";
my $first = 1;
############################################
#Get the adjustments for that player
############################################
($total, $total_fee, $total_paid, %adjustment_info) = get_adjustment_info($player_key);
############################################
# Loop through all the adjustments to get information about the fees
############################################
foreach my $count (sort {$a <=> $b} (keys %adjustment_info)){
############################################
# If the UPDATE ID is -1 or -3 it was an autmated fee.
# So we need to lookup what the fee is using the CHECK_ID field
############################################
if(($adjustment_info{$count}{"UPDATE_ID"} == -1)||($adjustment_info{$count}{"UPDATE_ID"} == -3)){
$checkout_override = get_checkout_override($adjustment_info{$count}{"CHECK_ID"}, $player_key);
if($checkout_override eq ""){
$check_id = $input_value = get_input_options($adjustment_info{$count}{"CHECK_ID"}, $player_key);
}else{
$check_id = $checkout_override;
}
}elsif($adjustment_info{$count}{"UPDATE_ID"} == -5){ #Multiplayer Discount
$check_id = get_multiplayer_discount_description($adjustment_info{$count}{"CHECK_ID"});
}else{
$check_id = $adjustment_info{$count}{"CHECK_ID"};
}
############################################
# This is to get the proper + and - for the paypal and pay table cell
# because a - number would look like $-10 we just strip it and make it -$10
# And then for paypal we do not want a + sign to start with.
############################################
my $adjustment_amount = $adjustment_info{$count}{"AMOUNT"} + 0;
my $abs_adjustment_amount = abs($adjustment_amount);
$adjustment_amount = sprintf("%.2f", $adjustment_amount);
my $display_plus_minus = "";
$plus_minus = "";
if(!(($adjustment_info{$count}{"UPDATE_ID"} == -2)||($adjustment_info{$count}{"UPDATE_ID"} == -3))){
if($adjustment_amount >= 0){
if($first == 0){
$plus_minus = " + ";
}
############################################
# Set string to first so we do not add a plus or minus in first item
############################################
$first = 0;
}else{
$plus_minus = " - ";
$display_plus_minus = "-";
$first = 0;
}
if(($plus_minus ne ' - ')||($adjustment_info{$count}{"UPDATE_ID"} == -5)){ #Only show fees and multiplayer discounts
#if($adjustment_info{$count}{"UPDATE_ID"} == -1){
############################################
# Get the paypal string
############################################
$paypal_string .= $plus_minus . '$' . $abs_adjustment_amount . " " . $check_id;
############################################
# This creates the cell for the pay table
############################################
$abs_adjustment_amount = sprintf("%.2f", $abs_adjustment_amount);
$return_cell .= $display_plus_minus . '$' . $abs_adjustment_amount . " " . $check_id . " ";
}
}
}
############################################
# Calculate return value and set return hash
############################################
$abs_total_paid = abs($total_paid);
$total_due = $total_fee + $total_paid;
$total_fee = sprintf("%.2f", $total_fee);
$total_paid = sprintf("%.2f", $total_paid);
$total_due = sprintf("%.2f", $total_due);
$abs_total_paid = sprintf("%.2f", $abs_total_paid);
#print "TOTAL PAID: $total_due \n";
$return_hash{"FEES_CELL"} = "
" . $return_cell . "
\n";
$return_hash{"TOTAL_FEE_CELL"} = "
\$" . $total_fee . "
\n";
$return_hash{"TOTAL_PAID_CELL"} = "
\$" . $abs_total_paid . "
\n";
$return_hash{"TOTAL_DUE_CELL"} = "
\$" . $total_due . "
\n";
$return_hash{"TOTAL_FEE"} = $total_fee;
$return_hash{"TOTAL_PAID"} = $total_paid;
$return_hash{"TOTAL_DUE"} = $total_due;
$return_hash{"PAYPAL_STRING"} = $paypal_string;
return %return_hash;
}
############################################
# Get adjustment info for one player
############################################
sub get_adjustment_info{
my $player_key = shift;
my $return_string = "";
my %return_hash = ();
my $total = 0;
my $total_fee = 0;
my $total_paid = 0;
my $count = 1;
my $dbh = get_dbh();
################################################################
# Query DB to verify Session
################################################################
my $query = "SELECT `Key`, `Amount`, `Check_ID`, `Update_Account_ID`, `Date` FROM `Reg_All_Transaction` WHERE `Player_ID` = '$player_key' ORDER BY `Key`";
#print "QUERY: $query \n";
my $sth = $dbh->prepare($query);
$sth->execute();
$sth->bind_columns(\$Key, \$Amount, \$Check_ID, \$Update_Account_ID, \$Date);
while($sth->fetch()) {
$return_hash{$count}{"KEY"} = $Key;
$return_hash{$count}{"AMOUNT"} = $Amount;
$return_hash{$count}{"CHECK_ID"} = $Check_ID;
$return_hash{$count}{"UPDATE_ID"} = $Update_Account_ID;
$return_hash{$count}{"DATE"} = $Date;
if(!(($Update_Account_ID == -2)||($Update_Account_ID == -3)||($Update_Account_ID == -5))){
if($Amount > 0){
$total_fee = $total_fee + $Amount;
}else{
$total_paid = $total_paid + $Amount;
}
$total = $total + $Amount;
}elsif($Update_Account_ID == -5){
$total_fee = $total_fee + $Amount;
}
$count++;
}
#if($player_key == 1409){
#print "TOTAL: $total T_PAID: $total_paid FEE: $total_fee \n";
#}
$sth->finish();
$dbh->disconnect;
return $total, $total_fee, $total_paid, %return_hash;
}
############################################
# Get reg definition info for one cell
############################################
sub get_specific_reg_definition{
my $org_id = shift;
my $reg_year = shift;
my $player_id = shift;
my $reg_key = shift;
my %return_hash = ();
my $dbh = get_dbh();
################################################################
# Query DB to verify Session
################################################################
my $query = "SELECT `Key`,`Description_Text`,`Input_Type`,`Selection_Link`,`Validation_Link`,`Required`,`Order` FROM `Reg_Definition` WHERE `Org_ID` = '$org_id' AND `Reg_Year` = '$reg_year' AND `Key` = '$reg_key';";
my $sth = $dbh->prepare($query);
#print "$query \n";
$sth->execute();
$sth->bind_columns(\$Key,\$Description_Text,\$Input_Type,\$Selection_Link,\$Validation_Link,\$Required,\$Order);
while($sth->fetch()) {
$return_hash{"DESCRIPTION_TEXT"} = $Description_Text;
$return_hash{"INPUT_TYPE"} = $Input_Type;
$return_hash{"SELECTION_LINK"} = $Selection_Link;
$return_hash{"VALIDATION_LINK"} = $Validation_Link;
$return_hash{"REQUIRED"} = $Required;
$return_hash{"ORDER"} = $Order;
}
# disconnect from database
$sth->finish();
$dbh->disconnect;
return %return_hash;
}
############################################
# Return description text for multiplayer discount
############################################
sub get_multiplayer_discount_description{
my $multiplayer_discount_id = shift;
my $dbh = get_dbh();
my $query = "SELECT `Description` FROM `Reg_Multi_Player_Discount` WHERE `Key` = '$multiplayer_discount_id'";
#print "QUERY: $query \n";
my $sth = $dbh->prepare($query);
$sth->execute();
my $description = "default discount";
while (my $hash_ref = $sth->fetchrow_hashref) {
$description = $hash_ref->{Description};
}
#print "PLAYER COUNT: $player_count \n";
# disconnect from database
$dbh->disconnect;
$sth->finish();
return $description;
}
############################################
# This creates the paypal and html for the checkout table.
############################################
sub get_nonconfirmed_user_table{
my $user_key = shift;
my %user_info = get_account_info($user_key);
my %player_info = get_player_info($user_key);
my $num_players = 0;
my $row_style = "cal_events3";
my $user_table = get_checkout_header("Pre-Registered Players");
my $user_table = "
Pre-Registered Players
\n";
my %status_hash = get_status_hash($org_id, $reg_year);
$checkout_header .= "
Name
Status
\n";
############################################
# Loop through players to create the display cell, paypal info and calculate totals
############################################
foreach my $user_number (sort {$a <=> $b} (keys %player_info)){
$num_players++;
my $this_player_confirmed_state = $player_info{$user_number}{'PLAYER_CONFIRMED'}; # Note that this is really a state
my $this_player_confirmed = $status_hash{$this_player_confirmed_state}{"ACCEPTED_STATE"};
my $assigned_league = $player_info{$user_number}{'ASSIGNED_LEAGUE'};
my $assigned_team = $player_info{$user_number}{'ASSIGNED_TEAM'};
my $player_key = $player_info{$user_number}{'KEY'};
#my $current_state = "Pre-Registration Only -$this_player_confirmed-$this_player_confirmed_state--$assigned_team-$assigned_league";
my $current_state = "Pre-Registration Only";
if($this_player_confirmed == 1){
my $player_team = get_input_options(1117, $player_key);
#$current_state = "Accepted -$player_team - $this_player_confirmed-$this_player_confirmed_state--$assigned_team-$assigned_league";
$current_state = "Accepted ($player_team)";
}
############################################
# Get the cell with a list of what is due for player, plus the paypal string
# that gets sent to have an itemized list on pay pal.
############################################
my $player_row = "
\n";
return $user_table;
}
############################################
# This creates the paypal and html for the checkout table.
############################################
sub get_checkout_table{
my $user_key = shift;
my $by_check = shift;
my %user_info = get_account_info($user_key);
my %player_info = get_player_info($user_key);
my $pay_pal_count = 1;
my $num_players = 0;
my $total_cost = 0;
my $walk_in_fee = 0;
my $walk_in_amount = 25;
my $adjustment_string = "";
if($by_check == 0){
$transaction_id = get_transaction_id();
}
my $user_table = get_checkout_header("Registered Players");
my $pay_pal = get_paypal_const();
############################################
# Loop through players to create the display cell, paypal info and calculate totals
############################################
foreach my $user_number (sort {$a <=> $b} (keys %player_info)){
$num_players++;
my $fees_cell = "";
my $total = 0;
my $player_total = 0; # this is just in case there are other adjustments
my $player_key = $player_info{$user_number}{"KEY"};
#my $amount_paid = $player_info{$user_number}{"PAID"};
############################################
# Get the cell with a list of what is due for player, plus the paypal string
# that gets sent to have an itemized list on pay pal.
############################################
my ($player_row, %checkout_hash) = get_checkout_row_for_player_and_total_due_and_pay_pal($player_key, $player_info{$user_number}{"FIRSTNAME"}, $player_info{$user_number}{"LASTNAME"}, "cal_events3");
my $amount_due = $checkout_hash{"TOTAL_DUE"};
my $paypal_string = $checkout_hash{"PAYPAL_STRING"};
my $total_paid = $checkout_hash{"TOTAL_PAID"};
$user_table .= $player_row;
#$amount_due = .02;
############################################
# Store temporary paypal record. This will not be valid until the paypal tx is finished.
############################################
if($by_check == 0){
insert_tmp_paypal($user_key, $player_key, $amount_due, $transaction_id);
}
############################################
# Add player specific paypal info
############################################
$pay_pal .= "\n";
$pay_pal .= "\n";
$pay_pal_count++;
############################################
# Keep running total of amount due for user
############################################
$total_cost = $amount_due + $total_cost;
}
############################################
# Finish user table
############################################
$total_cost = sprintf("%.2f", $total_cost);
$user_table .= "
TOTAL DUE: \$$total_cost
\n";
$user_table .= "\n";
############################################
# If its not pay by check, create form for submitting
############################################
if($by_check != 1){
$user_table .= "\n";
}
############################################
# If its not pay by check, give them thier payment options. Notice that
# this is stored in the pay_pal portion. I think this is becuase it gets
# printed to the screen after the others.
############################################
$pay_pal .= "\n";
$pay_pal .= "\n";
if($min_partial_pay == 0){
$pay_pal .= "\n";
}else{
$pay_pal .= "\n";
}
$pay_pal .= "
\n";
$pay_pal .= "\n";
############################################
# If it is pay by check. Display where to send it. Stored in var
############################################
if($by_check == 1){
if(($org_id == 167)||($org_id == 13)){ # Plymouth required forms
#$user_table .= "
$pay_by_check_message \n";
$user_table .= "";
}
return $user_table, $pay_pal;
}
############################################
# Check for discount for user (parent)
############################################
sub check_for_multiplayer_discounts_all_users{
my $user_key = shift;
my $player_key = shift;
my $player_count = shift;
my $dbh = get_dbh();
my $query = "SELECT `Key`, `Description`, `Discount` FROM `Reg_Multi_Player_Discount` WHERE `Org_ID` = '$org_id' AND `Reg_Year` = '$reg_year' AND `Number_Of_Reg_Players` <= '$player_count' AND `Max_Reg_Players` >= '$player_count'";
#print "QUERY: $query \n";
my $sth = $dbh->prepare($query);
$sth->execute();
while (my $hash_ref = $sth->fetchrow_hashref) {
my $description = $hash_ref->{Description};
my $discount = $hash_ref->{Discount};
my $key = $hash_ref->{Key};
#print "DESC: $description, DIS: $discount \n";
insert_adjustment($user_key, $player_key, $discount, $key, -5); #-5 just means that its a multiplayer discount
}
#print "PLAYER COUNT: $player_count \n";
# disconnect from database
$dbh->disconnect;
$sth->finish();
}
############################################
# This creates the paypal and html for the checkout table.
############################################
sub add_multi_player_discount_all_players{
my $user_key = shift;
my %player_info = get_player_info($user_key);
############################################
# Loop through players to check for discounts
############################################
my $num_players = 0;
foreach my $user_number (sort {$a <=> $b} (keys %player_info)){
$num_players++;
my $player_key = $player_info{$user_number}{"KEY"};
check_for_multiplayer_discounts_all_users($user_key, $player_key, $num_players);
}
}
############################################
# Check to see if multiplayer discounts
# Change because a player was removed
############################################
sub remove_all_multiplayer_discounts_after_remove_player{
my $user_key = shift;
my $player_count = 1; # Remove all, there should not be any less then 1?
my $dbh = get_dbh();
###################################
# Remove any discounts that require
# that require more players
# then the user now has
###################################
my $query = "SELECT `Key`, `Description`, `Discount` FROM `Reg_Multi_Player_Discount` WHERE `Org_ID` = '$org_id' AND `Reg_Year` = '$reg_year' AND `Number_Of_Reg_Players` > '$player_count'";
#my $query = "SELECT `Key`, `Description`, `Discount` FROM `Reg_Multi_Player_Discount` WHERE `Org_ID` = '$org_id' AND `Reg_Year` = '$reg_year' AND `Number_Of_Reg_Players` = '$player_count_before_remove'";
#print "QUERY: $query \n";
my $sth = $dbh->prepare($query);
$sth->execute();
############################################
# Loop through discounts that need to be removed
# for discountwwhere the min is greater then
# the current number of players.
############################################
while (my $hash_ref = $sth->fetchrow_hashref) {
my $description = $hash_ref->{Description};
my $discount = $hash_ref->{Discount};
my $key = $hash_ref->{Key};
change_discounts_to_removed_state($user_key, $key);
#print "DESC: $description, DIS: $discount \n";
#insert_adjustment($user_key, $player_key, $discount, $key, -5); #-5 just means that its a multiplayer discount
}
#print "PLAYER COUNT: $player_count \n";
# disconnect from database
$sth->finish();
$dbh->disconnect;
}
############################################
# Look for any discounts that do not
# meet player requirement and adjust
# them in db
############################################
sub change_discounts_to_removed_state{
my $user_key = shift;
my $discount_key = shift;
my $dbh = get_dbh();
########################################
# Select items where Discount = the key for that user
# and the update account is -5 for discount
########################################
my $query = "SELECT `Key`, `Player_ID`, `Amount` FROM Reg_All_Transaction WHERE `Check_ID` = '$discount_key' AND `Account_ID` = '$user_key' AND `Update_Account_ID` = '-5'";
#print "QUERY: $query \n";
my $sth = $dbh->prepare($query);
$sth->execute();
while (my $hash_ref = $sth->fetchrow_hashref) {
my $player_id = $hash_ref->{Player_ID};
my $amount = $hash_ref->{Amount};
my $key = $hash_ref->{Key};
my $option_change_amount = 0 - $amount;
insert_adjustment($user_key, $player_id, $option_change_amount, "MP Discount Removal 3", -2); #-2 just means that its an Option Change
update_adjustment_account_to_show_change($key);
}
# disconnect from database
$dbh->disconnect;
$sth->finish();
}
############################################
# Check to see if multiplayer discounts
# Change because a player was removed
############################################
sub verify_multiplayer_discounts_after_remove_player{
my $user_key = shift;
my $remove_player_key = shift;
my $player_count = get_player_count($user_key);
my $dbh = get_dbh();
###################################
# Remove any discounts that require
# that require more players
# then the user now has
###################################
my $query = "SELECT `Key`, `Description`, `Discount` FROM `Reg_Multi_Player_Discount` WHERE `Org_ID` = '$org_id' AND `Reg_Year` = '$reg_year' AND `Number_Of_Reg_Players` > '$player_count'";
#my $query = "SELECT `Key`, `Description`, `Discount` FROM `Reg_Multi_Player_Discount` WHERE `Org_ID` = '$org_id' AND `Reg_Year` = '$reg_year' AND `Number_Of_Reg_Players` = '$player_count_before_remove'";
#print "QUERY: $query \n";
my $sth = $dbh->prepare($query);
$sth->execute();
############################################
# Loop through discounts that need to be removed
# for discountwwhere the min is greater then
# the current number of players.
############################################
while (my $hash_ref = $sth->fetchrow_hashref) {
my $description = $hash_ref->{Description};
my $discount = $hash_ref->{Discount};
my $key = $hash_ref->{Key};
check_for_discounts_that_need_to_be_removed($user_key, $key);
#print "DESC: $description, DIS: $discount \n";
#insert_adjustment($user_key, $player_key, $discount, $key, -5); #-5 just means that its a multiplayer discount
}
#print "PLAYER COUNT: $player_count \n";
# disconnect from database
$sth->finish();
$dbh->disconnect;
############################################
# Check for chance that the removed user
# was part of an acitve discount and was not
# the player that was discounted in which
# case we need to remove on of the discount
# from another player
############################################
my $dbh2 = get_dbh();
my $query2 = "SELECT `Key`, `Description`, `Discount` FROM `Reg_Multi_Player_Discount` WHERE `Org_ID` = '$org_id' AND `Reg_Year` = '$reg_year' AND `Number_Of_Reg_Players` <= '$player_count'";
#print "QUERY: $query2 \n";
my $sth2 = $dbh2->prepare($query2);
$sth2->execute();
############################################
# Loop through discounts that need to be removed
############################################
while (my $hash_ref2 = $sth2->fetchrow_hashref) {
my $description = $hash_ref2->{Description};
my $discount = $hash_ref2->{Discount};
my $key = $hash_ref2->{Key};
my ($amount, $player_discount_key) = get_player_discount($remove_player_key, $key);
############################################
# If there was no multiplayer discount
# for the removed player, we have to remove
# a discount for another player of the same
# user (parent)
############################################
if($player_discount_key == 0){
remove_first_multi_player_discount($user_key, $key);
}
#print "DESC: $description, DIS: $discount \n";
#insert_adjustment($user_key, $player_key, $discount, $key, -5); #-5 just means that its a multiplayer discount
}
#print "PLAYER COUNT: $player_count \n";
# disconnect from database
$sth2->finish();
$dbh2->disconnect;
}
############################################
# Remove a multiplayer discount to compensate
# for the removed player which should not have
# been discounted
############################################
sub remove_first_multi_player_discount{
my $user_key = shift;
my $discount_key = shift;
my %player_info = get_player_info($user_key);
my $removed_discount = 0;
foreach my $user_number (sort {$a <=> $b} (keys %player_info)){
if($removed_discount == 0){
my $player_key = $player_info{$user_number}{"KEY"};
my ($amount, $player_discount_key) = get_player_discount($player_key, $discount_key);
if($player_discount_key > 0){
my $option_change_amount = 0 - $amount;
insert_adjustment($user_key, $player_key, $option_change_amount, "MP Discount Removal 2", -2); #-2 just means that its an Option Change
update_adjustment_account_to_show_change($player_discount_key);
$removed_discount = 1;
}
}
}
}
############################################
# Get the discount for a speific player
# and discount code
############################################
sub get_player_discount{
my $player_key = shift;
my $discount_key = shift;
my $dbh = get_dbh();
my $query = "SELECT `Key`, `Amount` FROM Reg_All_Transaction WHERE `Check_ID` = '$discount_key' AND `Update_Account_ID` = '-5' AND `Player_ID` = '$player_key'";
#print "QUERY: $query \n";
my $sth = $dbh->prepare($query);
$sth->execute();
my $amount = 0;
my $player_discount_key = 0;
while (my $hash_ref = $sth->fetchrow_hashref) {
$amount = $hash_ref->{Amount};
$player_discount_key = $hash_ref->{Key};
##### Not quite sure why this is here.
#my $option_change_amount = 0 - $amount;
#insert_adjustment($user_key, $player_id, $option_change_amount, "ADJUST MP Discount", -2); #-2 just means that its an Option Change
#update_adjustment_account_to_show_change($key);
#insert_adjustment($user_key, $player_key, $discount, $key, -5); #-5 just means that its a multiplayer discount
}
# disconnect from database
$dbh->disconnect;
$sth->finish();
return $amount, $player_discount_key;
}
############################################
# Look for any discounts that do not
# meet player requirement and adjust
# them in db
############################################
sub check_for_discounts_that_need_to_be_removed{
my $user_key = shift;
my $discount_key = shift;
my $dbh = get_dbh();
########################################
# Select items where Discount = the key for that user
# and the update account is -5 for discount
########################################
my $query = "SELECT `Key`, `Player_ID`, `Amount` FROM Reg_All_Transaction WHERE `Check_ID` = '$discount_key' AND `Account_ID` = '$user_key' AND `Update_Account_ID` = '-5'";
#print "QUERY: $query \n";
my $sth = $dbh->prepare($query);
$sth->execute();
while (my $hash_ref = $sth->fetchrow_hashref) {
my $player_id = $hash_ref->{Player_ID};
my $amount = $hash_ref->{Amount};
my $key = $hash_ref->{Key};
my $option_change_amount = 0 - $amount;
insert_adjustment($user_key, $player_id, $option_change_amount, "MP Discount Removal 1", -2); #-2 just means that its an Option Change
update_adjustment_account_to_show_change($key);
#insert_adjustment($user_key, $player_key, $discount, $key, -5); #-5 just means that its a multiplayer discount
}
#print "PLAYER COUNT: $player_count \n";
# disconnect from database
$dbh->disconnect;
$sth->finish();
}
############################################
# Check for discount for user (parent)
############################################
sub check_for_multiplayer_discounts{
my $user_key = shift;
my $player_key = shift;
my $player_count = get_player_count($user_key);
my $dbh = get_dbh();
my $query = "SELECT `Key`, `Description`, `Discount` FROM `Reg_Multi_Player_Discount` WHERE `Org_ID` = '$org_id' AND `Reg_Year` = '$reg_year' AND `Number_Of_Reg_Players` <= '$player_count' AND `Max_Reg_Players` >= '$player_count'";
#print "QUERY: $query \n";
my $sth = $dbh->prepare($query);
$sth->execute();
while (my $hash_ref = $sth->fetchrow_hashref) {
my $description = $hash_ref->{Description};
my $discount = $hash_ref->{Discount};
my $key = $hash_ref->{Key};
#print "DESC: $description, DIS: $discount \n";
insert_adjustment($user_key, $player_key, $discount, $key, -5); #-5 just means that its a multiplayer discount
}
#print "PLAYER COUNT: $player_count \n";
# disconnect from database
$dbh->disconnect;
$sth->finish();
}
############################################
# Get number of players per user(parent)
############################################
sub get_player_count{
my $user_key = shift;
my $dbh = get_dbh();
my $query = "SELECT COUNT(*) FROM `Reg_Player_Link` WHERE `Account_ID` = '$user_key' AND `Active` = '1' AND `Reg_Year` = '$reg_year'";
my $sth = $dbh->prepare($query);
print "QUERY: $query \n";
$sth->execute();
my $player_count = $sth->fetchrow_arrayref->[0];
# disconnect from database
$dbh->disconnect;
$sth->finish();
return $player_count;
}
############################################
# Get player info
############################################
sub get_player_info_old_reg{
my $user_key = shift;
my $old_reg_year = shift;
my %return_hash = ();
my $dbh = get_dbh();
################################################################
# Query DB to verify Session
################################################################
# Do not requre active
my $query = "SELECT `Key`,`Org_ID`,`Reg_Year`,`Firstname`,`Lastname`,`Account_ID`,`Paid`,`Active`,`Reg_Date`,`Player_Confirmed`,`Assigned_League`,`Assigned_Team` FROM `Reg_Player_Link` WHERE `Account_ID` = '$user_key' AND `Active` = '1' AND `Reg_Year` = '$old_reg_year' ORDER BY `Key`";
#my $query = "SELECT `Key`,`Org_ID`,`Reg_Year`,`Firstname`,`Lastname`,`Account_ID`,`Paid`,`Active`,`Reg_Date`,`Player_Confirmed`,`Assigned_League`,`Assigned_Team` FROM `Reg_Player_Link` WHERE `Account_ID` = '$user_key' AND `Reg_Year` = '$old_reg_year' ORDER BY `Key`";
my $sth = $dbh->prepare($query);
#print "$query \n";
$sth->execute();
$sth->bind_columns(\$Key, \$Org_ID, \$Reg_Year,
\$Firstname, \$Lastname,
\$Account_ID, \$Paid,
\$Active, \$Reg_Date,
\$Player_Confirmed,
\$Assigned_League, \$Assigned_Team);
$user_number = 1;
while($sth->fetch()) {
$return_hash{$user_number}{"KEY"} = $Key;
$return_hash{$user_number}{"ORG_ID"} = $Org_ID;
$return_hash{$user_number}{"REG_YEAR"} = $Reg_Year;
$return_hash{$user_number}{"FIRSTNAME"} = $Firstname;
$return_hash{$user_number}{"LASTNAME"} = $Lastname;
$return_hash{$user_number}{"ACCOUNT_ID"} = $Account_ID;
$return_hash{$user_number}{"PAID"} = $Paid;
$return_hash{$user_number}{"ACTIVE"} = $Active;
$return_hash{$user_number}{"REG_DATE"} = $Reg_Date;
$return_hash{$user_number}{"PLAYER_CONFIRMED"} = $Player_Confirmed;
$return_hash{$user_number}{"ASSIGNED_LEAGUE"} = $Assigned_League;
$return_hash{$user_number}{"ASSIGNED_TEAM"} = $Assigned_Team;
$user_number++;
}
# disconnect from database
$dbh->disconnect;
$sth->finish();
return %return_hash;
}
############################################
# Get player info
############################################
sub get_player_info{
my $user_key = shift;
my %return_hash = ();
my $dbh = get_dbh();
################################################################
# Query DB to verify Session
################################################################
my $query = "SELECT `Key`,`Org_ID`,`Reg_Year`,`Firstname`,`Lastname`,`Account_ID`,`Paid`,`Active`,`Reg_Date`,`Player_Confirmed`,`Assigned_League`,`Assigned_Team` FROM `Reg_Player_Link` WHERE `Account_ID` = '$user_key' AND `Active` = '1' AND `Reg_Year` = '$reg_year' ORDER BY `Key`";
my $sth = $dbh->prepare($query);
#print "$query \n";
$sth->execute();
$sth->bind_columns(\$Key, \$Org_ID, \$Reg_Year,
\$Firstname, \$Lastname,
\$Account_ID, \$Paid,
\$Active, \$Reg_Date,
\$Player_Confirmed,
\$Assigned_League, \$Assigned_Team);
$user_number = 1;
while($sth->fetch()) {
$return_hash{$user_number}{"KEY"} = $Key;
$return_hash{$user_number}{"ORG_ID"} = $Org_ID;
$return_hash{$user_number}{"REG_YEAR"} = $Reg_Year;
$return_hash{$user_number}{"FIRSTNAME"} = $Firstname;
$return_hash{$user_number}{"LASTNAME"} = $Lastname;
$return_hash{$user_number}{"ACCOUNT_ID"} = $Account_ID;
$return_hash{$user_number}{"PAID"} = $Paid;
$return_hash{$user_number}{"ACTIVE"} = $Active;
$return_hash{$user_number}{"REG_DATE"} = $Reg_Date;
$return_hash{$user_number}{"PLAYER_CONFIRMED"} = $Player_Confirmed;
$return_hash{$user_number}{"ASSIGNED_LEAGUE"} = $Assigned_League;
$return_hash{$user_number}{"ASSIGNED_TEAM"} = $Assigned_Team;
$user_number++;
}
# disconnect from database
$dbh->disconnect;
$sth->finish();
return %return_hash;
}
############################################
# Return html for checkout header
############################################
sub get_checkout_header{
my $header = shift;
my $checkout_header = "
$header
\n";
$checkout_header .= "
Name
Fee
Total
Paid
Due
\n";
return $checkout_header;
}
############################################
# Return html for paypal that wont change
############################################
sub get_paypal_const{
my $pay_pal = "
\n";
############################################
# If its not pay by check, create form for submitting
############################################
if($by_check != 1){
$user_table .= "\n";
}
############################################
# If its not pay by check, give them thier payment options. Notice that
# this is stored in the pay_pal portion. I think this is becuase it gets
# printed to the screen after the others.
############################################
$pay_pal .= "\n";
$pay_pal .= "\n";
if($min_partial_pay == 0){
$pay_pal .= "\n";
}else{
$pay_pal .= "\n";
}
$pay_pal .= "
\n";
$pay_pal .= "\n";
############################################
# If it is pay by check. Display where to send it. Stored in var
############################################
if($by_check == 1){
if(($org_id == 167)||($org_id == 13)){ # Plymouth required forms
#$user_table .= "
$pay_by_check_message \n";
$user_table .= "";
}
return $user_table, $pay_pal;
}
############################################
# This creates the paypal and html for the partial_checkout table.
############################################
sub get_partial_checkout_table{
my $user_key = shift;
my $by_check = shift;
my $partial = shift;
my ($total_num_players, $max_total_cost, $player_min) = get_num_players_and_total_cost($user_key);
my %user_info = get_account_info($user_key);
my %player_info = get_player_info($user_key);
my $pay_pal_count = 1;
my $num_players = 0;
my $total_cost = 0;
my $balance = 0;
$transaction_id = get_transaction_id();
my $user_table = get_checkout_header("Registered Players");
my $pay_pal = get_paypal_const();
############################################
# Loop through players to create the display cell, paypal info and calculate totals
############################################
foreach my $user_number (sort {$a <=> $b} (keys %player_info)){
$num_players++;
$player_key = $player_info{$user_number}{"KEY"};
############################################
# Get the cell with a list of what is due for player, plus the paypal string
# that gets sent to have an itemized list on pay pal.
############################################
my ($player_row, %checkout_hash) = get_checkout_row_for_player_and_total_due_and_pay_pal($player_key, $player_info{$user_number}{"FIRSTNAME"}, $player_info{$user_number}{"LASTNAME"}, "cal_events3");
my $amount_due = $checkout_hash{"TOTAL_DUE"};
my $paypal_string = $checkout_hash{"PAYPAL_STRING"};
my $total_paid = $checkout_hash{"TOTAL_PAID"};
$user_table .= $player_row;
############################################
# Calculate what the player owes and fill that cell Also, do some conversions to
# two decimal places
############################################
$max_pay = $amount_due;
my $amount_to_deduct = get_amount_to_deduct($partial, ($total_num_players - $num_players + 1), $total_cost, $max_pay, $player_min);
#$amount_to_deduct = .02;
############################################
# Store temporary paypal record. This will not be valid until the paypal tx is finished.
############################################
insert_tmp_paypal($user_key, $player_key, $amount_to_deduct, $transaction_id);
$balance = $balance + $amount_due;
############################################
# Add player specific paypal info
############################################
$pay_pal .= "\n";
$pay_pal .= "\n";
$pay_pal_count++;
############################################
# Keep running total of amount due for user
############################################
$total_cost = $amount_to_deduct + $total_cost;
}
############################################
# Finish user table
# if by_check equals 2, then we are confirming the payment
############################################
$total_remaining = $balance - $total_cost;
$total_remaining = sprintf("%.2f", $total_remaining);
$balance = sprintf("%.2f", $balance);
$total_cost = sprintf("%.2f", $total_cost);
if($by_check == 2){
$user_table .= "
$pay_by_check_message\n";
}
return $user_table, $pay_pal, $balance;
}
############################################
# Get the Registration values by player
############################################
sub get_reg_values_by_player{
my $org_id = shift;
my $reg_year = shift;
my $player_id = shift;
my %return_hash = ();
my $dbh = get_dbh();
################################################################
# Query DB to verify Session
################################################################
my $query = "SELECT `Key`,`Description_Text`,`Input_Type`,`Selection_Link`,`Validation_Link`,`Required`,`Order` FROM `Reg_Definition` WHERE `Org_ID` = '$org_id' AND `Reg_Year` = '$reg_year';";
my $sth = $dbh->prepare($query);
#print "$query \n";
$sth->execute();
$sth->bind_columns(\$Key,\$Description_Text,\$Input_Type,\$Selection_Link,\$Validation_Link,\$Required,\$Order);
while($sth->fetch()) {
$return_hash{$Key} = get_cell_value($Input_Type,$Key,$player_id);
}
# disconnect from database
$sth->finish();
$dbh->disconnect;
return %return_hash;
}
############################################
# Get value for a cell
############################################
sub get_cell_value{
my $Input_Type = shift;
my $Key = shift;
my $player_id = shift;
my $return_html = "";
if($Input_Type == 1){ # HEADER
$return_html = "
$Description_Text
";
}elsif($Input_Type == 2){ #INPUT
$input_value = get_input_text($Key, $player_id);
}elsif($Input_Type == 3){ #Select Dropdown
$input_value = get_input_options($Key, $player_id);
}elsif($Input_Type == 4){ #Date
$input_value = get_input_date($Key, $player_id);
$input_value = get_view_date_format($input_value);
}elsif($Input_Type == 5){ #Radio
$input_value = get_input_options($Key, $player_id);
}elsif($Input_Type == 6){ #Textarea
$input_value = get_input_longtext($Key, $player_id);
}elsif($Input_Type == 7){ #CheckBox
$input_value = get_input_checked($Key, $player_id);
$checked = "";
if($input_value == 1){
$checked = "CHECKED";
}
$input_value = $checked;
}elsif($Input_Type == 10){ #Select Dropdown Long value with payment attached
$input_value = get_input_options($Key, $player_id);
}elsif($Input_Type == 11){ #Radio with payment attached
$input_value = get_input_options($Key, $player_id);
}
return $input_value;
}
sub get_input_date{
my $reg_id = shift;
my $player_id = shift;
my $input_text = "";
my $dbh = get_dbh();
my $query = "SELECT `Input_Date` FROM Reg_Input_Date WHERE `Reg_ID` = '$reg_id' AND `Player_ID` = '$player_id';";
#print "QUERY: $query \n";
my $sth = $dbh->prepare($query);
$sth->execute();
$sth->bind_columns(\$Input_Date);
while($sth->fetch()) {
$input_date = $Input_Date;
}
$sth->finish();
$dbh->disconnect;
return $input_date;
}
sub get_input_longtext{
my $reg_id = shift;
my $player_id = shift;
my $input_text = "";
my $dbh = get_dbh();
my $query = "SELECT `Input_Longtext` FROM Reg_Input_Longtext WHERE `Reg_ID` = '$reg_id' AND `Player_ID` = '$player_id';";
#print "QUERY: $query \n";
my $sth = $dbh->prepare($query);
$sth->execute();
$sth->bind_columns(\$Input_Text);
while($sth->fetch()) {
$input_text = $Input_Text;
}
$sth->finish();
$dbh->disconnect;
return $input_text;
}
sub get_input_text{
my $reg_id = shift;
my $player_id = shift;
my $input_text = "";
my $dbh = get_dbh();
my $query = "SELECT `Input_Text` FROM Reg_Input_Text WHERE `Reg_ID` = '$reg_id' AND `Player_ID` = '$player_id';";
#print "QUERY: $query \n";
my $sth = $dbh->prepare($query);
$sth->execute();
$sth->bind_columns(\$Input_Text);
while($sth->fetch()) {
$input_text = $Input_Text;
}
$sth->finish();
$dbh->disconnect;
return $input_text;
}
sub get_input_checked{
my $reg_id = shift;
my $player_id = shift;
my $input_value = "";
my $dbh = get_dbh();
my $query = "SELECT `Input_Option` FROM Reg_Input_Options WHERE `Reg_ID` = '$reg_id' AND `Player_ID` = '$player_id';";
#print "QUERY: $query \n";
my $sth = $dbh->prepare($query);
$sth->execute();
$sth->bind_columns(\$Input_Value);
while($sth->fetch()) {
$input_value = $Input_Value;
}
$sth->finish();
$dbh->disconnect;
return $input_value;
}
sub get_input_options{
my $reg_id = shift;
my $player_id = shift;
my $input_value = "";
my $dbh = get_dbh();
my $query = "SELECT a.`Option` FROM Reg_Options a, Reg_Input_Options b WHERE b.`Reg_ID` = '$reg_id' AND b.`Player_ID` = '$player_id' AND b.`Input_Option` = a.`Key`;";
#print "QUERY: $query \n";
my $sth = $dbh->prepare($query);
$sth->execute();
$sth->bind_columns(\$Input_Value);
while($sth->fetch()) {
$input_value = $Input_Value;
}
$sth->finish();
$dbh->disconnect;
return $input_value;
}
###################################
# Get parameter for DB
###################################
sub get_param{
my $paramter = shift;
my $value = param($paramter);
$value =~ s/\'/''/g;
$value =~ s/\"//g;
$value =~ s/\>//g;
$value =~ s/\/g;
$value =~ s/\\//g;
return $value;
}
###################################
# Update Player Name
###################################
sub update_player_name{
my $player_key = shift;
my $first_name = shift;
my $last_name = shift;
my $query = "UPDATE Reg_Player_Link SET `Firstname` = '$first_name', `Lastname` = '$last_name' WHERE `Key` = '$player_key';";
#print "$query \n";
my $dbh = get_dbh();
my $sth = $dbh->prepare($query);
$sth->execute();
$sth->finish();
$dbh->disconnect;
return $player_key;
}
###################################
# Set Player Active to 0
###################################
sub remove_player{
my $player_key = shift;
my $query = "UPDATE Reg_Player_Link SET `Active` = '0' WHERE `Key` = '$player_key';";
#print "$query \n";
my $dbh = get_dbh();
my $sth = $dbh->prepare($query);
$sth->execute();
$sth->finish();
$dbh->disconnect;
return $player_key;
}
############################################
# Get how much the player is due for a specific field
############################################
sub get_checkout_override{
my $option_id = shift;
my $player_id = shift;
my $checkout_override = "";
my $dbh = get_dbh();
#my $query = "SELECT `Checkout_Override` FROM `Reg_Payment_Definition` WHERE `Option_ID` = '$option_id' ;";
my $query = "SELECT a.`Checkout_Override` FROM Reg_Payment_Definition a, Reg_Input_Options b WHERE b.`Reg_ID` = '$option_id' AND b.`Player_ID` = '$player_id' AND b.`Input_Option` = a.`Option_ID`;";
#print "QUERY: $query \n";
my $sth = $dbh->prepare($query);
$sth->execute();
$sth->bind_columns(\$Checkout_Override);
while($sth->fetch()) {
$checkout_override = $Checkout_Override;
}
$sth->finish();
$dbh->disconnect;
return $checkout_override;
}
############################################
# Get how much the player is due for a specific field
############################################
sub get_option_payment_amount{
my $option_id = shift;
my $amount = 0;
my $dbh = get_dbh();
my $query = "SELECT `Amount` FROM `Reg_Payment_Definition` WHERE `Option_ID` = '$option_id' ;";
#print "QUERY: $query \n";
my $sth = $dbh->prepare($query);
$sth->execute();
$sth->bind_columns(\$Amount);
while($sth->fetch()) {
$amount = $Amount;
}
$sth->finish();
$dbh->disconnect;
return $amount;
}
################################################################
### Get params and update DB
################################################################
sub get_player_params_and_update_database{
my $org_id = shift;
my $reg_year = shift;
my $player_id = shift;
my $user_id = shift;
my $dbh = get_dbh();
################################################################
# Query DB to verify Session
################################################################
my $query = "SELECT `Key`,`Description_Text`,`Input_Type`,`Selection_Link`,`Validation_Link`,`Required`,`Order` FROM `Reg_Definition` WHERE `Org_ID` = '$org_id' AND `Reg_Year` = '$reg_year';";
my $sth = $dbh->prepare($query);
#print "$query \n";
$sth->execute();
$sth->bind_columns(\$Key,\$Description_Text,\$Input_Type,\$Selection_Link,\$Validation_Link,\$Required,\$Order);
while($sth->fetch()) {
my $reg_id = "REG" . $Key;
my $required = "";
if($Input_Type == 1){ # HEADER
}elsif($Input_Type == 2){ #INPUT
$return_hash{$reg_id} = get_param($reg_id);
insert_text($Key, $player_id, $return_hash{$reg_id});
}elsif($Input_Type == 3){ #Select Dropdown
$return_hash{$reg_id} = get_param($reg_id);
insert_option($Key, $player_id, $return_hash{$reg_id});
}elsif($Input_Type == 4){ #Date
my $dob1 = get_param('dob1');
my $dob2 = get_param('dob2');
my $dob3 = get_param('dob3');
my $dob = $dob1 . "/" . $dob2 . "/" . $dob3;
$dob = get_db_date_format($dob);
$return_hash{$reg_id} = $dob;
insert_date($Key, $player_id, $return_hash{$reg_id});
}elsif($Input_Type == 5){ #Radio
$return_hash{$reg_id} = get_param($reg_id);
insert_option($Key, $player_id, $return_hash{$reg_id});
}elsif($Input_Type == 6){ #Textarea
$return_hash{$reg_id} = get_param($reg_id);
insert_longtext($Key, $player_id, $return_hash{$reg_id});
}elsif($Input_Type == 7){ #CheckBox
my $check_box = get_param($reg_id);
if($check_box eq "on"){
$check_box = 1;
}else{
$check_box = 0;
}
$return_hash{$reg_id} = $check_box;
insert_option($Key, $player_id, $return_hash{$reg_id});
}elsif($Input_Type == 10){ #Select Dropdown with payment attached
$return_hash{$reg_id} = get_param($reg_id);
insert_option($Key, $player_id, $return_hash{$reg_id});
update_adjustment_table_from_input($return_hash{$reg_id}, $user_id, $player_id, $Key);
}elsif($Input_Type == 11){ #Radio with payment attached
$return_hash{$reg_id} = get_param($reg_id);
insert_option($Key, $player_id, $return_hash{$reg_id});
my $player_amount_due = get_option_payment_amount($return_hash{$reg_id});
update_adjustment_table_from_input($return_hash{$reg_id}, $user_id, $player_id, $Key);
}
}
# disconnect from database
$sth->finish();
$dbh->disconnect;
}
sub update_adjustment_table_from_input{
my $option_id = shift;
my $user_id = shift;
my $player_id = shift;
my $reg_def_id = shift; # The reg definition id gets inserted as the check id
my $player_amount_due = get_option_payment_amount($option_id);
my $update = check_if_its_an_update_for_that_user($user_id, $reg_def_id, $player_id, $player_amount_due);
if(($player_amount_due >= 0)&&($update == 1)){ # -1 means no update
insert_adjustment($user_id, $player_id, $player_amount_due, $reg_def_id, -1); #-1 just means that its an auto update and it should get the text for trx from reg definition
}
}
sub check_if_its_an_update_for_that_user{
my $user_id = shift;
my $reg_def_id = shift;
my $player_id = shift;
my $new_amount_due = shift;
my $update = 1;
my $dbh = get_dbh();
my $query = "SELECT `Key` , `Account_ID`, `Amount`, `Check_ID`, `Date` FROM Reg_All_Transaction WHERE `Check_ID` = '$reg_def_id' AND `Player_ID` = '$player_id' AND `Update_Account_ID` = '-1'";
my $sth = $dbh->prepare($query);
$sth->execute();
$sth->bind_columns(\$Key,\$Account_ID,\$Amount,\$Check_ID,\$Date);
while($sth->fetch()) {
#$amount = 0 - $Amount;
$Amount = sprintf("%.2f",$Amount);
$new_amount_due = sprintf("%.2f",$new_amount_due);
#print "NEW: $new_amount_due - OLD: $Amount \n";
#print "NEW: $reg_def_id - OLD: $Check_ID \n";
if($new_amount_due != $Amount){
my $option_change_amount = 0 - $Amount;
insert_adjustment($user_id, $player_id, $option_change_amount, "Option Change", -2); #-2 just means that its an Option Change
update_adjustment_account_to_show_change($Key);
}else{
$update = 0;
}
}
$sth->finish();
$dbh->disconnect;
return $update;
}
sub update_adjustment_account_to_show_change{
my $key = shift;
my $dbh = get_dbh();
my $query = "UPDATE Reg_All_Transaction SET `Update_Account_ID` = '-3' WHERE `Key` = '$key' LIMIT 1";
my $sth = $dbh->prepare($query);
$sth->execute();
$sth->finish();
$dbh->disconnect;
}
################################################################
### Update adjustemnt
################################################################
sub insert_adjustment{
my $user_id = shift;
my $player_key = shift;
my $amount = shift;
my $check_notes = shift;
my $update_account = shift;
#my $update_account = get_player_id_from_cookie();
my $account_id = get_account_id_by_player_key($player_key);
my $date = get_date_time();
my $query = "INSERT INTO Reg_All_Transaction (`Key` , `Account_ID` , `Player_ID` , `Update_Account_ID`, `Amount`, `Check_ID`, `Date`) VALUES ('', '$user_id', '$player_key', '$update_account', '$amount', '$check_notes', '$date')";
#print "$query \n";
my $dbh = get_dbh();
my $sth = $dbh->prepare($query);
$sth->execute();
$sth->finish();
$dbh->disconnect;
return $player_key;
}
################################################################
# Get the reg form - READ ONLY
################################################################
sub get_read_only_reg_form{
my $org_id = shift;
my $reg_year = shift;
my $form_name = shift;
my $player_id = shift;
my %return_hash = ();
my $dbh = get_dbh();
################################################################
# Query DB to verify Session
################################################################
my $query = "SELECT `Key`,`Description_Text`,`Input_Type`,`Selection_Link`,`Validation_Link`,`Required`,`Order` FROM `Reg_Definition` WHERE `Org_ID` = '$org_id' AND `Reg_Year` = '$reg_year';";
my $sth = $dbh->prepare($query);
#print "$query \n";
$sth->execute();
$sth->bind_columns(\$Key,\$Description_Text,\$Input_Type,\$Selection_Link,\$Validation_Link,\$Required,\$Order);
while($sth->fetch()) {
my $reg_id = "REG" . $Key;
my $required = ":";
#if($Required == 1){
# $required = "*";
# ($return_hash{$Order}{"JSCRIPT_VARS"}, $return_hash{$Order}{"JSCRIPT_COMPARE"}) = get_required_js($Input_Type,$form_name,$reg_id);;
#}
if($Input_Type == 1){ # HEADER
$return_hash{$Order}{"HTML"} = "
";
$return_html = $radio_button;
}
return $return_html;
}
################################################################
### Create new player in db
################################################################
sub create_new_player{
my $org_id = shift;
my $reg_year = shift;
my $user_key = param("userkey");
my $first_name = get_param('fname');
my $last_name = get_param('lname');
my %return_hash = ();
my $player_id = link_new_player($user_key, $first_name, $last_name);
get_player_params_and_update_database($org_id, $reg_year, $player_id, $user_key);
return $player_id;
}
################################################################
### Covert the date to use in database
################################################################
sub get_db_date_format{
my $date = shift;
my($month, $day, $year) = split(/\//,$date);
$date = "$year-$month-$day";
return $date;
}
################################################################
### Create the date to viewable format
################################################################
sub get_view_date_format{
my $date = shift;
my($year, $month, $day) = split(/-/,$date);
$date = "$month/$day/$year";
return $date;
}
################################################################
### Create the date to viewable format
################################################################
sub get_month_day_year{
my $date = shift;
my($year, $month, $day) = split(/-/,$date);
return $month,$day,$year;
}
#########################################
# Insert item to the Reg_Input_Longtext table
#########################################
sub insert_longtext{
my $reg_id = shift;
my $player_id = shift;
my $insert_lt = shift;
my $query = "REPLACE INTO Reg_Input_Longtext (`Reg_ID`,`Player_ID`, `Input_Longtext`) VALUES ('$reg_id', '$player_id', '$insert_lt');";
#print "QUERY: $query \n";
my $dbh = get_dbh();
my $sth = $dbh->prepare($query);
$sth->execute();
$sth->finish();
$dbh->disconnect;
return $user_id;
}
#########################################
# Insert item to the Reg_Input_Date table
#########################################
sub insert_date{
my $reg_id = shift;
my $player_id = shift;
my $insert_date = shift;
my $query = "REPLACE INTO Reg_Input_Date (`Reg_ID`,`Player_ID`, `Input_Date`) VALUES ('$reg_id', '$player_id', '$insert_date');";
#print "QUERY: $query \n";
my $dbh = get_dbh();
my $sth = $dbh->prepare($query);
$sth->execute();
$sth->finish();
$dbh->disconnect;
return $user_id;
}
#########################################
# Insert item to insert_option table
#########################################
sub insert_option{
my $reg_id = shift;
my $player_id = shift;
my $insert_option = shift;
my $query = "REPLACE INTO Reg_Input_Options (`Reg_ID`,`Player_ID`, `Input_Option`) VALUES ('$reg_id', '$player_id', '$insert_option');";
#print "QUERY: $query \n";
my $dbh = get_dbh();
my $sth = $dbh->prepare($query);
$sth->execute();
$sth->finish();
$dbh->disconnect;
return $user_id;
}
#########################################
# Insert item to the Reg_Input_Text table
#########################################
sub insert_text{
my $reg_id = shift;
my $player_id = shift;
my $insert_text = shift;
my $query = "REPLACE INTO Reg_Input_Text (`Reg_ID`,`Player_ID`, `Input_Text`) VALUES ('$reg_id', '$player_id', '$insert_text');";
#print "QUERY: $query \n";
my $dbh = get_dbh();
my $sth = $dbh->prepare($query);
$sth->execute();
$sth->finish();
$dbh->disconnect;
return $user_id;
}
#########################################
# Link a player to account in the Reg_player_link table
#########################################
sub link_new_player{
my $user_key = shift;
my $first_name = shift;
my $last_name = shift;
my $reg_date = get_date_time();
my $query = "INSERT INTO Reg_Player_Link (`Key`, `Org_ID`, `Reg_Year`, `Account_ID`, `Firstname`, `Lastname`, `Active`, `Reg_Date`, `Player_Confirmed`,`Assigned_League`,`Assigned_Team`) VALUES ('', '$org_id', '$reg_year', '$user_key','$first_name','$last_name', '1', '$reg_date', '0','0','0');";
#print "QUERY: $query \n";
my $dbh = get_dbh();
my $sth = $dbh->prepare($query);
$sth->execute();
#########################################
# Get the ID of the just inserted event
#########################################
my $query2 = "SELECT LAST_INSERT_ID( );";
$sth = $dbh->prepare($query2);
$sth->execute();
# assign fields to variables
$sth->bind_columns(\$last_insert_id);
while($sth->fetch()) {
$user_id = $last_insert_id;
}
#print "$query \n";
$sth->finish();
$dbh->disconnect;
return $user_id;
}
#########################################
# Gets Temp password for email
#########################################
sub get_tmp_pw{
#my $tmp_pw = "123test";
my $tmp_pw = generate_random_string_pw(6);
return $tmp_pw;
}
#########################################
# Generates random string for temp password
#########################################
sub generate_random_string_pw{
$length = shift;
$rand_string = "";
$possible = '23456789';
while (length($rand_string) < $length) {
$rand_string .= substr($possible, (int(rand(length($possible)))), 1);
}
return $rand_string;
}
#########################################
# Generates random string for temp token
#########################################
sub get_tmp_string{
#my $tmp_string = "123test";
my $tmp_string = generate_random_string(20);
my $random_string_exists = get_email_by_x($tmp_string);
my $count = 1;
while($random_string_exists ne ""){
$random_string_exists = get_email_by_x($tmp_string);
$count++;
if($count > 10){
$random_string_exists = "";
}
}
return $tmp_string;
}
#########################################
# Check if tmp password is correct
#########################################
sub check_tmp_password{
my $email = shift;
my $pw = shift;
my $tmp_string = shift;
my $tmp_pw = 0;
my $valid_tmp_pw = 0;
my $dbh = get_dbh();
my $query = "SELECT `Key`, `Tmp_Password` FROM `Account_TMP_Password` WHERE `Email` = '$email' AND `Tmp_String` = '$tmp_string';";
my $sth = $dbh->prepare($query);
#print "$query \n";
$sth->execute();
$sth->bind_columns(\$Key, \$Tmp_Password);
while($sth->fetch()) {
$tmp_pw = $Tmp_Password;
$key = $Key;
}
if($tmp_pw eq $pw){
$valid_tmp_pw = 1;
}else{
if($tmp_pw == 0){
$valid_tmp_pw = 2;
}
}
$sth->finish();
$dbh->disconnect;
return $valid_tmp_pw;
}
#########################################
# Check if tmp password exist
#########################################
sub check_tmp_password_exist{
my $email = shift;
my $valid_tmp_string = "";
my $dbh = get_dbh();
my $query = "SELECT `Tmp_String`, `Tmp_Password` FROM `Account_TMP_Password` WHERE `Email` = '$email';";
my $sth = $dbh->prepare($query);
#print "$query \n";
$sth->execute();
$sth->bind_columns(\$Tmp_String, \$Tmp_Password);
while($sth->fetch()) {
$valid_tmp_string = $Tmp_String;
$tmp_password = $Tmp_Password;
}
$sth->finish();
$dbh->disconnect;
return $valid_tmp_string, $tmp_password;
}
#########################################
# Set the account tmp password
#########################################
sub set_account_tmp_password{
my $email = shift;
my $tmp_string = "";
my $pw = "";
my $date_time = "";
$date_time = get_date_time();
($tmp_string, $pw) = check_tmp_password_exist($email);
#print "TS: $tmp_string - PW: $pw \n";
if($tmp_string eq ""){
$pw = get_tmp_pw();
$tmp_string = get_tmp_string();
my $dbh = get_dbh();
my $query = "INSERT INTO `Account_TMP_Password` ( `Key` , `Email` , `Tmp_Password` , `Tmp_String` , `Date`) VALUES ('', '$email', '$pw', '$tmp_string', '$date_time');";
#print "QUERY: $query \n";
my $sth = $dbh->prepare($query);
$sth->execute();
$sth->finish();
$dbh->disconnect;
}
return $pw, $tmp_string, $date_time;
}
#########################################
# Send temp password to users
#########################################
sub send_tmppw{
my $email = shift;;
my $sendmail = "/bin/sendmail.localhost -t";
#my $reply_to = "Reply-to: jeremy\@hometeamz.com";
my $subject = "Account Password for $org_full_name_for_email Registration";
my $to = "$email";
#my $to = "jeremymclean\@gmail.com";
#my $from = "jeremy\@hometeamz.com";
my ($pw, $tmp_string, $date_time) = set_account_tmp_password($email);
#print "PASS: $pw \n"
my $found_email = 0;
if($pw ne ""){
$found_email = 1;
my %user_info = get_account_info_by_email($email);
$content = "Welcome,
\n\nThank you for registering with $org_full_name_for_email
\n\n";
$content .= "Please use the temporary password provided below to validate your new account.
\n";
$content .= "Your temporary password is: $pw \nYour login is: $email
\n\n";
$content .= "To proceed directly to the account verification window, please follow this link\n";
$content .= "\n";
#print "HEY \n";
open(SENDMAIL, "|$sendmail -f $from") or die "Cannot open $sendmail: $!";
print SENDMAIL "To: $to\n";
#print SENDMAIL "bcc: $bcc\n";
print SENDMAIL "Subject: $subject\n";
print SENDMAIL "Content-type: text/html\n\n";
print SENDMAIL $content;
close(SENDMAIL);
}
#print "$to $pw $from $content \n";
return $found_email, $tmp_string;
}
#########################################
# Set the account password to user
#########################################
sub send_pw{
my $email = param('email');
my $sendmail = "/bin/sendmail.localhost -t";
my $subject = "Password Reminder for $org_full_name_for_email";
my $to = "$email";
#my $to = "jeremymclean\@gmail.com";
#my $from = "jeremy\@hometeamz.com";
my $pw = get_pw_by_email($email);
my $found_email = 0;
if($pw eq ""){
#print "PW2: $pw \n";
}else{
#print "PW: $pw \n";
$found_email = 1;
my %user_info = get_account_info_by_email($email);
$content = "Hello $user_info{\"FIRSTNAME\"} $user_info{\"LASTNAME\"},\n\n";
$content .= "You have requested a password reminder from $org_full_name_for_email.\n\n";
$content .= "Your information is:\nLogin: $user_info{\"EMAIL\"}\nPassword: $pw \n";
open(SENDMAIL, "|$sendmail -f $from") or die "Cannot open $sendmail: $!";
print SENDMAIL "To: $to\n";
print SENDMAIL "bcc: $bcc\n";
print SENDMAIL "Subject: $subject\n";
print SENDMAIL "Content-type: text/plain\n\n";
print SENDMAIL $content;
close(SENDMAIL);
#print $content;
}
return $found_email;
}
################################################################
### This section prints the stylesheet as well as some
### Javascript functions
################################################################
sub print_style2{
$league_only = shift;
$menu_text_color = "#ffffff";
$menu_bg_color = "#999999";
$menu_border_color = "#ffffff";
$menu_head_text_color = "#ffffff";
$menu_head_bg_color = "#686b6e";
$menu_padding = "2";
#$menu_color_hover = "#ffffff";
#$menu_color_hover = "yellow";
$menu_color_hover = "#ffffff";
$menu_bg_color_hover = "#686b6e";
$bare_domain = $hometeamz_url;
$bare_domain = "hometeamzonline.com";
$bare_domain =~ s/www\.//;
if($league_only == 1){
$bg_url = "";
}else{
$bg_url = "$teamdata/hometeamz/bg_right3.gif";
}
#$style_sheet = "$lib/ht.css";
print <<__HTML__;
__HTML__
add_to_hit_counter($org_id);
}
################################################################
### This prints the left hand column
################################################################
sub print_reg_info{
print "
\n";
print_text_info_box("Registration", "$welcome_text");
#print_text_info_box("Program", "Baseball & Softball");
print_text_info_box("Season", "$season_text");
#print_text_info_box("Registration Fees", "Baseball - \$145 Early Bird discount. Register by Dec 15 and get \$25 off registration.");
print_text_info_box("Information", "$information_text");
#print_text_info_box("About Homteamz", "Hometeamz.com provides online league services for community sports programs. More Info");
print <<__HTML__;
__HTML__
print "
\n";
}
################################################################
### These are the text boxes on the left side
################################################################
sub print_text_info_box{
my $title = shift;
my $text = shift;
my $font_size = "10pt";
my $font_color = "black";
my $text_align = "center";
my $header_color = "#686b6e";
#my $header_color = "#666699";
my $body_color = "#ffffff";
print <<__HTML__;
$title
$text
__HTML__
}
################################################################
### Print extra style for reg pages.
################################################################
sub print_reg_style{
print <<__HTML__;
__HTML__
}
1;