mirror of
https://bitbucket.org/studiotem/jandl-taskmanager-app.git
synced 2026-10-21 14:29:34 +00:00
auto calculating tracked_time and estimated_time both for master and sub task
This commit is contained in:
1 parent
ef2ab75ce4
commit
ae900e24c0
7 files changed
+104
-34
No files matched your search
@@ -64,4 +64,23 @@ class MasterTask extends Model
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function recalculateEstimatedTime()
|
||||
{
|
||||
$this->estimated_time = $this->subtasks->sum('estimated_time');
|
||||
$this->save();
|
||||
}
|
||||
|
||||
public function recalculateTrackedTime()
|
||||
{
|
||||
$trackedTime = 0.0;
|
||||
|
||||
foreach ( $this->subtasks as $subtask ) {
|
||||
$trackedTime += $subtask->timesheets->sum('time');
|
||||
}
|
||||
|
||||
$this->tracked_time = $trackedTime;
|
||||
$this->save();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -46,8 +46,11 @@ class SubTask extends Model
|
||||
$item['user_id'] = $item['employee_id'];
|
||||
unset($item['employee_id']);
|
||||
$item['sub_task_id'] = $this->id;
|
||||
$item['created_at'] = Carbon::now();
|
||||
$item['updated_at'] = Carbon::now();
|
||||
|
||||
if (!$this->id) {
|
||||
$item['created_at'] = Carbon::now();
|
||||
$item['updated_at'] = Carbon::now();
|
||||
}
|
||||
return $item;
|
||||
}, $timesheets);
|
||||
|
||||
@@ -55,6 +58,9 @@ class SubTask extends Model
|
||||
$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();
|
||||
}
|
||||
|
||||
public function syncTimeblocks(Array $timeblocks)
|
||||
|
||||
@@ -12,6 +12,8 @@ class Timesheet extends Model
|
||||
public $timestamps = true;
|
||||
public $guarded = ['id'];
|
||||
|
||||
protected $touches = ['subtask'];
|
||||
|
||||
public function subtask()
|
||||
{
|
||||
return $this->belongsTo(SubTask::class, 'sub_task_id');
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace App\Observers;
|
||||
|
||||
use App\Models\SubTask;
|
||||
|
||||
class SubTaskObserver
|
||||
{
|
||||
/**
|
||||
* Handle the SubTask "created" event.
|
||||
*
|
||||
* @param \App\Models\SubTask $subTask
|
||||
* @return void
|
||||
*/
|
||||
public function created(SubTask $subTask)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function updating(SubTask $subTask)
|
||||
{
|
||||
$subTask->tracked_time = $subTask->timesheets->sum('time');
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the SubTask "updated" event.
|
||||
*
|
||||
* @param \App\Models\SubTask $subTask
|
||||
* @return void
|
||||
*/
|
||||
public function updated(SubTask $subTask)
|
||||
{
|
||||
$subTask->mastertask->recalculateEstimatedTime();
|
||||
$subTask->mastertask->recalculateTrackedTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the SubTask "deleted" event.
|
||||
*
|
||||
* @param \App\Models\SubTask $subTask
|
||||
* @return void
|
||||
*/
|
||||
public function deleted(SubTask $subTask)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the SubTask "restored" event.
|
||||
*
|
||||
* @param \App\Models\SubTask $subTask
|
||||
* @return void
|
||||
*/
|
||||
public function restored(SubTask $subTask)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the SubTask "force deleted" event.
|
||||
*
|
||||
* @param \App\Models\SubTask $subTask
|
||||
* @return void
|
||||
*/
|
||||
public function forceDeleted(SubTask $subTask)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,10 @@
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Models\MasterTask;
|
||||
use App\Models\SubTask;
|
||||
use App\Observers\MasterTaskObserver;
|
||||
use App\Observers\SubTaskObserver;
|
||||
use Illuminate\Auth\Events\Registered;
|
||||
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
|
||||
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
|
||||
@@ -27,7 +31,8 @@ class EventServiceProvider extends ServiceProvider
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
//
|
||||
MasterTask::observe( MasterTaskObserver::class);
|
||||
SubTask::observe( SubTaskObserver::class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Models\MasterTask;
|
||||
use App\Observers\MasterTaskObserver;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class MasterTaskServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Register services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Bootstrap services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
MasterTask::observe( MasterTaskObserver::class);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user