default to a session cookie instead of setting an explicit timeout, weird timezone...
[freeside.git] / FS / FS / cust_credit_bill.pm
1 package FS::cust_credit_bill;
2 use base qw( FS::cust_main_Mixin FS::cust_bill_ApplicationCommon );
3
4 use strict;
5 use vars qw( $conf );
6 use FS::UID;
7
8 #ask FS::UID to run this stuff for us later
9 FS::UID->install_callback( sub { 
10   $conf = new FS::Conf;
11 } );
12
13 =head1 NAME
14
15 FS::cust_credit_bill - Object methods for cust_credit_bill records
16
17 =head1 SYNOPSIS
18
19   use FS::cust_credit_bill;
20
21   $record = new FS::cust_credit_bill \%hash;
22   $record = new FS::cust_credit_bill { 'column' => 'value' };
23
24   $error = $record->insert;
25
26   $error = $new_record->replace($old_record);
27
28   $error = $record->delete;
29
30   $error = $record->check;
31
32 =head1 DESCRIPTION
33
34 An FS::cust_credit_bill object represents application of a credit (see
35 L<FS::cust_credit>) to an invoice (see L<FS::cust_bill>).  FS::cust_credit_bill
36 inherits from FS::cust_bill_ApplicationCommon and FS::Record.  The following
37 fields are currently supported:
38
39 =over 4
40
41 =item creditbillnum - primary key
42
43 =item crednum - credit being applied 
44
45 =item invnum - invoice to which credit is applied (see L<FS::cust_bill>)
46
47 =item amount - amount of the credit applied
48
49 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">.  Also see
50 L<Time::Local> and L<Date::Parse> for conversion functions.
51
52 =back
53
54 =head1 METHODS
55
56 =over 4
57
58 =item new HASHREF
59
60 Creates a new cust_credit_bill.  To add the cust_credit_bill to the database,
61 see L<"insert">.
62
63 =cut
64
65 sub table { 'cust_credit_bill'; }
66
67 sub _app_source_name  { 'credit'; }
68 sub _app_source_table { 'cust_credit'; }
69 sub _app_lineitem_breakdown_table { 'cust_credit_bill_pkg'; }
70 sub _app_part_pkg_weight_column { 'credit_weight'; }
71
72 =item insert
73
74 Adds this cust_credit_bill to the database ("Posts" all or part of a credit).
75 If there is an error, returns the error, otherwise returns false.
76
77 =item delete
78
79 Currently unimplemented.
80
81 =cut
82
83 sub delete {
84   my $self = shift;
85   return "Can't delete application for closed credit"
86     if $self->cust_credit->closed =~ /^Y/i;
87   return "Can't delete application for closed invoice"
88     if $self->cust_bill->closed =~ /^Y/i;
89   $self->SUPER::delete(@_);
90 }
91
92 =item replace OLD_RECORD
93
94 Application of credits may not be modified.
95
96 =cut
97
98 sub replace {
99   return "Can't modify application of credit!"
100 }
101
102 =item check
103
104 Checks all fields to make sure this is a valid credit application.  If there
105 is an error, returns the error, otherwise returns false.  Called by the insert
106 and replace methods.
107
108 =cut
109
110 sub check {
111   my $self = shift;
112
113   my $error =
114     $self->ut_numbern('creditbillnum')
115     || $self->ut_foreign_key('crednum', 'cust_credit', 'crednum')
116     || $self->ut_foreign_key('invnum', 'cust_bill', 'invnum' )
117     || $self->ut_numbern('_date')
118     || $self->ut_money('amount')
119     || $self->ut_foreign_keyn('pkgnum', 'cust_pkg', 'pkgnum')
120   ;
121   return $error if $error;
122
123   return "amount must be > 0" if $self->amount <= 0;
124
125   $self->_date(time) unless $self->_date;
126
127   return "Cannot apply more than remaining value of credit"
128     unless $self->amount <= $self->cust_credit->credited;
129
130   return "Cannot apply more than remaining value of invoice"
131     unless $self->amount <= $self->cust_bill->owed;
132
133   $self->SUPER::check;
134 }
135
136 =item sub cust_credit
137
138 Returns the credit (see L<FS::cust_credit>)
139
140 =back
141
142 =head1 BUGS
143
144 The delete method.
145
146 This probably should have been called cust_bill_credit.
147
148 =head1 SEE ALSO
149
150 L<FS::Record>, L<FS::cust_bill>, L<FS::cust_credit>,
151 schema.html from the base documentation.
152
153 =cut
154
155 1;
156