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