move signup server functions to self-service server. fix provisioning &
[freeside.git] / httemplate / edit / svc_forward.cgi
1 <!-- mason kludge -->
2 <%
3
4 my $conf = new FS::Conf;
5
6 my($svcnum, $pkgnum, $svcpart, $part_svc, $svc_forward);
7 if ( $cgi->param('error') ) {
8   $svc_forward = new FS::svc_forward ( {
9     map { $_, scalar($cgi->param($_)) } fields('svc_forward')
10   } );
11   $svcnum = $svc_forward->svcnum;
12   $pkgnum = $cgi->param('pkgnum');
13   $svcpart = $cgi->param('svcpart');
14   $part_svc=qsearchs('part_svc',{'svcpart'=>$svcpart});
15   die "No part_svc entry!" unless $part_svc;
16 } else {
17
18   my($query) = $cgi->keywords;
19
20   if ( $query =~ /^(\d+)$/ ) { #editing
21     $svcnum=$1;
22     $svc_forward=qsearchs('svc_forward',{'svcnum'=>$svcnum})
23       or die "Unknown (svc_forward) svcnum!";
24
25     my($cust_svc)=qsearchs('cust_svc',{'svcnum'=>$svcnum})
26       or die "Unknown (cust_svc) svcnum!";
27
28     $pkgnum=$cust_svc->pkgnum;
29     $svcpart=$cust_svc->svcpart;
30   
31     $part_svc=qsearchs('part_svc',{'svcpart'=>$svcpart});
32     die "No part_svc entry!" unless $part_svc;
33
34   } else { #adding
35
36     $svc_forward = new FS::svc_forward({});
37
38     foreach $_ (split(/-/,$query)) { #get & untaint pkgnum & svcpart
39       $pkgnum=$1 if /^pkgnum(\d+)$/;
40       $svcpart=$1 if /^svcpart(\d+)$/;
41     }
42     $part_svc=qsearchs('part_svc',{'svcpart'=>$svcpart});
43     die "No part_svc entry!" unless $part_svc;
44
45     $svcnum='';
46
47     #set fixed and default fields from part_svc
48     foreach my $part_svc_column (
49       grep { $_->columnflag } $part_svc->all_part_svc_column
50     ) {
51       $svc_forward->setfield( $part_svc_column->columnname,
52                               $part_svc_column->columnvalue,
53                             );
54     }
55   }
56
57 }
58 my $action = $svc_forward->svcnum ? 'Edit' : 'Add';
59
60 my %email;
61 if ($pkgnum) {
62
63   #find all possible user svcnums (and emails)
64
65   #starting with those currently attached
66   if ( $svc_forward->srcsvc ) {
67     my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $svc_forward->srcsvc } );
68     $email{$svc_forward->srcsvc} = $svc_acct->email;
69   }
70   if ( $svc_forward->dstsvc ) {
71     my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $svc_forward->dstsvc } );
72     $email{$svc_forward->dstsvc} = $svc_acct->email;
73   }
74
75   #and including the rest for this customer
76   my($u_part_svc,@u_acct_svcparts);
77   foreach $u_part_svc ( qsearch('part_svc',{'svcdb'=>'svc_acct'}) ) {
78     push @u_acct_svcparts,$u_part_svc->getfield('svcpart');
79   }
80
81   my($cust_pkg)=qsearchs('cust_pkg',{'pkgnum'=>$pkgnum});
82   my($custnum)=$cust_pkg->getfield('custnum');
83   my($i_cust_pkg);
84   foreach $i_cust_pkg ( qsearch('cust_pkg',{'custnum'=>$custnum}) ) {
85     my($cust_pkgnum)=$i_cust_pkg->getfield('pkgnum');
86     my($acct_svcpart);
87     foreach $acct_svcpart (@u_acct_svcparts) {   #now find the corresponding 
88                                               #record(s) in cust_svc ( for this
89                                               #pkgnum ! )
90       foreach my $i_cust_svc (
91         qsearch( 'cust_svc', { 'pkgnum'  => $cust_pkgnum,
92                                'svcpart' => $acct_svcpart } )
93       ) {
94         my $svc_acct =
95           qsearchs( 'svc_acct', { 'svcnum' => $i_cust_svc->svcnum } );
96         $email{$svc_acct->svcnum} = $svc_acct->email;
97       }  
98     }
99   }
100
101 } elsif ( $action eq 'Edit' ) {
102
103   my($svc_acct)=qsearchs('svc_acct',{'svcnum'=>$svc_forward->srcsvc});
104   $email{$svc_forward->srcsvc} = $svc_acct->email;
105
106   $svc_acct=qsearchs('svc_acct',{'svcnum'=>$svc_forward->dstsvc});
107   $email{$svc_forward->dstsvc} = $svc_acct->email;
108
109 } else {
110   die "\$action eq Add, but \$pkgnum is null!\n";
111 }
112
113 my($srcsvc,$dstsvc,$dst)=(
114   $svc_forward->srcsvc,
115   $svc_forward->dstsvc,
116   $svc_forward->dst,
117 );
118
119 #display
120
121 %>
122
123 <%= header("Mail Forward $action") %>
124
125 <% if ( $cgi->param('error') ) { %>
126   <FONT SIZE="+1" COLOR="#ff0000">Error: <%= $cgi->param('error') %></FONT>
127   <BR><BR>
128 <% } %>
129
130 Service #<%= $svcnum ? "<B>$svcnum</B>" : " (NEW)" %><BR>
131 Service: <B><%= $part_svc->svc %></B><BR><BR>
132
133 <FORM NAME="dummy">
134
135 <%= ntable("#cccccc",2) %>
136 <TR><TD ALIGN="right">Email to</TD><TD><SELECT NAME="srcsvc" SIZE=1>
137 <% foreach $_ (keys %email) { %>
138   <OPTION<%= $_ eq $srcsvc ? " SELECTED" : "" %> VALUE="<%= $_ %>"><%= $email{$_} %></OPTION>
139 <% } %>
140 </SELECT></TD></TR>
141
142 <%
143   tie my %tied_email, 'Tie::IxHash',
144     ''  => 'SELECT DESTINATION',
145     %email,
146     '0' => '(other email address)';
147   my $widget = new HTML::Widgets::SelectLayers(
148     'selected_layer' => $dstsvc,
149     'options'        => \%tied_email,
150     'form_name'      => 'dummy',
151     'form_action'    => 'process/svc_forward.cgi',
152     'form_select'    => ['srcsvc'],
153     'html_between'   => '</TD></TR></TABLE>',
154     'layer_callback' => sub {
155       my $layer = shift;
156       my $html = qq!<INPUT TYPE="hidden" NAME="svcnum" VALUE="$svcnum">!.
157                  qq!<INPUT TYPE="hidden" NAME="pkgnum" VALUE="$pkgnum">!.
158                  qq!<INPUT TYPE="hidden" NAME="svcpart" VALUE="$svcpart">!.
159                  qq!<INPUT TYPE="hidden" NAME="dstsvc" VALUE="$layer">!;
160       if ( $layer eq '0' ) {
161         $html .= ntable("#cccccc",2).
162                  '<TR><TD ALIGN="right">Destination email</TD>'.
163                  qq!<TD><INPUT TYPE="text" NAME="dst" VALUE="$dst"></TD>!.
164                  '</TR></TABLE>';
165       }
166       $html .= '<BR><INPUT TYPE="submit" VALUE="Submit">';
167       $html;
168     },
169   );
170 %>
171
172 <TR><TD ALIGN="right">Forwards to</TD>
173 <TD><%= $widget->html %>
174   </BODY>
175 </HTML>