This commit is contained in:
Simon Pocrnjič
2025-01-02 18:38:47 +01:00
parent 0c3bbfe18f
commit 0f8cfd3f16
41 changed files with 2013 additions and 591 deletions
+42
View File
@@ -0,0 +1,42 @@
<?php
namespace App\Events;
use App\Models\ClientCase;
use App\Models\Segment;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class ClientCaseToTerrain implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public ClientCase $clientCase;
/**
* Create a new event instance.
*/
public function __construct(ClientCase $clientCase)
{
$this->clientCase = $clientCase;
}
/**
* Get the channels the event should broadcast on.
*
* @return array<int, \Illuminate\Broadcasting\Channel>
*/
public function broadcastOn(): PrivateChannel
{
return new PrivateChannel('segments'.$this->clientCase->id);
}
public function broadcastAs(){
return 'client_case.terrain.add';
}
}
+43
View File
@@ -0,0 +1,43 @@
<?php
namespace App\Events;
use App\Models\Contract;
use App\Models\Segment;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class ContractToTerrain implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public Contract $contract;
public Segment $segment;
/**
* Create a new event instance.
*/
public function __construct(Contract $contract, Segment $segment)
{
//
$this->contract = $contract;
$this->segment = $segment;
}
/**
* Get the channels the event should broadcast on.
*
* @return array<int, \Illuminate\Broadcasting\Channel>
*/
public function broadcastOn(): PrivateChannel
{
return new PrivateChannel('contracts.'.$this->segment->id);
}
public function broadcastAs(){
return 'contract.terrain.add';
}
}
+15 -2
View File
@@ -131,7 +131,14 @@ public function storeActivity(ClientCase $clientCase, Request $request) {
]);
//Create activity
$clientCase->activities()->create($attributes);
$activity = $clientCase->activities()->create($attributes);
foreach ($activity->decision->events as $e) {
$class = '\\App\\Events\\' . $e->name;
event(new $class($clientCase));
}
return to_route('clientCase.show', $clientCase);
@@ -156,6 +163,11 @@ public function show(ClientCase $clientCase)
'person' => fn($que) => $que->with(['addresses', 'phones'])
])->where('active', 1)->findOrFail($clientCase->id);
$types = [
'address_types' => \App\Models\Person\AddressType::all(),
'phone_types' => \App\Models\Person\PhoneType::all()
];
return Inertia::render('Cases/Show', [
'client' => $case->client()->with('person', fn($q) => $q->with(['addresses', 'phones']))->firstOrFail(),
'client_case' => $case,
@@ -166,7 +178,8 @@ public function show(ClientCase $clientCase)
->orderByDesc('created_at')
->paginate(20, ['*'], 'activities'),
'contract_types' => \App\Models\ContractType::whereNull('deleted_at')->get(),
'actions' => \App\Models\Action::with('decisions')->get()
'actions' => \App\Models\Action::with('decisions')->get(),
'types' => $types
]);
}
+11
View File
@@ -33,6 +33,11 @@ public function show(Client $client, Request $request) {
->with(['person' => fn($que) => $que->with(['addresses','phones'])])
->findOrFail($client->id);
$types = [
'address_types' => \App\Models\Person\AddressType::all(),
'phone_types' => \App\Models\Person\PhoneType::all()
];
return Inertia::render('Client/Show', [
'client' => $data,
'client_cases' => $data->clientCases()
@@ -47,6 +52,7 @@ public function show(Client $client, Request $request) {
->orderByDesc('created_at')
->paginate(15)
->withQueryString(),
'types' => $types,
'filters' => $request->only(['search'])
]);
}
@@ -91,4 +97,9 @@ public function store(Request $request)
return to_route('client');
}
public function update(Client $client, Request $request) {
return to_route('client.show', $client);
}
}
+88
View File
@@ -4,6 +4,7 @@
use App\Models\Person\Person;
use Illuminate\Http\Request;
use Inertia\Inertia;
class PersonController extends Controller
{
@@ -19,4 +20,91 @@ public function create(Request $request){
public function store(Request $request){
}
public function update(Person $person, Request $request){
$attributes = $request->validate([
'full_name' => 'string|max:255',
'tax_number' => 'nullable|integer',
'social_security_number' => 'nullable|integer',
'description' => 'nullable|string|max:500'
]);
$person->update($attributes);
return response()->json([
'person' => [
'full_name' => $person->full_name,
'tax_number' => $person->tax_number,
'social_security_number' => $person->social_security_number,
'description' => $person->description
]
]);
}
public function createAddress(Person $person, Request $request){
$attributes = $request->validate([
'address' => 'required|string|max:150',
'country' => 'nullable|string',
'type_id' => 'required|integer|exists:address_types,id',
'description' => 'nullable|string|max:125'
]);
$address_id = $person->addresses()->create($attributes)->id;
return response()->json([
'address' => \App\Models\Person\PersonAddress::with(['type'])->findOrFail($address_id)
]);
}
public function updateAddress(Person $person, int $address_id, Request $request)
{
$attributes = $request->validate([
'address' => 'required|string|max:150',
'country' => 'nullable|string',
'type_id' => 'required|integer|exists:address_types,id',
'description' => 'nullable|string|max:125'
]);
$address = $person->addresses()->with(['type'])->findOrFail($address_id);
$address->update($attributes);
return response()->json([
'address' => $address
]);
}
public function createPhone(Person $person, Request $request)
{
$attributes = $request->validate([
'nu' => 'required|string|max:50',
'country_code' => 'nullable|integer',
'type_id' => 'required|integer|exists:phone_types,id',
'description' => 'nullable|string|max:125'
]);
$phone_id = $person->phones()->create($attributes)->id;
return response()->json([
'phone' => \App\Models\Person\PersonPhone::with(['type'])->findOrFail($phone_id)
]);
}
public function updatePhone(Person $person, int $phone_id, Request $request)
{
$attributes = $request->validate([
'nu' => 'required|string|max:50',
'country_code' => 'nullable|integer',
'type_id' => 'required|integer|exists:phone_types,id',
'description' => 'nullable|string|max:125'
]);
$phone = $person->phones()->with(['type'])->findOrFail($phone_id);
$phone->update($attributes);
return response()->json([
'phone' => $phone
]);
}
}
+2 -2
View File
@@ -13,10 +13,10 @@ public function index(Request $request){
return Inertia::render('Settings/Index', [
'actions' => \App\Models\Action::query()
->with('decisions', fn($q) => $q->get(['decisions.id']))
->with('decisions')
->get(),
'decisions' => \App\Models\Decision::query()
->with('actions', fn($q) => $q->get(['actions.id']))
->with('actions')
->get()
]
);
+41
View File
@@ -0,0 +1,41 @@
<?php
namespace App\Listeners;
use App\Events\ClientCaseToTerrain;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
class AddClientCaseToTerrain
{
/**
* Create the event listener.
*/
public function __construct()
{
//
}
/**
* Handle the event.
*/
public function handle(ClientCaseToTerrain $event): void
{
$clientCase = $event->clientCase;
$segment = \App\Models\Segment::where('name','terrain')->firstOrFail();
if( $segment ) {
$clientCase->segments()->detach($segment->id);
$clientCase->segments()->attach(
$segment->id,
);
\Log::info("Added contract to terrain", ['contract_id' => $clientCase->id, 'segment' => $segment->name ]);
}
}
public function failed(ClientCaseToTerrain $event, $exception)
{
\Log::error('Failed to update inventory', ['contract_id' => $event->clientCase->id, 'error' => $exception->getMessage()]);
}
}
+37
View File
@@ -0,0 +1,37 @@
<?php
namespace App\Listeners;
use App\Events\ContractToTerrain;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
class AddContractToTerrain implements ShouldQueue
{
/**
* Create the event listener.
*/
public function __construct()
{
//
}
/**
* Handle the event.
*/
public function handle(ContractToTerrain $event): void
{
$contract = $event->contract;
$segment = $event->segment->where('name', 'terrain')->firstOrFail();
if($segment) {
$contract->segments()->attach($segment->id);
//\Log::info("Added contract to terrain", ['contract_id' => $contract->id, 'segment' => $segment->name ]);
}
}
public function failed(ContractToTerrain $event, $exception)
{
//\Log::error('Failed to update inventory', ['contract_id' => $event->contract->id, 'error' => $exception->getMessage()]);
}
}
+7 -1
View File
@@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Builder;
use Laravel\Scout\Searchable;
@@ -47,7 +48,8 @@ public function client(): BelongsTo
public function person(): BelongsTo
{
return $this->belongsTo(\App\Models\Person\Person::class);
return $this->belongsTo(\App\Models\Person\Person::class)
->with(['phones', 'addresses']);
}
public function contracts(): HasMany
@@ -59,4 +61,8 @@ public function activities(): HasMany
{
return $this->hasMany(\App\Models\Activity::class);
}
public function segments(): BelongsToMany {
return $this->belongsToMany(\App\Models\Segment::class)->withTimestamps();
}
}
+13 -9
View File
@@ -3,10 +3,15 @@
namespace App\Models;
use App\Traits\Uuid;
use Illuminate\Database\Eloquent\Factories\BelongsToManyRelationship;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasOneOrManyThrough;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\SoftDeletes;
class Contract extends Model
@@ -36,17 +41,16 @@ public function type(): BelongsTo
return $this->belongsTo(\App\Models\ContractType::class, 'type_id');
}
public function client(): BelongsTo
public function clientCase(): BelongsTo
{
return $this->belongsTo(\App\Models\Person\Person::class, 'client_id');
return $this->belongsTo(\App\Models\ClientCase::class)
->with(['person']);
}
public function debtor(): BelongsTo
{
return $this->belongsTo(\App\Models\Person\Person::class, 'debtor_id');
}
public function segments(): BelongsToMany {
return $this->belongsToMany(\App\Models\Segment::class);
return $this->belongsToMany(\App\Models\Segment::class)
->withPivot('active', 'created_at')
->wherePivot('active', true);
}
}
+6
View File
@@ -4,9 +4,15 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class Event extends Model
{
/** @use HasFactory<\Database\Factories\EventFactory> */
use HasFactory;
public function decisions(): BelongsToMany
{
return $this->belongsToMany(\App\Models\Decision::class);
}
}
+4 -2
View File
@@ -76,14 +76,16 @@ public function phones(): HasMany
{
return $this->hasMany(\App\Models\Person\PersonPhone::class)
->with(['type'])
->where('active','=',1);
->where('active','=',1)
->orderBy('id');
}
public function addresses(): HasMany
{
return $this->hasMany(\App\Models\Person\PersonAddress::class)
->with(['type'])
->where('active','=',1);
->where('active','=',1)
->orderBy('id');
}
public function group(): BelongsTo
+4
View File
@@ -14,4 +14,8 @@ class Segment extends Model
public function contracts(): BelongsToMany {
return $this->belongsToMany(\App\Models\Contract::class);
}
public function clientCase(): BelongsToMany {
return $this->belongsToMany(\App\Models\ClientCase::class)->withTimestamps();
}
}