493e0c0c29a6fec8ee02614892006011de46e275
[freeside.git] / FS / FS / ClientAPI / MasonComponent.pm
1 package FS::ClientAPI::MasonComponent;
2
3 use strict;
4 use vars qw( $cache $DEBUG $me );
5 use subs qw( _cache );
6 use FS::Mason qw( mason_interps );
7 use FS::Conf;
8 use FS::ClientAPI_SessionCache;
9 use FS::Record qw( qsearch qsearchs );
10 use FS::cust_main;
11 use FS::part_pkg;
12
13 $DEBUG = 0;
14 $me = '[FS::ClientAPI::MasonComponent]';
15
16 my %allowed_comps = map { $_=>1 } qw(
17   /elements/customer-statement.html
18   /elements/select-did.html
19   /misc/areacodes.cgi
20   /misc/exchanges.cgi
21   /misc/phonenums.cgi
22   /misc/states.cgi
23   /misc/counties.cgi
24   /misc/cities.cgi
25   /misc/svc_acct-domains.cgi
26   /misc/part_svc-columns.cgi
27   /edit/elements/svc_forward.html
28 );
29
30 my %session_comps = map { $_=>1 } qw(
31   /elements/location.html
32   /elements/tr-amount_fee.html
33   /elements/select-part_pkg.html
34   /edit/cust_main/first_pkg/select-part_pkg.html
35 );
36
37 my %session_callbacks = (
38
39   '/elements/location.html' => sub {
40     my( $custnum, $argsref ) = @_;
41     my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
42       or return "unknown custnum $custnum";
43     my %args = @$argsref;
44     $args{object} = $cust_main->bill_location;
45     @$argsref = ( %args );
46     return ''; #no error
47   },
48
49   '/elements/tr-amount_fee.html' => sub {
50     my( $custnum, $argsref ) = @_;
51
52     my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
53       or return "unknown custnum $custnum";
54
55     my $conf = new FS::Conf;
56
57     my %args = @$argsref;
58     %args = (
59       %args,
60       'process-pkgpart'    =>
61         scalar($conf->config('selfservice_process-pkgpart', $cust_main->agentnum)),
62       'process-display'    => scalar($conf->config('selfservice_process-display')),
63       'process-skip_first' => $conf->exists('selfservice_process-skip_first'),
64       'num_payments'       => scalar($cust_main->cust_pay), 
65       'surcharge_percentage' => scalar($conf->config('credit-card-surcharge-percentage')),
66     );
67     @$argsref = ( %args );
68
69     return ''; #no error
70   },
71
72   '/edit/cust_main/first_pkg/select-part_pkg.html' => sub {
73     my( $custnum, $argsref ) = @_;
74     my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
75       or return "unknown custnum $custnum";
76
77     my $pkgpart = $cust_main->agent->pkgpart_hashref;
78
79     #false laziness w/ edit/cust_main/first_pkg.html
80     my @first_svc = ( 'svc_acct', 'svc_phone' );
81
82     my @part_pkg =
83       grep { $_->svcpart(\@first_svc)
84              && ( $pkgpart->{ $_->pkgpart } 
85                   || ( $_->agentnum && $_->agentnum == $cust_main->agentnum )
86                 )
87            }
88       qsearch( 'part_pkg', { 'disabled' => '' }, '', 'ORDER BY pkg' ); # case?
89
90     my $conf = new FS::Conf;
91     if ( $conf->exists('pkg-addon_classnum') ) {
92
93       my %classnum = map  { ( $_->addon_classnum => 1 ) }
94                      grep { $_->freq !~ /^0/ }
95                      map  { $_->part_pkg }
96                           $cust_main->ncancelled_pkgs;
97
98       unless ( $classnum{''} || ! keys %classnum ) {
99         @part_pkg = grep $classnum{ $_->classnum }, @part_pkg;
100       }
101     }
102
103     my %args = @$argsref;
104     $args{part_pkg} = \@part_pkg;
105     $args{first_svc} = \@first_svc;
106     $args{no_comment} = 1;
107     $args{label_callback} = sub { shift->pkg_comment };
108     @$argsref = ( %args );
109     return ''; #no error
110
111   },
112
113   '/elements/select-part_pkg.html' => sub {
114     my( $custnum, $argsref ) = @_;
115     my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
116       or return "unknown custnum $custnum";
117
118     my $pkgpart = $cust_main->agent->pkgpart_hashref;
119
120     #false laziness w/ edit/cust_main/first_pkg.html
121     my @first_svc = ( 'svc_acct', 'svc_phone' );
122
123     my @part_pkg =
124       grep { $pkgpart->{ $_->pkgpart } 
125                   || ( $_->agentnum && $_->agentnum == $cust_main->agentnum )
126            }
127       qsearch( 'part_pkg', { 'disabled' => '' }, '', 'ORDER BY pkg' ); # case?
128
129     push @$argsref, 'part_pkg' =>  \@part_pkg;
130     '';
131   },
132
133 );
134
135 my $outbuf;
136 my( $fs_interp, $rt_interp ) = mason_interps('standalone', 'outbuf'=>\$outbuf);
137
138 sub mason_comp {
139   my $packet = shift;
140
141   warn "$me mason_comp called on $packet\n" if $DEBUG;
142
143   my $comp = $packet->{'comp'};
144   unless ( $allowed_comps{$comp} || $session_comps{$comp} ) {
145     return { 'error' => 'Illegal component' };
146   }
147
148   my @args = $packet->{'args'} ? @{ $packet->{'args'} } : ();
149
150   if ( $session_comps{$comp} ) {
151
152     my $session = _cache->get($packet->{'session_id'})
153       or return ( 'error' => "Can't resume session" ); #better error message
154     my $custnum = $session->{'custnum'};
155
156     my $error = &{ $session_callbacks{$comp} }( $custnum, \@args );
157     return { 'error' => $error } if $error;
158
159   }
160
161   my $conf = new FS::Conf;
162   $FS::Mason::Request::FSURL = $conf->config('selfservice_server-base_url');
163   $FS::Mason::Request::FSURL .= '/' unless $FS::Mason::Request::FSURL =~ /\/$/;
164   $FS::Mason::Request::QUERY_STRING = $packet->{'query_string'} || '';
165
166   $outbuf = '';
167   $fs_interp->exec($comp, @args); #only FS for now alas...
168
169   #errors? (turn off in-line error reporting?)
170
171   return { 'output' => $outbuf };
172
173 }
174
175 #hmm
176 sub _cache {
177   $cache ||= new FS::ClientAPI_SessionCache( {
178                'namespace' => 'FS::ClientAPI::MyAccount',
179              } );
180 }
181
182 1;