so Search.tsf and Search.rdf work
[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   or $self->SUPER::check;
107
108 }
109
110 =item pkgpart_hashref
111
112 Returns a hash reference.  The keys of the hash are pkgparts.  The value is
113 true iff this agent may purchase the specified package definition.  See
114 L<FS::part_pkg>.
115
116 =cut
117
118 sub pkgpart_hashref {
119   my $self = shift;
120   my %pkgpart;
121   #$pkgpart{$_}++ foreach $self->pkgpart;
122   # not compatible w/5.004_04 (fixed in 5.004_05)
123   foreach ( $self->pkgpart ) { $pkgpart{$_}++; }
124   \%pkgpart;
125 }
126
127 =item type_pkgs
128
129 Returns all FS::type_pkgs objects (see L<FS::type_pkgs>) for this agent type.
130
131 =cut
132
133 sub type_pkgs {
134   my $self = shift;
135   qsearch('type_pkgs', { 'typenum' => $self->typenum } );
136 }
137
138 =item pkgpart
139
140 Returns the pkgpart of all package definitions (see L<FS::part_pkg>) for this
141 agent type.
142
143 =cut
144
145 sub pkgpart {
146   my $self = shift;
147   map $_->pkgpart, $self->type_pkgs;
148 }
149
150 =back
151
152 =head1 BUGS
153
154 =head1 SEE ALSO
155
156 L<FS::Record>, L<FS::agent>, L<FS::type_pkgs>, L<FS::cust_main>,
157 L<FS::part_pkg>, schema.html from the base documentation.
158
159 =cut
160
161 1;
162