4 use vars qw( $opt_s $opt_e $opt_r $opt_u $opt_c );
6 use Date::Parse qw(str2time);
8 use FS::UID qw(adminsuidsetup dbh);
9 use FS::Record qw(qsearch qsearchs);
15 my $user = shift or &usage;
18 my $sdate = $opt_s ? str2time($opt_s) : 0;
19 my $edate = $opt_e ? str2time($opt_e) + 86399 #not strictly correct on time zone boundary days, but good enough for this purpose
22 my $oldAutoCommit = $FS::UID::AutoCommit;
23 local $FS::UID::AutoCommit = 0;
30 foreach my $cust_pkg (
31 qsearch({ table => 'cust_pkg',
39 qsearchs({ table => 'h_cust_pkg',
41 pkgnum => $cust_pkg->pkgnum,
42 setup => { op=>'!=', value=>'' },
43 ($opt_u ? ('susp' => { op=>'!=', value=>'' })
46 ($opt_c ? ('cancel' => { op=>'!=', value=>'' })
50 extra_sql => " AND history_action IN ('insert','replace_old')".
51 ' AND history_date >= ? AND history_date <= ? ',
52 extra_param => [ [$sdate,'int'], [$edate,'int'] ],
53 order_by => 'ORDER BY history_date DESC LIMIT 1',
60 #print "restoring setup for pkgnum ". $cust_pkg->pkgnum.
61 # " (custnum ". $cust_pkg->custnum.
62 # ") to ". time2str('%D', $h_cust_pkg->setup). "\n";
63 print $cust_pkg->pkgnum. ','.
64 time2str('%D', $h_cust_pkg->setup). ','.
65 $cust_pkg->custnum. ','.
66 '"'. $cust_pkg->cust_main->name. '"'. "\n";
69 #don't actually do it yet ...
70 #$cust_pkg->set('setup', $h_cust_pkg->setup);
71 #my $error = $cust_pkg->replace;
72 ##die $error if $error;
73 #warn "error changing pkgnum ". $cust_pkg->pkgnum.': '. $error."\n";
78 $dbh->rollback or die $dbh->errstr; #if $oldAutoCommit;
80 $dbh->commit or die $dbh->errstr; #if $oldAutoCommit;
83 print "changed $changed packages\n";
86 die "usage: cust_pkg-restore_setup [ -s history_start ] [ -e history_end ] [-r ] employee_username\n";
91 cust_pkg-restore_setup
95 cust_pkg-restore_setup -d date -u history_username [ -r ] employee_username
99 Command-line tool to restore removed setup dates.
101 -s: Start date of time period to restore changes from
103 -e: End date of time period to restore changes from
105 -u: sUspended packages only
107 -c: Cancelled packages only