diff --git a/app/Http/Controllers/ReportActivityMaterialController.php b/app/Http/Controllers/ReportActivityMaterialController.php index 9ed7bea..b2cf7c7 100644 --- a/app/Http/Controllers/ReportActivityMaterialController.php +++ b/app/Http/Controllers/ReportActivityMaterialController.php @@ -31,7 +31,6 @@ class ReportActivityMaterialController extends Controller if(!ReportActivityMaterial::create($data)) return response()->json(['status'=>'failed','message'=>'Input progress report activity failed created','code'=>400]); - $this->countForProgress($request->activity_id); return response()->json(['status'=>'success','message'=>'Input progress report activity created','code'=>200]); } @@ -68,46 +67,6 @@ class ReportActivityMaterialController extends Controller return response()->json(['status'=>'success','message'=> 'Data deleted!','code'=>200], 200); } - private function countForProgress($id) - { - - $dataActivity = Activity::find($id); - $dataPlan = AssignMaterial::where('activity_id', $id)->get(); - $tmpPercentage1 = []; - if(!$dataPlan->isEmpty()){ - - foreach ($dataPlan as $value) { - # code... - $price = $value->material_id; - $qty_plan = $value->qty_planning; - $status_activity = $value->status_activity; - - $getDataVolActual = ReportActivityMaterial::where('assign_material_id', '=', $value->id)->sum("qty"); - $percentage1 = ($getDataVolActual * 100) / $qty_plan; - - if($status_activity == 'done'){ - $tmpPercentage1[] = $percentage1 > 100 ? 100 : $percentage1; - }else if($status_activity == 'on-progress'){ - $tmpPercentage1[] = $percentage1 >= config('app.max_percentage_not_done') ? config('app.max_percentage_not_done') : $percentage1; - }else{ - $tmpPercentage1[] = $percentage1 >= config('max_percentage_not_done') ? config('app.max_percentage_not_done') : $percentage1; - } - } - $totalPercentage = array_sum($tmpPercentage1) / count($tmpPercentage1); - }else{ - $totalPercentage = 0; - } - - - - - $dataUpdate = array( - "persentase_progress" => $totalPercentage, - "updated_by" => $this->currentName - ); - return $dataActivity->update($dataUpdate); - } - public function search(Request $request) { $payload = $request->all(); diff --git a/app/Models/Activity.php b/app/Models/Activity.php index 9ad288b..ac836bf 100644 --- a/app/Models/Activity.php +++ b/app/Models/Activity.php @@ -134,10 +134,9 @@ class Activity extends Model return 0; if(!$dataPlan = AssignMaterial::where('activity_id', $this->id)->get()) return 0; - $totalPlan = $dataPlan->sum('qty_planning'); - $totalVolumeActual = ReportActivityMaterial::where('activity_id', '=', $this->id)->sum("qty"); - $percentage = ($totalVolumeActual * 100) / $totalPlan; - return $percentage > config('app.max_percentage_not_done') ? config('app.max_percentage_not_done') : $percentage; + if($dataPlan[0]->status_activity == 'done') + return 100; + return $this->persentase_progress; } public function getAssignHrAttribute() diff --git a/app/Models/AssignMaterial.php b/app/Models/AssignMaterial.php index 988446a..1b6d29e 100644 --- a/app/Models/AssignMaterial.php +++ b/app/Models/AssignMaterial.php @@ -14,7 +14,9 @@ class AssignMaterial extends Model const UPDATED_AT = 'updated_at'; protected $fillable = [ - 'proyek_id', 'activity_id', 'material_id', 'qty_planning', 'budget', 'plan_date','created_at', 'created_by', 'updated_at', 'updated_by' + 'proyek_id', 'activity_id', 'material_id', 'qty_planning', + 'budget', 'plan_date', 'status_activity', + 'created_at', 'created_by', 'updated_at', 'updated_by' ]; protected $casts = [ diff --git a/app/Models/ReportActivityMaterial.php b/app/Models/ReportActivityMaterial.php index 2b90acb..3caac56 100644 --- a/app/Models/ReportActivityMaterial.php +++ b/app/Models/ReportActivityMaterial.php @@ -30,13 +30,22 @@ class ReportActivityMaterial extends Model parent::boot(); static::created(function($data) { - $activity = Activity::find($data->activity_id); $assignedMaterial = AssignMaterial::find($data->assign_material_id); $activity->biaya_actual += floatval($assignedMaterial->budget) * floatval($data->qty); - $activity->save(); + $dataPlan = AssignMaterial::where('activity_id', $activity->id)->get(); + if($dataPlan[0]->status_activity == 'done'){ + $activity->persentase_progress = 100; + } else { + $totalPlan = $dataPlan->sum('qty_planning'); + $totalVolumeActual = ReportActivityMaterial::where('activity_id', '=', $activity->id)->sum("qty"); + $percentage = ($totalVolumeActual * 100) / $totalPlan; + $activity->persentase_progress = $percentage >= config('app.max_percentage_not_done') ? config('app.max_percentage_not_done') : $percentage; + } + + $activity->save(); }); static::deleted(function($data) { @@ -45,6 +54,17 @@ class ReportActivityMaterial extends Model $assignedMaterial = AssignMaterial::find($data->assign_material_id); $activity->biaya_actual -= floatval($assignedMaterial->budget) * floatval($data->qty); + + $dataPlan = AssignMaterial::where('activity_id', $activity->id)->get(); + if($dataPlan[0]->status_activity == 'done'){ + $activity->persentase_progress = 100; + } else { + $totalPlan = $dataPlan->sum('qty_planning'); + $totalVolumeActual = ReportActivityMaterial::where('activity_id', '=', $activity->id)->sum("qty"); + $percentage = ($totalVolumeActual * 100) / $totalPlan; + $activity->persentase_progress = $percentage >= config('app.max_percentage_not_done') ? config('app.max_percentage_not_done') : $percentage; + } + $activity->save(); });