2 use base qw( FS::Template_Mixin FS::cust_main_Mixin FS::otaker_Mixin FS::Record );
7 use FS::Maketext qw( emt );
8 use FS::Record qw( qsearch qsearchs );
12 use FS::prospect_main;
13 use FS::quotation_pkg;
17 FS::quotation - Object methods for quotation records
23 $record = new FS::quotation \%hash;
24 $record = new FS::quotation { 'column' => 'value' };
26 $error = $record->insert;
28 $error = $new_record->replace($old_record);
30 $error = $record->delete;
32 $error = $record->check;
36 An FS::quotation object represents a quotation. FS::quotation inherits from
37 FS::Record. The following fields are currently supported:
74 Creates a new quotation. To add the quotation to the database, see L<"insert">.
76 Note that this stores the hash reference, not a distinct copy of the hash it
77 points to. You can ask the object for a copy with the I<hash> method.
81 sub table { 'quotation'; }
82 sub notice_name { 'Quotation'; }
83 sub template_conf { 'quotation_'; }
87 Adds this record to the database. If there is an error, returns the error,
88 otherwise returns false.
92 Delete this record from the database.
94 =item replace OLD_RECORD
96 Replaces the OLD_RECORD with this one in the database. If there is an error,
97 returns the error, otherwise returns false.
101 Checks all fields to make sure this is a valid quotation. If there is
102 an error, returns the error, otherwise returns false. Called by the insert
111 $self->ut_numbern('quotationnum')
112 || $self->ut_foreign_keyn('prospectnum', 'prospect_main', 'prospectnum' )
113 || $self->ut_foreign_keyn('custnum', 'cust_main', 'custnum' )
114 || $self->ut_numbern('_date')
115 || $self->ut_enum('disabled', [ '', 'Y' ])
116 || $self->ut_numbern('usernum')
118 return $error if $error;
120 $self->_date(time) unless $self->_date;
122 $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
124 return 'prospectnum or custnum must be specified'
125 if ! $self->prospectnum
137 qsearchs('prospect_main', { 'prospectnum' => $self->prospectnum } );
146 qsearchs('cust_main', { 'custnum' => $self->custnum } );
153 sub cust_bill_pkg { #actually quotation_pkg objects
155 qsearch('quotation_pkg', { quotationnum=>$self->quotationnum });
164 $self->_total('setup');
167 =item total_recur [ FREQ ]
173 #=item total_recur [ FREQ ]
174 #my $freq = @_ ? shift : '';
175 $self->_total('recur');
179 my( $self, $method ) = @_;
182 $total += $_->$method() for $self->cust_bill_pkg;
183 sprintf('%.2f', $total);
189 my $opt = shift || {};
190 if ($opt and !ref($opt)) {
191 die ref($self). '->email called with positional parameters';
194 my $conf = $self->conf;
196 my $from = delete $opt->{from};
198 # this is where we set the From: address
199 $from ||= $conf->config('quotation_from', $self->cust_or_prospect->agentnum )
200 || $conf->config('invoice_from', $self->cust_or_prospect->agentnum );
202 $self->SUPER::email( {
213 $self->conf->config('quotation_subject') #, $self->cust_main->agentnum)
216 #my $cust_main = $self->cust_main;
217 #my $name = $cust_main->name;
218 #my $name_short = $cust_main->name_short;
219 #my $invoice_number = $self->invnum;
220 #my $invoice_date = $self->_date_pretty;
225 =item cust_or_prosect
229 sub cust_or_prospect {
231 $self->custnum ? $self->cust_main : $self->prospect_main;
234 =item cust_or_prospect_label_link P
236 HTML links to either the customer or prospect.
238 Returns a list consisting of two elements. The first is a text label for the
239 link, and the second is the URL.
243 sub cust_or_prospect_label_link {
244 my( $self, $p ) = @_;
246 if ( my $custnum = $self->custnum ) {
247 my $display_custnum = $self->cust_main->display_custnum;
248 my $target = $FS::CurrentUser::CurrentUser->default_customer_view eq 'jumbo'
250 : ';show=quotations';
252 emt("View this customer (#[_1])",$display_custnum) =>
253 "${p}view/cust_main.cgi?custnum=$custnum$target"
255 } elsif ( my $prospectnum = $self->prospectnum ) {
257 emt("View this prospect (#[_1])",$prospectnum) =>
258 "${p}view/prospect_main.html?$prospectnum"
266 #prevent things from falsely showing up as taxes, at least until we support
267 # quoting tax amounts..
272 shift->cust_bill_pkg;
276 my( $self, $total_items ) = @_;
278 if ( $self->total_setup > 0 ) {
279 push @$total_items, {
280 'total_item' => $self->mt( $self->total_recur > 0 ? 'Total Setup' : 'Total' ),
281 'total_amount' => $self->total_setup,
285 #could/should add up the different recurring frequencies on lines of their own
286 # but this will cover the 95% cases for now
287 if ( $self->total_recur > 0 ) {
288 push @$total_items, {
289 'total_item' => $self->mt('Total Recurring'),
290 'total_amount' => $self->total_recur,
296 =item enable_previous
300 sub enable_previous { 0 }
302 =item convert_cust_main
304 If this quotation already belongs to a customer, then returns that customer, as
305 an FS::cust_main object.
307 Otherwise, creates a new customer (FS::cust_main object and record, and
308 associated) based on this quotation's prospect, then orders this quotation's
309 packages as real packages for the customer.
311 If there is an error, returns an error message, otherwise, returns the
312 newly-created FS::cust_main object.
316 sub convert_cust_main {
319 my $cust_main = $self->cust_main;
320 return $cust_main if $cust_main; #already converted, don't again
322 my $oldAutoCommit = $FS::UID::AutoCommit;
323 local $FS::UID::AutoCommit = 0;
326 $cust_main = $self->prospect_main->convert_cust_main;
327 unless ( ref($cust_main) ) { # eq 'FS::cust_main' ) {
328 $dbh->rollback if $oldAutoCommit;
332 $self->prospectnum('');
333 $self->custnum( $cust_main->custnum );
334 my $error = $self->replace || $self->order;
336 $dbh->rollback if $oldAutoCommit;
340 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
348 This method is for use with quotations which are already associated with a customer.
350 Orders this quotation's packages as real packages for the customer.
352 If there is an error, returns an error message, otherwise returns false.
359 tie my %cust_pkg, 'Tie::RefHash',
360 map { FS::cust_pkg->new({ pkgpart => $_->pkgpart,
361 quantity => $_->quantity,
365 $self->quotation_pkg ;
367 $self->cust_main->order_pkgs( \%cust_pkg );
377 qsearch('quotation_pkg', { 'quotationnum' => $self->quotationnum } );
382 Disables this quotation (sets disabled to Y, which hides the quotation on
383 prospects and customers).
385 If there is an error, returns an error message, otherwise returns false.
391 $self->disabled('Y');
397 Enables this quotation.
399 If there is an error, returns an error message, otherwise returns false.
416 =item search_sql_where HASHREF
418 Class method which returns an SQL WHERE fragment to search for parameters
419 specified in HASHREF. Valid parameters are
425 List reference of start date, end date, as UNIX timestamps.
435 List reference of charged limits (exclusive).
439 List reference of charged limits (exclusive).
443 flag, return open invoices only
447 flag, return net invoices only
455 Note: validates all passed-in data; i.e. safe to use with unchecked CGI params.
459 sub search_sql_where {
460 my($class, $param) = @_;
462 # warn "$me search_sql_where called with params: \n".
463 # join("\n", map { " $_: ". $param->{$_} } keys %$param ). "\n";
469 if ( $param->{'agentnum'} =~ /^(\d+)$/ ) {
470 push @search, "( prospect_main.agentnum = $1 OR cust_main.agentnum = $1 )";
474 # if ( $param->{'refnum'} =~ /^(\d+)$/ ) {
475 # push @search, "cust_main.refnum = $1";
479 if ( $param->{'prospectnum'} =~ /^(\d+)$/ ) {
480 push @search, "quotation.prospectnum = $1";
484 if ( $param->{'custnum'} =~ /^(\d+)$/ ) {
485 push @search, "cust_bill.custnum = $1";
489 if ( $param->{_date} ) {
490 my($beginning, $ending) = @{$param->{_date}};
492 push @search, "quotation._date >= $beginning",
493 "quotation._date < $ending";
497 if ( $param->{'quotationnum_min'} =~ /^(\d+)$/ ) {
498 push @search, "quotation.quotationnum >= $1";
500 if ( $param->{'quotationnum_max'} =~ /^(\d+)$/ ) {
501 push @search, "quotation.quotationnum <= $1";
505 # if ( $param->{charged} ) {
506 # my @charged = ref($param->{charged})
507 # ? @{ $param->{charged} }
508 # : ($param->{charged});
510 # push @search, map { s/^charged/cust_bill.charged/; $_; }
514 my $owed_sql = FS::cust_bill->owed_sql;
517 push @search, "quotation._date < ". (time-86400*$param->{'days'})
520 #agent virtualization
521 my $curuser = $FS::CurrentUser::CurrentUser;
522 #false laziness w/search/quotation.html
523 push @search,' ( '. $curuser->agentnums_sql( table=>'prospect_main' ).
524 ' OR '. $curuser->agentnums_sql( table=>'cust_main' ).
527 join(' AND ', @search );
537 L<FS::Record>, schema.html from the base documentation.