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 );
205 warn "FS::svc_Common::cust_header called"
208 my %header2method = (
209 'Customer' => 'name',
210 'Cust#' => 'custnum',
212 'Company' => 'company',
213 '(bill) Customer' => 'name',
214 '(service) Customer' => 'ship_name',
215 '(bill) Name' => 'contact',
216 '(service) Name' => 'ship_contact',
217 '(bill) Company' => 'company',
218 '(service) Company' => 'ship_company',
219 'Address 1' => 'address1',
220 'Address 2' => 'address2',
224 'Country' => 'country_full',
225 'Day phone' => 'daytime', # XXX should use msgcat, but how?
226 'Night phone' => 'night', # XXX should use msgcat, but how?
227 'Invoicing email(s)' => 'invoicing_list_emailonly',
234 warn " using supplied cust-fields override".
235 " (ignoring cust-fields config file)"
237 $cust_fields = shift;
241 my $conf = new FS::Conf;
242 if ( $conf->exists('cust-fields')
243 && $conf->config('cust-fields') =~ /^([\w \|\#\(\)]+):?/
246 warn " found cust-fields configuration value"
250 warn " no cust-fields configuration value found; using default 'Customer'"
252 $cust_fields = 'Customer';
257 @cust_header = split(/ \| /, $cust_fields);
258 @cust_fields = map { $header2method{$_} } @cust_header;
264 =item cust_sql_fields [ CUST_FIELDS_VALUE ]
266 Returns a list of fields for the SELECT portion of an SQL query.
268 As with L<the cust_header subroutine|/cust_header>, the fields returned are
269 defined by the supplied customer fields setting, or if no customer fields
270 setting is supplied, the <B>cust-fields</B> configuration value.
274 sub cust_sql_fields {
276 my @fields = qw( last first company );
277 push @fields, map "ship_$_", @fields;
278 push @fields, 'country';
281 #inefficientish, but tiny lists and only run once per page
283 grep { my $field = $_; grep { $_ eq $field } @cust_fields }
284 qw( address1 address2 city state zip daytime night );
286 map "cust_main.$_", @fields;
289 =item cust_fields SVC_OBJECT [ CUST_FIELDS_VALUE ]
291 Given a svc_ object that contains fields from cust_main (say, from a
292 JOINed search. See httemplate/search/svc_* for examples), returns an array
293 of customer information, or "(unlinked)" if this service is not linked to a
296 As with L<the cust_header subroutine|/cust_header>, the fields returned are
297 defined by the supplied customer fields setting, or if no customer fields
298 setting is supplied, the <B>cust-fields</B> configuration value.
304 warn "FS::svc_Common::cust_fields called for $svc_x ".
305 "(cust_fields: @cust_fields)"
308 #cust_header(@_) unless @cust_fields; #now need to cache to keep cust_fields
309 # #override incase we were passed as a sub
311 my $seen_unlinked = 0;
313 if ( $svc_x->custnum ) {
318 warn " ($svc_x unlinked)"
320 $seen_unlinked++ ? '' : '(unlinked)';
326 # begin JSRPC code...
329 package FS::UI::Web::JSRPC;
334 use Storable qw(nfreeze);
338 use FS::Record qw(qsearchs);
353 croak "CGI object required as second argument" unless $self->{'cgi'};
362 my $cgi = $self->{'cgi'};
364 # XXX this should parse JSON foo and build a proper data structure
365 my @args = $cgi->param('arg');
367 #work around konqueror bug!
368 @args = map { s/\x00$//; $_; } @args;
370 my $sub = $cgi->param('sub'); #????
372 warn "FS::UI::Web::JSRPC::process:\n".
375 " args=".join(', ',@args)."\n"
378 if ( $sub eq 'start_job' ) {
380 $self->start_job(@args);
382 } elsif ( $sub eq 'job_status' ) {
384 $self->job_status(@args);
388 die "unknown sub $sub";
397 warn "FS::UI::Web::start_job: ". join(', ', @_) if $DEBUG;
401 my( $field, $value ) = splice(@_, 0, 2);
402 unless ( exists( $param{$field} ) ) {
403 $param{$field} = $value;
404 } elsif ( ! ref($param{$field}) ) {
405 $param{$field} = [ $param{$field}, $value ];
407 push @{$param{$field}}, $value;
410 warn "FS::UI::Web::start_job\n".
412 if ( ref($param{$_}) ) {
413 " $_ => [ ". join(', ', @{$param{$_}}). " ]\n";
415 " $_ => $param{$_}\n";
420 #first get the CGI params shipped off to a job ASAP so an id can be returned
423 my $job = new FS::queue { 'job' => $self->{'job'} };
425 #too slow to insert all the cgi params as individual args..,?
426 #my $error = $queue->insert('_JOB', $cgi->Vars);
428 #warn 'froze string of size '. length(nfreeze(\%param)). " for job args\n"
431 my $error = $job->insert( '_JOB', encode_base64(nfreeze(\%param)) );
435 warn "job not inserted: $error\n"
438 $error; #this doesn't seem to be handled well,
439 # will trigger "illegal jobnum" below?
440 # (should never be an error inserting the job, though, only thing
441 # would be Pg f%*kage)
444 warn "job inserted successfully with jobnum ". $job->jobnum. "\n"
453 my( $self, $jobnum ) = @_; #$url ???
455 sleep 1; # XXX could use something better...
458 if ( $jobnum =~ /^(\d+)$/ ) {
459 $job = qsearchs('queue', { 'jobnum' => $jobnum } );
461 die "FS::UI::Web::job_status: illegal jobnum $jobnum\n";
465 if ( $job && $job->status ne 'failed' ) {
466 @return = ( 'progress', $job->statustext );
467 } elsif ( !$job ) { #handle job gone case : job successful
468 # so close popup, redirect parent window...
469 @return = ( 'complete' );
471 @return = ( 'error', $job ? $job->statustext : $jobnum );