#!/usr/bin/perl

# report_creation - Created by James Pattie, PC & Web Xperience, Inc.
# 09/27/2000.  This script will install the system level reports into a fresh
# or updated accounting installation.  

#JT: It would be nice if these scripts checked the environment for the variables
#a wrapper script could then call this script for 1 upgrade or repetitively
#as in a system upgrade for many databases.

use strict;
use Pg;

use vars '$conn';
use vars '$result';

my $version = "";
my $db_name = "";
my $db_passwd = "";
my $db_host = "";
my $db_user = "";

if ($db_name <= 0)
{
  print "Database Name:";
  while (length $db_name <= 0)
  {
    $db_name = <STDIN>;
  }
  chomp $db_name;
  print "Database Host(Server):";
  while (length $db_host <= 0)
  {
    $db_host = <STDIN>;
  }
  chomp $db_host;
  print "Username:";
  while (length $db_user <= 0)
  {
    $db_user = <STDIN>;
  }
  chomp $db_user;
  print "Password:";
  while (length $db_passwd <= 0)
  {
    $db_passwd = <STDIN>;
  }
  chomp $db_passwd;

  print "    The data you entered was:\n$db_name\n";
  print "$db_host\n";
  print "$db_user\n";
  print "$db_passwd\n";
  print "\nType Ctrl+C to cancel or Enter to continue:\n";
  <STDIN>;
}

# now we connect to the global database.
$conn = Pg::connectdb("dbname = $db_name host = $db_host user = $db_user password = $db_passwd");
if ($conn->status == PGRES_CONNECTION_BAD)
{
  die $conn->errorMessage . "Error:  Couldn't connect to the global database <$db_name>!\n";
}

# version table
$result = $conn->exec("SELECT version FROM version_tb");
if ($result->resultStatus != PGRES_TUPLES_OK)
{
  die $conn->errorMessage . "Error:  Getting version failed!\n";
}
else
{
  $version = $result->getvalue(0, 0);
}

# Open the file system_reports.psql and work with it.
open(SYSREPORTS, "system_reports.psql") or die "Error: Opening Reports File Failed!\n$!\n";

# Start a transaction
$result = $conn->exec("BEGIN WORK");
if ($result->resultStatus != PGRES_COMMAND_OK)
{
  die $conn->errorMessage . "Error:  Starting Transaction Failed!\n";
}

# now delete all reports where id < 500 in the system.
$result = $conn->exec("DELETE FROM report_type_tb WHERE id < 500");
if ($result->resultStatus != PGRES_COMMAND_OK)
{
  die $conn->errorMessage . "Error:  Deleting old System Reports Failed!\n";
}

$result = $conn->exec("DELETE FROM report_data_tb WHERE id < 500");
if ($result->resultStatus != PGRES_COMMAND_OK)
{
  die $conn->errorMessage . "Error:  Deleting old System Reports Data Failed!\n";
}

# now read in each line from the file and call $conn->exec on them.
while (my $line = <SYSREPORTS>)
{
  $result = $conn->exec($line);
  if ($result->resultStatus != PGRES_COMMAND_OK)
  {
    die $conn->errorMessage . "Error:  Executing line = '$line' failed!\n";
  }
}

close (SYSREPORTS);

# now set all reports with id < 500 non-editable.
$result = $conn->exec("UPDATE report_type_tb SET editable = false WHERE id < 500");
if ($result->resultStatus != PGRES_COMMAND_OK)
{
  die $conn->errorMessage . "Error:  Setting new System Reports Non-Editable Failed!\n";
}

# Close the transaction
$result = $conn->exec("COMMIT WORK");
if ($result->resultStatus != PGRES_COMMAND_OK)
{
  die $conn->errorMessage . "Error:  Ending the Transaction Failed!\n";
}

print "\nReport Creation/Updation Succeeded.\n";
