Browse Source

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

Reviewed-on: ibnu/generic-ospro-backend#77
pull/1/head
farhantock 8 months ago
parent
commit
9d0ef0877e
  1. 61
      app/Http/Controllers/CompanyController.php
  2. 10
      app/Http/Controllers/ImageController.php
  3. 2
      app/Http/Controllers/ProjectController.php
  4. 19
      app/Http/Controllers/RoleMenuController.php

61
app/Http/Controllers/CompanyController.php

@ -5,6 +5,8 @@ namespace App\Http\Controllers;
use App\Models\{User, RoleMenu, Role, ProductTransaction, MenuCompany, Company}; use App\Models\{User, RoleMenu, Role, ProductTransaction, MenuCompany, Company};
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Carbon\Carbon;
class CompanyController extends Controller class CompanyController extends Controller
{ {
@ -14,7 +16,9 @@ class CompanyController extends Controller
'company_name' => 'required', 'company_name' => 'required',
'registration_no' => 'required', 'registration_no' => 'required',
'template_id' => 'required', 'template_id' => 'required',
'is_active' => 'required' 'is_active' => 'required',
'type_paket' => 'required|in:Free,Basic',
'base_url' => 'required'
]); ]);
$data = $request->all(); $data = $request->all();
@ -24,11 +28,12 @@ class CompanyController extends Controller
$data['base_url'] = json_encode($data['base_url'], true); $data['base_url'] = json_encode($data['base_url'], true);
} }
$result = Company::create($data); $result = Company::create($data);
$addTransaction = $this->addTransaction($result['id'],$data['type_paket']);
if ($result) { if ($result && $addTransaction) {
return response()->json(['status' => 'success','data' => $result, 'message' => 'add Company successfully!', 'code' => 200], 200); return response()->json(['status' => 'success','data' => $result, 'message' => 'Add Company successfully!', 'code' => 200], 200);
} else { } else {
return response()->json(['status' => 'failed', 'message' => 'add data Company failed!', 'code' => 400], 400); return response()->json(['status' => 'failed', 'message' => 'Add data Company failed!', 'code' => 400], 400);
} }
} }
@ -62,11 +67,12 @@ class CompanyController extends Controller
} }
if ($data) { if ($data) {
$result = $data->update($request->all()); $result = $data->update($request->all());
$updateTransaction = $this->updateTransaction($id,$request->type_paket);
} else { } else {
return response()->json(['status' => 'failed', 'message' => 'data Company not found!', 'code' => 400], 400); return response()->json(['status' => 'failed', 'message' => 'data Company not found!', 'code' => 400], 400);
die(); die();
} }
if ($result) { if ($result && $updateTransaction) {
return response()->json(['status' => 'success', 'message' => 'data Company successfully updated!', 'code' => 200], 200); return response()->json(['status' => 'success', 'message' => 'data Company successfully updated!', 'code' => 200], 200);
} else { } else {
return response()->json(['status' => 'failed', 'message' => 'data Company failed updated!', 'code' => 400], 400); return response()->json(['status' => 'failed', 'message' => 'data Company failed updated!', 'code' => 400], 400);
@ -78,7 +84,7 @@ class CompanyController extends Controller
DB::beginTransaction(); DB::beginTransaction();
$data = Company::find((int)$id); $data = Company::find((int)$id);
if ($data) { if ($data) {
$this->deleteRelative((int)$data['id']); $this->deleteRelative($data);
if ($data->delete()) { if ($data->delete()) {
DB::commit(); DB::commit();
return response()->json(['status' => 'success', 'message' => 'Data Company successfully deleted!', 'code' => 200], 200); return response()->json(['status' => 'success', 'message' => 'Data Company successfully deleted!', 'code' => 200], 200);
@ -93,18 +99,51 @@ class CompanyController extends Controller
} }
} }
private function deleteRelative($company_id) private function deleteRelative($data)
{ {
MenuCompany::where('company_id', $company_id)->delete(); $directory = 'assets/' . $data['company_name'];
User::where('company_id', $company_id)->delete(); if (is_dir($directory)) {
$role = Role::where('company_id', $company_id)->first(); File::deleteDirectory($directory);
}
MenuCompany::where('company_id', $data['id'])->delete();
User::where('company_id', $data['id'])->delete();
$role = Role::where('company_id', $data['id'])->first();
if ($role) { if ($role) {
RoleMenu::where('role_id', $role->id)->delete(); RoleMenu::where('role_id', $role->id)->delete();
$role->delete(); $role->delete();
} }
ProductTransaction::where('company_id', $company_id)->delete(); ProductTransaction::where('company_id', $data['id'])->delete();
} }
protected function addTransaction($id_company, $type_paket)
{
$currentDate = Carbon::now();
$finalDate = $currentDate->copy()->addDays(30);
$formData = array(
'company_id' => (int)$id_company,
'type_paket' => $type_paket,
'exp_ospro' => $finalDate,
'amount' => $type_paket === 'Free' ? 0 : 250000
);
$result = ProductTransaction::create($formData);
if($result) {
return true;
} else {
return false;
}
}
protected function updateTransaction($id_company, $type_paket)
{
$result = ProductTransaction::where('company_id', (int)$id_company)->first();
$result->update(['type_paket' => $type_paket]);
if($result) {
return true;
} else {
return false;
}
}
public function search(Request $request) public function search(Request $request)
{ {

10
app/Http/Controllers/ImageController.php

@ -75,12 +75,12 @@ class ImageController extends Controller
} else { } else {
DB::rollBack(); DB::rollBack();
unlink($destinationPath['pathImage'].$name); unlink($destinationPath['pathImage'].$name);
return response()->json(['status' => 'failed','message' => 'image upload is failed! pertama' ,'code' => 400], 400); return response()->json(['status' => 'failed','message' => 'image upload is failed!' ,'code' => 400], 400);
} }
} else { } else {
DB::rollBack(); DB::rollBack();
return response()->json(['status'=>'failed','message'=>'image upload is failed! kedua','code'=>400], 400); return response()->json(['status'=>'failed','message'=>'image upload is failed!','code'=>400], 400);
} }
} else { } else {
DB::rollBack(); DB::rollBack();
@ -105,7 +105,10 @@ 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) {
$totalSize = 0; $totalSize = 0;
$sizeFile = $dokumen->getSize(); $sizeFile = 0;
foreach ($dokumen as $file) {
$sizeFile += $file->getSize();
}
$destinationPath = $this->setCustomeDirectoryUpload($company['company_name']); $destinationPath = $this->setCustomeDirectoryUpload($company['company_name']);
$folderPath = $destinationPath['pathImage']; $folderPath = $destinationPath['pathImage'];
@ -114,7 +117,6 @@ class ImageController extends Controller
} }
$files = scandir($folderPath); $files = scandir($folderPath);
$totalSize = 0;
foreach ($files as $file) { foreach ($files as $file) {
if (is_file($folderPath . '/' . $file)) { if (is_file($folderPath . '/' . $file)) {

2
app/Http/Controllers/ProjectController.php

@ -120,7 +120,7 @@ class ProjectController extends Controller
'md.name as divisi_name', 'md.name as divisi_name',
'm_proyek.company_id' 'm_proyek.company_id'
) )
->join('m_divisi as md', 'md.id', '=', 'm_proyek.divisi_id') ->leftJoin('m_divisi as md', 'md.id', '=', 'm_proyek.divisi_id')
->where('m_proyek.id', $id) ->where('m_proyek.id', $id)
->first(); ->first();
if (!$result) { if (!$result) {

19
app/Http/Controllers/RoleMenuController.php

@ -5,25 +5,28 @@ namespace App\Http\Controllers;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Models\Menu; use App\Models\Menu;
use App\Models\RoleMenu; use App\Models\RoleMenu;
use Illuminate\Support\Facades\Log;
class RoleMenuController extends Controller class RoleMenuController extends Controller
{ {
public function add(Request $request) public function add(Request $request)
{ {
$this->validate($request, [ $this->validate($request, [
'role_id' => 'required', 'role_id.*' => 'required',
'menu_id' => 'required', 'menu_id.*' => 'required'
]); ]);
$data = $request->all(); $data = $request->all();
$data['created_by'] = $this->currentName; $countData = 0;
foreach($data as $arrData) {
$result = RoleMenu::create($data); $arrData['created_by'] = $this->currentName;
$result = RoleMenu::create($arrData);
if($result){ $countData++;
}
if($result && $countData > 0) {
return response()->json(['status'=>'success','message'=>'add data role menu successfully!','code'=>200], 200); return response()->json(['status'=>'success','message'=>'add data role menu successfully!','code'=>200], 200);
}else{ } else {
return response()->json(['status'=>'failed','message'=>'add data role menu failed!','code'=>400], 400); return response()->json(['status'=>'failed','message'=>'add data role menu failed!','code'=>400], 400);
} }
} }

Loading…
Cancel
Save