Passed
Push — master ( 99708e...74870e )
by Thomas
14:05
created

fixIncorrectDiscountValues::handle()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 16
rs 9.9332
1
<?php
2
3
namespace App\Console\Commands;
4
5
use App\Models\Discount;
6
use App\Models\Enrollment;
7
use App\Models\Invoice;
8
use Illuminate\Console\Command;
9
10
class fixIncorrectDiscountValues extends Command
11
{
12
    protected $signature = 'academico:fix-discount-values';
13
14
    protected $description = 'Command description';
15
16
    public function handle()
17
    {
18
        $anomalies = 0;
19
        foreach (Invoice::all() as $invoice) {
20
            if ($invoice->invoiceDetails->where('product_type', Discount::class)->count() > 0) {
21
                $enrollmentValue = $invoice->invoiceDetails->where('product_type', Enrollment::class)->sum('price');
22
                foreach ($invoice->invoiceDetails->where('product_type', Discount::class) as $discount) {
23
                    $discountValue = $discount->price / 100;
24
                    $discount->update(['price' => ($discountValue * $enrollmentValue)]);
25
                    $anomalies++;
26
                    echo "\ninvoice ".$invoice->id . "fixed";
27
                }
28
29
            }
30
        }
31
        echo "\n".$anomalies.' anomalies fixed';
32
    }
33
}
34