c50a406ede46dd6f3213bec9b7bcf58a6ea4d730
[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
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 ( FS::part_referral->all_part_referral(1) ) {
71
72   if ( $bgcolor eq $bgcolor1 ) {
73     $bgcolor = $bgcolor2;
74   } else {
75     $bgcolor = $bgcolor1;
76   }
77
78   $a = 0;
79
80 %>
81       <TR>
82
83         <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>">
84           <% if ( $part_referral->agentnum || $curuser->access_right('Edit global advertising sources') ) { 
85             $a++;
86           %>
87             <A HREF="<%= $p %>edit/part_referral.html?<%= $part_referral->refnum %>">
88           <% } %>
89           <%= $part_referral->refnum %><%= $a ? '</A>' : '' %></TD>
90         <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>">
91           <% if ( $a ) { %>
92             <A HREF="<%= $p %>edit/part_referral.html?<%= $part_referral->refnum %>">
93           <% } %>
94           <%= $part_referral->referral %><%= $a ? '</A>' : '' %></TD>
95
96         <% if ( $show_agentnums ) { %>
97           <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"><%= $part_referral->agentnum ? $part_referral->agent->agent : '(global)' %></TD>
98         <% } %>
99
100         <% for my $period ( keys %after ) {
101           $sth->execute( $part_referral->refnum,
102                          $today-$after{$period},
103                          $today+$before{$period},
104           ) or die $sth->errstr;
105           my $number = $sth->fetchrow_arrayref->[0];
106         %>
107           <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>" ALIGN="right"><%= $number %></TD>
108         <% } %>
109       </TR>
110 <% } %>
111
112 <%
113   $statement =~ s/AND refnum = \?//;
114   $sth = dbh->prepare($statement)
115     or die dbh->errstr;
116 %>
117       <TR>
118         <TD BGCOLOR="#dddddd" ALIGN="center" COLSPAN=3><B>Total</B></TD>
119         <% for my $period ( keys %after ) {
120           $sth->execute( $today-$after{$period},
121                          $today+$before{$period},
122           ) or die $sth->errstr;
123           my $number = $sth->fetchrow_arrayref->[0];
124         %>
125           <TD BGCOLOR="#dddddd" ALIGN="right"><B><%= $number %><B></TD>
126         <% } %>
127       </TR>
128     </TABLE>
129   </BODY>
130 </HTML>