#!/usr/local/bin/perl
#######################################################################
# 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.                      #
#######################################################################
# dn1g; code to print barcodes for source_plates

use strict;
use WrapDBI;
use BarcodeRevised;


# Database
my $db = 'platetrack';
warn "Connecting to database...\n";
my $dbh = WrapDBI->connect("$db", {RaiseError => 1});

# Get the relevant data from the database

my @to_print;

my $state = "
select sp.id_sourceplate, sp.platename, ss.remark, spd.description, sp.libraryname, v.vectorname
from source_plate sp,
sourceplate_status ss,
sourceplatetypedict spd,
library l,
vectorsite v
where sp.id_sourceplate = ss.id_sourceplate
and sp.id_type = spd.id_dict
and sp.libraryname = l.libraryname
and l.id_vectorsite = v.id_vectorsite
and ss.iscurrent = 1
and ss.status = 1
and sp.id_sourceplate between 201 and 202";
my $sth1 = $dbh->prepare($state) || die "ERROR:$DBI::errstr";
$sth1->execute || die "ERROR:$DBI::errstr";

my ($id_sourceplate, $platename, $remark, $description, $libraryname, $vectorname);
while (($id_sourceplate, $platename, $remark, $description, $libraryname, $vectorname) = $sth1->fetchrow) {
    my %hash;
    $hash{id_sourceplate} = $id_sourceplate;
    $hash{platename} = $platename;
    $hash{remark} = $remark;
    $hash{description} = $description;
    $hash{libraryname} = $libraryname;
    $hash{vectorname} = $vectorname;
    warn "id_sourceplate $id_sourceplate\nplatename $platename\nremark $remark\ndescription $description\nlibraryname $libraryname\nvectorname $vectorname\n";
    warn "-----------------------------\n";
    push @to_print, \%hash;
}

# Printer object
my $printer = Barcode->new($dbh);

# Actually print the barcode

warn "Printing...\n";

foreach my $this (@to_print){

    my ($lib, $vec, $pri) = $this->{remark} =~ /(\w+)-(\w+)-(\w+)/;
    
    $printer->Printer("d318bc");
    $printer->Type("SO");
    $printer->Name("$this->{platename}");
    $printer->Project("$this->{libraryname}");
    $printer->Suffix("$this->{vectorname}");
    $printer->Owner("$pri");
    $printer->Barcode("$this->{id_sourceplate}");
    $printer->Quantity(1);
    $printer->Labellist();
}
eval {
    $printer->Printlabel();
};
    
if ($@){
    warn "Problems printing...\n";
    die $@;
}



END {

    if (defined $sth1){
	$sth1->finish;
    }
   
    if (defined $dbh){
	warn "Rolling back...\n";
	$dbh->rollback;
	warn "Disconnecting from database...\n";
	$dbh->disconnect;
    }
};



