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