Added factory and seeder for sub-bookings.

This commit is contained in:
Lukas Halbritter
2018-09-20 16:48:56 +02:00
parent c9bc400308
commit b38d7ea54e
4 changed files with 57 additions and 5 deletions
+50 -1
View File
@@ -1,9 +1,58 @@
<?php
use Faker\Generator as Faker;
use Carbon\Carbon;
use App\Project;
use App\AccountType;
use App\PersonTarget;
use App\Booking;
$factory->define(App\SubBooking::class, function (Faker $faker) {
// Select randomly an existing target from the database
$randomPersonTarget = PersonTarget::all()->random();
$randomAccountType = null;
// Randomly decide, if the target is paid, underpaid or overpaid
if (rand_bool(20)) {
// Overpaid
if ($randomPersonTarget->ioType->name === 'Einnahme') {
$randomAccountType = AccountType::incomes()->random();
$amount = $randomPersonTarget->difference() + rand(1, 100);
} else {
$randomAccountType = AccountType::expeditures()->random();
$amount = $randomPersonTarget->difference() + rand(1, 100);
}
} else if (rand_bool(20)) {
// Underpaid
if ($randomPersonTarget->ioType->name === 'Einnahme') {
$randomAccountType = AccountType::incomes()->random();
$amount = $randomPersonTarget->difference() - rand(1, $randomPersonTarget->difference());
} else {
$randomAccountType = AccountType::expeditures()->random();
$amount = $randomPersonTarget->difference() - rand(1, $randomPersonTarget->difference());
}
} else {
if ($randomPersonTarget->ioType->name === 'Einnahme') {
$randomAccountType = AccountType::incomes()->random();
$amount = $randomPersonTarget->difference();
} else {
$randomAccountType = AccountType::expeditures()->random();
$amount = $randomPersonTarget->difference();
}
}
return [
''
'booking_id' => Booking::all('id')->random()->id,
'project_id' => Project::all('id')->random()->id,
'account_type_id' => $randomAccountType->id,
'person_target_id' => $randomPersonTarget->id,
'reference' => $faker->text(50),
'amount' => $amount,
'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
'updated_at' => Carbon::now()->format('Y-m-d H:i:s')
];
});