working textradius export
[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 );
6 use FS::agent;
7 use FS::type_pkgs;
8
9 @ISA = qw( FS::Record );
10
11 =head1 NAME
12
13 FS::agent_type - Object methods for agent_type records
14
15 =head1 SYNOPSIS
16
17   use FS::agent_type;
18
19   $record = new FS::agent_type \%hash;
20   $record = new FS::agent_type { 'column' => 'value' };
21
22   $error = $record->insert;
23
24   $error = $new_record->replace($old_record);
25
26   $error = $record->delete;
27
28   $error = $record->check;
29
30   $hashref = $record->pkgpart_hashref;
31   #may purchase $pkgpart if $hashref->{$pkgpart};
32
33   @type_pkgs = $record->type_pkgs;
34
35   @pkgparts = $record->pkgpart;
36
37 =head1 DESCRIPTION
38
39 An FS::agent_type object represents an agent type.  Every agent (see
40 L<FS::agent>) has an agent type.  Agent types define which packages (see
41 L<FS::part_pkg>) may be purchased by customers (see L<FS::cust_main>), via 
42 FS::type_pkgs records (see L<FS::type_pkgs>).  FS::agent_type inherits from
43 FS::Record.  The following fields are currently supported:
44
45 =over 4
46
47 =item typenum - primary key (assigned automatically for new agent types)
48
49 =item atype - Text name of this agent type
50
51 =back
52
53 =head1 METHODS
54
55 =over 4
56
57 =item new HASHREF
58
59 Creates a new agent type.  To add the agent type to the database, see
60 L<"insert">.
61
62 =cut
63
64 sub table { 'agent_type'; }
65
66 =item insert
67
68 Adds this agent type to the database.  If there is an error, returns the error,
69 otherwise returns false.
70
71 =item delete
72
73 Deletes this agent type from the database.  Only agent types with no agents
74 can be deleted.  If there is an error, returns the error, otherwise returns
75 false.
76
77 =cut
78
79 sub delete {
80   my $self = shift;
81
82   return "Can't delete an agent_type with agents!"
83     if qsearch( 'agent', { 'typenum' => $self->typenum } );
84
85   $self->SUPER::delete;
86 }
87
88 =item replace OLD_RECORD
89
90 Replaces OLD_RECORD with this one in the database.  If there is an error,
91 returns the error, otherwise returns false.
92
93 =item check
94
95 Checks all fields to make sure this is a valid agent type.  If there is an
96 error, returns the error, otherwise returns false.  Called by the insert and
97 replace methods.
98
99 =cut
100
101 sub check {
102   my $self = shift;
103
104   $self->ut_numbern('typenum')
105   or $self->ut_text('atype');
106
107 }
108
109 =item pkgpart_hashref
110
111 Returns a hash reference.  The keys of the hash are pkgparts.  The value is
112 true iff this agent may purchase the specified package definition.  See
113 L<FS::part_pkg>.
114
115 =cut
116
117 sub pkgpart_hashref {
118   my $self = shift;
119   my %pkgpart;
120   #$pkgpart{$_}++ foreach $self->pkgpart;
121   # not compatible w/5.004_04 (fixed in 5.004_05)
122   foreach ( $self->pkgpart ) { $pkgpart{$_}++; }
123   \%pkgpart;
124 }
125
126 =item type_pkgs
127
128 Returns all FS::type_pkgs objects (see L<FS::type_pkgs>) for this agent type.
129
130 =cut
131
132 sub type_pkgs {
133   my $self = shift;
134   qsearch('type_pkgs', { 'typenum' => $self->typenum } );
135 }
136
137 =item pkgpart
138
139 Returns the pkgpart of all package definitions (see L<FS::part_pkg>) for this
140 agent type.
141
142 =cut
143
144 sub pkgpart {
145   my $self = shift;
146   map $_->pkgpart, $self->type_pkgs;
147 }
148
149 =back
150
151 =head1 VERSION
152
153 $Id: agent_type.pm,v 1.1 1999-08-04 09:03:53 ivan Exp $
154
155 =head1 BUGS
156
157 =head1 SEE ALSO
158
159 L<FS::Record>, L<FS::agent>, L<FS::type_pkgs>, L<FS::cust_main>,
160 L<FS::part_pkg>, schema.html from the base documentation.
161
162 =cut
163
164 1;
165