bold the total footer
[freeside.git] / httemplate / browse / part_referral.html
1 <%= include("/elements/header.html","Advertising source Listing" ) %>
2
3 Where a customer heard about your service. Tracked for informational purposes.
4 <BR><BR>
5
6 <A HREF="<%= $p %>edit/part_referral.html"><I>Add a new advertising source</I></A>
7 <BR><BR>
8
9 <%
10   my $today = timelocal(0, 0, 0, (localtime(time))[3..5] );
11   my %after;
12   tie %after, 'Tie::IxHash',
13     'Today'         =>        0,
14     'Yesterday'     =>    86400, # 60sec * 60min * 24hrs
15     'Past week'     =>   518400, # 60sec * 60min * 24hrs * 6days
16     'Past 30 days'  =>  2505600, # 60sec * 60min * 24hrs * 29days 
17     'Past 60 days'  =>  5097600, # 60sec * 60min * 24hrs * 59days 
18     'Past 90 days'  =>  7689600, # 60sec * 60min * 24hrs * 89days 
19     'Past 6 months' => 15724800, # 60sec * 60min * 24hrs * 182days 
20     'Past year'     => 31486000, # 60sec * 60min * 24hrs * 364days 
21     'Total'         => $today,
22   ;
23   my %before = (
24     'Today'         =>   86400, # 60sec * 60min * 24hrs
25     'Yesterday'     =>       0,
26     'Past week'     =>   86400, # 60sec * 60min * 24hrs
27     'Past 30 days'  =>   86400, # 60sec * 60min * 24hrs
28     'Past 60 days'  =>   86400, # 60sec * 60min * 24hrs
29     'Past 90 days'  =>   86400, # 60sec * 60min * 24hrs
30     'Past 6 months' =>   86400, # 60sec * 60min * 24hrs
31     'Past year'     =>   86400, # 60sec * 60min * 24hrs
32     'Total'         =>   86400, # 60sec * 60min * 24hrs
33   );
34
35   my $curuser = $FS::CurrentUser::CurrentUser;
36   my $extra_sql = $curuser->agentnums_sql;
37   $extra_sql .= ' OR agentnum IS NULL '
38     if $curuser->access_right('Edit global advertising sources');
39
40   $extra_sql = " WHERE $extra_sql";
41
42   my $statement = "SELECT COUNT(*) FROM h_cust_main
43                     WHERE history_action = 'insert'
44                       AND refnum = ?
45                       AND history_date >= ?
46                       AND history_date < ?
47                       AND ". $curuser->agentnums_sql;
48   my $sth = dbh->prepare($statement)
49     or die dbh->errstr;
50
51   my $show_agentnums = scalar($curuser->agentnums);
52
53 %>
54
55 <%= include('/elements/table-grid.html') %>
56
57 <% my $bgcolor1 = '#eeeeee';
58    my $bgcolor2 = '#ffffff';
59    my $bgcolor = '';
60 %>
61
62 <TR>
63   <TH CLASS="grid" BGCOLOR="#cccccc" COLSPAN=2 ROWSPAN=2>Advertising source</TH>
64   <% if ( $show_agentnums ) { %>
65     <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2>Agent</TH>
66   <% } %>
67   <TH CLASS="grid" BGCOLOR="#cccccc" COLSPAN=<%= scalar(keys %after) %>>Customers</TH>
68 </TR>
69 <% for my $period ( keys %after ) { %>
70   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1><%= $period %></FONT></TH>
71 <% } %>
72 </TR>
73
74 <%
75 foreach my $part_referral (
76
77   qsearch({
78     'table'     => 'part_referral',
79     'extra_sql' => "$extra_sql ORDER BY refnum",
80   })
81
82 ) {
83
84   if ( $bgcolor eq $bgcolor1 ) {
85     $bgcolor = $bgcolor2;
86   } else {
87     $bgcolor = $bgcolor1;
88   }
89
90 %>
91       <TR>
92
93         <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"><A HREF="<%= $p %>edit/part_referral.html?<%= $part_referral->refnum %>">
94           <%= $part_referral->refnum %></A></TD>
95         <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"><A HREF="<%= $p %>edit/part_referral.html?<%= $part_referral->refnum %>">
96           <%= $part_referral->referral %></A></TD>
97
98         <% if ( $show_agentnums ) { %>
99           <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"><%= $part_referral->agentnum ? $part_referral->agent->agent : '(global)' %></TD>
100         <% } %>
101
102         <% for my $period ( keys %after ) {
103           $sth->execute( $part_referral->refnum,
104                          $today-$after{$period},
105                          $today+$before{$period},
106           ) or die $sth->errstr;
107           my $number = $sth->fetchrow_arrayref->[0];
108         %>
109           <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>" ALIGN="right"><%= $number %></TD>
110         <% } %>
111       </TR>
112 <% } %>
113
114 <%
115   $statement =~ s/AND refnum = \?//;
116   $sth = dbh->prepare($statement)
117     or die dbh->errstr;
118 %>
119       <TR>
120         <TD BGCOLOR="#dddddd" ALIGN="center" COLSPAN=3><B>Total</B></TD>
121         <% for my $period ( keys %after ) {
122           $sth->execute( $today-$after{$period},
123                          $today+$before{$period},
124           ) or die $sth->errstr;
125           my $number = $sth->fetchrow_arrayref->[0];
126         %>
127           <TD BGCOLOR="#dddddd" ALIGN="right"><B><%= $number %><B></TD>
128         <% } %>
129       </TR>
130     </TABLE>
131   </BODY>
132 </HTML>