Browse Source

fix endline, fix sync report

pull/3/head
wahyu 1 year ago
parent
commit
0d74de7e74
  1. 1826
      app/Http/Controllers/ActivityController.php
  2. 8
      app/Http/Controllers/ProjectController.php
  3. 492
      app/Models/Activity.php

1826
app/Http/Controllers/ActivityController.php

File diff suppressed because it is too large Load Diff

8
app/Http/Controllers/ProjectController.php

@ -399,7 +399,13 @@ class ProjectController extends Controller
$firstReport = ReportActivityMaterial::where('activity_id', $activity_id)->orderBy('report_date')->first(); $firstReport = ReportActivityMaterial::where('activity_id', $activity_id)->orderBy('report_date')->first();
$reports[] = ProjectController::setSyncDate($activity_id, $activity, $firstReport); $reports[] = ProjectController::setSyncDate($activity_id, $activity, $firstReport);
} }
$activity->reports = $reports; $activity->reports = $reports;
$successor = Link::where('t_activity_id', $activity->id)->first();
if ($successor) {
$predecessor = Activity::find($successor->s_activity_id);
$activity->start_date = $predecessor->end_date;
}
$activity->save();
} }
/* return response()->json(['status'=>'success','data'=> $reports,'code'=>200], 200); */ /* return response()->json(['status'=>'success','data'=> $reports,'code'=>200], 200); */
/* return response()->json(['status'=>'success','data'=> $activities,'code'=>200], 200); */ /* return response()->json(['status'=>'success','data'=> $activities,'code'=>200], 200); */

492
app/Models/Activity.php

