4 use vars qw($DEBUG @ISA @EXPORT_OK $me);
6 use Carp qw( confess );
9 use FS::Misc::DateTime qw( parse_datetime day_end );
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 $ending = day_end( parse_datetime($1) );
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' => 'bill_address1',
238 'Address 2' => 'bill_address2',
239 'City' => 'bill_city',
240 'State' => 'bill_state',
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' => 'bill_address1',
247 '(bill) Address 2' => 'bill_address2',
248 '(bill) City' => 'bill_city',
249 '(bill) State' => 'bill_state',
250 '(bill) Zip' => 'bill_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;
340 #inefficientish, but tiny lists and only run once per page
343 foreach my $field (qw( address1 address2 city state zip )) {
344 foreach my $pre ('bill_','ship_') {
345 if ( grep { $_ eq $pre.$field } @cust_fields ) {
346 push @location_fields, $pre.'location.'.$field.' AS '.$pre.$field;
351 push @fields, 'payby' if grep { $_ eq 'payby'} @cust_fields;
352 push @fields, 'agent_custid';
354 my @extra_fields = ();
355 if (grep { $_ eq 'current_balance' } @cust_fields) {
356 push @extra_fields, FS::cust_main->balance_sql . " AS current_balance";
359 map("cust_main.$_", @fields), @location_fields, @extra_fields;
362 =item join_cust_main [ TABLE[.CUSTNUM] ] [ LOCATION_TABLE[.LOCATIONNUM] ]
364 Returns an SQL join phrase for the FROM clause so that the fields listed
365 in L<cust_sql_fields> will be available. Currently joins to cust_main
366 itself, as well as cust_location (under the aliases 'bill_location' and
367 'ship_location') if address fields are needed. L<cust_header()> should have
370 All of these will be left joins; if you want to exclude rows with no linked
371 cust_main record (or bill_location/ship_location), you can do so in the
374 TABLE is the table containing the custnum field. If CUSTNUM (a field name
375 in that table) is specified, that field will be joined to cust_main.custnum.
376 Otherwise, this function will assume the field is named "custnum". If the
377 argument isn't present at all, the join will just say "USING (custnum)",
380 As a special case, if TABLE is 'cust_main', only the joins to cust_location
383 LOCATION_TABLE is an optional table name to use for joining ship_location,
384 in case your query also includes package information and you want the
385 "service address" columns to reflect package addresses.
390 my ($cust_table, $location_table) = @_;
391 my ($custnum, $locationnum);
392 ($cust_table, $custnum) = split(/\./, $cust_table);
393 $custnum ||= 'custnum';
394 ($location_table, $locationnum) = split(/\./, $location_table);
395 $locationnum ||= 'locationnum';
399 $sql = " LEFT JOIN cust_main ON (cust_main.custnum = $cust_table.$custnum)"
400 unless $cust_table eq 'cust_main';
402 $sql = " LEFT JOIN cust_main USING (custnum)";
405 if ( !@cust_fields or grep /^bill_/, @cust_fields ) {
407 $sql .= ' LEFT JOIN cust_location bill_location'.
408 ' ON (bill_location.locationnum = cust_main.bill_locationnum)';
412 if ( !@cust_fields or grep /^ship_/, @cust_fields ) {
414 if (!$location_table) {
415 $location_table = 'cust_main';
416 $locationnum = 'ship_locationnum';
419 $sql .= ' LEFT JOIN cust_location ship_location'.
420 " ON (ship_location.locationnum = $location_table.$locationnum) ";
426 =item cust_fields OBJECT [ CUST_FIELDS_VALUE ]
428 Given an object that contains fields from cust_main (say, from a
429 JOINed search. See httemplate/search/svc_* for examples), returns an array
430 of customer information, or "(unlinked)" if this service is not linked to a
433 As with L<the cust_header subroutine|/cust_header>, the fields returned are
434 defined by the supplied customer fields setting, or if no customer fields
435 setting is supplied, the <B>cust-fields</B> configuration value.
442 warn "FS::UI::Web::cust_fields called for $record ".
443 "(cust_fields: @cust_fields)"
446 #cust_header(@_) unless @cust_fields; #now need to cache to keep cust_fields
447 # #override incase we were passed as a sub
449 my $seen_unlinked = 0;
452 if ( $record->custnum ) {
453 warn " $record -> $_" if $DEBUG > 1;
454 encode_entities( $record->$_(@_) );
456 warn " ($record unlinked)" if $DEBUG > 1;
457 $seen_unlinked++ ? '' : '(unlinked)';
462 =item cust_fields_subs
464 Returns an array of subroutine references for returning customer field values.
465 This is similar to cust_fields, but returns each field's sub as a distinct
470 sub cust_fields_subs {
471 my $unlinked_warn = 0;
474 if( $unlinked_warn++ ) {
477 if( $record->custnum ) {
488 $record->$f(@_) if $record->custnum;
496 Returns an array of subroutine references (or empty strings) for returning
497 customer information colors.
499 As with L<the cust_header subroutine|/cust_header>, the fields returned are
500 defined by the supplied customer fields setting, or if no customer fields
501 setting is supplied, the <B>cust-fields</B> configuration value.
509 sub { shift->$method(@_) };
518 Returns an array of customer information styles.
520 As with L<the cust_header subroutine|/cust_header>, the fields returned are
521 defined by the supplied customer fields setting, or if no customer fields
522 setting is supplied, the <B>cust-fields</B> configuration value.
538 Returns an array or scalar (depending on context) of customer information
541 As with L<the cust_header subroutine|/cust_header>, the fields returned are
542 defined by the supplied customer fields setting, or if no customer fields
543 setting is supplied, the <B>cust-fields</B> configuration value.
551 join('', @cust_aligns);
557 Utility function to determine if the client is a mobile browser.
562 my $ua = $ENV{'HTTP_USER_AGENT'} || '';
563 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 ) {
570 # begin JSRPC code...
573 package FS::UI::Web::JSRPC;
578 use Storable qw(nfreeze);
581 use FS::UID qw(getotaker);
582 use FS::Record qw(qsearchs);
584 use FS::CGI qw(rooturl);
598 croak "CGI object required as second argument" unless $self->{'cgi'};
607 my $cgi = $self->{'cgi'};
609 # XXX this should parse JSON foo and build a proper data structure
610 my @args = $cgi->param('arg');
612 #work around konqueror bug!
613 @args = map { s/\x00$//; $_; } @args;
615 my $sub = $cgi->param('sub'); #????
617 warn "FS::UI::Web::JSRPC::process:\n".
620 " args=".join(', ',@args)."\n"
623 if ( $sub eq 'start_job' ) {
625 $self->start_job(@args);
627 } elsif ( $sub eq 'job_status' ) {
629 $self->job_status(@args);
633 die "unknown sub $sub";
642 warn "FS::UI::Web::start_job: ". join(', ', @_) if $DEBUG;
646 my( $field, $value ) = splice(@_, 0, 2);
647 unless ( exists( $param{$field} ) ) {
648 $param{$field} = $value;
649 } elsif ( ! ref($param{$field}) ) {
650 $param{$field} = [ $param{$field}, $value ];
652 push @{$param{$field}}, $value;
655 $param{CurrentUser} = getotaker();
656 $param{RootURL} = rooturl($self->{cgi}->self_url);
657 warn "FS::UI::Web::start_job\n".
659 if ( ref($param{$_}) ) {
660 " $_ => [ ". join(', ', @{$param{$_}}). " ]\n";
662 " $_ => $param{$_}\n";
667 #first get the CGI params shipped off to a job ASAP so an id can be returned
670 my $job = new FS::queue { 'job' => $self->{'job'} };
672 #too slow to insert all the cgi params as individual args..,?
673 #my $error = $queue->insert('_JOB', $cgi->Vars);
675 #warn 'froze string of size '. length(nfreeze(\%param)). " for job args\n"
678 my $error = $job->insert( '_JOB', encode_base64(nfreeze(\%param)) );
682 warn "job not inserted: $error\n"
685 $error; #this doesn't seem to be handled well,
686 # will trigger "illegal jobnum" below?
687 # (should never be an error inserting the job, though, only thing
688 # would be Pg f%*kage)
691 warn "job inserted successfully with jobnum ". $job->jobnum. "\n"
700 my( $self, $jobnum ) = @_; #$url ???
702 sleep 1; # XXX could use something better...
705 if ( $jobnum =~ /^(\d+)$/ ) {
706 $job = qsearchs('queue', { 'jobnum' => $jobnum } );
708 die "FS::UI::Web::job_status: illegal jobnum $jobnum\n";
712 if ( $job && $job->status ne 'failed' && $job->status ne 'done' ) {
713 my ($progress, $action) = split ',', $job->statustext, 2;
714 $action ||= 'Server processing job';
715 @return = ( 'progress', $progress, $action );
716 } elsif ( !$job ) { #handle job gone case : job successful
717 # so close popup, redirect parent window...
718 @return = ( 'complete' );
719 } elsif ( $job->status eq 'done' ) {
720 @return = ( 'done', $job->statustext, '' );
722 @return = ( 'error', $job ? $job->statustext : $jobnum );
725 #to_json(\@return); #waiting on deb 5.0 for new JSON.pm?
726 #silence the warning though
727 my $to_json = JSON->can('to_json') || JSON->can('objToJson');