fix creation of custom discounts on quotations, and ordering of discounted quoted...
[freeside.git] / FS / FS / quotation_pkg_discount.pm
1 package FS::quotation_pkg_discount;
2
3 use strict;
4 use base qw( FS::pkg_discount_Mixin 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 =item delete
85
86 Delete this record from the database.
87
88 =item replace OLD_RECORD
89
90 Replaces the OLD_RECORD with this one in the database.  If there is an error,
91 returns the error, otherwise returns false.
92
93 =item check
94
95 Checks all fields to make sure this is a valid quotation package discount.
96 If there is an error, returns the error, otherwise returns false.
97 Called by the insert and replace methods.
98
99 =cut
100
101 # the check method should currently be supplied - FS::Record contains some
102 # data checking routines
103
104 sub check {
105   my $self = shift;
106
107   my $error = 
108     $self->ut_numbern('quotationpkgdiscountnum')
109     || $self->ut_foreign_key('quotationpkgnum', 'quotation_pkg', 'quotationpkgnum' )
110     || $self->ut_foreign_key('discountnum', 'discount', 'discountnum' )
111     || $self->ut_moneyn('setup_amount')
112     || $self->ut_moneyn('recur_amount')
113   ;
114   return $error if $error;
115
116   $self->SUPER::check;
117 }
118
119 =back
120
121 =item amount
122
123 Returns the total amount of this discount (setup + recur), for compatibility
124 with L<FS::cust_bill_pkg_discount>.
125
126 =cut
127
128 sub amount {
129   my $self = shift;
130   return $self->get('setup_amount') + $self->get('recur_amount');
131 }
132
133 =item description
134
135 Returns a string describing the discount (for use on the quotation).
136
137 =cut
138
139 sub description {
140   my $self = shift;
141   my $discount = $self->discount;
142   my $desc = $discount->description_short;
143   # XXX localize to prospect language, once prospects get languages
144   $desc .= mt(' each') if $self->quotation_pkg->quantity > 1;
145
146   if ($discount->months) {
147     # unlike cust_bill_pkg_discount, there are no "months remaining"; it 
148     # hasn't started yet.
149     $desc .= mt(' (for [quant,_1,month])', $discount->months);
150   }
151   return $desc;
152 }
153
154 #stub for 3.x
155 sub quotation_pkg {
156   my $self = shift;
157   FS::quotation_pkg->by_key($self->quotationpkgnum);
158 }
159
160 sub discount {
161   my $self = shift;
162   FS::discount->by_key($self->discountnum);
163 }
164
165 =head1 BUGS
166
167 =head1 SEE ALSO
168
169 L<FS::Record>, schema.html from the base documentation.
170
171 =cut
172
173 1;
174