2dfb0c88eca328a1d8df61fb14f4f49910cb9674
[freeside.git] / bin / svc_broadband_update_speeds
1 #!/usr/bin/perl
2
3 use strict;
4 use Getopt::Std;
5 use FS::UID qw(adminsuidsetup);
6 use FS::Record qw(qsearch qsearchs);
7 use FS::svc_broadband;
8 use Data::Dumper;
9
10 ###
11 # parse command line
12 ###
13
14 use vars qw( $opt_h $opt_v $opt_e $opt_s $opt_c $opt_r $opt_p $opt_d );
15 getopts('hves:c:r:pd:');
16
17 warn ("running\n");
18
19 my $user = shift or die &usage;
20 adminsuidsetup $user;
21
22 sub usage { "
23   Usage:
24       svc_broadband_update_speeds [ -h help] [ -v verbose] [ -e export service ] [ -s service_part_num (required) ] [ -c sibling service_part_num ] [ -r (speed rate in KB 'up,down') ] [ -p (get speed from package fcc rate) ] [ -d directory for exception file (required) ] user (required)\n
25       A directory for the exception file, freeside user name and a service to update is required.\n
26       Must set one or more of options p, c, or r. \n
27       Also must run this report as user freeside.\n
28       Option r up and down speed seperated by a comma in kbps.
29       Getting speed from option p (package fcc rates) first if set, if no rates found then checks for option c (rate of sibling service) if set, if still no rates found checks for rate in option r if set.  If no rates found service will be placed in exception file.
30       By default serivce will not export if there is a export assigned to service.  Setting option e will perform the export.
31 " }
32
33 unless ($opt_d && $opt_s) { die &usage(); }
34 if ($opt_h) { die &usage(); }
35 unless ($opt_p || $opt_c || $opt_r) { die &usage(); }
36
37 ### get list of all provisioned services
38 my $extra_sql = " WHERE cust_svc.svcpart = $opt_s";
39 my @services = qsearch({
40     'select'    => 'svc_broadband.*, cust_svc.svcpart, cust_svc.pkgnum, cust_pkg.pkgpart',
41     'table'     => 'svc_broadband',
42     'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum ) LEFT JOIN cust_pkg USING (pkgnum)',
43     'extra_sql' => $extra_sql,
44 });
45
46 ### get list of all unprovisioned services
47 my $ups_extra_sql = "where cust_pkg.cancel is null and pkg_svc.quantity > 0 and  pkg_svc.quantity > (select count(1) from cust_svc where  cust_svc.pkgnum = cust_pkg.pkgnum and  cust_svc.svcpart = pkg_svc.svcpart)  and pkg_svc.svcpart = $opt_s";
48 my @unprovisioned_services = qsearchs({
49     'table'     => 'cust_pkg',
50     'addl_from' => 'JOIN pkg_svc using (pkgpart)',
51     'extra_sql' => $ups_extra_sql,
52 });
53
54 my $speed;
55 $speed = 'package' if $opt_p;
56
57 foreach my $svc (@services) {
58   _update_service($svc);
59 }
60
61 sub _update_service {
62   my $service = shift;
63   my $speed_up;
64   my $speed_down;
65
66   my $package = qsearchs({
67      'table'     => 'part_pkg',
68      'hashref'   => { 'pkgpart' => $service->pkgpart, },
69   });
70
71   ## get speed from package fcc option first if option p
72   if ($opt_p) {
73         warn ("Getting speed for service ".$service->description."(".$service->svcnum.") from package fcc info\n") if $opt_v;
74      $speed_up = $package->fcc_option('broadband_upstream') * 1000;
75      $speed_down = $package->fcc_option('broadband_downstream') * 1000;
76   }
77
78   ## if no fcc option get speed from sibling broadband service if option c
79   if ((!$speed_up || !$speed_down) && $opt_c) {
80         warn ("Getting speed for service ".$service->description."(".$service->svcnum.") from sibling service of package ".$service->pkgnum) if $opt_v;
81      my $sibling_service = qsearchs({
82        'select'    => 'svc_broadband.*, cust_svc.svcpart',
83        'table'     => 'svc_broadband',
84        'addl_from' => ' LEFT JOIN cust_svc USING ( svcnum )',
85        'extra_sql' => ' WHERE cust_svc.pkgnum = '.$service->pkgnum.' AND cust_svc.svcpart = '.$opt_c.' AND (svc_broadband.speed_up IS NOT NULL AND svc_broadband.speed_down IS NOT NULL)',
86      });
87      $speed_up = $sibling_service->speed_up if $sibling_service;
88      $speed_down = $sibling_service->speed_down if $sibling_service;
89   }
90      
91   ## if no fcc options and no speed from sibling service than get speed from option r if option r is set.
92   if ((!$speed_up || !$speed_down) && $opt_r) {
93         warn ("Getting speed for service ".$service->description."(".$service->svcnum.") from option r ($opt_r)\n") if $opt_v;
94      ($speed_up, $speed_down) = split /\,/, $opt_r;
95      warn ("Option r speeds not correct.  Must be in kbps up and down seperated by comma. [ -r xxxxxx,xxxxxx ]\n") if $opt_v && (!$speed_up || !$speed_down);
96   }
97
98   ## update service with new speed.
99   if ($speed_up && $speed_down) {
100     $service->set('speed_up', $speed_up);
101     $service->set('speed_down', $speed_down);
102
103     warn("updating service ".$service->description."(".$service->svcnum.") with upload speed ($speed_up) and download speed ($speed_down)\n") if $opt_v;
104     $service->set('no_export', $opt_e);
105     my $error = $service->replace();
106     warn($error) if $error;
107     ###todo: if no error provision service if not provisioned ie new svc_broadband.
108   }
109   else {
110     open(FILE, ">$opt_d/svcbroadband_update_exceptions.txt")
111       or die "can't open $opt_d: $!";
112       print FILE $service->description."(".$service->svcnum.") Has no up or download speed and could not set one.\n";
113     close FILE or die "can't close $opt_d: $!";
114     warn($service->description."(".$service->svcnum.") Has no up or download speed\n") if $opt_v;
115   }
116   return;
117 }
118
119 exit;
120
121 =head2 svc_broadband_update_speeds
122
123 This script allows for the mas update of up and down speeds for a svc_broadband service.
124
125 the script will obtain the new speed from option p (package fcc rates) first if set, 
126 if no rates found then checks for option c (rate of sibling service) if set, 
127 if still no rates found checks for rate in option r if set.  
128 If no rates found service will be placed in exception file. 
129
130 Script must be run as user freeside.
131 Options -s, -d and freeside user are required.
132
133 example:
134 sudo -u freeside ./svc_broadband_update_speeds -v -s 4 -c 2 -r 148000,248000 -p -d /home/freeside/ freesideuser
135
136 =cut