<%doc>
Produces a report of Assets
</%doc>

<%args>
$report_type => undef
$sort        => 'type'
</%args>

<%init>
my $DEBUG = 0;
my (@headers, @rows, @assets) = ();

my %t;
my $grandtotal = 0;

if ( $report_type eq 'type_product' || $report_type eq 'detailed' ){

    my $dbh = Netdot::Model->db_Main;
    
    my $assets = $dbh->selectall_arrayref("
              SELECT asset.id, asset.serial_number, asset.inventory_number, asset.reserved_for,
                     product.id, product.name, producttype.name
              FROM   asset, product, producttype 
              WHERE  asset.product_id=product.id
                 AND product.type=producttype.id");
    
    
    my $installed = Asset->get_installed_hash();
    
    foreach my $row ( @$assets ){
	my ($id, $sn, $inv, $res, $pid, $pname, $ptype) = @$row;
	$t{$ptype}{total}++;
	$t{$ptype}{prod}{$pname}{total}++;
	$t{$ptype}{prod}{$pname}{id}  = $pid;
	$t{$ptype}{prod}{$pname}{assets}{$sn}{id}  = $id;
	$t{$ptype}{prod}{$pname}{assets}{$sn}{installed} = (exists($installed->{$id}))? 1 : 0;
	$t{$ptype}{prod}{$pname}{assets}{$sn}{inv} = $inv;
	$t{$ptype}{prod}{$pname}{assets}{$sn}{res} = $res;
	$grandtotal++;
    }

    if ( $DEBUG ){
	use Data::Dumper;
	print '<pre>';
	print Dumper(%t);
	print Dumper($installed);
	print '</pre>';
    }
}
</%init>

<div class="container">
  <div class="containerhead">Asset Inventory</div>
    <div class="containerbody"
    <p><br>
    <form name="report_form" action="asset_inventory.html" method="POST">
         By <select name="report_type">
         <option value="">Select Report</option>
         <option value="type_product" <% ($report_type eq "type_product")?"SELECTED":"" %>>Type/Model Summary</option>
         <option value="detailed" <% ($report_type eq "detailed")?"SELECTED":"" %>>Detailed</option>
         </select>
         <input type="submit" name="submit" class="btn" value="Go" >
    </form>

<%perl>

if ( $report_type eq 'type_product' ){
    @headers = ( 'Type', 'Model', 'Count' );
    my @row = ();
    push( @row, "Total Assets:" );
    push( @row, "&nbsp;" );
    push( @row, $grandtotal );
    push( @rows, \@row );

    foreach my $ptype ( sort keys %t ){
	my @row = ();
	push( @row, "<strong>$ptype</strong>" );
	push( @row, "&nbsp;" );
	push( @row, '<strong>' . $t{$ptype}{total} . '</strong>' );
	push( @rows, \@row );

	foreach my $pname ( sort keys %{ $t{$ptype}{prod} } ){
	    my @row = ();
	    push( @row, "&nbsp;" );
	    my $pid = $t{$ptype}{prod}{$pname}{id};
	    push( @row, "<a href=\"../generic/search_obj.html?table=Asset&product_id=$pid&res=1\">$pname</a>" );
	    push( @row, $t{$ptype}{prod}{$pname}{total}  );
	    push( @rows, \@row );
	}
    }

}elsif ( $report_type eq 'detailed' ){
    my %sortpos;
    my $i = 0;
    foreach my $header ('Type', 'Model', 'S/N', 'Inventory', 'Installed?', 'Reserved For'){
	$sortpos{$header} = $i++;
	push @headers, "<a href=\"asset_inventory.html?report_type=detailed&sort=$header\">$header</a>";
    }
    foreach my $ptype ( sort keys %t ){
	foreach my $pname ( keys %{ $t{$ptype}{prod} } ){
	    foreach my $sn ( keys %{ $t{$ptype}{prod}{$pname}{assets} } ){
		my @row = ();
		push( @row, $ptype );
		push( @row, $pname );
		my $id  = $t{$ptype}{prod}{$pname}{assets}{$sn}{id};
		push( @row, "<a href=\"../generic/view.html?table=Asset&id=$id\">$sn</a>" );
		push( @row, $t{$ptype}{prod}{$pname}{assets}{$sn}{inv} );
		push( @row, ($t{$ptype}{prod}{$pname}{assets}{$sn}{installed})? 'yes' : 'no');
		push( @row, $t{$ptype}{prod}{$pname}{assets}{$sn}{res} );
		push( @rows, \@row );
	    }
	}
    }

    # Sort the thing
    my $pos = $sortpos{$sort};
    @rows = sort { $a->[$pos] cmp $b->[$pos] } @rows;
}

$m->comp('/generic/data_table.mhtml', field_headers=>\@headers, data=>\@rows);
</%perl>

    </div>
  </div>
</div>

