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