4 use vars qw($DEBUG $me);
6 use FS::Record qw(dbdef);
13 $me = '[FS::UID::Web]';
20 sub parse_beginning_ending {
21 my($cgi, $prefix) = @_;
22 $prefix .= '_' if $prefix;
25 if ( $cgi->param($prefix.'begin') =~ /^(\d+)$/ ) {
27 } elsif ( $cgi->param($prefix.'beginning') =~ /^([ 0-9\-\/]{1,64})$/ ) {
28 $beginning = str2time($1) || 0;
31 my $ending = 4294967295; #2^32-1
32 if ( $cgi->param($prefix.'end') =~ /^(\d+)$/ ) {
34 } elsif ( $cgi->param($prefix.'ending') =~ /^([ 0-9\-\/]{1,64})$/ ) {
35 #probably need an option to turn off the + 86399
36 $ending = str2time($1) + 86399;
39 ( $beginning, $ending );
44 Returns a service URL, first checking to see if there is a service-specific
45 page to link to, otherwise to a generic service handling page. Options are
46 passed as a list of name-value pairs, and include:
50 =item * m - Mason request object ($m)
52 =item * action - The action for which to construct "edit", "view", or "search"
54 =item ** part_svc - Service definition (see L<FS::part_svc>)
56 =item ** svcdb - Service table
58 =item *** query - Query string
60 =item *** svc - FS::cust_svc or FS::svc_* object
62 =item ahref - Optional flag, if set true returns <A HREF="$url"> instead of just the URL.
68 ** part_svc OR svcdb is required
70 *** query OR svc is required
77 # 'm' => $m, #mason request object
78 # 'action' => 'edit', #or 'view'
80 # 'part_svc' => $part_svc, #usual
82 # 'svcdb' => 'svc_table',
84 # 'query' => #optional query string
85 # # (pass a blank string if you want a "raw" URL to add your
88 # 'svc' => $svc_x, #or $cust_svc, it just needs a svcnum
93 # 'ahref' => 1, # if set true, returns <A HREF="$url">
95 use FS::CGI qw(rooturl);
99 #? return '' unless ref($opt{part_svc});
101 my $svcdb = $opt{svcdb} || $opt{part_svc}->svcdb;
102 my $query = exists($opt{query}) ? $opt{query} : $opt{svc}->svcnum;
104 warn "$me [svc_url] checking for /$opt{action}/$svcdb.cgi component"
106 if ( $opt{m}->interp->comp_exists("/$opt{action}/$svcdb.cgi") ) {
107 $url = "$svcdb.cgi?";
110 my $generic = $opt{action} eq 'search' ? 'cust_svc' : 'svc_Common';
112 $url = "$generic.html?svcdb=$svcdb;";
113 $url .= 'svcnum=' if $query =~ /^\d+(;|$)/ or $query eq '';
116 my $return = rooturl(). "$opt{action}/$url$query";
118 $return = qq!<A HREF="$return">! if $opt{ahref};
124 my($m, $part_svc, $cust_svc) = @_ or return '';
125 svc_X_link( $part_svc->svc, @_ );
129 my($m, $part_svc, $cust_svc) = @_ or return '';
130 svc_X_link( ($cust_svc->label)[1], @_ );
134 my ($x, $m, $part_svc, $cust_svc) = @_ or return '';
139 'part_svc' => $part_svc,
147 my($cgi, $field) = @_;
156 foreach my $op (keys %op) {
158 warn "checking for ${field}_$op field\n"
161 if ( $cgi->param($field."_$op") =~ /^\s*\$?\s*([\d\,\s]+(\.\d\d)?)\s*$/ ) {
164 $num =~ s/[\,\s]+//g;
165 my $search = "$field $op{$op} $num";
166 push @search, $search;
168 warn "found ${field}_$op field; adding search element $search\n"
178 sub bytecount_unexact {
182 return(sprintf("%.2f Kbytes", $bc/1000))
184 return(sprintf("%.2f Mbytes", $bc/1000000))
185 if ($bc < 1000000000);
186 return(sprintf("%.2f Gbytes", $bc/1000000000));
190 # cust_main report subroutines
194 =item cust_header [ CUST_FIELDS_VALUE ]
196 Returns an array of customer information headers according to the supplied
197 customer fields value, or if no value is supplied, the B<cust-fields>
202 use vars qw( @cust_fields @cust_colors @cust_styles @cust_aligns );
206 warn "FS::UI:Web::cust_header called"
209 my %header2method = (
210 'Customer' => 'name',
211 'Cust. Status' => 'ucfirst_cust_status',
212 'Cust#' => 'custnum',
214 'Company' => 'company',
215 '(bill) Customer' => 'name',
216 '(service) Customer' => 'ship_name',
217 '(bill) Name' => 'contact',
218 '(service) Name' => 'ship_contact',
219 '(bill) Company' => 'company',
220 '(service) Company' => 'ship_company',
221 'Address 1' => 'address1',
222 'Address 2' => 'address2',
226 'Country' => 'country_full',
227 'Day phone' => 'daytime', # XXX should use msgcat, but how?
228 'Night phone' => 'night', # XXX should use msgcat, but how?
229 'Invoicing email(s)' => 'invoicing_list_emailonly_scalar',
232 my %header2colormethod = (
233 'Cust. Status' => 'cust_statuscolor',
236 'Cust. Status' => 'b',
239 'Cust. Status' => 'c',
246 warn " using supplied cust-fields override".
247 " (ignoring cust-fields config file)"
249 $cust_fields = shift;
253 my $conf = new FS::Conf;
254 if ( $conf->exists('cust-fields')
255 && $conf->config('cust-fields') =~ /^([\w\. \|\#\(\)]+):?/
258 warn " found cust-fields configuration value"
262 warn " no cust-fields configuration value found; using default 'Cust. Status | Customer'"
264 $cust_fields = 'Cust. Status | Customer';
269 @cust_header = split(/ \| /, $cust_fields);
270 @cust_fields = map { $header2method{$_} } @cust_header;
271 @cust_colors = map { exists $header2colormethod{$_}
272 ? $header2colormethod{$_}
276 @cust_styles = map { exists $header2style{$_} ? $header2style{$_} : '' }
278 @cust_aligns = map { exists $header2align{$_} ? $header2align{$_} : 'l' }
285 =item cust_sql_fields [ CUST_FIELDS_VALUE ]
287 Returns a list of fields for the SELECT portion of an SQL query.
289 As with L<the cust_header subroutine|/cust_header>, the fields returned are
290 defined by the supplied customer fields setting, or if no customer fields
291 setting is supplied, the <B>cust-fields</B> configuration value.
295 sub cust_sql_fields {
297 my @fields = qw( last first company );
298 push @fields, map "ship_$_", @fields;
299 push @fields, 'country';
302 #inefficientish, but tiny lists and only run once per page
304 grep { my $field = $_; grep { $_ eq $field } @cust_fields }
305 qw( address1 address2 city state zip daytime night );
307 map "cust_main.$_", @fields;
310 =item cust_fields OBJECT [ CUST_FIELDS_VALUE ]
312 Given an object that contains fields from cust_main (say, from a
313 JOINed search. See httemplate/search/svc_* for examples), returns an array
314 of customer information, or "(unlinked)" if this service is not linked to a
317 As with L<the cust_header subroutine|/cust_header>, the fields returned are
318 defined by the supplied customer fields setting, or if no customer fields
319 setting is supplied, the <B>cust-fields</B> configuration value.
325 warn "FS::UI::Web::cust_fields called for $svc_x ".
326 "(cust_fields: @cust_fields)"
329 #cust_header(@_) unless @cust_fields; #now need to cache to keep cust_fields
330 # #override incase we were passed as a sub
332 my $seen_unlinked = 0;
334 if ( $svc_x->custnum ) {
339 warn " ($svc_x unlinked)"
341 $seen_unlinked++ ? '' : '(unlinked)';
348 Returns an array of subroutine references (or empty strings) for returning
349 customer information colors.
351 As with L<the cust_header subroutine|/cust_header>, the fields returned are
352 defined by the supplied customer fields setting, or if no customer fields
353 setting is supplied, the <B>cust-fields</B> configuration value.
361 sub { shift->$method(@_) };
370 Returns an array of customer information styles.
372 As with L<the cust_header subroutine|/cust_header>, the fields returned are
373 defined by the supplied customer fields setting, or if no customer fields
374 setting is supplied, the <B>cust-fields</B> configuration value.
390 Returns an array or scalar (depending on context) of customer information
393 As with L<the cust_header subroutine|/cust_header>, the fields returned are
394 defined by the supplied customer fields setting, or if no customer fields
395 setting is supplied, the <B>cust-fields</B> configuration value.
403 join('', @cust_aligns);
408 # begin JSRPC code...
411 package FS::UI::Web::JSRPC;
416 use Storable qw(nfreeze);
420 use FS::Record qw(qsearchs);
435 croak "CGI object required as second argument" unless $self->{'cgi'};
444 my $cgi = $self->{'cgi'};
446 # XXX this should parse JSON foo and build a proper data structure
447 my @args = $cgi->param('arg');
449 #work around konqueror bug!
450 @args = map { s/\x00$//; $_; } @args;
452 my $sub = $cgi->param('sub'); #????
454 warn "FS::UI::Web::JSRPC::process:\n".
457 " args=".join(', ',@args)."\n"
460 if ( $sub eq 'start_job' ) {
462 $self->start_job(@args);
464 } elsif ( $sub eq 'job_status' ) {
466 $self->job_status(@args);
470 die "unknown sub $sub";
479 warn "FS::UI::Web::start_job: ". join(', ', @_) if $DEBUG;
483 my( $field, $value ) = splice(@_, 0, 2);
484 unless ( exists( $param{$field} ) ) {
485 $param{$field} = $value;
486 } elsif ( ! ref($param{$field}) ) {
487 $param{$field} = [ $param{$field}, $value ];
489 push @{$param{$field}}, $value;
492 warn "FS::UI::Web::start_job\n".
494 if ( ref($param{$_}) ) {
495 " $_ => [ ". join(', ', @{$param{$_}}). " ]\n";
497 " $_ => $param{$_}\n";
502 #first get the CGI params shipped off to a job ASAP so an id can be returned
505 my $job = new FS::queue { 'job' => $self->{'job'} };
507 #too slow to insert all the cgi params as individual args..,?
508 #my $error = $queue->insert('_JOB', $cgi->Vars);
510 #warn 'froze string of size '. length(nfreeze(\%param)). " for job args\n"
513 my $error = $job->insert( '_JOB', encode_base64(nfreeze(\%param)) );
517 warn "job not inserted: $error\n"
520 $error; #this doesn't seem to be handled well,
521 # will trigger "illegal jobnum" below?
522 # (should never be an error inserting the job, though, only thing
523 # would be Pg f%*kage)
526 warn "job inserted successfully with jobnum ". $job->jobnum. "\n"
535 my( $self, $jobnum ) = @_; #$url ???
537 sleep 1; # XXX could use something better...
540 if ( $jobnum =~ /^(\d+)$/ ) {
541 $job = qsearchs('queue', { 'jobnum' => $jobnum } );
543 die "FS::UI::Web::job_status: illegal jobnum $jobnum\n";
547 if ( $job && $job->status ne 'failed' ) {
548 @return = ( 'progress', $job->statustext );
549 } elsif ( !$job ) { #handle job gone case : job successful
550 # so close popup, redirect parent window...
551 @return = ( 'complete' );
553 @return = ( 'error', $job ? $job->statustext : $jobnum );