Add subscriptions

This commit is contained in:
philipp lang 2021-07-04 12:09:30 +02:00
parent 0202d29c32
commit 9c580b7eb5
10 changed files with 73 additions and 14 deletions

View File

@ -4,6 +4,8 @@ namespace App;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Member\Member;
use App\Payment\Subscription;
class Fee extends Model class Fee extends Model
{ {
@ -11,4 +13,9 @@ class Fee extends Model
public $fillable = ['name', 'nami_id']; public $fillable = ['name', 'nami_id'];
public $timestamps = false; public $timestamps = false;
public function subscriptions() {
return $this->hasMany(Subscription::class);
}
} }

View File

@ -15,7 +15,11 @@ class InitializeFees {
public function handle() { public function handle() {
$this->bar->task('Synchronisiere Beiträge', function() { $this->bar->task('Synchronisiere Beiträge', function() {
$this->api->group(auth()->user()->getNamiGroupId())->fees()->each(function($fee) { $this->api->group(auth()->user()->getNamiGroupId())->fees()->each(function($fee) {
\App\Fee::create(['nami_id' => $fee->id, 'name' => $fee->name]); \App\Fee::create(['nami_id' => $fee->id, 'name' => $fee->name])
->subscriptions()->create([
'name' => $fee->name,
'amount' => 1000,
]);
}); });
}); });
} }

View File

@ -22,6 +22,15 @@ class InitializeMembers {
$this->api = $api; $this->api = $api;
} }
public function getSubscriptionId($member) {
$fee = Fee::firstWhere('nami_id', $member->fee_id ?: -1);
if (is_null($fee)) {
return null;
}
return optional($fee->subscriptions()->first())->id;
}
public function handle() { public function handle() {
$allMembers = collect([]); $allMembers = collect([]);
@ -52,7 +61,7 @@ class InitializeMembers {
'confession_id' => optional(Confession::firstWhere('nami_id', $member->confession_id ?: -1))->id, 'confession_id' => optional(Confession::firstWhere('nami_id', $member->confession_id ?: -1))->id,
'region_id' => optional(Region::firstWhere('nami_id', $member->region_id ?: -1))->id, 'region_id' => optional(Region::firstWhere('nami_id', $member->region_id ?: -1))->id,
'country_id' => Country::where('nami_id', $member->country_id)->firstOrFail()->id, 'country_id' => Country::where('nami_id', $member->country_id)->firstOrFail()->id,
'fee_id' => optional(Fee::firstWhere('nami_id', $member->fee_id ?: -1))->id, 'subscription_id' => $this->getSubscriptionId($member),
'nationality_id' => Nationality::where('nami_id', $member->nationality_id)->firstOrFail()->id, 'nationality_id' => Nationality::where('nami_id', $member->nationality_id)->firstOrFail()->id,
'version' => $member->version, 'version' => $member->version,
]); ]);

View File

@ -62,7 +62,7 @@ class CreateJob implements ShouldQueue
'confession_id' => $this->member->confession ? $this->member->confession->nami_id : Confession::firstWhere('is_null', true)->id, 'confession_id' => $this->member->confession ? $this->member->confession->nami_id : Confession::firstWhere('is_null', true)->id,
'region_id' => optional($this->member->region)->nami_id, 'region_id' => optional($this->member->region)->nami_id,
'country_id' => $this->member->country->nami_id, 'country_id' => $this->member->country->nami_id,
'fee_id' => optional($this->member->fee)->nami_id, 'fee_id' => $this->member->getNamiFeeId(),
'nationality_id' => $this->member->nationality->nami_id, 'nationality_id' => $this->member->nationality->nami_id,
'group_id' => $this->member->group->nami_id, 'group_id' => $this->member->group->nami_id,
'first_activity_id' => $this->member->firstActivity->nami_id, 'first_activity_id' => $this->member->firstActivity->nami_id,

View File

