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. Accepts the following options:
111 quotation_details - optional arrayref of detail strings to add (creates quotation_pkg_detail records)
113 copy_on_order - value for this field when creating quotation_pkg_detail records (same for all details)
118 my ($self, %options) = @_;
121 my $oldAutoCommit = $FS::UID::AutoCommit;
122 local $FS::UID::AutoCommit = 0;
124 my $error = $self->SUPER::insert;
126 if ( !$error and ($self->setup_discountnum || $self->recur_discountnum) ) {
127 #warn "inserting discount\n";
128 $error = $self->insert_discount;
129 $error .= ' (setting discount)' if $error;
133 $dbh->rollback if $oldAutoCommit;
137 if ($options{'quotation_details'}) {
138 $error = $self->set_details(
139 details => $options{'quotation_details'},
140 copy_on_order => $options{'copy_on_order'} ? 'Y' : '',
143 $error .= ' (setting details)';
144 $dbh->rollback if $oldAutoCommit;
149 $dbh->commit if $oldAutoCommit;
155 Delete this record from the database.
163 my $oldAutoCommit = $FS::UID::AutoCommit;
164 local $FS::UID::AutoCommit = 0;
166 my $error = $self->delete_details;
168 $dbh->rollback if $oldAutoCommit;
172 foreach ($self->quotation_pkg_discount, $self->quotation_pkg_tax) {
175 $dbh->rollback if $oldAutoCommit;
176 return $error . ' (deleting discount)';
180 $error = $self->SUPER::delete;
182 $dbh->rollback if $oldAutoCommit;
185 $dbh->commit if $oldAutoCommit;
188 $self->quotation->estimate;
191 =item replace OLD_RECORD
193 Replaces the OLD_RECORD with this one in the database. If there is an error,
194 returns the error, otherwise returns false.
198 Checks all fields to make sure this is a valid quotation package. If there is
199 an error, returns the error, otherwise returns false. Called by the insert
207 my @freqs = ('', keys (%{ FS::Misc->pkg_freqs }));
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('freq', \@freqs)
220 || $self->ut_enum('waive_setup', [ '', 'Y'] )
223 if ($self->locationnum eq '') {
224 # use the customer default
225 my $quotation = $self->quotation;
226 if ($quotation->custnum) {
227 $self->set('locationnum', $quotation->cust_main->ship_locationnum);
228 } elsif ($quotation->prospectnum) {
229 # use the first non-disabled location for that prospect
230 my $cust_location = qsearchs('cust_location',
231 { prospectnum => $quotation->prospectnum,
233 $self->set('locationnum', $cust_location->locationnum) if $cust_location;
234 } # else the quotation is invalid
237 return $error if $error;
242 #it looks redundant with a v4.x+ auto-generated method, but need to override
243 # FS::TemplateItem_Mixin's version
246 qsearchs('part_pkg', { 'pkgpart' => $self->pkgpart } );
251 $self->part_pkg->pkg;
256 =item insert_discount
258 Associates this package with a discount (see L<FS::cust_pkg_discount>,
259 possibly inserting a new discount on the fly (see L<FS::discount>). Properties
260 of the discount will be taken from this object.
264 sub insert_discount {
265 #my ($self, %options) = @_;
268 foreach my $x (qw(setup recur)) {
269 if ( my $discountnum = $self->get("${x}_discountnum") ) {
270 my $cust_pkg_discount = FS::quotation_pkg_discount->new( {
271 'quotationpkgnum' => $self->quotationpkgnum,
272 'discountnum' => $discountnum,
274 #for the create a new discount case
275 'amount' => $self->get("${x}_discountnum_amount"),
276 'percent' => $self->get("${x}_discountnum_percent"),
277 'months' => $self->get("${x}_discountnum_months"),
279 if ( $x eq 'setup' ) {
280 $cust_pkg_discount->setup('Y');
281 $cust_pkg_discount->months('');
283 my $error = $cust_pkg_discount->insert;
284 return $error if $error;
292 my $setuprecur = $options{'setuprecur'};
293 # a little different from cust_bill_pkg::_item_discount, in that this one
294 # is asked specifically whether to show setup or recur discounts (because
295 # on the quotation they're separate sections entirely)
297 my @pkg_discounts = grep { $_->setuprecur eq $setuprecur }
299 return if @pkg_discounts == 0;
304 description => $self->mt('Discount'),
306 ext_description => \@ext,
307 # maybe should show quantity/unit discount?
309 foreach my $pkg_discount (@pkg_discounts) {
310 push @ext, $pkg_discount->description;
311 my $amount = $pkg_discount->get('amount');
312 $d->{amount} -= $amount;
314 $d->{amount} = sprintf('%.2f', $d->{amount} * $self->quantity);
321 return '0.00' if $self->waive_setup eq 'Y';;
322 my $discount_amount = sum(0, map { $_->amount }
323 grep { $_->setuprecur eq 'setup' }
326 ($self->unitsetup - $discount_amount) * ($self->quantity || 1);
332 sum(0, map { $_->setup_amount } $self->quotation_pkg_tax);
337 my $discount_amount = sum(0, map { $_->amount }
338 grep { $_->setuprecur eq 'recur' }
341 ($self->unitrecur - $discount_amount) * ($self->quantity || 1);
347 sum(0, map { $_->recur_amount } $self->quotation_pkg_tax);
350 =item part_pkg_currency_option OPTIONNAME
352 Returns a two item list consisting of the currency of this quotation's customer
353 or prospect, if any, and a value for the provided option. If the customer or
354 prospect has a currency, the value is the option value the given name and the
355 currency (see L<FS::part_pkg_currency>). Otherwise, if the customer or
356 prospect has no currency, is the regular option value for the given name (see
357 L<FS::part_pkg_option>).
361 #false laziness w/cust_pkg->part_pkg_currency_option
362 sub part_pkg_currency_option {
363 my( $self, $optionname ) = @_;
364 my $part_pkg = $self->part_pkg;
365 my $prospect_or_customer = $self->cust_main || $self->prospect_main;
366 if ( my $currency = $prospect_or_customer->currency ) {
367 ($currency, $part_pkg->part_pkg_currency_option($currency, $optionname) );
369 ('', $part_pkg->option($optionname) );
375 Deletes all quotation_pkgs_details associated with this pkg (see L<FS::quotation_pkg_detail>).
382 my $oldAutoCommit = $FS::UID::AutoCommit;
383 local $FS::UID::AutoCommit = 0;
386 foreach my $detail ( qsearch('quotation_pkg_detail',{ 'quotationpkgnum' => $self->quotationpkgnum }) ) {
387 my $error = $detail->delete;
389 $dbh->rollback if $oldAutoCommit;
390 return "error removing old detail: $error";
394 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
399 =item set_details PARAM
401 Sets new quotation details for this package (see L<FS::quotation_pkg_detail>),
402 removing existing details.
404 Recognizes the following parameters:
406 details - arrayref of strings, one for each new detail
408 copy_on_order - if true, sets copy_on_order flag on new details
410 If there is an error, returns the error, otherwise returns false.
418 $opt{'details'} ||= [];
419 my @details = @{$opt{'details'}};
421 my $oldAutoCommit = $FS::UID::AutoCommit;
422 local $FS::UID::AutoCommit = 0;
425 my $error = $self->delete_details;
427 $dbh->rollback if $oldAutoCommit;
431 foreach my $detail ( @details ) {
432 my $quotation_pkg_detail = new FS::quotation_pkg_detail {
433 'quotationpkgnum' => $self->quotationpkgnum,
435 'copy_on_order' => $opt{'copy_on_order'} ? 'Y' : '',
437 $error = $quotation_pkg_detail->insert;
439 $dbh->rollback if $oldAutoCommit;
440 return "error adding new detail: $error";
444 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
453 =item cust_bill_pkg_display [ type => TYPE ]
457 sub cust_bill_pkg_display {
458 my ( $self, %opt ) = @_;
460 my $type = $opt{type} if exists $opt{type};
461 return () if $type eq 'U'; #quotations don't have usage
463 if ( $self->get('display') ) {
464 return ( grep { defined($type) ? ($type eq $_->type) : 1 }
465 @{ $self->get('display') }
470 my $setup = $self->new($self->hashref);
471 $setup->{'_NO_RECUR_KLUDGE'} = 1;
472 $setup->{'type'} = 'S';
473 my $recur = $self->new($self->hashref);
474 $recur->{'_NO_SETUP_KLUDGE'} = 1;
475 $recur->{'type'} = 'R';
477 if ( $type eq 'S' ) {
479 } elsif ( $type eq 'R' ) {
482 #return ($setup, $recur);
492 Returns the customer (L<FS::cust_main> object).
498 my $quotation = $self->quotation or return '';
499 $quotation->cust_main;
504 Returns the prospect (L<FS::prospect_main> object).
510 my $quotation = $self->quotation or return '';
511 $quotation->prospect_main;
514 sub tax_locationnum {
521 my @quotation_pkg_without_location =
522 qsearch( 'quotation_pkg', { locationnum => '' } );
523 if (@quotation_pkg_without_location) {
524 warn "setting default location on quotation_pkg records\n";
525 foreach my $quotation_pkg (@quotation_pkg_without_location) {
526 # check() will fix this
527 my $error = $quotation_pkg->replace;
529 die "quotation #".$quotation_pkg->quotationnum.": $error\n";
540 Doesn't support fees, or add-on packages.
544 L<FS::Record>, schema.html from the base documentation.