fixed: create subscription on the fly
This commit is contained in:
parent
609d452e56
commit
bcefd9f356
|
@ -9,6 +9,7 @@ use App\Gender;
|
|||
use App\Group;
|
||||
use App\Member\Member;
|
||||
use App\Nationality;
|
||||
use App\Payment\Subscription;
|
||||
use App\Region;
|
||||
use Lorisleiva\Actions\Concerns\AsAction;
|
||||
use Zoomyboy\LaravelNami\Data\Member as NamiMember;
|
||||
|
@ -45,20 +46,23 @@ class InsertMemberAction
|
|||
'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,
|
||||
'subscription_id' => $this->getSubscriptionId($member),
|
||||
'subscription_id' => $this->getSubscription($member)?->id,
|
||||
'nationality_id' => Nationality::where('nami_id', $member->nationalityId)->firstOrFail()->id,
|
||||
'mitgliedsnr' => $member->memberId,
|
||||
'version' => $member->version,
|
||||
]);
|
||||
}
|
||||
|
||||
public function getSubscriptionId(NamiMember $member): ?int
|
||||
public function getSubscription(NamiMember $member): ?Subscription
|
||||
{
|
||||
$fee = Fee::firstWhere('nami_id', $member->feeId ?: -1);
|
||||
$fee = Fee::nami($member->feeId ?: -1);
|
||||
|
||||
if (is_null($fee)) {
|
||||
return null;
|
||||
$fee = Fee::create(['name' => $member->feeName, 'nami_id' => $member->feeId]);
|
||||
$subscription = $fee->subscriptions()->create(['name' => $member->feeName]);
|
||||
$subscription->children()->create(['name' => $member->feeName, 'amount' => 1000]);
|
||||
}
|
||||
|
||||
return optional($fee->subscriptions()->first())->id;
|
||||
return $fee->subscriptions()->first();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace App;
|
||||
|
||||
use App\Nami\HasNamiField;
|
||||
use App\Payment\Subscription;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
@ -10,6 +11,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
|
|||
class Fee extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
use HasNamiField;
|
||||
|
||||
/** @var array<int, string> */
|
||||
public $fillable = ['name', 'nami_id'];
|
||||
|
|
|
@ -1 +1 @@
|
|||
Subproject commit 7b08f30cb2d44a74f59c1441f797ab544e753701
|
||||
Subproject commit 5b4ec86fe9caef98c965c315f4b7694402a07271
|
|
@ -93,4 +93,46 @@ class PullMemberActionTest extends TestCase
|
|||
'region_id' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
public function testItSetsFirstSubscriptionFromFee(): void
|
||||
{
|
||||
Region::factory()->inNami(999)->name('nicht-de')->create(['is_null' => true]);
|
||||
Subscription::factory()->for(Fee::factory()->inNami(54))->create();
|
||||
$should = Subscription::factory()->for(Fee::factory()->inNami(55))->create();
|
||||
Subscription::factory()->for(Fee::factory()->inNami(56))->create();
|
||||
app(MemberFake::class)->shows(1000, 1001, [
|
||||
'beitragsartId' => 55,
|
||||
]);
|
||||
|
||||
app(PullMemberAction::class)->handle(1000, 1001);
|
||||
|
||||
$this->assertDatabaseHas('members', [
|
||||
'subscription_id' => $should->id,
|
||||
]);
|
||||
}
|
||||
|
||||
public function testItCreatesSubscriptionOnTheFly(): void
|
||||
{
|
||||
Region::factory()->inNami(999)->name('nicht-de')->create(['is_null' => true]);
|
||||
app(MemberFake::class)->shows(1000, 1001, [
|
||||
'beitragsartId' => 55,
|
||||
'beitragsart' => 'Lala',
|
||||
]);
|
||||
|
||||
app(PullMemberAction::class)->handle(1000, 1001);
|
||||
|
||||
$fee = Fee::where('nami_id', 55)->firstOrFail();
|
||||
$subscription = Subscription::where('fee_id', $fee->id)->firstOrFail();
|
||||
$this->assertDatabaseHas('subscriptions', [
|
||||
'fee_id' => $fee->id,
|
||||
'name' => 'Lala',
|
||||
'split' => false,
|
||||
'for_promise' => false,
|
||||
]);
|
||||
$this->assertDatabaseHas('subscription_children', [
|
||||
'name' => 'Lala',
|
||||
'amount' => 1000,
|
||||
'parent_id' => $subscription->id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue