b82e2e73215736bd0c12eac6602082063369bb16
[freeside.git] / FS / FS / cust_main_invoice.pm
1 package FS::cust_main_invoice;
2
3 use strict;
4 use vars qw(@ISA $conf);
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, or `POST' to enable mailing (the default if no cust_main_invoice records exist)
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 repalce 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   ''; #noerror
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   if ( $self->dest eq 'POST' ) {
131     #contemplate our navel
132   } elsif ( $self->dest =~ /^(\d+)$/ ) {
133     return "Unknown local account (specified by svcnum: ". $self->dest. ")"
134       unless qsearchs( 'svc_acct', { 'svcnum' => $self->dest } );
135   } elsif ( $self->dest =~ /^([\w\.\-\&\+]+)\@(([\w\.\-]+\.)+\w+)$/ ) {
136     my($user, $domain) = ($1, $2);
137 #    if ( $domain eq $mydomain ) {
138 #      my $svc_acct = qsearchs( 'svc_acct', { 'username' => $user } );
139 #      return "Unknown local account: $user\@$domain (specified literally)"
140 #        unless $svc_acct;
141 #      $svc_acct->svcnum =~ /^(\d+)$/ or die "Non-numeric svcnum?!";
142 #      $self->dest($1);
143 #    }
144     $self->dest("$1\@$2");
145   } else {
146     return gettext("illegal_email_invoice_address");
147   }
148
149   ''; #no error
150 }
151
152 =item address
153
154 Returns the literal email address for this record (or `POST').
155
156 =cut
157
158 sub address {
159   my $self = shift;
160   if ( $self->dest =~ /^(\d+)$/ ) {
161     my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $1 } )
162       or return undef;
163     $svc_acct->email;
164   } else {
165     $self->dest;
166   }
167 }
168
169 =back
170
171 =head1 VERSION
172
173 $Id: cust_main_invoice.pm,v 1.11 2002-04-10 13:42:48 ivan Exp $
174
175 =head1 BUGS
176
177 =head1 SEE ALSO
178
179 L<FS::Record>, L<FS::cust_main>
180
181 =cut
182
183 1;
184