From acf280346fa1ab155fd02faea8e910779efb727e Mon Sep 17 00:00:00 2001 From: Philipp Lang Date: Wed, 20 Dec 2023 21:45:37 +0100 Subject: [PATCH] Add migration for invoices --- ...023_12_12_015320_create_invoices_table.php | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/database/migrations/2023_12_12_015320_create_invoices_table.php b/database/migrations/2023_12_12_015320_create_invoices_table.php index af962e0b..67bffca6 100644 --- a/database/migrations/2023_12_12_015320_create_invoices_table.php +++ b/database/migrations/2023_12_12_015320_create_invoices_table.php @@ -1,5 +1,6 @@ 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'); } /**