library support for editing email invoice destinations (not in sub collect yet)
[freeside.git] / site_perl / 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(qsearch qsearchs);
7 use FS::Conf;
8
9 @ISA = qw(FS::Record);
10
11 #ask FS::UID to run this stuff for us later
12 $FS::UID::callback{'FS::cust_main_invoice'} = sub { 
13   $conf = new FS::Conf;
14   $mydomain = $conf->config('domain');
15 };
16
17 =head1 NAME
18
19 FS::cust_main_invoice - Object methods for cust_main_invoice records
20
21 =head1 SYNOPSIS
22
23   use FS::cust_main_invoice;
24
25   $record = create FS::cust_main_invoice \%hash;
26   $record = create FS::cust_main_invoice { 'column' => 'value' };
27
28   $error = $record->insert;
29
30   $error = $new_record->replace($old_record);
31
32   $error = $record->delete;
33
34   $error = $record->check;
35
36   $email_address = $record->address;
37
38 =head1 DESCRIPTION
39
40 An FS::cust_main_invoice object represents an invoice destination.  FS::cust_main_invoice inherits from
41 FS::Record.  The following fields are currently supported:
42
43 =over 4
44
45 =item destnum - primary key
46
47 =item custnum - customer (see L<FS::cust_main>)
48
49 =item dest - Invoice destination: If numeric, a <a href="#svc_acct">svcnum</a>, if string, a literal email address, or `POST' to enable mailing (the default if no cust_main_invoice records exist)
50
51 =back
52
53 =head1 METHODS
54
55 =over 4
56
57 =item create HASHREF
58
59 Creates a new invoice destination.  To add the invoice destination to the database, see L<"insert">.
60
61 Note that this stores the hash reference, not a distinct copy of the hash it
62 points to.  You can ask the object for a copy with the I<hash> method.
63
64 =cut
65
66 sub create {
67   my($proto,$hashref)=@_;
68
69   $proto->new('cust_main_invoice',$hashref);
70
71 }
72
73 =item insert
74
75 Adds this record to the database.  If there is an error, returns the error,
76 otherwise returns false.
77
78 =cut
79
80 sub insert {
81   my($self)=@_;
82
83   #local $SIG{HUP} = 'IGNORE';
84   #local $SIG{INT} = 'IGNORE';
85   #local $SIG{QUIT} = 'IGNORE';
86   #local $SIG{TERM} = 'IGNORE';
87   #local $SIG{TSTP} = 'IGNORE';
88
89   $self->check or
90   $self->add;
91 }
92
93 =item delete
94
95 Delete this record from the database.
96
97 =cut
98
99 sub delete {
100   my($self)=@_;
101
102   $self->del;
103 }
104
105 =item replace OLD_RECORD
106
107 Replaces the OLD_RECORD with this one in the database.  If there is an error,
108 returns the error, otherwise returns false.
109
110 =cut
111
112 sub replace {
113   my($new,$old)=@_;
114   return "(Old) Not a cust_main_invoice record!" unless $old->table eq "cust_main_invoice";
115
116   return "Can't change destnum!"
117      unless $old->getfield('destnum') eq $new->getfield('destnum');
118   return "Can't change custnum!"
119      unless $old->getfield('custnum') eq $new->getfield('custnum');
120
121   $new->check or
122   $new->rep($old);
123 }
124
125
126 =item check
127
128 Checks all fields to make sure this is a valid invoice destination.  If there is
129 an error, returns the error, otherwise returns false.  Called by the insert
130 and repalce methods.
131
132 =cut
133
134 sub check {
135   my($self)=@_;
136   return "Not a cust_main_invoice record!" unless $self->table eq "cust_main_invoice";
137
138   my $error = $self->ut_number('destnum')
139         or $self->ut_number('custnum')
140         or $self->ut_text('dest')
141   ;
142   return $error if $error;
143
144   return "Unknown customer"
145     unless qsearchs('cust_main',{ 'custnum' => $self->custnum });
146
147   if ( $self->dest eq 'POST' ) {
148     #contemplate our navel
149   } elsif ( $self->dest =~ /^(\d+)$/ ) {
150     return "Unknown local account (specified by svcnum)"
151       unless qsearchs('svc_acct', { 'svcnum' => $self->dest } );
152   } elsif ( $self->dest =~ /^([\w\.\-]+)\@(([\w\.\-]\.)+\w+)$/ ) {
153     my($user, $domain) = ($1, $2);
154     if ( $domain eq $mydomain ) {
155       my $svc_acct = qsearchs('svc_acct', { 'username' => $user } );
156       return "Unknown local account (specified literally)" unless $svc_acct;
157       $svc_acct->svcnum =~ /^(\d+)$/ or die "Non-numeric svcnum?!";
158       $self->dest($1);
159     }
160   } else {
161     return "Illegal destination!";
162   }
163
164   ''; #no error
165 }
166
167 =item address
168
169 Returns the literal email address for this record (or `POST').
170
171 =cut
172
173 sub address {
174   my $self = shift;
175   if ( $self->dest =~ /(\d+)$/ ) {
176     my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $1 } );
177     $svc_acct->username . '@' . $mydomain;
178   } else {
179     $self->dest;
180   }
181 }
182
183 =back
184
185 =head1 VERSION
186
187 $Id: cust_main_invoice.pm,v 1.2 1998-12-16 09:58:53 ivan Exp $
188
189 =head1 BUGS
190
191 =head1 SEE ALSO
192
193 L<FS::Record>, L<FS::cust_main>
194
195 =head1 HISTORY
196
197 ivan@voicenet.com 97-jul-1
198
199 added hfields
200 ivan@sisd.com 97-nov-13
201
202 $Log: cust_main_invoice.pm,v $
203 Revision 1.2  1998-12-16 09:58:53  ivan
204 library support for editing email invoice destinations (not in sub collect yet)
205
206 Revision 1.1  1998/12/16 07:40:02  ivan
207 new table
208
209 Revision 1.3  1998/11/15 04:33:00  ivan
210 updates for newest versoin
211
212 Revision 1.2  1998/11/15 03:48:49  ivan
213 update for current version
214
215
216 =cut
217
218 1;
219