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