discounts + quotations, #33099
[freeside.git] / FS / FS / quotation_pkg_discount.pm
1 package FS::quotation_pkg_discount;
2
3 use strict;
4 use base qw( FS::Record );
5 use FS::Record qw( qsearch qsearchs );
6 use FS::quotation_pkg;
7 use FS::discount;
8 use FS::Maketext 'mt';
9
10 =head1 NAME
11
12 FS::quotation_pkg_discount - Object methods for quotation_pkg_discount records
13
14 =head1 SYNOPSIS
15
16   use FS::quotation_pkg_discount;
17
18   $record = new FS::quotation_pkg_discount \%hash;
19   $record = new FS::quotation_pkg_discount { '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::quotation_pkg_discount object represents a quotation package discount.
32 FS::quotation_pkg_discount inherits from FS::Record.  The following fields are
33 currently supported:
34
35 =over 4
36
37 =item quotationpkgdiscountnum
38
39 primary key
40
41 =item quotationpkgnum
42
43 quotationpkgnum of the L<FS::quotation_pkg> record that this discount is
44 for.
45
46 =item discountnum
47
48 discountnum (L<FS::discount>)
49
50 =item setup_amount
51
52 Amount that will be discounted from setup fees, per package quantity.
53
54 =item recur_amount
55
56 Amount that will be discounted from recurring fees in the first billing
57 cycle, per package quantity.
58
59 =back
60
61 =head1 METHODS
62
63 =over 4
64
65 =item new HASHREF
66
67 Creates a new quotation package discount.  To add the quotation package
68 discount to the database, see L<"insert">.
69
70 Note that this stores the hash reference, not a distinct copy of the hash it
71 points to.  You can ask the object for a copy with the I<hash> method.
72
73 =cut
74
75 # the new method can be inherited from FS::Record, if a table method is defined
76
77 sub table { 'quotation_pkg_discount'; }
78
79 =item insert
80
81 Adds this record to the database.  If there is an error, returns the error,
82 otherwise returns false.
83
84 =cut
85
86 # the insert method can be inherited from FS::Record
87
88 =item delete
89
90 Delete this record from the database.
91
92 =cut
93
94 # the delete method can be inherited from FS::Record
95
96 =item replace OLD_RECORD
97
98 Replaces the OLD_RECORD with this one in the database.  If there is an error,
99 returns the error, otherwise returns false.
100
101 =cut
102
103 # the replace method can be inherited from FS::Record
104
105 =item check
106
107 Checks all fields to make sure this is a valid quotation package discount.
108 If there is an error, returns the error, otherwise returns false.
109 Called by the insert and replace methods.
110
111 =cut
112
113 # the check method should currently be supplied - FS::Record contains some
114 # data checking routines
115
116 sub check {
117   my $self = shift;
118
119   my $error = 
120     $self->ut_numbern('quotationpkgdiscountnum')
121     || $self->ut_foreign_key('quotationpkgnum', 'quotation_pkg', 'quotationpkgnum' )
122     || $self->ut_foreign_key('discountnum', 'discount', 'discountnum' )
123     || $self->ut_moneyn('setup_amount')
124     || $self->ut_moneyn('recur_amount')
125   ;
126   return $error if $error;
127
128   $self->SUPER::check;
129 }
130
131 =back
132
133 =item amount
134
135 Returns the total amount of this discount (setup + recur), for compatibility
136 with L<FS::cust_bill_pkg_discount>.
137
138 =cut
139
140 sub amount {
141   my $self = shift;
142   return $self->get('setup_amount') + $self->get('recur_amount');
143 }
144
145 =item description
146
147 Returns a string describing the discount (for use on the quotation).
148
149 =cut
150
151 sub description {
152   my $self = shift;
153   my $discount = $self->discount;
154   my $desc = $discount->description_short;
155   # XXX localize to prospect language, once prospects get languages
156   $desc .= mt(' each') if $self->quotation_pkg->quantity > 1;
157
158   if ($discount->months) {
159     # unlike cust_bill_pkg_discount, there are no "months remaining"; it 
160     # hasn't started yet.
161     $desc .= mt(' (for [quant,_1,month])', $discount->months);
162   }
163   return $desc;
164 }
165
166 #stub for 3.x
167 sub quotation_pkg {
168   my $self = shift;
169   FS::quotation_pkg->by_key($self->quotationpkgnum);
170 }
171
172 sub discount {
173   my $self = shift;
174   FS::discount->by_key($self->discountnum);
175 }
176
177 =head1 BUGS
178
179 =head1 SEE ALSO
180
181 L<FS::Record>, schema.html from the base documentation.
182
183 =cut
184
185 1;
186