include discounts in gross sales reports, #25943
[freeside.git] / httemplate / search / cust_bill.html
1 <& elements/search.html,
2   'title'       => emt('Invoice Search Results'),
3   'html_init'   => $html_init,
4   'menubar'     => $menubar,
5   'name'        => 'invoices',
6   'query'       => $sql_query,
7   'count_query' => $count_query,
8   'count_addl'  => $count_addl,
9   'redirect'    => $link,
10   'header'      => [ emt('Invoice #'),
11                      emt('Gross Amount'),
12                      emt('Discount'),
13                      emt('Credits'),
14                      emt('Net Amount'),
15                      emt('Balance'),
16                      emt('Date'),
17                      FS::UI::Web::cust_header(),
18                    ],
19   'fields'      => [
20     'display_invnum',
21     'gross',
22     'discounted',
23     'credited',
24     'net',
25     'owed',
26     sub { time2str('%b %d %Y', shift->_date ) },
27     \&FS::UI::Web::cust_fields,
28   ],
29   'sort_fields' => [
30     'COALESCE( agent_invid, invnum )',
31     'gross',
32     'discounted',
33     'credited',
34     'net',
35     'owed',
36     '_date',
37   ],
38   'format' => [
39     '',
40     $money_char.'%.2f',
41     $money_char.'%.2f',
42     $money_char.'%.2f',
43     $money_char.'%.2f',
44     $money_char.'%.2f',
45     '',
46   ],
47   'align' => 'rrrrrrl'.FS::UI::Web::cust_aligns(),
48   'links' => [
49     $link,
50     $link,
51     $link,
52     $link,
53     $link,
54     $link,
55     $link,
56     ( map { $_ ne 'Cust. Status' ? $clink : '' }
57           FS::UI::Web::cust_header()
58     ),
59   ],
60   'color' => [ 
61                '',
62                '',
63                '',
64                '',
65                '',
66                '',
67                '',
68                FS::UI::Web::cust_colors(),
69              ],
70   'style' => [ 
71                '',
72                '',
73                '',
74                '',
75                '',
76                '',
77                '',
78                FS::UI::Web::cust_styles(),
79              ],
80 &>
81 <%init>
82
83 die "access denied"
84   unless $FS::CurrentUser::CurrentUser->access_right('List invoices');
85
86 my( $count_query, $sql_query );
87 my $count_addl = '';
88 my %search = ();
89
90 if ( $cgi->param('invnum') =~ /^\s*(FS-)?(\d+)\s*$/ ) {
91
92   my $join_cust_main = FS::UI::Web::join_cust_main('cust_bill');
93   #here is the agent virtualization
94   my $agentnums_sql = $FS::CurrentUser::CurrentUser->agentnums_sql;
95
96   my $invnum_or_invid = "( invnum = $2 OR agent_invid = $2 )";
97   my $where = "WHERE $invnum_or_invid AND $agentnums_sql";
98   
99   $count_query = "SELECT COUNT(*) FROM cust_bill $join_cust_main $where";
100
101   $sql_query = {
102     'table'     => 'cust_bill',
103     'addl_from' => $join_cust_main,
104     'hashref'   => {},
105     'extra_sql' => $where,
106   };
107
108   if ( FS::Record->scalar_sql($count_query) == 0 ) {
109
110     # check for a voided invoice
111     $count_query =~ s/cust_bill/cust_bill_void/g;
112     if ( FS::Record->scalar_sql($count_query) > 0 ) {
113       # Redirect to the void search.
114       my $url = $cgi->self_url;
115       $url =~ s(search/cust_bill)(search/cust_bill_void);
116       $m->clear_buffer;
117       $m->print($cgi->redirect($url));
118       $m->abort;
119     }
120   }
121
122 } else {
123
124   #deal with some old-style URLs
125   my($query) = $cgi->keywords;
126   if ( $query =~ /^(OPEN(\d*)_)?(invnum|date|custnum)$/ ) {
127     $search{'open'} = 1 if $1;
128     ($search{'days'}, my $field) = ($2, $3);
129     $field = "_date" if $field eq 'date';
130     $search{'order_by'} = "cust_bill.$field";
131   }
132
133   #scalars
134   for (qw( agentnum custnum cust_status refnum invnum_min invnum_max
135            open net newest_percust 
136
137       )) 
138   {
139     $search{$_} = $cgi->param($_) if length($cgi->param($_));
140   }
141
142   #arrays
143   for my $param (qw( cust_classnum )) {
144   $search{$param} = [ $cgi->param($param) ]
145     if grep { $_ eq $param } $cgi->param;
146   }
147
148   #amounts (range)
149   $search{$_} = [ FS::UI::Web::parse_lt_gt($cgi, $_) ]
150     foreach qw( charged owed );
151
152   my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, '');
153   $search{'_date'} = [ $beginning, $ending ]
154     unless $beginning == 0 && $ending == 4294967295;
155
156   # promised date
157   my $start_of_day = timelocal(0, 0, 0, (localtime(time))[3,4,5]);
158   foreach ( $cgi->param('promised_date') ) {
159     # only if at least one box is checked
160     $search{promised_date} ||= [ $start_of_day, $start_of_day, 0 ];
161     if ($_ eq 'past') {
162       # accept everything before today
163       $search{promised_date}[0] = 0;
164     }
165     elsif ( $_ eq 'future' ) {
166       # accept everything after today
167       $search{promised_date}[1] = 4294967295;
168     }
169     elsif ( $_ eq 'null' ) {
170       # accept nulls
171       $search{promised_date}[2] = 1;
172     }
173   }
174
175   $sql_query   = FS::cust_bill->search(\%search);
176   $count_query = delete( $sql_query->{'count_query'} );
177   $count_addl  = delete( $sql_query->{'count_addl'}  );
178
179 }
180
181 my $link  = [ "${p}view/cust_bill.cgi?", 'invnum', ];
182 my $clink = sub {
183   my $cust_bill = shift;
184   $cust_bill->cust_main_custnum
185     ? [ "${p}view/cust_main.cgi?", 'custnum' ]
186     : '';
187 };
188
189 my $conf = new FS::Conf;
190 my $money_char = $conf->config('money_char') || '$';
191
192 my $html_init = join("\n", map {
193  ( my $action = $_ ) =~ s/_$//;
194  include('/elements/progress-init.html',
195            $_.'form',
196            [ keys %search ],
197            "../misc/${_}invoices.cgi",
198            { 'message' => "Invoices re-${action}ed" }, #would be nice to show the number of them, but...
199            $_, #key
200         ),
201  qq!<FORM NAME="${_}form">!,
202  ( map { my $f = $_;
203          my @values = ref($search{$f}) ? @{ $search{$f} } : $search{$f};
204          map qq!<INPUT TYPE="hidden" NAME="$f" VALUE="$_">!, @values;
205        }
206        keys %search
207  ),
208  qq!</FORM>!
209 } qw( print_ email_ fax_ ftp_ spool_ ) ). 
210
211 '<SCRIPT TYPE="text/javascript">
212
213 function confirm_print_process() {
214   if ( ! confirm('.js_mt("Are you sure you want to reprint these invoices?").') ) {
215     return;
216   }
217   print_process();
218 }
219 function confirm_email_process() {
220   if ( ! confirm('.js_mt("Are you sure you want to re-email these invoices?").') ) {
221     return;
222   }
223   email_process();
224 }
225 function confirm_fax_process() {
226   if ( ! confirm('.js_mt("Are you sure you want to re-fax these invoices?").') ) {
227     return;
228   }
229   fax_process();
230 }
231 function confirm_ftp_process() {
232   if ( ! confirm('.js_mt("Are you sure you want to re-FTP these invoices?").') ) {
233     return;
234   }
235   ftp_process();
236 }
237 function confirm_spool_process() {
238   if ( ! confirm('.js_mt("Are you sure you want to re-spool these invoices?").') ) {
239     return;
240   }
241   spool_process();
242 }
243
244 </SCRIPT>';
245
246 my $menubar = [];
247
248 if ( $FS::CurrentUser::CurrentUser->access_right('Resend invoices') ) {
249
250   push @$menubar, emt('Print these invoices') =>
251                     "javascript:confirm_print_process()",
252                   emt('Email these invoices') =>
253                     "javascript:confirm_email_process()";
254
255   push @$menubar, emt('Fax these invoices') =>
256                     "javascript:confirm_fax_process()"
257     if $conf->exists('hylafax');
258
259   push @$menubar, emt('FTP these invoices') =>
260                     "javascript:confirm_ftp_process()"
261     if $conf->exists('cust_bill-ftpformat');
262
263   push @$menubar, emt('Spool these invoices') =>
264                     "javascript:confirm_spool_process()"
265     if $conf->exists('cust_bill-spoolformat');
266
267 }
268
269 </%init>