Browse Source

Merge pull request 'dev-wahyun' (#69) from dev-wahyun into staging

Reviewed-on: ibnu/generic-ospro-backend#69
pull/1/head
ibnu 7 months ago
parent
commit
e1abaf1b6c
  1. 29
      app/Http/Controllers/ActivityController.php
  2. 5
      app/Http/Controllers/ActivityDokumenController.php
  3. 133
      app/Http/Controllers/Controller.php
  4. 274
      app/Http/Controllers/FolderDocumentProyekController.php
  5. 37
      app/Http/Controllers/ImageController.php
  6. 7
      app/Http/Controllers/ProjectController.php
  7. 49
      app/Http/Controllers/ProjectDokumenController.php
  8. 4
      app/Models/Project.php
  9. 2
      routes/web.php

29
app/Http/Controllers/ActivityController.php

@ -21,6 +21,7 @@ use App\Models\ProductTransaction;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use App\Helpers\MasterFunctionsHelper; use App\Helpers\MasterFunctionsHelper;
use Carbon\Carbon;
class ActivityController extends Controller class ActivityController extends Controller
{ {
@ -1126,33 +1127,49 @@ class ActivityController extends Controller
public function uploadTmpImport(Request $request) public function uploadTmpImport(Request $request)
{ {
DB::beginTransaction();
if ($request->hasFile('dokumen')) { if ($request->hasFile('dokumen')) {
$document = $request->file('dokumen'); $document = $request->file('dokumen');
$gantt_id = $request->gantt_id; $gantt_id = $request->gantt_id;
$name = $document->getClientOriginalName(); $timeNow = Carbon::now();
$originalFilename = $document->getClientOriginalName();
$extension = pathinfo($originalFilename, PATHINFO_EXTENSION);
$filename = pathinfo($originalFilename, PATHINFO_FILENAME);
$name = $filename . '_' . $timeNow->format('d-m-y-His') . '.' . $extension;
// Limited Storage // Limited Storage
$company = Company::where('company_name', $request->company_name)->first(); $company = Company::where('company_name', $request->company_name)->first();
if($company) { if($company) {
$destinationPath = $this->setCustomeDirectoryUpload($company['company_name']); $destinationPath = $this->setCustomeDirectoryUpload($company['company_name']);
$getLimitStorage = $this->setLimitsStorage($company, $document, $name, $destinationPath['pathTmpImport'], $destinationPath); $getLimitStorage = $this->setLimitsStorage($company, $document, $destinationPath['pathTmpImport'], $destinationPath);
} }
if (isset($getLimitStorage) && $getLimitStorage['resultMove']) { if (isset($getLimitStorage)) {
if($getLimitStorage === false) {
DB::rollBack();
return response()->json(['status' => 'failed', 'message' => 'Limited storage maximum!', 'code' => 500], 500);
}
$data = [ $data = [
'gantt_id' => (int) $gantt_id, 'gantt_id' => (int) $gantt_id,
'file' => $name, 'file' => $name,
'type_dokumen' => $request->type_dokumen 'type_dokumen' => $request->type_dokumen
]; ];
$getLimitStorage['resultMove'] = TmpImport::create($data); $result = TmpImport::create($data);
if (!$getLimitStorage['resultMove']) { if (!$document->move($destinationPath['pathTmpImport'], $name) && $result) {
unlink($this->pathTmpImport . $name); unlink($destinationPath['pathTmpImport'].$name);
DB::rollBack();
return response()->json(['status' => 'failed', 'message' => 'Upload failed!', 'code' => 500], 500); return response()->json(['status' => 'failed', 'message' => 'Upload failed!', 'code' => 500], 500);
} }
DB::commit();
return response()->json(['status' => 'success', 'message' => 'Upload successful!', 'code' => 200], 200); return response()->json(['status' => 'success', 'message' => 'Upload successful!', 'code' => 200], 200);
} }
DB::rollBack();
return response()->json(['status' => 'failed', 'message' => 'Upload failed!', 'code' => 500], 500); return response()->json(['status' => 'failed', 'message' => 'Upload failed!', 'code' => 500], 500);
} }
DB::rollBack();
return response()->json(['status' => 'failed', 'message' => 'File is required!', 'code' => 400], 400); return response()->json(['status' => 'failed', 'message' => 'File is required!', 'code' => 400], 400);
} }

5
app/Http/Controllers/ActivityDokumenController.php

@ -51,15 +51,16 @@ class ActivityDokumenController extends Controller
$destinationPath = $this->setCustomeDirectoryUpload($company['company_name']); $destinationPath = $this->setCustomeDirectoryUpload($company['company_name']);
$result = $document->move($destinationPath['pathActivityDocument'], $name); $result = $document->move($destinationPath['pathActivityDocument'], $name);
} }
if(!$result || !$company) if(!$result || !$company) {
return response()->json(['status'=>'failed','message'=>'Upload failed!','code'=> 500], 500); return response()->json(['status'=>'failed','message'=>'Upload failed!','code'=> 500], 500);
}
$data = [ $data = [
'activity_id' => (int)$activity_id, 'activity_id' => (int)$activity_id,
'file' => $name, 'file' => $name,
'description' => $request->description 'description' => $request->description
]; ];
$result = ActivityDokumen::create($data); $result = ActivityDokumen::create($data);
if(!$result || !$company){ if(!$result || !$company) {
unlink($destinationPath['pathActivityDocument'].$name); unlink($destinationPath['pathActivityDocument'].$name);
return response()->json(['status'=>'failed','message'=>'Upload failed!','code'=> 500], 500); return response()->json(['status'=>'failed','message'=>'Upload failed!','code'=> 500], 500);
} }

133
app/Http/Controllers/Controller.php

@ -46,10 +46,14 @@ class Controller extends BaseController
protected function setCustomeDirectoryUpload($company_name) protected function setCustomeDirectoryUpload($company_name)
{ {
$pathImage = 'assets/' . $company_name . '/image/'; $current_date = date('Y-m-d');
$pathDocument = 'assets/' . $company_name . '/file/project/'; [$year, $month] = explode('-', $current_date);
$pathTmpImport = 'assets/' . $company_name . '/file/tmpimport/'; $yearMonth = $year.'-'.$month;
$pathActivityDocument = 'assets/' . $company_name . '/file/activity/';
$pathImage = 'assets/' . $company_name .'/'. $yearMonth . '/image/';
$pathDocument = 'assets/' . $company_name .'/'. $yearMonth . '/file/project/';
$pathTmpImport = 'assets/' . $company_name .'/'. $yearMonth . '/file/tmpimport/';
$pathActivityDocument = 'assets/' . $company_name .'/'. $yearMonth . '/file/activity/';
return [ return [
'pathImage' => $pathImage, 'pathImage' => $pathImage,
@ -118,75 +122,76 @@ class Controller extends BaseController
} }
} }
protected function setLimitsStorage($company, $dokumen, $name, $initPath, $destinatePath) protected function setLimitsStorage($company, $dokumen, $initPath, $destinatePath)
{ {
$totalSize = 0; $countCreate = false;
$sizeFile = $dokumen->getSize(); DB::transaction(function() use($company, $dokumen, $initPath, $destinatePath, &$countCreate) {
// Path $totalSize = 0;
$folderPath = $destinatePath['pathDocument']; $sizeFile = $dokumen->getSize();
$folderPathImage = $destinatePath['pathImage']; // Path
$folderPathTmpImport = $destinatePath['pathTmpImport']; $folderPath = $destinatePath['pathDocument'];
$folderPathActivityDocument = $destinatePath['pathActivityDocument']; $folderPathImage = $destinatePath['pathImage'];
// Create Directory $folderPathTmpImport = $destinatePath['pathTmpImport'];
if (!file_exists($initPath)) { $folderPathActivityDocument = $destinatePath['pathActivityDocument'];
mkdir($initPath, 0777, true); // Create Directory
} if (!file_exists($initPath)) {
// Scanning & Existing Folder mkdir($initPath, 0777, true);
if (file_exists($folderPathImage)) { }
$files = scandir($folderPathImage); // Scanning & Existing Folder
foreach ($files as $file) { if (file_exists($folderPathImage)) {
if (is_file($folderPathImage . '/' . $file)) { $files = scandir($folderPathImage);
$totalSize += filesize($folderPathImage . '/' . $file); foreach ($files as $file) {
if (is_file($folderPathImage . '/' . $file)) {
$totalSize += filesize($folderPathImage . '/' . $file);
}
} }
} }
} if (file_exists($folderPath)) {
if (file_exists($folderPath)) { $filesImage = scandir($folderPath);
$filesImage = scandir($folderPath); foreach ($filesImage as $file) {
foreach ($filesImage as $file) { if (is_file($folderPath . '/' . $file)) {
if (is_file($folderPath . '/' . $file)) { $totalSize += filesize($folderPath . '/' . $file);
$totalSize += filesize($folderPath . '/' . $file); }
} }
} }
} if (file_exists($folderPathTmpImport)) {
if (file_exists($folderPathTmpImport)) { $filesTmpImport = scandir($folderPathTmpImport);
$filesTmpImport = scandir($folderPathTmpImport); foreach ($filesTmpImport as $file) {
foreach ($filesTmpImport as $file) { if (is_file($folderPathTmpImport . '/' . $file)) {
if (is_file($folderPathTmpImport . '/' . $file)) { $totalSize += filesize($folderPathTmpImport . '/' . $file);
$totalSize += filesize($folderPathTmpImport . '/' . $file); }
} }
} }
} if (file_exists($folderPathActivityDocument)) {
if (file_exists($folderPathActivityDocument)) { $filesActivityDocument = scandir($folderPathActivityDocument);
$filesActivityDocument = scandir($folderPathActivityDocument); foreach ($filesActivityDocument as $file) {
foreach ($filesActivityDocument as $file) { if (is_file($folderPathActivityDocument . '/' . $file)) {
if (is_file($folderPathActivityDocument . '/' . $file)) { $totalSize += filesize($folderPathActivityDocument . '/' . $file);
$totalSize += filesize($folderPathActivityDocument . '/' . $file); }
} }
} }
} // Logic
// Logic $totalSize += $sizeFile;
$totalSize += $sizeFile; $transaction = ProductTransaction::query()
$transaction = ProductTransaction::query() ->where('company_id', $company['id']);
->where('company_id', $company['id']); $cloneQueryTransaction = clone $transaction;
$cloneQueryTransaction = clone $transaction; if ($transaction->where([['type_paket', 'Basic'], ['amount', '!=', null]])->exists()) {
$countCreate = false; $maximumSize = 500 * 1024 * 1024;
if ($transaction->where([['type_paket', 'Basic'], ['amount', '!=', null]])->exists()) { $countCreate = true;
$maximumSize = 500 * 1024 * 1024; } elseif ($cloneQueryTransaction->where([['type_paket', 'Free'], ['amount', 0]])->exists()) {
$countCreate = true; $maximumSize = 50 * 1024 * 1024;
} elseif ($cloneQueryTransaction->where([['type_paket', 'Free'], ['amount', 0]])->exists()) { $countCreate = true;
$maximumSize = 50 * 1024 * 1024; } else {
$countCreate = true; $countCreate = true;
}
if ($countCreate) {
if ($totalSize > $maximumSize) {
return response()->json(['status' => 'failed', 'message' => 'Limited storage maximum!', 'code' => 500], 500);
} }
}
$resultMove = $dokumen->move($initPath, $name);
return [ if ($countCreate) {
'resultMove' => $resultMove if (floatval($totalSize) > floatval($maximumSize)) {
]; $countCreate = false;
}
}
});
return $countCreate;
} }
protected function setUpPayload($condition, $tableSelf) protected function setUpPayload($condition, $tableSelf)
@ -334,6 +339,10 @@ class Controller extends BaseController
$query = $query->whereIn($tableColumn . "." . $column, $value); $query = $query->whereIn($tableColumn . "." . $column, $value);
} else if ($operator == "notin") { } else if ($operator == "notin") {
$query = $query->whereNotIn($tableColumn . "." . $column, $value); $query = $query->whereNotIn($tableColumn . "." . $column, $value);
} else if ($operator == "isnull") {
$query = $query->WhereNull($tableColumn . "." . $column);
} else if ($operator == "notnull") {
$query = $query->WhereNotNull($tableColumn . "." . $column);
} else { } else {
$query = $query->where($tableColumn . "." . $column, $operator, $value); $query = $query->where($tableColumn . "." . $column, $operator, $value);
} }

