fix otaker upgrade for cust_attachment & cust_main_note, hopefully
[freeside.git] / FS / FS / cust_main_note.pm
1 package FS::cust_main_note;
2
3 use strict;
4 use base qw( FS::otaker_Mixin FS::Record );
5 use FS::Record qw( qsearch qsearchs );
6
7 =head1 NAME
8
9 FS::cust_main_note - Object methods for cust_main_note records
10
11 =head1 SYNOPSIS
12
13   use FS::cust_main_note;
14
15   $record = new FS::cust_main_note \%hash;
16   $record = new FS::cust_main_note { 'column' => 'value' };
17
18   $error = $record->insert;
19
20   $error = $new_record->replace($old_record);
21
22   $error = $record->delete;
23
24   $error = $record->check;
25
26 =head1 DESCRIPTION
27
28 An FS::cust_main_note object represents a note attachted to a customer.
29 FS::cust_main_note inherits from FS::Record.  The following fields are
30 currently supported:
31
32 =over 4
33
34 =item notenum
35
36 primary key
37
38 =item custnum
39
40 =item _date
41
42 =item usernum
43
44 =item comments
45
46 =back
47
48 =head1 METHODS
49
50 =over 4
51
52 =item new HASHREF
53
54 Creates a new customer note.  To add the note to the database, see L<"insert">.
55
56 Note that this stores the hash reference, not a distinct copy of the hash it
57 points to.  You can ask the object for a copy with the I<hash> method.
58
59 =cut
60
61 # the new method can be inherited from FS::Record, if a table method is defined
62
63 sub table { 'cust_main_note'; }
64
65 =item insert
66
67 Adds this record to the database.  If there is an error, returns the error,
68 otherwise returns false.
69
70 =cut
71
72 # the insert method can be inherited from FS::Record
73
74 =item delete
75
76 Delete this record from the database.
77
78 =cut
79
80 # the delete method can be inherited from FS::Record
81
82 =item replace OLD_RECORD
83
84 Replaces the OLD_RECORD with this one in the database.  If there is an error,
85 returns the error, otherwise returns false.
86
87 =cut
88
89 # the replace method can be inherited from FS::Record
90
91 =item check
92
93 Checks all fields to make sure this is a valid example.  If there is
94 an error, returns the error, otherwise returns false.  Called by the insert
95 and replace methods.
96
97 =cut
98
99 # the check method should currently be supplied - FS::Record contains some
100 # data checking routines
101
102 sub check {
103   my $self = shift;
104
105   my $error = 
106     $self->ut_numbern('notenum')
107     || $self->ut_number('custnum')
108     || $self->ut_numbern('_date')
109     || $self->ut_textn('otaker')
110     || $self->ut_anything('comments')
111   ;
112   return $error if $error;
113
114   $self->SUPER::check;
115 }
116
117 #false laziness w/otaker_Mixin & cust_attachment
118 sub otaker {
119   my $self = shift;
120   if ( scalar(@_) ) { #set
121     my $otaker = shift;
122     my($l,$f) = (split(', ', $otaker));
123     my $access_user =  qsearchs('access_user', { 'username'=>$otaker }     )
124                     || qsearchs('access_user', { 'first'=>$f, 'last'=>$l } )
125       or croak "can't set otaker: $otaker not found!"; #confess?
126     $self->usernum( $access_user->usernum );
127     $otaker; #not sure return is used anywhere, but just in case
128   } else { #get
129     if ( $self->usernum ) {
130       $self->access_user->username;
131     } elsif ( length($self->get('otaker')) ) {
132       $self->get('otaker');
133     } else {
134       '';
135     }
136   }
137 }
138
139 # Used by FS::Upgrade to migrate to a new database.
140 sub _upgrade_data {  # class method
141   my ($class, %opts) = @_;
142   $class->_upgrade_otaker(%opts);
143 }
144
145 =back
146
147 =head1 BUGS
148
149 Lurking in the cracks.
150
151 =head1 SEE ALSO
152
153 L<FS::Record>, schema.html from the base documentation.
154
155 =cut
156
157 1;
158