1 <% include('/elements/header.html', "$classname Inventory Activity Report") %>
2 <% include('/elements/table-grid.html') %>
4 % my $TH = 'TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=1';
5 <<%$TH%> WIDTH="10%" ALIGN="left">Day (<% time2str("%B %Y", $sdate) %>)</TH>
6 % foreach my $day (0..$numdays-1) {
7 <<%$TH%> WIDTH="2%" ALIGN="right"><% $day+1 %></TH>
10 % for (my $r=0; $r < scalar(@rows); $r++) {
12 % my $TD = 'TD CLASS="grid" BGCOLOR="'.($r % 2 ? '#ffffff' : '#eeeeee').'"';
13 <<%$TD%>><% $labels[$r] %></TD>
14 % for my $day (0..$numdays-1) {
15 <<%$TD%> ALIGN="right"><% $rows[$r][$day] %></TD>
22 use Date::Parse 'str2time';
23 use Date::Format 'time2str';
24 use Data::Dumper 'Dumper';
26 my ($agentnum, $classnum, $month, $year, $sdate, $edate);
27 $classnum = $cgi->param('classnum'); # may be empty
28 $agentnum = $cgi->param('agentnum'); # may also be empty
31 my $class = qsearchs('inventory_class', { classnum => $classnum });
32 die "classnum $classnum not found!" if !$class;
33 $classname = $class->classname . ' ';
36 $month = $cgi->param('_month') || time2str('%m', time);
37 $year = $cgi->param('_year') || time2str('%Y', time);
39 $sdate = str2time("$year-$month-01");
40 $edate = str2time($year + ($month == 12 ? 1 : 0) .
42 (($month + 1) % 12 || 12) .
44 my $numdays = sprintf("%.0f",($edate-$sdate)/86400);
45 my @days = (0..$numdays - 1);
46 # Initialize each row with zeroes.
55 push @labels, 'Transfer In', 'Transfer Out';
57 push @labels, 'Closing Balance';
59 my %agent = ('agentnum' => $agentnum) if $agentnum;
60 my %class = ('classnum' => $classnum) if $classnum;
62 my @rows = ( map {[ (0) x $numdays ]} @labels);
63 local($FS::Record::qsearch_qualify_columns) = 0;
64 my $opening_balance = scalar(
65 qsearch('h_inventory_item',
69 FS::h_inventory_item->sql_h_search($sdate) )
72 foreach my $day (0..$numdays-1) {
73 $rows[0][$day] = ($day == 0) ?
78 foreach my $action (qw(insert replace_new replace_old)) {
81 'table' => 'h_inventory_item',
82 'hashref' => { 'history_action' => $action,
84 'order_by' => 'ORDER BY itemnum, history_date',
86 ' AND history_date >= '.($sdate + 86400*$day).
87 ' AND history_date < ' .($sdate + 86400*($day+1)),
91 # Incoming items: simple, just count the inserts
92 $rows[1][$day] = scalar(grep {!$agentnum or $_->agentnum == $agentnum}
93 @{ $history{'insert'} });
95 # Other item changes: trickier.
96 # Notice the order_by parameter above.
97 # Both lists are sorted by itemnum, then by date, so unless some villain has
98 # been rapidly replacing the same record several times per second, the
99 # replace_old and replace_new from the same operation will be in the same
101 while(my $h_new = shift @{ $history{'replace_new'} }) {
102 my $h_old = shift @{ $history{'replace_old'} };
103 die "history error" if !defined($h_old)
104 or $h_old->itemnum != $h_new->itemnum;
105 if(!$agentnum or $h_new->agentnum == $agentnum) {
106 if(!$h_old->svcnum and $h_new->svcnum) {
107 # item was put into service.
110 elsif($h_old->svcnum and !$h_new->svcnum) {
111 # item was taken out of service.
115 if($agentnum and $h_old->agentnum != $agentnum and $h_new->agentnum == $agentnum) {
116 # item was transferred from another agent
119 elsif($agentnum and $h_old->agentnum == $agentnum and $h_new->agentnum != $agentnum) {
120 # item was transferred to another agent
123 # Add other cases here.
126 $rows[-1][$day] = $rows[0][$day]
131 $rows[-1][$day] += $rows[4][$day] - $rows[5][$day];