Custom Backend OSPRO Surveyor Indonesia
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
6.0 KiB

<?php
namespace App\Http\Controllers;
use App\Models\{User, RoleMenu, Role, ProductTransaction, MenuCompany, Company};
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Carbon\Carbon;
class CompanyController extends Controller
{
public function add(Request $request)
{
$this->validate($request, [
1 year ago
'company_name' => 'required',
'registration_no' => 'required',
'template_id' => 'required',
'is_active' => 'required',
'type_paket' => 'required|in:Free,Basic,Enterprise',
'base_url' => 'required'
]);
$data = $request->all();
$data['created_by'] = $this->currentName;
1 year ago
if(isset($data['base_url']) && $data['base_url'] != NULL) {
$data['base_url'] = json_encode($data['base_url'], true);
}
$result = Company::create($data);
$addTransaction = $this->addTransaction($result['id'],$data['type_paket']);
if ($result && $addTransaction) {
return response()->json(['status' => 'success','data' => $result, 'message' => 'Add Company successfully!', 'code' => 200], 200);
} else {
return response()->json(['status' => 'failed', 'message' => 'Add data Company failed!', 'code' => 400], 400);
}
}
public function edit($id)
{
if (!$id || (int) $id < 0 || $id == "") {
return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400);
die();
}
$result = Company::find($id);
if ($result) {
return response()->json(['status' => 'success', 'code' => 200, 'data' => $result], 200);
} else {
return response()->json(['status' => 'failed', 'message' => 'failed get data Company, please try again later!', 'code' => 400], 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);
}
$data = Company::find($id);
1 year ago
if(isset($data['base_url']) && $data['base_url'] != NULL) {
$data->update(['base_url'=>null]);
$data['base_url'] = json_encode($data['base_url'], true);
}
if ($data) {
$result = $data->update($request->all());
$updateTransaction = $this->updateTransaction($id,$request->type_paket);
} else {
return response()->json(['status' => 'failed', 'message' => 'data Company not found!', 'code' => 400], 400);
die();
}
if ($result && $updateTransaction) {
return response()->json(['status' => 'success', 'message' => 'data Company successfully updated!', 'code' => 200], 200);
} else {
return response()->json(['status' => 'failed', 'message' => 'data Company failed updated!', 'code' => 400], 400);
}
}
public function delete($id)
{
DB::beginTransaction();
$data = Company::find((int)$id);
if ($data) {
$this->deleteRelative($data);
if ($data->delete()) {
DB::commit();
return response()->json(['status' => 'success', 'message' => 'Data Company successfully deleted!', 'code' => 200], 200);
} else {
DB::rollBack();
return response()->json(['status' => 'failed', 'message' => 'Data Company failed deleted!', 'code' => 400], 400);
}
} else {
DB::rollBack();
return response()->json(['status' => 'failed', 'message' => 'Data Company not found!', 'code' => 400], 400);
die();
}
}
private function deleteRelative($data)
{
$directory = 'assets/' . $data['company_name'];
if (is_dir($directory)) {
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) {
RoleMenu::where('role_id', $role->id)->delete();
$role->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)
{
$payload = $request->all();
$dataBuilder = $this->setUpPayload($payload, 'm_company');
$builder = $dataBuilder['builder'];
$countBuilder = $dataBuilder['count'];
$dataGet = $builder->get();
$totalRecord = $countBuilder->count();
return response()->json(['status' => 'success', 'code' => 200, 'data' => $dataGet, 'totalRecord' => $totalRecord], 200);
}
public function list()
{
$data = Company::all();
$countData = $data->count();
if ($data) {
return response()->json(['status' => 'success', 'code' => 200, 'data' => $data, 'totalRecord' => $countData], 200);
} else {
return response()->json(['status' => 'failed', 'message' => 'failed get list Company, please try again later!', 'code' => 400], 400);
}
}
}