Skip to content

Commit

Permalink
Employee additional fields
Browse files Browse the repository at this point in the history
  • Loading branch information
KirinZero0 committed Feb 24, 2025
1 parent 9964730 commit 374837b
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 13 deletions.
26 changes: 23 additions & 3 deletions app/Actions/HumanResources/Employee/StoreEmployee.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use App\Actions\SysAdmin\Organisation\Hydrators\OrganisationHydrateEmployees;
use App\Actions\SysAdmin\User\StoreUser;
use App\Actions\Traits\Rules\WithNoStrictRules;
use App\Actions\Traits\WithModelAddressActions;
use App\Actions\Traits\WithPreparePositionsForValidation;
use App\Actions\Traits\WithReorganisePositions;
use App\Enums\HumanResources\Employee\EmployeeStateEnum;
Expand All @@ -25,6 +26,7 @@
use App\Models\SysAdmin\Organisation;
use App\Rules\AlphaDashDot;
use App\Rules\IUnique;
use App\Rules\ValidAddress;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
Expand All @@ -39,19 +41,25 @@ class StoreEmployee extends OrgAction
use WithPreparePositionsForValidation;
use WithReorganisePositions;
use WithNoStrictRules;

use WithModelAddressActions;
/**
* @throws \Throwable
*/
public function handle(Organisation|Workplace $parent, array $modelData): Employee
{
$contactAddressData = null;
if (class_basename($parent) === 'Workplace') {
$organisation = $parent->organisation;
data_set($modelData, 'organisation_id', $organisation->id);
} else {
$organisation = $parent;
}

if(Arr::get($modelData, 'contact_address')) {
$contactAddressData = Arr::get($modelData, 'contact_address', []);
Arr::forget($modelData, 'contact_address');
}

data_set($modelData, 'group_id', $organisation->group_id);

$credentials = Arr::only($modelData, ['username', 'password', 'reset_password', 'user_model_status']);
Expand All @@ -62,11 +70,19 @@ public function handle(Organisation|Workplace $parent, array $modelData): Employ
data_forget($modelData, 'positions');
$positions = $this->reorganisePositionsSlugsToIds($positions);

$employee = DB::transaction(function () use ($parent, $modelData, $positions, $credentials) {
$employee = DB::transaction(function () use ($parent, $modelData, $positions, $credentials, $contactAddressData) {
/** @var Employee $employee */
$employee = $parent->employees()->create($modelData);
$employee->stats()->create();

if($contactAddressData){
$employee = $this->addAddressToModelFromArray(
model: $employee,
addressData: $contactAddressData,
);
$employee->refresh();
}

if (in_array($employee->state, [EmployeeStateEnum::LEAVING, EmployeeStateEnum::WORKING])) {
SetEmployeePin::make()->action($employee, updateQuietly: true);
}
Expand Down Expand Up @@ -192,7 +208,11 @@ public function rules(): array
'reset_password' => ['exclude_if:username,null', 'sometimes', 'boolean'],
'user_model_status' => ['exclude_if:username,null', 'sometimes', 'boolean'],
'emergency_contact' => ['sometimes', 'nullable', 'string', 'max:256'],
'type' => ['required', Rule::enum(EmployeeTypeEnum::class)]
'type' => ['required', Rule::enum(EmployeeTypeEnum::class)],
'contact_address' => ['sometimes', 'nullable', new ValidAddress()],
'notes' => ['sometimes', 'nullable', 'string', 'max:256'],
'identity_document_type' => ['sometimes', 'nullable', 'string', 'max:256'],
'identity_document_number' => ['sometimes', 'nullable', 'string', 'max:256'],
];

if (!$this->strict) {
Expand Down
59 changes: 49 additions & 10 deletions app/Actions/HumanResources/Employee/UI/CreateEmployee.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@

namespace App\Actions\HumanResources\Employee\UI;

use App\Actions\Helpers\Country\UI\GetAddressData;
use App\Actions\OrgAction;
use App\Enums\HumanResources\Employee\EmployeeStateEnum;
use App\Enums\HumanResources\Employee\EmployeeTypeEnum;
use App\Http\Resources\HumanResources\JobPositionResource;
use App\Http\Resources\Inventory\WarehouseResource;
use App\Enums\Catalogue\Shop\ShopTypeEnum;
use App\Http\Resources\Catalogue\ShopResource;
use App\Http\Resources\Helpers\AddressFormFieldsResource;
use App\Models\Helpers\Address;
use App\Models\SysAdmin\Organisation;
use Exception;
use Inertia\Inertia;
Expand All @@ -23,7 +26,8 @@

class CreateEmployee extends OrgAction
{
public function htmlResponse(ActionRequest $request): Response

public function handle(Organisation $organisation, ActionRequest $request): Response
{
return Inertia::render(
'CreateModel',
Expand Down Expand Up @@ -63,6 +67,47 @@ public function htmlResponse(ActionRequest $request): Response
'label' => __('personal email'),
'value' => ''
],
'phone' => [
'type' => 'input',
'label' => __('contact number'),
'value' => ''
],
'contact_address' => [
'type' => 'address',
'label' => __('Address'),
'value' => AddressFormFieldsResource::make(
new Address(
[
'country_id' => $organisation->country_id,

]
)
)->getArray(),
'options' => [
'countriesAddressData' => GetAddressData::run()

]
],
'emergency_contact' => [
'type' => 'textarea',
'label' => __('emergency contact'),
'value' => ''
],
'identity_document_type' => [
'type' => 'input',
'label' => __('identity document type'),
'value' => ''
],
'identity_document_number' => [
'type' => 'input',
'label' => __('identity document number'),
'value' => ''
],
'notes' => [
'type' => 'textarea',
'label' => __('notes'),
'value' => ''
],

]
],
Expand Down Expand Up @@ -104,12 +149,6 @@ public function htmlResponse(ActionRequest $request): Response
'required' => true,
'value' => ''
],
'emergency_contact' => [
'type' => 'input',
'label' => __('Emergency Contact'),
'required' => false,
'value' => ''
],
'alias' => [
'type' => 'input',
'label' => __('alias'),
Expand Down Expand Up @@ -196,7 +235,7 @@ public function htmlResponse(ActionRequest $request): Response
'route' => [
'name' => 'grp.models.org.employee.store',
'parameters' => [
'organisation' => $this->organisation->id
'organisation' => $organisation->id
]
]
],
Expand All @@ -213,11 +252,11 @@ public function authorize(ActionRequest $request): bool
/**
* @throws Exception
*/
public function asController(Organisation $organisation, ActionRequest $request): ActionRequest
public function asController(Organisation $organisation, ActionRequest $request): Response
{
$this->initialisation($organisation, $request);

return $request;
return $this->handle($organisation, $request);
}


Expand Down
4 changes: 4 additions & 0 deletions app/Models/HumanResources/Employee.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use App\Models\SysAdmin\Organisation;
use App\Models\SysAdmin\Task;
use App\Models\SysAdmin\User;
use App\Models\Traits\HasAddress;
use App\Models\Traits\HasAddresses;
use App\Models\Traits\HasAttachments;
use App\Models\Traits\HasHistory;
use App\Models\Traits\HasImage;
Expand Down Expand Up @@ -110,6 +112,8 @@
class Employee extends Model implements HasMedia, Auditable
{
use HasSlug;
use HasAddress;
use HasAddresses;
use SoftDeletes;
use HasUniversalSearch;
use HasImage;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('employees', function (Blueprint $table) {
$table->unsignedInteger('address_id')->index()->nullable();
$table->foreign('address_id')->references('id')->on('addresses');
$table->jsonb('location')->nullable();
$table->text('notes')->nullable();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('employees', function (Blueprint $table) {
$table->dropForeign(['address_id']);
$table->dropColumn('address_id');
$table->dropColumn('location');
$table->dropColumn('notes');
});
}
};

0 comments on commit 374837b

Please sign in to comment.