@ -1,246 +1,246 @@
<?php <?php
namespace App\Models; namespace App\Models;
use App\Models\AssignMaterial; use App\Models\AssignMaterial;
use App\Models\ActivityProgressLog; use App\Models\ActivityProgressLog;
use App\Models\AssignTools; use App\Models\AssignTools;
use App\Models\ReportActivityMaterial; use App\Models\ReportActivityMaterial;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Carbon\Carbon; use Carbon\Carbon;
class Activity extends Model class Activity extends Model
{ {
protected $table = 'm_activity'; protected $table = 'm_activity';
const CREATED_AT = 'created_at'; const CREATED_AT = 'created_at';
const UPDATED_AT = 'updated_at'; const UPDATED_AT = 'updated_at';
// persentase bobot gak kepake yg dipakenya bobot_planning // persentase bobot gak kepake yg dipakenya bobot_planning
protected $fillable = [ protected $fillable = [
'proyek_id', 'parent_id', 'kode_sortname', 'name', 'rencana_biaya', 'start_date', 'proyek_id', 'parent_id', 'kode_sortname', 'name', 'rencana_biaya', 'start_date',
'end_date', 'area_kerja', 'biaya_actual', 'persentase_bobot', 'persentase_progress', 'end_date', 'area_kerja', 'biaya_actual', 'persentase_bobot', 'persentase_progress',
'buffer_radius', 'duration', 'color_progress', 'jumlah_pekerjaan', 'satuan', 'buffer_radius', 'duration', 'color_progress', 'jumlah_pekerjaan', 'satuan',
'description', 'priority', 'bobot_planning', 'type_activity', 'open', 'geom', 'description', 'priority', 'bobot_planning', 'type_activity', 'open', 'geom',
'version_gantt_id', 'budget_plan', 'biaya_material_plan', 'biaya_human_plan', 'biaya_tools_plan', 'version_gantt_id', 'budget_plan', 'biaya_material_plan', 'biaya_human_plan', 'biaya_tools_plan',
'planned_start', 'planned_end', 'satuan_id', 'actual_start', 'actual_end', 'planned_start', 'planned_end', 'satuan_id', 'actual_start', 'actual_end',
'created_at', 'created_by', 'updated_at', 'updated_by', 'sortorder' 'created_at', 'created_by', 'updated_at', 'updated_by', 'sortorder'
]; ];
protected $appends = [ protected $appends = [
'jobs_done', 'assign_hr', 'assign_material', 'assign_tools', 'assign_expense' 'jobs_done', 'assign_hr', 'assign_material', 'assign_tools', 'assign_expense'
]; ];
public function getStartDateAttribute($value) public function getStartDateAttribute($value)
{ {
if ($value instanceof \DateTime) { if ($value instanceof \DateTime) {
return Carbon::instance($value) return Carbon::instance($value)
->timezone(env('APP_TIMEZONE')) ->timezone(env('APP_TIMEZONE'))
->toDateTimeString(); ->toDateTimeString();
} }
return Carbon::createFromTimestamp(strtotime($value)) return Carbon::createFromTimestamp(strtotime($value))
->timezone(env('APP_TIMEZONE')) ->timezone(env('APP_TIMEZONE'))
->toDateTimeString(); ->toDateTimeString();
} }
public function getEndDateAttribute($value) public function getEndDateAttribute($value)
{ {
if ($value instanceof \DateTime) { if ($value instanceof \DateTime) {
return Carbon::instance($value) return Carbon::instance($value)
->timezone(env('APP_TIMEZONE')) ->timezone(env('APP_TIMEZONE'))
->toDateTimeString(); ->toDateTimeString();
} }
return Carbon::createFromTimestamp(strtotime($value)) return Carbon::createFromTimestamp(strtotime($value))
->timezone(env('APP_TIMEZONE')) ->timezone(env('APP_TIMEZONE'))
->toDateTimeString(); ->toDateTimeString();
} }
public static function boot() { public static function boot() {
parent::boot(); parent::boot();
static::updating(function($data) { static::updating(function($data) {
$data->logPersentaseProgress(); $data->logPersentaseProgress();
}); });
static::updated(function($data) { static::updated(function($data) {
$data->updateBobot(); $data->updateBobot();
$data->updateCostPlanning(); $data->updateCostPlanning();
if($data->bobot_planning){ if($data->bobot_planning){
$data->updatePersentaseProgress(); $data->updatePersentaseProgress();
} }
$data->updateCostActual(); $data->updateCostActual();
// if($data->start_date != request()->start_date || $data->end_date != request()->end_date) { // if($data->start_date != request()->start_date || $data->end_date != request()->end_date) {
// $data->updateStartEndDateHeader(); // $data->updateStartEndDateHeader();
// } // }
}); });
static::deleted(function($data) { static::deleted(function($data) {
if(Activity::where("parent_id", $data->parent_id)->count() == 0) if(Activity::where("parent_id", $data->parent_id)->count() == 0)
Activity::find($data->parent_id)->update(["type_activity"=>"task"]); Activity::find($data->parent_id)->update(["type_activity"=>"task"]);
$data->updateBobot(true); $data->updateBobot(true);
$data->updateCostPlanning(); $data->updateCostPlanning();
if($data->bobot_planning){ if($data->bobot_planning){
$data->updatePersentaseProgress(); $data->updatePersentaseProgress();
} }
$data->updateCostActual(); $data->updateCostActual();
$data->updateStartEndDateHeader(); $data->updateStartEndDateHeader();
}); });
} }
private function updateBobot($isDelete = false) private function updateBobot($isDelete = false)
{ {
$root = Activity::where('version_gantt_id', $this->version_gantt_id) $root = Activity::where('version_gantt_id', $this->version_gantt_id)
->where("proyek_id", $this->proyek_id) ->where("proyek_id", $this->proyek_id)
->whereNull('parent_id') ->whereNull('parent_id')
->first(); ->first();
if($root->rencana_biaya > 0){ if($root->rencana_biaya > 0){
$activities = Activity::where("proyek_id", $this->proyek_id)->where("version_gantt_id", $this->version_gantt_id)->get(); $activities = Activity::where("proyek_id", $this->proyek_id)->where("version_gantt_id", $this->version_gantt_id)->get();
foreach ($activities as $activity) { foreach ($activities as $activity) {
if($isDelete && $activity->id == $this->id) if($isDelete && $activity->id == $this->id)
continue; continue;
$activity->bobot_planning = ( (int)$activity->rencana_biaya / $root->rencana_biaya) * 100; $activity->bobot_planning = ( (int)$activity->rencana_biaya / $root->rencana_biaya) * 100;
$activity->updated_by = auth()->user() ? auth()->user()->name : "system"; $activity->updated_by = auth()->user() ? auth()->user()->name : "system";
$activity->saveQuietly(); $activity->saveQuietly();
} }
} else { } else {
if($parent = Activity::find($this->parent_id)){ if($parent = Activity::find($this->parent_id)){
$totalChildWeight = Activity::where("parent_id", $this->parent_id)->sum('bobot_planning'); $totalChildWeight = Activity::where("parent_id", $this->parent_id)->sum('bobot_planning');
$parent->update([ $parent->update([
"bobot_planning" => $totalChildWeight "bobot_planning" => $totalChildWeight
]); ]);
} }
} }
} }
private function updateCostActual() private function updateCostActual()
{ {
$actualCost = Activity::where("parent_id", $this->parent_id)->sum("biaya_actual"); $actualCost = Activity::where("parent_id", $this->parent_id)->sum("biaya_actual");
$this->biaya_actual = $actualCost; $this->biaya_actual = $actualCost;
if($parent = Activity::find($this->parent_id)){ if($parent = Activity::find($this->parent_id)){
$parent->update([ $parent->update([
"biaya_actual" => $actualCost "biaya_actual" => $actualCost
]); ]);
} }
} }
private function updatePersentaseProgress() private function updatePersentaseProgress()
{ {
if($parent = Activity::find($this->parent_id)){ if($parent = Activity::find($this->parent_id)){
$parentActWeight = $parent->bobot_planning; $parentActWeight = $parent->bobot_planning;
if ($parentActWeight == 0) { if ($parentActWeight == 0) {
$parent->update([ $parent->update([
"persentase_progress" => 0 "persentase_progress" => 0
]); ]);
return; return;
} }
$totalChildProportionalProgress = 0; $totalChildProportionalProgress = 0;
$childs = Activity::where("parent_id", $parent->id)->get(); $childs = Activity::where("parent_id", $parent->id)->get();
foreach($childs as $child){ foreach($childs as $child){
$currentActWeight = $child->bobot_planning; $currentActWeight = $child->bobot_planning;
$currentActProportionalProgress = ($currentActWeight / $parentActWeight) * $child->persentase_progress; $currentActProportionalProgress = ($currentActWeight / $parentActWeight) * $child->persentase_progress;
$totalChildProportionalProgress += $currentActProportionalProgress; $totalChildProportionalProgress += $currentActProportionalProgress;
} }
$parent->update([ $parent->update([
"persentase_progress" => $totalChildProportionalProgress "persentase_progress" => $totalChildProportionalProgress
]); ]);
} }
} }
private function updateCostPlanning() { private function updateCostPlanning() {
$sumBiaya = Activity::select(DB::raw('sum(cast(rencana_biaya as double precision))')) $sumBiaya = Activity::select(DB::raw('sum(cast(rencana_biaya as double precision))'))
->where("parent_id", $this->parent_id) ->where("parent_id", $this->parent_id)
->first(); ->first();
$this->rencana_biaya = $sumBiaya->sum; $this->rencana_biaya = $sumBiaya->sum;
if($parent = Activity::find($this->parent_id)){ if($parent = Activity::find($this->parent_id)){
$parent->update([ $parent->update([
"rencana_biaya" => $sumBiaya->sum, "rencana_biaya" => $sumBiaya->sum,
]); ]);
} }
} }
private function logPersentaseProgress() private function logPersentaseProgress()
{ {
ActivityProgressLog::create([ ActivityProgressLog::create([
'version_gantt_id' => $this->version_gantt_id, 'version_gantt_id' => $this->version_gantt_id,
'activity_id' => request()->id, 'activity_id' => request()->id,
'old_percentage' => $this->persentase_progress, 'old_percentage' => $this->persentase_progress,
'new_percentage' => request()->persentase_progress, 'new_percentage' => request()->persentase_progress,
'variance' => $this->persentase_progress - request()->persentase_progress, 'variance' => $this->persentase_progress - request()->persentase_progress,
'created_by'=> "system" 'created_by'=> "system"
]); ]);
} }
private function updateStartEndDateHeader() private function updateStartEndDateHeader()
{ {
$earliestStartDate = Activity::where('version_gantt_id', $this->version_gantt_id)->whereNotNull('parent_id')->oldest('start_date')->pluck('start_date')->first(); $earliestStartDate = Activity::where('version_gantt_id', $this->version_gantt_id)->whereNotNull('parent_id')->oldest('start_date')->pluck('start_date')->first();
$latestEndDate = Activity::where('version_gantt_id', $this->version_gantt_id)->whereNotNull('parent_id')->latest('end_date')->pluck('end_date')->first(); $latestEndDate = Activity::where('version_gantt_id', $this->version_gantt_id)->whereNotNull('parent_id')->latest('end_date')->pluck('end_date')->first();
if($header = Activity::where('version_gantt_id', $this->version_gantt_id)->whereNull('parent_id')->first()) { if($header = Activity::where('version_gantt_id', $this->version_gantt_id)->whereNull('parent_id')->first()) {
$header->start_date = $earliestStartDate; $header->start_date = $earliestStartDate;
$header->end_date = $latestEndDate; $header->end_date = $latestEndDate;
$header->saveQuietly(); $header->saveQuietly();
} }
} }
public function getJobsDoneAttribute() public function getJobsDoneAttribute()
{ {
if(!ReportActivityMaterial::where('activity_id', $this->id)->first()) if(!ReportActivityMaterial::where('activity_id', $this->id)->first())
return 0; return 0;
if(!$dataPlan = AssignMaterial::where('activity_id', $this->id)->get()) if(!$dataPlan = AssignMaterial::where('activity_id', $this->id)->get())
return 0; return 0;
if($dataPlan->isEmpty()) if($dataPlan->isEmpty())
return 0; return 0;
if($dataPlan[0]->status_activity == 'done') if($dataPlan[0]->status_activity == 'done')
return 100; return 100;
return $this->persentase_progress; return $this->persentase_progress;
} }
public function getAssignHrAttribute() public function getAssignHrAttribute()
{ {
return Arr::flatten(UserToActivity::select("u.name as name") return Arr::flatten(UserToActivity::select("u.name as name")
->join("m_users as u", "u.id", "=", "assign_hr_to_activity.user_id") ->join("m_users as u", "u.id", "=", "assign_hr_to_activity.user_id")
->where('assign_hr_to_activity.activity_id', $this->id) ->where('assign_hr_to_activity.activity_id', $this->id)
->get() ->get()
->toArray()); ->toArray());
} }
public function getAssignMaterialAttribute() public function getAssignMaterialAttribute()
{ {
return Arr::flatten(AssignMaterial::select("m.description as name") return Arr::flatten(AssignMaterial::select("m.description as name")
->join("m_req_material as m", "m.id", "=", "assign_material_to_activity.material_id") ->join("m_req_material as m", "m.id", "=", "assign_material_to_activity.material_id")
->where('assign_material_to_activity.activity_id', $this->id) ->where('assign_material_to_activity.activity_id', $this->id)
->where('assign_material_to_activity.type', "material") ->where('assign_material_to_activity.type', "material")
->get() ->get()
->toArray()); ->toArray());
} }
public function getAssignExpenseAttribute() public function getAssignExpenseAttribute()
{ {
return Arr::flatten(AssignMaterial::select("m.description as name") return Arr::flatten(AssignMaterial::select("m.description as name")
->join("m_req_material as m", "m.id", "=", "assign_material_to_activity.material_id") ->join("m_req_material as m", "m.id", "=", "assign_material_to_activity.material_id")
->where('assign_material_to_activity.activity_id', $this->id) ->where('assign_material_to_activity.activity_id', $this->id)
->where('assign_material_to_activity.type', "expense") ->where('assign_material_to_activity.type', "expense")
->get() ->get()
->toArray()); ->toArray());
} }
public function getAssignToolsAttribute() public function getAssignToolsAttribute()
{ {
return Arr::flatten(AssignTools::select("m.name as name") return Arr::flatten(AssignTools::select("m.name as name")
->join("m_tools_resource as m", "m.id", "=", "assign_tools_to_activity.tools_id") ->join("m_tools_resource as m", "m.id", "=", "assign_tools_to_activity.tools_id")
->where('assign_tools_to_activity.activity_id', $this->id) ->where('assign_tools_to_activity.activity_id', $this->id)
->get() ->get()
->toArray()); ->toArray());
} }
} }

Loading…
Cancel
Save