#!/usr/bin/perl

use strict;

use CGI;

use Time::localtime;

use Opals::Context;
use Opals::Template qw(
    tmpl_read
    tmpl_write
    tmpl_preference
);

use Opals::Eq_Inventory qw(

    inv_getInventoryStatusByBcList
);

use Opals::Equipment qw(
    
    eq_item_findByBarcode

);

use Opals::Date qw(
    date_time_text
);

my $tm = localtime;
my $todayStr = sprintf("%04d-%02d-%02d", $tm->year+1900, ($tm->mon)+1, $tm->mday);


my $dbh = Opals::Context->dbh();
END { $dbh->disconnect(); }

my $cgi = CGI->new;
my $input = $cgi->Vars();
my ($permission, $cookieList, $template) = tmpl_read(
    {
        dbh             => $dbh,
        cgi             => $cgi,
        tmplFile        => '/eqmnt/report/inventory_prt.tmpl',
        reqPermission   => 'eq_report',
    }
);


my $dateFrom    = $input->{'dateFrom'};
my $dateTo      = $input->{'dateTo'};

my $type = $input->{'type'};
my $invId = $input->{'invId'};
my $sortField = $input->{'sortField'};
my $status = $input->{'status'};

if ($permission && $permission->{'eq_report'}) {

    my $bcList;
    my @bc;
    my $bcString="";
    if ($type =~ m/^selected$/){
        $bcList   = $input->{'bcList'};
        my @bcTmp = split(/\$/, $bcList);
        foreach my $bc(@bcTmp) {  
            $bc =~ s/updated:://i;
            if ($bc && $bc ne ""){
                $bcString .=  "'" . $bc . "'"  . ",";
            }
        }
    }
    $bcString =~ s/,$//;
    my $bcRet = inv_getInventoryStatusByBcList($dbh, $invId, $bcString, $status , $type ,$sortField);

    $template->param (
        bcList  =>  $bcRet
    );
}


tmpl_write($dbh, $cgi, $cookieList, $template);

#------------------------------------------------------------------

