sub write_batch {
my $self = shift;
my $q = $self->query();
my %batch_info;
my $batch_id = $q->param('batch_id');
my $batch = Grewt::Batch->retrieve($batch_id);
my $do_entry = $q->param('entry_flag');
my @studies = Grewt::Study->retrieve_all;
my %study_ref;
my %by_study;
my @add_tubes;
my @del_tubes;
my @btests = Grewt::BatchTest->search(batch => $batch_id);
my @old_tubes = Grewt::BatchTube->search(batch => $batch_id);
my @new_tubes = $q->param('assigned');
#Need to do two loops to determine 1.What has been added and
#2.What has been removed.
#Loop through tubes in 'assigned' list
foreach my $ntube (@new_tubes) {
my $found = "no";
#Loop through existing tubes
foreach my $otube (@old_tubes) {
#If we already have this tube in the list
if ($otube->tube->number eq $ntube) {
#Then set a flag
$found = "yes";
}
}
#If this tube is newly added to the batch
if ($found eq "no") {
#Then put it on the list to be inserted into the table
push @add_tubes , $ntube;
}
#Either way, we want to pass its information on to the template
push @{ $batch_info{$ntube->study->id} }, $tube;
}
#Loop through the existing tubes
foreach my $otube (@old_tubes) {
my $found = "no";
#Loop through the list of assigned tubes
foreach my $ntube (@new_tubes) {
#If it's still in the list
if ($otube->tube->number eq $ntube) {
#Then set a flag to leave it alone
$found = "yes"
}
}
#If it's been removed from the list
if ($found eq "no") {
#Then add it to the list of tubes to be deleted
push @del_tubes , $otube->tube->number;
}
}
#Loop through tubes to be deleted
foreach my $del (@del_tubes) {
Grewt::BatchTube->search(batch => $batch_id, tube => $del)->delete_all;
#BatchRegistry will auto-cascade, I hope.
}
#Loop through tubes to be added
foreach my $add (@add_tubes) {
my $tube = Grewt::Tube->retrieve($add);
my $new_bt = Grewt::BatchTube->insert( {
batch => $batch_id,
tube => $add
});
$new_bt->update;
#Now insert into the BatchRegistry table
foreach my $btest (@btests) {
my $new_reg = Grewt::BatchRegistry-> insert(
{
batch_test => $btest,
batch_tube => $new_bt
});
$new_reg->update;
}
}
# If the user chose to go on and enter data, pass them to the create_entry rm
if ($do_entry eq "true") {
return $self->redirect(
"http://helpdesk.gentris.com/cgi-bin/Grewt.pl?rm=create_entry&batch_id=$batch_id");
}
prep_batch_sets(\%study_ref,\%by_study,$batch);
return $self->tt_process('edit_batch_sets.tmpl', {
batch => $batch,
tlist => \%by_study,
batch_info => \%batch_info,
sref => \%study_ref,
});
}