1 package FS::agent_type;
5 use FS::Record qw( qsearch dbh );
10 @ISA = qw( FS::m2m_Common FS::Record );
14 FS::agent_type - Object methods for agent_type records
20 $record = new FS::agent_type \%hash;
21 $record = new FS::agent_type { 'column' => 'value' };
23 $error = $record->insert;
25 $error = $new_record->replace($old_record);
27 $error = $record->delete;
29 $error = $record->check;
31 $hashref = $record->pkgpart_hashref;
32 #may purchase $pkgpart if $hashref->{$pkgpart};
34 @type_pkgs = $record->type_pkgs;
36 @pkgparts = $record->pkgpart;
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:
48 =item typenum - primary key (assigned automatically for new agent types)
50 =item atype - Text name of this agent type
60 Creates a new agent type. To add the agent type to the database, see
65 sub table { 'agent_type'; }
69 Adds this agent type to the database. If there is an error, returns the error,
70 otherwise returns false.
74 Deletes this agent type from the database. Only agent types with no agents
75 can be deleted. If there is an error, returns the error, otherwise returns
83 return "Can't delete an agent_type with agents!"
84 if qsearch( 'agent', { 'typenum' => $self->typenum } );
89 =item replace OLD_RECORD
91 Replaces OLD_RECORD with this one in the database. If there is an error,
92 returns the error, otherwise returns false.
96 Checks all fields to make sure this is a valid agent type. If there is an
97 error, returns the error, otherwise returns false. Called by the insert and
105 $self->ut_numbern('typenum')
106 or $self->ut_text('atype')
107 or $self->SUPER::check;
111 =item pkgpart_hashref
113 Returns a hash reference. The keys of the hash are pkgparts. The value is
114 true iff this agent may purchase the specified package definition. See
119 sub pkgpart_hashref {
122 $pkgpart{$_}++ foreach $self->pkgpart;
128 Returns all FS::type_pkgs objects (see L<FS::type_pkgs>) for this agent type.
134 qsearch('type_pkgs', { 'typenum' => $self->typenum } );
137 =item type_pkgs_enabled
139 Returns all FS::type_pkg objects (see L<FS::type_pkgs>) that link to enabled
140 package definitions (see L<FS::part_pkg>).
142 An additional strange feature is that the returned type_pkg objects also have
143 all fields of the associated part_pkg object.
147 sub type_pkgs_enabled {
150 'table' => 'type_pkgs',
151 'addl_from' => 'JOIN part_pkg USING ( pkgpart )',
152 'hashref' => { 'typenum' => $self->typenum },
153 'extra_sql' => " AND ( disabled = '' OR disabled IS NULL )".
160 Returns the pkgpart of all package definitions (see L<FS::part_pkg>) for this
168 #map $_->pkgpart, $self->type_pkgs;
170 my $sql = 'SELECT pkgpart FROM type_pkgs WHERE typenum = ?';
171 my $sth = dbh->prepare($sql) or die dbh->errstr;
172 $sth->execute( $self->typenum ) or die $sth->errstr;
173 map $_->[0], @{ $sth->fetchall_arrayref };
180 type_pkgs_enabled should order itself by something (pkg?)
182 type_pkgs_enabled should populate something that caches for the part_pkg method
183 rather than add fields to this object, right? In fact we need a "poop" object
184 framework that does that automatically for any joined search at some point....
189 L<FS::Record>, L<FS::agent>, L<FS::type_pkgs>, L<FS::cust_main>,
190 L<FS::part_pkg>, schema.html from the base documentation.