Completed
Push — master ( adb5f1...9300e7 )
by Nasrul Hazim
01:47
created

OpenPayrollDemoSeeder::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
use Illuminate\Database\Seeder;
4
5
class OpenPayrollDemoSeeder extends Seeder
6
{
7
    public $faker;
8
9
    /**
10
     * Run the database seeds.
11
     */
12
    public function run()
13
    {
14
        $this->faker = \Faker\Factory::create();
15
        $this->seedEmployees();
16
        $this->seedPayroll();
17
        $this->calculatePayroll();
18
    }
19
20
    private function seedEmployees()
21
    {
22
        $this->command->info('Seeding employees...');
23
        factory(\App\Models\OpenPayroll\Employee::class, 3)->create()->each(function($user) {
24
            $user->addresses()->create([
25
                'primary'    => $this->faker->address,
26
                'postcode'   => $this->faker->postcode,
27
                'city'       => $this->faker->city,
28
                'state'      => $this->faker->state,
29
                'country_id' => $this->faker->randomElement(range(1, 100)),
30
                'is_default' => true,
31
            ]);
32
33
            $user->phones()->create([
34
                'phone_type_id' => $this->faker->randomElement([1, 2]),
35
                'phone_number'  => $this->faker->phoneNumber,
36
                'is_default'    => true,
37
            ]);
38
39
            $user->banks()->create([
40
                'bank_id'    => $this->faker->randomElement(range(1, 50)),
41
                'account_no' => $this->faker->bankAccountNumber,
42
            ]);
43
44
            $user->position()->create([
45
                'name' => $this->faker->jobTitle,
46
            ]);
47
48
            $user->salary()->create([
49
                'amount' => $this->faker->randomElement(range(10000, 20000)) * 100,
50
            ]);
51
        });
52
    }
53
54
    private function seedPayroll()
55
    {
56
        $this->command->info('Seeding payroll...');
57
        $admin = \App\Models\OpenPayroll\Admin::create([
58
            'name'     => 'OpenPayroll Admin',
59
            'email'    => '[email protected]',
60
            'password' => bcrypt('password'),
61
        ]);
62
63
        for ($month = 1; $month <= 12; ++$month) {
64
            $admin->payrolls()->create([
65
                'month' => now()->addMonths($month)->format('n'),
66
                'year'  => now()->addMonths($month)->format('Y'),
67
                'date'  => now()->addMonths($month)->format('Y-m-d'),
68
            ]);
69
        }
70
    }
71
72
    private function calculatePayroll()
73
    {
74
        $payrolls = \App\Models\OpenPayroll\Payroll::all();
75
76
        \App\Models\OpenPayroll\Employee::with('salary')->get()->each(function($employee) use ($payrolls) {
77
            $payrolls->each(function($payroll) use ($employee) {
78
                $payroll->payslips()->create([
79
                    'user_id'      => $employee->id,
80
                    'payroll_id'   => $payroll->id,
81
                    'basic_salary' => optional($employee->salary)->amount ?? 0,
82
                    'gross_salary' => optional($employee->salary)->amount ?? 0,
83
                    'net_salary'   => optional($employee->salary)->amount ?? 0,
84
                ]);
85
86
                $payslip = $payroll->payslips()->first();
87
88
                // earnings
89
                config('open-payroll.models.earning')::create([
90
                    'user_id'         => $employee->id,
91
                    'payroll_id'      => $payroll->id,
92
                    'payslip_id'      => $payslip->id,
93
                    'earning_type_id' => 1,
94
                    'name'            => 'Basic Salary',
95
                    'description'     => 'Basic Salary',
96
                    'amount'          => optional($employee->salary)->amount ?? 0,
97
                ]);
98
99
                // deduction
100
                // @todo
101
            });
102
        });
103
    }
104
}
105