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 my($svc, $label, $svcdb) = $cust_svc->label;
139 svc_X_link( $label, @_ );
143 my ($x, $m, $part_svc, $cust_svc) = @_ or return '';
146 unless $FS::CurrentUser::CurrentUser->access_right('View customer services');
148 confess "svc_X_link called without a service ($x, $m, $part_svc, $cust_svc)\n"
155 'part_svc' => $part_svc,
162 #this probably needs an ACL too...
163 sub svc_export_links {
164 my ($m, $part_svc, $cust_svc) = @_ or return '';
166 my $ahref = $cust_svc->export_links;
172 my($cgi, $field) = @_;
181 foreach my $op (keys %op) {
183 warn "checking for ${field}_$op field\n"
186 if ( $cgi->param($field."_$op") =~ /^\s*\$?\s*(-?[\d\,\s]+(\.\d\d)?)\s*$/ ) {
189 $num =~ s/[\,\s]+//g;
190 my $search = "$field $op{$op} $num";
191 push @search, $search;
193 warn "found ${field}_$op field; adding search element $search\n"
204 # cust_main report subroutines
208 =item cust_header [ CUST_FIELDS_VALUE ]
210 Returns an array of customer information headers according to the supplied
211 customer fields value, or if no value is supplied, the B<cust-fields>
216 use vars qw( @cust_fields @cust_colors @cust_styles @cust_aligns );
220 warn "FS::UI:Web::cust_header called"
223 my $conf = new FS::Conf;
225 my %header2method = (
226 'Customer' => 'name',
227 'Cust. Status' => 'ucfirst_cust_status',
228 'Cust#' => 'custnum',
230 'Company' => 'company',
231 '(bill) Customer' => 'name',
232 '(service) Customer' => 'ship_name',
233 '(bill) Name' => 'contact',
234 '(service) Name' => 'ship_contact',
235 '(bill) Company' => 'company',
236 '(service) Company' => 'ship_company',
237 'Address 1' => 'address1',
238 'Address 2' => 'address2',
242 'Country' => 'country_full',
243 'Day phone' => 'daytime', # XXX should use msgcat, but how?
244 'Night phone' => 'night', # XXX should use msgcat, but how?
245 'Fax number' => 'fax',
246 '(bill) Address 1' => 'address1',
247 '(bill) Address 2' => 'address2',
248 '(bill) City' => 'city',
249 '(bill) State' => 'state',
250 '(bill) Zip' => 'zip',
251 '(bill) Country' => 'country_full',
252 '(bill) Day phone' => 'daytime', # XXX should use msgcat, but how?
253 '(bill) Night phone' => 'night', # XXX should use msgcat, but how?
254 '(bill) Fax number' => 'fax',
255 '(service) Address 1' => 'ship_address1',
256 '(service) Address 2' => 'ship_address2',
257 '(service) City' => 'ship_city',
258 '(service) State' => 'ship_state',
259 '(service) Zip' => 'ship_zip',
260 '(service) Country' => 'ship_country_full',
261 '(service) Day phone' => 'ship_daytime', # XXX should use msgcat, how?
262 '(service) Night phone' => 'ship_night', # XXX should use msgcat, how?
263 '(service) Fax number' => 'ship_fax',
264 'Invoicing email(s)' => 'invoicing_list_emailonly_scalar',
265 'Payment Type' => 'payby',
266 'Current Balance' => 'current_balance',
268 $header2method{'Cust#'} = 'display_custnum'
269 if $conf->exists('cust_main-default_agent_custid');
271 my %header2colormethod = (
272 'Cust. Status' => 'cust_statuscolor',
275 'Cust. Status' => 'b',
278 'Cust. Status' => 'c',
286 warn " using supplied cust-fields override".
287 " (ignoring cust-fields config file)"
289 $cust_fields = shift;
293 if ( $conf->exists('cust-fields')
294 && $conf->config('cust-fields') =~ /^([\w\. \|\#\(\)]+):?/
297 warn " found cust-fields configuration value"
301 warn " no cust-fields configuration value found; using default 'Cust. Status | Customer'"
303 $cust_fields = 'Cust. Status | Customer';
308 @cust_header = split(/ \| /, $cust_fields);
309 @cust_fields = map { $header2method{$_} || $_ } @cust_header;
310 @cust_colors = map { exists $header2colormethod{$_}
311 ? $header2colormethod{$_}
315 @cust_styles = map { exists $header2style{$_} ? $header2style{$_} : '' }
317 @cust_aligns = map { exists $header2align{$_} ? $header2align{$_} : 'l' }
324 =item cust_sql_fields [ CUST_FIELDS_VALUE ]
326 Returns a list of fields for the SELECT portion of an SQL query.
328 As with L<the cust_header subroutine|/cust_header>, the fields returned are
329 defined by the supplied customer fields setting, or if no customer fields
330 setting is supplied, the <B>cust-fields</B> configuration value.
334 sub cust_sql_fields {
336 my @fields = qw( last first company );
337 push @fields, map "ship_$_", @fields;
338 push @fields, 'country';
341 #inefficientish, but tiny lists and only run once per page
343 my @add_fields = qw( address1 address2 city state zip daytime night fax );
345 grep { my $field = $_; grep { $_ eq $field } @cust_fields }
346 ( @add_fields, ( map "ship_$_", @add_fields ), 'payby' );
348 push @fields, 'agent_custid';
350 my @extra_fields = ();
351 if (grep { $_ eq 'current_balance' } @cust_fields) {
352 push @extra_fields, FS::cust_main->balance_sql . " AS current_balance";
355 map("cust_main.$_", @fields), @extra_fields;
358 =item cust_fields OBJECT [ CUST_FIELDS_VALUE ]
360 Given an object that contains fields from cust_main (say, from a
361 JOINed search. See httemplate/search/svc_* for examples), returns an array
362 of customer information, or "(unlinked)" if this service is not linked to a
365 As with L<the cust_header subroutine|/cust_header>, the fields returned are
366 defined by the supplied customer fields setting, or if no customer fields
367 setting is supplied, the <B>cust-fields</B> configuration value.
374 warn "FS::UI::Web::cust_fields called for $record ".
375 "(cust_fields: @cust_fields)"
378 #cust_header(@_) unless @cust_fields; #now need to cache to keep cust_fields
379 # #override incase we were passed as a sub
381 my $seen_unlinked = 0;
384 if ( $record->custnum ) {
385 warn " $record -> $_" if $DEBUG > 1;
388 warn " ($record unlinked)" if $DEBUG > 1;
389 $seen_unlinked++ ? '' : '(unlinked)';
394 =item cust_fields_subs
396 Returns an array of subroutine references for returning customer field values.
397 This is similar to cust_fields, but returns each field's sub as a distinct
402 sub cust_fields_subs {
403 my $unlinked_warn = 0;
406 if( $unlinked_warn++ ) {
409 if( $record->custnum ) {
420 $record->$f(@_) if $record->custnum;
428 Returns an array of subroutine references (or empty strings) for returning
429 customer information colors.
431 As with L<the cust_header subroutine|/cust_header>, the fields returned are
432 defined by the supplied customer fields setting, or if no customer fields
433 setting is supplied, the <B>cust-fields</B> configuration value.
441 sub { shift->$method(@_) };
450 Returns an array of customer information styles.
452 As with L<the cust_header subroutine|/cust_header>, the fields returned are
453 defined by the supplied customer fields setting, or if no customer fields
454 setting is supplied, the <B>cust-fields</B> configuration value.
470 Returns an array or scalar (depending on context) of customer information
473 As with L<the cust_header subroutine|/cust_header>, the fields returned are
474 defined by the supplied customer fields setting, or if no customer fields
475 setting is supplied, the <B>cust-fields</B> configuration value.
483 join('', @cust_aligns);
489 Utility function to determine if the client is a mobile browser.
494 my $ua = $ENV{'HTTP_USER_AGENT'} || '';
495 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 ) {
502 # begin JSRPC code...
505 package FS::UI::Web::JSRPC;
510 use Storable qw(nfreeze);
513 use FS::UID qw(getotaker);
514 use FS::Record qw(qsearchs);
516 use FS::CGI qw(rooturl);
530 croak "CGI object required as second argument" unless $self->{'cgi'};
539 my $cgi = $self->{'cgi'};
541 # XXX this should parse JSON foo and build a proper data structure
542 my @args = $cgi->param('arg');
544 #work around konqueror bug!
545 @args = map { s/\x00$//; $_; } @args;
547 my $sub = $cgi->param('sub'); #????
549 warn "FS::UI::Web::JSRPC::process:\n".
552 " args=".join(', ',@args)."\n"
555 if ( $sub eq 'start_job' ) {
557 $self->start_job(@args);
559 } elsif ( $sub eq 'job_status' ) {
561 $self->job_status(@args);
565 die "unknown sub $sub";
574 warn "FS::UI::Web::start_job: ". join(', ', @_) if $DEBUG;
578 my( $field, $value ) = splice(@_, 0, 2);
579 unless ( exists( $param{$field} ) ) {
580 $param{$field} = $value;
581 } elsif ( ! ref($param{$field}) ) {
582 $param{$field} = [ $param{$field}, $value ];
584 push @{$param{$field}}, $value;
587 $param{CurrentUser} = getotaker();
588 $param{RootURL} = rooturl($self->{cgi}->self_url);
589 warn "FS::UI::Web::start_job\n".
591 if ( ref($param{$_}) ) {
592 " $_ => [ ". join(', ', @{$param{$_}}). " ]\n";
594 " $_ => $param{$_}\n";
599 #first get the CGI params shipped off to a job ASAP so an id can be returned
602 my $job = new FS::queue { 'job' => $self->{'job'} };
604 #too slow to insert all the cgi params as individual args..,?
605 #my $error = $queue->insert('_JOB', $cgi->Vars);
607 #warn 'froze string of size '. length(nfreeze(\%param)). " for job args\n"
610 my $error = $job->insert( '_JOB', encode_base64(nfreeze(\%param)) );
614 warn "job not inserted: $error\n"
617 $error; #this doesn't seem to be handled well,
618 # will trigger "illegal jobnum" below?
619 # (should never be an error inserting the job, though, only thing
620 # would be Pg f%*kage)
623 warn "job inserted successfully with jobnum ". $job->jobnum. "\n"
632 my( $self, $jobnum ) = @_; #$url ???
634 sleep 1; # XXX could use something better...
637 if ( $jobnum =~ /^(\d+)$/ ) {
638 $job = qsearchs('queue', { 'jobnum' => $jobnum } );
640 die "FS::UI::Web::job_status: illegal jobnum $jobnum\n";
644 if ( $job && $job->status ne 'failed' && $job->status ne 'done' ) {
645 my ($progress, $action) = split ',', $job->statustext, 2;
646 $action ||= 'Server processing job';
647 @return = ( 'progress', $progress, $action );
648 } elsif ( !$job ) { #handle job gone case : job successful
649 # so close popup, redirect parent window...
650 @return = ( 'complete' );
651 } elsif ( $job->status eq 'done' ) {
652 @return = ( 'done', $job->statustext, '' );
654 @return = ( 'error', $job ? $job->statustext : $jobnum );
657 #to_json(\@return); #waiting on deb 5.0 for new JSON.pm?