#!/tools/bin/perl -w
#------------------------------------------------------------------------------
#   convert change request data from a Flat File from a Legacy sys to RT.
#   Turn OFF RT and then change your RT_SiteConfig.pm to turn off logging.
#   Be sure to TURN OFF all scrips either before being RT down or via DBA/SQL.
#   scrips will try to run and logging will make the program run FOREVER!!
#   flat file input can be created by via Data::Dumper;
#   Obviously, field names will be different. OUR Data Names are all CAPS_CAPS
#------------------------------------------------------------------------------
use strict;

use lib "/$RTHome/rt/perl/lib";
use lib "/$RTHome/rt/lib";

use RT;
use RT::Ticket;
use RT::Transaction;
use RT::Attachment;
use MIME::Entity;  



#------------------------------------------------------------------------------
#    process all requests into tickets
#------------------------------------------------------------------------------
sub     Read_Temp_File
{
    my ( $fh ) = @_;

    my
    (
        $in,
        $objectId
    );

    while ( $in = <$fh> )
    {
        chomp $in;
        $objectId = New_Ticket( $VAR1 );    #ObjectId = Ticket.id
    }
}



#------------------------------------------------------------------------------
#    create Ticket for one request
#------------------------------------------------------------------------------
sub     New_Ticket
{
    my ( $in ) = @_;

    my
    (
        $ticket,
        $mimeObj,
        $ticketid, 
        $transaction_object, 
        $err,
        $rc,
        $NUMBER,
        $DATE_OPENED,
        $DATE_RESOLVED,
        $REQUEST_TYPE,
        $SYSTEM_AREA,
        $ISSUE_DESCRIPTION,
        $RESOLUTION,
        %ticket_vals
    );
  

    (  $NUMBER, $DATE_OPENED, $DATE_RESOLVED, $REQUEST_TYPE, $SYSTEM_AREA, $ISSUE_DESCRIPTION, $RESOLUTION ) = split( "\t", $in );     #your field names for your data
    $ticket = RT::Ticket->new( $RT::SystemUser );
    $mimeObj = MIME::Entity->build(Data => $ISSUE_DESCRIPTION, Type => 'text/plain');    #whatever field has descr

#   set Date Fields

    If  ($DATE_OPENED)
        {
         my $opened = RT::Date->new($RT::SystemUser);
         $opened->Set(Format=>'unknown', Value=>$DATE_OPENED);
         $DATE_OPENED = $opened->ISO;
        }

    If  ($DATE_RESOLVED)
        {
         my $resolved = RT::Date->new($RT::SystemUser);
         $resolved->Set(Format=>'unknown', Value=>$DATE_RESOLVED);
         $DATE_RESOLVED = $opened->ISO;
        }

#------------------------------------------------------------------------------
#         build ticket values
#------------------------------------------------------------------------------

    %ticket_vals = 
    (  
        Subject => $ISSUE_DESCRIPTION,
        MIMEObj => $mimeObj,                
        Creator => 4233,   #API bug won't look up corresponding id for name
        Owner => 4233,
        Requestor => 4233,
        Priority => 3,
        InitialPriority => 3,
        FinalPriority => 3,
        Started => $DATE_OPENED,
        Resolved => $DATE_RESOLVED,
        Status => "resolved"
    );


    ( $ticketid, $transaction_object, $err ) = $ticket->Create( %ticket_vals );
    print "New_Ticket request=", $NUMBER, " $err \n";  #debug
    die "New_Ticket Error: $err \n"   unless $ticketid;  

    ( $rc, $err ) = $ticket->AddCustomFieldValue( Field => 'Description', Value => $ISSUE_DESCRIPTION );  
    ( $rc, $err ) = $ticket->AddCustomFieldValue( Field => 'Reference Number', Value => $NUMBER );
    ( $rc, $err ) = $ticket->AddCustomFieldValue( Field => 'Travel-Area of System', Value => $SYSTEM_AREA );
    ( $rc, $err ) = $ticket->AddCustomFieldValue( Field => 'Work-State', Value => "Resolved" );
    ( $rc, $err ) = $ticket->AddCustomFieldValue( Field => 'Resolution Type', Value => $REQUEST_TYPE );
    ( $rc, $err ) = $ticket->AddCustomFieldValue( Field => 'Resolution Description', Value => $RESOLUTION );  


    return $ticketid;     #Ticket.id
}

#
#------------------------------------------------------------------------------
#   main
#------------------------------------------------------------------------------
my
(
    $fh,
    $fname,
);

RT::LoadConfig();
RT::Init();

$fname = '/var/tmp/unx.travel_export_data_rt.txt';     #your input file name here
$fh = IO::File->new( "$fname" )   or die "cannot open $fname $!\n";
Read_Temp_File( $fh );
$fh->close();