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 'Invoicing email(s)' => 'invoicing_list_emailonly_scalar',
226 my %header2colormethod = (
227 'Cust. Status' => 'cust_statuscolor',
230 'Cust. Status' => 'b',
233 'Cust. Status' => 'c',
240 warn " using supplied cust-fields override".
241 " (ignoring cust-fields config file)"
243 $cust_fields = shift;
247 my $conf = new FS::Conf;
248 if ( $conf->exists('cust-fields')
249 && $conf->config('cust-fields') =~ /^([\w\. \|\#\(\)]+):?/
252 warn " found cust-fields configuration value"
256 warn " no cust-fields configuration value found; using default 'Cust. Status | Customer'"
258 $cust_fields = 'Cust. Status | Customer';
263 @cust_header = split(/ \| /, $cust_fields);
264 @cust_fields = map { $header2method{$_} } @cust_header;
265 @cust_colors = map { exists $header2colormethod{$_}
266 ? $header2colormethod{$_}
270 @cust_styles = map { exists $header2style{$_} ? $header2style{$_} : '' }
272 @cust_aligns = map { exists $header2align{$_} ? $header2align{$_} : 'l' }
279 =item cust_sql_fields [ CUST_FIELDS_VALUE ]
281 Returns a list of fields for the SELECT portion of an SQL query.
283 As with L<the cust_header subroutine|/cust_header>, the fields returned are
284 defined by the supplied customer fields setting, or if no customer fields
285 setting is supplied, the <B>cust-fields</B> configuration value.
289 sub cust_sql_fields {
291 my @fields = qw( last first company );
292 push @fields, map "ship_$_", @fields;
293 push @fields, 'country';
296 #inefficientish, but tiny lists and only run once per page
298 grep { my $field = $_; grep { $_ eq $field } @cust_fields }
299 qw( address1 address2 city state zip daytime night );
301 map "cust_main.$_", @fields;
304 =item cust_fields OBJECT [ CUST_FIELDS_VALUE ]
306 Given an object that contains fields from cust_main (say, from a
307 JOINed search. See httemplate/search/svc_* for examples), returns an array
308 of customer information, or "(unlinked)" if this service is not linked to a
311 As with L<the cust_header subroutine|/cust_header>, the fields returned are
312 defined by the supplied customer fields setting, or if no customer fields
313 setting is supplied, the <B>cust-fields</B> configuration value.
319 warn "FS::UI::Web::cust_fields called for $svc_x ".
320 "(cust_fields: @cust_fields)"
323 #cust_header(@_) unless @cust_fields; #now need to cache to keep cust_fields
324 # #override incase we were passed as a sub
326 my $seen_unlinked = 0;
328 if ( $svc_x->custnum ) {
333 warn " ($svc_x unlinked)"
335 $seen_unlinked++ ? '' : '(unlinked)';
342 Returns an array of subroutine references (or empty strings) for returning
343 customer information colors.
345 As with L<the cust_header subroutine|/cust_header>, the fields returned are
346 defined by the supplied customer fields setting, or if no customer fields
347 setting is supplied, the <B>cust-fields</B> configuration value.
355 sub { shift->$method(@_) };
364 Returns an array of customer information styles.
366 As with L<the cust_header subroutine|/cust_header>, the fields returned are
367 defined by the supplied customer fields setting, or if no customer fields
368 setting is supplied, the <B>cust-fields</B> configuration value.
384 Returns an array or scalar (depending on context) of customer information
387 As with L<the cust_header subroutine|/cust_header>, the fields returned are
388 defined by the supplied customer fields setting, or if no customer fields
389 setting is supplied, the <B>cust-fields</B> configuration value.
397 join('', @cust_aligns);
402 # begin JSRPC code...
405 package FS::UI::Web::JSRPC;
410 use Storable qw(nfreeze);
414 use FS::Record qw(qsearchs);
429 croak "CGI object required as second argument" unless $self->{'cgi'};
438 my $cgi = $self->{'cgi'};
440 # XXX this should parse JSON foo and build a proper data structure
441 my @args = $cgi->param('arg');
443 #work around konqueror bug!
444 @args = map { s/\x00$//; $_; } @args;
446 my $sub = $cgi->param('sub'); #????
448 warn "FS::UI::Web::JSRPC::process:\n".
451 " args=".join(', ',@args)."\n"
454 if ( $sub eq 'start_job' ) {
456 $self->start_job(@args);
458 } elsif ( $sub eq 'job_status' ) {
460 $self->job_status(@args);
464 die "unknown sub $sub";
473 warn "FS::UI::Web::start_job: ". join(', ', @_) if $DEBUG;
477 my( $field, $value ) = splice(@_, 0, 2);
478 unless ( exists( $param{$field} ) ) {
479 $param{$field} = $value;
480 } elsif ( ! ref($param{$field}) ) {
481 $param{$field} = [ $param{$field}, $value ];
483 push @{$param{$field}}, $value;
486 warn "FS::UI::Web::start_job\n".
488 if ( ref($param{$_}) ) {
489 " $_ => [ ". join(', ', @{$param{$_}}). " ]\n";
491 " $_ => $param{$_}\n";
496 #first get the CGI params shipped off to a job ASAP so an id can be returned
499 my $job = new FS::queue { 'job' => $self->{'job'} };
501 #too slow to insert all the cgi params as individual args..,?
502 #my $error = $queue->insert('_JOB', $cgi->Vars);
504 #warn 'froze string of size '. length(nfreeze(\%param)). " for job args\n"
507 my $error = $job->insert( '_JOB', encode_base64(nfreeze(\%param)) );
511 warn "job not inserted: $error\n"
514 $error; #this doesn't seem to be handled well,
515 # will trigger "illegal jobnum" below?
516 # (should never be an error inserting the job, though, only thing
517 # would be Pg f%*kage)
520 warn "job inserted successfully with jobnum ". $job->jobnum. "\n"
529 my( $self, $jobnum ) = @_; #$url ???
531 sleep 1; # XXX could use something better...
534 if ( $jobnum =~ /^(\d+)$/ ) {
535 $job = qsearchs('queue', { 'jobnum' => $jobnum } );
537 die "FS::UI::Web::job_status: illegal jobnum $jobnum\n";
541 if ( $job && $job->status ne 'failed' ) {
542 @return = ( 'progress', $job->statustext );
543 } elsif ( !$job ) { #handle job gone case : job successful
544 # so close popup, redirect parent window...
545 @return = ( 'complete' );
547 @return = ( 'error', $job ? $job->statustext : $jobnum );