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