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