4 use vars qw($DEBUG @ISA @EXPORT_OK $me);
7 use FS::Record qw(dbdef);
12 @ISA = qw( Exporter );
14 @EXPORT_OK = qw( svc_url );
17 $me = '[FS::UID::Web]';
24 sub parse_beginning_ending {
25 my($cgi, $prefix) = @_;
26 $prefix .= '_' if $prefix;
29 if ( $cgi->param($prefix.'begin') =~ /^(\d+)$/ ) {
31 } elsif ( $cgi->param($prefix.'beginning') =~ /^([ 0-9\-\/]{1,64})$/ ) {
32 $beginning = str2time($1) || 0;
35 my $ending = 4294967295; #2^32-1
36 if ( $cgi->param($prefix.'end') =~ /^(\d+)$/ ) {
38 } elsif ( $cgi->param($prefix.'ending') =~ /^([ 0-9\-\/]{1,64})$/ ) {
39 #probably need an option to turn off the + 86399
40 $ending = str2time($1) + 86399;
43 ( $beginning, $ending );
48 Returns a service URL, first checking to see if there is a service-specific
49 page to link to, otherwise to a generic service handling page. Options are
50 passed as a list of name-value pairs, and include:
54 =item * m - Mason request object ($m)
56 =item * action - The action for which to construct "edit", "view", or "search"
58 =item ** part_svc - Service definition (see L<FS::part_svc>)
60 =item ** svcdb - Service table
62 =item *** query - Query string
64 =item *** svc - FS::cust_svc or FS::svc_* object
66 =item ahref - Optional flag, if set true returns <A HREF="$url"> instead of just the URL.
72 ** part_svc OR svcdb is required
74 *** query OR svc is required
81 # 'm' => $m, #mason request object
82 # 'action' => 'edit', #or 'view'
84 # 'part_svc' => $part_svc, #usual
86 # 'svcdb' => 'svc_table',
88 # 'query' => #optional query string
89 # # (pass a blank string if you want a "raw" URL to add your
92 # 'svc' => $svc_x, #or $cust_svc, it just needs a svcnum
97 # 'ahref' => 1, # if set true, returns <A HREF="$url">
99 use FS::CGI qw(rooturl);
103 #? return '' unless ref($opt{part_svc});
105 my $svcdb = $opt{svcdb} || $opt{part_svc}->svcdb;
106 my $query = exists($opt{query}) ? $opt{query} : $opt{svc}->svcnum;
108 warn "$me [svc_url] checking for /$opt{action}/$svcdb.cgi component"
110 if ( $opt{m}->interp->comp_exists("/$opt{action}/$svcdb.cgi") ) {
111 $url = "$svcdb.cgi?";
114 my $generic = $opt{action} eq 'search' ? 'cust_svc' : 'svc_Common';
116 $url = "$generic.html?svcdb=$svcdb;";
117 $url .= 'svcnum=' if $query =~ /^\d+(;|$)/ or $query eq '';
120 import FS::CGI 'rooturl'; #WTF! why is this necessary
121 my $return = rooturl(). "$opt{action}/$url$query";
123 $return = qq!<A HREF="$return">! if $opt{ahref};
129 my($m, $part_svc, $cust_svc) = @_ or return '';
130 svc_X_link( $part_svc->svc, @_ );
134 my($m, $part_svc, $cust_svc) = @_ or return '';
135 svc_X_link( ($cust_svc->label)[1], @_ );
139 my ($x, $m, $part_svc, $cust_svc) = @_ or return '';
144 'part_svc' => $part_svc,
152 my($cgi, $field) = @_;
161 foreach my $op (keys %op) {
163 warn "checking for ${field}_$op field\n"
166 if ( $cgi->param($field."_$op") =~ /^\s*\$?\s*([\d\,\s]+(\.\d\d)?)\s*$/ ) {
169 $num =~ s/[\,\s]+//g;
170 my $search = "$field $op{$op} $num";
171 push @search, $search;
173 warn "found ${field}_$op field; adding search element $search\n"
184 # cust_main report subroutines
188 =item cust_header [ CUST_FIELDS_VALUE ]
190 Returns an array of customer information headers according to the supplied
191 customer fields value, or if no value is supplied, the B<cust-fields>
196 use vars qw( @cust_fields @cust_colors @cust_styles @cust_aligns );
200 warn "FS::UI:Web::cust_header called"
203 my %header2method = (
204 'Customer' => 'name',
205 'Cust. Status' => 'ucfirst_cust_status',
206 'Cust#' => 'custnum',
208 'Company' => 'company',
209 '(bill) Customer' => 'name',
210 '(service) Customer' => 'ship_name',
211 '(bill) Name' => 'contact',
212 '(service) Name' => 'ship_contact',
213 '(bill) Company' => 'company',
214 '(service) Company' => 'ship_company',
215 'Address 1' => 'address1',
216 'Address 2' => 'address2',
220 'Country' => 'country_full',
221 'Day phone' => 'daytime', # XXX should use msgcat, but how?
222 'Night phone' => 'night', # XXX should use msgcat, but how?
223 'Fax number' => 'fax',
224 'Invoicing email(s)' => 'invoicing_list_emailonly_scalar',
225 'Payment Type' => 'payby',
228 my %header2colormethod = (
229 'Cust. Status' => 'cust_statuscolor',
232 'Cust. Status' => 'b',
235 'Cust. Status' => 'c',
242 warn " using supplied cust-fields override".
243 " (ignoring cust-fields config file)"
245 $cust_fields = shift;
249 my $conf = new FS::Conf;
250 if ( $conf->exists('cust-fields')
251 && $conf->config('cust-fields') =~ /^([\w\. \|\#\(\)]+):?/
254 warn " found cust-fields configuration value"
258 warn " no cust-fields configuration value found; using default 'Cust. Status | Customer'"
260 $cust_fields = 'Cust. Status | Customer';
265 @cust_header = split(/ \| /, $cust_fields);
266 @cust_fields = map { $header2method{$_} } @cust_header;
267 @cust_colors = map { exists $header2colormethod{$_}
268 ? $header2colormethod{$_}
272 @cust_styles = map { exists $header2style{$_} ? $header2style{$_} : '' }
274 @cust_aligns = map { exists $header2align{$_} ? $header2align{$_} : 'l' }
281 =item cust_sql_fields [ CUST_FIELDS_VALUE ]
283 Returns a list of fields for the SELECT portion of an SQL query.
285 As with L<the cust_header subroutine|/cust_header>, the fields returned are
286 defined by the supplied customer fields setting, or if no customer fields
287 setting is supplied, the <B>cust-fields</B> configuration value.
291 sub cust_sql_fields {
293 my @fields = qw( last first company );
294 push @fields, map "ship_$_", @fields;
295 push @fields, 'country';
298 #inefficientish, but tiny lists and only run once per page
300 grep { my $field = $_; grep { $_ eq $field } @cust_fields }
301 qw( address1 address2 city state zip daytime night );
303 map "cust_main.$_", @fields;
306 =item cust_fields OBJECT [ CUST_FIELDS_VALUE ]
308 Given an object that contains fields from cust_main (say, from a
309 JOINed search. See httemplate/search/svc_* for examples), returns an array
310 of customer information, or "(unlinked)" if this service is not linked to a
313 As with L<the cust_header subroutine|/cust_header>, the fields returned are
314 defined by the supplied customer fields setting, or if no customer fields
315 setting is supplied, the <B>cust-fields</B> configuration value.
321 warn "FS::UI::Web::cust_fields called for $svc_x ".
322 "(cust_fields: @cust_fields)"
325 #cust_header(@_) unless @cust_fields; #now need to cache to keep cust_fields
326 # #override incase we were passed as a sub
328 my $seen_unlinked = 0;
330 if ( $svc_x->custnum ) {
335 warn " ($svc_x unlinked)"
337 $seen_unlinked++ ? '' : '(unlinked)';
344 Returns an array of subroutine references (or empty strings) for returning
345 customer information colors.
347 As with L<the cust_header subroutine|/cust_header>, the fields returned are
348 defined by the supplied customer fields setting, or if no customer fields
349 setting is supplied, the <B>cust-fields</B> configuration value.
357 sub { shift->$method(@_) };
366 Returns an array of customer information styles.
368 As with L<the cust_header subroutine|/cust_header>, the fields returned are
369 defined by the supplied customer fields setting, or if no customer fields
370 setting is supplied, the <B>cust-fields</B> configuration value.
386 Returns an array or scalar (depending on context) of customer information
389 As with L<the cust_header subroutine|/cust_header>, the fields returned are
390 defined by the supplied customer fields setting, or if no customer fields
391 setting is supplied, the <B>cust-fields</B> configuration value.
399 join('', @cust_aligns);
404 # begin JSRPC code...
407 package FS::UI::Web::JSRPC;
412 use Storable qw(nfreeze);
415 use FS::UID qw(getotaker);
416 use FS::Record qw(qsearchs);
431 croak "CGI object required as second argument" unless $self->{'cgi'};
440 my $cgi = $self->{'cgi'};
442 # XXX this should parse JSON foo and build a proper data structure
443 my @args = $cgi->param('arg');
445 #work around konqueror bug!
446 @args = map { s/\x00$//; $_; } @args;
448 my $sub = $cgi->param('sub'); #????
450 warn "FS::UI::Web::JSRPC::process:\n".
453 " args=".join(', ',@args)."\n"
456 if ( $sub eq 'start_job' ) {
458 $self->start_job(@args);
460 } elsif ( $sub eq 'job_status' ) {
462 $self->job_status(@args);
466 die "unknown sub $sub";
475 warn "FS::UI::Web::start_job: ". join(', ', @_) if $DEBUG;
479 my( $field, $value ) = splice(@_, 0, 2);
480 unless ( exists( $param{$field} ) ) {
481 $param{$field} = $value;
482 } elsif ( ! ref($param{$field}) ) {
483 $param{$field} = [ $param{$field}, $value ];
485 push @{$param{$field}}, $value;
488 $param{CurrentUser} = getotaker();
489 warn "FS::UI::Web::start_job\n".
491 if ( ref($param{$_}) ) {
492 " $_ => [ ". join(', ', @{$param{$_}}). " ]\n";
494 " $_ => $param{$_}\n";
499 #first get the CGI params shipped off to a job ASAP so an id can be returned
502 my $job = new FS::queue { 'job' => $self->{'job'} };
504 #too slow to insert all the cgi params as individual args..,?
505 #my $error = $queue->insert('_JOB', $cgi->Vars);
507 #warn 'froze string of size '. length(nfreeze(\%param)). " for job args\n"
510 my $error = $job->insert( '_JOB', encode_base64(nfreeze(\%param)) );
514 warn "job not inserted: $error\n"
517 $error; #this doesn't seem to be handled well,
518 # will trigger "illegal jobnum" below?
519 # (should never be an error inserting the job, though, only thing
520 # would be Pg f%*kage)
523 warn "job inserted successfully with jobnum ". $job->jobnum. "\n"
532 my( $self, $jobnum ) = @_; #$url ???
534 sleep 1; # XXX could use something better...
537 if ( $jobnum =~ /^(\d+)$/ ) {
538 $job = qsearchs('queue', { 'jobnum' => $jobnum } );
540 die "FS::UI::Web::job_status: illegal jobnum $jobnum\n";
544 if ( $job && $job->status ne 'failed' ) {
545 @return = ( 'progress', $job->statustext );
546 } elsif ( !$job ) { #handle job gone case : job successful
547 # so close popup, redirect parent window...
548 @return = ( 'complete' );
550 @return = ( 'error', $job ? $job->statustext : $jobnum );