#!/usr/bin/perl
#
# jobs_export.pl
#
# Copyright 1999 -- 2001, onShore Development Inc. <URL:http://www.onshore-devel.com/>
#
#
# This program is free software under the terms of the GNU General Public
# License (GPL). A copy of the GPL, "COPYING", should have been made
# available with this software.  If not, a copy may be obtained at 
# http://www.fsf.org/copyleft/gpl.html
#
# 
# $Id: timesheet-export-jobs,v 1.6 2001/08/29 22:23:15 adam Exp $
#
##use strict qw(refs vars);
require 'etc/timesheet.conf';
require 'lib/common-funcs.pl';
use ADB;

# My ADB database connection
my $dbconn;
# variable to hold SQL query replies from ADB
my $reply;
# string we put error messages into
my $oops;
# What is my filename to export to?
my $jobsexportfile = $Conf::EXPORTDIR . "/" . &date_download . "-jobs.txt";


print "exporting Jobs to file: $jobsexportfile\n";

# Connect to backend database
$dbconn = new ADB($Conf::DBADDR, $Conf::SQLDB);
if(!$dbconn->is_ok){
    $oops = $dbconn->errorstring;
    &error("Cannot Connect to backend: $oops\n");
}

(-f $jobsexportfile) && die("The export file already exists\n");

open(EXPORT, ">$jobsexportfile") ||
  &error("Cannot open exportfile: $jobsexportfile");

#do SuperUser download

my $sql = "SELECT * from job where downloaded='n' ORDER BY job_id";
$reply = $dbconn->query($sql);
if(!$reply){
  $oops = $dbconn->errorstring;
    &error("Error getting your jobs: $oops: SQL: $sql");
}
my $numrows = $reply->get_num_rows;
my %row;  
my @jobss;   
my $i = 0;
&print_heading;

open(LOG, ">>$Conf::EXPORT_LOG") || &error("Cannot open logfile $Conf::EXPORT_LOG for writing.  Contact Timesheet Aministrator");
while($i < $numrows){
    %row = $reply->get_row($i);
    &print_row(%row);
    push @jobs, $row{'job_id'};
    $i++;
}
my $date = &dbdate_today;
print LOG "exportjobs:$date:@jobs";
print LOG "\n";
close LOG;
print "Exported Jobs IDS:@jobs\n";

my $var;
foreach $var (@jobs){
    $reply = $dbconn->query("UPDATE job SET downloaded = 'y' WHERE job_id = $var;");
}

sub print_row {
  my %rec = @_;
  my $var;
  my $first;
  $first = 0;
  
  # Untabbify 
  # Stel hack, to make him happy
  
  my @variables = ('job_id','fkclient_id','fksupervisor_id',
		   'estimated_hours', 'category',
		   'open_date', 'bill_rate','job_description');
  
  foreach $var (@variables){
    $rec{$var} =~ s/\t/    /g;
    $rec{$var} =~ s/\n/ /g;
    $rec{$var} =~ s/\r/ /g;
    $rec{$var} =~ s/\f/ /g;
    if ($first == 0){
      $first = 1;
      print EXPORT "$rec{$var}";	
    }else{
      print EXPORT "\t$rec{$var}";
    }
  }
  print EXPORT "\r";
}

sub print_heading{
  my $first;
  my $var;
  my @variables = ('job_id','fkclient_id','fksupervisor_id',
		   'estimated_hours','category',
		   'open_date', 'bill_rate','job_description');
  $first = 0;
  foreach $var (@variables){
    $var =~ s/\n/ /g;
    $var =~ s/\r/ /g;
    $var =~ s/\f/ /g;
    if ($first == 0){
      $first = 1;
      print EXPORT "$var";
    }else{
      print EXPORT "\t$var";
    }
  }
  print EXPORT"\r";
}
