add stack backtrace to fatal problems in virtual field check
[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   $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   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     $self->dest("$1\@$2");
138   } else {
139     return gettext("illegal_email_invoice_address");
140   }
141
142   ''; #no error
143 }
144
145 =item address
146
147 Returns the literal email address for this record (or `POST').
148
149 =cut
150
151 sub address {
152   my $self = shift;
153   if ( $self->dest =~ /^(\d+)$/ ) {
154     my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $1 } )
155       or return undef;
156     $svc_acct->email;
157   } else {
158     $self->dest;
159   }
160 }
161
162 =back
163
164 =head1 VERSION
165
166 $Id: cust_main_invoice.pm,v 1.14 2003-08-05 00:20:42 khoff Exp $
167
168 =head1 BUGS
169
170 =head1 SEE ALSO
171
172 L<FS::Record>, L<FS::cust_main>
173
174 =cut
175
176 1;
177