RT# 83450 - fixed rateplan export
[freeside.git] / FS / FS / cust_attachment.pm
1 package FS::cust_attachment;
2
3 use strict;
4 use base qw( FS::cust_main_Mixin FS::otaker_Mixin FS::Record );
5 use Carp;
6 use FS::Record qw( qsearch qsearchs );
7 use FS::Conf;
8
9 =head1 NAME
10
11 FS::cust_attachment - Object methods for cust_attachment records
12
13 =head1 SYNOPSIS
14
15   use FS::cust_attachment;
16
17   $record = new FS::cust_attachment \%hash;
18   $record = new FS::cust_attachment { 'column' => 'value' };
19
20   $error = $record->insert;
21
22   $error = $new_record->replace($old_record);
23
24   $error = $record->delete;
25
26   $error = $record->check;
27
28 =head1 DESCRIPTION
29
30 An FS::cust_attachment object represents a file attached to a L<FS::cust_main>
31 object.  FS::cust_attachment inherits from FS::Record.  The following fields 
32 are currently supported:
33
34 =over 4
35
36 =item attachnum
37
38 Primary key (assigned automatically).
39
40 =item custnum
41
42 Customer number (see L<FS::cust_main>).
43
44 =item _date
45
46 The date the record was last updated.
47
48 =item usernum
49
50 Order taker (see L<FS::access_user>)
51
52 =item filename
53
54 The file's name.
55
56 =item mime_type
57
58 The Content-Type of the file.
59
60 =item body
61
62 The contents of the file.
63
64 =item disabled
65
66 If the attachment was disabled, this contains the date it was disabled.
67
68 =back
69
70 =head1 METHODS
71
72 =over 4
73
74 =item new HASHREF
75
76 Creates a new attachment object.
77
78 =cut
79
80 # the new method can be inherited from FS::Record, if a table method is defined
81
82 sub table { 'cust_attachment'; }
83
84 sub nohistory_fields { 'body'; }
85
86 =item insert
87
88 Adds this record to the database.  If there is an error, returns the error,
89 otherwise returns false.
90
91 =cut
92
93 =item delete
94
95 Delete this record from the database.
96
97 =cut
98
99 =item replace OLD_RECORD
100
101 Replaces the 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 # the replace method can be inherited from FS::Record
107
108 =item check
109
110 Checks all fields to make sure this is a valid example.  If there is
111 an error, returns the error, otherwise returns false.  Called by the insert
112 and replace methods.
113
114 =cut
115
116 # the check method should currently be supplied - FS::Record contains some
117 # data checking routines
118
119 sub check {
120   my $self = shift;
121
122   my $conf = new FS::Conf;
123   my $error;
124   if($conf->config('disable_cust_attachment') ) {
125     $error = 'Attachments disabled (see configuration)';
126   }
127
128   $error = 
129     $self->ut_numbern('attachnum')
130     || $self->ut_number('custnum')
131     || $self->ut_numbern('_date')
132     || $self->ut_textn('otaker')
133     || $self->ut_text('filename')
134     || $self->ut_text('mime_type')
135     || $self->ut_numbern('disabled')
136     || $self->ut_anything('body')
137   ;
138   if($conf->config('max_attachment_size') 
139     and $self->size > $conf->config('max_attachment_size') ) {
140     $error = 'Attachment too large'
141   }
142   return $error if $error;
143
144   $self->SUPER::check;
145 }
146
147 =item size
148
149 Returns the size of the attachment in bytes.
150
151 =cut
152
153 sub size {
154   my $self = shift;
155   return length($self->body);
156 }
157
158 #false laziness w/otaker_Mixin & cust_main_note
159 sub otaker {
160   my $self = shift;
161   if ( scalar(@_) ) { #set
162     my $otaker = shift;
163     my($l,$f) = (split(', ', $otaker));
164     my $access_user =  qsearchs('access_user', { 'username'=>$otaker }     )
165                     || qsearchs('access_user', { 'first'=>$f, 'last'=>$l } )
166       or croak "can't set otaker: $otaker not found!"; #confess?
167     $self->usernum( $access_user->usernum );
168     $otaker; #not sure return is used anywhere, but just in case
169   } else { #get
170     if ( $self->usernum ) {
171       $self->access_user->username;
172     } elsif ( length($self->get('otaker')) ) {
173       $self->get('otaker');
174     } else {
175       '';
176     }
177   }
178 }
179
180 # Used by FS::Upgrade to migrate to a new database.
181 sub _upgrade_data {  # class method
182   my ($class, %opts) = @_;
183   $class->_upgrade_otaker(%opts);
184 }
185
186 =back
187
188 =head1 BUGS
189
190 Doesn't work on non-Postgres systems.
191
192 =head1 SEE ALSO
193
194 L<FS::Record>, schema.html from the base documentation.
195
196 =cut
197
198 1;
199