no default default_dir (ironic)
[freeside.git] / site_perl / agent_type.pm
1 package FS::agent_type;
2
3 use strict;
4 use vars qw(@ISA @EXPORT_OK);
5 use Exporter;
6 use FS::Record qw(qsearch fields);
7
8 @ISA = qw(FS::Record Exporter);
9 @EXPORT_OK = qw(fields);
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 = create FS::agent_type \%hash;
20   $record = create 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 =head1 DESCRIPTION
31
32 An FS::agent_type object represents an agent type.  Every agent (see
33 L<FS::agent>) has an agent type.  Agent types define which packages (see
34 L<FS::part_pkg>) may be purchased by customers (see L<FS::cust_main>), via 
35 FS::type_pkgs records (see L<FS::type_pkgs>).  FS::agent_type inherits from
36 FS::Record.  The following fields are currently supported:
37
38 =over 4
39
40 =item typenum - primary key (assigned automatically for new agent types)
41
42 =item atype - Text name of this agent type
43
44 =back
45
46 =head1 METHODS
47
48 =over 4
49
50 =item create HASHREF
51
52 Creates a new agent type.  To add the agent type to the database, see
53 L<"insert">.
54
55 =cut
56
57 sub create {
58   my($proto,$hashref)=@_;
59
60   #now in FS::Record::new
61   #my($field);
62   #foreach $field (fields('agent_type')) {
63   #  $hashref->{$field}='' unless defined $hashref->{$field};
64   #}
65
66   $proto->new('agent_type',$hashref);
67
68 }
69
70 =item insert
71
72 Adds this agent type to the database.  If there is an error, returns the error,
73 otherwise returns false.
74
75 =cut
76
77 sub insert {
78   my($self)=@_;
79
80   $self->check or
81   $self->add;
82 }
83
84 =item delete
85
86 Deletes this agent type from the database.  Only agent types with no agents
87 can be deleted.  If there is an error, returns the error, otherwise returns
88 false.
89
90 =cut
91
92 sub delete {
93   my($self)=@_;
94   return "Can't delete an agent_type with agents!"
95     if qsearch('agent',{'typenum' => $self->typenum});
96   $self->del;
97 }
98
99 =item replace OLD_RECORD
100
101 Replaces OLD_RECORD with this one in the database.  If there is an error,
102 returns the error, otherwise returns false.
103
104 =cut
105
106 sub replace {
107   my($new,$old)=@_;
108   return "(Old) Not a agent_type record!" unless $old->table eq "agent_type";
109   return "Can't change typenum!"   
110     unless $old->getfield('typenum') eq $new->getfield('typenum');
111   $new->check or
112   $new->rep($old);
113 }
114
115 =item check
116
117 Checks all fields to make sure this is a valid agent type.  If there is an
118 error, returns the error, otherwise returns false.  Called by the insert and
119 replace methods.
120
121 =cut
122
123 sub check {
124   my($self)=@_;
125   return "Not a agent_type record!" unless $self->table eq "agent_type";
126
127   $self->ut_numbern('typenum')
128   or $self->ut_text('atype');
129
130 }
131
132 =back
133
134 =head1 BUGS
135
136 It doesn't properly override FS::Record yet.
137
138 =head1 SEE ALSO
139
140 L<FS::Record>, L<FS::agent>, L<FS::type_pkgs>, L<FS::cust_main>,
141 L<FS::part_pkg>, schema.html from the base documentation.
142
143 =head1 HISTORY
144
145 Class for the different sets of allowable packages you can assign to an
146 agent.
147
148 ivan@sisd.com 97-nov-13
149
150 ut_ FS::Record methods
151 ivan@sisd.com 97-dec-10
152
153 Changed 'type' to 'atype' because Pg6.3 reserves the type word
154         bmccane@maxbaud.net     98-apr-3
155
156 pod, added check in delete ivan@sisd.com 98-sep-21
157
158 =cut
159
160 1;
161