4 use vars qw($DEBUG @ISA @EXPORT_OK $me);
6 use Carp qw( confess );;
8 use FS::Misc::DateTime qw( parse_datetime );
9 use FS::Record qw(dbdef);
10 use FS::cust_main; # are sql_balance and sql_date_balance in the right module?
15 @ISA = qw( Exporter );
17 @EXPORT_OK = qw( svc_url );
20 $me = '[FS::UID::Web]';
27 sub parse_beginning_ending {
28 my($cgi, $prefix) = @_;
29 $prefix .= '_' if $prefix;
32 if ( $cgi->param($prefix.'begin') =~ /^(\d+)$/ ) {
34 } elsif ( $cgi->param($prefix.'beginning') =~ /^([ 0-9\-\/]{1,64})$/ ) {
35 $beginning = parse_datetime($1) || 0;
38 my $ending = 4294967295; #2^32-1
39 if ( $cgi->param($prefix.'end') =~ /^(\d+)$/ ) {
41 } elsif ( $cgi->param($prefix.'ending') =~ /^([ 0-9\-\/]{1,64})$/ ) {
42 #probably need an option to turn off the + 86399
43 $ending = parse_datetime($1) + 86399;
46 ( $beginning, $ending );
51 Returns a service URL, first checking to see if there is a service-specific
52 page to link to, otherwise to a generic service handling page. Options are
53 passed as a list of name-value pairs, and include:
57 =item * m - Mason request object ($m)
59 =item * action - The action for which to construct "edit", "view", or "search"
61 =item ** part_svc - Service definition (see L<FS::part_svc>)
63 =item ** svcdb - Service table
65 =item *** query - Query string
67 =item *** svc - FS::cust_svc or FS::svc_* object
69 =item ahref - Optional flag, if set true returns <A HREF="$url"> instead of just the URL.
75 ** part_svc OR svcdb is required
77 *** query OR svc is required
84 # 'm' => $m, #mason request object
85 # 'action' => 'edit', #or 'view'
87 # 'part_svc' => $part_svc, #usual
89 # 'svcdb' => 'svc_table',
91 # 'query' => #optional query string
92 # # (pass a blank string if you want a "raw" URL to add your
95 # 'svc' => $svc_x, #or $cust_svc, it just needs a svcnum
100 # 'ahref' => 1, # if set true, returns <A HREF="$url">
102 use FS::CGI qw(rooturl);
106 #? return '' unless ref($opt{part_svc});
108 my $svcdb = $opt{svcdb} || $opt{part_svc}->svcdb;
109 my $query = exists($opt{query}) ? $opt{query} : $opt{svc}->svcnum;
111 warn "$me [svc_url] checking for /$opt{action}/$svcdb.cgi component"
113 if ( $opt{m}->interp->comp_exists("/$opt{action}/$svcdb.cgi") ) {
114 $url = "$svcdb.cgi?";
117 my $generic = $opt{action} eq 'search' ? 'cust_svc' : 'svc_Common';
119 $url = "$generic.html?svcdb=$svcdb;";
120 $url .= 'svcnum=' if $query =~ /^\d+(;|$)/ or $query eq '';
123 import FS::CGI 'rooturl'; #WTF! why is this necessary
124 my $return = rooturl(). "$opt{action}/$url$query";
126 $return = qq!<A HREF="$return">! if $opt{ahref};
132 my($m, $part_svc, $cust_svc) = @_ or return '';
133 svc_X_link( $part_svc->svc, @_ );
137 my($m, $part_svc, $cust_svc) = @_ or return '';
138 svc_X_link( ($cust_svc->label)[1], @_ );
142 my ($x, $m, $part_svc, $cust_svc) = @_ or return '';
145 unless $FS::CurrentUser::CurrentUser->access_right('View customer services');
147 confess "svc_X_link called without a service ($x, $m, $part_svc, $cust_svc)\n"
154 'part_svc' => $part_svc,
161 #this probably needs an ACL too...
162 sub svc_export_links {
163 my ($m, $part_svc, $cust_svc) = @_ or return '';
165 my $ahref = $cust_svc->export_links;
171 my($cgi, $field) = @_;
180 foreach my $op (keys %op) {
182 warn "checking for ${field}_$op field\n"
185 if ( $cgi->param($field."_$op") =~ /^\s*\$?\s*(-?[\d\,\s]+(\.\d\d)?)\s*$/ ) {
188 $num =~ s/[\,\s]+//g;
189 my $search = "$field $op{$op} $num";
190 push @search, $search;
192 warn "found ${field}_$op field; adding search element $search\n"
203 # cust_main report subroutines
207 =item cust_header [ CUST_FIELDS_VALUE ]
209 Returns an array of customer information headers according to the supplied
210 customer fields value, or if no value is supplied, the B<cust-fields>
215 use vars qw( @cust_fields @cust_colors @cust_styles @cust_aligns );
219 warn "FS::UI:Web::cust_header called"
222 my $conf = new FS::Conf;
224 my %header2method = (
225 'Customer' => 'name',
226 'Cust. Status' => 'ucfirst_cust_status',
227 'Cust#' => 'custnum',
229 'Company' => 'company',
230 '(bill) Customer' => 'name',
231 '(service) Customer' => 'ship_name',
232 '(bill) Name' => 'contact',
233 '(service) Name' => 'ship_contact',
234 '(bill) Company' => 'company',
235 '(service) Company' => 'ship_company',
236 'Address 1' => 'address1',
237 'Address 2' => 'address2',
241 'Country' => 'country_full',
242 'Day phone' => 'daytime', # XXX should use msgcat, but how?
243 'Night phone' => 'night', # XXX should use msgcat, but how?
244 'Fax number' => 'fax',
245 '(bill) Address 1' => 'address1',
246 '(bill) Address 2' => 'address2',
247 '(bill) City' => 'city',
248 '(bill) State' => 'state',
249 '(bill) Zip' => 'zip',
250 '(bill) Country' => 'country_full',
251 '(bill) Day phone' => 'daytime', # XXX should use msgcat, but how?
252 '(bill) Night phone' => 'night', # XXX should use msgcat, but how?
253 '(bill) Fax number' => 'fax',
254 '(service) Address 1' => 'ship_address1',
255 '(service) Address 2' => 'ship_address2',
256 '(service) City' => 'ship_city',
257 '(service) State' => 'ship_state',
258 '(service) Zip' => 'ship_zip',
259 '(service) Country' => 'ship_country_full',
260 '(service) Day phone' => 'ship_daytime', # XXX should use msgcat, how?
261 '(service) Night phone' => 'ship_night', # XXX should use msgcat, how?
262 '(service) Fax number' => 'ship_fax',
263 'Invoicing email(s)' => 'invoicing_list_emailonly_scalar',
264 'Payment Type' => 'payby',
265 'Current Balance' => 'current_balance',
267 $header2method{'Cust#'} = 'display_custnum'
268 if $conf->exists('cust_main-default_agent_custid');
270 my %header2colormethod = (
271 'Cust. Status' => 'cust_statuscolor',
274 'Cust. Status' => 'b',
277 'Cust. Status' => 'c',
285 warn " using supplied cust-fields override".
286 " (ignoring cust-fields config file)"
288 $cust_fields = shift;
292 if ( $conf->exists('cust-fields')
293 && $conf->config('cust-fields') =~ /^([\w\. \|\#\(\)]+):?/
296 warn " found cust-fields configuration value"
300 warn " no cust-fields configuration value found; using default 'Cust. Status | Customer'"
302 $cust_fields = 'Cust. Status | Customer';
307 @cust_header = split(/ \| /, $cust_fields);
308 @cust_fields = map { $header2method{$_} || $_ } @cust_header;
309 @cust_colors = map { exists $header2colormethod{$_}
310 ? $header2colormethod{$_}
314 @cust_styles = map { exists $header2style{$_} ? $header2style{$_} : '' }
316 @cust_aligns = map { exists $header2align{$_} ? $header2align{$_} : 'l' }
323 =item cust_sql_fields [ CUST_FIELDS_VALUE ]
325 Returns a list of fields for the SELECT portion of an SQL query.
327 As with L<the cust_header subroutine|/cust_header>, the fields returned are
328 defined by the supplied customer fields setting, or if no customer fields
329 setting is supplied, the <B>cust-fields</B> configuration value.
333 sub cust_sql_fields {
335 my @fields = qw( last first company );
336 push @fields, map "ship_$_", @fields;
337 push @fields, 'country';
340 #inefficientish, but tiny lists and only run once per page
342 my @add_fields = qw( address1 address2 city state zip daytime night fax );
344 grep { my $field = $_; grep { $_ eq $field } @cust_fields }
345 ( @add_fields, ( map "ship_$_", @add_fields ), 'payby' );
347 push @fields, 'agent_custid';
349 my @extra_fields = ();
350 if (grep { $_ eq 'current_balance' } @cust_fields) {
351 push @extra_fields, FS::cust_main->balance_sql . " AS current_balance";
354 map("cust_main.$_", @fields), @extra_fields;
357 =item cust_fields OBJECT [ CUST_FIELDS_VALUE ]
359 Given an object that contains fields from cust_main (say, from a
360 JOINed search. See httemplate/search/svc_* for examples), returns an array
361 of customer information, or "(unlinked)" if this service is not linked to a
364 As with L<the cust_header subroutine|/cust_header>, the fields returned are
365 defined by the supplied customer fields setting, or if no customer fields
366 setting is supplied, the <B>cust-fields</B> configuration value.
373 warn "FS::UI::Web::cust_fields called for $record ".
374 "(cust_fields: @cust_fields)"
377 #cust_header(@_) unless @cust_fields; #now need to cache to keep cust_fields
378 # #override incase we were passed as a sub
380 my $seen_unlinked = 0;
383 if ( $record->custnum ) {
384 warn " $record -> $_" if $DEBUG > 1;
387 warn " ($record unlinked)" if $DEBUG > 1;
388 $seen_unlinked++ ? '' : '(unlinked)';
393 =item cust_fields_subs
395 Returns an array of subroutine references for returning customer field values.
396 This is similar to cust_fields, but returns each field's sub as a distinct
401 sub cust_fields_subs {
402 my $unlinked_warn = 0;
405 if( $unlinked_warn++ ) {
408 if( $record->custnum ) {
419 $record->$f(@_) if $record->custnum;
427 Returns an array of subroutine references (or empty strings) for returning
428 customer information colors.
430 As with L<the cust_header subroutine|/cust_header>, the fields returned are
431 defined by the supplied customer fields setting, or if no customer fields
432 setting is supplied, the <B>cust-fields</B> configuration value.
440 sub { shift->$method(@_) };
449 Returns an array of customer information styles.
451 As with L<the cust_header subroutine|/cust_header>, the fields returned are
452 defined by the supplied customer fields setting, or if no customer fields
453 setting is supplied, the <B>cust-fields</B> configuration value.
469 Returns an array or scalar (depending on context) of customer information
472 As with L<the cust_header subroutine|/cust_header>, the fields returned are
473 defined by the supplied customer fields setting, or if no customer fields
474 setting is supplied, the <B>cust-fields</B> configuration value.
482 join('', @cust_aligns);
487 # begin JSRPC code...
490 package FS::UI::Web::JSRPC;
495 use Storable qw(nfreeze);
498 use FS::UID qw(getotaker);
499 use FS::Record qw(qsearchs);
501 use FS::CGI qw(rooturl);
515 croak "CGI object required as second argument" unless $self->{'cgi'};
524 my $cgi = $self->{'cgi'};
526 # XXX this should parse JSON foo and build a proper data structure
527 my @args = $cgi->param('arg');
529 #work around konqueror bug!
530 @args = map { s/\x00$//; $_; } @args;
532 my $sub = $cgi->param('sub'); #????
534 warn "FS::UI::Web::JSRPC::process:\n".
537 " args=".join(', ',@args)."\n"
540 if ( $sub eq 'start_job' ) {
542 $self->start_job(@args);
544 } elsif ( $sub eq 'job_status' ) {
546 $self->job_status(@args);
550 die "unknown sub $sub";
559 warn "FS::UI::Web::start_job: ". join(', ', @_) if $DEBUG;
563 my( $field, $value ) = splice(@_, 0, 2);
564 unless ( exists( $param{$field} ) ) {
565 $param{$field} = $value;
566 } elsif ( ! ref($param{$field}) ) {
567 $param{$field} = [ $param{$field}, $value ];
569 push @{$param{$field}}, $value;
572 $param{CurrentUser} = getotaker();
573 $param{RootURL} = rooturl($self->{cgi}->self_url);
574 warn "FS::UI::Web::start_job\n".
576 if ( ref($param{$_}) ) {
577 " $_ => [ ". join(', ', @{$param{$_}}). " ]\n";
579 " $_ => $param{$_}\n";
584 #first get the CGI params shipped off to a job ASAP so an id can be returned
587 my $job = new FS::queue { 'job' => $self->{'job'} };
589 #too slow to insert all the cgi params as individual args..,?
590 #my $error = $queue->insert('_JOB', $cgi->Vars);
592 #warn 'froze string of size '. length(nfreeze(\%param)). " for job args\n"
595 my $error = $job->insert( '_JOB', encode_base64(nfreeze(\%param)) );
599 warn "job not inserted: $error\n"
602 $error; #this doesn't seem to be handled well,
603 # will trigger "illegal jobnum" below?
604 # (should never be an error inserting the job, though, only thing
605 # would be Pg f%*kage)
608 warn "job inserted successfully with jobnum ". $job->jobnum. "\n"
617 my( $self, $jobnum ) = @_; #$url ???
619 sleep 1; # XXX could use something better...
622 if ( $jobnum =~ /^(\d+)$/ ) {
623 $job = qsearchs('queue', { 'jobnum' => $jobnum } );
625 die "FS::UI::Web::job_status: illegal jobnum $jobnum\n";
629 if ( $job && $job->status ne 'failed' && $job->status ne 'done' ) {
630 my ($progress, $action) = split ',', $job->statustext, 2;
631 $action ||= 'Server processing job';
632 @return = ( 'progress', $progress, $action );
633 } elsif ( !$job ) { #handle job gone case : job successful
634 # so close popup, redirect parent window...
635 @return = ( 'complete' );
636 } elsif ( $job->status eq 'done' ) {
637 @return = ( 'done', $job->statustext, '' );
639 @return = ( 'error', $job ? $job->statustext : $jobnum );
642 #to_json(\@return); #waiting on deb 5.0 for new JSON.pm?