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