add less than and greater than amounts to credit and payment searches
[freeside.git] / FS / FS / UI / Web.pm
1 package FS::UI::Web;
2
3 use vars qw($DEBUG);
4 use FS::Conf;
5 use FS::Record qw(dbdef);
6
7 #use vars qw(@ISA);
8 #use FS::UI
9 #@ISA = qw( FS::UI );
10
11 $DEBUG = 0;
12
13 use Date::Parse;
14 sub parse_beginning_ending {
15   my($cgi) = @_;
16
17   my $beginning = 0;
18   if ( $cgi->param('begin') =~ /^(\d+)$/ ) {
19     $beginning = $1;
20   } elsif ( $cgi->param('beginning') =~ /^([ 0-9\-\/]{1,64})$/ ) {
21     $beginning = str2time($1) || 0;
22   }
23
24   my $ending = 4294967295; #2^32-1
25   if ( $cgi->param('end') =~ /^(\d+)$/ ) {
26     $ending = $1 - 1;
27   } elsif ( $cgi->param('ending') =~ /^([ 0-9\-\/]{1,64})$/ ) {
28     #probably need an option to turn off the + 86399
29     $ending = str2time($1) + 86399;
30   }
31
32   ( $beginning, $ending );
33 }
34
35 sub parse_lt_gt {
36   my($cgi, $field) = @_;
37
38   my @search = ();
39
40   my %op = ( 
41     'lt' => '<',
42     'gt' => '>',
43   );
44
45   foreach my $op (keys %op) {
46
47     warn "checking for ${field}_$op field\n"
48       if $DEBUG;
49
50     if ( $cgi->param($field."_$op") =~ /^\s*\$?\s*([\d\,\s]+(\.\d\d)?)\s*$/ ) {
51
52       my $num = $1;
53       $num =~ s/[\,\s]+//g;
54       my $search = "$field $op{$op} $num";
55       push @search, $search;
56
57       warn "found ${field}_$op field; adding search element $search\n"
58         if $DEBUG;
59     }
60
61   }
62
63   @search;
64
65 }
66
67 ###
68 # cust_main report subroutines
69 ###
70
71
72 =item cust_header [ CUST_FIELDS_VALUE ]
73
74 Returns an array of customer information headers according to the supplied
75 customer fields value, or if no value is supplied, the B<cust-fields>
76 configuration value.
77
78 =cut
79
80 use vars qw( @cust_fields );
81
82 sub cust_header {
83
84   warn "FS::svc_Common::cust_header called"
85     if $DEBUG;
86
87   my %header2method = (
88     'Customer'                 => 'name',
89     'Cust#'                    => 'custnum',
90     'Name'                     => 'contact',
91     'Company'                  => 'company',
92     '(bill) Customer'          => 'name',
93     '(service) Customer'       => 'ship_name',
94     '(bill) Name'              => 'contact',
95     '(service) Name'           => 'ship_contact',
96     '(bill) Company'           => 'company',
97     '(service) Company'        => 'ship_company',
98     'Address 1'                => 'address1',
99     'Address 2'                => 'address2',
100     'City'                     => 'city',
101     'State'                    => 'state',
102     'Zip'                      => 'zip',
103     'Country'                  => 'country_full',
104     'Day phone'                => 'daytime', # XXX should use msgcat, but how?
105     'Night phone'              => 'night',   # XXX should use msgcat, but how?
106     'Invoicing email(s)'       => 'invoicing_list_emailonly',
107   );
108
109   my $cust_fields;
110   my @cust_header;
111   if ( @_ && $_[0] ) {
112
113     warn "  using supplied cust-fields override".
114           " (ignoring cust-fields config file)"
115       if $DEBUG;
116     $cust_fields = shift;
117
118   } else {
119
120     my $conf = new FS::Conf;
121     if (    $conf->exists('cust-fields')
122          && $conf->config('cust-fields') =~ /^([\w \|\#\(\)]+):?/
123        )
124     {
125       warn "  found cust-fields configuration value"
126         if $DEBUG;
127       $cust_fields = $1;
128     } else { 
129       warn "  no cust-fields configuration value found; using default 'Customer'"
130         if $DEBUG;
131       $cust_fields = 'Customer';
132     }
133   
134   }
135
136   @cust_header = split(/ \| /, $cust_fields);
137   @cust_fields = map { $header2method{$_} } @cust_header;
138
139   #my $svc_x = shift;
140   @cust_header;
141 }
142
143 =item cust_sql_fields [ CUST_FIELDS_VALUE ]
144
145 Returns a list of fields for the SELECT portion of an SQL query.
146
147 As with L<the cust_header subroutine|/cust_header>, the fields returned are
148 defined by the supplied customer fields setting, or if no customer fields
149 setting is supplied, the <B>cust-fields</B> configuration value. 
150
151 =cut
152
153 sub cust_sql_fields {
154
155   my @fields = qw( last first company );
156   push @fields, map "ship_$_", @fields;
157   push @fields, 'country';
158
159   cust_header(@_);
160   #inefficientish, but tiny lists and only run once per page
161   push @fields,
162     grep { my $field = $_; grep { $_ eq $field } @cust_fields }
163          qw( address1 address2 city state zip daytime night );
164
165   map "cust_main.$_", @fields;
166 }
167
168 =item cust_fields SVC_OBJECT [ CUST_FIELDS_VALUE ]
169
170 Given a svc_ object that contains fields from cust_main (say, from a
171 JOINed search.  See httemplate/search/svc_* for examples), returns an array
172 of customer information, or "(unlinked)" if this service is not linked to a
173 customer.
174
175 As with L<the cust_header subroutine|/cust_header>, the fields returned are
176 defined by the supplied customer fields setting, or if no customer fields
177 setting is supplied, the <B>cust-fields</B> configuration value. 
178
179 =cut
180
181 sub cust_fields {
182   my $svc_x = shift;
183   warn "FS::svc_Common::cust_fields called for $svc_x ".
184        "(cust_fields: @cust_fields)"
185     if $DEBUG > 1;
186
187   #cust_header(@_) unless @cust_fields; #now need to cache to keep cust_fields
188   #                                     #override incase we were passed as a sub
189
190   my $seen_unlinked = 0;
191   map { 
192     if ( $svc_x->custnum ) {
193       warn "  $svc_x -> $_"
194         if $DEBUG > 1;
195       $svc_x->$_(@_);
196     } else {
197       warn "  ($svc_x unlinked)"
198         if $DEBUG > 1;
199       $seen_unlinked++ ? '' : '(unlinked)';
200     }
201   } @cust_fields;
202 }
203
204 ###
205 # begin JSRPC code...
206 ###
207
208 package FS::UI::Web::JSRPC;
209
210 use strict;
211 use vars qw($DEBUG);
212 use Carp;
213 use Storable qw(nfreeze);
214 use MIME::Base64;
215 use JSON;
216 use FS::UID;
217 use FS::Record qw(qsearchs);
218 use FS::queue;
219
220 $DEBUG = 0;
221
222 sub new {
223         my $class = shift;
224         my $self  = {
225                 env => {},
226                 job => shift,
227                 cgi => shift,
228         };
229
230         bless $self, $class;
231
232         croak "CGI object required as second argument" unless $self->{'cgi'};
233
234         return $self;
235 }
236
237 sub process {
238
239   my $self = shift;
240
241   my $cgi = $self->{'cgi'};
242
243   # XXX this should parse JSON foo and build a proper data structure
244   my @args = $cgi->param('arg');
245
246   #work around konqueror bug!
247   @args = map { s/\x00$//; $_; } @args;
248
249   my $sub = $cgi->param('sub'); #????
250
251   warn "FS::UI::Web::JSRPC::process:\n".
252        "  cgi=$cgi\n".
253        "  sub=$sub\n".
254        "  args=".join(', ',@args)."\n"
255     if $DEBUG;
256
257   if ( $sub eq 'start_job' ) {
258
259     $self->start_job(@args);
260
261   } elsif ( $sub eq 'job_status' ) {
262
263     $self->job_status(@args);
264
265   } else {
266
267     die "unknown sub $sub";
268
269   }
270
271 }
272
273 sub start_job {
274   my $self = shift;
275
276   warn "FS::UI::Web::start_job: ". join(', ', @_) if $DEBUG;
277 #  my %param = @_;
278   my %param = ();
279   while ( @_ ) {
280     my( $field, $value ) = splice(@_, 0, 2);
281     unless ( exists( $param{$field} ) ) {
282       $param{$field} = $value;
283     } elsif ( ! ref($param{$field}) ) {
284       $param{$field} = [ $param{$field}, $value ];
285     } else {
286       push @{$param{$field}}, $value;
287     }
288   }
289   warn "FS::UI::Web::start_job\n".
290        join('', map {
291                       if ( ref($param{$_}) ) {
292                         "  $_ => [ ". join(', ', @{$param{$_}}). " ]\n";
293                       } else {
294                         "  $_ => $param{$_}\n";
295                       }
296                     } keys %param )
297     if $DEBUG;
298
299   #first get the CGI params shipped off to a job ASAP so an id can be returned
300   #to the caller
301   
302   my $job = new FS::queue { 'job' => $self->{'job'} };
303   
304   #too slow to insert all the cgi params as individual args..,?
305   #my $error = $queue->insert('_JOB', $cgi->Vars);
306   
307   #warn 'froze string of size '. length(nfreeze(\%param)). " for job args\n"
308   #  if $DEBUG;
309
310   my $error = $job->insert( '_JOB', encode_base64(nfreeze(\%param)) );
311
312   if ( $error ) {
313
314     warn "job not inserted: $error\n"
315       if $DEBUG;
316
317     $error;  #this doesn't seem to be handled well,
318              # will trigger "illegal jobnum" below?
319              # (should never be an error inserting the job, though, only thing
320              #  would be Pg f%*kage)
321   } else {
322
323     warn "job inserted successfully with jobnum ". $job->jobnum. "\n"
324       if $DEBUG;
325
326     $job->jobnum;
327   }
328   
329 }
330
331 sub job_status {
332   my( $self, $jobnum ) = @_; #$url ???
333
334   sleep 1; # XXX could use something better...
335
336   my $job;
337   if ( $jobnum =~ /^(\d+)$/ ) {
338     $job = qsearchs('queue', { 'jobnum' => $jobnum } );
339   } else {
340     die "FS::UI::Web::job_status: illegal jobnum $jobnum\n";
341   }
342
343   my @return;
344   if ( $job && $job->status ne 'failed' ) {
345     @return = ( 'progress', $job->statustext );
346   } elsif ( !$job ) { #handle job gone case : job successful
347                       # so close popup, redirect parent window...
348     @return = ( 'complete' );
349   } else {
350     @return = ( 'error', $job ? $job->statustext : $jobnum );
351   }
352
353   objToJson(\@return);
354
355 }
356
357 1;
358