274
app/Http/Controllers/FolderDocumentProyekController.php

@ -1,131 +1,143 @@
<?php <?php
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Illuminate\Http\Request; use App\Models\Company;
use App\Models\FolderDocumentProyek; use Illuminate\Http\Request;
use App\Models\DokumenProject; use App\Models\DokumenProject;
use App\Models\FolderDocumentProyek;
class FolderDocumentProyekController extends Controller use Illuminate\Support\Facades\DB;
{ use Illuminate\Support\Facades\Log;
public function add(Request $request){
$this->validate($request, [ class FolderDocumentProyekController extends Controller
'name_folder' => 'required', {
'proyek_id' => 'required' public function add(Request $request){
]); $this->validate($request, [
$data = $request->all(); 'name_folder' => 'required',
$data['created_by'] = $this->currentName; 'proyek_id' => 'required'
]);
$result = FolderDocumentProyek::create($data); $data = $request->all();
if($result){ $data['created_by'] = $this->currentName;
return response()->json(['status'=>'success','message'=>'Folder Document Proyek successfull created','code'=>200]);
}else{ $result = FolderDocumentProyek::create($data);
return response()->json(['status'=>'failed','message'=>'Folder Document Proyek failed created','code'=>400]); if($result){
} return response()->json(['status'=>'success','message'=>'Folder Document Proyek successfull created','code'=>200]);
} }else{
return response()->json(['status'=>'failed','message'=>'Folder Document Proyek failed created','code'=>400]);
public function update(Request $request, $id){ }
}
if(!$id || (int) $id < 0 || $id==""){
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); public function update(Request $request, $id){
}
$data = FolderDocumentProyek::find($id); if(!$id || (int) $id < 0 || $id==""){
if($data){ return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400);
$result = $data->update($request->all()); }
}else{ $data = FolderDocumentProyek::find($id);
return response()->json(['status'=>'failed','message'=>'Data Folder Document Proyek not found!','code'=>400], 400); if($data){
die(); $result = $data->update($request->all());
} }else{
return response()->json(['status'=>'failed','message'=>'Data Folder Document Proyek not found!','code'=>400], 400);
if($result){ die();
return response()->json(['status'=>'success','message'=>'Folder Document Proyek successfully updated!','code'=>200], 200); }
}else{
return response()->json(['status'=>'failed','message'=>'Folder Document Proyek failed updated!','code'=>400], 400); if($result){
} return response()->json(['status'=>'success','message'=>'Folder Document Proyek successfully updated!','code'=>200], 200);
} }else{
return response()->json(['status'=>'failed','message'=>'Folder Document Proyek failed updated!','code'=>400], 400);
public function delete($id) }
{ }
$data = FolderDocumentProyek::find($id);
public function delete($id, $company_id)
if($data){ {
$delete = $data->delete(); DB::beginTransaction();
}else{ $data = FolderDocumentProyek::find($id);
return response()->json(['status'=>'failed','message'=>'data Folder Document Proyek not found!','code'=>400], 400); $company = Company::find($company_id);
die(); if($data && $company) {
} $destinationPath = $this->setCustomeDirectoryUpload($company['company_name']);
$dir = $destinationPath['pathDocument'];
$dataFile = DokumenProject::where([['type_dokumen', 'project-document-in-folder'],['ref_id', $data['id']]])->orderByRaw('id ASC')->get();
if($delete){ if (is_dir($dir)) {
return response()->json(['status'=>'success','message'=>'Folder Document Proyek successfully deleted!','code'=>200], 200); foreach ($dataFile as $file) {
}else{ unlink($dir.$file['file']);
return response()->json(['status'=>'failed','message'=>'Folder Document Proyek failed deleted!','code'=>400], 400); }
} }
} if($data->delete()) {
DB::commit();
public function search(Request $request) return response()->json(['status'=>'success','message'=>'Folder Document Proyek successfully deleted!','code'=>200], 200);
{ } else {
$payload = $request->all(); DB::rollBack();
$dataBuilder = $this->setUpPayload($payload, 'm_FolderDocumentProyek'); return response()->json(['status'=>'failed','message'=>'Folder Document Proyek failed deleted!','code'=>400], 400);
$builder = $dataBuilder['builder']; }
$countBuilder = $dataBuilder['count']; } else {
$dataGet = $builder->get(); DB::rollBack();
$totalRecord = $countBuilder->count(); return response()->json(['status'=>'failed','message'=>'Data Folder Document Proyek not found!','code'=>400], 400);
return response()->json(['status'=>'success','code'=>200,'data'=>$dataGet, 'totalRecord'=>$totalRecord], 200); }
} }
public function list() public function search(Request $request)
{ {
$data = FolderDocumentProyek::all(); $payload = $request->all();
$countData = $data->count(); $dataBuilder = $this->setUpPayload($payload, 'm_FolderDocumentProyek');
$builder = $dataBuilder['builder'];
if($data){ $countBuilder = $dataBuilder['count'];
return response()->json(['status'=>'success','code'=>200,'data'=>$data, 'totalRecord'=>$countData], 200); $dataGet = $builder->get();
}else{ $totalRecord = $countBuilder->count();
return response()->json(['status'=>'failed','message'=>'failed get list tools request, please try again later!','code'=>400], 400); return response()->json(['status'=>'success','code'=>200,'data'=>$dataGet, 'totalRecord'=>$totalRecord], 200);
} }
}
public function getTree($id) public function list()
{ {
$data = FolderDocumentProyek::where('proyek_id', $id)->whereNull('parent_id')->orderByRaw('id ASC')->get(); $data = FolderDocumentProyek::all();
$dataFile = DokumenProject::where('type_dokumen', 'project-document-out-folder')->where('ref_id', $id)->orderByRaw('id ASC')->get(); $countData = $data->count();
$finalData = [];
foreach($data as $objRow){ if($data){
$objRow->children = $this->getChildren($id, $objRow->id); return response()->json(['status'=>'success','code'=>200,'data'=>$data, 'totalRecord'=>$countData], 200);
$objRow->key = rand(1, 1000); }else{
$objRow->isDir = true; return response()->json(['status'=>'failed','message'=>'failed get list tools request, please try again later!','code'=>400], 400);
$finalData[] = $objRow; }
} }
foreach($dataFile as $objRowFile) { public function getTree($id)
$objRowFile->key = rand(1, 1000); {
$objRowFile->isDir = false; $data = FolderDocumentProyek::where('proyek_id', $id)->whereNull('parent_id')->orderByRaw('id ASC')->get();
$objRowFile->proyek_id = $id; $dataFile = DokumenProject::where('type_dokumen', 'project-document-out-folder')->where('ref_id', $id)->orderByRaw('id ASC')->get();
$finalData[] = $objRowFile; $finalData = [];
} foreach($data as $objRow){
$objRow->children = $this->getChildren($id, $objRow->id);
return response()->json(['status'=>'success','data'=>$finalData,'code'=>200], 200); $objRow->key = rand(1, 1000);
} $objRow->isDir = true;
$finalData[] = $objRow;
private function getChildren($proyek_id, $parent_id) }
{
$finalData = []; foreach($dataFile as $objRowFile) {
$data = FolderDocumentProyek::where('proyek_id', $proyek_id)->where('parent_id', $parent_id)->orderByRaw('id ASC')->get(); $objRowFile->key = rand(1, 1000);
$dataFile = DokumenProject::where('type_dokumen', 'project-document-in-folder')->where('ref_id', $parent_id)->orderByRaw('id ASC')->get(); $objRowFile->isDir = false;
foreach($data as $objRow){ $objRowFile->proyek_id = (int)$id;
$objRow->key = rand(1, 1000); $finalData[] = $objRowFile;
$objRow->children = $this->getChildren($proyek_id, $objRow->id); }
$objRow->isDir = true;
$finalData[] = $objRow; return response()->json(['status'=>'success','data'=>$finalData,'code'=>200], 200);
} }
foreach($dataFile as $objRowFile) { private function getChildren($proyek_id, $parent_id)
$objRowFile->key = rand(1, 1000); {
$objRowFile->isDir = false; $finalData = [];
$objRowFile->proyek_id = $proyek_id; $data = FolderDocumentProyek::where('proyek_id', $proyek_id)->where('parent_id', $parent_id)->orderByRaw('id ASC')->get();
$finalData[] = $objRowFile; $dataFile = DokumenProject::where('type_dokumen', 'project-document-in-folder')->where('ref_id', $parent_id)->orderByRaw('id ASC')->get();
} foreach($data as $objRow){
$objRow->key = rand(1, 1000);
return $finalData; $objRow->children = $this->getChildren($proyek_id, $objRow->id);
} $objRow->isDir = true;
} $finalData[] = $objRow;
}
foreach($dataFile as $objRowFile) {
$objRowFile->key = rand(1, 1000);
$objRowFile->isDir = false;
$objRowFile->proyek_id = (int)$proyek_id;
$finalData[] = $objRowFile;
}
return $finalData;
}
}

