4 use vars qw($DEBUG @ISA @EXPORT_OK $me);
6 use Carp qw( confess );
9 use FS::Misc::DateTime qw( parse_datetime );
10 use FS::Record qw(dbdef);
11 use FS::cust_main; # are sql_balance and sql_date_balance in the right module?
16 @ISA = qw( Exporter );
18 @EXPORT_OK = qw( svc_url );
21 $me = '[FS::UID::Web]';
28 sub parse_beginning_ending {
29 my($cgi, $prefix) = @_;
30 $prefix .= '_' if $prefix;
33 if ( $cgi->param($prefix.'begin') =~ /^(\d+)$/ ) {
35 } elsif ( $cgi->param($prefix.'beginning') =~ /^([ 0-9\-\/]{1,64})$/ ) {
36 $beginning = parse_datetime($1) || 0;
39 my $ending = 4294967295; #2^32-1
40 if ( $cgi->param($prefix.'end') =~ /^(\d+)$/ ) {
42 } elsif ( $cgi->param($prefix.'ending') =~ /^([ 0-9\-\/]{1,64})$/ ) {
43 #probably need an option to turn off the + 86399
44 $ending = parse_datetime($1) + 86399;
47 ( $beginning, $ending );
52 Returns a service URL, first checking to see if there is a service-specific
53 page to link to, otherwise to a generic service handling page. Options are
54 passed as a list of name-value pairs, and include:
58 =item * m - Mason request object ($m)
60 =item * action - The action for which to construct "edit", "view", or "search"
62 =item ** part_svc - Service definition (see L<FS::part_svc>)
64 =item ** svcdb - Service table
66 =item *** query - Query string
68 =item *** svc - FS::cust_svc or FS::svc_* object
70 =item ahref - Optional flag, if set true returns <A HREF="$url"> instead of just the URL.
76 ** part_svc OR svcdb is required
78 *** query OR svc is required
85 # 'm' => $m, #mason request object
86 # 'action' => 'edit', #or 'view'
88 # 'part_svc' => $part_svc, #usual
90 # 'svcdb' => 'svc_table',
92 # 'query' => #optional query string
93 # # (pass a blank string if you want a "raw" URL to add your
96 # 'svc' => $svc_x, #or $cust_svc, it just needs a svcnum
101 # 'ahref' => 1, # if set true, returns <A HREF="$url">
103 use FS::CGI qw(rooturl);
107 #? return '' unless ref($opt{part_svc});
109 my $svcdb = $opt{svcdb} || $opt{part_svc}->svcdb;
110 my $query = exists($opt{query}) ? $opt{query} : $opt{svc}->svcnum;
112 warn "$me [svc_url] checking for /$opt{action}/$svcdb.cgi component"
114 if ( $opt{m}->interp->comp_exists("/$opt{action}/$svcdb.cgi") ) {
115 $url = "$svcdb.cgi?";
118 my $generic = $opt{action} eq 'search' ? 'cust_svc' : 'svc_Common';
120 $url = "$generic.html?svcdb=$svcdb;";
121 $url .= 'svcnum=' if $query =~ /^\d+(;|$)/ or $query eq '';
124 import FS::CGI 'rooturl'; #WTF! why is this necessary
125 my $return = rooturl(). "$opt{action}/$url$query";
127 $return = qq!<A HREF="$return">! if $opt{ahref};
133 my($m, $part_svc, $cust_svc) = @_ or return '';
134 svc_X_link( $part_svc->svc, @_ );
138 my($m, $part_svc, $cust_svc) = @_ or return '';
139 my($svc, $label, $svcdb) = $cust_svc->label;
140 svc_X_link( $label, @_ );
144 my ($x, $m, $part_svc, $cust_svc) = @_ or return '';
147 unless $FS::CurrentUser::CurrentUser->access_right('View customer services');
149 confess "svc_X_link called without a service ($x, $m, $part_svc, $cust_svc)\n"
156 'part_svc' => $part_svc,
163 #this probably needs an ACL too...
164 sub svc_export_links {
165 my ($m, $part_svc, $cust_svc) = @_ or return '';
167 my $ahref = $cust_svc->export_links;
173 my($cgi, $field) = @_;
182 foreach my $op (keys %op) {
184 warn "checking for ${field}_$op field\n"
187 if ( $cgi->param($field."_$op") =~ /^\s*\$?\s*(-?[\d\,\s]+(\.\d\d)?)\s*$/ ) {
190 $num =~ s/[\,\s]+//g;
191 my $search = "$field $op{$op} $num";
192 push @search, $search;
194 warn "found ${field}_$op field; adding search element $search\n"
205 # cust_main report subroutines
209 =item cust_header [ CUST_FIELDS_VALUE ]
211 Returns an array of customer information headers according to the supplied
212 customer fields value, or if no value is supplied, the B<cust-fields>
217 use vars qw( @cust_fields @cust_colors @cust_styles @cust_aligns );
221 warn "FS::UI:Web::cust_header called"
224 my $conf = new FS::Conf;
226 my %header2method = (
227 'Customer' => 'name',
228 'Cust. Status' => 'ucfirst_cust_status',
229 'Cust#' => 'custnum',
231 'Company' => 'company',
232 '(bill) Customer' => 'name',
233 '(service) Customer' => 'ship_name',
234 '(bill) Name' => 'contact',
235 '(service) Name' => 'ship_contact',
236 '(bill) Company' => 'company',
237 '(service) Company' => 'ship_company',
238 'Address 1' => 'address1',
239 'Address 2' => 'address2',
243 'Country' => 'country_full',
244 'Day phone' => 'daytime', # XXX should use msgcat, but how?
245 'Night phone' => 'night', # XXX should use msgcat, but how?
246 'Fax number' => 'fax',
247 '(bill) Address 1' => 'address1',
248 '(bill) Address 2' => 'address2',
249 '(bill) City' => 'city',
250 '(bill) State' => 'state',
251 '(bill) Zip' => 'zip',
252 '(bill) Country' => 'country_full',
253 '(bill) Day phone' => 'daytime', # XXX should use msgcat, but how?
254 '(bill) Night phone' => 'night', # XXX should use msgcat, but how?
255 '(bill) Fax number' => 'fax',
256 '(service) Address 1' => 'ship_address1',
257 '(service) Address 2' => 'ship_address2',
258 '(service) City' => 'ship_city',
259 '(service) State' => 'ship_state',
260 '(service) Zip' => 'ship_zip',
261 '(service) Country' => 'ship_country_full',
262 '(service) Day phone' => 'ship_daytime', # XXX should use msgcat, how?
263 '(service) Night phone' => 'ship_night', # XXX should use msgcat, how?
264 '(service) Fax number' => 'ship_fax',
265 'Invoicing email(s)' => 'invoicing_list_emailonly_scalar',
266 'Payment Type' => 'payby',
267 'Current Balance' => 'current_balance',
269 $header2method{'Cust#'} = 'display_custnum'
270 if $conf->exists('cust_main-default_agent_custid');
272 my %header2colormethod = (
273 'Cust. Status' => 'cust_statuscolor',
276 'Cust. Status' => 'b',
279 'Cust. Status' => 'c',
287 warn " using supplied cust-fields override".
288 " (ignoring cust-fields config file)"
290 $cust_fields = shift;
294 if ( $conf->exists('cust-fields')
295 && $conf->config('cust-fields') =~ /^([\w\. \|\#\(\)]+):?/
298 warn " found cust-fields configuration value"
302 warn " no cust-fields configuration value found; using default 'Cust. Status | Customer'"
304 $cust_fields = 'Cust. Status | Customer';
309 @cust_header = split(/ \| /, $cust_fields);
310 @cust_fields = map { $header2method{$_} || $_ } @cust_header;
311 @cust_colors = map { exists $header2colormethod{$_}
312 ? $header2colormethod{$_}
316 @cust_styles = map { exists $header2style{$_} ? $header2style{$_} : '' }
318 @cust_aligns = map { exists $header2align{$_} ? $header2align{$_} : 'l' }
325 =item cust_sql_fields [ CUST_FIELDS_VALUE ]
327 Returns a list of fields for the SELECT portion of an SQL query.
329 As with L<the cust_header subroutine|/cust_header>, the fields returned are
330 defined by the supplied customer fields setting, or if no customer fields
331 setting is supplied, the <B>cust-fields</B> configuration value.
335 sub cust_sql_fields {
337 my @fields = qw( last first company );
338 push @fields, map "ship_$_", @fields;
339 push @fields, 'country';
342 #inefficientish, but tiny lists and only run once per page
344 my @add_fields = qw( address1 address2 city state zip daytime night fax );
346 grep { my $field = $_; grep { $_ eq $field } @cust_fields }
347 ( @add_fields, ( map "ship_$_", @add_fields ), 'payby' );
349 push @fields, 'agent_custid';
351 my @extra_fields = ();
352 if (grep { $_ eq 'current_balance' } @cust_fields) {
353 push @extra_fields, FS::cust_main->balance_sql . " AS current_balance";
356 map("cust_main.$_", @fields), @extra_fields;
359 =item cust_fields OBJECT [ CUST_FIELDS_VALUE ]
361 Given an object that contains fields from cust_main (say, from a
362 JOINed search. See httemplate/search/svc_* for examples), returns an array
363 of customer information, or "(unlinked)" if this service is not linked to a
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.
375 warn "FS::UI::Web::cust_fields called for $record ".
376 "(cust_fields: @cust_fields)"
379 #cust_header(@_) unless @cust_fields; #now need to cache to keep cust_fields
380 # #override incase we were passed as a sub
382 my $seen_unlinked = 0;
385 if ( $record->custnum ) {
386 warn " $record -> $_" if $DEBUG > 1;
387 encode_entities( $record->$_(@_) );
389 warn " ($record unlinked)" if $DEBUG > 1;
390 $seen_unlinked++ ? '' : '(unlinked)';
395 =item cust_fields_subs
397 Returns an array of subroutine references for returning customer field values.
398 This is similar to cust_fields, but returns each field's sub as a distinct
403 sub cust_fields_subs {
404 my $unlinked_warn = 0;
407 if( $unlinked_warn++ ) {
410 if( $record->custnum ) {
421 $record->$f(@_) if $record->custnum;
429 Returns an array of subroutine references (or empty strings) for returning
430 customer information colors.
432 As with L<the cust_header subroutine|/cust_header>, the fields returned are
433 defined by the supplied customer fields setting, or if no customer fields
434 setting is supplied, the <B>cust-fields</B> configuration value.
442 sub { shift->$method(@_) };
451 Returns an array of customer information styles.
453 As with L<the cust_header subroutine|/cust_header>, the fields returned are
454 defined by the supplied customer fields setting, or if no customer fields
455 setting is supplied, the <B>cust-fields</B> configuration value.
471 Returns an array or scalar (depending on context) of customer information
474 As with L<the cust_header subroutine|/cust_header>, the fields returned are
475 defined by the supplied customer fields setting, or if no customer fields
476 setting is supplied, the <B>cust-fields</B> configuration value.
484 join('', @cust_aligns);
490 Utility function to determine if the client is a mobile browser.
495 my $ua = $ENV{'HTTP_USER_AGENT'} || '';
496 if ( $ua =~ /(?:hiptop|Blazer|Novarra|Vagabond|SonyEricsson|Symbian|NetFront|UP.Browser|UP.Link|Windows CE|MIDP|J2ME|DoCoMo|J-PHONE|PalmOS|PalmSource|iPhone|iPod|AvantGo|Nokia|Android|WebOS|S60|Opera Mini|Opera Mobi)/io ) {
503 # begin JSRPC code...
506 package FS::UI::Web::JSRPC;
511 use Storable qw(nfreeze);
514 use FS::UID qw(getotaker);
515 use FS::Record qw(qsearchs);
517 use FS::CGI qw(rooturl);
531 croak "CGI object required as second argument" unless $self->{'cgi'};
540 my $cgi = $self->{'cgi'};
542 # XXX this should parse JSON foo and build a proper data structure
543 my @args = $cgi->param('arg');
545 #work around konqueror bug!
546 @args = map { s/\x00$//; $_; } @args;
548 my $sub = $cgi->param('sub'); #????
550 warn "FS::UI::Web::JSRPC::process:\n".
553 " args=".join(', ',@args)."\n"
556 if ( $sub eq 'start_job' ) {
558 $self->start_job(@args);
560 } elsif ( $sub eq 'job_status' ) {
562 $self->job_status(@args);
566 die "unknown sub $sub";
575 warn "FS::UI::Web::start_job: ". join(', ', @_) if $DEBUG;
579 my( $field, $value ) = splice(@_, 0, 2);
580 unless ( exists( $param{$field} ) ) {
581 $param{$field} = $value;
582 } elsif ( ! ref($param{$field}) ) {
583 $param{$field} = [ $param{$field}, $value ];
585 push @{$param{$field}}, $value;
588 $param{CurrentUser} = getotaker();
589 $param{RootURL} = rooturl($self->{cgi}->self_url);
590 warn "FS::UI::Web::start_job\n".
592 if ( ref($param{$_}) ) {
593 " $_ => [ ". join(', ', @{$param{$_}}). " ]\n";
595 " $_ => $param{$_}\n";
600 #first get the CGI params shipped off to a job ASAP so an id can be returned
603 my $job = new FS::queue { 'job' => $self->{'job'} };
605 #too slow to insert all the cgi params as individual args..,?
606 #my $error = $queue->insert('_JOB', $cgi->Vars);
608 #warn 'froze string of size '. length(nfreeze(\%param)). " for job args\n"
611 my $error = $job->insert( '_JOB', encode_base64(nfreeze(\%param)) );
615 warn "job not inserted: $error\n"
618 $error; #this doesn't seem to be handled well,
619 # will trigger "illegal jobnum" below?
620 # (should never be an error inserting the job, though, only thing
621 # would be Pg f%*kage)
624 warn "job inserted successfully with jobnum ". $job->jobnum. "\n"
633 my( $self, $jobnum ) = @_; #$url ???
635 sleep 1; # XXX could use something better...
638 if ( $jobnum =~ /^(\d+)$/ ) {
639 $job = qsearchs('queue', { 'jobnum' => $jobnum } );
641 die "FS::UI::Web::job_status: illegal jobnum $jobnum\n";
645 if ( $job && $job->status ne 'failed' && $job->status ne 'done' ) {
646 my ($progress, $action) = split ',', $job->statustext, 2;
647 $action ||= 'Server processing job';
648 @return = ( 'progress', $progress, $action );
649 } elsif ( !$job ) { #handle job gone case : job successful
650 # so close popup, redirect parent window...
651 @return = ( 'complete' );
652 } elsif ( $job->status eq 'done' ) {
653 @return = ( 'done', $job->statustext, '' );
655 @return = ( 'error', $job ? $job->statustext : $jobnum );
658 #to_json(\@return); #waiting on deb 5.0 for new JSON.pm?
659 #silence the warning though
660 my $to_json = JSON->can('to_json') || JSON->can('objToJson');