4 use vars qw($DEBUG $me);
6 use FS::Record qw(dbdef);
14 $me = '[FS::UID::Web]';
21 sub parse_beginning_ending {
22 my($cgi, $prefix) = @_;
23 $prefix .= '_' if $prefix;
26 if ( $cgi->param($prefix.'begin') =~ /^(\d+)$/ ) {
28 } elsif ( $cgi->param($prefix.'beginning') =~ /^([ 0-9\-\/]{1,64})$/ ) {
29 $beginning = str2time($1) || 0;
32 my $ending = 4294967295; #2^32-1
33 if ( $cgi->param($prefix.'end') =~ /^(\d+)$/ ) {
35 } elsif ( $cgi->param($prefix.'ending') =~ /^([ 0-9\-\/]{1,64})$/ ) {
36 #probably need an option to turn off the + 86399
37 $ending = str2time($1) + 86399;
40 ( $beginning, $ending );
45 Returns a service URL, first checking to see if there is a service-specific
46 page to link to, otherwise to a generic service handling page. Options are
47 passed as a list of name-value pairs, and include:
51 =item * m - Mason request object ($m)
53 =item * action - The action for which to construct "edit", "view", or "search"
55 =item ** part_svc - Service definition (see L<FS::part_svc>)
57 =item ** svcdb - Service table
59 =item *** query - Query string
61 =item *** svc - FS::cust_svc or FS::svc_* object
63 =item ahref - Optional flag, if set true returns <A HREF="$url"> instead of just the URL.
69 ** part_svc OR svcdb is required
71 *** query OR svc is required
78 # 'm' => $m, #mason request object
79 # 'action' => 'edit', #or 'view'
81 # 'part_svc' => $part_svc, #usual
83 # 'svcdb' => 'svc_table',
85 # 'query' => #optional query string
86 # # (pass a blank string if you want a "raw" URL to add your
89 # 'svc' => $svc_x, #or $cust_svc, it just needs a svcnum
94 # 'ahref' => 1, # if set true, returns <A HREF="$url">
96 use FS::CGI qw(rooturl);
100 #? return '' unless ref($opt{part_svc});
102 my $svcdb = $opt{svcdb} || $opt{part_svc}->svcdb;
103 my $query = exists($opt{query}) ? $opt{query} : $opt{svc}->svcnum;
105 warn "$me [svc_url] checking for /$opt{action}/$svcdb.cgi component"
107 if ( $opt{m}->interp->comp_exists("/$opt{action}/$svcdb.cgi") ) {
108 $url = "$svcdb.cgi?";
111 my $generic = $opt{action} eq 'search' ? 'cust_svc' : 'svc_Common';
113 $url = "$generic.html?svcdb=$svcdb;";
114 $url .= 'svcnum=' if $query =~ /^\d+(;|$)/ or $query eq '';
117 my $return = rooturl(). "$opt{action}/$url$query";
119 $return = qq!<A HREF="$return">! if $opt{ahref};
125 my($m, $part_svc, $cust_svc) = @_ or return '';
126 svc_X_link( $part_svc->svc, @_ );
130 my($m, $part_svc, $cust_svc) = @_ or return '';
131 svc_X_link( ($cust_svc->label)[1], @_ );
135 my ($x, $m, $part_svc, $cust_svc) = @_ or return '';
140 'part_svc' => $part_svc,
148 my($cgi, $field) = @_;
157 foreach my $op (keys %op) {
159 warn "checking for ${field}_$op field\n"
162 if ( $cgi->param($field."_$op") =~ /^\s*\$?\s*([\d\,\s]+(\.\d\d)?)\s*$/ ) {
165 $num =~ s/[\,\s]+//g;
166 my $search = "$field $op{$op} $num";
167 push @search, $search;
169 warn "found ${field}_$op field; adding search element $search\n"
179 sub bytecount_unexact {
183 return(sprintf("%.2f Kbytes", $bc/1000))
185 return(sprintf("%.2f Mbytes", $bc/1000000))
186 if ($bc < 1000000000);
187 return(sprintf("%.2f Gbytes", $bc/1000000000));
190 sub parse_bytecount {
192 return $bc if (($bc =~ tr/.//) > 1);
193 $bc =~ /^\s*([\d.]*)\s*([kKmMgGtT]?)[bB]?\s*$/ or return $bc;
195 return $bc unless length $base;
196 my $exponent = index ' kmgt', lc($2);
197 return $bc if ($exponent < 0 && $2);
198 $exponent = 0 if ($exponent < 0);
199 return $base * 1024 ** $exponent;
202 sub display_bytecount {
204 return $bc unless ($bc =~ /^(\d+)$/);
205 my $conf = new FS::Conf;
206 my $f = new Number::Format;
207 my $precision = $conf->exists('datavolume-significantdigits')
208 ? $conf->config('datavolume-significantdigits')
210 my $unit = $conf->exists('datavolume-forcemegabytes') ? 'M' : 'A';
212 return $f->format_bytes($bc, precision => $precision, unit => $unit);
216 # cust_main report subroutines
220 =item cust_header [ CUST_FIELDS_VALUE ]
222 Returns an array of customer information headers according to the supplied
223 customer fields value, or if no value is supplied, the B<cust-fields>
228 use vars qw( @cust_fields @cust_colors @cust_styles @cust_aligns );
232 warn "FS::UI:Web::cust_header called"
235 my %header2method = (
236 'Customer' => 'name',
237 'Cust. Status' => 'ucfirst_cust_status',
238 'Cust#' => 'custnum',
240 'Company' => 'company',
241 '(bill) Customer' => 'name',
242 '(service) Customer' => 'ship_name',
243 '(bill) Name' => 'contact',
244 '(service) Name' => 'ship_contact',
245 '(bill) Company' => 'company',
246 '(service) Company' => 'ship_company',
247 'Address 1' => 'address1',
248 'Address 2' => 'address2',
252 'Country' => 'country_full',
253 'Day phone' => 'daytime', # XXX should use msgcat, but how?
254 'Night phone' => 'night', # XXX should use msgcat, but how?
255 'Invoicing email(s)' => 'invoicing_list_emailonly_scalar',
258 my %header2colormethod = (
259 'Cust. Status' => 'cust_statuscolor',
262 'Cust. Status' => 'b',
265 'Cust. Status' => 'c',
272 warn " using supplied cust-fields override".
273 " (ignoring cust-fields config file)"
275 $cust_fields = shift;
279 my $conf = new FS::Conf;
280 if ( $conf->exists('cust-fields')
281 && $conf->config('cust-fields') =~ /^([\w\. \|\#\(\)]+):?/
284 warn " found cust-fields configuration value"
288 warn " no cust-fields configuration value found; using default 'Cust. Status | Customer'"
290 $cust_fields = 'Cust. Status | Customer';
295 @cust_header = split(/ \| /, $cust_fields);
296 @cust_fields = map { $header2method{$_} } @cust_header;
297 @cust_colors = map { exists $header2colormethod{$_}
298 ? $header2colormethod{$_}
302 @cust_styles = map { exists $header2style{$_} ? $header2style{$_} : '' }
304 @cust_aligns = map { exists $header2align{$_} ? $header2align{$_} : 'l' }
311 =item cust_sql_fields [ CUST_FIELDS_VALUE ]
313 Returns a list of fields for the SELECT portion of an SQL query.
315 As with L<the cust_header subroutine|/cust_header>, the fields returned are
316 defined by the supplied customer fields setting, or if no customer fields
317 setting is supplied, the <B>cust-fields</B> configuration value.
321 sub cust_sql_fields {
323 my @fields = qw( last first company );
324 push @fields, map "ship_$_", @fields;
325 push @fields, 'country';
328 #inefficientish, but tiny lists and only run once per page
330 grep { my $field = $_; grep { $_ eq $field } @cust_fields }
331 qw( address1 address2 city state zip daytime night );
333 map "cust_main.$_", @fields;
336 =item cust_fields OBJECT [ CUST_FIELDS_VALUE ]
338 Given an object that contains fields from cust_main (say, from a
339 JOINed search. See httemplate/search/svc_* for examples), returns an array
340 of customer information, or "(unlinked)" if this service is not linked to a
343 As with L<the cust_header subroutine|/cust_header>, the fields returned are
344 defined by the supplied customer fields setting, or if no customer fields
345 setting is supplied, the <B>cust-fields</B> configuration value.
351 warn "FS::UI::Web::cust_fields called for $svc_x ".
352 "(cust_fields: @cust_fields)"
355 #cust_header(@_) unless @cust_fields; #now need to cache to keep cust_fields
356 # #override incase we were passed as a sub
358 my $seen_unlinked = 0;
360 if ( $svc_x->custnum ) {
365 warn " ($svc_x unlinked)"
367 $seen_unlinked++ ? '' : '(unlinked)';
374 Returns an array of subroutine references (or empty strings) for returning
375 customer information colors.
377 As with L<the cust_header subroutine|/cust_header>, the fields returned are
378 defined by the supplied customer fields setting, or if no customer fields
379 setting is supplied, the <B>cust-fields</B> configuration value.
387 sub { shift->$method(@_) };
396 Returns an array of customer information styles.
398 As with L<the cust_header subroutine|/cust_header>, the fields returned are
399 defined by the supplied customer fields setting, or if no customer fields
400 setting is supplied, the <B>cust-fields</B> configuration value.
416 Returns an array or scalar (depending on context) of customer information
419 As with L<the cust_header subroutine|/cust_header>, the fields returned are
420 defined by the supplied customer fields setting, or if no customer fields
421 setting is supplied, the <B>cust-fields</B> configuration value.
429 join('', @cust_aligns);
434 # begin JSRPC code...
437 package FS::UI::Web::JSRPC;
442 use Storable qw(nfreeze);
446 use FS::Record qw(qsearchs);
461 croak "CGI object required as second argument" unless $self->{'cgi'};
470 my $cgi = $self->{'cgi'};
472 # XXX this should parse JSON foo and build a proper data structure
473 my @args = $cgi->param('arg');
475 #work around konqueror bug!
476 @args = map { s/\x00$//; $_; } @args;
478 my $sub = $cgi->param('sub'); #????
480 warn "FS::UI::Web::JSRPC::process:\n".
483 " args=".join(', ',@args)."\n"
486 if ( $sub eq 'start_job' ) {
488 $self->start_job(@args);
490 } elsif ( $sub eq 'job_status' ) {
492 $self->job_status(@args);
496 die "unknown sub $sub";
505 warn "FS::UI::Web::start_job: ". join(', ', @_) if $DEBUG;
509 my( $field, $value ) = splice(@_, 0, 2);
510 unless ( exists( $param{$field} ) ) {
511 $param{$field} = $value;
512 } elsif ( ! ref($param{$field}) ) {
513 $param{$field} = [ $param{$field}, $value ];
515 push @{$param{$field}}, $value;
518 warn "FS::UI::Web::start_job\n".
520 if ( ref($param{$_}) ) {
521 " $_ => [ ". join(', ', @{$param{$_}}). " ]\n";
523 " $_ => $param{$_}\n";
528 #first get the CGI params shipped off to a job ASAP so an id can be returned
531 my $job = new FS::queue { 'job' => $self->{'job'} };
533 #too slow to insert all the cgi params as individual args..,?
534 #my $error = $queue->insert('_JOB', $cgi->Vars);
536 #warn 'froze string of size '. length(nfreeze(\%param)). " for job args\n"
539 my $error = $job->insert( '_JOB', encode_base64(nfreeze(\%param)) );
543 warn "job not inserted: $error\n"
546 $error; #this doesn't seem to be handled well,
547 # will trigger "illegal jobnum" below?
548 # (should never be an error inserting the job, though, only thing
549 # would be Pg f%*kage)
552 warn "job inserted successfully with jobnum ". $job->jobnum. "\n"
561 my( $self, $jobnum ) = @_; #$url ???
563 sleep 1; # XXX could use something better...
566 if ( $jobnum =~ /^(\d+)$/ ) {
567 $job = qsearchs('queue', { 'jobnum' => $jobnum } );
569 die "FS::UI::Web::job_status: illegal jobnum $jobnum\n";
573 if ( $job && $job->status ne 'failed' ) {
574 @return = ( 'progress', $job->statustext );
575 } elsif ( !$job ) { #handle job gone case : job successful
576 # so close popup, redirect parent window...
577 @return = ( 'complete' );
579 @return = ( 'error', $job ? $job->statustext : $jobnum );