Browse Source

Merge pull request 'staging' (#39) from staging into master

Reviewed-on: ibnu/generic-ospro-backend#39
pull/1/head
farhantock 11 months ago
parent
commit
02f2c896f8
  1. 43
      app/Http/Controllers/AssignMaterialController.php
  2. 120
      app/Http/Controllers/ImageController.php
  3. 2
      routes/web.php

43
app/Http/Controllers/AssignMaterialController.php

@ -12,13 +12,15 @@ use Datatables;
class AssignMaterialController extends Controller class AssignMaterialController extends Controller
{ {
private function sanitizeDecimal($number) { private function sanitizeDecimal($number)
{
$number = str_replace(".", "", $number); $number = str_replace(".", "", $number);
$number = str_replace(",", ".", $number); $number = str_replace(",", ".", $number);
return $number; return $number;
} }
public function add(Request $request){ public function add(Request $request)
{
$this->validate($request, [ $this->validate($request, [
'activity_id' => 'required', 'activity_id' => 'required',
'material_id' => 'required', 'material_id' => 'required',
@ -51,7 +53,8 @@ class AssignMaterialController extends Controller
} }
} }
public function update(Request $request, $id){ public function update(Request $request, $id)
{
if (empty($id) || !is_int((int)$id)) if (empty($id) || !is_int((int)$id))
return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400); return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400);
@ -83,7 +86,8 @@ class AssignMaterialController extends Controller
return response()->json(['status' => 'failed', 'message' => 'Failed to delete!', 'code' => 500], 500); return response()->json(['status' => 'failed', 'message' => 'Failed to delete!', 'code' => 500], 500);
} }
public function edit($id){ public function edit($id)
{
if (empty($id) || !is_int((int)$id)) if (empty($id) || !is_int((int)$id))
return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400); return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400);
@ -117,11 +121,14 @@ class AssignMaterialController extends Controller
return response()->json(['status' => 'failed', 'message' => 'Failed to get the data!', 'code' => 500], 500); return response()->json(['status' => 'failed', 'message' => 'Failed to get the data!', 'code' => 500], 500);
} }
public function datatables(Request $request){ public function datatables(Request $request)
{
$id_activity = $request->query('idact'); $id_activity = $request->query('idact');
$type = $request->query('type') ?? "material"; $type = $request->query('type') ?? "material";
$data = AssignMaterial::select( $data = AssignMaterial::select(
"assign_material_to_activity.*","m.description as material_name", "m.uom as uom" "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") ->join("m_req_material as m", "m.id", "=", "assign_material_to_activity.material_id")
->where('assign_material_to_activity.activity_id', $id_activity) ->where('assign_material_to_activity.activity_id', $id_activity)
@ -137,21 +144,26 @@ class AssignMaterialController extends Controller
->rawColumns(['action'])->make(true); ->rawColumns(['action'])->make(true);
} }
public function datatablesForReportActivity(Request $request){ public function datatablesForReportActivity(Request $request)
{
$id_activity = $request->query('idact'); $id_activity = $request->query('idact');
$data = $query = AssignMaterial::select(
AssignMaterial::select(
DB::raw('(SELECT SUM(assign_material_to_activity.qty_planning) FROM assign_material_to_activity WHERE assign_material_to_activity.activity_id = ram.activity_id) as qty_planning'), DB::raw('(SELECT SUM(assign_material_to_activity.qty_planning) FROM assign_material_to_activity WHERE assign_material_to_activity.activity_id = ram.activity_id) as qty_planning'),
DB::raw('SUM(ram.qty) as qty_sum'), DB::raw('SUM(ram.qty) as qty_sum'),
"m.description as material_name", "m.description as material_name",
"assign_material_to_activity.activity_id", "assign_material_to_activity.activity_id"
) )
->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");
->join("report_activity_material as ram", "ram.assign_material_id", "=", "assign_material_to_activity.id") if (!AssignMaterial::join("report_activity_material as ram", "ram.assign_material_id", "=", "assign_material_to_activity.id")->where("assign_material_to_activity.activity_id", $id_activity)->exists()) {
->groupBy("m.description") $query->leftJoin("report_activity_material as ram", "ram.assign_material_id", "=", "assign_material_to_activity.id");
} else {
$query->join("report_activity_material as ram", "ram.assign_material_id", "=", "assign_material_to_activity.id");
}
$data = $query->groupBy("m.description")
->groupBy("ram.activity_id") ->groupBy("ram.activity_id")
->groupBy("assign_material_to_activity.activity_id") ->groupBy("assign_material_to_activity.activity_id")
->where("assign_material_to_activity.activity_id", $id_activity)->get(); ->where("assign_material_to_activity.activity_id", $id_activity)
->get();
return Datatables::of($data) return Datatables::of($data)
->addIndexColumn() ->addIndexColumn()
// ->addColumn('qty_planning', function($row){ // ->addColumn('qty_planning', function($row){
@ -205,7 +217,8 @@ class AssignMaterialController extends Controller
->rawColumns(['action'])->make(true); ->rawColumns(['action'])->make(true);
} }
public function ForReportActivityByMaterial(Request $request){ public function ForReportActivityByMaterial(Request $request)
{
$id_activity = $request->idact; $id_activity = $request->idact;
$data = AssignMaterial::select("assign_material_to_activity.*", "m.description as material_name", "m.uom as uom") $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") ->join("m_req_material as m", "m.id", "=", "assign_material_to_activity.material_id")

120
app/Http/Controllers/ImageController.php

@ -2,6 +2,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Models\Company;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Models\Image; use App\Models\Image;
@ -19,6 +20,7 @@ class ImageController extends Controller
$ref_id = $request->ref_id; $ref_id = $request->ref_id;
$dokumen = $request->file('files'); $dokumen = $request->file('files');
$extension = $dokumen->extension(); $extension = $dokumen->extension();
$category = $request->category;
$name = $ref_id.date("YmdHis").".".$extension; $name = $ref_id.date("YmdHis").".".$extension;
$resultMove = $dokumen->move($this->pathImage, $name); $resultMove = $dokumen->move($this->pathImage, $name);
@ -27,11 +29,37 @@ class ImageController extends Controller
$data = [ $data = [
'ref_id' => (int)$ref_id, 'ref_id' => (int)$ref_id,
'image' => $name, 'image' => $name,
'category' => $request->category 'category' => $category
]; ];
$result = Image::create($data); $result = Image::create($data);
$company = Company::find((int)$ref_id);
if($company && $category === 'company_logo_login') {
$dataArray = json_decode($company['logo_login'], true);
if(isset($dataArray['slider'])) {
$jsonImageSlider['login'] = $name;
$jsonImageSlider['slider'] = $dataArray['slider'];
} else {
$jsonImageSlider['login'] = $name;
}
$company->update([
'logo_login' => $jsonImageSlider
]);
}
if($company && $category === 'company_logo_header') {
$company->update([
'logo_header' => array('content'=>$name)
]);
}
if($company && $category === 'company_favicon') {
$company->update([
'favicon_image' => array('content'=>$name)
]);
}
if($result){ if($result){
return response()->json(['status'=>'success', "name_image"=>$name,'message'=>'image upload is successfully!','code'=>200], 200); return response()->json(['status'=>'success', "name_image"=>$name,'message'=>'image upload is successfully!','code'=>200], 200);
}else{ }else{
@ -47,6 +75,67 @@ class ImageController extends Controller
} }
} }
public function uploadMultipleImage(Request $request) {
$this->validate($request, [
'ref_id' => 'required',
'category' => 'required',
'files.*' => 'required|file|mimes:jpeg,png,jpg,gif|max:2048',
]);
if ($request->hasFile('files')) {
$ref_id = $request->ref_id;
$category = $request->category;
$dokumen = $request->file('files');
$successCount = 0;
$imageSlider = [];
foreach ($dokumen as $file) {
$extension = $file->extension();
$name = $ref_id.date("YmdHis").$successCount.".".$extension;
$resultMove = $file->move($this->pathImage, $name);
if ($resultMove) {
$data = [
'ref_id' => (int) $ref_id,
'image' => $name,
'category' => $category
];
$imageSlider[] = [
'content' => $name
];
$result = Image::create($data);
if ($result) {
$successCount++;
} else {
unlink($this->pathImage . $name);
}
}
}
$company = Company::find((int)$ref_id);
if($company && $category === 'company_slider_login') {
$dataArray = json_decode($company['logo_login'], true);
if(isset($dataArray['login'])) {
$jsonImageSlider['login'] = $dataArray['login'];
$jsonImageSlider['slider'] = $imageSlider;
} else {
$jsonImageSlider['slider'] = $imageSlider;
}
$company->update([
'logo_login' => $jsonImageSlider
]);
}
if ($successCount > 0 && $company) {
return response()->json(['status' => 'success', 'message' => "$successCount images uploaded successfully!", 'code' => 200], 200);
} else {
return response()->json(['status' => 'failed', 'message' => 'No images uploaded successfully!', 'code' => 400], 400);
}
} else {
return response()->json(['status' => 'failed', 'message' => 'Image is required!', 'code' => 400], 400);
}
}
public function delete($id) public function delete($id)
{ {
$data = Image::find($id); $data = Image::find($id);
@ -80,7 +169,7 @@ class ImageController extends Controller
public function deleteByRef($id, $category) public function deleteByRef($id, $category)
{ {
$data = Image::where("ref_id", $id)->where("category", $category)->first(); $data = Image::where("ref_id", intval($id))->where("category", $category)->first();
if($data){ if($data){
unlink($this->pathImage.$data->image); unlink($this->pathImage.$data->image);
@ -98,6 +187,33 @@ class ImageController extends Controller
} }
} }
public function deleteByRefMultiple($id, $category)
{
$successCount = 0;
$data = Image::where([
["ref_id", intval($id)],
["category", $category]
])->get();
if($data->isNotEmpty()) {
foreach($data as $img) {
unlink($this->pathImage.$img->image);
$delete = $img->delete();
if($delete){
$successCount++;
}
}
} else {
return response()->json(['status'=>'failed','message'=>'data image not found!','code'=>400], 400);
die();
}
if($successCount > 0) {
return response()->json(['status'=>'success','message'=>'data image successfully deleted!','code'=>200], 200);
} else {
return response()->json(['status'=>'failed','message'=>'data image failed deleted!','code'=>400], 400);
}
}
public function getByRefId($id, $category) public function getByRefId($id, $category)
{ {
if(!$id || (int) $id < 0 || $id==""){ if(!$id || (int) $id < 0 || $id==""){

2
routes/web.php

@ -314,7 +314,9 @@ $router->group(['prefix' => 'api', 'middleware' => 'cors'], function () use ($ro
$router->post('/image/search', 'ImageController@search'); $router->post('/image/search', 'ImageController@search');
$router->delete('/image/delete/{id}', 'ImageController@delete'); $router->delete('/image/delete/{id}', 'ImageController@delete');
$router->delete('/image/delete/{id}/{category}', 'ImageController@deleteByRef'); $router->delete('/image/delete/{id}/{category}', 'ImageController@deleteByRef');
$router->delete('/image/multiple-delete/{id}/{category}', 'ImageController@deleteByRefMultiple');
$router->post('/image/upload', 'ImageController@uploadImage'); $router->post('/image/upload', 'ImageController@uploadImage');
$router->post('/image/multiple-upload', 'ImageController@uploadMultipleImage');
$router->get('/image/{id}/{category}', 'ImageController@getByRefId'); $router->get('/image/{id}/{category}', 'ImageController@getByRefId');
$router->post('/panic-button/add', 'PanicButtonController@add'); $router->post('/panic-button/add', 'PanicButtonController@add');

Loading…
Cancel
Save