first part of ACL and re-skinning work and some other small stuff
[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::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 - primary key (assigned automatically for new agent types)
49
50 =item atype - Text name of this agent type
51
52 =back
53
54 =head1 METHODS
55
56 =over 4
57
58 =item new HASHREF
59
60 Creates a new agent type.  To add the agent type to the database, see
61 L<"insert">.
62
63 =cut
64
65 sub table { 'agent_type'; }
66
67 =item insert
68
69 Adds this agent type to the database.  If there is an error, returns the error,
70 otherwise returns false.
71
72 =item delete
73
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
76 false.
77
78 =cut
79
80 sub delete {
81   my $self = shift;
82
83   return "Can't delete an agent_type with agents!"
84     if qsearch( 'agent', { 'typenum' => $self->typenum } );
85
86   $self->SUPER::delete;
87 }
88
89 =item replace OLD_RECORD
90
91 Replaces OLD_RECORD with this one in the database.  If there is an error,
92 returns the error, otherwise returns false.
93
94 =item check
95
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
98 replace methods.
99
100 =cut
101
102 sub check {
103   my $self = shift;
104
105   $self->ut_numbern('typenum')
106   or $self->ut_text('atype')
107   or $self->SUPER::check;
108
109 }
110
111 =item pkgpart_hashref
112
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
115 L<FS::part_pkg>.
116
117 =cut
118
119 sub pkgpart_hashref {
120   my $self = shift;
121   my %pkgpart;
122   #$pkgpart{$_}++ foreach $self->pkgpart;
123   # not compatible w/5.004_04 (fixed in 5.004_05)
124   foreach ( $self->pkgpart ) { $pkgpart{$_}++; }
125   \%pkgpart;
126 }
127
128 =item type_pkgs
129
130 Returns all FS::type_pkgs objects (see L<FS::type_pkgs>) for this agent type.
131
132 =cut
133
134 sub type_pkgs {
135   my $self = shift;
136   qsearch('type_pkgs', { 'typenum' => $self->typenum } );
137 }
138
139 =item pkgpart
140
141 Returns the pkgpart of all package definitions (see L<FS::part_pkg>) for this
142 agent type.
143
144 =cut
145
146 sub pkgpart {
147   my $self = shift;
148   map $_->pkgpart, $self->type_pkgs;
149 }
150
151 =back
152
153 =head1 BUGS
154
155 =head1 SEE ALSO
156
157 L<FS::Record>, L<FS::agent>, L<FS::type_pkgs>, L<FS::cust_main>,
158 L<FS::part_pkg>, schema.html from the base documentation.
159
160 =cut
161
162 1;
163