summaryrefslogtreecommitdiff
path: root/FS
diff options
context:
space:
mode:
Diffstat (limited to 'FS')
-rw-r--r--FS/FS/prospect_main.pm53
-rw-r--r--FS/FS/quotation.pm72
2 files changed, 123 insertions, 2 deletions
diff --git a/FS/FS/prospect_main.pm b/FS/FS/prospect_main.pm
index a18c8ff67..2e3c0b930 100644
--- a/FS/FS/prospect_main.pm
+++ b/FS/FS/prospect_main.pm
@@ -7,6 +7,7 @@ use Scalar::Util qw( blessed );
use FS::Record qw( dbh qsearch qsearchs );
use FS::agent;
use FS::cust_location;
+use FS::cust_main;
use FS::contact;
use FS::qual;
@@ -81,7 +82,11 @@ primary key
=item agentnum
-Agent
+Agent (see L<FS::agent>)
+
+=item refnum
+
+Referral (see L<FS::part_referral>)
=item company
@@ -240,7 +245,8 @@ sub check {
my $error =
$self->ut_numbern('prospectnum')
- || $self->ut_foreign_key('agentnum', 'agent', 'agentnum' )
+ || $self->ut_foreign_key( 'agentnum', 'agent', 'agentnum' )
+ || $self->ut_foreign_key( 'refnum', 'part_referral', 'refnum' )
|| $self->ut_textn('company')
;
return $error if $error;
@@ -316,6 +322,49 @@ sub agent {
qsearchs( 'agent', { 'agentnum' => $self->agentnum } );
}
+=item convert_cust_main
+
+Converts this prospect to a customer.
+
+If there is an error, returns an error message, otherwise, returns the
+newly-created FS::cust_main object.
+
+=cut
+
+sub convert_cust_main {
+ my $self = shift;
+
+ my @cust_location = $self->cust_location;
+ #the interface only allows one, so we're just gonna go with that for now
+
+ my @contact = $self->contact;
+
+ #XXX define one contact type as "billing", then we could pick just that one
+ my @invoicing_list = map $_->emailaddress, map $_->contact_email, @contact;
+
+ #XXX i'm not compatible with cust_main-require_phone (which is kind of a
+ # pre-contact thing anyway)
+
+ my $cust_main = new FS::cust_main {
+ 'bill_location' => $cust_location[0],
+ 'ship_location' => $cust_location[0],
+ ( map { $_ => $self->$_ } qw( agentnum refnum company ) ),
+ };
+
+ #XXX again, arbitrary, if one contact was "billing", that would be better
+ if ( $contact[0] ) {
+ $cust_main->set($_, $contact[0]->get($_)) foreach qw( first last );
+ } else {
+ $cust_main->set('first', 'Unknown');
+ $cust_main->set('last', 'Unknown');
+ }
+
+ $cust_main->insert( {}, \@invoicing_list,
+ 'prospectnum' => $self->prospectnum,
+ )
+ or $cust_main;
+}
+
=item search HASHREF
(Class method)
diff --git a/FS/FS/quotation.pm b/FS/FS/quotation.pm
index 47f13e6dc..7f53e3130 100644
--- a/FS/FS/quotation.pm
+++ b/FS/FS/quotation.pm
@@ -2,9 +2,12 @@ package FS::quotation;
use base qw( FS::Template_Mixin FS::cust_main_Mixin FS::otaker_Mixin FS::Record );
use strict;
+use Tie::RefHash;
+use FS::UID qw( dbh );
use FS::Record qw( qsearch qsearchs );
use FS::CurrentUser;
use FS::cust_main;
+use FS::cust_pkg;
use FS::prospect_main;
use FS::quotation_pkg;
@@ -212,6 +215,75 @@ sub _items_total {
sub enable_previous { 0 }
+=item convert_cust_main
+
+If this quotation already belongs to a customer, then returns that customer, as
+an FS::cust_main object.
+
+Otherwise, creates a new customer (FS::cust_main object and record, and
+associated) based on this quotation's prospect, then orders this quotation's
+packages as real packages for the customer.
+
+If there is an error, returns an error message, otherwise, returns the
+newly-created FS::cust_main object.
+
+=cut
+
+sub convert_cust_main {
+ my $self = shift;
+
+ my $cust_main = $self->cust_main;
+ return $cust_main if $cust_main; #already converted, don't again
+
+ my $oldAutoCommit = $FS::UID::AutoCommit;
+ local $FS::UID::AutoCommit = 0;
+ my $dbh = dbh;
+
+ $cust_main = $self->prospect_main->convert_cust_main;
+ unless ( ref($cust_main) ) { # eq 'FS::cust_main' ) {
+ $dbh->rollback if $oldAutoCommit;
+ return $cust_main;
+ }
+
+ $self->prospectnum('');
+ $self->custnum( $cust_main->custnum );
+ my $error = $self->replace || $self->order;
+ if ( $error ) {
+ $dbh->rollback if $oldAutoCommit;
+ return $error;
+ }
+
+ $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+
+ $cust_main;
+
+}
+
+=item order
+
+This method is for use with quotations which are already associated with a customer.
+
+Orders this quotation's packages as real packages for the customer.
+
+If there is an error, returns an error message, otherwise returns false.
+
+=cut
+
+sub order {
+ my $self = shift;
+
+ tie my %cust_pkg, 'Tie::RefHash',
+ map { FS::cust_pkg->new({ pkgpart => $_->pkgpart,
+ quantity => $_->quantity,
+ })
+ => [] #services
+ }
+ $self->quotation_pkg ;
+
+ $self->cust_main->order_pkgs( \%cust_pkg );
+
+}
+
=back
=head1 CLASS METHODS