5 use FS::Record qw(dbdef);
14 sub parse_beginning_ending {
18 if ( $cgi->param('begin') =~ /^(\d+)$/ ) {
20 } elsif ( $cgi->param('beginning') =~ /^([ 0-9\-\/]{1,64})$/ ) {
21 $beginning = str2time($1) || 0;
24 my $ending = 4294967295; #2^32-1
25 if ( $cgi->param('end') =~ /^(\d+)$/ ) {
27 } elsif ( $cgi->param('ending') =~ /^([ 0-9\-\/]{1,64})$/ ) {
28 #probably need an option to turn off the + 86399
29 $ending = str2time($1) + 86399;
32 ( $beginning, $ending );
36 my($cgi, $field) = @_;
45 foreach my $op (keys %op) {
47 warn "checking for ${field}_$op field\n"
50 if ( $cgi->param($field."_$op") =~ /^\s*\$?\s*([\d\,\s]+(\.\d\d)?)\s*$/ ) {
54 my $search = "$field $op{$op} $num";
55 push @search, $search;
57 warn "found ${field}_$op field; adding search element $search\n"
67 sub bytecount_unexact {
71 return(sprintf("%.2f Kbytes", $bc/1000))
73 return(sprintf("%.2f Mbytes", $bc/1000000))
74 if ($bc < 1000000000);
75 return(sprintf("%.2f Gbytes", $bc/1000000000));
79 # cust_main report subroutines
83 =item cust_header [ CUST_FIELDS_VALUE ]
85 Returns an array of customer information headers according to the supplied
86 customer fields value, or if no value is supplied, the B<cust-fields>
91 use vars qw( @cust_fields );
95 warn "FS::svc_Common::cust_header called"
100 'Cust#' => 'custnum',
102 'Company' => 'company',
103 '(bill) Customer' => 'name',
104 '(service) Customer' => 'ship_name',
105 '(bill) Name' => 'contact',
106 '(service) Name' => 'ship_contact',
107 '(bill) Company' => 'company',
108 '(service) Company' => 'ship_company',
109 'Address 1' => 'address1',
110 'Address 2' => 'address2',
114 'Country' => 'country_full',
115 'Day phone' => 'daytime', # XXX should use msgcat, but how?
116 'Night phone' => 'night', # XXX should use msgcat, but how?
117 'Invoicing email(s)' => 'invoicing_list_emailonly',
124 warn " using supplied cust-fields override".
125 " (ignoring cust-fields config file)"
127 $cust_fields = shift;
131 my $conf = new FS::Conf;
132 if ( $conf->exists('cust-fields')
133 && $conf->config('cust-fields') =~ /^([\w \|\#\(\)]+):?/
136 warn " found cust-fields configuration value"
140 warn " no cust-fields configuration value found; using default 'Customer'"
142 $cust_fields = 'Customer';
147 @cust_header = split(/ \| /, $cust_fields);
148 @cust_fields = map { $header2method{$_} } @cust_header;
154 =item cust_sql_fields [ CUST_FIELDS_VALUE ]
156 Returns a list of fields for the SELECT portion of an SQL query.
158 As with L<the cust_header subroutine|/cust_header>, the fields returned are
159 defined by the supplied customer fields setting, or if no customer fields
160 setting is supplied, the <B>cust-fields</B> configuration value.
164 sub cust_sql_fields {
166 my @fields = qw( last first company );
167 push @fields, map "ship_$_", @fields;
168 push @fields, 'country';
171 #inefficientish, but tiny lists and only run once per page
173 grep { my $field = $_; grep { $_ eq $field } @cust_fields }
174 qw( address1 address2 city state zip daytime night );
176 map "cust_main.$_", @fields;
179 =item cust_fields SVC_OBJECT [ CUST_FIELDS_VALUE ]
181 Given a svc_ object that contains fields from cust_main (say, from a
182 JOINed search. See httemplate/search/svc_* for examples), returns an array
183 of customer information, or "(unlinked)" if this service is not linked to a
186 As with L<the cust_header subroutine|/cust_header>, the fields returned are
187 defined by the supplied customer fields setting, or if no customer fields
188 setting is supplied, the <B>cust-fields</B> configuration value.
194 warn "FS::svc_Common::cust_fields called for $svc_x ".
195 "(cust_fields: @cust_fields)"
198 #cust_header(@_) unless @cust_fields; #now need to cache to keep cust_fields
199 # #override incase we were passed as a sub
201 my $seen_unlinked = 0;
203 if ( $svc_x->custnum ) {
208 warn " ($svc_x unlinked)"
210 $seen_unlinked++ ? '' : '(unlinked)';
216 # begin JSRPC code...
219 package FS::UI::Web::JSRPC;
224 use Storable qw(nfreeze);
228 use FS::Record qw(qsearchs);
243 croak "CGI object required as second argument" unless $self->{'cgi'};
252 my $cgi = $self->{'cgi'};
254 # XXX this should parse JSON foo and build a proper data structure
255 my @args = $cgi->param('arg');
257 #work around konqueror bug!
258 @args = map { s/\x00$//; $_; } @args;
260 my $sub = $cgi->param('sub'); #????
262 warn "FS::UI::Web::JSRPC::process:\n".
265 " args=".join(', ',@args)."\n"
268 if ( $sub eq 'start_job' ) {
270 $self->start_job(@args);
272 } elsif ( $sub eq 'job_status' ) {
274 $self->job_status(@args);
278 die "unknown sub $sub";
287 warn "FS::UI::Web::start_job: ". join(', ', @_) if $DEBUG;
291 my( $field, $value ) = splice(@_, 0, 2);
292 unless ( exists( $param{$field} ) ) {
293 $param{$field} = $value;
294 } elsif ( ! ref($param{$field}) ) {
295 $param{$field} = [ $param{$field}, $value ];
297 push @{$param{$field}}, $value;
300 warn "FS::UI::Web::start_job\n".
302 if ( ref($param{$_}) ) {
303 " $_ => [ ". join(', ', @{$param{$_}}). " ]\n";
305 " $_ => $param{$_}\n";
310 #first get the CGI params shipped off to a job ASAP so an id can be returned
313 my $job = new FS::queue { 'job' => $self->{'job'} };
315 #too slow to insert all the cgi params as individual args..,?
316 #my $error = $queue->insert('_JOB', $cgi->Vars);
318 #warn 'froze string of size '. length(nfreeze(\%param)). " for job args\n"
321 my $error = $job->insert( '_JOB', encode_base64(nfreeze(\%param)) );
325 warn "job not inserted: $error\n"
328 $error; #this doesn't seem to be handled well,
329 # will trigger "illegal jobnum" below?
330 # (should never be an error inserting the job, though, only thing
331 # would be Pg f%*kage)
334 warn "job inserted successfully with jobnum ". $job->jobnum. "\n"
343 my( $self, $jobnum ) = @_; #$url ???
345 sleep 1; # XXX could use something better...
348 if ( $jobnum =~ /^(\d+)$/ ) {
349 $job = qsearchs('queue', { 'jobnum' => $jobnum } );
351 die "FS::UI::Web::job_status: illegal jobnum $jobnum\n";
355 if ( $job && $job->status ne 'failed' ) {
356 @return = ( 'progress', $job->statustext );
357 } elsif ( !$job ) { #handle job gone case : job successful
358 # so close popup, redirect parent window...
359 @return = ( 'complete' );
361 @return = ( 'error', $job ? $job->statustext : $jobnum );