fix A/R report
[freeside.git] / httemplate / search / quotation.html
1 <& elements/search.html,
2                  'title'              => emt('Quotation Search Results'),
3                  'html_init'          => $html_init,
4                  'menubar'            => $menubar,
5                  'name'               => 'quotations',
6                  'query'              => $sql_query,
7                  'count_query'        => $count_query,
8                  'count_addl'         => $count_addl,
9                  'redirect'           => $link,
10                  'disableable'        => 1,
11                  'disabled_statuspos' => 1,
12                  'header'             => [ emt('Quotation #'),
13                                            emt('Setup'),
14                                            emt('Recurring'),
15                                            emt('Date'),
16                                            emt('Prospect'),
17                                            emt('Customer'),
18                                            emt('Confidence'),
19                                            emt('Close date'),
20                                          ],
21                  'fields'             => [
22                    'quotationnum',
23                    sub { $money_char. shift->total_setup },
24                    sub { $money_char. shift->total_recur },
25                    sub { time2str('%b %d %Y', shift->_date ) },
26                    sub { my $prospect_main = shift->prospect_main;
27                          $prospect_main ? $prospect_main->name : '';
28                        },
29                    sub { my $cust_main = shift->cust_main;
30                          $cust_main ? encode_entities($cust_main->name) : '';
31                        },
32                    sub { my $quotation = shift;
33                          length($quotation->confidence) ? $quotation->confidence . '%' : '';
34                        },
35                    sub { my $quotation = shift;
36                          $quotation->close_date ? time2str('%b %d %Y', $quotation->close_date) : '';
37                        },
38                    #\&FS::UI::Web::cust_fields,
39                  ],
40                  'sort_fields'        => [
41                    'quotationnum',
42                    '', #FS::quotation->total_setup_sql,
43                    '', #FS::quotation->total_recur_sql,
44                    '_date',
45                    '',
46                    '',
47                    'confidence',
48                    'close_date',
49                  ],
50                  'align' => 'rrrrllrr', #.FS::UI::Web::cust_aligns(),
51                  'links' => [
52                    $link,
53                    $link,
54                    $link,
55                    $link,
56                    $prospect_link,
57                    $cust_link,
58                    $link,
59                    $link,
60                    #( map { $_ ne 'Cust. Status' ? $cust_link : '' }
61                    #      FS::UI::Web::cust_header()
62                    #),
63                  ],
64 #                 'color' => [ 
65 #                              '',
66 #                              '',
67 #                              '',
68 #                              '',
69 #                              '',
70 #                              FS::UI::Web::cust_colors(),
71 #                            ],
72 #                 'style' => [ 
73 #                              '',
74 #                              '',
75 #                              '',
76 #                              '',
77 #                              '',
78 #                              FS::UI::Web::cust_styles(),
79 #                            ],
80 &>
81 <%init>
82
83 my $curuser = $FS::CurrentUser::CurrentUser;
84
85 die "access denied"
86   unless $curuser->access_right('List quotations');
87
88 local($FS::Record::qsearch_qualify_columns) = 1;
89
90 my $join_prospect_main = 'LEFT JOIN prospect_main USING ( prospectnum )';
91 my $join_cust_main = FS::UI::Web::join_cust_main('quotation');
92
93 #here is the agent virtualization
94 my $agentnums_sql = ' (    '. $curuser->agentnums_sql( table=>'prospect_main' ).
95                     '   OR '. $curuser->agentnums_sql( table=>'cust_main' ).
96                     ' )    ';
97
98 my( $count_query, $sql_query );
99 my $count_addl = '';
100 my %search;
101
102 #if ( $cgi->param('quotationnum') =~ /^\s*(FS-)?(\d+)\s*$/ ) {
103 #
104 #  my $where = "WHERE quotationnum = $2 AND $agentnums_sql";
105 #  
106 #  $count_query = "SELECT COUNT(*) FROM quotation $join_prospect_main $join_cust_main $where";
107 #
108 #  $sql_query = {
109 #    'table'     => 'quotation',
110 #    'addl_from' => "$join_prospect_main $join_cust_main",
111 #    'hashref'   => {},
112 #    'extra_sql' => $where,
113 #  };
114 #
115 #} else {
116
117   #some false laziness w/cust_bill::re_X
118   my $orderby = 'ORDER BY quotation._date';
119
120   if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
121     $search{'agentnum'} = $1;
122   }
123
124 #  if ( $cgi->param('refnum') =~ /^(\d+)$/ ) {
125 #    $search{'refnum'} = $1;
126 #  }
127
128   if ( $cgi->param('prospectnum') =~ /^(\d+)$/ ) {
129     $search{'prospectnum'} = $1;
130   }
131
132   if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
133     $search{'custnum'} = $1;
134   }
135
136   # begin/end/beginning/ending
137   my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, '');
138   $search{'_date'} = [ $beginning, $ending ]
139     unless $beginning == 0 && $ending == 4294967295;
140
141   if ( $cgi->param('quotationnum_min') =~ /^\s*(\d+)\s*$/ ) {
142     $search{'quotationnum_min'} = $1;
143   }
144   if ( $cgi->param('quotationnum_max') =~ /^\s*(\d+)\s*$/ ) {
145     $search{'quotationnum_max'} = $1;
146   }
147
148   #amounts
149   $search{$_} = [ FS::UI::Web::parse_lt_gt($cgi, $_) ]
150     foreach qw( total_setup total_recur );
151
152 #  my($query) = $cgi->keywords;
153 #  if ( $query =~ /^(OPEN(\d*)_)?(invnum|date|custnum)$/ ) {
154 #    $search{'open'} = 1 if $1;
155 #    ($search{'days'}, my $field) = ($2, $3);
156 #    $field = "_date" if $field eq 'date';
157 #    $orderby = "ORDER BY cust_bill.$field";
158 #  }
159
160 #  if ( $cgi->param('newest_percust') ) {
161 #    $search{'newest_percust'} = 1;
162 #    $count_query = "SELECT COUNT(DISTINCT cust_bill.custnum), 'N/A', 'N/A'";
163 #  }
164
165   my $extra_sql = ' WHERE '. FS::quotation->search_sql_where( \%search );
166
167   unless ( $count_query ) {
168     $count_query = 'SELECT COUNT(*)';
169   }
170   $count_query .=  " FROM quotation $join_prospect_main $join_cust_main $extra_sql";
171
172   $sql_query = {
173     'table'     => 'quotation',
174     'addl_from' => "$join_prospect_main $join_cust_main",
175     'hashref'   => {},
176     'select'    => join(', ',
177                      'quotation.*',
178                      #( map "cust_main.$_", qw(custnum last first company) ),
179                      'prospect_main.prospectnum as prospect_main_prospectnum',
180                      'cust_main.custnum as cust_main_custnum',
181                      #FS::UI::Web::cust_sql_fields(),
182                    ),
183     'extra_sql' => $extra_sql,
184     'order_by'  => $orderby,
185   };
186
187 #}
188
189 my $link  = [ "${p}view/quotation.html?", 'quotationnum', ];
190 my $prospect_link = sub {
191   my $quotation = shift;
192   $quotation->prospect_main_prospectnum
193     ? [ "${p}view/prospect_main.html?", 'prospectnum' ]
194     : '';
195 };
196
197 my $cust_link = sub {
198   my $quotation = shift;
199   $quotation->cust_main_custnum
200     ? [ "${p}view/cust_main.cgi?", 'custnum' ]
201     : '';
202 };
203
204 my $conf = new FS::Conf;
205 my $money_char = $conf->config('money_char') || '$';
206
207 my $html_init = join("\n", map {
208  ( my $action = $_ ) =~ s/_$//;
209  include('/elements/progress-init.html',
210            $_.'form',
211            [ keys %search ],
212            "../misc/${_}invoices.cgi",
213            { 'message' => "Invoices re-${action}ed" }, #would be nice to show the number of them, but...
214            $_, #key
215         ),
216  qq!<FORM NAME="${_}form">!,
217  ( map { my $f = $_;
218          my @values = ref($search{$f}) ? @{ $search{$f} } : $search{$f};
219          map qq!<INPUT TYPE="hidden" NAME="$f" VALUE="$_">!, @values;
220        }
221        keys %search
222  ),
223  qq!</FORM>!
224 } qw( print_ email_ fax_ ftp_ spool_ ) ). 
225
226 '<SCRIPT TYPE="text/javascript">
227
228 function confirm_print_process() {
229   if ( ! confirm('.js_mt("Are you sure you want to reprint these invoices?").') ) {
230     return;
231   }
232   print_process();
233 }
234 function confirm_email_process() {
235   if ( ! confirm('.js_mt("Are you sure you want to re-email these invoices?").') ) {
236     return;
237   }
238   email_process();
239 }
240 function confirm_fax_process() {
241   if ( ! confirm('.js_mt("Are you sure you want to re-fax these invoices?").') ) {
242     return;
243   }
244   fax_process();
245 }
246 function confirm_ftp_process() {
247   if ( ! confirm('.js_mt("Are you sure you want to re-FTP these invoices?").') ) {
248     return;
249   }
250   ftp_process();
251 }
252 function confirm_spool_process() {
253   if ( ! confirm('.js_mt("Are you sure you want to re-spool these invoices?").') ) {
254     return;
255   }
256   spool_process();
257 }
258
259 </SCRIPT>';
260
261 my $menubar = [];
262
263 #if ( $curuser->access_right('Resend quotations') ) {
264 #
265 #  push @$menubar, emt('Print these invoices') =>
266 #                    "javascript:confirm_print_process()",
267 #                  emt('Email these invoices') =>
268 #                    "javascript:confirm_email_process()";
269 #
270 #  push @$menubar, emt('Fax these invoices') =>
271 #                    "javascript:confirm_fax_process()"
272 #    if $conf->exists('hylafax');
273 #
274 #  push @$menubar, emt('FTP these invoices') =>
275 #                    "javascript:confirm_ftp_process()"
276 #    if $conf->exists('cust_bill-ftpformat');
277 #
278 #  push @$menubar, emt('Spool these invoices') =>
279 #                    "javascript:confirm_spool_process()"
280 #    if $conf->exists('cust_bill-spoolformat');
281 #
282 #}
283
284 </%init>