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