diff --git a/app/Activity.php b/app/Activity.php index ac3b29fa..49919d7f 100644 --- a/app/Activity.php +++ b/app/Activity.php @@ -15,7 +15,7 @@ class Activity extends Model 'nami_id' => 'integer' ]; - public function groups() { - return $this->belongsToMany(Group::class); + public function subactivities() { + return $this->belongsToMany(Subactivity::class); } } diff --git a/app/Group.php b/app/Group.php index 990ab62a..3b1ccd99 100644 --- a/app/Group.php +++ b/app/Group.php @@ -2,18 +2,13 @@ namespace App; -use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Factories\HasFactory; +use Illuminate\Database\Eloquent\Model; class Group extends Model { use HasFactory; - public $fillable = ['name', 'nami_id']; - + public $fillable = ['nami_id', 'name']; public $timestamps = false; - - public function activities() { - return $this->belongsToMany(Activity::class); - } } diff --git a/app/Initialize/InitializeActivities.php b/app/Initialize/InitializeActivities.php index d9151fab..9d42ada8 100644 --- a/app/Initialize/InitializeActivities.php +++ b/app/Initialize/InitializeActivities.php @@ -19,7 +19,7 @@ class InitializeActivities { $groups = []; $this->api->subactivitiesOf($activity->id)->each(function($group) use ($activity, &$groups) { - $group = \App\Group::updateOrCreate(['nami_id' => $group->id], ['nami_id' => $group->id, 'name' => $group->name]); + $group = \App\Subactivity::updateOrCreate(['nami_id' => $group->id], ['nami_id' => $group->id, 'name' => $group->name]); $groups[] = $group->id; }); $activity->groups()->sync($groups); diff --git a/app/Initialize/InitializeMembers.php b/app/Initialize/InitializeMembers.php index 00f6d65c..4afd44d1 100644 --- a/app/Initialize/InitializeMembers.php +++ b/app/Initialize/InitializeMembers.php @@ -10,6 +10,7 @@ use App\Region; use App\Nationality; use App\Fee; use App\Group; +use App\Activity; class InitializeMembers { diff --git a/app/Member/CreateJob.php b/app/Member/CreateJob.php new file mode 100644 index 00000000..b55c4f1d --- /dev/null +++ b/app/Member/CreateJob.php @@ -0,0 +1,73 @@ +memberId = $member->id; + $this->user = $user; + } + + /** + * Execute the job. + * + * @return void + */ + public function handle() + { + $this->member = Member::find($this->memberId); + + if ($this->member->hasNami) { + return false; + } + + $response = Nami::login($this->user->mglnr)->putMember([ + 'firstname' => $this->member->firstname, + 'lastname' => $this->member->lastname, + 'nickname' => $this->member->nickname, + 'joined_at' => $this->member->joined_at, + 'birthday' => $this->member->birthday, + 'send_newspaper' => $this->member->send_newspaper, + 'address' => $this->member->address, + 'zip' => $this->member->zip, + 'location' => $this->member->location, + 'nickname' => $this->member->nickname, + 'other_country' => $this->member->other_country, + 'further_address' => $this->member->further_address, + 'main_phone' => $this->member->main_phone, + 'mobile_phone' => $this->member->mobile_phone, + 'work_phone' => $this->member->work_phone, + 'fax' => $this->member->fax, + 'email' => $this->member->email, + 'email_parents' => $this->member->email_parents, + 'gender_id' => optional($this->member->gender)->nami_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, + 'country_id' => $this->member->country->nami_id, + 'fee_id' => optional($this->member->fee)->nami_id, + 'nationality_id' => $this->member->nationality->nami_id, + 'group_id' => $this->member->group->nami_id, + 'first_activity_id' => $this->member->firstActivity->nami_id, + 'first_subactivity_id' => $this->member->firstSubactivity->nami_id, + ]); + Member::withoutEvents(function() use ($response) { + $this->member->update(['version' => $response['version']]); + }); + } +} diff --git a/app/Member/Member.php b/app/Member/Member.php index 04d2ccf0..b210ed9a 100644 --- a/app/Member/Member.php +++ b/app/Member/Member.php @@ -10,13 +10,15 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use App\Nationality; use App\Fee; use App\Group; +use App\Activity; +use App\Subactivity; class Member extends Model { use Notifiable; 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']; + 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']; public $dates = ['joined_at', 'birthday']; @@ -102,6 +104,14 @@ class Member extends Model return $this->belongsTo(Group::class); } + public function firstActivity() { + return $this->belongsTo(Activity::class, 'first_activity_id'); + } + + public function firstSubActivity() { + return $this->belongsTo(Subactivity::class, 'first_subactivity_id'); + } + public static function booted() { static::updating(function($model) { if ($model->nami_id === null) { @@ -110,7 +120,12 @@ class Member extends Model }); static::updated(function($model) { - UpdateJob::dispatch($model, auth()->user()); + if ($model->nami_id !== null && $model->getOriginal()['nami_id'] !== null) { + UpdateJob::dispatch($model, auth()->user()); + } + if ($model->nami_id !== null && $model->getOriginal()['nami_id'] === null) { + CreateJob::dispatch($model, auth()->user()); + } }); } } diff --git a/app/Member/MemberController.php b/app/Member/MemberController.php index 2aed1807..35ca0b04 100644 --- a/app/Member/MemberController.php +++ b/app/Member/MemberController.php @@ -11,6 +11,8 @@ use App\Country; use App\Nationality; use App\Confession; use App\Bill\BillKind; +use App\Activity; +use App\Group; class MemberController extends Controller { @@ -29,7 +31,13 @@ class MemberController extends Controller session()->put('menu', 'member'); session()->put('title', 'Mitglied erstellen'); + $activities = Activity::with('subactivities')->get(); + return \Inertia::render('member/Form', [ + 'activities' => $activities->pluck('name', 'id'), + 'subactivities' => $activities->map(function($activity) { + return ['subactivities' => $activity->subactivities->pluck('name', 'id'), 'id' => $activity->id]; + })->pluck('subactivities', 'id'), 'billKinds' => BillKind::get()->pluck('name', 'id'), 'genders' => Gender::get()->pluck('name', 'id'), 'countries' => Country::get()->pluck('name', 'id'), diff --git a/app/Member/MemberRequest.php b/app/Member/MemberRequest.php index 33df5212..37cb6249 100644 --- a/app/Member/MemberRequest.php +++ b/app/Member/MemberRequest.php @@ -3,6 +3,8 @@ namespace App\Member; use Illuminate\Foundation\Http\FormRequest; +use Illuminate\Validation\Rule; +use App\Group; class MemberRequest extends FormRequest { @@ -24,6 +26,8 @@ class MemberRequest extends FormRequest public function rules() { return [ + 'first_activity_id' => Rule::requiredIf(fn() => $this->method() == 'POST'), + 'first_subactivity_id' => Rule::requiredIf(fn() => $this->method() == 'POST'), 'firstname' => 'required', 'lastname' => 'required', 'address' => 'required', @@ -42,7 +46,8 @@ class MemberRequest extends FormRequest } public function persistCreate() { - Member::create($this->input()); + $this->merge(['group_id' => Group::where('nami_id', auth()->user()->getNamiGroupId())->firstOrFail()->id]); + $m = Member::create($this->input()); } public function persistUpdate(Member $member) { diff --git a/app/Subactivity.php b/app/Subactivity.php new file mode 100644 index 00000000..3c049bf8 --- /dev/null +++ b/app/Subactivity.php @@ -0,0 +1,19 @@ +belongsToMany(Activity::class); + } +} diff --git a/database/migrations/2020_04_11_215831_create_activity_group_table.php b/database/migrations/2020_04_11_215742_create_subactivities_table.php similarity index 54% rename from database/migrations/2020_04_11_215831_create_activity_group_table.php rename to database/migrations/2020_04_11_215742_create_subactivities_table.php index f1aaf20b..b800cab3 100644 --- a/database/migrations/2020_04_11_215831_create_activity_group_table.php +++ b/database/migrations/2020_04_11_215742_create_subactivities_table.php @@ -4,7 +4,7 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -class CreateActivityGroupTable extends Migration +class CreateSubactivitiesTable extends Migration { /** * Run the migrations. @@ -13,10 +13,10 @@ class CreateActivityGroupTable extends Migration */ public function up() { - Schema::create('activity_group', function (Blueprint $table) { - $table->integer('activity_id'); - $table->integer('group_id'); - $table->unique(['activity_id', 'group_id']); + Schema::create('subactivities', function (Blueprint $table) { + $table->id(); + $table->string('name'); + $table->string('nami_id')->nullable(); }); } @@ -27,6 +27,6 @@ class CreateActivityGroupTable extends Migration */ public function down() { - Schema::dropIfExists('activity_group'); + Schema::dropIfExists('groups'); } } diff --git a/database/migrations/2020_04_11_215831_create_activity_subactivity_table.php b/database/migrations/2020_04_11_215831_create_activity_subactivity_table.php new file mode 100644 index 00000000..99e14cd3 --- /dev/null +++ b/database/migrations/2020_04_11_215831_create_activity_subactivity_table.php @@ -0,0 +1,32 @@ +foreignId('activity_id')->constrained(); + $table->foreignId('subactivity_id')->constrained(); + $table->unique(['activity_id', 'subactivity_id']); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('activity_subactivity'); + } +} diff --git a/database/migrations/2020_04_11_215742_create_groups_table.php b/database/migrations/2020_04_12_010000_create_groups_table.php similarity index 91% rename from database/migrations/2020_04_11_215742_create_groups_table.php rename to database/migrations/2020_04_12_010000_create_groups_table.php index 7923d9c9..9b486fcc 100644 --- a/database/migrations/2020_04_11_215742_create_groups_table.php +++ b/database/migrations/2020_04_12_010000_create_groups_table.php @@ -16,7 +16,7 @@ class CreateGroupsTable extends Migration Schema::create('groups', function (Blueprint $table) { $table->id(); $table->string('name'); - $table->string('nami_id')->nullable(); + $table->unsignedInteger('nami_id'); }); } diff --git a/database/migrations/2020_04_12_223230_create_members_table.php b/database/migrations/2020_04_12_223230_create_members_table.php index dfdc1eed..da1a9224 100644 --- a/database/migrations/2020_04_12_223230_create_members_table.php +++ b/database/migrations/2020_04_12_223230_create_members_table.php @@ -48,9 +48,11 @@ class CreateMembersTable extends Migration $table->string('email_parents')->nullable(); $table->integer('nami_id')->nullable(); $table->foreignId('nationality_id')->constrained(); - $table->foreignId('fee_id')->constrained(); + $table->foreignId('fee_id')->nullable()->constrained(); $table->text('letter_address')->nullable(); $table->foreignId('bill_kind_id')->nullable()->constrained(); + $table->foreignId('first_activity_id')->nullable()->constrained('activities'); + $table->foreignId('first_subactivity_id')->nullable()->constrained('subactivities'); $table->unsignedInteger('version')->default(1); $table->timestamps(); diff --git a/resources/js/views/member/Form.vue b/resources/js/views/member/Form.vue index 3cb2f0c6..38686c9b 100644 --- a/resources/js/views/member/Form.vue +++ b/resources/js/views/member/Form.vue @@ -24,6 +24,12 @@ + +
+

Erste Gruppierung

+ + +
@@ -64,6 +70,8 @@ export default { }, props: { + subactivities: {}, + activities: {}, mode: {}, genders: {}, fees: {}, diff --git a/resources/lang/de/validation.php b/resources/lang/de/validation.php index c9c73a8b..bdbaa7c3 100644 --- a/resources/lang/de/validation.php +++ b/resources/lang/de/validation.php @@ -185,5 +185,7 @@ return [ 'zip' => 'PLZ', 'location' => 'Ort', 'nationality_id' => 'Staatsangehörigkeit', + 'first_group_id' => 'Erste Untertätigkeit', + 'first_activity_id' => 'Erste Tätigkeit', ], ];