a52c56cc1a0e90847ae3e96722a42102e75aa2e6
[freeside.git] / bin / convert-477-options
1 #!/usr/bin/perl
2
3 my $user = shift;
4 use FS::UID 'adminsuidsetup';
5 use FS::Record qw(qsearch qsearchs dbh);
6 use FS::part_pkg_report_option;
7 use Text::CSV;
8
9 adminsuidsetup($user) or die "invalid user '$user'";
10 $FS::UID::AutoCommit = 1;
11 $FS::Record::nowarn_classload = 1;
12
13 print "This script will convert your per-package FCC 477 report options
14 from the classic style (part IA, IB, IIA...) to the 2014 style.
15 This is an approximate conversion, and you should review the 
16 resulting package settings for accuracy.  In particular:
17   - Broadband speeds will be set to the lowest speed in their 
18     tier.
19   - Broadband technologies for ADSL and cable modem will be set
20     to 'other ADSL' and 'other cable modem'.  You should set 
21     them to the specific ADSL or cable encapsulation in use.
22   - The 'consumer grade' vs. 'business grade' classification 
23     was introduced in 2014 and will not be set.
24
25 Configuring packages...
26 ";
27
28 my @min_download_speed = ( 0.2, 0.768, 1.5, 3, 6, 10, 25, 100 );
29 my @min_upload_speed = ( 0.1, @min_download_speed );
30 my @media_type = ( 'Copper', 'Copper', 'Copper', 'Cable Modem',
31                    'Fiber', 'Satellite', 'Fixed Wireless', 'Mobile Wireless',
32                    'Other', 'Other' );
33 my @technology = ( 10, 20, 30, 40,
34                    50, 60, 70, 80,
35                    90, 0 );
36
37 my @phone_option = (
38   'phone_longdistance:1',   # LD carrier
39   'phone_localloop:owned',  # owned loops
40   'phone_localloop:leased', # unswitched UNE loops
41   'phone_localloop:resale', # UNE-P (is pretty much extinct...)
42   'phone_localloop:resale', # UNE-P replacement
43   'media:Fiber',
44   'media:Cable Modem',
45   'media:Fixed Wireless',
46 );
47
48 my @voip_option = (
49   '', #nomadic; no longer reported
50   'media:Copper',
51   'media:Fiber',
52   'media:Cable Modem',
53   'media:Fixed Wireless',
54   'media:Other'
55 );
56
57 my %num_to_formkey = map { $_->formvalue => $_->formkey }
58                       qsearch('fcc477map', {});
59
60 sub report_option_to_fcc_option {
61   my $report_option_num = shift;
62   my $formkey = $num_to_formkey{$report_option_num}
63     or return;
64   if      ($formkey =~ /^part1_column_option_(\d+)/) {
65     #download speed
66     return (broadband_downstream => $min_download_speed[$1]);
67   } elsif ($formkey =~ /^part1_row_option_(\d+)/) {
68     #upload speed
69     return (broadband_upstream   => $min_upload_speed[$1]);
70   } elsif ($formkey =~ /^part1_technology_option_(\d+)/) {
71     #broadband tech
72     return (is_broadband  => 1,
73             media         => $media_type[$1],
74             technology    => $technology[$1]);
75   } elsif ($formkey =~ /^part2a_row_option_(\d+)/) {
76     #local phone options
77     return (split(':', $phone_option[$1]));
78   } elsif ($formkey =~ /^part2b_row_option_(\d+)/) {
79     #VoIP options (are all media types)
80     return (split(':', $voip_option[$1]));
81   } else {
82     warn "can't parse option with formkey '$formkey'\n";
83     return;
84   }
85 }
86
87 for my $part_pkg (qsearch('part_pkg', { freq => {op => '!=', value => '0'}})) {
88   my $pkgpart = $part_pkg->pkgpart;
89   print "#$pkgpart\n";
90   my %report_opts = $part_pkg->options;
91   my @fcc_opts;
92   foreach my $optionname (keys(%report_opts)) {
93     $optionname =~ /^report_option_(\d+)$/ or next;
94     my $num = $1;
95     push @fcc_opts, report_option_to_fcc_option($num);
96   }
97   # other special stuff:
98   # FCC voice class (VoIP OTT, VoIP + broadband)
99   if ($part_pkg->fcc_voip_class == 1) {
100     push @fcc_opts, 'is_voip' => 1;
101   } elsif ( $part_pkg->fcc_voip_class == 2) {
102     push @fcc_opts, 'is_voip' => 1, 'is_broadband' => 1;
103   }
104   # DS0 equivalent lines
105   if ( $part_pkg->fcc_ds0s ) {
106     if ($part_pkg->fcc_voip_class) {
107       # there's no such thing as a VoIP DS0 equivalent, but this is
108       # what we used the field for
109       push @fcc_opts, 'voip_lines' => $part_pkg->fcc_ds0s;
110     } else {
111       push @fcc_opts, 'phone_lines' => $part_pkg->fcc_ds0s, 'is_phone' => 1;
112     }
113   }
114
115   my %fcc_opts = @fcc_opts;
116   print map {"\t$_\t".$fcc_opts{$_}."\n"} keys %fcc_opts;
117   my $error = $part_pkg->process_fcc_options(\%fcc_opts);
118   if ( $error ) {
119     die "$error\n";
120   }
121   print "\n";
122 }
123
124 print "Finished.\n";
125