#!/usr/local/bin/perl5.6.1 -w
#######################################################################
# This software has been created by Genome Research Limited (GRL).    #
# GRL hereby grants permission to use, copy, modify and distribute    #
# this software and its documentation for non-commercial purposes     #
# without fee at the user's own risk on the basis set out below.      #
# GRL neither undertakes nor accepts any duty whether contractual or  #
# otherwise in connection with the software, its use or the use of    #
# any derivative, and makes no representations or warranties, express #
# or implied, concerning the software, its suitability, fitness for   #
# a particular purpose or non-infringement.                           #
# In no event shall the authors of the software or GRL be responsible #
# or liable for any loss or damage whatsoever arising in any way      #
# directly or indirectly out of the use of this software or its       #
# derivatives, even if advised of the possibility of such damage.     #
# Our software can be freely distributed under the conditions set out #
# above, and must contain this copyright notice.                      #
#######################################################################
# $Id: primer_wrap.pl,v 1.3 2006/11/09 12:49:38 kva Exp $

use strict;
use xml_primer_order;
#require $ENV{'HOME'}.'/badger-bin/finch/xml_primer_order.pm';
use Data::Dumper;
use WrapDBI;

my $order_id = $ARGV[0];

$order_id=~ s/\D*//g; 
#print "FILENAME = $order_id\n";

my $debug=1;
#my $file_type = '';
my $file_type = 'SIGMA_HTTPS_TEMP';

my $db = 'reports';
my $dbh = WrapDBI->connect("$db", {RaiseError => 1});    
$dbh->{autocommit} = 0;

my $stat_get_primers = "select e.id_primer, p.projectname||'.'||p.primername, p.primerseq from ext_fin_primer_orders e, primer p where e.id_primer = p.id_primer and order_id = ?";
my $get_primers = $dbh->selectall_arrayref($stat_get_primers,{},$order_id) || die "ERROR:$DBI::errstr";

if (scalar(@$get_primers) == '0'){
    print "NO PRIMERS ATTACHED TO THIS ORDER NUMBER.\n";
    exit;
}

#foreach (@$get_primers){
#    print "@$_\n";
#}

##Obtain the purchase order number.
my $purchase_stat = "select pt.prefix, p.id_purchaseorder, c.cost_team, ps.operator from primer pr, costcode c, primer_status ps, purchaseorderdict p, purchase_team_prefix_dict pt where pr.id_costcode = c.id_costcode and pr.id_primer = ps.id_primer and ps.status in (1,6) and p.iscurrent = 1 and c.cost_team = pt.team and pr.id_primer = ?";
my $sth_purchase = $dbh->prepare($purchase_stat) || die "ERROR:$DBI::errstr";
$sth_purchase->execute($order_id) || die "ERROR:$DBI::errstr";
my ($purchase_prefix,$purchase_no, $teamname, $who) = $sth_purchase->fetchrow();

#print "PURCHASE INFO - $purchase_prefix,$purchase_no, $teamname, $who\n";

my ($account,$po_prefix,$purchase_order,$secret,$supplier,$priority,$default_sender,$recipient);
$secret = 'sanger finishing';
$supplier = 'sigma';
$priority = 1;
$account = '5513901004';
$default_sender = 'ftp-primer@sanger.ac.uk';
#$recipient = 'kva';#set for test purposes 
$recipient = 'team58'; 

my $file;
#foreach $file (@all){

#print "FILE - $file\n";

my %PRIMER_ORD_DATA;
my $primer_count = 0;
foreach my $entry (@$get_primers){    
    $primer_count++;
    $PRIMER_ORD_DATA{$primer_count}{'OligoName'}=$entry->[1];
    $PRIMER_ORD_DATA{$primer_count}{'OligoSequence'}=$entry->[2];
    $PRIMER_ORD_DATA{$primer_count}{'OligoScale'}='0.025';
    $PRIMER_ORD_DATA{$primer_count}{'OligoPurification'}='DST';
    $PRIMER_ORD_DATA{$primer_count}{'OligoNotes'}='[ALIQUOT-72HR]';
    $PRIMER_ORD_DATA{$primer_count}{'OligoWells'}=1;
}

#print "ORDER ID = $order_id\n";

#print "HASH OF HASH CONTENTS primer order data----------->\n";
#print Data::Dumper::Dumper(\%PRIMER_ORD_DATA);
#print "----------------------------------------->\n";

my %PRIMER_PLATE_DATA;
$PRIMER_PLATE_DATA{'StockPlateNumber'}='NONE';
$PRIMER_PLATE_DATA{'StockPlateID'}='NONE';
$PRIMER_PLATE_DATA{'WorkingPlateNumber'}='NONE';
$PRIMER_PLATE_DATA{'WorkingPlateID'}='NONE';
$PRIMER_PLATE_DATA{'Comment1'}=$teamname;
$PRIMER_PLATE_DATA{'Comment2'}=$order_id;
$PRIMER_PLATE_DATA{'Comment3'}='';
$PRIMER_PLATE_DATA{'OligoCount'} = $primer_count;

#print "HASH OF HASH CONTENTS  primer plate data----------->\n";
#print Data::Dumper::Dumper(\%PRIMER_PLATE_DATA);
#print "----------------------------------------->\n";

$po_prefix = $purchase_prefix;
$purchase_order = $purchase_no.'.'.$order_id;

my %SENDER_DETAILS;
$SENDER_DETAILS{'Sender'}=$recipient; #person to receive sigma reply emails.
$SENDER_DETAILS{'Orderer'}=$who; #person who placed the original order. 
$SENDER_DETAILS{'Order_ID'}=$order_id;
$SENDER_DETAILS{'Team'}=$teamname;
$SENDER_DETAILS{'Account'}=$account;
$SENDER_DETAILS{'PO_Prefix'}=$po_prefix;
$SENDER_DETAILS{'Purchase_Order'}=$purchase_order;
$SENDER_DETAILS{'Secret_Passphrase'}=$secret;
$SENDER_DETAILS{'Supplier'}=$supplier;
$SENDER_DETAILS{'Priority'}=$priority;
$SENDER_DETAILS{'FileName'}='FIN'.$order_id;
$SENDER_DETAILS{'Default_Sender'}=$default_sender;
$SENDER_DETAILS{'File_Type'}=$file_type;

#print "HASH OF HASH CONTENTS  sender details----------->\n";
#print Data::Dumper::Dumper(\%SENDER_DETAILS);
#print "----------------------------------------->\n";


my $return_error = &xml_primer_order::create_order(\%SENDER_DETAILS,\%PRIMER_PLATE_DATA,\%PRIMER_ORD_DATA,$debug);

unless ($return_error == 0){
    warn "Error raised during the XML order creation step: $return_error : $!\n";
}

#}

##END FILE HANDLES
$sth_purchase->finish();

END {
    if ($dbh){
        eval {
            $dbh->rollback();
            $dbh->disconnect();
        }
    }
};    


__END__

=head1 NAME

 primer_wrap.pl

=head1 SYNOPSIS

 primer_wrap.pl [args]

=head1 DESCRIPTION

=head1 AUTHOR

 Karen Ambrose

=head1 HISTORY

 # $Log: primer_wrap.pl,v $
% Revision 1.3  2006/11/09  12:49:38  kva
% *** empty log message ***
%
% Revision 1.2  2006/10/24  10:34:27  kva
% *** empty log message ***
%
% Revision 1.1  2006/10/23  15:12:54  kva
% Initial revision
%

=cut
