clean up some leftover bits from cust-fields work
[freeside.git] / FS / FS / UI / Web.pm
1 package FS::UI::Web;
2
3 use vars qw($DEBUG);
4 use FS::Conf;
5 use FS::Record qw(dbdef);
6
7 #use vars qw(@ISA);
8 #use FS::UI
9 #@ISA = qw( FS::UI );
10
11 use Date::Parse;
12 sub parse_beginning_ending {
13   my($cgi) = @_;
14
15   $cgi->param('beginning') =~ /^([ 0-9\-\/]{0,10})$/;
16   my $beginning = str2time($1) || 0;
17
18   #need an option to turn off the + 86399 ???
19   $cgi->param('ending') =~ /^([ 0-9\-\/]{0,10})$/;
20   my $ending =  ( $1 ? str2time($1) : 4294880896 ) + 86399;
21
22   ( $beginning, $ending );
23 }
24
25 ###
26 # cust_main report methods
27 ###
28
29 =item cust_header
30
31 Returns an array of customer information headers according to the
32 B<cust-fields> configuration setting.
33
34 =cut
35
36 use vars qw( @cust_fields );
37
38 sub cust_sql_fields {
39   my @fields = qw( last first company );
40   push @fields, map "ship_$_", @fields
41     if dbdef->table('cust_main')->column('ship_last');
42   map "cust_main.$_", @fields;
43 }
44
45 sub cust_header {
46
47   warn "FS::svc_Common::cust_header called"
48     if $DEBUG;
49
50   my $conf = new FS::Conf;
51
52   my %header2method = (
53     'Customer'           => 'name',
54     'Cust#'              => 'custnum',
55     'Name'               => 'contact',
56     'Company'            => 'company',
57     '(bill) Customer'    => 'name',
58     '(service) Customer' => 'ship_name',
59     '(bill) Name'        => 'contact',
60     '(service) Name'     => 'ship_contact',
61     '(bill) Company'     => 'company',
62     '(service) Company'  => 'ship_company',
63   );
64
65   my @cust_header;
66   if (    $conf->exists('cust-fields')
67        && $conf->config('cust-fields') =~ /^([\w \|\#\(\)]+):/
68      )
69   {
70     warn "  found cust-fields configuration value"
71       if $DEBUG;
72
73     my $cust_fields = $1;
74      @cust_header = split(/ \| /, $cust_fields);
75      @cust_fields = map { $header2method{$_} } @cust_header;
76   } else { 
77     warn "  no cust-fields configuration value found; using default 'Customer'"
78       if $DEBUG;
79     @cust_header = ( 'Customer' );
80     @cust_fields = ( 'name' );
81   }
82
83   #my $svc_x = shift;
84   @cust_header;
85 }
86
87 =item cust_fields
88
89 Given a svc_ object that contains fields from cust_main (say, from a
90 JOINed search.  See httemplate/search/svc_* for examples), returns an array
91 of customer information according to the <B>cust-fields</B> configuration
92 setting, or "(unlinked)" if this service is not linked to a customer.
93
94 =cut
95
96 sub cust_fields {
97   my $svc_x = shift;
98   warn "FS::svc_Common::cust_fields called for $svc_x ".
99        "(cust_fields: @cust_fields)"
100     if $DEBUG > 1;
101
102   cust_header() unless @cust_fields;
103
104   my $seen_unlinked = 0;
105   map { 
106     if ( $svc_x->custnum ) {
107       warn "  $svc_x -> $_"
108         if $DEBUG > 1;
109       $svc_x->$_(@_);
110     } else {
111       warn "  ($svc_x unlinked)"
112         if $DEBUG > 1;
113       $seen_unlinked++ ? '' : '(unlinked)';
114     }
115   } @cust_fields;
116 }
117
118 ###
119 # begin JSRPC code...
120 ###
121
122 package FS::UI::Web::JSRPC;
123
124 use strict;
125 use vars qw(@ISA $DEBUG);
126 use Storable qw(nfreeze);
127 use MIME::Base64;
128 use JavaScript::RPC::Server::CGI;
129 use FS::UID;
130 use FS::Record qw(qsearchs);
131 use FS::queue;
132
133 @ISA = qw( JavaScript::RPC::Server::CGI );
134 $DEBUG = 0;
135
136 sub new {
137         my $class = shift;
138         my $self  = {
139                 env => {},
140                 job => shift,
141         };
142
143         bless $self, $class;
144
145         return $self;
146 }
147
148 sub start_job {
149   my $self = shift;
150
151   warn "FS::UI::Web::start_job: ". join(', ', @_) if $DEBUG;
152 #  my %param = @_;
153   my %param = ();
154   while ( @_ ) {
155     my( $field, $value ) = splice(@_, 0, 2);
156     unless ( exists( $param{$field} ) ) {
157       $param{$field} = $value;
158     } elsif ( ! ref($param{$field}) ) {
159       $param{$field} = [ $param{$field}, $value ];
160     } else {
161       push @{$param{$field}}, $value;
162     }
163   }
164   warn "FS::UI::Web::start_job\n".
165        join('', map {
166                       if ( ref($param{$_}) ) {
167                         "  $_ => [ ". join(', ', @{$param{$_}}). " ]\n";
168                       } else {
169                         "  $_ => $param{$_}\n";
170                       }
171                     } keys %param )
172     if $DEBUG;
173
174   #first get the CGI params shipped off to a job ASAP so an id can be returned
175   #to the caller
176   
177   my $job = new FS::queue { 'job' => $self->{'job'} };
178   
179   #too slow to insert all the cgi params as individual args..,?
180   #my $error = $queue->insert('_JOB', $cgi->Vars);
181   
182   #warn 'froze string of size '. length(nfreeze(\%param)). " for job args\n"
183   #  if $DEBUG;
184
185   my $error = $job->insert( '_JOB', encode_base64(nfreeze(\%param)) );
186
187   if ( $error ) {
188     $error;
189   } else {
190     $job->jobnum;
191   }
192   
193 }
194
195 sub job_status {
196   my( $self, $jobnum ) = @_; #$url ???
197
198   sleep 5; #could use something better...
199
200   my $job;
201   if ( $jobnum =~ /^(\d+)$/ ) {
202     $job = qsearchs('queue', { 'jobnum' => $jobnum } );
203   } else {
204     die "FS::UI::Web::job_status: illegal jobnum $jobnum\n";
205   }
206
207   my @return;
208   if ( $job && $job->status ne 'failed' ) {
209     @return = ( 'progress', $job->statustext );
210   } elsif ( !$job ) { #handle job gone case : job sucessful
211                       # so close popup, redirect parent window...
212     @return = ( 'complete' );
213   } else {
214     @return = ( 'error', $job ? $job->statustext : $jobnum );
215   }
216
217   join("\n",@return);
218
219 }
220
221 sub get_new_query {
222   FS::UID::cgi();
223 }
224
225 1;
226