1 package FS::quotation_pkg;
2 use base qw( FS::TemplateItem_Mixin FS::Record );
5 use FS::Record qw( qsearchs qsearch dbh );
7 use FS::quotation_pkg_discount; #so its loaded when TemplateItem_Mixin needs it
8 use FS::quotation_pkg_detail;
9 use List::Util qw(sum);
13 FS::quotation_pkg - Object methods for quotation_pkg records
17 use FS::quotation_pkg;
19 $record = new FS::quotation_pkg \%hash;
20 $record = new FS::quotation_pkg { 'column' => 'value' };
22 $error = $record->insert;
24 $error = $new_record->replace($old_record);
26 $error = $record->delete;
28 $error = $record->check;
32 An FS::quotation_pkg object represents a quotation package.
33 FS::quotation_pkg inherits from FS::Record. The following fields are currently
44 pkgpart (L<FS::part_pkg>) of the package
48 locationnum (L<FS::cust_location>) where the package will be in service
52 expected start date for the package, as a timestamp
64 'Y' to waive the setup fee
68 The amount per package that will be charged in setup/one-time fees.
72 The amount per package that will be charged per billing cycle.
76 The length of the billing cycle. If zero it's a one-time charge; if any
77 other number it's that many months; other values are in L<FS::Misc::pkg_freqs>.
87 Creates a new quotation package. To add the quotation package to the database,
90 Note that this stores the hash reference, not a distinct copy of the hash it
91 points to. You can ask the object for a copy with the I<hash> method.
95 sub table { 'quotation_pkg'; }
97 sub display_table { 'quotation_pkg'; }
99 #forget it, just overriding cust_bill_pkg_display entirely
100 #sub display_table_orderby { 'quotationpkgnum'; } # something else?
101 # # (for invoice display order)
103 sub discount_table { 'quotation_pkg_discount'; }
104 sub detail_table { 'quotation_pkg_detail'; }
108 Adds this record to the database. If there is an error, returns the error,
109 otherwise returns false.
114 my ($self, %options) = @_;
117 my $oldAutoCommit = $FS::UID::AutoCommit;
118 local $FS::UID::AutoCommit = 0;
120 my $error = $self->SUPER::insert;
122 if ( !$error and ($self->setup_discountnum || $self->recur_discountnum) ) {
123 #warn "inserting discount\n";
124 $error = $self->insert_discount;
125 $error .= ' (setting discount)' if $error;
129 $dbh->rollback if $oldAutoCommit;
132 $dbh->commit if $oldAutoCommit;
139 Delete this record from the database.
147 my $oldAutoCommit = $FS::UID::AutoCommit;
148 local $FS::UID::AutoCommit = 0;
150 my $error = $self->delete_details;
152 $dbh->rollback if $oldAutoCommit;
156 foreach ($self->quotation_pkg_discount, $self->quotation_pkg_tax) {
159 $dbh->rollback if $oldAutoCommit;
160 return $error . ' (deleting discount)';
164 $error = $self->SUPER::delete;
166 $dbh->rollback if $oldAutoCommit;
169 $dbh->commit if $oldAutoCommit;
172 $self->quotation->estimate;
175 =item replace OLD_RECORD
177 Replaces the OLD_RECORD with this one in the database. If there is an error,
178 returns the error, otherwise returns false.
182 Checks all fields to make sure this is a valid quotation package. If there is
183 an error, returns the error, otherwise returns false. Called by the insert
191 my @freqs = ('', keys (%{ FS::Misc->pkg_freqs }));
194 $self->ut_numbern('quotationpkgnum')
195 || $self->ut_foreign_key( 'quotationnum', 'quotation', 'quotationnum' )
196 || $self->ut_foreign_key( 'pkgpart', 'part_pkg', 'pkgpart' )
197 || $self->ut_foreign_keyn( 'locationnum', 'cust_location', 'locationnum' )
198 || $self->ut_numbern('start_date')
199 || $self->ut_numbern('contract_end')
200 || $self->ut_numbern('quantity')
201 || $self->ut_moneyn('unitsetup')
202 || $self->ut_moneyn('unitrecur')
203 || $self->ut_enum('freq', \@freqs)
204 || $self->ut_enum('waive_setup', [ '', 'Y'] )
207 if ($self->locationnum eq '') {
208 # use the customer default
209 my $quotation = $self->quotation;
210 if ($quotation->custnum) {
211 $self->set('locationnum', $quotation->cust_main->ship_locationnum);
212 } elsif ($quotation->prospectnum) {
213 # use the first non-disabled location for that prospect
214 my $cust_location = qsearchs('cust_location',
215 { prospectnum => $quotation->prospectnum,
217 $self->set('locationnum', $cust_location->locationnum) if $cust_location;
218 } # else the quotation is invalid
221 return $error if $error;
226 #it looks redundant with a v4.x+ auto-generated method, but need to override
227 # FS::TemplateItem_Mixin's version
230 qsearchs('part_pkg', { 'pkgpart' => $self->pkgpart } );
235 $self->part_pkg->pkg;
240 =item insert_discount
242 Associates this package with a discount (see L<FS::cust_pkg_discount>,
243 possibly inserting a new discount on the fly (see L<FS::discount>). Properties
244 of the discount will be taken from this object.
248 sub insert_discount {
249 #my ($self, %options) = @_;
252 foreach my $x (qw(setup recur)) {
253 if ( my $discountnum = $self->get("${x}_discountnum") ) {
254 my $cust_pkg_discount = FS::quotation_pkg_discount->new( {
255 'quotationpkgnum' => $self->quotationpkgnum,
256 'discountnum' => $discountnum,
258 #for the create a new discount case
259 'amount' => $self->get("${x}_discountnum_amount"),
260 'percent' => $self->get("${x}_discountnum_percent"),
261 'months' => $self->get("${x}_discountnum_months"),
263 if ( $x eq 'setup' ) {
264 $cust_pkg_discount->setup('Y');
265 $cust_pkg_discount->months('');
267 my $error = $cust_pkg_discount->insert;
268 return $error if $error;
276 my $setuprecur = $options{'setuprecur'};
277 # a little different from cust_bill_pkg::_item_discount, in that this one
278 # is asked specifically whether to show setup or recur discounts (because
279 # on the quotation they're separate sections entirely)
281 my @pkg_discounts = grep { $_->setuprecur eq $setuprecur }
283 return if @pkg_discounts == 0;
288 description => $self->mt('Discount'),
290 ext_description => \@ext,
291 # maybe should show quantity/unit discount?
293 foreach my $pkg_discount (@pkg_discounts) {
294 push @ext, $pkg_discount->description;
295 my $amount = $pkg_discount->get('amount');
296 $d->{amount} -= $amount;
298 $d->{amount} = sprintf('%.2f', $d->{amount} * $self->quantity);
305 return '0.00' if $self->waive_setup eq 'Y';;
306 my $discount_amount = sum(0, map { $_->amount }
307 grep { $_->setuprecur eq 'setup' }
310 ($self->unitsetup - $discount_amount) * ($self->quantity || 1);
316 sum(0, map { $_->setup_amount } $self->quotation_pkg_tax);
321 my $discount_amount = sum(0, map { $_->amount }
322 grep { $_->setuprecur eq 'recur' }
325 ($self->unitrecur - $discount_amount) * ($self->quantity || 1);
331 sum(0, map { $_->recur_amount } $self->quotation_pkg_tax);
334 =item part_pkg_currency_option OPTIONNAME
336 Returns a two item list consisting of the currency of this quotation's customer
337 or prospect, if any, and a value for the provided option. If the customer or
338 prospect has a currency, the value is the option value the given name and the
339 currency (see L<FS::part_pkg_currency>). Otherwise, if the customer or
340 prospect has no currency, is the regular option value for the given name (see
341 L<FS::part_pkg_option>).
345 #false laziness w/cust_pkg->part_pkg_currency_option
346 sub part_pkg_currency_option {
347 my( $self, $optionname ) = @_;
348 my $part_pkg = $self->part_pkg;
349 my $prospect_or_customer = $self->cust_main || $self->prospect_main;
350 if ( my $currency = $prospect_or_customer->currency ) {
351 ($currency, $part_pkg->part_pkg_currency_option($currency, $optionname) );
353 ('', $part_pkg->option($optionname) );
359 Deletes all quotation_pkgs_details associated with this pkg (see L<FS::quotation_pkg_detail>).
366 my $oldAutoCommit = $FS::UID::AutoCommit;
367 local $FS::UID::AutoCommit = 0;
370 foreach my $detail ( qsearch('quotation_pkg_detail',{ 'quotationpkgnum' => $self->quotationpkgnum }) ) {
371 my $error = $detail->delete;
373 $dbh->rollback if $oldAutoCommit;
374 return "error removing old detail: $error";
378 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
383 =item set_details [ DETAIL, DETAIL, ... ]
385 Sets quotation details for this package (see L<FS::quotation_pkg_detail>).
387 If there is an error, returns the error, otherwise returns false.
392 my( $self, @details ) = @_;
394 my $oldAutoCommit = $FS::UID::AutoCommit;
395 local $FS::UID::AutoCommit = 0;
398 my $error = $self->delete_details;
400 $dbh->rollback if $oldAutoCommit;
404 foreach my $detail ( @details ) {
405 my $quotation_pkg_detail = new FS::quotation_pkg_detail {
406 'quotationpkgnum' => $self->quotationpkgnum,
409 $error = $quotation_pkg_detail->insert;
411 $dbh->rollback if $oldAutoCommit;
412 return "error adding new detail: $error";
416 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
425 =item cust_bill_pkg_display [ type => TYPE ]
429 sub cust_bill_pkg_display {
430 my ( $self, %opt ) = @_;
432 my $type = $opt{type} if exists $opt{type};
433 return () if $type eq 'U'; #quotations don't have usage
435 if ( $self->get('display') ) {
436 return ( grep { defined($type) ? ($type eq $_->type) : 1 }
437 @{ $self->get('display') }
442 my $setup = $self->new($self->hashref);
443 $setup->{'_NO_RECUR_KLUDGE'} = 1;
444 $setup->{'type'} = 'S';
445 my $recur = $self->new($self->hashref);
446 $recur->{'_NO_SETUP_KLUDGE'} = 1;
447 $recur->{'type'} = 'R';
449 if ( $type eq 'S' ) {
451 } elsif ( $type eq 'R' ) {
454 #return ($setup, $recur);
464 Returns the customer (L<FS::cust_main> object).
470 my $quotation = $self->quotation or return '';
471 $quotation->cust_main;
476 Returns the prospect (L<FS::prospect_main> object).
482 my $quotation = $self->quotation or return '';
483 $quotation->prospect_main;
486 sub tax_locationnum {
493 my @quotation_pkg_without_location =
494 qsearch( 'quotation_pkg', { locationnum => '' } );
495 if (@quotation_pkg_without_location) {
496 warn "setting default location on quotation_pkg records\n";
497 foreach my $quotation_pkg (@quotation_pkg_without_location) {
498 # check() will fix this
499 my $error = $quotation_pkg->replace;
501 die "quotation #".$quotation_pkg->quotationnum.": $error\n";
512 Doesn't support fees, or add-on packages.
516 L<FS::Record>, schema.html from the base documentation.