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