fix TeleAPI import (what kind of crack was Christopher smoking that he couldn't fix...
[freeside.git] / FS / FS / agent_type.pm
1 package FS::agent_type;
2
3 use strict;
4 use vars qw( @ISA );
5 use FS::Record qw( qsearch dbh );
6 use FS::m2m_Common;
7 use FS::agent;
8 use FS::type_pkgs;
9
10 @ISA = qw( FS::m2m_Common FS::Record );
11
12 =head1 NAME
13
14 FS::agent_type - Object methods for agent_type records
15
16 =head1 SYNOPSIS
17
18   use FS::agent_type;
19
20   $record = new FS::agent_type \%hash;
21   $record = new FS::agent_type { 'column' => 'value' };
22
23   $error = $record->insert;
24
25   $error = $new_record->replace($old_record);
26
27   $error = $record->delete;
28
29   $error = $record->check;
30
31   $hashref = $record->pkgpart_hashref;
32   #may purchase $pkgpart if $hashref->{$pkgpart};
33
34   @type_pkgs = $record->type_pkgs;
35
36   @pkgparts = $record->pkgpart;
37
38 =head1 DESCRIPTION
39
40 An FS::agent_type object represents an agent type.  Every agent (see
41 L<FS::agent>) has an agent type.  Agent types define which packages (see
42 L<FS::part_pkg>) may be purchased by customers (see L<FS::cust_main>), via 
43 FS::type_pkgs records (see L<FS::type_pkgs>).  FS::agent_type inherits from
44 FS::Record.  The following fields are currently supported:
45
46 =over 4
47
48 =item typenum
49
50 primary key (assigned automatically for new agent types)
51
52 =item atype
53
54 Text name of this agent type
55
56 =item disabled
57
58 Disabled flag, empty or 'Y'
59
60 =back
61
62 =head1 METHODS
63
64 =over 4
65
66 =item new HASHREF
67
68 Creates a new agent type.  To add the agent type to the database, see
69 L<"insert">.
70
71 =cut
72
73 sub table { 'agent_type'; }
74
75 =item insert
76
77 Adds this agent type to the database.  If there is an error, returns the error,
78 otherwise returns false.
79
80 =item delete
81
82 Deletes this agent type from the database.  Only agent types with no agents
83 can be deleted.  If there is an error, returns the error, otherwise returns
84 false.
85
86 =cut
87
88 sub delete {
89   my $self = shift;
90
91   return "Can't delete an agent_type with agents!"
92     if qsearch( 'agent', { 'typenum' => $self->typenum } );
93
94   $self->SUPER::delete;
95 }
96
97 =item replace OLD_RECORD
98
99 Replaces OLD_RECORD with this one in the database.  If there is an error,
100 returns the error, otherwise returns false.
101
102 =item check
103
104 Checks all fields to make sure this is a valid agent type.  If there is an
105 error, returns the error, otherwise returns false.  Called by the insert and
106 replace methods.
107
108 =cut
109
110 sub check {
111   my $self = shift;
112
113   $self->ut_numbern('typenum')
114     || $self->ut_text('atype')
115     || $self->ut_enum('disabled', [ '', 'Y' ] )
116     || $self->SUPER::check;
117
118 }
119
120 =item pkgpart_hashref
121
122 Returns a hash reference.  The keys of the hash are pkgparts.  The value is
123 true iff this agent may purchase the specified package definition.  See
124 L<FS::part_pkg>.
125
126 =cut
127
128 sub pkgpart_hashref {
129   my $self = shift;
130   my %pkgpart;
131   $pkgpart{$_}++ foreach $self->pkgpart;
132   \%pkgpart;
133 }
134
135 =item type_pkgs
136
137 Returns all FS::type_pkgs objects (see L<FS::type_pkgs>) for this agent type.
138
139 =cut
140
141 sub type_pkgs {
142   my $self = shift;
143   qsearch('type_pkgs', { 'typenum' => $self->typenum } );
144 }
145
146 =item type_pkgs_enabled
147
148 Returns all FS::type_pkg objects (see L<FS::type_pkgs>) that link to enabled
149 package definitions (see L<FS::part_pkg>).
150
151 An additional strange feature is that the returned type_pkg objects also have
152 all fields of the associated part_pkg object.
153
154 =cut
155
156 sub type_pkgs_enabled {
157   my $self = shift;
158   qsearch({
159     'table'     => 'type_pkgs',
160     'addl_from' => 'JOIN part_pkg USING ( pkgpart )',
161     'hashref'   => { 'typenum' => $self->typenum },
162     'extra_sql' => " AND ( disabled = '' OR disabled IS NULL )".
163                    " ORDER BY pkg",
164   });
165 }
166
167 =item pkgpart
168
169 Returns the pkgpart of all package definitions (see L<FS::part_pkg>) for this
170 agent type.
171
172 =cut
173
174 sub pkgpart {
175   my $self = shift;
176
177   #map $_->pkgpart, $self->type_pkgs;
178
179   my $sql = 'SELECT pkgpart FROM type_pkgs WHERE typenum = ?';
180   my $sth = dbh->prepare($sql)    or die  dbh->errstr;
181   $sth->execute( $self->typenum ) or die $sth->errstr;
182   map $_->[0], @{ $sth->fetchall_arrayref };
183 }
184
185 =back
186
187 =head1 BUGS
188
189 type_pkgs_enabled should order itself by something (pkg?)
190
191 type_pkgs_enabled should populate something that caches for the part_pkg method
192 rather than add fields to this object, right?  In fact we need a "poop" object
193 framework that does that automatically for any joined search at some point....
194 right?
195
196 =head1 SEE ALSO
197
198 L<FS::Record>, L<FS::agent>, L<FS::type_pkgs>, L<FS::cust_main>,
199 L<FS::part_pkg>, schema.html from the base documentation.
200
201 =cut
202
203 1;
204