This commit was generated by cvs2svn to compensate for changes in r11022,
[freeside.git] / FS / FS / cust_main_invoice.pm
1 package FS::cust_main_invoice;
2
3 use strict;
4 use vars qw(@ISA);
5 use Exporter;
6 use FS::Record qw( qsearchs );
7 use FS::Conf;
8 use FS::cust_main;
9 use FS::svc_acct;
10 use FS::Msgcat qw(gettext);
11
12 @ISA = qw( FS::Record );
13
14 =head1 NAME
15
16 FS::cust_main_invoice - Object methods for cust_main_invoice records
17
18 =head1 SYNOPSIS
19
20   use FS::cust_main_invoice;
21
22   $record = new FS::cust_main_invoice \%hash;
23   $record = new FS::cust_main_invoice { 'column' => 'value' };
24
25   $error = $record->insert;
26
27   $error = $new_record->replace($old_record);
28
29   $error = $record->delete;
30
31   $error = $record->check;
32
33   $email_address = $record->address;
34
35 =head1 DESCRIPTION
36
37 An FS::cust_main_invoice object represents an invoice destination.  FS::cust_main_invoice inherits from
38 FS::Record.  The following fields are currently supported:
39
40 =over 4
41
42 =item destnum - primary key
43
44 =item custnum - customer (see L<FS::cust_main>)
45
46 =item dest - Invoice destination: If numeric, a svcnum (see L<FS::svc_acct>), if string, a literal email address, `POST' to enable mailing (the default if no cust_main_invoice records exist), or `FAX' to enable faxing via a HylaFAX server.
47
48 =back
49
50 =head1 METHODS
51
52 =over 4
53
54 =item new HASHREF
55
56 Creates a new invoice destination.  To add the invoice destination to the database, see L<"insert">.
57
58 Note that this stores the hash reference, not a distinct copy of the hash it
59 points to.  You can ask the object for a copy with the I<hash> method.
60
61 =cut
62
63 sub table { 'cust_main_invoice'; }
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 =item delete
71
72 Delete this record from the database.
73
74 =item replace OLD_RECORD
75
76 Replaces the OLD_RECORD with this one in the database.  If there is an error,
77 returns the error, otherwise returns false.
78
79 =cut
80
81 sub replace {
82   my ( $new, $old ) = ( shift, shift );
83
84   return "Can't change custnum!" unless $old->custnum == $new->custnum;
85
86   $new->SUPER::replace($old);
87 }
88
89
90 =item check
91
92 Checks all fields to make sure this is a valid invoice destination.  If there is
93 an error, returns the error, otherwise returns false.  Called by the insert
94 and replace methods.
95
96 =cut
97
98 sub check {
99   my $self = shift;
100
101   my $error = $self->ut_numbern('destnum')
102            || $self->ut_number('custnum')
103            || $self->checkdest;
104   ;
105   return $error if $error;
106
107   return "Unknown customer"
108     unless qsearchs('cust_main',{ 'custnum' => $self->custnum });
109
110   $self->SUPER::check;
111 }
112
113 =item checkdest
114
115 Checks the dest field only.
116
117 #If it finds that the account ends in the
118 #same domain configured as the B<domain> configuration file, it will change the
119 #invoice destination from an email address to a service number (see
120 #L<FS::svc_acct>).
121
122 =cut
123
124 sub checkdest { 
125   my $self = shift;
126
127   my $error = $self->ut_text('dest');
128   return $error if $error;
129
130   my $conf = new FS::Conf;
131
132   if ( $self->dest =~ /^(POST|FAX)$/ ) {
133     #contemplate our navel
134   } elsif ( $self->dest =~ /^(\d+)$/ ) {
135     return "Unknown local account (specified by svcnum: ". $self->dest. ")"
136       unless qsearchs( 'svc_acct', { 'svcnum' => $self->dest } );
137   } elsif ( $conf->exists('emailinvoice-apostrophe')
138               ? $self->dest =~ /^\s*([\w\.\-\&\+\']+)\@(([\w\.\-]+\.)+\w+)\s*$/
139               : $self->dest =~ /^\s*([\w\.\-\&\+]+)\@(([\w\.\-]+\.)+\w+)\s*$/ ){
140     my($user, $domain) = ($1, $2);
141     $self->dest("$1\@$2");
142   } else {
143     return gettext("illegal_email_invoice_address"). ': '. $self->dest;
144   }
145
146   ''; #no error
147 }
148
149 =item address
150
151 Returns the literal email address for this record (or `POST' or `FAX').
152
153 =cut
154
155 sub address {
156   my $self = shift;
157   if ( $self->dest =~ /^(\d+)$/ ) {
158     my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $1 } )
159       or return undef;
160     $svc_acct->email;
161   } else {
162     $self->dest;
163   }
164 }
165
166 =item cust_main
167
168 Returns the parent customer object (see L<FS::cust_main>).
169
170 =cut
171
172 sub cust_main {
173   my $self = shift;
174   qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
175 }
176
177 =back
178
179 =head1 BUGS
180
181 =head1 SEE ALSO
182
183 L<FS::Record>, L<FS::cust_main>
184
185 =cut
186
187 1;
188