add cust_main.agent_custid (at least to schema and customer view, no manual editing...
[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   $extra_sql = " WHERE ". FS::part_referral->acl_agentnum_sql;
36
37   my $statement = "SELECT COUNT(*) FROM h_cust_main
38                     WHERE history_action = 'insert'
39                       AND refnum = ?
40                       AND history_date >= ?
41                       AND history_date < ?
42                       AND ". $curuser->agentnums_sql;
43   my $sth = dbh->prepare($statement)
44     or die dbh->errstr;
45
46   my $show_agentnums = scalar($curuser->agentnums);
47
48 %>
49
50 <%= include('/elements/table-grid.html') %>
51
52 <% my $bgcolor1 = '#eeeeee';
53    my $bgcolor2 = '#ffffff';
54    my $bgcolor = '';
55 %>
56
57 <TR>
58   <TH CLASS="grid" BGCOLOR="#cccccc" COLSPAN=2 ROWSPAN=2>Advertising source</TH>
59   <% if ( $show_agentnums ) { %>
60     <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2>Agent</TH>
61   <% } %>
62   <TH CLASS="grid" BGCOLOR="#cccccc" COLSPAN=<%= scalar(keys %after) %>>Customers</TH>
63 </TR>
64 <% for my $period ( keys %after ) { %>
65   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1><%= $period %></FONT></TH>
66 <% } %>
67 </TR>
68
69 <%
70 foreach my $part_referral (
71
72   qsearch({
73     'table'     => 'part_referral',
74     'extra_sql' => "$extra_sql ORDER BY refnum",
75   })
76
77 ) {
78
79   if ( $bgcolor eq $bgcolor1 ) {
80     $bgcolor = $bgcolor2;
81   } else {
82     $bgcolor = $bgcolor1;
83   }
84
85 %>
86       <TR>
87
88         <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"><A HREF="<%= $p %>edit/part_referral.html?<%= $part_referral->refnum %>">
89           <%= $part_referral->refnum %></A></TD>
90         <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"><A HREF="<%= $p %>edit/part_referral.html?<%= $part_referral->refnum %>">
91           <%= $part_referral->referral %></A></TD>
92
93         <% if ( $show_agentnums ) { %>
94           <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"><%= $part_referral->agentnum ? $part_referral->agent->agent : '(global)' %></TD>
95         <% } %>
96
97         <% for my $period ( keys %after ) {
98           $sth->execute( $part_referral->refnum,
99                          $today-$after{$period},
100                          $today+$before{$period},
101           ) or die $sth->errstr;
102           my $number = $sth->fetchrow_arrayref->[0];
103         %>
104           <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>" ALIGN="right"><%= $number %></TD>
105         <% } %>
106       </TR>
107 <% } %>
108
109 <%
110   $statement =~ s/AND refnum = \?//;
111   $sth = dbh->prepare($statement)
112     or die dbh->errstr;
113 %>
114       <TR>
115         <TD BGCOLOR="#dddddd" ALIGN="center" COLSPAN=3><B>Total</B></TD>
116         <% for my $period ( keys %after ) {
117           $sth->execute( $today-$after{$period},
118                          $today+$before{$period},
119           ) or die $sth->errstr;
120           my $number = $sth->fetchrow_arrayref->[0];
121         %>
122           <TD BGCOLOR="#dddddd" ALIGN="right"><B><%= $number %><B></TD>
123         <% } %>
124       </TR>
125     </TABLE>
126   </BODY>
127 </HTML>