Add migration for invoices

This commit is contained in:
Philipp Lang 2023-12-20 21:45:37 +01:00
parent 3a3b20ed72
commit acf280346f
1 changed files with 58 additions and 0 deletions

View File

@ -1,5 +1,6 @@
<?php
use App\Invoice\BillKind;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
@ -34,6 +35,63 @@ return new class extends Migration
$table->unsignedBigInteger('price');
$table->timestamps();
});
foreach (DB::table('subscriptions')->get() as $subscription) {
$children = DB::table('subscription_children')->where('parent_id', $subscription->id)->get();
if ($subscription->split === 1) {
foreach ($children as $child) {
$newName = 'Beitrag {year} für {name} (' . $child->name . ')';
DB::table('subscription_children')->where('id', $child->id)->update(['name' => $newName]);
}
} else {
DB::table('subscription_children')->where('parent_id', $subscription->id)->delete();
DB::table('subscription_children')->insert([
'id' => Str::uuid()->toString(),
'name' => 'Beitrag {year} für {name} (' . $subscription->name . ')',
'amount' => $children->sum('amount'),
'parent_id' => $subscription->id,
]);
}
}
$paymentGroups = DB::table('payments')->where('status_id', 2)->get()->groupBy(function ($payment) {
$member = DB::table('members')->where('id', $payment->member_id)->first();
return $member->lastname . $member->address . $member->location . $member->zip;
});
foreach ($paymentGroups as $payments) {
$member = DB::table('members')->where('id', $payments->first()->member_id)->first();
$invoiceId = DB::table('invoices')->insertGetId([
'to' => json_encode([
'name' => 'Familie ' . $member->lastname,
'address' => $member->address,
'zip' => $member->zip,
'location' => $member->location,
]),
'greeting' => 'Liebe Familie ' . $member->lastname,
'status' => 'Rechnung gestellt',
'via' => BillKind::fromValue($member->bill_kind)->value,
'usage' => 'Mitgliedsbeitrag für ' . $member->lastname,
'mail_email' => $member->email_parents ?: $member->email,
'last_remembered_at' => $payments->first()->last_remembered_at,
'sent_at' => $payments->first()->last_remembered_at,
]);
foreach ($payments as $payment) {
$subscription = DB::table('subscriptions')->where('id', $payment->subscription_id)->first();
$subscriptionChildren = DB::table('subscription_children')->where('parent_id', $subscription->id)->get();
$paymentMember = DB::table('members')->where('id', $payment->member_id)->first();
foreach ($subscriptionChildren as $child) {
DB::table('invoice_positions')->insert([
'invoice_id' => $invoiceId,
'description' => str($child->name)->replace('{name}', $paymentMember->firstname . ' ' . $paymentMember->lastname)->replace('{year}', $payment->nr),
'price' => $child->amount,
'member_id' => $member->id,
]);
}
}
}
Schema::dropIfExists('payments');
}
/**