@ -8,18 +8,18 @@ use Illuminate\Notifications\Notifiable;
use App\Bill\BillKind; use App\Bill\BillKind;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Nationality; use App\Nationality;
use App\Fee;
use App\Group; use App\Group;
use App\Activity; use App\Activity;
use App\Subactivity; use App\Subactivity;
use Zoomyboy\LaravelNami\NamiUser; use Zoomyboy\LaravelNami\NamiUser;
use App\Payment\Subscription;
class Member extends Model class Member extends Model
{ {
use Notifiable; use Notifiable;
use HasFactory; use HasFactory;
public $fillable = ['firstname', 'lastname', 'nickname', 'other_country', 'birthday', 'joined_at', 'send_newspaper', 'address', 'further_address', 'zip', 'location', 'main_phone', 'mobile_phone', 'work_phone', 'fax', 'email', 'email_parents', 'nami_id', 'group_id', 'letter_address', 'country_id', 'way_id', 'nationality_id', 'fee_id', 'region_id', 'gender_id', 'confession_id', 'letter_address', 'bill_kind_id', 'version', 'first_subactivity_id', 'first_activity_id', 'confirmed_at', 'children_phone']; public $fillable = ['firstname', 'lastname', 'nickname', 'other_country', 'birthday', 'joined_at', 'send_newspaper', 'address', 'further_address', 'zip', 'location', 'main_phone', 'mobile_phone', 'work_phone', 'fax', 'email', 'email_parents', 'nami_id', 'group_id', 'letter_address', 'country_id', 'way_id', 'nationality_id', 'subscription_id', 'region_id', 'gender_id', 'confession_id', 'letter_address', 'bill_kind_id', 'version', 'first_subactivity_id', 'first_activity_id', 'confirmed_at', 'children_phone'];
public $dates = ['joined_at', 'birthday']; public $dates = ['joined_at', 'birthday'];
@ -56,6 +56,14 @@ class Member extends Model
return $api->group($this->group->nami_id)->member($this->nami_id)->memberships()->toArray(); return $api->group($this->group->nami_id)->member($this->nami_id)->memberships()->toArray();
} }
public function getNamiFeeId() {
if (!$this->subscription) {
return null;
}
return $this->subscription->fee->nami_id;
}
//---------------------------------- Relations ---------------------------------- //---------------------------------- Relations ----------------------------------
public function country() public function country()
{ {
@ -97,9 +105,9 @@ class Member extends Model
return $this->hasMany(Membership::class); return $this->hasMany(Membership::class);
} }
public function fee() public function subscription()
{ {
return $this->belongsTo(Fee::class); return $this->belongsTo(Subscription::class);
} }
public function billKind() { public function billKind() {

View File

@ -5,7 +5,6 @@ namespace App\Member;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Gender; use App\Gender;
use App\Fee;
use App\Region; use App\Region;
use App\Country; use App\Country;
use App\Nationality; use App\Nationality;
@ -13,6 +12,7 @@ use App\Confession;
use App\Bill\BillKind; use App\Bill\BillKind;
use App\Activity; use App\Activity;
use App\Group; use App\Group;
use App\Payment\Subscription;
class MemberController extends Controller class MemberController extends Controller
{ {
@ -44,7 +44,7 @@ class MemberController extends Controller
'regions' => Region::where('is_null', false)->get()->pluck('name', 'id'), 'regions' => Region::where('is_null', false)->get()->pluck('name', 'id'),
'nationalities' => Nationality::get()->pluck('name', 'id'), 'nationalities' => Nationality::get()->pluck('name', 'id'),
'confessions' => Confession::where('is_null', false)->get()->pluck('name', 'id'), 'confessions' => Confession::where('is_null', false)->get()->pluck('name', 'id'),
'fees' => Fee::get()->pluck('name', 'id'), 'subscriptions' => Subscription::get()->pluck('name', 'id'),
'data' => [ 'data' => [
'country_id' => Country::default() 'country_id' => Country::default()
], ],
@ -69,7 +69,7 @@ class MemberController extends Controller
'regions' => Region::where('is_null', false)->get()->pluck('name', 'id'), 'regions' => Region::where('is_null', false)->get()->pluck('name', 'id'),
'nationalities' => Nationality::get()->pluck('name', 'id'), 'nationalities' => Nationality::get()->pluck('name', 'id'),
'confessions' => Confession::where('is_null', false)->get()->pluck('name', 'id'), 'confessions' => Confession::where('is_null', false)->get()->pluck('name', 'id'),
'fees' => Fee::get()->pluck('name', 'id'), 'subscriptions' => Subscription::get()->pluck('name', 'id'),
'data' => new MemberResource($member), 'data' => new MemberResource($member),
'mode' => 'edit', 'mode' => 'edit',
]); ]);

View File

