4 use vars qw($DEBUG @ISA @EXPORT_OK $me);
7 use FS::Misc::DateTime qw( parse_datetime );
8 use FS::Record qw(dbdef);
9 use FS::cust_main; # are sql_balance and sql_date_balance in the right module?
14 @ISA = qw( Exporter );
16 @EXPORT_OK = qw( svc_url );
19 $me = '[FS::UID::Web]';
26 sub parse_beginning_ending {
27 my($cgi, $prefix) = @_;
28 $prefix .= '_' if $prefix;
31 if ( $cgi->param($prefix.'begin') =~ /^(\d+)$/ ) {
33 } elsif ( $cgi->param($prefix.'beginning') =~ /^([ 0-9\-\/]{1,64})$/ ) {
34 $beginning = parse_datetime($1) || 0;
37 my $ending = 4294967295; #2^32-1
38 if ( $cgi->param($prefix.'end') =~ /^(\d+)$/ ) {
40 } elsif ( $cgi->param($prefix.'ending') =~ /^([ 0-9\-\/]{1,64})$/ ) {
41 #probably need an option to turn off the + 86399
42 $ending = parse_datetime($1) + 86399;
45 ( $beginning, $ending );
50 Returns a service URL, first checking to see if there is a service-specific
51 page to link to, otherwise to a generic service handling page. Options are
52 passed as a list of name-value pairs, and include:
56 =item * m - Mason request object ($m)
58 =item * action - The action for which to construct "edit", "view", or "search"
60 =item ** part_svc - Service definition (see L<FS::part_svc>)
62 =item ** svcdb - Service table
64 =item *** query - Query string
66 =item *** svc - FS::cust_svc or FS::svc_* object
68 =item ahref - Optional flag, if set true returns <A HREF="$url"> instead of just the URL.
74 ** part_svc OR svcdb is required
76 *** query OR svc is required
83 # 'm' => $m, #mason request object
84 # 'action' => 'edit', #or 'view'
86 # 'part_svc' => $part_svc, #usual
88 # 'svcdb' => 'svc_table',
90 # 'query' => #optional query string
91 # # (pass a blank string if you want a "raw" URL to add your
94 # 'svc' => $svc_x, #or $cust_svc, it just needs a svcnum
99 # 'ahref' => 1, # if set true, returns <A HREF="$url">
101 use FS::CGI qw(rooturl);
105 #? return '' unless ref($opt{part_svc});
107 my $svcdb = $opt{svcdb} || $opt{part_svc}->svcdb;
108 my $query = exists($opt{query}) ? $opt{query} : $opt{svc}->svcnum;
110 warn "$me [svc_url] checking for /$opt{action}/$svcdb.cgi component"
112 if ( $opt{m}->interp->comp_exists("/$opt{action}/$svcdb.cgi") ) {
113 $url = "$svcdb.cgi?";
116 my $generic = $opt{action} eq 'search' ? 'cust_svc' : 'svc_Common';
118 $url = "$generic.html?svcdb=$svcdb;";
119 $url .= 'svcnum=' if $query =~ /^\d+(;|$)/ or $query eq '';
122 import FS::CGI 'rooturl'; #WTF! why is this necessary
123 my $return = rooturl(). "$opt{action}/$url$query";
125 $return = qq!<A HREF="$return">! if $opt{ahref};
131 my($m, $part_svc, $cust_svc) = @_ or return '';
132 svc_X_link( $part_svc->svc, @_ );
136 my($m, $part_svc, $cust_svc) = @_ or return '';
137 svc_X_link( ($cust_svc->label)[1], @_ );
141 my ($x, $m, $part_svc, $cust_svc) = @_ or return '';
144 unless $FS::CurrentUser::CurrentUser->access_right('View customer services');
150 'part_svc' => $part_svc,
157 #this probably needs an ACL too...
158 sub svc_export_links {
159 my ($m, $part_svc, $cust_svc) = @_ or return '';
161 my $ahref = $cust_svc->export_links;
167 my($cgi, $field) = @_;
176 foreach my $op (keys %op) {
178 warn "checking for ${field}_$op field\n"
181 if ( $cgi->param($field."_$op") =~ /^\s*\$?\s*(-?[\d\,\s]+(\.\d\d)?)\s*$/ ) {
184 $num =~ s/[\,\s]+//g;
185 my $search = "$field $op{$op} $num";
186 push @search, $search;
188 warn "found ${field}_$op field; adding search element $search\n"
199 # cust_main report subroutines
203 =item cust_header [ CUST_FIELDS_VALUE ]
205 Returns an array of customer information headers according to the supplied
206 customer fields value, or if no value is supplied, the B<cust-fields>
211 use vars qw( @cust_fields @cust_colors @cust_styles @cust_aligns );
215 warn "FS::UI:Web::cust_header called"
218 my $conf = new FS::Conf;
220 my %header2method = (
221 'Customer' => 'name',
222 'Cust. Status' => 'ucfirst_cust_status',
223 'Cust#' => 'custnum',
225 'Company' => 'company',
226 '(bill) Customer' => 'name',
227 '(service) Customer' => 'ship_name',
228 '(bill) Name' => 'contact',
229 '(service) Name' => 'ship_contact',
230 '(bill) Company' => 'company',
231 '(service) Company' => 'ship_company',
232 'Address 1' => 'address1',
233 'Address 2' => 'address2',
237 'Country' => 'country_full',
238 'Day phone' => 'daytime', # XXX should use msgcat, but how?
239 'Night phone' => 'night', # XXX should use msgcat, but how?
240 'Fax number' => 'fax',
241 '(bill) Address 1' => 'address1',
242 '(bill) Address 2' => 'address2',
243 '(bill) City' => 'city',
244 '(bill) State' => 'state',
245 '(bill) Zip' => 'zip',
246 '(bill) Country' => 'country_full',
247 '(bill) Day phone' => 'daytime', # XXX should use msgcat, but how?
248 '(bill) Night phone' => 'night', # XXX should use msgcat, but how?
249 '(bill) Fax number' => 'fax',
250 '(service) Address 1' => 'ship_address1',
251 '(service) Address 2' => 'ship_address2',
252 '(service) City' => 'ship_city',
253 '(service) State' => 'ship_state',
254 '(service) Zip' => 'ship_zip',
255 '(service) Country' => 'ship_country_full',
256 '(service) Day phone' => 'ship_daytime', # XXX should use msgcat, how?
257 '(service) Night phone' => 'ship_night', # XXX should use msgcat, how?
258 '(service) Fax number' => 'ship_fax',
259 'Invoicing email(s)' => 'invoicing_list_emailonly_scalar',
260 'Payment Type' => 'payby',
261 'Current Balance' => 'current_balance',
263 $header2method{'Cust#'} = 'display_custnum'
264 if $conf->exists('cust_main-default_agent_custid');
266 my %header2colormethod = (
267 'Cust. Status' => 'cust_statuscolor',
270 'Cust. Status' => 'b',
273 'Cust. Status' => 'c',
281 warn " using supplied cust-fields override".
282 " (ignoring cust-fields config file)"
284 $cust_fields = shift;
288 if ( $conf->exists('cust-fields')
289 && $conf->config('cust-fields') =~ /^([\w\. \|\#\(\)]+):?/
292 warn " found cust-fields configuration value"
296 warn " no cust-fields configuration value found; using default 'Cust. Status | Customer'"
298 $cust_fields = 'Cust. Status | Customer';
303 @cust_header = split(/ \| /, $cust_fields);
304 @cust_fields = map { $header2method{$_} || $_ } @cust_header;
305 @cust_colors = map { exists $header2colormethod{$_}
306 ? $header2colormethod{$_}
310 @cust_styles = map { exists $header2style{$_} ? $header2style{$_} : '' }
312 @cust_aligns = map { exists $header2align{$_} ? $header2align{$_} : 'l' }
319 =item cust_sql_fields [ CUST_FIELDS_VALUE ]
321 Returns a list of fields for the SELECT portion of an SQL query.
323 As with L<the cust_header subroutine|/cust_header>, the fields returned are
324 defined by the supplied customer fields setting, or if no customer fields
325 setting is supplied, the <B>cust-fields</B> configuration value.
329 sub cust_sql_fields {
331 my @fields = qw( last first company );
332 push @fields, map "ship_$_", @fields;
333 push @fields, 'country';
336 #inefficientish, but tiny lists and only run once per page
338 my @add_fields = qw( address1 address2 city state zip daytime night fax );
340 grep { my $field = $_; grep { $_ eq $field } @cust_fields }
341 ( @add_fields, ( map "ship_$_", @add_fields ), 'payby' );
343 push @fields, 'agent_custid';
345 my @extra_fields = ();
346 if (grep { $_ eq 'current_balance' } @cust_fields) {
347 push @extra_fields, FS::cust_main->balance_sql . " AS current_balance";
350 map("cust_main.$_", @fields), @extra_fields;
353 =item cust_fields OBJECT [ CUST_FIELDS_VALUE ]
355 Given an object that contains fields from cust_main (say, from a
356 JOINed search. See httemplate/search/svc_* for examples), returns an array
357 of customer information, or "(unlinked)" if this service is not linked to a
360 As with L<the cust_header subroutine|/cust_header>, the fields returned are
361 defined by the supplied customer fields setting, or if no customer fields
362 setting is supplied, the <B>cust-fields</B> configuration value.
369 warn "FS::UI::Web::cust_fields called for $record ".
370 "(cust_fields: @cust_fields)"
373 #cust_header(@_) unless @cust_fields; #now need to cache to keep cust_fields
374 # #override incase we were passed as a sub
376 my $seen_unlinked = 0;
379 if ( $record->custnum ) {
380 warn " $record -> $_" if $DEBUG > 1;
383 warn " ($record unlinked)" if $DEBUG > 1;
384 $seen_unlinked++ ? '' : '(unlinked)';
389 =item cust_fields_subs
391 Returns an array of subroutine references for returning customer field values.
392 This is similar to cust_fields, but returns each field's sub as a distinct
397 sub cust_fields_subs {
398 my $unlinked_warn = 0;
401 if( $unlinked_warn++ ) {
404 if( $record->custnum ) {
415 $record->$f(@_) if $record->custnum;
423 Returns an array of subroutine references (or empty strings) for returning
424 customer information colors.
426 As with L<the cust_header subroutine|/cust_header>, the fields returned are
427 defined by the supplied customer fields setting, or if no customer fields
428 setting is supplied, the <B>cust-fields</B> configuration value.
436 sub { shift->$method(@_) };
445 Returns an array of customer information styles.
447 As with L<the cust_header subroutine|/cust_header>, the fields returned are
448 defined by the supplied customer fields setting, or if no customer fields
449 setting is supplied, the <B>cust-fields</B> configuration value.
465 Returns an array or scalar (depending on context) of customer information
468 As with L<the cust_header subroutine|/cust_header>, the fields returned are
469 defined by the supplied customer fields setting, or if no customer fields
470 setting is supplied, the <B>cust-fields</B> configuration value.
478 join('', @cust_aligns);
483 # begin JSRPC code...
486 package FS::UI::Web::JSRPC;
491 use Storable qw(nfreeze);
494 use FS::UID qw(getotaker);
495 use FS::Record qw(qsearchs);
497 use FS::CGI qw(rooturl);
511 croak "CGI object required as second argument" unless $self->{'cgi'};
520 my $cgi = $self->{'cgi'};
522 # XXX this should parse JSON foo and build a proper data structure
523 my @args = $cgi->param('arg');
525 #work around konqueror bug!
526 @args = map { s/\x00$//; $_; } @args;
528 my $sub = $cgi->param('sub'); #????
530 warn "FS::UI::Web::JSRPC::process:\n".
533 " args=".join(', ',@args)."\n"
536 if ( $sub eq 'start_job' ) {
538 $self->start_job(@args);
540 } elsif ( $sub eq 'job_status' ) {
542 $self->job_status(@args);
546 die "unknown sub $sub";
555 warn "FS::UI::Web::start_job: ". join(', ', @_) if $DEBUG;
559 my( $field, $value ) = splice(@_, 0, 2);
560 unless ( exists( $param{$field} ) ) {
561 $param{$field} = $value;
562 } elsif ( ! ref($param{$field}) ) {
563 $param{$field} = [ $param{$field}, $value ];
565 push @{$param{$field}}, $value;
568 $param{CurrentUser} = getotaker();
569 $param{RootURL} = rooturl($self->{cgi}->self_url);
570 warn "FS::UI::Web::start_job\n".
572 if ( ref($param{$_}) ) {
573 " $_ => [ ". join(', ', @{$param{$_}}). " ]\n";
575 " $_ => $param{$_}\n";
580 #first get the CGI params shipped off to a job ASAP so an id can be returned
583 my $job = new FS::queue { 'job' => $self->{'job'} };
585 #too slow to insert all the cgi params as individual args..,?
586 #my $error = $queue->insert('_JOB', $cgi->Vars);
588 #warn 'froze string of size '. length(nfreeze(\%param)). " for job args\n"
591 my $error = $job->insert( '_JOB', encode_base64(nfreeze(\%param)) );
595 warn "job not inserted: $error\n"
598 $error; #this doesn't seem to be handled well,
599 # will trigger "illegal jobnum" below?
600 # (should never be an error inserting the job, though, only thing
601 # would be Pg f%*kage)
604 warn "job inserted successfully with jobnum ". $job->jobnum. "\n"
613 my( $self, $jobnum ) = @_; #$url ???
615 sleep 1; # XXX could use something better...
618 if ( $jobnum =~ /^(\d+)$/ ) {
619 $job = qsearchs('queue', { 'jobnum' => $jobnum } );
621 die "FS::UI::Web::job_status: illegal jobnum $jobnum\n";
625 if ( $job && $job->status ne 'failed' && $job->status ne 'done' ) {
626 my ($progress, $action) = split ',', $job->statustext, 2;
627 $action ||= 'Server processing job';
628 @return = ( 'progress', $progress, $action );
629 } elsif ( !$job ) { #handle job gone case : job successful
630 # so close popup, redirect parent window...
631 @return = ( 'complete' );
632 } elsif ( $job->status eq 'done' ) {
633 @return = ( 'done', $job->statustext, '' );
635 @return = ( 'error', $job ? $job->statustext : $jobnum );
638 #to_json(\@return); #waiting on deb 5.0 for new JSON.pm?