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'; }
105 # detail table uses non-quotation fieldnames, see billpkgnum below
106 sub detail_table { 'quotation_pkg_detail'; }
110 Sets/returns quotationpkgnum, for ease of integration with TemplateItem_Mixin::details
116 $self->quotationpkgnum(@_);
121 Adds this record to the database. If there is an error, returns the error,
122 otherwise returns false.
127 my ($self, %options) = @_;
130 my $oldAutoCommit = $FS::UID::AutoCommit;
131 local $FS::UID::AutoCommit = 0;
133 my $error = $self->SUPER::insert;
135 if ( !$error and $self->discountnum ) {
136 warn "inserting discount #".$self->discountnum."\n";
137 $error = $self->insert_discount;
138 $error .= ' (setting discount)' if $error;
142 $dbh->rollback if $oldAutoCommit;
145 $dbh->commit if $oldAutoCommit;
152 Delete this record from the database.
160 my $oldAutoCommit = $FS::UID::AutoCommit;
161 local $FS::UID::AutoCommit = 0;
163 my $error = $self->delete_details;
165 $dbh->rollback if $oldAutoCommit;
169 foreach ($self->quotation_pkg_discount, $self->quotation_pkg_tax) {
172 $dbh->rollback if $oldAutoCommit;
173 return $error . ' (deleting discount)';
177 $error = $self->SUPER::delete;
179 $dbh->rollback if $oldAutoCommit;
182 $dbh->commit if $oldAutoCommit;
185 $self->quotation->estimate;
188 =item replace OLD_RECORD
190 Replaces the OLD_RECORD with this one in the database. If there is an error,
191 returns the error, otherwise returns false.
195 Checks all fields to make sure this is a valid quotation package. If there is
196 an error, returns the error, otherwise returns false. Called by the insert
204 my @freqs = ('', keys (%{ FS::Misc->pkg_freqs }));
207 $self->ut_numbern('quotationpkgnum')
208 || $self->ut_foreign_key( 'quotationnum', 'quotation', 'quotationnum' )
209 || $self->ut_foreign_key( 'pkgpart', 'part_pkg', 'pkgpart' )
210 || $self->ut_foreign_keyn( 'locationnum', 'cust_location', 'locationnum' )
211 || $self->ut_numbern('start_date')
212 || $self->ut_numbern('contract_end')
213 || $self->ut_numbern('quantity')
214 || $self->ut_moneyn('unitsetup')
215 || $self->ut_moneyn('unitrecur')
216 || $self->ut_enum('freq', \@freqs)
217 || $self->ut_enum('waive_setup', [ '', 'Y'] )
220 if ($self->locationnum eq '') {
221 # use the customer default
222 my $quotation = $self->quotation;
223 if ($quotation->custnum) {
224 $self->set('locationnum', $quotation->cust_main->ship_locationnum);
225 } elsif ($quotation->prospectnum) {
226 # use the first non-disabled location for that prospect
227 my $cust_location = qsearchs('cust_location',
228 { prospectnum => $quotation->prospectnum,
230 $self->set('locationnum', $cust_location->locationnum) if $cust_location;
231 } # else the quotation is invalid
234 return $error if $error;
239 #it looks redundant with a v4.x+ auto-generated method, but need to override
240 # FS::TemplateItem_Mixin's version
243 qsearchs('part_pkg', { 'pkgpart' => $self->pkgpart } );
248 $self->part_pkg->pkg;
253 =item insert_discount
255 Associates this package with a discount (see L<FS::cust_pkg_discount>,
256 possibly inserting a new discount on the fly (see L<FS::discount>). Properties
257 of the discount will be taken from this object.
261 sub insert_discount {
262 #my ($self, %options) = @_;
265 my $quotation_pkg_discount = FS::quotation_pkg_discount->new( {
266 'quotationpkgnum' => $self->quotationpkgnum,
267 'discountnum' => $self->discountnum,
268 #for the create a new discount case
269 '_type' => $self->discountnum__type,
270 'amount' => $self->discountnum_amount,
271 'percent' => $self->discountnum_percent,
272 'months' => $self->discountnum_months,
273 'setup' => $self->discountnum_setup,
276 $quotation_pkg_discount->insert;
282 my $setuprecur = $options{'setuprecur'};
284 # kind of silly treating this as multiple records, but it works, and will
285 # work if we allow multiple discounts at some point
286 my @pkg_discounts = $self->pkg_discount;
287 return if @pkg_discounts == 0;
292 description => $self->mt('Discount'),
294 ext_description => \@ext,
295 # maybe should show quantity/unit discount?
297 foreach my $pkg_discount (@pkg_discounts) {
298 push @ext, $pkg_discount->description;
299 my $amount = $pkg_discount->get($setuprecur.'_amount');
300 $d->{amount} -= $amount;
302 $d->{amount} = sprintf('%.2f', $d->{amount} * $self->quantity);
309 ($self->unitsetup - sum(0, map { $_->setup_amount } $self->pkg_discount))
310 * ($self->quantity || 1);
316 sum(0, map { $_->setup_amount } $self->quotation_pkg_tax);
321 ($self->unitrecur - sum(0, map { $_->recur_amount } $self->pkg_discount))
322 * ($self->quantity || 1)
327 sum(0, map { $_->recur_amount } $self->quotation_pkg_tax);
330 =item part_pkg_currency_option OPTIONNAME
332 Returns a two item list consisting of the currency of this quotation's customer
333 or prospect, if any, and a value for the provided option. If the customer or
334 prospect has a currency, the value is the option value the given name and the
335 currency (see L<FS::part_pkg_currency>). Otherwise, if the customer or
336 prospect has no currency, is the regular option value for the given name (see
337 L<FS::part_pkg_option>).
341 #false laziness w/cust_pkg->part_pkg_currency_option
342 sub part_pkg_currency_option {
343 my( $self, $optionname ) = @_;
344 my $part_pkg = $self->part_pkg;
345 my $prospect_or_customer = $self->cust_main || $self->prospect_main;
346 if ( my $currency = $prospect_or_customer->currency ) {
347 ($currency, $part_pkg->part_pkg_currency_option($currency, $optionname) );
349 ('', $part_pkg->option($optionname) );
355 Deletes all quotation_pkgs_details associated with this pkg (see L<FS::quotation_pkg_detail>).
362 my $oldAutoCommit = $FS::UID::AutoCommit;
363 local $FS::UID::AutoCommit = 0;
366 foreach my $detail ( qsearch('quotation_pkg_detail',{ 'billpkgnum' => $self->quotationpkgnum }) ) {
367 my $error = $detail->delete;
369 $dbh->rollback if $oldAutoCommit;
370 return "error removing old detail: $error";
374 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
379 =item set_details [ DETAIL, DETAIL, ... ]
381 Sets quotation details for this package (see L<FS::quotation_pkg_detail>).
383 If there is an error, returns the error, otherwise returns false.
388 my( $self, @details ) = @_;
390 my $oldAutoCommit = $FS::UID::AutoCommit;
391 local $FS::UID::AutoCommit = 0;
394 my $error = $self->delete_details;
396 $dbh->rollback if $oldAutoCommit;
400 foreach my $detail ( @details ) {
401 my $quotation_pkg_detail = new FS::quotation_pkg_detail {
402 'billpkgnum' => $self->quotationpkgnum,
405 $error = $quotation_pkg_detail->insert;
407 $dbh->rollback if $oldAutoCommit;
408 return "error adding new detail: $error";
412 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
417 =item cust_bill_pkg_display [ type => TYPE ]
421 sub cust_bill_pkg_display {
422 my ( $self, %opt ) = @_;
424 my $type = $opt{type} if exists $opt{type};
425 return () if $type eq 'U'; #quotations don't have usage
427 if ( $self->get('display') ) {
428 return ( grep { defined($type) ? ($type eq $_->type) : 1 }
429 @{ $self->get('display') }
434 my $setup = $self->new($self->hashref);
435 $setup->{'_NO_RECUR_KLUDGE'} = 1;
436 $setup->{'type'} = 'S';
437 my $recur = $self->new($self->hashref);
438 $recur->{'_NO_SETUP_KLUDGE'} = 1;
439 $recur->{'type'} = 'R';
441 if ( $type eq 'S' ) {
443 } elsif ( $type eq 'R' ) {
446 #return ($setup, $recur);
456 Returns the customer (L<FS::cust_main> object).
462 my $quotation = $self->quotation or return '';
463 $quotation->cust_main;
468 Returns the prospect (L<FS::prospect_main> object).
474 my $quotation = $self->quotation or return '';
475 $quotation->prospect_main;
478 sub tax_locationnum {
485 my @quotation_pkg_without_location =
486 qsearch( 'quotation_pkg', { locationnum => '' } );
487 if (@quotation_pkg_without_location) {
488 warn "setting default location on quotation_pkg records\n";
489 foreach my $quotation_pkg (@quotation_pkg_without_location) {
490 # check() will fix this
491 my $error = $quotation_pkg->replace;
493 die "quotation #".$quotation_pkg->quotationnum.": $error\n";
504 Doesn't support fees, or add-on packages.
508 L<FS::Record>, schema.html from the base documentation.