+# extract details (false laziness with /misc/order_pkg.html)
+my $details = {
+ 'invoice_detail' => [],
+ 'package_comment' => [],
+ 'quotation_detail' => [],
+};
+foreach my $field ( $cgi->param ) {
+ foreach my $detailtype ( keys %$details ) {
+ if ($field =~ /^$detailtype(\d+)$/) {
+ $details->{$detailtype}->[$1] = $cgi->param($field);
+ }
+ }
+}
+foreach my $detailtype ( keys %$details ) {
+ @{ $details->{$detailtype} } = grep { length($_) } @{ $details->{$detailtype} };
+}
+
+if ( $quotationnum ) {
+
+ $quotation_pkg = new FS::quotation_pkg \%hash;
+ $quotation_pkg->quotationnum($quotationnum);
+ $quotation_pkg->prospectnum($prospect_main->prospectnum) if $prospect_main;
+
+ my %opt = @{ $details->{'quotation_detail'} }
+ ? (
+ quotation_details => $details->{'quotation_detail'},
+ copy_on_order => scalar($cgi->param('copy_on_order')) ? 'Y' : '',
+ )
+ : ();
+
+ if ( $locationnum == -1 ) {
+ my $cust_location = FS::cust_location->new({
+ 'custnum' => $cust_main ? $cust_main->custnum : '',
+ 'prospectnum' => $prospect_main ? $prospect_main->prospectnum : '',
+ map { $_ => scalar($cgi->param($_)) }
+ FS::cust_main->location_fields
+ });
+ $opt{'cust_location'} = $cust_location;
+ } else {
+ $opt{'locationnum'} = $locationnum;
+ }
+
+ $error = $quotation_pkg->insert(%opt);
+
+} else {
+
+ $cust_pkg = new FS::cust_pkg \%hash;
+
+ $cust_pkg->no_auto( scalar($cgi->param('no_auto')) );
+
+ my %opt = ( 'cust_pkg' => $cust_pkg );
+
+ if ( $contactnum == -1 ) {
+ my $contact = FS::contact->new({
+ 'custnum' => scalar($cgi->param('custnum')),
+ map { $_ => scalar($cgi->param("contactnum_$_")) } qw( first last )
+ });
+ $opt{'contact'} = $contact;
+ }
+
+ if ( $locationnum == -1 ) {
+ my $cust_location = FS::cust_location->new({
+ map { $_ => scalar($cgi->param($_)) }
+ ('custnum', FS::cust_main->location_fields)
+ });
+ $opt{'cust_location'} = $cust_location;
+ } else {
+ $opt{'locationnum'} = $locationnum;
+ }
+
+ $opt{'invoice_details'} = $details->{'invoice_detail'} if @{ $details->{'invoice_detail'} };
+ $opt{'package_comments'} = $details->{'package_comment'} if @{ $details->{'package_comment'} };
+
+ $error = $cust_main->order_pkg( \%opt );
+
+}