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