6 use FS::Record 'dbdef';
11 my $eventpart = $opt{e};
12 my $part_event = FS::part_event->by_key($opt{e})
13 or die "usage: initialize-event -e <eventpart> <username>\n";
16 my $eventtable = $part_event->eventtable;
17 my $pkey = dbdef->table($eventtable)->primary_key;
18 my $from = " LEFT JOIN (SELECT DISTINCT tablenum AS $pkey FROM cust_event
19 WHERE eventpart = $eventpart) AS done USING ($pkey)",
20 my $where = " WHERE done.$pkey IS NULL";
22 my $count = FS::Record->scalar_sql("SELECT COUNT(*) FROM $eventtable $from $where");
23 print "Event ".$part_event->event."\n".
24 "Will initialize on $count $eventtable records.\n";
26 print "Run with -x to make changes.\n";
31 print "Disabling event.\n";
32 $part_event->disabled('Y');
33 my $error = $part_event->replace;
35 my $cursor = FS::Cursor->new({
40 my $user = $FS::CurrentUser::CurrentUser->username;
41 my $statustext = "Manually by $user";
42 while (my $record = $cursor->fetch) {
43 my $cust_event = FS::cust_event->new({
45 eventpart => $eventpart,
46 tablenum => $record->get($pkey),
48 statustext => $statustext,
50 $error = $cust_event->insert;
52 print "$eventtable #".$record->get($pkey).": $error\n" if $error;
57 print "$count unprocessed records.";
59 print "Re-enabling event.\n";
60 $part_event->disabled('');
61 $error = $part_event->replace;
64 print "Event is still disabled.\n";