Browse Source

update uploadProjectDokumen

pull/1/head
wahyun 9 months ago
parent
commit
5dadd294bf
  1. 47
      app/Http/Controllers/ProjectDokumenController.php

47
app/Http/Controllers/ProjectDokumenController.php

@ -2,10 +2,10 @@
namespace App\Http\Controllers;
use App\Models\Company;
use Illuminate\Http\Request;
use App\Models\DokumenProject;
use App\Models\ProductTransaction;
use App\Models\{DokumenProject,Company};
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
class ProjectDokumenController extends Controller
{
@ -35,8 +35,8 @@ class ProjectDokumenController extends Controller
$company = Company::find($company_id);
if($company) {
$destinationPath = $this->setCustomeDirectoryUpload($company['company_name']);
if(file_exists($destinationPath['pathDocument'].$document->file)){
unlink($destinationPath['pathDocument'].$document->file);
if(file_exists($destinationPath['pathDocument'].$document['file'])){
unlink($destinationPath['pathDocument'].$document['file']);
}
}
$document->delete();
@ -45,33 +45,47 @@ class ProjectDokumenController extends Controller
public function uploadProjectDokumen(Request $request)
{
DB::beginTransaction();
$timeNow = Carbon::now();
if($request->hasFile('dokumen')){
$document = $request->file('dokumen');
$ref_id = $request->ref_id;
$name = $document->getClientOriginalName();
$originalFilename = $document->getClientOriginalName();
$extension = pathinfo($originalFilename, PATHINFO_EXTENSION);
$filename = pathinfo($originalFilename, PATHINFO_FILENAME);
$name = $filename . '_' . $timeNow->format('d-m-y-His') . '.' . $extension;
// Limited Storage
$company = Company::where('company_name', $request->company_name)->first();
$company = Company::whereId($request->company_id)->first();
if($company) {
$destinationPath = $this->setCustomeDirectoryUpload($company['company_name']);
$getLimitStorage = $this->setLimitsStorage($company, $document, $name, $destinationPath['pathDocument'],$destinationPath);
$getLimitStorage = $this->setLimitsStorage($company, $document, $destinationPath['pathDocument'],$destinationPath);
}
if(isset($getLimitStorage)) {
if($getLimitStorage === false) {
DB::rollBack();
return response()->json(['status' => 'failed', 'message' => 'Limited storage maximum!', 'code' => 500], 500);
}
if(isset($getLimitStorage) && $getLimitStorage['resultMove']) {
$data = [
'ref_id' => (int)$ref_id,
'file' => $name,
'type_dokumen' => isset($request->type_dokumen) ? $request->type_dokumen : 'project-document'
];
$getLimitStorage['resultMove'] = DokumenProject::create($data);
$result = DokumenProject::create($data);
if(!$getLimitStorage['resultMove']){
if(!$document->move($destinationPath['pathDocument'], $name) && $result) {
unlink($destinationPath['pathDocument'].$name);
DB::rollBack();
return response()->json(['status'=>'failed','message'=>'Dokumen project gagal diupload!','code'=> 500], 500);
}
DB::commit();
return response()->json(['status'=>'success','message'=>'Dokumen project berhasil diupload!','code'=>200], 200);
}
DB::rollBack();
return response()->json(['status'=>'failed','message'=>'Dokumen project gagal diupload!','code'=> 500], 500);
}
DB::rollBack();
return response()->json(['status'=>'failed','message'=>'File is required!','code'=>400], 400);
}
@ -94,15 +108,18 @@ class ProjectDokumenController extends Controller
}
$document = DokumenProject::find($id);
$company = Company::find($company_id);
if(!$document || !$company) {
return response()->json(['status'=>'failed','message'=>'Data not found!','code'=> 404], 404);
}
if(!file_exists($this->pathDocument.$document->file)) {
return response()->json(['status'=>'failed','message'=>'Data not found!','code'=> 404], 404);
$destinationPath = $this->setCustomeDirectoryUpload($company['company_name']);
$pathToFile = $destinationPath['pathDocument'].$document['file'];
if(!file_exists($pathToFile)) {
return response()->json(['status'=>'failed','message'=>'Directory not found!','code'=> 404], 404);
}
if($company) {
$destinationPath = $this->setCustomeDirectoryUpload($company['company_name']);
$pathToFile = $destinationPath['pathDocument'].$document->file;
$name = pathinfo($pathToFile, PATHINFO_FILENAME) . "." . pathinfo($pathToFile, PATHINFO_EXTENSION);
}
$headers = [

Loading…
Cancel
Save