+sub start_copy_skel {
+ my $self = shift;
+
+ #'mg_user_preference' => {},
+ #'mg_user_indicator_profile' => { 'mg_profile_indicator' => { 'mg_profile_details' }, },
+ #'mg_watchlist_header' => { 'mg_watchlist_details' },
+ #'mg_user_grid_header' => { 'mg_user_grid_details' },
+ #'mg_portfolio_header' => { 'mg_portfolio_trades' => { 'mg_portfolio_trades_positions' } },
+ my @tables = eval($conf->config('cust_main-skeleton_tables'));
+ die $@ if $@;
+
+ _copy_skel( 'cust_main', #tablename
+ $conf->config('cust_main-skeleton_custnum'), #sourceid
+ $self->custnum, #destid
+ @tables, #child tables
+ );
+}
+
+#recursive subroutine, not a method
+sub _copy_skel {
+ my( $table, $sourceid, $destid, %child_tables ) = @_;
+
+ my $dbdef_table = dbdef->table($table);
+ my $primary_key = $dbdef_table->primary_key
+ or return "$table has no primary key".
+ " (or do you need to run dbdef-create?)";
+
+ foreach my $child_table ( keys %child_tables ) {
+
+ my $child_pkey = dbdef->table($child_table)->primary_key;
+ # or return "$table has no primary key".
+ # " (or do you need to run dbdef-create?)\n";
+ my $sequence = '';
+ if ( keys %{ $child_tables{$child_table} } ) {
+
+ return "$child_table has no primary key\n" unless $child_pkey;
+
+ #false laziness w/Record::insert and only works on Pg
+ #refactor the proper last-inserted-id stuff out of Record::insert if this
+ # ever gets use for anything besides a quick kludge for one customer
+ my $default = dbdef->table($child_table)->column($child_pkey)->default;
+ $default =~ /^nextval\(\(?'"?([\w\.]+)"?'/i
+ or return "can't parse $child_table.$child_pkey default value ".
+ " for sequence name: $default";
+ $sequence = $1;
+
+ }
+
+ my @sel_columns = grep { $_ ne $primary_key } dbdef->table($table)->columns;
+ my $sel_columns = ' ( '. join(', ', @sel_columns ). ' ) ';
+
+ my @ins_columns = grep { $_ ne $child_pkey } @sel_columns;
+ my $ins_columns = ' ( ', join(', ', $primary_key, @ins_columns ). ' ) ',
+ my $placeholders = ' ( ?, '. join(', ', map '?', @ins_columns ). ' ) ';
+
+ my $sel_sth = dbh->prepare( "SELECT $sel_columns FROM $child_table".
+ " WHERE $primary_key = $sourceid")
+ or return dbh->errstr;
+
+ $sel_sth->execute or return $sel_sth->errstr;
+
+ while ( my $row = $sel_sth->fetchrow_hashref ) {
+
+ my $ins_sth =
+ dbh->prepare("INSERT INTO $child_table $ins_columns".
+ " VALUES $placeholders")
+ or return dbh->errstr;
+ $ins_sth->execute( $destid, map $row->{$_}, @ins_columns )
+ or return $ins_sth->errstr;
+
+ #next unless keys %{ $child_tables{$child_table} };
+ next unless $sequence;
+
+ #another section of that laziness
+ my $seq_sql = "SELECT currval('$sequence')";
+ my $seq_sth = dbh->prepare($seq_sql) or return dbh->errstr;
+ $seq_sth->execute or return $seq_sth->errstr;
+ my $insertid = $seq_sth->fetchrow_arrayref->[0];
+
+ # don't drink soap! recurse! recurse! okay!
+ my $error =
+ _copy_skel( $child_table,
+ $row->{$child_pkey}, #sourceid
+ $insertid, #destid
+ %{ $child_tables{$child_table} },
+ );
+ return $error if $error;
+
+ }
+
+ }
+
+ return '';
+
+}
+