Custom Backend OSPRO Surveyor Indonesia
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.1 KiB

3 years ago
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Models\Activity;
use App\Models\Link;
use DateTime;
3 years ago
class VersionGantt extends Model
{
protected $table = 'm_version_gantt';
const CREATED_AT = 'created_at';
const UPDATED_AT = 'updated_at';
protected $fillable = [
2 years ago
'name_version',
3 years ago
'description',
'date_base_line',
'proyek_id',
'config_dayoff',
2 years ago
'auto_schedule',
'calculation_type',
'committed_cost',
'cost_to_complete',
'progress',
'bobot',
'hierarchy_ftth_id',
2 years ago
'created_at',
'created_by',
'updated_at',
3 years ago
'updated_by'
];
public static function boot() {
parent::boot();
static::updated(function($data) {
$data->updateActDuration();
});
}
public function updateActDuration(){
$daysOff = explode(',', $this->config_dayoff);
if (in_array('0', $daysOff)) {
$key = array_search('0', $daysOff, false);
$daysOff[$key] = '7';
}
$activities = Activity::where('version_gantt_id', $this->id)->get();
foreach ($activities as $value) {
$exist = Link::where('t_activity_id', $value->id)->exists();
$startDate = new DateTime($value->start_date);
$endDate = new DateTime($value->end_date);
$duration = $endDate->diff($startDate)->days + 1;
if ($exist) {
$duration--;
}
// Iterate through each day and subtract the days off
for ($i = 0; $i < $duration; $i++) {
$currentDate = clone $startDate;
$currentDate->modify("+$i day");
$currentDayOfWeek = $currentDate->format('N'); // Get the day of the week (1 - Monday, 7 - Sunday)
if (in_array($currentDayOfWeek, $daysOff)) {
$duration--; // Subtract one day from the duration for each day off
}
}
// Update the activity duration
$value->duration = $duration;
$value->save();
}
}
3 years ago
}