adrema/app/Actions/InsertMemberAction.php

75 lines
2.8 KiB
PHP
Raw Permalink Normal View History

2023-02-12 23:18:58 +01:00
<?php
namespace App\Actions;
use App\Confession;
use App\Country;
use App\Fee;
use App\Gender;
use App\Group;
use App\Member\Member;
use App\Nationality;
2023-03-06 09:49:03 +01:00
use App\Payment\Subscription;
2023-02-12 23:18:58 +01:00
use App\Region;
use Lorisleiva\Actions\Concerns\AsAction;
use Zoomyboy\LaravelNami\Data\Member as NamiMember;
class InsertMemberAction
{
use AsAction;
public function handle(NamiMember $member): Member
{
$region = Region::firstWhere('nami_id', $member->regionId ?: -1);
return Member::updateOrCreate(['nami_id' => $member->id], [
'firstname' => $member->firstname,
'lastname' => $member->lastname,
'joined_at' => $member->joinedAt,
'birthday' => $member->birthday,
'send_newspaper' => $member->sendNewspaper,
'address' => $member->address,
'zip' => $member->zip,
'location' => $member->location,
'nickname' => $member->nickname,
'other_country' => $member->otherCountry,
'further_address' => $member->furtherAddress,
'main_phone' => $member->mainPhone,
'mobile_phone' => $member->mobilePhone,
'work_phone' => $member->workPhone,
'fax' => $member->fax,
'email' => $member->email,
'email_parents' => $member->emailParents,
'nami_id' => $member->id,
'group_id' => Group::firstOrCreate(['nami_id' => $member->groupId], ['nami_id' => $member->groupId, 'name' => $member->groupName])->id,
'gender_id' => optional(Gender::firstWhere('nami_id', $member->genderId ?: -1))->id,
'confession_id' => optional(Confession::firstWhere('nami_id', $member->confessionId ?: -1))->id,
'region_id' => $region && !$region->is_null ? $region->id : null,
'country_id' => optional(Country::where('nami_id', $member->countryId)->first())->id,
2023-03-06 09:49:03 +01:00
'subscription_id' => $this->getSubscription($member)?->id,
2023-02-12 23:18:58 +01:00
'nationality_id' => Nationality::where('nami_id', $member->nationalityId)->firstOrFail()->id,
2023-03-02 23:14:25 +01:00
'mitgliedsnr' => $member->memberId,
2023-02-12 23:18:58 +01:00
'version' => $member->version,
2024-11-07 01:29:26 +01:00
'keepdata' => $member->keepdata,
2023-02-12 23:18:58 +01:00
]);
}
2023-03-06 09:49:03 +01:00
public function getSubscription(NamiMember $member): ?Subscription
2023-02-12 23:18:58 +01:00
{
2023-03-06 15:18:48 +01:00
if (is_null($member->feeId)) {
return null;
}
$fee = Fee::nami($member->feeId);
2023-03-06 09:49:03 +01:00
2023-02-12 23:18:58 +01:00
if (is_null($fee)) {
2023-03-06 10:47:23 +01:00
$feeName = $member->feeName ?: 'Default';
$fee = Fee::create(['name' => $feeName, 'nami_id' => $member->feeId]);
$subscription = $fee->subscriptions()->create(['name' => $feeName]);
$subscription->children()->create(['name' => $feeName, 'amount' => 1000]);
2023-02-12 23:18:58 +01:00
}
2023-03-06 09:49:03 +01:00
return $fee->subscriptions()->first();
2023-02-12 23:18:58 +01:00
}
}