Optimize "Customer has a referring customer" condition, RT#74452
[freeside.git] / httemplate / browse / part_referral.html
1 <& /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 <% $cgi->param('showdisabled')
10     ? do { $cgi->param('showdisabled', 0);
11            '( <a href="'. $cgi->self_url. '">hide disabled advertising sources</a> )'; }
12     : do { $cgi->param('showdisabled', 1);
13            '( <a href="'. $cgi->self_url. '">show disabled advertising sources</a> )'; }
14 %>
15
16 % ###
17 % # Header
18 % ###
19
20 <& /elements/table-grid.html &>
21 % my $bgcolor1 = '#eeeeee';
22 %   my $bgcolor2 = '#ffffff';
23 %   my $bgcolor = '';
24
25 <TR>
26   <TH CLASS="grid" BGCOLOR="#cccccc" COLSPAN=2 ROWSPAN=2>Advertising source</TH>
27
28 %       if ( ! $cgi->param('showdisabled') ) { 
29           <TH CLASS="grid" BGCOLOR="#cccccc" ALIGN="center" ROWSPAN=2></TH>
30 %       }
31
32 % if ( $show_agentnums ) { 
33     <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2>Agent</TH>
34 % } 
35
36   <TH CLASS="grid" BGCOLOR="#cccccc" COLSPAN=<% scalar(keys %after) %>>Prospects, Customers and Packages</TH>
37 </TR>
38 % for my $period ( keys %after ) { 
39
40   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1><% $period %></FONT></TH>
41 % } 
42
43 </TR>
44
45 % ###
46 % # Rows
47 % ###
48
49 %foreach my $part_referral ( FS::part_referral->all_part_referral(1,!scalar($cgi->param('showdisabled'))) ) {
50 %
51 %  if ( $bgcolor eq $bgcolor1 ) {
52 %    $bgcolor = $bgcolor2;
53 %  } else {
54 %    $bgcolor = $bgcolor1;
55 %  }
56 %
57 %  $a = 0;
58
59       <TR>
60
61         <TD CLASS="grid" BGCOLOR="<% $bgcolor %>">
62 % if ( $part_referral->agentnum || $curuser->access_right('Edit global advertising sources') ) { 
63 %            $a++;
64 %          
65
66             <A HREF="<% $p %>edit/part_referral.html?<% $part_referral->refnum %>">
67 % } 
68
69           <% $part_referral->refnum %><% $a ? '</A>' : '' %></TD>
70         <TD CLASS="grid" BGCOLOR="<% $bgcolor %>">
71 % if ( $a ) { 
72
73             <A HREF="<% $p %>edit/part_referral.html?<% $part_referral->refnum %>">
74 % } 
75
76           <% $part_referral->referral %><% $a ? '</A>' : '' %></TD>
77
78 %       if ( ! $cgi->param('showdisabled') ) { 
79           <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ALIGN="center">
80             <% $part_referral->disabled
81                  ? '<FONT COLOR="#FF0000"><B>DISABLED</B></FONT>'
82                  : '<FONT COLOR="#00CC00"><B>Active</B></FONT>'
83             %>
84           </TD>
85 %       }
86
87 % if ( $show_agentnums ) { 
88
89           <TD CLASS="grid" BGCOLOR="<% $bgcolor %>"><% $part_referral->agentnum ? $part_referral->agent->agent : '(global)' %></TD>
90 % } 
91 % for my $period ( keys %after ) {
92 %          my @param = ( $part_referral->refnum,
93 %                        $today-$after{$period},
94 %                        $today+$before{$period},
95 %                      );
96 %          $prospect_sth->execute(@param) or die $prospect_sth->errstr;
97 %          my $num_prospect = $prospect_sth->fetchrow_arrayref->[0];
98 %          $cust_sth->execute(@param) or die $cust_sth->errstr;
99 %          my $num_cust = $cust_sth->fetchrow_arrayref->[0];
100 %          $pkg_sth->execute(@param) or die $pkg_sth->errstr;
101 %          my $num_pkg = $pkg_sth->fetchrow_arrayref->[0];
102
103           <TD CLASS="inv" BGCOLOR="<% $bgcolor %>" ALIGN="right">
104             <TABLE CLASS="inv" CELLSPACING=0 CELLPADDING=0>
105               <TR>
106                 <TD ALIGN="right"><B><% $num_prospect %></B></TD>
107                 <TD ALIGN="left">&nbsp;prospects&nbsp;</TD>
108               </TR>
109               <TR>
110                 <TD ALIGN="right"><B><% $num_cust %></B></TD>
111                 <TD ALIGN="left">&nbsp;customers&nbsp;</TD>
112               </TR>
113               <TR>
114                 <TD ALIGN="right"><B><% $num_pkg %></B></TD>
115                 <TD ALIGN="left">&nbsp;packages&nbsp;</TD>
116               </TR>
117             </TABLE>
118           </TD>
119 % } 
120
121       </TR>
122 % } 
123
124
125 % ###
126 % # Bottom Totals
127 % ###
128
129 %  $prospect_statement =~ s/AND refnum = \?//;
130 %  $prospect_sth = dbh->prepare($prospect_statement)
131 %    or die dbh->errstr;
132 %
133 %  $cust_statement =~ s/AND refnum = \?//;
134 %  $cust_sth = dbh->prepare($cust_statement)
135 %    or die dbh->errstr;
136 %
137 %  $pkg_statement =~ s/AND h_pkg_referral\.refnum = \?//;
138 %  $pkg_sth = dbh->prepare($pkg_statement)
139 %    or die dbh->errstr;
140
141       <TR>
142         <TD BGCOLOR="#dddddd" ALIGN="center" COLSPAN=<% 2 + $show_agentnums + ! $cgi->param('showdisabled') %><B>Total</B></TD>
143 % for my $period ( keys %after ) {
144 %          my @param = ( $today-$after{$period},
145 %                        $today+$before{$period},
146 %                      );
147 %          $prospect_sth->execute(@param) or die $prospect_sth->errstr;
148 %          my $num_prospect = $prospect_sth->fetchrow_arrayref->[0];
149 %          $cust_sth->execute( @param ) or die $cust_sth->errstr;
150 %          my $num_cust = $cust_sth->fetchrow_arrayref->[0];
151 %          $pkg_sth->execute(@param) or die $pkg_sth->errstr;
152 %          my $num_pkg = $pkg_sth->fetchrow_arrayref->[0];
153
154           <TD CLASS="inv" BGCOLOR="#dddddd" ALIGN="right">
155             <TABLE CLASS="inv" CELLSPACING=0 CELLPADDING=0>
156               <TR>
157                 <TD ALIGN="right"><B><% $num_prospect %></B></TD>
158                 <TD ALIGN="left">&nbsp;prospects&nbsp;</TD>
159               </TR>
160               <TR>
161                 <TD ALIGN="right"><B><% $num_cust %></B></TD>
162                 <TD ALIGN="left">&nbsp;customers&nbsp;</TD>
163               </TR>
164               <TR>
165                 <TD ALIGN="right"><B><% $num_pkg %></B></TD>
166                 <TD ALIGN="left">&nbsp;packages&nbsp;</TD>
167               </TR>
168             </TABLE>
169           </TD>
170
171 % } 
172
173       </TR>
174     </TABLE>
175
176 <& /elements/footer.html &>
177 <%init>
178
179 die "access denied"
180   unless $FS::CurrentUser::CurrentUser->access_right('Edit advertising sources')
181       || $FS::CurrentUser::CurrentUser->access_right('Edit global advertising sources');
182
183 my $today = timelocal(0, 0, 0, (localtime(time))[3..5] );
184
185 tie my %after, 'Tie::IxHash',
186   'Today'         =>        0,
187   'Yesterday'     =>    86400, # 60sec * 60min * 24hrs
188   'Past week'     =>   518400, # 60sec * 60min * 24hrs * 6days
189   'Past 30 days'  =>  2505600, # 60sec * 60min * 24hrs * 29days 
190   'Past 60 days'  =>  5097600, # 60sec * 60min * 24hrs * 59days 
191   'Past 90 days'  =>  7689600, # 60sec * 60min * 24hrs * 89days 
192   'Past 6 months' => 15724800, # 60sec * 60min * 24hrs * 182days 
193   'Past year'     => 31486000, # 60sec * 60min * 24hrs * 364days 
194   'Total'         => $today,
195 ;
196 my %before = (
197   'Today'         =>   86400, # 60sec * 60min * 24hrs
198   'Yesterday'     =>       0,
199   'Past week'     =>   86400, # 60sec * 60min * 24hrs
200   'Past 30 days'  =>   86400, # 60sec * 60min * 24hrs
201   'Past 60 days'  =>   86400, # 60sec * 60min * 24hrs
202   'Past 90 days'  =>   86400, # 60sec * 60min * 24hrs
203   'Past 6 months' =>   86400, # 60sec * 60min * 24hrs
204   'Past year'     =>   86400, # 60sec * 60min * 24hrs
205   'Total'         =>   86400, # 60sec * 60min * 24hrs
206 );
207
208 my $curuser = $FS::CurrentUser::CurrentUser;
209
210 my $show_agentnums = ( scalar($curuser->agentnums) > 1 );
211
212 my $prospect_statement = "SELECT COUNT(*) FROM h_prospect_main
213                            WHERE history_action = 'insert'
214                              AND refnum = ?
215                              AND history_date >= ?
216                              AND history_date < ?
217                              AND ". $curuser->agentnums_sql;
218 my $prospect_sth = dbh->prepare($prospect_statement)
219   or die dbh->errstr;
220
221 my $cust_statement = "SELECT COUNT(*) FROM h_cust_main
222                        WHERE history_action = 'insert'
223                          AND refnum = ?
224                          AND history_date >= ?
225                          AND history_date < ?
226                          AND ". $curuser->agentnums_sql;
227 my $cust_sth = dbh->prepare($cust_statement)
228   or die dbh->errstr;
229
230 my $pkg_statement = "SELECT COUNT(*) FROM h_pkg_referral
231                        LEFT JOIN cust_pkg  USING ( pkgnum )
232                        LEFT JOIN cust_main USING ( custnum )
233                        WHERE history_action = 'insert'
234                          AND h_pkg_referral.refnum = ?
235                          AND history_date >= ?
236                          AND history_date < ?
237                          AND ". $curuser->agentnums_sql;
238 my $pkg_sth = dbh->prepare($pkg_statement)
239   or die dbh->errstr;
240
241 </%init>