give better error message on bad invnum, also 'use FS::cust_bill' here
[freeside.git] / FS / FS / cust_bill_event.pm
1 package FS::cust_bill_event;
2
3 use strict;
4 use vars qw( @ISA );
5 use FS::Record qw( qsearch qsearchs );
6 use FS::cust_bill;
7 use FS::part_bill_event;
8
9 @ISA = qw(FS::Record);
10
11 =head1 NAME
12
13 FS::cust_bill_event - Object methods for cust_bill_event records
14
15 =head1 SYNOPSIS
16
17   use FS::cust_bill_event;
18
19   $record = new FS::cust_bill_event \%hash;
20   $record = new FS::cust_bill_event { 'column' => 'value' };
21
22   $error = $record->insert;
23
24   $error = $new_record->replace($old_record);
25
26   $error = $record->delete;
27
28   $error = $record->check;
29
30 =head1 DESCRIPTION
31
32 An FS::cust_bill_event object represents an complete invoice event.
33 FS::cust_bill_event inherits from FS::Record.  The following fields are
34 currently supported:
35
36 =over 4
37
38 =item eventnum - primary key
39
40 =item invnum - invoice (see L<FS::cust_bill>)
41
42 =item eventpart - event definition (see L<FS::part_bill_event>)
43
44 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">.  Also see
45 L<Time::Local> and L<Date::Parse> for conversion functions.
46
47 =back
48
49 =head1 METHODS
50
51 =over 4
52
53 =item new HASHREF
54
55 Creates a new completed invoice event.  To add the compelted invoice event to
56 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 # the new method can be inherited from FS::Record, if a table method is defined
64
65 sub table { 'cust_bill_event'; }
66
67 =item insert
68
69 Adds this record to the database.  If there is an error, returns the error,
70 otherwise returns false.
71
72 =cut
73
74 # the insert method can be inherited from FS::Record
75
76 =item delete
77
78 Delete this record from the database.
79
80 =cut
81
82 # the delete method can be inherited from FS::Record
83
84 =item replace OLD_RECORD
85
86 Replaces the OLD_RECORD with this one in the database.  If there is an error,
87 returns the error, otherwise returns false.
88
89 =cut
90
91 # the replace method can be inherited from FS::Record
92
93 =item check
94
95 Checks all fields to make sure this is a valid completed invoice event.  If
96 there is an error, returns the error, otherwise returns false.  Called by the
97 insert and replace methods.
98
99 =cut
100
101 # the check method should currently be supplied - FS::Record contains some
102 # data checking routines
103
104 sub check {
105   my $self = shift;
106
107   my $error = $self->ut_numbern('eventnum')
108     || $self->ut_number('invnum')
109     || $self->ut_number('eventpart')
110     || $self->ut_number('_date')
111     || $self->ut_enum('status', [qw( done failed )])
112     || $self->ut_textn('statustext')
113   ;
114
115   return "Unknown invnum ". $self->invnum
116     unless qsearchs( 'cust_bill' ,{ 'invnum' => $self->invnum } );
117
118   return "Unknown eventpart ". $self->eventpart
119     unless qsearchs( 'part_bill_event' ,{ 'eventpart' => $self->eventpart } );
120
121   ''; #no error
122 }
123
124 =item part_bill_event
125
126 Returns the invoice event definition (see L<FS::part_bill_event>) for this
127 completed invoice event.
128
129 =cut
130
131 sub part_bill_event {
132   my $self = shift;
133   qsearchs( 'part_bill_event', { 'eventpart' => $self->eventpart } );
134 }
135
136 =item cust_bill
137
138 Returns the invoice (see L<FS::cust_bill>) for this completed invoice event.
139
140 =cut
141
142 sub cust_bill {
143   my $self = shift;
144   qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
145 }
146
147 =item retry
148
149 Changes the status of this event from B<done> to B<failed>, allowing it to be
150 retried.
151
152 =cut
153
154 sub retry {
155   my $self = shift;
156   return '' unless $self->status eq 'done';
157   my $old = ref($self)->new( { $self->hash } );
158   $self->status('failed');
159   $self->replace($old);
160 }
161
162 =back
163
164 =head1 BUGS
165
166 Far too early in the morning.
167
168 =head1 SEE ALSO
169
170 L<FS::part_bill_event>, L<FS::cust_bill>, L<FS::Record>, schema.html from the
171 base documentation.
172
173 =cut
174
175 1;
176