wahyun
10 months ago
8 changed files with 432 additions and 234 deletions
@ -1,105 +1,114 @@ |
|||||||
<?php |
<?php |
||||||
|
|
||||||
namespace App\Http\Controllers; |
namespace App\Http\Controllers; |
||||||
|
|
||||||
use Illuminate\Http\Request; |
use App\Models\Company; |
||||||
use App\Models\DokumenProject; |
use Illuminate\Http\Request; |
||||||
|
use App\Models\DokumenProject; |
||||||
class ProjectDokumenController extends Controller |
use App\Models\ProductTransaction; |
||||||
{ |
|
||||||
public function dokumenByProyekId($id){ |
class ProjectDokumenController extends Controller |
||||||
if(empty($id) || !is_int((int)$id)) { |
{ |
||||||
return response()->json(['status'=>'failed','message'=>'project id is required!','code'=>400], 400); |
public function dokumenByProyekId($id){ |
||||||
} |
if(empty($id) || !is_int((int)$id)) { |
||||||
|
return response()->json(['status'=>'failed','message'=>'project id is required!','code'=>400], 400); |
||||||
$document = DokumenProject::where("ref_id", $id)->get(); |
} |
||||||
|
|
||||||
if(count($document) == 0) |
$document = DokumenProject::where("ref_id", $id)->get(); |
||||||
return response()->json(['status'=>'','message'=>'Data not found!' ,'code'=>404], 404); |
|
||||||
|
if(count($document) == 0) |
||||||
return response()->json(['status'=>'success','data'=> $document ,'code'=>200], 200); |
return response()->json(['status'=>'','message'=>'Data not found!' ,'code'=>404], 404); |
||||||
} |
|
||||||
|
return response()->json(['status'=>'success','data'=> $document ,'code'=>200], 200); |
||||||
public function delete($id) |
} |
||||||
{ |
|
||||||
if(empty($id) || !is_int((int)$id)) { |
public function delete($id, $company_id) |
||||||
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); |
{ |
||||||
} |
if(empty($id) || !is_int((int)$id)) { |
||||||
|
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); |
||||||
$document = DokumenProject::find($id); |
} |
||||||
if(!$document){ |
|
||||||
return response()->json(['status'=>'failed','message'=>'Data not found!','code'=>400], 400); |
$document = DokumenProject::find($id); |
||||||
} |
if(!$document){ |
||||||
|
return response()->json(['status'=>'failed','message'=>'Data not found!','code'=>400], 400); |
||||||
if(file_exists($this->pathDocument.$document->file)){ |
} |
||||||
unlink($this->pathDocument.$document->file); |
$company = Company::find($company_id); |
||||||
} |
if($company) { |
||||||
|
$destinationPath = $this->setCustomeDirectoryUpload($company['company_name']); |
||||||
$document->delete(); |
if(file_exists($destinationPath['pathDocument'].$document->file)){ |
||||||
|
unlink($destinationPath['pathDocument'].$document->file); |
||||||
return response()->json(['status'=>'success','message'=>'Data deleted!','code'=>200], 200); |
} |
||||||
} |
} |
||||||
|
$document->delete(); |
||||||
public function uploadProjectDokumen(Request $request) |
return response()->json(['status'=>'success','message'=>'Data deleted!','code'=>200], 200); |
||||||
{ |
} |
||||||
if($request->hasFile('dokumen')){ |
|
||||||
$document = $request->file('dokumen'); |
public function uploadProjectDokumen(Request $request) |
||||||
$ref_id = $request->ref_id; |
{ |
||||||
$name = $document->getClientOriginalName(); |
if($request->hasFile('dokumen')){ |
||||||
|
$document = $request->file('dokumen'); |
||||||
$result = $document->move($this->pathDocument, $name); |
$ref_id = $request->ref_id; |
||||||
if($result){ |
$name = $document->getClientOriginalName(); |
||||||
$data = [ |
// Limited Storage |
||||||
'ref_id' => (int)$ref_id, |
$company = Company::where('company_name', $request->company_name)->first(); |
||||||
'file' => $name, |
if($company) { |
||||||
'type_dokumen' => isset($request->type_dokumen) ? $request->type_dokumen : 'project-document' |
$destinationPath = $this->setCustomeDirectoryUpload($company['company_name']); |
||||||
]; |
$getLimitStorage = $this->setLimitsStorage($company, $document, $name, $destinationPath['pathDocument'],$destinationPath); |
||||||
|
} |
||||||
$result = DokumenProject::create($data); |
if(isset($getLimitStorage) && $getLimitStorage['resultMove']) { |
||||||
|
$data = [ |
||||||
if(!$result){ |
'ref_id' => (int)$ref_id, |
||||||
unlink($this->pathDocument.$name); |
'file' => $name, |
||||||
return response()->json(['status'=>'failed','message'=>'Upload failed!','code'=> 500], 500); |
'type_dokumen' => isset($request->type_dokumen) ? $request->type_dokumen : 'project-document' |
||||||
} |
]; |
||||||
return response()->json(['status'=>'success','message'=>'Upload successful!','code'=>200], 200); |
|
||||||
|
$getLimitStorage['resultMove'] = DokumenProject::create($data); |
||||||
} |
|
||||||
return response()->json(['status'=>'failed','message'=>'Upload failed!','code'=> 500], 500); |
if(!$getLimitStorage['resultMove']){ |
||||||
} |
unlink($destinationPath['pathDocument'].$name); |
||||||
return response()->json(['status'=>'failed','message'=>'File is required!','code'=>400], 400); |
return response()->json(['status'=>'failed','message'=>'Dokumen project gagal diupload!','code'=> 500], 500); |
||||||
} |
} |
||||||
|
return response()->json(['status'=>'success','message'=>'Dokumen project berhasil diupload!','code'=>200], 200); |
||||||
public function searchDocProject(Request $request){ |
} |
||||||
$payload = $request->all(); |
return response()->json(['status'=>'failed','message'=>'Dokumen project gagal diupload!','code'=> 500], 500); |
||||||
|
} |
||||||
$dataBuilder = $this->setUpPayload($payload, 'document_project'); |
return response()->json(['status'=>'failed','message'=>'File is required!','code'=>400], 400); |
||||||
$builder = $dataBuilder['builder']; |
} |
||||||
$countBuilder = $dataBuilder['count']; |
|
||||||
$dataGet = $builder->get(); |
public function searchDocProject(Request $request){ |
||||||
$totalRecord = $countBuilder->count(); |
$payload = $request->all(); |
||||||
|
|
||||||
return response()->json(['status'=>'success','code'=>200,'data'=>$dataGet, 'totalRecord'=>$totalRecord], 200); |
$dataBuilder = $this->setUpPayload($payload, 'document_project'); |
||||||
} |
$builder = $dataBuilder['builder']; |
||||||
|
$countBuilder = $dataBuilder['count']; |
||||||
public function downloadDokumen($id) |
$dataGet = $builder->get(); |
||||||
{ |
$totalRecord = $countBuilder->count(); |
||||||
if(empty($id) || !is_int((int)$id)) |
|
||||||
return response()->json(['status'=>'failed','message'=>'Id is required!','code'=>400], 400); |
return response()->json(['status'=>'success','code'=>200,'data'=>$dataGet, 'totalRecord'=>$totalRecord], 200); |
||||||
|
} |
||||||
$document = DokumenProject::find($id); |
|
||||||
|
public function downloadDokumen($id, $company_id) |
||||||
if(!$document) |
{ |
||||||
return response()->json(['status'=>'failed','message'=>'Data not found!','code'=> 404], 404); |
if(empty($id) || !is_int((int)$id)) { |
||||||
|
return response()->json(['status'=>'failed','message'=>'Id is required!','code'=>400], 400); |
||||||
if(!file_exists($this->pathDocument.$document->file)) |
} |
||||||
return response()->json(['status'=>'failed','message'=>'Data not found!','code'=> 404], 404); |
$document = DokumenProject::find($id); |
||||||
|
$company = Company::find($company_id); |
||||||
$pathToFile = $this->pathDocument.$document->file; |
if(!$document || !$company) { |
||||||
$name = pathinfo($pathToFile, PATHINFO_FILENAME) . "." . pathinfo($pathToFile, PATHINFO_EXTENSION); |
return response()->json(['status'=>'failed','message'=>'Data not found!','code'=> 404], 404); |
||||||
$headers = [ |
} |
||||||
'Content-Disposition' => 'attachment; filename="'.$name.'"' |
if(!file_exists($this->pathDocument.$document->file)) { |
||||||
]; |
return response()->json(['status'=>'failed','message'=>'Data not found!','code'=> 404], 404); |
||||||
// dd($name, $headers); |
} |
||||||
return response()->download($pathToFile, $name, $headers); |
if($company) { |
||||||
} |
$destinationPath = $this->setCustomeDirectoryUpload($company['company_name']); |
||||||
} |
$pathToFile = $destinationPath['pathDocument'].$document->file; |
||||||
|
$name = pathinfo($pathToFile, PATHINFO_FILENAME) . "." . pathinfo($pathToFile, PATHINFO_EXTENSION); |
||||||
|
} |
||||||
|
$headers = [ |
||||||
|
'Content-Disposition' => 'attachment; filename="'.$name.'"' |
||||||
|
]; |
||||||
|
|
||||||
|
return response()->download($pathToFile, $name, $headers); |
||||||
|
} |
||||||
|
} |
||||||
|
Loading…
Reference in new issue