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