add pkey to batch payments and fix a doc typo
[freeside.git] / FS / FS / svc_acct_pop.pm
1 package FS::svc_acct_pop;
2
3 use strict;
4 use vars qw( @ISA @EXPORT_OK @svc_acct_pop %svc_acct_pop );
5 use FS::Record qw( qsearch qsearchs );
6
7 @ISA = qw( FS::Record Exporter );
8 @EXPORT_OK = qw( popselector );
9
10 =head1 NAME
11
12 FS::svc_acct_pop - Object methods for svc_acct_pop records
13
14 =head1 SYNOPSIS
15
16   use FS::svc_acct_pop;
17
18   $record = new FS::svc_acct_pop \%hash;
19   $record = new FS::svc_acct_pop { 'column' => 'value' };
20
21   $error = $record->insert;
22
23   $error = $new_record->replace($old_record);
24
25   $error = $record->delete;
26
27   $error = $record->check;
28
29   $html = FS::svc_acct_pop::popselector( $popnum, $state );
30
31 =head1 DESCRIPTION
32
33 An FS::svc_acct object represents an point of presence.  FS::svc_acct_pop
34 inherits from FS::Record.  The following fields are currently supported:
35
36 =over 4
37
38 =item popnum - primary key (assigned automatically for new accounts)
39
40 =item city
41
42 =item state
43
44 =item ac - area code
45
46 =item exch - exchange
47
48 =item loc - rest of number
49
50 =back
51
52 =head1 METHODS
53
54 =over 4
55
56 =item new HASHREF
57
58 Creates a new point of presence (if only it were that easy!).  To add the 
59 point of presence to the database, see L<"insert">.
60
61 =cut
62
63 sub table { 'svc_acct_pop'; }
64
65 =item insert
66
67 Adds this point of presence to the database.  If there is an error, returns the
68 error, otherwise returns false.
69
70 =item delete
71
72 Removes this point of presence from the database.
73
74 =item replace OLD_RECORD
75
76 Replaces OLD_RECORD with this one in the database.  If there is an error,
77 returns the error, otherwise returns false.
78
79 =item check
80
81 Checks all fields to make sure this is a valid point of presence.  If there is
82 an error, returns the error, otherwise returns false.  Called by the insert
83 and replace methods.
84
85 =cut
86
87 sub check {
88   my $self = shift;
89
90     $self->ut_numbern('popnum')
91       or $self->ut_text('city')
92       or $self->ut_text('state')
93       or $self->ut_number('ac')
94       or $self->ut_number('exch')
95       or $self->ut_numbern('loc')
96   ;
97
98 }
99
100 =item text
101
102 Returns:
103
104 "$city, $state ($ac)/$exch"
105
106 =cut
107
108 sub text {
109   my $self = shift;
110   $self->city. ', '. $self->state. ' ('. $self->ac. ')/'. $self->exch;
111 }
112
113 =back
114
115 =head1 SUBROUTINES
116
117 =over 4
118
119 =item popselector [ POPNUM [ STATE ] ]
120
121 =cut
122
123 sub popselector {
124   my( $popnum, $state ) = @_;
125
126   unless ( @svc_acct_pop ) { #cache pop list
127     @svc_acct_pop = qsearch('svc_acct_pop', {} );
128     %svc_acct_pop = ();
129     push @{$svc_acct_pop{$_->state}}, $_ foreach @svc_acct_pop;
130   }
131
132   my $text = <<END;
133     <SCRIPT>
134     function opt(what,href,text) {
135       var optionName = new Option(text, href, false, false)
136       var length = what.length;
137       what.options[length] = optionName;
138     }
139     
140     function popstate_changed(what) {
141       state = what.options[what.selectedIndex].text;
142       for (var i = what.form.popnum.length;i > 0;i--)
143                 what.form.popnum.options[i] = null;
144       what.form.popnum.options[0] = new Option("", "", false, true);
145 END
146
147   foreach my $popstate ( sort { $a cmp $b } keys %svc_acct_pop ) {
148     $text .= "\nif ( state == \"$popstate\" ) {\n";
149
150     foreach my $pop ( @{$svc_acct_pop{$popstate}}) {
151       my $o_popnum = $pop->popnum;
152       my $poptext = $pop->text;
153       $text .= "opt(what.form.popnum, \"$o_popnum\", \"$poptext\");\n"
154     }
155     $text .= "}\n";
156   }
157
158   $text .= "}\n</SCRIPT>\n";
159
160   $text .=
161     qq!<SELECT NAME="popstate" SIZE=1 onChange="popstate_changed(this)">!.
162     qq!<OPTION> !;
163   $text .= "<OPTION>$_" foreach sort { $a cmp $b } keys %svc_acct_pop;
164   $text .= '</SELECT>'; #callback? return 3 html pieces?  #'</TD><TD>';
165
166   $text .= qq!<SELECT NAME="popnum" SIZE=1><OPTION> !;
167   foreach my $pop ( @svc_acct_pop ) {
168     $text .= qq!<OPTION VALUE="!. $pop->popnum. '"'.
169              ( ( $popnum && $pop->popnum == $popnum ) ? ' SELECTED' : '' ). ">".
170              $pop->text;
171   }
172   $text .= '</SELECT>';
173
174   $text;
175
176 }
177
178 =back
179
180 =head1 VERSION
181
182 $Id: svc_acct_pop.pm,v 1.5 2001-09-27 21:49:31 ivan Exp $
183
184 =head1 BUGS
185
186 It should be renamed to part_pop.
187
188 popselector?  putting web ui components in here?  they should probably live
189 somewhere else...
190
191 =head1 SEE ALSO
192
193 L<FS::Record>, L<FS::svc_acct>, L<FS::part_pop_local>, schema.html from the
194 base documentation.
195
196 =cut
197
198 1;
199