diff --git a/.gitignore b/.gitignore index 66ed42a..4bde401 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ Homestead.yaml api-testing.http bootstrap/backup .config-prod.json +activity_log.txt diff --git a/app/Http/Controllers/ActivityController.php b/app/Http/Controllers/ActivityController.php index da5c268..80b3624 100644 --- a/app/Http/Controllers/ActivityController.php +++ b/app/Http/Controllers/ActivityController.php @@ -325,8 +325,8 @@ class ActivityController extends Controller } $data = Activity::find($id); - $oldRencanaBiaya = (int)$data->rencana_biaya; - $newRencanaBiaya = (int)$request->rencana_biaya; + $oldRencanaBiaya = $data->rencana_biaya; + $newRencanaBiaya = str_replace(",",".",$request->rencana_biaya); if($oldRencanaBiaya==$newRencanaBiaya){ $updateBobot = false; }else{ @@ -341,7 +341,6 @@ class ActivityController extends Controller $dataUpdate['type_activity'] = $request->type; } if($data){ - $upactual = false; $result = $data->update($dataUpdate); if($result){ if($parent && (int)$parent > 0){ @@ -448,6 +447,7 @@ class ActivityController extends Controller $data = Activity::find($id); if($data){ $data->progress = (int)$data->persentase_progress/100; + $data->rencana_biaya = str_replace(".", ",", $data->rencana_biaya); $data->jobs_done = $this->sumVolumeActualMaterial($data->id); $data->assign_hr = $this->getUserActivity($data->id); $data->assign_tools = $this->getToolsActivity($data->id); @@ -475,14 +475,6 @@ class ActivityController extends Controller } } - private function calculateProgress($id) - { - $sumBobot = Activity::where("parent_id", $id)->sum("bobot_planning"); - $dataActivity = Activity::find($id); - $restBobot = $dataActivity->bobot_planning - $sumBobot; - return $restBobot; - } - private function updatePersentaseProgress($id) { $sumProgress = Activity::where("parent_id", $id)->sum("persentase_progress"); diff --git a/app/Http/Controllers/AssignMaterialController.php b/app/Http/Controllers/AssignMaterialController.php index 7460da9..a3a7991 100644 --- a/app/Http/Controllers/AssignMaterialController.php +++ b/app/Http/Controllers/AssignMaterialController.php @@ -99,7 +99,16 @@ class AssignMaterialController extends Controller public function datatables(Request $request){ $id_activity = $request->query('idact'); - $data = AssignMaterial::select("assign_material_to_activity.*","m.description as material_name", "m.uom as uom")->join("m_req_material as m", "m.id", "=", "assign_material_to_activity.material_id")->where('assign_material_to_activity.activity_id', $id_activity)->get(); + $data = AssignMaterial::select( + "assign_material_to_activity.*","m.description as material_name", "m.uom as uom" + ) + ->join("m_req_material as m", "m.id", "=", "assign_material_to_activity.material_id") + ->where('assign_material_to_activity.activity_id', $id_activity) + ->orderBy('plan_date', 'desc') + ->get(); + foreach($data as $d){ + $d->budget = str_replace(".", ",", $d->budget); + } return Datatables::of($data) ->addIndexColumn() ->addColumn('action', function($row){ @@ -111,7 +120,13 @@ class AssignMaterialController extends Controller public function datatablesForReportActivity(Request $request){ $id_activity = $request->query('idact'); - $data = AssignMaterial::select(AssignMaterial::raw('SUM(qty_planning) as qty_planning'), "m.description as material_name", "assign_material_to_activity.activity_id as activity_id") + $data = + AssignMaterial::select( + AssignMaterial::raw( + 'SUM(qty_planning) as qty_planning'), + "m.description as material_name", + "assign_material_to_activity.activity_id as activity_id" + ) ->join("m_req_material as m", "m.id", "=", "assign_material_to_activity.material_id") ->groupBy("m.description") ->groupBy("assign_material_to_activity.activity_id") diff --git a/app/Http/Controllers/DivisiController.php b/app/Http/Controllers/DivisiController.php index aa265d8..06a533c 100644 --- a/app/Http/Controllers/DivisiController.php +++ b/app/Http/Controllers/DivisiController.php @@ -7,10 +7,20 @@ use App\Models\Divisi; class DivisiController extends Controller { + + private function getAllChildren($divisi, $depth = 0, $array = []) { + $array[$divisi->id] = $divisi->name; + foreach($divisi->children as $child){ + $array = $this->getAllChildren($child, $depth + 1, $array); + } + return $array; + } + public function add(Request $request){ $this->validate($request, [ 'name' => 'required', - 'description' => 'required' + 'description' => 'string', + 'parent' => 'integer' ]); $data = $request->all(); @@ -53,7 +63,6 @@ class DivisiController extends Controller $delete = $data->delete(); - if(!$delete) return response()->json(['status'=>'failed','message'=> 'Delete failed!','code'=> 500], 500); @@ -64,6 +73,9 @@ class DivisiController extends Controller { $payload = $request->all(); + if($payload['columns'][0]['value'] == "") + $this->list(); + $dataBuilder = $this->setUpPayload($payload, 'm_divisi'); $builder = $dataBuilder['builder']; $countBuilder = $dataBuilder['count']; @@ -75,12 +87,16 @@ class DivisiController extends Controller public function list() { - $data = Divisi::all(); - $countData = $data->count(); - - if(!$data) + $parentMenus = Divisi::whereNull('parent')->with('children')->get(); + $divisions = []; + foreach($parentMenus as $menu){ + $childs = $this->getAllChildren($menu); + $divisions = $divisions + $childs; + } + $countData = count($divisions); + if($countData == 0) return response()->json(['status'=>'failed','message'=>'Data not found!','code'=> 404], 404); - return response()->json(['status'=>'success','code'=>200,'data'=>$data, 'totalRecord'=>$countData], 200); + return response()->json(['status'=>'success','code'=>200,'data'=> $divisions, 'totalRecord'=> $countData], 200); } } diff --git a/app/Http/Controllers/MaterialResourceController.php b/app/Http/Controllers/MaterialResourceController.php index 5975279..f7fa569 100644 --- a/app/Http/Controllers/MaterialResourceController.php +++ b/app/Http/Controllers/MaterialResourceController.php @@ -18,6 +18,7 @@ class MaterialResourceController extends Controller ]); $data = $request->all(); + $data['description'] = trim($data['description']); $data['created_by'] = $this->currentName; @@ -59,8 +60,8 @@ class MaterialResourceController extends Controller return response()->json(['status'=>'failed','message'=>'data material resource not found!','code'=>400], 400); die(); } - - + + if($result){ return response()->json(['status'=>'success','message'=>'data material resource successfully updated!','code'=>200], 200); }else{ @@ -78,7 +79,7 @@ class MaterialResourceController extends Controller return response()->json(['status'=>'failed','message'=>'data material resource not found!','code'=>400], 400); die(); } - + if($delete){ return response()->json(['status'=>'success','message'=>'data material resource successfully deleted!','code'=>200], 200); diff --git a/app/Http/Controllers/ReportActivityMaterialController.php b/app/Http/Controllers/ReportActivityMaterialController.php index 7473e39..fe726a2 100644 --- a/app/Http/Controllers/ReportActivityMaterialController.php +++ b/app/Http/Controllers/ReportActivityMaterialController.php @@ -2,7 +2,7 @@ namespace App\Http\Controllers; -use Illuminate\Http\Request; +use Illuminate\Http\Request; use App\Models\ReportActivityMaterial; use App\Models\Activity; use App\Models\AssignMaterial; @@ -11,209 +11,182 @@ use Datatables; class ReportActivityMaterialController extends Controller { - public function add(Request $request){ - $this->validate($request, [ - 'activity_id' => 'required', - 'qty' => 'required' - ]); - - $valAssignMaterialId = AssignMaterial::where('activity_id', $request->activity_id)->first(); - - $data = $request->all(); - $data['created_by'] = $this->currentName; - $data['assign_material_id'] = $valAssignMaterialId->id; - - $checkPrice = AssignMaterial::where("id", $valAssignMaterialId->id)->first(); - $price = $checkPrice->budget; - - $updateActivity = Activity::find($request->activity_id); - if($updateActivity){ - $costActualOld = $updateActivity->biaya_actual == null ? 0 : (int)$updateActivity->biaya_actual; - $costActualNew = $costActualOld + ($price*$request->qty); - $updateActivity->biaya_actual = $costActualNew; - $updateActivity->save(); - $result = ReportActivityMaterial::create($data); - if($result){ - $this->countForProgress($request->activity_id); - return response()->json(['status'=>'success','message'=>'Input progress report activity created','code'=>200]); - }else{ - return response()->json(['status'=>'failed','message'=>'Input progress report activity failed created','code'=>400]); - } - }else{ - return response()->json(['status'=>'success','message'=>'Update cost actual failed','code'=>400]); - } - } - - public function updateStatusStartFinish(Request $request){ - - $payloadUpdate = array( - 'start_activity' => $request->start_activity, - 'finish_activity' => $request->finish_activity, - 'status_activity' => $request->status_activity - ); - $updateData = AssignMaterial::where('activity_id',$request->activity_id) - ->update($payloadUpdate); - if($updateData){ - if($request->status_activity == 'done'){ - $dataUpdate = array( - "persentase_progress" => 100, - "updated_by" => $this->currentName - ); - Activity::where('id', $request->activity_id)->update($dataUpdate); - } - - return response()->json(['status'=>'success','message'=>'Update data status successfully ', 'code'=>200]); - } - else{ - return response()->json(['status'=>'failed','message'=>'Update data status failed', 'code'=>400]); - } - } - - public function delete($id) - { - $data = ReportActivityMaterial::find($id); - - if($data){ - $delete = $data->delete(); - }else{ - return response()->json(['status'=>'failed','message'=>'data menu not found!','code'=>400], 400); - die(); - } - - - if($delete){ - return response()->json(['status'=>'success','message'=>'data menu successfully deleted!','code'=>200], 200); - }else{ - return response()->json(['status'=>'failed','message'=>'data menu failed deleted!','code'=>400], 400); - } - } - - 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 >= 100 ? 90 : $percentage1; - }else{ - $tmpPercentage1[] = $percentage1 >= 100 ? 90 : $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 countForProgressTest() - { - $dataPlan = AssignMaterial::where('activity_id', 807)->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; - $percentage1 = ($getDataVolActual * 100) / $qty_plan; - if($status_activity == 'done'){ - $tmpPercentage1[] = $percentage1 > 100 ? 100 : $percentage1; - }else if($status_activity == 'on-progress'){ - $tmpPercentage1[] = $percentage1 >= 100 ? 90 : $percentage1; - }else{ - $tmpPercentage1[] = $percentage1 >= 100 ? 90 : $percentage1; - } - - - } - $totalPercentage = array_sum($tmpPercentage1) / count($tmpPercentage1); - }else{ - $totalPercentage = 0; - } - - return response()->json(['status'=>'success','code'=>200,'data'=>$dataPlan, 'tmpPercentage1'=> $tmpPercentage1, 'totalPercentage'=> $totalPercentage], 200); - } - - public function search(Request $request) - { - $payload = $request->all(); - $dataBuilder = $this->setUpPayload($payload, 'report_activity_material'); - $builder = $dataBuilder['builder']; - $countBuilder = $dataBuilder['count']; - $dataGet = $builder->get(); - $totalRecord = $countBuilder->count(); - return response()->json(['status'=>'success','code'=>200,'data'=>$dataGet, 'totalRecord'=>$totalRecord], 200); - } - - public function list() - { - $data = ReportActivityMaterial::all(); - $countData = $data->count(); - - if($data){ - return response()->json(['status'=>'success','code'=>200,'data'=>$data, 'totalRecord'=>$countData], 200); - }else{ - return response()->json(['status'=>'failed','message'=>'failed get Repport Activity material, please try again later!','code'=>400], 400); - } - } - - public function datatables(Request $request){ - $id_activity = $request->query('idAct'); - $id_assign_material_id= $request->query('idAmi'); - $type = $request->query('type'); - if($type == 'plan'){ - $data = AssignMaterial::select("assign_material_to_activity.*","m.description as material_name", "m.uom as uom") - ->join("m_req_material as m", "m.id", "=", "assign_material_to_activity.material_id") - ->where('assign_material_to_activity.activity_id', $id_activity) - ->orderBy('assign_material_to_activity.id', 'asc') - ->get(); - return Datatables::of($data) - ->addIndexColumn() - ->addColumn('action', function($row){ - $actionBtn = ''; - return $actionBtn; - }) - ->rawColumns(['action'])->make(true); - }else{ - $data = ReportActivityMaterial::select("report_activity_material.*", "u.name as human_resource") - ->join("m_users as u", "u.id", "=", "report_activity_material.user_id") - ->where('report_activity_material.activity_id', $id_activity) - ->orderBy('report_activity_material.report_date', 'asc') - ->get(); - return Datatables::of($data) - ->addIndexColumn() - ->addColumn('action', function($row){ - $actionBtn = ''; - return $actionBtn; - }) - ->rawColumns(['action'])->make(true); - } - } + public function add(Request $request){ + $this->validate($request, [ + 'activity_id' => 'required', + 'qty' => 'required' + ]); + + $data = $request->all(); + $data['created_by'] = $this->currentName; + $data['assign_material_id'] = AssignMaterial::where('activity_id', $request->activity_id)->pluck('id')->first(); + + 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]); + } + + public function updateStatusStartFinish(Request $request){ + + $payloadUpdate = array( + 'start_activity' => $request->start_activity, + 'finish_activity' => $request->finish_activity, + 'status_activity' => $request->status_activity + ); + $updateData = AssignMaterial::where('activity_id',$request->activity_id) + ->update($payloadUpdate); + if($updateData){ + if($request->status_activity == 'done'){ + $dataUpdate = array( + "persentase_progress" => 100, + "updated_by" => $this->currentName + ); + Activity::where('id', $request->activity_id)->update($dataUpdate); + } + + return response()->json(['status'=>'success','message'=>'Update data status successfully ', 'code'=>200]); + } + else{ + return response()->json(['status'=>'failed','message'=>'Update data status failed', 'code'=>400]); + } + } + + public function delete($id) + { + if(!ReportActivityMaterial::findOrFail($id)->delete()) + return response()->json(['status'=>'failed','message'=>'Failed to deleted the data!','code'=> 500], 500); + + 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 >= 100 ? 90 : $percentage1; + }else{ + $tmpPercentage1[] = $percentage1 >= 100 ? 90 : $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 countForProgressTest() + { + $dataPlan = AssignMaterial::where('activity_id', 807)->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; + $percentage1 = ($getDataVolActual * 100) / $qty_plan; + if($status_activity == 'done'){ + $tmpPercentage1[] = $percentage1 > 100 ? 100 : $percentage1; + }else if($status_activity == 'on-progress'){ + $tmpPercentage1[] = $percentage1 >= 100 ? 90 : $percentage1; + }else{ + $tmpPercentage1[] = $percentage1 >= 100 ? 90 : $percentage1; + } + + + } + $totalPercentage = array_sum($tmpPercentage1) / count($tmpPercentage1); + }else{ + $totalPercentage = 0; + } + + return response()->json(['status'=>'success','code'=>200,'data'=>$dataPlan, 'tmpPercentage1'=> $tmpPercentage1, 'totalPercentage'=> $totalPercentage], 200); + } + + public function search(Request $request) + { + $payload = $request->all(); + $dataBuilder = $this->setUpPayload($payload, 'report_activity_material'); + $builder = $dataBuilder['builder']; + $countBuilder = $dataBuilder['count']; + $dataGet = $builder->get(); + $totalRecord = $countBuilder->count(); + return response()->json(['status'=>'success','code'=>200,'data'=>$dataGet, 'totalRecord'=>$totalRecord], 200); + } + + public function list() + { + $data = ReportActivityMaterial::all(); + $countData = $data->count(); + + if($data){ + return response()->json(['status'=>'success','code'=>200,'data'=>$data, 'totalRecord'=>$countData], 200); + }else{ + return response()->json(['status'=>'failed','message'=>'failed get Repport Activity material, please try again later!','code'=>400], 400); + } + } + + public function datatables(Request $request){ + $id_activity = $request->query('idAct'); + $id_assign_material_id= $request->query('idAmi'); + $type = $request->query('type'); + if($type == 'plan'){ + $data = AssignMaterial::select("assign_material_to_activity.*","m.description as material_name", "m.uom as uom") + ->join("m_req_material as m", "m.id", "=", "assign_material_to_activity.material_id") + ->where('assign_material_to_activity.activity_id', $id_activity) + ->orderBy('assign_material_to_activity.id', 'asc') + ->get(); + return Datatables::of($data) + ->addIndexColumn() + ->addColumn('action', function($row){ + $actionBtn = ''; + return $actionBtn; + }) + ->rawColumns(['action'])->make(true); + }else{ + $data = ReportActivityMaterial::select("report_activity_material.*", "u.name as human_resource") + ->join("m_users as u", "u.id", "=", "report_activity_material.user_id") + ->where('report_activity_material.activity_id', $id_activity) + ->orderBy('report_activity_material.report_date', 'asc') + ->get(); + return Datatables::of($data) + ->addIndexColumn() + ->addColumn('action', function($row){ + $actionBtn = ''; + return $actionBtn; + }) + ->rawColumns(['action'])->make(true); + } + } } diff --git a/app/Http/Controllers/RequestMaterialController.php b/app/Http/Controllers/RequestMaterialController.php index 474e2af..2f4ee1a 100644 --- a/app/Http/Controllers/RequestMaterialController.php +++ b/app/Http/Controllers/RequestMaterialController.php @@ -19,7 +19,8 @@ class RequestMaterialController extends Controller ]); $data = $request->all(); - + $data['price'] = str_replace(",", ".", $data['price']); + $data['qty'] = str_replace(",", ".", $data['qty']); $data['status'] = "fom"; $data['created_by'] = $this->currentName; diff --git a/app/Models/AssignMaterial.php b/app/Models/AssignMaterial.php index 297b525..e3957a8 100644 --- a/app/Models/AssignMaterial.php +++ b/app/Models/AssignMaterial.php @@ -20,8 +20,7 @@ class AssignMaterial extends Model protected $casts = [ 'id' => 'integer', - 'budget' => 'float', - 'qty_planning' => 'float', + 'budget' => 'string', ]; public static function boot() { diff --git a/app/Models/Divisi.php b/app/Models/Divisi.php index 9b0dea1..5a37c85 100644 --- a/app/Models/Divisi.php +++ b/app/Models/Divisi.php @@ -12,11 +12,30 @@ class Divisi extends Model const UPDATED_AT = 'updated_at'; protected $fillable = [ - 'name', - 'description', - 'created_at', - 'created_by', - 'updated_at', + 'name', + 'parent', + 'description', + 'created_at', + 'created_by', + 'updated_at', 'updated_by' ]; + + public static function boot() { + parent::boot(); + + static::deleting(function($data) { + $data->children()->delete(); + }); + } + + public function parent() + { + return $this->belongsTo('App\Models\Divisi','parent')->where('parent', null)->with('parent'); + } + + public function children() + { + return $this->hasMany('App\Models\Divisi','parent')->with('children'); + } } diff --git a/app/Models/ReportActivityMaterial.php b/app/Models/ReportActivityMaterial.php index 4c455ee..1549248 100644 --- a/app/Models/ReportActivityMaterial.php +++ b/app/Models/ReportActivityMaterial.php @@ -3,16 +3,43 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; +use App\Models\Activity; +use App\Models\AssignMaterial; class ReportActivityMaterial extends Model { - protected $table = 'report_activity_material'; + protected $table = 'report_activity_material'; - const CREATED_AT = 'created_at'; - const UPDATED_AT = 'updated_at'; + const CREATED_AT = 'created_at'; + const UPDATED_AT = 'updated_at'; - protected $fillable = [ - 'activity_id', 'user_id', 'qty', 'lat', 'lon','assign_material_id', - 'report_date', 'description', 'created_at', 'created_by', 'updated_at', 'updated_by' - ]; + protected $fillable = [ + 'activity_id', 'user_id', 'qty', 'lat', 'lon','assign_material_id', + 'report_date', 'description', 'created_at', 'created_by', 'updated_at', 'updated_by' + ]; + + public static function boot() { + 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(); + + }); + + static::deleted(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(); + + }); + + } } diff --git a/app/Models/RequestMaterial.php b/app/Models/RequestMaterial.php index df32ec2..c4239fd 100644 --- a/app/Models/RequestMaterial.php +++ b/app/Models/RequestMaterial.php @@ -13,8 +13,7 @@ class RequestMaterial extends Model protected $casts = [ 'id' => 'integer', - 'price' => 'float', - 'qty' => 'float', + 'price' => 'string', ]; protected $fillable = [