1 package FS::quotation_pkg;
4 use base qw( FS::TemplateItem_Mixin FS::Record );
5 use FS::Record qw( qsearch qsearchs dbh );
9 use FS::quotation_pkg_discount; #so its loaded when TemplateItem_Mixin needs it
10 use FS::quotation_pkg_detail;
11 use List::Util qw(sum);
15 FS::quotation_pkg - Object methods for quotation_pkg records
19 use FS::quotation_pkg;
21 $record = new FS::quotation_pkg \%hash;
22 $record = new FS::quotation_pkg { 'column' => 'value' };
24 $error = $record->insert;
26 $error = $new_record->replace($old_record);
28 $error = $record->delete;
30 $error = $record->check;
34 An FS::quotation_pkg object represents a quotation package.
35 FS::quotation_pkg inherits from FS::Record. The following fields are currently
46 pkgpart (L<FS::part_pkg>) of the package
50 locationnum (L<FS::cust_location>) where the package will be in service
54 expected start date for the package, as a timestamp
66 'Y' to waive the setup fee
70 The amount per package that will be charged in setup/one-time fees.
74 The amount per package that will be charged per billing cycle.
84 Creates a new quotation package. To add the quotation package to the database,
87 Note that this stores the hash reference, not a distinct copy of the hash it
88 points to. You can ask the object for a copy with the I<hash> method.
92 sub table { 'quotation_pkg'; }
94 sub display_table { 'quotation_pkg'; }
96 #forget it, just overriding cust_bill_pkg_display entirely
97 #sub display_table_orderby { 'quotationpkgnum'; } # something else?
98 # # (for invoice display order)
100 sub discount_table { 'quotation_pkg_discount'; }
101 sub detail_table { 'quotation_pkg_detail'; }
105 Adds this record to the database. If there is an error, returns the error,
106 otherwise returns false.
111 my ($self, %options) = @_;
114 my $oldAutoCommit = $FS::UID::AutoCommit;
115 local $FS::UID::AutoCommit = 0;
117 #false laziness w/cust_main::Packages::order_pkg
118 if ( $options{'locationnum'} and $options{'locationnum'} != -1 ) {
120 $self->locationnum($options{'locationnum'});
122 } elsif ( $options{'cust_location'} ) {
124 my $error = $options{'cust_location'}->find_or_insert;
126 $dbh->rollback if $oldAutoCommit;
127 return "inserting cust_location (transaction rolled back): $error";
129 $self->locationnum($options{'cust_location'}->locationnum);
133 my $error = $self->SUPER::insert;
135 if ( !$error and $self->discountnum ) {
136 $error = $self->insert_discount;
137 $error .= ' (setting discount)' if $error;
140 # update $self and any discounts with their amounts
142 $error = $self->estimate;
143 $error .= ' (calculating charges)' if $error;
147 $dbh->rollback if $oldAutoCommit;
150 $dbh->commit if $oldAutoCommit;
157 Delete this record from the database.
165 my $oldAutoCommit = $FS::UID::AutoCommit;
166 local $FS::UID::AutoCommit = 0;
168 my $error = $self->delete_details;
170 $dbh->rollback if $oldAutoCommit;
174 foreach ($self->quotation_pkg_discount, $self->quotation_pkg_tax) {
177 $dbh->rollback if $oldAutoCommit;
178 return $error . ' (deleting discount)';
182 $error = $self->SUPER::delete;
184 $dbh->rollback if $oldAutoCommit;
187 $dbh->commit if $oldAutoCommit;
193 =item replace OLD_RECORD
195 Replaces the OLD_RECORD with this one in the database. If there is an error,
196 returns the error, otherwise returns false.
200 Checks all fields to make sure this is a valid quotation package. If there is
201 an error, returns the error, otherwise returns false. Called by the insert
210 $self->ut_numbern('quotationpkgnum')
211 || $self->ut_foreign_key( 'quotationnum', 'quotation', 'quotationnum' )
212 || $self->ut_foreign_key( 'pkgpart', 'part_pkg', 'pkgpart' )
213 || $self->ut_foreign_keyn( 'locationnum', 'cust_location', 'locationnum' )
214 || $self->ut_numbern('start_date')
215 || $self->ut_numbern('contract_end')
216 || $self->ut_numbern('quantity')
217 || $self->ut_moneyn('unitsetup')
218 || $self->ut_moneyn('unitrecur')
219 || $self->ut_enum('waive_setup', [ '', 'Y'] )
222 if ($self->locationnum eq '') {
223 # use the customer default
224 my $quotation = $self->quotation;
225 if ($quotation->custnum) {
226 $self->set('locationnum', $quotation->cust_main->ship_locationnum);
227 } elsif ($quotation->prospectnum) {
228 # use the first non-disabled location for that prospect
229 my $cust_location = qsearchs('cust_location',
230 { prospectnum => $quotation->prospectnum,
232 $self->set('locationnum', $cust_location->locationnum) if $cust_location;
233 } # else the quotation is invalid
236 return $error if $error;
243 qsearchs('part_pkg', { 'pkgpart' => $self->pkgpart } );
248 $self->part_pkg->pkg;
253 Update the quotation_pkg record with the estimated setup and recurring
254 charges for the package. Returns nothing on success, or an error message
262 my( $unitsetup, $unitrecur ) = (0, 0);
263 foreach my $part_pkg ( $self->part_pkg->self_and_bill_linked ) {
265 $unitsetup += $part_pkg->option('setup_fee',1) || '0' # 3.x only
266 unless $self->waive_setup eq 'Y' || $self->{'_NO_SETUP_KLUDGE'};
268 $unitrecur += $part_pkg->base_recur
269 unless $self->{'_NO_RECUR_KLUDGE'};
273 $self->set('unitsetup', sprintf('%.2f', $unitsetup) );
274 $self->set('unitrecur', sprintf('%.2f', $unitrecur) );
275 my $error = $self->replace;
276 return $error if $error;
278 # semi-duplicates calc_discount
279 my $setup_discount = 0;
280 my $recur_discount = 0;
282 my %setup_discounts; # quotationpkgdiscountnum => amount
283 my %recur_discounts; # quotationpkgdiscountnum => amount
285 # XXX the order of applying discounts is ill-defined, which matters
286 # if there are percentage and amount discounts on the same package.
288 # but right now there can only be one discount on any package, so
290 foreach my $pkg_discount ($self->quotation_pkg_discount) {
292 my $discount = $pkg_discount->discount;
293 my $this_setup_discount = 0;
294 my $this_recur_discount = 0;
296 if ( $discount->percent > 0 ) {
298 if ( $discount->setup ) {
299 $this_setup_discount = ($discount->percent * $unitsetup / 100);
301 $this_recur_discount = ($discount->percent * $unitrecur / 100);
303 } elsif ( $discount->amount > 0 ) {
305 my $discount_left = $discount->amount;
306 if ( $discount->setup ) {
307 if ( $discount_left > $unitsetup - $setup_discount ) {
308 # then discount the setup to zero
309 $discount_left -= $unitsetup - $setup_discount;
310 $this_setup_discount = $unitsetup - $setup_discount;
312 # not enough discount to fully cover the setup
313 $this_setup_discount = $discount_left;
317 # same logic for recur
318 if ( $discount_left > $unitrecur - $recur_discount ) {
319 $this_recur_discount = $unitrecur - $recur_discount;
321 $this_recur_discount = $discount_left;
326 # increment the total discountage
327 $setup_discount += $this_setup_discount;
328 $recur_discount += $this_recur_discount;
329 # and update the pkg_discount object
330 $pkg_discount->set('setup_amount', sprintf('%.2f', $setup_discount));
331 $pkg_discount->set('recur_amount', sprintf('%.2f', $recur_discount));
332 my $error = $pkg_discount->replace;
333 return $error if $error;
339 =item insert_discount
341 Associates this package with a discount (see L<FS::cust_pkg_discount>,
342 possibly inserting a new discount on the fly (see L<FS::discount>). Properties
343 of the discount will be taken from this object.
347 sub insert_discount {
348 #my ($self, %options) = @_;
351 my $quotation_pkg_discount = FS::quotation_pkg_discount->new( {
352 'quotationpkgnum' => $self->quotationpkgnum,
353 'discountnum' => $self->discountnum,
354 #for the create a new discount case
355 '_type' => $self->discountnum__type,
356 'amount' => $self->discountnum_amount,
357 'percent' => $self->discountnum_percent,
358 'months' => $self->discountnum_months,
359 'setup' => $self->discountnum_setup,
362 $quotation_pkg_discount->insert;
367 my @pkg_discounts = $self->pkg_discount;
368 return if @pkg_discounts == 0;
373 description => $self->mt('Discount'),
377 ext_description => \@ext,
378 # maybe should show quantity/unit discount?
380 foreach my $pkg_discount (@pkg_discounts) {
381 push @ext, $pkg_discount->description;
382 $d->{setup_amount} -= $pkg_discount->setup_amount;
383 $d->{recur_amount} -= $pkg_discount->recur_amount;
385 $d->{setup_amount} *= $self->quantity || 1;
386 $d->{recur_amount} *= $self->quantity || 1;
387 $d->{amount} = $d->{setup_amount} + $d->{recur_amount};
394 ($self->unitsetup - sum(0, map { $_->setup_amount } $self->pkg_discount))
395 * ($self->quantity || 1);
398 sub setup_show_zero {
400 return $self->part_pkg->setup_show_zero;
405 ($self->unitrecur - sum(0, map { $_->recur_amount } $self->pkg_discount))
406 * ($self->quantity || 1);
409 sub recur_show_zero {
411 return $self->part_pkg->recur_show_zero;
416 Deletes all quotation_pkgs_details associated with this pkg (see L<FS::quotation_pkg_detail>).
423 my $oldAutoCommit = $FS::UID::AutoCommit;
424 local $FS::UID::AutoCommit = 0;
427 foreach my $detail ( qsearch('quotation_pkg_detail',{ 'quotationpkgnum' => $self->quotationpkgnum }) ) {
428 my $error = $detail->delete;
430 $dbh->rollback if $oldAutoCommit;
431 return "error removing old detail: $error";
435 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
440 =item set_details PARAM
442 Sets new quotation details for this package (see L<FS::quotation_pkg_detail>),
443 removing existing details.
445 Recognizes the following parameters:
447 details - arrayref of strings, one for each new detail
449 copy_on_order - if true, sets copy_on_order flag on new details
451 If there is an error, returns the error, otherwise returns false.
459 $opt{'details'} ||= [];
460 my @details = @{$opt{'details'}};
462 my $oldAutoCommit = $FS::UID::AutoCommit;
463 local $FS::UID::AutoCommit = 0;
466 my $error = $self->delete_details;
468 $dbh->rollback if $oldAutoCommit;
472 foreach my $detail ( @details ) {
473 my $quotation_pkg_detail = new FS::quotation_pkg_detail {
474 'quotationpkgnum' => $self->quotationpkgnum,
476 'copy_on_order' => $opt{'copy_on_order'} ? 'Y' : '',
478 $error = $quotation_pkg_detail->insert;
480 $dbh->rollback if $oldAutoCommit;
481 return "error adding new detail: $error";
485 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
494 =item cust_bill_pkg_display [ type => TYPE ]
498 sub cust_bill_pkg_display {
499 my ( $self, %opt ) = @_;
501 my $type = $opt{type} if exists $opt{type};
502 return () if $type eq 'U'; #quotations don't have usage
504 if ( $self->get('display') ) {
505 return ( grep { defined($type) ? ($type eq $_->type) : 1 }
506 @{ $self->get('display') }
511 my $setup = $self->new($self->hashref);
512 $setup->{'_NO_RECUR_KLUDGE'} = 1;
513 $setup->{'type'} = 'S';
514 my $recur = $self->new($self->hashref);
515 $recur->{'_NO_SETUP_KLUDGE'} = 1;
516 $recur->{'type'} = 'R';
518 if ( $type eq 'S' ) {
520 } elsif ( $type eq 'R' ) {
523 #return ($setup, $recur);
533 Returns the customer (L<FS::cust_main> object).
539 my $quotation = FS::quotation->by_key($self->quotationnum) or return '';
540 $quotation->cust_main;
543 sub tax_locationnum {
552 FS::quotation->by_key($self->quotationnum);
555 sub quotation_pkg_detail {
557 sort { $a->detailnum <=> $b->detailnum }
558 qsearch('quotation_pkg_detail', { quotationpkgnum => $self->quotationpkgnum });
561 sub quotation_pkg_discount {
563 qsearch('quotation_pkg_discount', { quotationpkgnum => $self->quotationpkgnum });
566 sub quotation_pkg_tax {
568 qsearch('quotation_pkg_tax', { quotationpkgnum => $self->quotationpkgnum });
573 $self->locationnum ? qsearchs('cust_location', { locationnum => $self->locationnum }) : '';
579 my @quotation_pkg_without_location =
580 qsearch( 'quotation_pkg', { locationnum => '' } );
581 if (@quotation_pkg_without_location) {
582 warn "setting default location on quotation_pkg records\n";
583 foreach my $quotation_pkg (@quotation_pkg_without_location) {
584 # check() will fix this
585 my $error = $quotation_pkg->replace;
587 die "quotation #".$quotation_pkg->quotationnum.": $error\n";
598 Doesn't support fees, or add-on packages.
602 L<FS::Record>, schema.html from the base documentation.