so Search.tsf and Search.rdf work
[freeside.git] / httemplate / edit / cust_pkg.cgi
1 <!-- mason kludge -->
2 <%
3
4 my %pkg = ();
5 my %comment = ();
6 my %all_pkg = ();
7 my %all_comment = ();
8 #foreach (qsearch('part_pkg', { 'disabled' => '' })) {
9 #  $pkg{ $_ -> getfield('pkgpart') } = $_->getfield('pkg');
10 #  $comment{ $_ -> getfield('pkgpart') } = $_->getfield('comment');
11 #}
12 foreach (qsearch('part_pkg', {} )) {
13   $all_pkg{ $_ -> getfield('pkgpart') } = $_->getfield('pkg');
14   $all_comment{ $_ -> getfield('pkgpart') } = $_->getfield('comment');
15   next if $_->disabled;
16   $pkg{ $_ -> getfield('pkgpart') } = $_->getfield('pkg');
17   $comment{ $_ -> getfield('pkgpart') } = $_->getfield('comment');
18 }
19
20 my($custnum, %remove_pkg);
21 if ( $cgi->param('error') ) {
22   $custnum = $cgi->param('custnum');
23   %remove_pkg = map { $_ => 1 } $cgi->param('remove_pkg');
24 } else {
25   my($query) = $cgi->keywords;
26   $query =~ /^(\d+)$/;
27   $custnum = $1;
28   %remove_pkg = ();
29 }
30
31 my $p1 = popurl(1);
32 print header("Add/Edit Packages", '');
33
34 print qq!<FONT SIZE="+1" COLOR="#ff0000">Error: !, $cgi->param('error'),
35       "</FONT>"
36   if $cgi->param('error');
37
38 print qq!<FORM ACTION="${p1}process/cust_pkg.cgi" METHOD=POST>!;
39
40 print qq!<INPUT TYPE="hidden" NAME="custnum" VALUE="$custnum">!;
41
42 #current packages
43 my @cust_pkg = qsearch('cust_pkg',{ 'custnum' => $custnum, 'cancel' => '' } );
44
45 if (@cust_pkg) {
46   print <<END;
47 Current packages - select to remove (services are moved to a new package below)
48 <TABLE>
49   <TR STYLE="background-color: #cccccc;">
50     <TH COLSPAN="2">Pkg #</TH>
51     <TH>Package description</TH>
52   </TR>
53 <BR><BR>
54 END
55
56   foreach (sort { $all_pkg{$a->getfield('pkgpart')} cmp $all_pkg{$b->getfield('pkgpart')} } @cust_pkg) {
57     my($pkgnum,$pkgpart)=( $_->getfield('pkgnum'), $_->getfield('pkgpart') );
58     my $checked = $remove_pkg{$pkgnum} ? ' CHECKED' : '';
59     print <<END;
60   <TR>
61     <TD><INPUT TYPE="checkbox" NAME="remove_pkg" VALUE="$pkgnum"${checked}></TD>
62     <TD ALIGN="right">$pkgnum:</TD>\n
63     <TD>$all_pkg{$pkgpart} - $all_comment{$pkgpart}</TD>
64   </TR>
65 END
66   }
67   print qq!</TABLE><BR><BR>!;
68 }
69
70 print <<END;
71 Order new packages<BR><BR>
72 END
73
74 my $cust_main = qsearchs('cust_main',{'custnum'=>$custnum});
75 my $agent = qsearchs('agent',{'agentnum'=> $cust_main->agentnum });
76
77 my %agent_pkgs = map { ( $_->pkgpart , $all_pkg{$_->pkgpart} ) }
78                      qsearch('type_pkgs',{'typenum'=> $agent->typenum });
79
80 my $count = 0;
81 my $pkgparts = 0;
82 print <<END;
83 <TABLE>
84   <TR STYLE="background-color: #cccccc;">
85     <TH>Qty.</TH>
86     <TH COLSPAN="2">Package Description</TH>
87   </TR>
88 END
89 #foreach my $type_pkgs ( qsearch('type_pkgs',{'typenum'=> $agent->typenum }) ) {
90 foreach my $pkgpart ( sort { $agent_pkgs{$a} cmp $agent_pkgs{$b} }
91                              keys(%agent_pkgs) ) {
92   $pkgparts++;
93   next unless exists $pkg{$pkgpart}; #skip disabled ones
94   #print qq!<TR>! if ( $count == 0 );
95   my $value = $cgi->param("pkg$pkgpart") || 0;
96   print <<END;
97   <TR>
98     <TD><INPUT TYPE="text" NAME="pkg$pkgpart" VALUE="$value" SIZE="2" MAXLENGTH="2"></TD>
99     <TD ALIGN="right">$pkgpart:</TD>
100     <TD>$pkg{$pkgpart} - $comment{$pkgpart}</TD>
101   </TR>
102 END
103   $count ++ ;
104   #if ( $count == 2 ) {
105   #  print qq!</TR>\n! ;
106   #  $count = 0;
107   #}
108 }
109 print qq!</TABLE>!;
110
111 unless ( $pkgparts ) {
112   my $p2 = popurl(2);
113   my $typenum = $agent->typenum;
114   my $agent_type = qsearchs( 'agent_type', { 'typenum' => $typenum } );
115   my $atype = $agent_type->atype;
116   print <<END;
117 (No <a href="${p2}browse/part_pkg.cgi">package definitions</a>, or agent type
118 <a href="${p2}edit/agent_type.cgi?$typenum">$atype</a> not allowed to purchase
119 any packages.)
120 END
121 }
122
123 #submit
124 print <<END;
125 <P><INPUT TYPE="submit" VALUE="Order">
126     </FORM>
127   </BODY>
128 </HTML>
129 END
130 %>