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