@ -26,7 +26,7 @@ class MemberResource extends JsonResource
'joined_at' => $this->joined_at->format('Y-m-d'), 'joined_at' => $this->joined_at->format('Y-m-d'),
'joined_at_human' => $this->joined_at->format('d.m.Y'), 'joined_at_human' => $this->joined_at->format('d.m.Y'),
'id' => $this->id, 'id' => $this->id,
'fee_id' => $this->fee_id, 'subscription_id' => $this->subscription_id,
'gender_id' => $this->gender_id, 'gender_id' => $this->gender_id,
'further_address' => $this->further_address, 'further_address' => $this->further_address,
'work_phone' => $this->work_phone, 'work_phone' => $this->work_phone,

View File

@ -60,7 +60,7 @@ class UpdateJob implements ShouldQueue
'confession_id' => $this->member->confession ? $this->member->confession->nami_id : Confession::firstWhere('is_null', true)->id, 'confession_id' => $this->member->confession ? $this->member->confession->nami_id : Confession::firstWhere('is_null', true)->id,
'region_id' => optional($this->member->region)->nami_id, 'region_id' => optional($this->member->region)->nami_id,
'country_id' => $this->member->country->nami_id, 'country_id' => $this->member->country->nami_id,
'fee_id' => optional($this->member->fee)->nami_id, 'fee_id' => $this->member->getNamiFeeId(),
'nationality_id' => $this->member->nationality->nami_id, 'nationality_id' => $this->member->nationality->nami_id,
'id' => $this->member->nami_id, 'id' => $this->member->nami_id,
'group_id' => $this->member->group->nami_id, 'group_id' => $this->member->group->nami_id,

View File

@ -3,6 +3,9 @@
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Schema;
use App\Payment\Subscription;
use App\Fee;
use App\Member\Member;
class CreateSubscriptionsRelationColumn extends Migration class CreateSubscriptionsRelationColumn extends Migration
{ {
@ -18,6 +21,34 @@ class CreateSubscriptionsRelationColumn extends Migration
$table->unsignedInteger('amount')->after('name'); $table->unsignedInteger('amount')->after('name');
$table->foreignId('fee_id')->after('name')->constrained(); $table->foreignId('fee_id')->after('name')->constrained();
}); });
foreach(Fee::get() as $fee) {
Subscription::create([
'amount' => 1000,
'fee_id' => $fee->id,
'name' => $fee->name,
]);
}
Schema::table('members', function (Blueprint $table) {
$table->foreignId('subscription_id')->after('version')->nullable()->default(1)->constrained();
});
Member::withoutEvents(function() {
foreach (Member::get() as $member) {
if (is_null($member->fee_id)) {
$member->update(['subscription_id' => null]);
continue;
}
$member->update(['subscription_id' => Subscription::firstWhere('fee_id', $member->fee_id)->id]);
}
});
Schema::table('members', function (Blueprint $table) {
$table->dropForeign(['fee_id']);
$table->dropColumn('fee_id');
});
} }
/** /**

View File

@ -50,7 +50,7 @@
<f-switch id="send_newspaper" v-model="inner.send_newspaper" label="Mittendrin"></f-switch> <f-switch id="send_newspaper" v-model="inner.send_newspaper" label="Mittendrin"></f-switch>
<f-text class="col-span-2" type="date" id="joined_at" v-model="inner.joined_at" label="Eintrittsdatum"></f-text> <f-text class="col-span-2" type="date" id="joined_at" v-model="inner.joined_at" label="Eintrittsdatum"></f-text>
<f-select class="col-span-2" :options="confessions" id="confession_id" v-model="inner.confession_id" label="Konfession"></f-select> <f-select class="col-span-2" :options="confessions" id="confession_id" v-model="inner.confession_id" label="Konfession"></f-select>
<f-select class="col-span-2" :options="fees" id="fee_id" v-model="inner.fee_id" label="Beitrag"></f-select> <f-select class="col-span-2" :options="subscriptions" id="subscription_id" v-model="inner.subscription_id" label="Beitrag"></f-select>
<f-textarea class="col-span-2" rows="4" id="letter_address" v-model="inner.letter_address" label="Brief-Adresse"></f-textarea> <f-textarea class="col-span-2" rows="4" id="letter_address" v-model="inner.letter_address" label="Brief-Adresse"></f-textarea>
</div> </div>
</div> </div>
@ -77,7 +77,7 @@ export default {
activities: {}, activities: {},
mode: {}, mode: {},
genders: {}, genders: {},
fees: {}, subscriptions: {},
data: {}, data: {},
regions: {}, regions: {},
countries: {}, countries: {},