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