mirror of
https://bitbucket.org/studiotem/jandl-taskmanager-app.git
synced 2026-10-21 14:29:34 +00:00
171 lines
4.7 KiB
PHP
171 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class SubTask extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
public $timestamps = true;
|
|
public $guarded = ['id'];
|
|
|
|
protected $casts = [
|
|
'draft_data' => 'array',
|
|
];
|
|
|
|
/**
|
|
* Scope pre koncepty (drafty) - nezverejnené subtasky
|
|
*/
|
|
public function scopeDraft($query)
|
|
{
|
|
return $query->where('is_draft', true);
|
|
}
|
|
|
|
/**
|
|
* Scope pre publikované subtasky (nie drafty)
|
|
*/
|
|
public function scopePublished($query)
|
|
{
|
|
return $query->where('is_draft', false);
|
|
}
|
|
|
|
public function mastertask()
|
|
{
|
|
return $this->belongsTo(MasterTask::class, 'master_task_id');
|
|
}
|
|
|
|
public function user()
|
|
{
|
|
return $this->belongsTo(User::class)->withTrashed();
|
|
}
|
|
|
|
public function timeblocks()
|
|
{
|
|
return $this->hasMany(Timeblock::class);
|
|
}
|
|
|
|
public function timesheets()
|
|
{
|
|
return $this->hasMany(Timesheet::class);
|
|
}
|
|
|
|
public function attachments()
|
|
{
|
|
return $this->hasMany(Attachment::class);
|
|
}
|
|
|
|
// syncnutie vykazu. ak array obsahuje NULL id, tak ho vytvori, ak obsahuje ID, tak ho upravi. ak sa v array nenachadza, tak ho zmaze z DB
|
|
public function syncTimesheets(Array $timesheets)
|
|
{
|
|
// pripravenie array na upsert
|
|
$timesheets = array_map(function($item) {
|
|
$item['user_id'] = $item['employee_id'];
|
|
unset($item['employee_id']);
|
|
unset($item['timestamp']);
|
|
$item['sub_task_id'] = $this->id;
|
|
|
|
if (!$this->id) {
|
|
$item['created_at'] = Carbon::now();
|
|
$item['updated_at'] = Carbon::now();
|
|
}
|
|
return $item;
|
|
}, $timesheets);
|
|
|
|
// zmazanie vykazov, ktore neboli poslane z frontendu
|
|
$this->timesheets()->whereNotIn('id', collect($timesheets)->pluck('id')->filter())->delete();
|
|
|
|
$this->timesheets()->upsert($timesheets, ['id']);
|
|
|
|
// kedze updatujem cez upsert, tak sa neaktualizuje updated_at. preto ho musim updatnut manualne
|
|
$this->touch();
|
|
}
|
|
|
|
/**
|
|
* Synchronizácia timeblocks - aktualizuje existujúce, vytvorí nové, zmaže nepoužité
|
|
*/
|
|
public function syncTimeblocks(Array $timeblocks)
|
|
{
|
|
if (empty($timeblocks)) {
|
|
return $this->timeblocks()->delete();
|
|
}
|
|
|
|
// Získame ID existujúcich timeblocks tohto subtasku
|
|
$existingIds = $this->timeblocks()->pluck('id')->toArray();
|
|
|
|
// Rozdelíme na existujúce (s ID) a nové (bez ID alebo s neexistujúcim ID)
|
|
$timeblocksToUpdate = [];
|
|
$timeblocksToCreate = [];
|
|
$receivedIds = [];
|
|
|
|
foreach ($timeblocks as $item) {
|
|
$item['sub_task_id'] = $this->id;
|
|
|
|
// Ak má ID a toto ID existuje v našich timeblocks, aktualizujeme
|
|
if (isset($item['id']) && in_array($item['id'], $existingIds)) {
|
|
$timeblocksToUpdate[] = $item;
|
|
$receivedIds[] = $item['id'];
|
|
} else {
|
|
// Nový timeblock - odstránime ID aby sa vytvoril nový záznam
|
|
unset($item['id']);
|
|
$timeblocksToCreate[] = $item;
|
|
}
|
|
}
|
|
|
|
// Zmažeme timeblocks, ktoré neboli poslané z frontendu
|
|
$this->timeblocks()->whereNotIn('id', $receivedIds)->delete();
|
|
|
|
// Aktualizujeme existujúce
|
|
foreach ($timeblocksToUpdate as $timeblock) {
|
|
$this->timeblocks()->where('id', $timeblock['id'])->update([
|
|
'start' => $timeblock['start'],
|
|
'end' => $timeblock['end'],
|
|
]);
|
|
}
|
|
|
|
// Vytvoríme nové
|
|
if (! empty($timeblocksToCreate)) {
|
|
$this->timeblocks()->createMany($timeblocksToCreate);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Vytvorenie nových timeblocks - pre použitie pri publikovaní draftu
|
|
*/
|
|
public function createTimeblocks(Array $timeblocks)
|
|
{
|
|
if (empty($timeblocks)) {
|
|
return;
|
|
}
|
|
|
|
$timeblocksToCreate = array_map(function($item) {
|
|
// Odstránime ID a sub_task_id pre istotu
|
|
unset($item['id']);
|
|
unset($item['sub_task_id']);
|
|
return [
|
|
'start' => $item['start'],
|
|
'end' => $item['end'],
|
|
'is_finished' => $item['is_finished'] ?? false,
|
|
];
|
|
}, $timeblocks);
|
|
|
|
$this->timeblocks()->createMany($timeblocksToCreate);
|
|
}
|
|
|
|
public function deleteAttachments()
|
|
{
|
|
foreach ( $this->attachments as $attachment ) {
|
|
$attachment->deleteFromS3();
|
|
}
|
|
|
|
$this->attachments()->delete();
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|