autoload methods returning foreign records, RT#13971
[freeside.git] / FS / FS / cust_credit_void.pm
1 package FS::cust_credit_void; 
2 use base qw( FS::otaker_Mixin FS::cust_main_Mixin FS::Record );
3
4 use strict;
5 use FS::Record qw(qsearchs); # qsearch qsearchs);
6 use FS::CurrentUser;
7 use FS::access_user;
8 use FS::cust_credit;
9
10 =head1 NAME
11
12 FS::cust_credit_void - Object methods for cust_credit_void objects
13
14 =head1 SYNOPSIS
15
16   use FS::cust_credit_void;
17
18   $record = new FS::cust_credit_void \%hash;
19   $record = new FS::cust_credit_void { '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_credit_void object represents a voided credit.  All fields in
32 FS::cust_credit are present, as well as:
33
34 =over 4
35
36 =item void_date - the date (unix timestamp) that the credit was voided
37
38 =item void_reason - the reason (a freeform string)
39
40 =item void_usernum - the user (L<FS::access_user>) who voided it
41
42 =back
43
44 =head1 METHODS
45
46 =over 4 
47
48 =item new HASHREF
49
50 Creates a new voided credit record.
51
52 =cut
53
54 sub table { 'cust_credit_void'; }
55
56 =item insert
57
58 Adds this voided credit to the database.
59
60 =item check
61
62 Checks all fields to make sure this is a valid voided credit.  If there is an
63 error, returns the error, otherwise returns false.  Called by the insert
64 method.
65
66 =cut
67
68 sub check {
69   my $self = shift;
70
71   my $error =
72     $self->ut_numbern('crednum')
73     || $self->ut_number('custnum')
74     || $self->ut_numbern('_date')
75     || $self->ut_money('amount')
76     || $self->ut_alphan('otaker')
77     || $self->ut_textn('reason')
78     || $self->ut_textn('addlinfo')
79     || $self->ut_enum('closed', [ '', 'Y' ])
80     || $self->ut_foreign_keyn('pkgnum', 'cust_pkg', 'pkgnum')
81     || $self->ut_foreign_keyn('eventnum', 'cust_event', 'eventnum')
82     || $self->ut_foreign_keyn('commission_agentnum',  'agent', 'agentnum')
83     || $self->ut_foreign_keyn('commission_salesnum',  'sales', 'salesnum')
84     || $self->ut_foreign_keyn('commission_pkgnum', 'cust_pkg', 'pkgnum')
85     || $self->ut_numbern('void_date')
86     || $self->ut_textn('void_reason')
87     || $self->ut_foreign_keyn('void_usernum', 'access_user', 'usernum')
88   ;
89   return $error if $error;
90
91   $self->void_date(time) unless $self->void_date;
92
93   $self->void_usernum($FS::CurrentUser::CurrentUser->usernum)
94     unless $self->void_usernum;
95
96   $self->SUPER::check;
97 }
98
99 =item cust_main
100
101 Returns the parent customer object (see L<FS::cust_main>).
102
103 =item void_access_user
104
105 Returns the voiding employee object (see L<FS::access_user>).
106
107 =cut
108
109 sub void_access_user {
110   my $self = shift;
111   qsearchs('access_user', { 'usernum' => $self->void_usernum } );
112 }
113
114 =back
115
116 =head1 BUGS
117
118 Doesn't yet support unvoid.
119
120 =head1 SEE ALSO
121
122 L<FS::cust_credit>, L<FS::Record>, schema.html from the base documentation.
123
124 =cut
125
126 1;
127