summaryrefslogtreecommitdiff
path: root/bin/freeside-migrate-events
blob: f33a0ad0e9a52f507b79f90b49ffd2b54d820071 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/perl -w

use strict;

use FS::UID qw(adminsuidsetup);
use FS::Record qw( qsearch );
use FS::part_bill_event;
use FS::part_event;
use FS::cust_bill_event;
use FS::cust_event;

my $user = shift or die &usage;
adminsuidsetup($user);

my %plan2action = (
  'fee'                    => 'fee',
  'fee_percent'            => 'NOTYET', #XXX need fee_percent action
  'suspend'                => 'suspend',
  'suspend-if-balance'     => 'NOTYET', #XXX "if balance" becomes a balance condition
  'suspend-if-pkgpart'     => 'suspend_if_pkgpart',
  'suspend-unless-pkgpart' => 'suspend_unless_pkgpart',
  'cancel'                 => 'cancel',
  'addpost'                => 'addpost',
  'comp'                   => 'NOTYET', #XXX or N/A or something
  'credit'                 => 'NOTYET',
  'realtime-card'          => 'cust_bill_realtime_card',
  'realtime-check'         => 'cust_bill_realtime_check',
  'realtime-lec'           => 'cust_bill_realtime_lec',
  'batch-card'             => 'cust_bill_batch',
  #?'retriable'             =>
  'send'                   => 'cust_bill_send',
  'send_email'             => 'NOTYET', 
  'send_alternate'         => 'cust_bill_send_alternate',
  'send_if_newest'         => 'cust_bill_send_if_newest',
  'send_agent'             => 'cust_bill_send_agent',
  'send_csv_ftp'           => 'cust_bill_send_csv_ftp',
  'spool_csv',             => 'cust_bill_spool_csv',
  'bill'                   => 'bill',
  'apply'                  => 'apply',
  'collect'                => 'collect',
);


foreach my $part_bill_event (
  qsearch({
    'table'   => 'part_bill_event',
  })
) {

  print $part_bill_event->event;

  my $action = $plan2action{ $part_bill_event->plan };

  if ( $action eq 'NOTYET' ) {
    warn "not migrating part_bill_event.eventpart ".$part_bill_event->eventpart.
         "; ". $part_bill_event->plan. " plan not (yet) handled";
    next;
  } elsif ( ! $action ) {
    warn "not migrating part_bill_event.eventpart ".$part_bill_event->eventpart.
         "; unknown plan ". $part_bill_event->plan;
    next;
  }

  my %plandata = map { /^(\w+) (.*)$/; ($1, $2); }
                     split(/\n/, $part_bill_event->plandata);

  #XXX may need to fudge some plandata2option names!!!

  my $part_event = new FS::part_event {
    'event'      => $part_bill_event->event,
    'eventtable' => 'cust_bill',
    'check_freq' => $part_bill_event->freq || '1d',
    'weight'     => $part_bill_event->weight,
    'action'     => $action,
    'disabled'   => $part_bill_event->disabled,
  };

  my $error = $part_event->insert(\%plandata);
  die "error inserting part_event: $error\n" if $error;

  print ' '. $part_event->eventpart;

  my $once = new FS::part_event_condition {
    'eventpart'     => $part_event->eventpart,
    'conditionname' => 'once'
  };
  $error = $once->insert;
  die $error if $error;
  
  my $balance = new FS::part_event_condition {
    'eventpart'     => $part_event->eventpart,
    'conditionname' => 'balance'
  };
  $error = $balance->insert( 'balance' => 0 );
  die $error if $error;

  my $payby = new FS::part_event_condition {
    'eventpart'     => $part_event->eventpart,
    'conditionname' => 'payby'
  };
  $error = $payby->insert( 'payby' => { $part_bill_event->payby => 1 } );
  die $error if $error;

  if ( $part_bill_event->seconds ) {

    my $age = new FS::part_event_condition { 
      'eventpart'     => $part_event->eventpart,
      'conditionname' => 'cust_bill_age'
    };
    $error = $age->insert( 'age' => ($part_bill_event->seconds/86400 ).'d' );
    die $error if $error;

  }
  
  #my $derror = $part_bill_event->delete;
  #die "error removing part_bill_event: $derror\n" if $derror;

  foreach my $cust_bill_event (
    qsearch({
      'table'     => 'cust_bill_event',
      'hashref'   => { 'eventpart' => $part_bill_event->eventpart, },
    })
  ) {

    my $cust_event = new FS::cust_event {
      'eventpart'  => $part_event->eventpart,
      'tablenum'   => $cust_bill_event->invnum,
      '_date'      => $cust_bill_event->_date,
      'status'     => $cust_bill_event->status,
      'statustext' => $cust_bill_event->statustext,
    };

    my $cerror = $cust_event->insert;
    #die "error inserting cust_event: $cerror\n" if $cerror;
    warn "error inserting cust_event: $cerror\n" if $cerror;
  
    #my $dcerror = $cust_bill_event->delete;
    #die "error removing cust_bill_event: $dcerror\n" if $dcerror;

    print ".";

  }

  print "\n";

}

sub usage {
  die "Usage:\n  freeside-migrate-events user\n"; 
}

=head1 NAME

freeside-migrate-events - Migrates 1.7/1.8-style invoice events to
                          1.9/2.0-style billing events

=head1 SYNOPSIS

  freeside-migrate-events

=head1 DESCRIPTION

Migrates events from L<FS::part_bill_event> to L<FS::part_event> and friends,
and from L<FS::cust_bill_event> records to L<FS::cust_event>

=head1 BUGS

Doesn't migrate any action options yet.

Doesn't translate option names that changed.

Doesn't migrate reasons.

Doesn't delete the old events (which is not a big deal, since the new code
won't run them...)

=head1 SEE ALSO

=cut

1;

__END__

#part_bill_event      part_event
#
#eventpart            n/a
#event                event
#freq                 check_freq
#payby                part_event_condition.conditionname = payby
#eventcode            PARSE_WITH_REGEX (probably can just get from plandata)
#seconds              part_event_condition.conditionname = cust_bill_age
#plandata             PARSE_WITH_REGEX (along with eventcode, yuck)
#reason               part_event_option.optionname = reason
#disabled             disabled
#

    #these might help parse existing eventcode

    $c =~ /^\s*\$cust_main\->(suspend|cancel|invoicing_list_addpost|bill|collect)\(\);\s*("";)?\s*$/

      or $c =~ /^\s*\$cust_bill\->(comp|realtime_(card|ach|lec)|batch_card|send)\((%options)*\);\s*$/

      or $c =~ /^\s*\$cust_bill\->send(_if_newest)?\(\'[\w\-\s]+\'\s*(,\s*(\d+|\[\s*\d+(,\s*\d+)*\s*\])\s*,\s*'[\w\@\.\-\+]*'\s*)?\);\s*$/

#      or $c =~ /^\s*\$cust_main\->apply_payments; \$cust_main->apply_credits; "";\s*$/
      or $c =~ /^\s*\$cust_main\->apply_payments_and_credits; "";\s*$/

      or $c =~ /^\s*\$cust_main\->charge\( \s*\d*\.?\d*\s*,\s*\'[\w \!\@\#\$\%\&\(\)\-\+\;\:\"\,\.\?\/]*\'\s*\);\s*$/

      or $c =~ /^\s*\$cust_main\->suspend_(if|unless)_pkgpart\([\d\,\s]*\);\s*$/

      or $c =~ /^\s*\$cust_bill\->cust_suspend_if_balance_over\([\d\.\s]*\);\s*$/

      or do {
        #log
        return "illegal eventcode: $c";
      };

  }