add ability to select specific package defs. and package status to package report...
[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 $DEBUG = 0;
12
13 use Date::Parse;
14 sub parse_beginning_ending {
15   my($cgi) = @_;
16
17   my $beginning = 0;
18   if ( $cgi->param('begin') =~ /^(\d+)$/ ) {
19     $beginning = $1;
20   } elsif ( $cgi->param('beginning') =~ /^([ 0-9\-\/]{1,64})$/ ) {
21     $beginning = str2time($1) || 0;
22   }
23
24   my $ending = 4294967295; #2^32-1
25   if ( $cgi->param('end') =~ /^(\d+)$/ ) {
26     $ending = $1 - 1;
27   } elsif ( $cgi->param('ending') =~ /^([ 0-9\-\/]{1,64})$/ ) {
28     #probably need an option to turn off the + 86399
29     $ending = str2time($1) + 86399;
30   }
31
32   ( $beginning, $ending );
33 }
34
35 ###
36 # cust_main report subroutines
37 ###
38
39
40 =item cust_header [ CUST_FIELDS_VALUE ]
41
42 Returns an array of customer information headers according to the supplied
43 customer fields value, or if no value is supplied, the B<cust-fields>
44 configuration value.
45
46 =cut
47
48 use vars qw( @cust_fields );
49
50 sub cust_header {
51
52   warn "FS::svc_Common::cust_header called"
53     if $DEBUG;
54
55   my %header2method = (
56     'Customer'                 => 'name',
57     'Cust#'                    => 'custnum',
58     'Name'                     => 'contact',
59     'Company'                  => 'company',
60     '(bill) Customer'          => 'name',
61     '(service) Customer'       => 'ship_name',
62     '(bill) Name'              => 'contact',
63     '(service) Name'           => 'ship_contact',
64     '(bill) Company'           => 'company',
65     '(service) Company'        => 'ship_company',
66     'Address 1'                => 'address1',
67     'Address 2'                => 'address2',
68     'City'                     => 'city',
69     'State'                    => 'state',
70     'Zip'                      => 'zip',
71     'Country'                  => 'country_full',
72     'Day phone'                => 'daytime', # XXX should use msgcat, but how?
73     'Night phone'              => 'night',   # XXX should use msgcat, but how?
74     'Invoicing email(s)'       => 'invoicing_list_emailonly',
75   );
76
77   my $cust_fields;
78   my @cust_header;
79   if ( @_ && $_[0] ) {
80
81     warn "  using supplied cust-fields override".
82           " (ignoring cust-fields config file)"
83       if $DEBUG;
84     $cust_fields = shift;
85
86   } else {
87
88     my $conf = new FS::Conf;
89     if (    $conf->exists('cust-fields')
90          && $conf->config('cust-fields') =~ /^([\w \|\#\(\)]+):?/
91        )
92     {
93       warn "  found cust-fields configuration value"
94         if $DEBUG;
95       $cust_fields = $1;
96     } else { 
97       warn "  no cust-fields configuration value found; using default 'Customer'"
98         if $DEBUG;
99       $cust_fields = 'Customer';
100     }
101   
102   }
103
104   @cust_header = split(/ \| /, $cust_fields);
105   @cust_fields = map { $header2method{$_} } @cust_header;
106
107   #my $svc_x = shift;
108   @cust_header;
109 }
110
111 =item cust_sql_fields [ CUST_FIELDS_VALUE ]
112
113 Returns a list of fields for the SELECT portion of an SQL query.
114
115 As with L<the cust_header subroutine|/cust_header>, the fields returned are
116 defined by the supplied customer fields setting, or if no customer fields
117 setting is supplied, the <B>cust-fields</B> configuration value. 
118
119 =cut
120
121 sub cust_sql_fields {
122
123   my @fields = qw( last first company );
124   push @fields, map "ship_$_", @fields;
125   push @fields, 'country';
126
127   cust_header(@_);
128   #inefficientish, but tiny lists and only run once per page
129   push @fields,
130     grep { my $field = $_; grep { $_ eq $field } @cust_fields }
131          qw( address1 address2 city state zip daytime night );
132
133   map "cust_main.$_", @fields;
134 }
135
136 =item cust_fields SVC_OBJECT [ CUST_FIELDS_VALUE ]
137
138 Given a svc_ object that contains fields from cust_main (say, from a
139 JOINed search.  See httemplate/search/svc_* for examples), returns an array
140 of customer information, or "(unlinked)" if this service is not linked to a
141 customer.
142
143 As with L<the cust_header subroutine|/cust_header>, the fields returned are
144 defined by the supplied customer fields setting, or if no customer fields
145 setting is supplied, the <B>cust-fields</B> configuration value. 
146
147 =cut
148
149 sub cust_fields {
150   my $svc_x = shift;
151   warn "FS::svc_Common::cust_fields called for $svc_x ".
152        "(cust_fields: @cust_fields)"
153     if $DEBUG > 1;
154
155   #cust_header(@_) unless @cust_fields; #now need to cache to keep cust_fields
156   #                                     #override incase we were passed as a sub
157
158   my $seen_unlinked = 0;
159   map { 
160     if ( $svc_x->custnum ) {
161       warn "  $svc_x -> $_"
162         if $DEBUG > 1;
163       $svc_x->$_(@_);
164     } else {
165       warn "  ($svc_x unlinked)"
166         if $DEBUG > 1;
167       $seen_unlinked++ ? '' : '(unlinked)';
168     }
169   } @cust_fields;
170 }
171
172 ###
173 # begin JSRPC code...
174 ###
175
176 package FS::UI::Web::JSRPC;
177
178 use strict;
179 use vars qw($DEBUG);
180 use Carp;
181 use Storable qw(nfreeze);
182 use MIME::Base64;
183 use JSON;
184 use FS::UID;
185 use FS::Record qw(qsearchs);
186 use FS::queue;
187
188 $DEBUG = 0;
189
190 sub new {
191         my $class = shift;
192         my $self  = {
193                 env => {},
194                 job => shift,
195                 cgi => shift,
196         };
197
198         bless $self, $class;
199
200         croak "CGI object required as second argument" unless $self->{'cgi'};
201
202         return $self;
203 }
204
205 sub process {
206
207   my $self = shift;
208
209   my $cgi = $self->{'cgi'};
210
211   # XXX this should parse JSON foo and build a proper data structure
212   my @args = $cgi->param('arg');
213
214   #work around konqueror bug!
215   @args = map { s/\x00$//; $_; } @args;
216
217   my $sub = $cgi->param('sub'); #????
218
219   warn "FS::UI::Web::JSRPC::process:\n".
220        "  cgi=$cgi\n".
221        "  sub=$sub\n".
222        "  args=".join(', ',@args)."\n"
223     if $DEBUG;
224
225   if ( $sub eq 'start_job' ) {
226
227     $self->start_job(@args);
228
229   } elsif ( $sub eq 'job_status' ) {
230
231     $self->job_status(@args);
232
233   } else {
234
235     die "unknown sub $sub";
236
237   }
238
239 }
240
241 sub start_job {
242   my $self = shift;
243
244   warn "FS::UI::Web::start_job: ". join(', ', @_) if $DEBUG;
245 #  my %param = @_;
246   my %param = ();
247   while ( @_ ) {
248     my( $field, $value ) = splice(@_, 0, 2);
249     unless ( exists( $param{$field} ) ) {
250       $param{$field} = $value;
251     } elsif ( ! ref($param{$field}) ) {
252       $param{$field} = [ $param{$field}, $value ];
253     } else {
254       push @{$param{$field}}, $value;
255     }
256   }
257   warn "FS::UI::Web::start_job\n".
258        join('', map {
259                       if ( ref($param{$_}) ) {
260                         "  $_ => [ ". join(', ', @{$param{$_}}). " ]\n";
261                       } else {
262                         "  $_ => $param{$_}\n";
263                       }
264                     } keys %param )
265     if $DEBUG;
266
267   #first get the CGI params shipped off to a job ASAP so an id can be returned
268   #to the caller
269   
270   my $job = new FS::queue { 'job' => $self->{'job'} };
271   
272   #too slow to insert all the cgi params as individual args..,?
273   #my $error = $queue->insert('_JOB', $cgi->Vars);
274   
275   #warn 'froze string of size '. length(nfreeze(\%param)). " for job args\n"
276   #  if $DEBUG;
277
278   my $error = $job->insert( '_JOB', encode_base64(nfreeze(\%param)) );
279
280   if ( $error ) {
281
282     warn "job not inserted: $error\n"
283       if $DEBUG;
284
285     $error;  #this doesn't seem to be handled well,
286              # will trigger "illegal jobnum" below?
287              # (should never be an error inserting the job, though, only thing
288              #  would be Pg f%*kage)
289   } else {
290
291     warn "job inserted successfully with jobnum ". $job->jobnum. "\n"
292       if $DEBUG;
293
294     $job->jobnum;
295   }
296   
297 }
298
299 sub job_status {
300   my( $self, $jobnum ) = @_; #$url ???
301
302   sleep 1; # XXX could use something better...
303
304   my $job;
305   if ( $jobnum =~ /^(\d+)$/ ) {
306     $job = qsearchs('queue', { 'jobnum' => $jobnum } );
307   } else {
308     die "FS::UI::Web::job_status: illegal jobnum $jobnum\n";
309   }
310
311   my @return;
312   if ( $job && $job->status ne 'failed' ) {
313     @return = ( 'progress', $job->statustext );
314   } elsif ( !$job ) { #handle job gone case : job successful
315                       # so close popup, redirect parent window...
316     @return = ( 'complete' );
317   } else {
318     @return = ( 'error', $job ? $job->statustext : $jobnum );
319   }
320
321   objToJson(\@return);
322
323 }
324
325 1;
326