+sub insert {
+ my $oldAutoCommit = $FS::UID::AutoCommit;
+ local $FS::UID::AutoCommit = 0;
+ my $dbh = dbh;
+
+ my $self = shift;
+ my $error = $self->SUPER::insert(@_);
+ if ( $error ) {
+ $dbh->rollback if $oldAutoCommit;
+ return $error if $error;
+ }
+
+ if ( $self->link_type eq 'supp' ) {
+ # queue this?
+ my @main_pkgs = qsearch('cust_pkg', {
+ pkgpart => $self->src_pkgpart,
+ cancel => '',
+ });
+ foreach my $main_pkg (@main_pkgs) {
+ # duplicates code in FS::cust_pkg::uncancel, sort of
+ my $supp_pkg = FS::cust_pkg->new({
+ 'pkgpart' => $self->dst_pkgpart,
+ 'pkglinknum' => $self->pkglinknum,
+ 'main_pkgnum' => $main_pkg->pkgnum,
+ 'order_date' => time,
+ map { $_ => $main_pkg->get($_) }
+ qw( custnum locationnum pkgbatch
+ start_date setup expire adjourn contract_end bill susp
+ refnum discountnum waive_setup quantity
+ recur_show_zero setup_show_zero )
+ });
+ $error = $supp_pkg->insert;
+ if ( $error ) {
+ $dbh->rollback if $oldAutoCommit;
+ return "$error (ordering new supplemental package to pkg#".$main_pkg->pkgnum.")" if $error;
+ }
+ }
+
+ return $error if $error;
+ }
+
+ $dbh->commit if $oldAutoCommit;
+ return;
+}