Passed
Push — master ( 26caeb...0e28f7 )
by Thomas
11:18
created

migratePayments::handle()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 13
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 21
rs 9.8333
1
<?php
2
3
namespace App\Console\Commands;
4
5
use App\Models\Enrollment;
6
use App\Models\Invoice;
7
use Illuminate\Console\Command;
8
use Illuminate\Support\Facades\DB;
9
10
class migratePayments extends Command
11
{
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'migrate:payments';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Command description';
25
26
    public function handle() : void
27
    {
28
        foreach (Enrollment::all() as $enrollment) {
29
            $payments = DB::table('payments')->where('enrollment_id', $enrollment->id)->get();
30
31
            if ($payments->count() > 0) {
32
                foreach ($payments as $payment) {
33
                    // generate an Invoice
34
                    $invoice = Invoice::create([
35
                        'invoice_type_id' => 1,
36
                        'receipt_number' => $payment->receipt_id,
37
                        'total_price' => $payment->value / 100,
38
                        'date' => $payment->created_at,
39
                        'created_at' => $payment->created_at,
40
                    ]);
41
42
                    $invoice->setNumber();
43
44
                    $enrollment->invoices()->attach($invoice);
45
46
                    DB::table('payments')->where('id', $payment->id)->update(['invoice_id' => $invoice->id]);
47
                }
48
            }
49
        }
50
    }
51
}
52