5 use FS::UID qw(adminsuidsetup);
6 use FS::Record qw( qsearch );
7 use FS::part_bill_event;
9 use FS::cust_bill_event;
12 my $user = shift or die &usage;
13 adminsuidsetup($user);
17 'fee_percent' => 'NOTYET', #XXX need fee_percent action
18 'suspend' => 'suspend',
19 'suspend-if-balance' => 'NOTYET', #XXX "if balance" becomes a balance condition
20 'suspend-if-pkgpart' => 'suspend_if_pkgpart',
21 'suspend-unless-pkgpart' => 'suspend_unless_pkgpart',
23 'addpost' => 'addpost',
24 'comp' => 'NOTYET', #XXX or N/A or something
26 'realtime-card' => 'cust_bill_realtime_card',
27 'realtime-check' => 'cust_bill_realtime_check',
28 'realtime-lec' => 'cust_bill_realtime_lec',
29 'batch-card' => 'cust_bill_batch',
31 'send' => 'cust_bill_send',
32 'send_email' => 'NOTYET',
33 'send_alternate' => 'cust_bill_send_alternate',
34 'send_if_newest' => 'cust_bill_send_if_newest',
35 'send_agent' => 'cust_bill_send_agent',
36 'send_csv_ftp' => 'cust_bill_send_csv_ftp',
37 'spool_csv', => 'cust_bill_spool_csv',
40 'collect' => 'collect',
44 foreach my $part_bill_event (
46 'table' => 'part_bill_event',
50 print $part_bill_event->event;
52 my $action = $plan2action{ $part_bill_event->plan };
54 if ( $action eq 'NOTYET' ) {
55 warn "not migrating part_bill_event.eventpart ".$part_bill_event->eventpart.
56 "; ". $part_bill_event->plan. " plan not (yet) handled";
58 } elsif ( ! $action ) {
59 warn "not migrating part_bill_event.eventpart ".$part_bill_event->eventpart.
60 "; unknown plan ". $part_bill_event->plan;
64 my %plandata = map { /^(\w+) (.*)$/; ($1, $2); }
65 split(/\n/, $part_bill_event->plandata);
67 #XXX may need to fudge some plandata2option names!!!
69 my $part_event = new FS::part_event {
70 'event' => $part_bill_event->event,
71 'eventtable' => 'cust_bill',
72 'check_freq' => $part_bill_event->freq || '1d',
73 'weight' => $part_bill_event->weight,
75 'disabled' => $part_bill_event->disabled,
78 my $error = $part_event->insert(\%plandata);
79 die "error inserting part_event: $error\n" if $error;
81 print ' '. $part_event->eventpart;
83 my $once = new FS::part_event_condition {
84 'eventpart' => $part_event->eventpart,
85 'conditionname' => 'once'
87 $error = $once->insert;
90 my $balance = new FS::part_event_condition {
91 'eventpart' => $part_event->eventpart,
92 'conditionname' => 'balance'
94 $error = $balance->insert( 'balance' => 0 );
97 my $cust_bill_owed = new FS::part_event_condition {
98 'eventpart' => $part_event->eventpart,
99 'conditionname' => 'cust_bill_owed'
101 $error = $cust_bill_owed->insert( 'owed' => 0 );
102 die $error if $error;
104 my $payby = new FS::part_event_condition {
105 'eventpart' => $part_event->eventpart,
106 'conditionname' => 'payby'
108 $error = $payby->insert( 'payby' => { $part_bill_event->payby => 1 } );
109 die $error if $error;
111 if ( $part_bill_event->seconds ) {
113 my $age = new FS::part_event_condition {
114 'eventpart' => $part_event->eventpart,
115 'conditionname' => 'cust_bill_age'
117 $error = $age->insert( 'age' => ($part_bill_event->seconds/86400 ).'d' );
118 die $error if $error;
122 #my $derror = $part_bill_event->delete;
123 #die "error removing part_bill_event: $derror\n" if $derror;
125 foreach my $cust_bill_event (
127 'table' => 'cust_bill_event',
128 'hashref' => { 'eventpart' => $part_bill_event->eventpart, },
132 my $cust_event = new FS::cust_event {
133 'eventpart' => $part_event->eventpart,
134 'tablenum' => $cust_bill_event->invnum,
135 '_date' => $cust_bill_event->_date,
136 'status' => $cust_bill_event->status,
137 'statustext' => $cust_bill_event->statustext,
140 my $cerror = $cust_event->insert;
141 #die "error inserting cust_event: $cerror\n" if $cerror;
142 warn "error inserting cust_event: $cerror\n" if $cerror;
144 #my $dcerror = $cust_bill_event->delete;
145 #die "error removing cust_bill_event: $dcerror\n" if $dcerror;
156 die "Usage:\n freeside-migrate-events user\n";
161 freeside-migrate-events - Migrates 1.7/1.8-style invoice events to
162 1.9/2.0-style billing events
166 freeside-migrate-events
170 Migrates events from L<FS::part_bill_event> to L<FS::part_event> and friends,
171 and from L<FS::cust_bill_event> records to L<FS::cust_event>
175 Doesn't migrate any action options yet.
177 Doesn't translate option names that changed.
179 Doesn't migrate reasons.
181 Doesn't delete the old events (which is not a big deal, since the new code
192 #part_bill_event part_event
197 #payby part_event_condition.conditionname = payby
198 #eventcode PARSE_WITH_REGEX (probably can just get from plandata)
199 #seconds part_event_condition.conditionname = cust_bill_age
200 #plandata PARSE_WITH_REGEX (along with eventcode, yuck)
201 #reason part_event_option.optionname = reason
205 #these might help parse existing eventcode
207 $c =~ /^\s*\$cust_main\->(suspend|cancel|invoicing_list_addpost|bill|collect)\(\);\s*("";)?\s*$/
209 or $c =~ /^\s*\$cust_bill\->(comp|realtime_(card|ach|lec)|batch_card|send)\((%options)*\);\s*$/
211 or $c =~ /^\s*\$cust_bill\->send(_if_newest)?\(\'[\w\-\s]+\'\s*(,\s*(\d+|\[\s*\d+(,\s*\d+)*\s*\])\s*,\s*'[\w\@\.\-\+]*'\s*)?\);\s*$/
213 # or $c =~ /^\s*\$cust_main\->apply_payments; \$cust_main->apply_credits; "";\s*$/
214 or $c =~ /^\s*\$cust_main\->apply_payments_and_credits; "";\s*$/
216 or $c =~ /^\s*\$cust_main\->charge\( \s*\d*\.?\d*\s*,\s*\'[\w \!\@\#\$\%\&\(\)\-\+\;\:\"\,\.\?\/]*\'\s*\);\s*$/
218 or $c =~ /^\s*\$cust_main\->suspend_(if|unless)_pkgpart\([\d\,\s]*\);\s*$/
220 or $c =~ /^\s*\$cust_bill\->cust_suspend_if_balance_over\([\d\.\s]*\);\s*$/
224 return "illegal eventcode: $c";