4 use vars qw($DEBUG $me);
6 use FS::Record qw(dbdef);
13 $me = '[FS::UID::Web]';
20 sub parse_beginning_ending {
24 if ( $cgi->param('begin') =~ /^(\d+)$/ ) {
26 } elsif ( $cgi->param('beginning') =~ /^([ 0-9\-\/]{1,64})$/ ) {
27 $beginning = str2time($1) || 0;
30 my $ending = 4294967295; #2^32-1
31 if ( $cgi->param('end') =~ /^(\d+)$/ ) {
33 } elsif ( $cgi->param('ending') =~ /^([ 0-9\-\/]{1,64})$/ ) {
34 #probably need an option to turn off the + 86399
35 $ending = str2time($1) + 86399;
38 ( $beginning, $ending );
43 Returns a service URL, first checking to see if there is a service-specific
44 page to link to, otherwise to a generic service handling page. Options are
45 passed as a list of name-value pairs, and include:
49 =item * m - Mason request object ($m)
51 =item * action - The action for which to construct "edit", "view", or "search"
53 =item ** part_svc - Service definition (see L<FS::part_svc>)
55 =item ** svcdb - Service table
57 =item *** query - Query string
59 =item *** svc - FS::cust_svc or FS::svc_* object
61 =item ahref - Optional flag, if set true returns <A HREF="$url"> instead of just the URL.
67 ** part_svc OR svcdb is required
69 *** query OR svc is required
76 # 'm' => $m, #mason request object
77 # 'action' => 'edit', #or 'view'
79 # 'part_svc' => $part_svc, #usual
81 # 'svcdb' => 'svc_table',
83 # 'query' => #optional query string
84 # # (pass a blank string if you want a "raw" URL to add your
87 # 'svc' => $svc_x, #or $cust_svc, it just needs a svcnum
92 # 'ahref' => 1, # if set true, returns <A HREF="$url">
94 use FS::CGI qw(rooturl);
98 #? return '' unless ref($opt{part_svc});
100 my $svcdb = $opt{svcdb} || $opt{part_svc}->svcdb;
101 my $query = exists($opt{query}) ? $opt{query} : $opt{svc}->svcnum;
103 warn "$me [svc_url] checking for /$opt{action}/$svcdb.cgi component"
105 if ( $opt{m}->interp->comp_exists("/$opt{action}/$svcdb.cgi") ) {
106 $url = "$svcdb.cgi?";
109 my $generic = $opt{action} eq 'search' ? 'cust_svc' : 'svc_Common';
111 $url = "$generic.html?svcdb=$svcdb;";
112 $url .= 'svcnum=' if $query =~ /^\d+(;|$)/ or $query eq '';
115 my $return = rooturl(). "$opt{action}/$url$query";
117 $return = qq!<A HREF="$return">! if $opt{ahref};
123 my($m, $part_svc, $cust_svc) = @_ or return '';
124 svc_X_link( $part_svc->svc, @_ );
128 my($m, $part_svc, $cust_svc) = @_ or return '';
129 svc_X_link( ($cust_svc->label)[1], @_ );
133 my ($x, $m, $part_svc, $cust_svc) = @_ or return '';
138 'part_svc' => $part_svc,
146 my($cgi, $field) = @_;
155 foreach my $op (keys %op) {
157 warn "checking for ${field}_$op field\n"
160 if ( $cgi->param($field."_$op") =~ /^\s*\$?\s*([\d\,\s]+(\.\d\d)?)\s*$/ ) {
163 $num =~ s/[\,\s]+//g;
164 my $search = "$field $op{$op} $num";
165 push @search, $search;
167 warn "found ${field}_$op field; adding search element $search\n"
177 sub bytecount_unexact {
181 return(sprintf("%.2f Kbytes", $bc/1000))
183 return(sprintf("%.2f Mbytes", $bc/1000000))
184 if ($bc < 1000000000);
185 return(sprintf("%.2f Gbytes", $bc/1000000000));
189 # cust_main report subroutines
193 =item cust_header [ CUST_FIELDS_VALUE ]
195 Returns an array of customer information headers according to the supplied
196 customer fields value, or if no value is supplied, the B<cust-fields>
201 use vars qw( @cust_fields @cust_colors @cust_styles @cust_aligns );
205 warn "FS::UI:Web::cust_header called"
208 my %header2method = (
209 'Customer' => 'name',
210 'Cust. Status' => 'ucfirst_cust_status',
211 'Cust#' => 'custnum',
213 'Company' => 'company',
214 '(bill) Customer' => 'name',
215 '(service) Customer' => 'ship_name',
216 '(bill) Name' => 'contact',
217 '(service) Name' => 'ship_contact',
218 '(bill) Company' => 'company',
219 '(service) Company' => 'ship_company',
220 'Address 1' => 'address1',
221 'Address 2' => 'address2',
225 'Country' => 'country_full',
226 'Day phone' => 'daytime', # XXX should use msgcat, but how?
227 'Night phone' => 'night', # XXX should use msgcat, but how?
228 'Invoicing email(s)' => 'invoicing_list_emailonly_scalar',
231 my %header2colormethod = (
232 'Cust. Status' => 'cust_statuscolor',
235 'Cust. Status' => 'b',
238 'Cust. Status' => 'c',
245 warn " using supplied cust-fields override".
246 " (ignoring cust-fields config file)"
248 $cust_fields = shift;
252 my $conf = new FS::Conf;
253 if ( $conf->exists('cust-fields')
254 && $conf->config('cust-fields') =~ /^([\w\. \|\#\(\)]+):?/
257 warn " found cust-fields configuration value"
261 warn " no cust-fields configuration value found; using default 'Cust. Status | Customer'"
263 $cust_fields = 'Cust. Status | Customer';
268 @cust_header = split(/ \| /, $cust_fields);
269 @cust_fields = map { $header2method{$_} } @cust_header;
270 @cust_colors = map { exists $header2colormethod{$_}
271 ? $header2colormethod{$_}
275 @cust_styles = map { exists $header2style{$_} ? $header2style{$_} : '' }
277 @cust_aligns = map { exists $header2align{$_} ? $header2align{$_} : 'l' }
284 =item cust_sql_fields [ CUST_FIELDS_VALUE ]
286 Returns a list of fields for the SELECT portion of an SQL query.
288 As with L<the cust_header subroutine|/cust_header>, the fields returned are
289 defined by the supplied customer fields setting, or if no customer fields
290 setting is supplied, the <B>cust-fields</B> configuration value.
294 sub cust_sql_fields {
296 my @fields = qw( last first company );
297 push @fields, map "ship_$_", @fields;
298 push @fields, 'country';
301 #inefficientish, but tiny lists and only run once per page
303 grep { my $field = $_; grep { $_ eq $field } @cust_fields }
304 qw( address1 address2 city state zip daytime night );
306 map "cust_main.$_", @fields;
309 =item cust_fields OBJECT [ CUST_FIELDS_VALUE ]
311 Given an object that contains fields from cust_main (say, from a
312 JOINed search. See httemplate/search/svc_* for examples), returns an array
313 of customer information, or "(unlinked)" if this service is not linked to a
316 As with L<the cust_header subroutine|/cust_header>, the fields returned are
317 defined by the supplied customer fields setting, or if no customer fields
318 setting is supplied, the <B>cust-fields</B> configuration value.
324 warn "FS::UI::Web::cust_fields called for $svc_x ".
325 "(cust_fields: @cust_fields)"
328 #cust_header(@_) unless @cust_fields; #now need to cache to keep cust_fields
329 # #override incase we were passed as a sub
331 my $seen_unlinked = 0;
333 if ( $svc_x->custnum ) {
338 warn " ($svc_x unlinked)"
340 $seen_unlinked++ ? '' : '(unlinked)';
347 Returns an array of subroutine references (or empty strings) for returning
348 customer information colors.
350 As with L<the cust_header subroutine|/cust_header>, the fields returned are
351 defined by the supplied customer fields setting, or if no customer fields
352 setting is supplied, the <B>cust-fields</B> configuration value.
360 sub { shift->$method(@_) };
369 Returns an array of customer information styles.
371 As with L<the cust_header subroutine|/cust_header>, the fields returned are
372 defined by the supplied customer fields setting, or if no customer fields
373 setting is supplied, the <B>cust-fields</B> configuration value.
389 Returns an array or scalar (depending on context) of customer information
392 As with L<the cust_header subroutine|/cust_header>, the fields returned are
393 defined by the supplied customer fields setting, or if no customer fields
394 setting is supplied, the <B>cust-fields</B> configuration value.
402 join('', @cust_aligns);
407 # begin JSRPC code...
410 package FS::UI::Web::JSRPC;
415 use Storable qw(nfreeze);
419 use FS::Record qw(qsearchs);
434 croak "CGI object required as second argument" unless $self->{'cgi'};
443 my $cgi = $self->{'cgi'};
445 # XXX this should parse JSON foo and build a proper data structure
446 my @args = $cgi->param('arg');
448 #work around konqueror bug!
449 @args = map { s/\x00$//; $_; } @args;
451 my $sub = $cgi->param('sub'); #????
453 warn "FS::UI::Web::JSRPC::process:\n".
456 " args=".join(', ',@args)."\n"
459 if ( $sub eq 'start_job' ) {
461 $self->start_job(@args);
463 } elsif ( $sub eq 'job_status' ) {
465 $self->job_status(@args);
469 die "unknown sub $sub";
478 warn "FS::UI::Web::start_job: ". join(', ', @_) if $DEBUG;
482 my( $field, $value ) = splice(@_, 0, 2);
483 unless ( exists( $param{$field} ) ) {
484 $param{$field} = $value;
485 } elsif ( ! ref($param{$field}) ) {
486 $param{$field} = [ $param{$field}, $value ];
488 push @{$param{$field}}, $value;
491 warn "FS::UI::Web::start_job\n".
493 if ( ref($param{$_}) ) {
494 " $_ => [ ". join(', ', @{$param{$_}}). " ]\n";
496 " $_ => $param{$_}\n";
501 #first get the CGI params shipped off to a job ASAP so an id can be returned
504 my $job = new FS::queue { 'job' => $self->{'job'} };
506 #too slow to insert all the cgi params as individual args..,?
507 #my $error = $queue->insert('_JOB', $cgi->Vars);
509 #warn 'froze string of size '. length(nfreeze(\%param)). " for job args\n"
512 my $error = $job->insert( '_JOB', encode_base64(nfreeze(\%param)) );
516 warn "job not inserted: $error\n"
519 $error; #this doesn't seem to be handled well,
520 # will trigger "illegal jobnum" below?
521 # (should never be an error inserting the job, though, only thing
522 # would be Pg f%*kage)
525 warn "job inserted successfully with jobnum ". $job->jobnum. "\n"
534 my( $self, $jobnum ) = @_; #$url ???
536 sleep 1; # XXX could use something better...
539 if ( $jobnum =~ /^(\d+)$/ ) {
540 $job = qsearchs('queue', { 'jobnum' => $jobnum } );
542 die "FS::UI::Web::job_status: illegal jobnum $jobnum\n";
546 if ( $job && $job->status ne 'failed' ) {
547 @return = ( 'progress', $job->statustext );
548 } elsif ( !$job ) { #handle job gone case : job successful
549 # so close popup, redirect parent window...
550 @return = ( 'complete' );
552 @return = ( 'error', $job ? $job->statustext : $jobnum );