RT#8460: monthly opening balance
[freeside.git] / httemplate / search / h_inventory_item.html
1 <% include('/elements/header.html', "$classname Inventory Activity Report") %>
2 <% include('/elements/table-grid.html') %>
3   <TR>
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>
8 % }
9   </TR>
10 % for (my $r=0; $r < scalar(@rows); $r++) {
11   <TR>
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>
16 %   }
17   </TR>
18 % }
19 </TABLE>
20
21 <%init>
22 use Date::Parse 'str2time';
23 use Date::Format 'time2str';
24 #use Data::Dumper::HTML 'dumper_html';
25
26 my ($classnum, $month, $year, $sdate, $edate);
27 $classnum = $cgi->param('classnum'); # may be empty
28 my $classname = '';
29 if($classnum) {
30   my $class = qsearchs('inventory_class', { classnum => $classnum });
31   die "classnum $classnum not found!" if !$class;
32   $classname = $class->classname . ' ';
33 }
34
35 $month = $cgi->param('_month') || time2str('%m', time);
36 $year = $cgi->param('_year') || time2str('%Y', time);
37
38 $sdate = str2time("$year-$month-01");
39 $edate = str2time($year + ($month == 12 ? 1 : 0) .
40                   '-' .
41                   (($month + 1) % 12 || 12) .
42                   '-01');
43 my $numdays = sprintf("%.0f",($edate-$sdate)/86400);
44 my @days = (0..$numdays - 1);
45 # Initialize each row with zeroes.
46 my @labels = (
47   'Opening Balance',
48   'Quantity Received',
49   'Quantity Sold',
50   'Quantity Returned',
51   'Closing Balance',
52 );
53 my @rows = ( map {[ (0) x $numdays ]} @labels);
54 my $opening_balance = scalar(
55   qsearch('h_inventory_item', 
56           { 'svcnum' => '',
57             ($classnum ? ('classnum' => $classnum) : () ) },
58           FS::h_inventory_item->sql_h_search($sdate) )
59   ) || 0;
60
61 foreach my $day (0..$numdays-1) {
62   $rows[0][$day] = ($day == 0) ? 
63                     $opening_balance : 
64                     $rows[4][$day-1];
65
66   my %history;
67   foreach my $action (qw(insert replace_new replace_old)) {
68     $history{$action} = [
69                         qsearch({
70                           'table'     => 'h_inventory_item',
71                           'hashref'   => { $classnum ? ('classnum' => $classnum) : (),
72                                            'history_action' => $action },
73                           'order_by'  => 'ORDER BY itemnum, history_date',
74                           'extra_sql' => 
75                             ' AND history_date >= '.($sdate + 86400*$day).
76                             ' AND history_date < ' .($sdate + 86400*($day+1)),
77                           } ) 
78                         ];
79   }
80   # Incoming items: simple, just count the inserts
81   $rows[1][$day] = scalar(@{ $history{'insert'} });
82
83   # Other item changes: trickier.
84   # Notice the order_by parameter above.
85   # Both lists are sorted by itemnum, then by date, so unless some villain has 
86   # been rapidly replacing the same record several times per second, the 
87   # replace_old and replace_new from the same operation will be in the same 
88   # position.
89   while(my $h_new = shift @{ $history{'replace_new'} }) {
90     my $h_old = shift @{ $history{'replace_old'} };
91     die "history error" if !defined($h_old) 
92                            or $h_old->itemnum != $h_new->itemnum;
93     if(!$h_old->svcnum and $h_new->svcnum) {
94       # item was put into service.
95       $rows[2][$day]++;
96     }
97     elsif($h_old->svcnum and !$h_new->svcnum) {
98       # item was taken out of service.
99       $rows[3][$day]++;
100     }
101     # Add other cases here.
102   }
103   # Closing balance
104   $rows[4][$day] = $rows[0][$day]
105                  + $rows[1][$day]
106                  - $rows[2][$day]
107                  + $rows[3][$day];
108 }
109
110 </%init>
111