37
app/Http/Controllers/ImageController.php

@ -6,11 +6,13 @@ use App\Models\Image;
use App\Models\Company; use App\Models\Company;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Models\ProductTransaction; use App\Models\ProductTransaction;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class ImageController extends Controller class ImageController extends Controller
{ {
public function uploadImage(Request $request){ public function uploadImage(Request $request){
DB::beginTransaction();
if($request->hasFile('files')){ if($request->hasFile('files')){
$this->validate($request, [ $this->validate($request, [
@ -27,9 +29,13 @@ class ImageController extends Controller
$company = Company::where('company_name', $request->company_name)->first(); $company = Company::where('company_name', $request->company_name)->first();
if($company) { if($company) {
$destinationPath = $this->setCustomeDirectoryUpload($company['company_name']); $destinationPath = $this->setCustomeDirectoryUpload($company['company_name']);
$getLimitStorage = $this->setLimitsStorage($company, $dokumen, $name, $destinationPath['pathImage'], $destinationPath); $getLimitStorage = $this->setLimitsStorage($company, $dokumen, $destinationPath['pathImage'], $destinationPath);
} }
if(isset($getLimitStorage) && $getLimitStorage['resultMove']) { if(isset($getLimitStorage)) {
if($getLimitStorage === false) {
DB::rollBack();
return response()->json(['status' => 'failed', 'message' => 'Limited storage maximum!', 'code' => 500], 500);
}
$data = [ $data = [
'ref_id' => (int)$ref_id, 'ref_id' => (int)$ref_id,
'image' => $name, 'image' => $name,
@ -54,27 +60,30 @@ class ImageController extends Controller
if($company && $category === 'company_logo_header') { if($company && $category === 'company_logo_header') {
$company->update([ $company->update([
'logo_header' => array('content'=>$name) 'logo_header' => array('content' => $name)
]); ]);
} }
if($company && $category === 'company_favicon') { if($company && $category === 'company_favicon') {
$company->update([ $company->update([
'favicon_image' => array('content'=>$name) 'favicon_image' => array('content' => $name)
]); ]);
} }
if($dokumen->move($destinationPath['pathImage'], $name) && $result) {
if($result){ DB::commit();
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 {
DB::rollBack();
unlink($destinationPath['pathImage'].$name); unlink($destinationPath['pathImage'].$name);
return response()->json(['status'=>'failed','message'=>'image upload is failed!','code'=>400], 400); return response()->json(['status' => 'failed','message' => 'image upload is failed! pertama' ,'code' => 400], 400);
} }
}else{ } else {
return response()->json(['status'=>'failed','message'=>'image upload is failed!','code'=>400], 400); DB::rollBack();
return response()->json(['status'=>'failed','message'=>'image upload is failed! kedua','code'=>400], 400);
} }
}else{ } else {
DB::rollBack();
return response()->json(['status'=>'failed','message'=>'image is required!','code'=>400], 400); return response()->json(['status'=>'failed','message'=>'image is required!','code'=>400], 400);
} }
} }
@ -210,7 +219,7 @@ class ImageController extends Controller
public function deleteByRef($id, $category, $company_id) public function deleteByRef($id, $category, $company_id)
{ {
$data = Image::where("ref_id", intval($id))->where("category", $category)->first(); $data = Image::where("ref_id", (int)$id)->where("category", $category)->first();
$company = Company::find($company_id); $company = Company::find($company_id);
if($data && $company){ if($data && $company){
$destinationPath = $this->setCustomeDirectoryUpload($company['company_name']); $destinationPath = $this->setCustomeDirectoryUpload($company['company_name']);

7
app/Http/Controllers/ProjectController.php

@ -13,6 +13,7 @@ use App\Models\{ReportActivityMaterial,ProjectToChecklistK3,FolderDocumentProyek
use Illuminate\Database\Query\Builder; use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\{Artisan,DB}; use Illuminate\Support\Facades\{Artisan,DB};
use App\Helpers\MasterFunctionsHelper; use App\Helpers\MasterFunctionsHelper;
use Carbon\Carbon;
const API_GEOLOCATION = "https://nominatim.oslogdev.com/search/ADDR?format=json&addressdetails=1&limit=1"; const API_GEOLOCATION = "https://nominatim.oslogdev.com/search/ADDR?format=json&addressdetails=1&limit=1";
@ -178,9 +179,11 @@ class ProjectController extends Controller
{ {
DB::beginTransaction(); DB::beginTransaction();
$data = Project::find($id); $data = Project::find($id);
$dateNow = Carbon::now();
if ($data) { if ($data) {
$this->deleteRelative($id, $company_id); // $this->deleteRelative($id, $company_id);
if ($data->delete()) { // if ($data->delete()) {
if ($data->update(['deleted_at' => $dateNow, 'deleted_by_id' => $this->currentId])) {
DB::commit(); DB::commit();
return response()->json(['status' => 'success', 'message' => 'Data deleted!', 'code' => 200], 200); return response()->json(['status' => 'success', 'message' => 'Data deleted!', 'code' => 200], 200);
} else { } else {

49
app/Http/Controllers/ProjectDokumenController.php

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

4
app/Models/Project.php

@ -52,6 +52,8 @@ class Project extends Model
'updated_at', 'updated_at',
'updated_by', 'updated_by',
'created_by_id', 'created_by_id',
'company_id' 'company_id',
'deleted_at',
'deleted_by_id'
]; ];
} }

2
routes/web.php

@ -494,7 +494,7 @@ $router->group(['prefix' => 'api', 'middleware' => 'cors'], function () use ($ro
$router->get('/folder-document-proyek/edit/{id}', 'FolderDocumentProyekController@edit'); $router->get('/folder-document-proyek/edit/{id}', 'FolderDocumentProyekController@edit');
$router->put('/folder-document-proyek/update/{id}', 'FolderDocumentProyekController@update'); $router->put('/folder-document-proyek/update/{id}', 'FolderDocumentProyekController@update');
$router->post('/folder-document-proyek/search', 'FolderDocumentProyekController@search'); $router->post('/folder-document-proyek/search', 'FolderDocumentProyekController@search');
$router->delete('/folder-document-proyek/delete/{id}', 'FolderDocumentProyekController@delete'); $router->delete('/folder-document-proyek/delete/{id}/{company_id}', 'FolderDocumentProyekController@delete');
$router->get('/folder-document-proyek/list', 'FolderDocumentProyekController@list'); $router->get('/folder-document-proyek/list', 'FolderDocumentProyekController@list');
$router->get('/folder-document-proyek/get-tree/{id}', 'FolderDocumentProyekController@getTree'); $router->get('/folder-document-proyek/get-tree/{id}', 'FolderDocumentProyekController@getTree');

Loading…
Cancel
Save