fix quotations vs. currency_fixed price plan, RT#26810, RT#21565
[freeside.git] / FS / FS / quotation_pkg.pm
1 package FS::quotation_pkg;
2 use base qw( FS::TemplateItem_Mixin FS::Record );
3
4 use strict;
5 use FS::Record qw( qsearchs ); #qsearch
6 use FS::part_pkg;
7 use FS::quotation_pkg_discount; #so its loaded when TemplateItem_Mixin needs it
8
9 =head1 NAME
10
11 FS::quotation_pkg - Object methods for quotation_pkg records
12
13 =head1 SYNOPSIS
14
15   use FS::quotation_pkg;
16
17   $record = new FS::quotation_pkg \%hash;
18   $record = new FS::quotation_pkg { 'column' => 'value' };
19
20   $error = $record->insert;
21
22   $error = $new_record->replace($old_record);
23
24   $error = $record->delete;
25
26   $error = $record->check;
27
28 =head1 DESCRIPTION
29
30 An FS::quotation_pkg object represents a quotation package.
31 FS::quotation_pkg inherits from FS::Record.  The following fields are currently
32 supported:
33
34 =over 4
35
36 =item quotationpkgnum
37
38 primary key
39
40 =item pkgpart
41
42 pkgpart
43
44 =item locationnum
45
46 locationnum
47
48 =item start_date
49
50 start_date
51
52 =item contract_end
53
54 contract_end
55
56 =item quantity
57
58 quantity
59
60 =item waive_setup
61
62 waive_setup
63
64
65 =back
66
67 =head1 METHODS
68
69 =over 4
70
71 =item new HASHREF
72
73 Creates a new quotation package.  To add the quotation package to the database,
74 see L<"insert">.
75
76 Note that this stores the hash reference, not a distinct copy of the hash it
77 points to.  You can ask the object for a copy with the I<hash> method.
78
79 =cut
80
81 sub table { 'quotation_pkg'; }
82
83 sub display_table         { 'quotation_pkg'; }
84
85 #forget it, just overriding cust_bill_pkg_display entirely
86 #sub display_table_orderby { 'quotationpkgnum'; } # something else?
87 #                                                 #  (for invoice display order)
88
89 sub discount_table        { 'quotation_pkg_discount'; }
90
91 =item insert
92
93 Adds this record to the database.  If there is an error, returns the error,
94 otherwise returns false.
95
96 =item delete
97
98 Delete this record from the database.
99
100 =item replace OLD_RECORD
101
102 Replaces the OLD_RECORD with this one in the database.  If there is an error,
103 returns the error, otherwise returns false.
104
105 =item check
106
107 Checks all fields to make sure this is a valid quotation package.  If there is
108 an error, returns the error, otherwise returns false.  Called by the insert
109 and replace methods.
110
111 =cut
112
113 sub check {
114   my $self = shift;
115
116   my $error = 
117     $self->ut_numbern('quotationpkgnum')
118     || $self->ut_foreign_key(  'quotationnum', 'quotation',    'quotationnum' )
119     || $self->ut_foreign_key(  'pkgpart',      'part_pkg',     'pkgpart'      )
120     || $self->ut_foreign_keyn( 'locationnum', 'cust_location', 'locationnum'  )
121     || $self->ut_numbern('start_date')
122     || $self->ut_numbern('contract_end')
123     || $self->ut_numbern('quantity')
124     || $self->ut_enum('waive_setup', [ '', 'Y'] )
125   ;
126   return $error if $error;
127
128   $self->SUPER::check;
129 }
130
131 #it looks redundant with a v4.x+ auto-generated method, but need to override
132 # FS::TemplateItem_Mixin's version
133 sub part_pkg {
134   my $self = shift;
135   qsearchs('part_pkg', { 'pkgpart' => $self->pkgpart } );
136 }
137
138 sub desc {
139   my $self = shift;
140   $self->part_pkg->pkg;
141 }
142
143 sub setup {
144   my $self = shift;
145   return '0.00' if $self->waive_setup eq 'Y' || $self->{'_NO_SETUP_KLUDGE'};
146   my $part_pkg = $self->part_pkg;
147   #my $setup = $part_pkg->can('base_setup') ? $part_pkg->base_setup
148   #                                         : $part_pkg->option('setup_fee');
149   my $setup = $part_pkg->option('setup_fee');
150   #XXX discounts
151   $setup *= $self->quantity if $self->quantity;
152   sprintf('%.2f', $setup);
153
154 }
155
156 sub recur {
157   my $self = shift;
158   return '0.00' if $self->{'_NO_RECUR_KLUDGE'};
159   my $part_pkg = $self->part_pkg;
160   my $recur = $part_pkg->can('base_recur') ? $part_pkg->base_recur($self)
161                                            : $part_pkg->option('recur_fee');
162   #XXX discounts
163   $recur *= $self->quantity if $self->quantity;
164   sprintf('%.2f', $recur);
165 }
166
167 =item part_pkg_currency_option OPTIONNAME
168
169 Returns a two item list consisting of the currency of this quotation's customer
170 or prospect, if any, and a value for the provided option.  If the customer or
171 prospect has a currency, the value is the option value the given name and the
172 currency (see L<FS::part_pkg_currency>).  Otherwise, if the customer or
173 prospect has no currency, is the regular option value for the given name (see
174 L<FS::part_pkg_option>).
175
176 =cut
177
178 #false laziness w/cust_pkg->part_pkg_currency_option
179 sub part_pkg_currency_option {
180   my( $self, $optionname ) = @_;
181   my $part_pkg = $self->part_pkg;
182   my $prospect_or_customer = $self->cust_main || $self->prospect_main;
183   if ( my $currency = $prospect_or_customer->currency ) {
184     ($currency, $part_pkg->part_pkg_currency_option($currency, $optionname) );
185   } else {
186     ('', $part_pkg->option($optionname) );
187   }
188 }
189
190
191 =item cust_bill_pkg_display [ type => TYPE ]
192
193 =cut
194
195 sub cust_bill_pkg_display {
196   my ( $self, %opt ) = @_;
197
198   my $type = $opt{type} if exists $opt{type};
199   return () if $type eq 'U'; #quotations don't have usage
200
201   if ( $self->get('display') ) {
202     return ( grep { defined($type) ? ($type eq $_->type) : 1 }
203                @{ $self->get('display') }
204            );
205   } else {
206
207     #??
208     my $setup = $self->new($self->hashref);
209     $setup->{'_NO_RECUR_KLUDGE'} = 1;
210     $setup->{'type'} = 'S';
211     my $recur = $self->new($self->hashref);
212     $recur->{'_NO_SETUP_KLUDGE'} = 1;
213     $recur->{'type'} = 'R';
214
215     if ( $type eq 'S' ) {
216       return ($setup);
217     } elsif ( $type eq 'R' ) {
218       return ($recur);
219     } else {
220       #return ($setup, $recur);
221       return ($self);
222     }
223
224   }
225
226 }
227
228 =item cust_main
229
230 Returns the customer (L<FS::cust_main> object).
231
232 =cut
233
234 sub cust_main {
235   my $self = shift;
236   my $quotation = FS::quotation->by_key($self->quotationnum) or return '';
237   $quotation->cust_main;
238 }
239
240 =item prospect_main
241
242 Returns the prospect (L<FS::prospect_main> object).
243
244 =cut
245
246 sub prospect_main {
247   my $self = shift;
248   my $quotation = FS::quotation->by_key($self->quotationnum) or return '';
249   $quotation->prospect_main;
250 }
251
252 =back
253
254 =head1 BUGS
255
256 =head1 SEE ALSO
257
258 L<FS::Record>, schema.html from the base documentation.
259
260 =cut
261
262 1;
263