4 use vars qw($DEBUG @ISA @EXPORT_OK $me);
7 use FS::Record qw(dbdef);
8 use FS::cust_main; # are sql_balance and sql_date_balance in the right module?
13 @ISA = qw( Exporter );
15 @EXPORT_OK = qw( svc_url );
18 $me = '[FS::UID::Web]';
25 sub parse_beginning_ending {
26 my($cgi, $prefix) = @_;
27 $prefix .= '_' if $prefix;
30 if ( $cgi->param($prefix.'begin') =~ /^(\d+)$/ ) {
32 } elsif ( $cgi->param($prefix.'beginning') =~ /^([ 0-9\-\/]{1,64})$/ ) {
33 $beginning = str2time($1) || 0;
36 my $ending = 4294967295; #2^32-1
37 if ( $cgi->param($prefix.'end') =~ /^(\d+)$/ ) {
39 } elsif ( $cgi->param($prefix.'ending') =~ /^([ 0-9\-\/]{1,64})$/ ) {
40 #probably need an option to turn off the + 86399
41 $ending = str2time($1) + 86399;
44 ( $beginning, $ending );
49 Returns a service URL, first checking to see if there is a service-specific
50 page to link to, otherwise to a generic service handling page. Options are
51 passed as a list of name-value pairs, and include:
55 =item * m - Mason request object ($m)
57 =item * action - The action for which to construct "edit", "view", or "search"
59 =item ** part_svc - Service definition (see L<FS::part_svc>)
61 =item ** svcdb - Service table
63 =item *** query - Query string
65 =item *** svc - FS::cust_svc or FS::svc_* object
67 =item ahref - Optional flag, if set true returns <A HREF="$url"> instead of just the URL.
73 ** part_svc OR svcdb is required
75 *** query OR svc is required
82 # 'm' => $m, #mason request object
83 # 'action' => 'edit', #or 'view'
85 # 'part_svc' => $part_svc, #usual
87 # 'svcdb' => 'svc_table',
89 # 'query' => #optional query string
90 # # (pass a blank string if you want a "raw" URL to add your
93 # 'svc' => $svc_x, #or $cust_svc, it just needs a svcnum
98 # 'ahref' => 1, # if set true, returns <A HREF="$url">
100 use FS::CGI qw(rooturl);
104 #? return '' unless ref($opt{part_svc});
106 my $svcdb = $opt{svcdb} || $opt{part_svc}->svcdb;
107 my $query = exists($opt{query}) ? $opt{query} : $opt{svc}->svcnum;
109 warn "$me [svc_url] checking for /$opt{action}/$svcdb.cgi component"
111 if ( $opt{m}->interp->comp_exists("/$opt{action}/$svcdb.cgi") ) {
112 $url = "$svcdb.cgi?";
115 my $generic = $opt{action} eq 'search' ? 'cust_svc' : 'svc_Common';
117 $url = "$generic.html?svcdb=$svcdb;";
118 $url .= 'svcnum=' if $query =~ /^\d+(;|$)/ or $query eq '';
121 import FS::CGI 'rooturl'; #WTF! why is this necessary
122 my $return = rooturl(). "$opt{action}/$url$query";
124 $return = qq!<A HREF="$return">! if $opt{ahref};
130 my($m, $part_svc, $cust_svc) = @_ or return '';
131 svc_X_link( $part_svc->svc, @_ );
135 my($m, $part_svc, $cust_svc) = @_ or return '';
136 svc_X_link( ($cust_svc->label)[1], @_ );
140 my ($x, $m, $part_svc, $cust_svc) = @_ or return '';
143 unless $FS::CurrentUser::CurrentUser->access_right('View customer services');
149 'part_svc' => $part_svc,
156 #this probably needs an ACL too...
157 sub svc_export_links {
158 my ($m, $part_svc, $cust_svc) = @_ or return '';
160 my $ahref = $cust_svc->export_links;
166 my($cgi, $field) = @_;
175 foreach my $op (keys %op) {
177 warn "checking for ${field}_$op field\n"
180 if ( $cgi->param($field."_$op") =~ /^\s*\$?\s*(-?[\d\,\s]+(\.\d\d)?)\s*$/ ) {
183 $num =~ s/[\,\s]+//g;
184 my $search = "$field $op{$op} $num";
185 push @search, $search;
187 warn "found ${field}_$op field; adding search element $search\n"
198 # cust_main report subroutines
202 =item cust_header [ CUST_FIELDS_VALUE ]
204 Returns an array of customer information headers according to the supplied
205 customer fields value, or if no value is supplied, the B<cust-fields>
210 use vars qw( @cust_fields @cust_colors @cust_styles @cust_aligns );
214 warn "FS::UI:Web::cust_header called"
217 my $conf = new FS::Conf;
219 my %header2method = (
220 'Customer' => 'name',
221 'Cust. Status' => 'ucfirst_cust_status',
222 'Cust#' => 'custnum',
224 'Company' => 'company',
225 '(bill) Customer' => 'name',
226 '(service) Customer' => 'ship_name',
227 '(bill) Name' => 'contact',
228 '(service) Name' => 'ship_contact',
229 '(bill) Company' => 'company',
230 '(service) Company' => 'ship_company',
231 'Address 1' => 'address1',
232 'Address 2' => 'address2',
236 'Country' => 'country_full',
237 'Day phone' => 'daytime', # XXX should use msgcat, but how?
238 'Night phone' => 'night', # XXX should use msgcat, but how?
239 'Fax number' => 'fax',
240 '(bill) Address 1' => 'address1',
241 '(bill) Address 2' => 'address2',
242 '(bill) City' => 'city',
243 '(bill) State' => 'state',
244 '(bill) Zip' => 'zip',
245 '(bill) Country' => 'country_full',
246 '(bill) Day phone' => 'daytime', # XXX should use msgcat, but how?
247 '(bill) Night phone' => 'night', # XXX should use msgcat, but how?
248 '(bill) Fax number' => 'fax',
249 '(service) Address 1' => 'ship_address1',
250 '(service) Address 2' => 'ship_address2',
251 '(service) City' => 'ship_city',
252 '(service) State' => 'ship_state',
253 '(service) Zip' => 'ship_zip',
254 '(service) Country' => 'ship_country_full',
255 '(service) Day phone' => 'ship_daytime', # XXX should use msgcat, how?
256 '(service) Night phone' => 'ship_night', # XXX should use msgcat, how?
257 '(service) Fax number' => 'ship_fax',
258 'Invoicing email(s)' => 'invoicing_list_emailonly_scalar',
259 'Payment Type' => 'payby',
260 'Current Balance' => 'current_balance',
262 $header2method{'Cust#'} = 'display_custnum'
263 if $conf->exists('cust_main-default_agent_custid');
265 my %header2colormethod = (
266 'Cust. Status' => 'cust_statuscolor',
269 'Cust. Status' => 'b',
272 'Cust. Status' => 'c',
279 warn " using supplied cust-fields override".
280 " (ignoring cust-fields config file)"
282 $cust_fields = shift;
286 if ( $conf->exists('cust-fields')
287 && $conf->config('cust-fields') =~ /^([\w\. \|\#\(\)]+):?/
290 warn " found cust-fields configuration value"
294 warn " no cust-fields configuration value found; using default 'Cust. Status | Customer'"
296 $cust_fields = 'Cust. Status | Customer';
301 @cust_header = split(/ \| /, $cust_fields);
302 @cust_fields = map { $header2method{$_} || $_ } @cust_header;
303 @cust_colors = map { exists $header2colormethod{$_}
304 ? $header2colormethod{$_}
308 @cust_styles = map { exists $header2style{$_} ? $header2style{$_} : '' }
310 @cust_aligns = map { exists $header2align{$_} ? $header2align{$_} : 'l' }
317 =item cust_sql_fields [ CUST_FIELDS_VALUE ]
319 Returns a list of fields for the SELECT portion of an SQL query.
321 As with L<the cust_header subroutine|/cust_header>, the fields returned are
322 defined by the supplied customer fields setting, or if no customer fields
323 setting is supplied, the <B>cust-fields</B> configuration value.
327 sub cust_sql_fields {
329 my @fields = qw( last first company );
330 push @fields, map "ship_$_", @fields;
331 push @fields, 'country';
334 #inefficientish, but tiny lists and only run once per page
336 my @add_fields = qw( address1 address2 city state zip daytime night fax );
338 grep { my $field = $_; grep { $_ eq $field } @cust_fields }
339 ( @add_fields, ( map "ship_$_", @add_fields ), 'payby' );
341 push @fields, 'agent_custid';
343 my @extra_fields = ();
344 if (grep { $_ eq 'current_balance' } @cust_fields) {
345 push @extra_fields, FS::cust_main->balance_sql . " AS current_balance";
348 map("cust_main.$_", @fields), @extra_fields;
351 =item cust_fields OBJECT [ CUST_FIELDS_VALUE ]
353 Given an object that contains fields from cust_main (say, from a
354 JOINed search. See httemplate/search/svc_* for examples), returns an array
355 of customer information, or "(unlinked)" if this service is not linked to a
358 As with L<the cust_header subroutine|/cust_header>, the fields returned are
359 defined by the supplied customer fields setting, or if no customer fields
360 setting is supplied, the <B>cust-fields</B> configuration value.
366 warn "FS::UI::Web::cust_fields called for $record ".
367 "(cust_fields: @cust_fields)"
370 #cust_header(@_) unless @cust_fields; #now need to cache to keep cust_fields
371 # #override incase we were passed as a sub
373 my $seen_unlinked = 0;
375 if ( $record->custnum ) {
376 warn " $record -> $_"
380 warn " ($record unlinked)"
382 $seen_unlinked++ ? '' : '(unlinked)';
389 Returns an array of subroutine references (or empty strings) for returning
390 customer information colors.
392 As with L<the cust_header subroutine|/cust_header>, the fields returned are
393 defined by the supplied customer fields setting, or if no customer fields
394 setting is supplied, the <B>cust-fields</B> configuration value.
402 sub { shift->$method(@_) };
411 Returns an array of customer information styles.
413 As with L<the cust_header subroutine|/cust_header>, the fields returned are
414 defined by the supplied customer fields setting, or if no customer fields
415 setting is supplied, the <B>cust-fields</B> configuration value.
431 Returns an array or scalar (depending on context) of customer information
434 As with L<the cust_header subroutine|/cust_header>, the fields returned are
435 defined by the supplied customer fields setting, or if no customer fields
436 setting is supplied, the <B>cust-fields</B> configuration value.
444 join('', @cust_aligns);
449 # begin JSRPC code...
452 package FS::UI::Web::JSRPC;
457 use Storable qw(nfreeze);
460 use FS::UID qw(getotaker);
461 use FS::Record qw(qsearchs);
476 croak "CGI object required as second argument" unless $self->{'cgi'};
485 my $cgi = $self->{'cgi'};
487 # XXX this should parse JSON foo and build a proper data structure
488 my @args = $cgi->param('arg');
490 #work around konqueror bug!
491 @args = map { s/\x00$//; $_; } @args;
493 my $sub = $cgi->param('sub'); #????
495 warn "FS::UI::Web::JSRPC::process:\n".
498 " args=".join(', ',@args)."\n"
501 if ( $sub eq 'start_job' ) {
503 $self->start_job(@args);
505 } elsif ( $sub eq 'job_status' ) {
507 $self->job_status(@args);
511 die "unknown sub $sub";
520 warn "FS::UI::Web::start_job: ". join(', ', @_) if $DEBUG;
524 my( $field, $value ) = splice(@_, 0, 2);
525 unless ( exists( $param{$field} ) ) {
526 $param{$field} = $value;
527 } elsif ( ! ref($param{$field}) ) {
528 $param{$field} = [ $param{$field}, $value ];
530 push @{$param{$field}}, $value;
533 $param{CurrentUser} = getotaker();
534 warn "FS::UI::Web::start_job\n".
536 if ( ref($param{$_}) ) {
537 " $_ => [ ". join(', ', @{$param{$_}}). " ]\n";
539 " $_ => $param{$_}\n";
544 #first get the CGI params shipped off to a job ASAP so an id can be returned
547 my $job = new FS::queue { 'job' => $self->{'job'} };
549 #too slow to insert all the cgi params as individual args..,?
550 #my $error = $queue->insert('_JOB', $cgi->Vars);
552 #warn 'froze string of size '. length(nfreeze(\%param)). " for job args\n"
555 my $error = $job->insert( '_JOB', encode_base64(nfreeze(\%param)) );
559 warn "job not inserted: $error\n"
562 $error; #this doesn't seem to be handled well,
563 # will trigger "illegal jobnum" below?
564 # (should never be an error inserting the job, though, only thing
565 # would be Pg f%*kage)
568 warn "job inserted successfully with jobnum ". $job->jobnum. "\n"
577 my( $self, $jobnum ) = @_; #$url ???
579 sleep 1; # XXX could use something better...
582 if ( $jobnum =~ /^(\d+)$/ ) {
583 $job = qsearchs('queue', { 'jobnum' => $jobnum } );
585 die "FS::UI::Web::job_status: illegal jobnum $jobnum\n";
589 if ( $job && $job->status ne 'failed' ) {
590 my ($progress, $action) = split ',', $job->statustext, 2;
591 $action ||= 'Server processing job';
592 @return = ( 'progress', $progress, $action );
593 } elsif ( !$job ) { #handle job gone case : job successful
594 # so close popup, redirect parent window...
595 @return = ( 'complete' );
597 @return = ( 'error', $job ? $job->statustext : $jobnum );
600 #to_json(\@return); #waiting on deb 5.0 for new JSON.pm?