Passed
Pull Request — master (#13021)
by Kenny
11:48
created

BillingCalculate   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 13
eloc 102
c 1
b 0
f 1
dl 0
loc 149
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
C handle() 0 118 12
1
<?php
2
3
namespace App\Console\Commands;
4
5
use App\Models\Bill;
6
use App\Models\BillHistory;
7
use Illuminate\Console\Command;
8
use LibreNMS\Util\Number;
9
10
require_once base_path() . '/includes/init.php';
11
12
class BillingCalculate extends Command
13
{
14
    /**
15
     * The name and signature of the console command.
16
     *
17
     * @var string
18
     */
19
    protected $signature = 'billing:calculate {--r}';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'calculate the port usage for the billing.';
27
28
    /**
29
     * Create a new command instance.
30
     *
31
     * @return void
32
     */
33
    public function __construct()
34
    {
35
        parent::__construct();
36
    }
37
38
    /**
39
     * Execute the console command.
40
     *
41
     * @return int
42
     */
43
    public function handle()
44
    {
45
        if ($this->option('r')) {
46
            $this->info("Clearing history table.\n");
47
            BillHistory::truncate();
48
        }
49
        //get all bills and order by bill_id
50
        foreach (Bill::orderBy('bill_id')->get() as $bill) {
51
            $this->info(str_pad($bill->bill_id . ' ' . $bill->bill_name, 30) . " \n");
52
            $i = 0;
53
            while ($i <= 24) {
54
                unset($class);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $class seems to be never defined.
Loading history...
55
                unset($rate_data);
56
                $day_data = getDates($bill->bill_day, $i);
57
58
                $datefrom = $day_data['0'];
59
                $dateto = $day_data['1'];
60
                $check = BillHistory::where(['bill_id' => $bill->bill_id, 'bill_datefrom' => $datefrom, 'bill_dateto' => $dateto])->first();
61
                $period = getPeriod($bill['bill_id'], $datefrom, $dateto);
62
                $date_updated = str_replace('-', '', str_replace(':', '', str_replace(' ', '', $check['updated'])));
63
                if ($period > 0 && $dateto > $date_updated) {
64
                    $rate_data = getRates($bill['bill_id'], $datefrom, $dateto, $dir_95th);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dir_95th does not seem to be defined for all execution paths leading up to this point.
Loading history...
65
                    $rate_95th = $rate_data['rate_95th'];
0 ignored issues
show
Unused Code introduced by
The assignment to $rate_95th is dead and can be removed.
Loading history...
66
                    $dir_95th = $rate_data['dir_95th'];
67
                    $total_data = $rate_data['total_data'];
0 ignored issues
show
Unused Code introduced by
The assignment to $total_data is dead and can be removed.
Loading history...
68
                    $rate_average = $rate_data['rate_average'];
0 ignored issues
show
Unused Code introduced by
The assignment to $rate_average is dead and can be removed.
Loading history...
69
70
                    if ($bill['bill_type'] == 'cdr') {
71
                        $type = 'CDR';
72
                        $allowed = $bill['bill_cdr'];
73
                        $used = $rate_data['rate_95th'];
74
                        $allowed_text = Number::formatSi($allowed, 2, 3, 'bps');
75
                        $used_text = Number::formatSi($used, 2, 3, 'bps');
76
                        $overuse = ($used - $allowed);
77
                        $overuse = (($overuse <= 0) ? '0' : $overuse);
78
                        $percent = round((($rate_data['rate_95th'] / $bill['bill_cdr']) * 100), 2);
79
                    } elseif ($bill['bill_type'] == 'quota') {
80
                        $type = 'Quota';
81
                        $allowed = $bill['bill_quota'];
82
                        $used = $rate_data['total_data'];
83
                        $allowed_text = format_bytes_billing($allowed);
84
                        $used_text = format_bytes_billing($used);
85
                        $overuse = ($used - $allowed);
86
                        $overuse = (($overuse <= 0) ? '0' : $overuse);
87
                        $percent = round((($rate_data['total_data'] / $bill['bill_quota']) * 100), 2);
88
                    }
89
90
                    $this->info(strftime('%x @ %X', strtotime($datefrom)) . ' to ' . strftime('%x @ %X', strtotime($dateto)) . ' ' . str_pad($type, 8) . ' ' . str_pad($allowed_text, 10) . ' ' . str_pad($used_text, 10) . ' ' . $percent . '%');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $allowed_text does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $type does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $used_text does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $percent does not seem to be defined for all execution paths leading up to this point.
Loading history...
91
92
                    if ($i == '0') {
93
                        $update = [
94
                            'rate_95th' => $rate_data['rate_95th'],
95
                            'rate_95th_in' => $rate_data['rate_95th_in'],
96
                            'rate_95th_out' => $rate_data['rate_95th_out'],
97
                            'dir_95th' => $rate_data['dir_95th'],
98
                            'total_data' => $rate_data['total_data'],
99
                            'total_data_in' => $rate_data['total_data_in'],
100
                            'total_data_out' => $rate_data['total_data_out'],
101
                            'rate_average' => $rate_data['rate_average'],
102
                            'rate_average_in' => $rate_data['rate_average_in'],
103
                            'rate_average_out' => $rate_data['rate_average_out'],
104
                            'bill_last_calc' => ['NOW()'],
105
                        ];
106
                        Bill::where('bill_id', $bill->bill_id)->update($update);
107
                        $this->info('Updated!');
108
                    }
109
                    if ($check['bill_id'] == $bill['bill_id']) {
110
                        $update = [
111
                            'rate_95th' => $rate_data['rate_95th'],
112
                            'rate_95th_in' => $rate_data['rate_95th_in'],
113
                            'rate_95th_out' => $rate_data['rate_95th_out'],
114
                            'dir_95th' => $rate_data['dir_95th'],
115
                            'rate_average' => $rate_data['rate_average'],
116
                            'rate_average_in' => $rate_data['rate_average_in'],
117
                            'rate_average_out' => $rate_data['rate_average_out'],
118
                            'traf_total' => $rate_data['total_data'],
119
                            'traf_in' => $rate_data['total_data_in'],
120
                            'traf_out' => $rate_data['total_data_out'],
121
                            'bill_used' => $used,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $used does not seem to be defined for all execution paths leading up to this point.
Loading history...
122
                            'bill_overuse' => $overuse,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $overuse does not seem to be defined for all execution paths leading up to this point.
Loading history...
123
                            'bill_percent' => $percent,
124
                            'updated' => ['NOW()'],
125
                        ];
126
                        BillHistory::where('bill_hist_id', $check->bill_hist_id)->update($update);
127
                        $this->info('Updated history!');
128
                    } else {
129
                        $update = [
130
                            'rate_95th' => $rate_data['rate_95th'],
131
                            'rate_95th_in' => $rate_data['rate_95th_in'],
132
                            'rate_95th_out' => $rate_data['rate_95th_out'],
133
                            'dir_95th' => $rate_data['dir_95th'],
134
                            'rate_average' => $rate_data['rate_average'],
135
                            'rate_average_in' => $rate_data['rate_average_in'],
136
                            'rate_average_out' => $rate_data['rate_average_out'],
137
                            'traf_total' => $rate_data['total_data'],
138
                            'traf_in' => $rate_data['total_data_in'],
139
                            'traf_out' => $rate_data['total_data_out'],
140
                            'bill_datefrom' => $datefrom,
141
                            'bill_dateto' => $dateto,
142
                            'bill_type' => $type,
143
                            'bill_allowed' => $allowed,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $allowed does not seem to be defined for all execution paths leading up to this point.
Loading history...
144
                            'bill_used' => $used,
145
                            'bill_overuse' => $overuse,
146
                            'bill_percent' => $percent,
147
                            'bill_id' => $bill['bill_id'],
148
                        ];
149
                        BillHistory::insert($update);
150
//                        dbInsert($update, 'bill_history');
151
                        $this->info('Generated history!');
152
                    }//end if
153
                    echo "\n\n";
154
                }//end if
155
156
                $i++;
157
            }
158
        }
159
160
        return 0;
161
    }
162
}
163