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.
131 lines
4.7 KiB
131 lines
4.7 KiB
<?php |
|
|
|
namespace App\Http\Controllers; |
|
|
|
use App\Models\{User, RoleMenu, Role, ProductTransaction, MenuCompany, Company}; |
|
use Illuminate\Http\Request; |
|
use Illuminate\Support\Facades\DB; |
|
|
|
class CompanyController extends Controller |
|
{ |
|
public function add(Request $request) |
|
{ |
|
$this->validate($request, [ |
|
'company_name' => 'required', |
|
'registration_no' => 'required', |
|
'template_id' => 'required', |
|
'is_active' => 'required' |
|
]); |
|
|
|
$data = $request->all(); |
|
|
|
$data['created_by'] = $this->currentName; |
|
if(isset($data['base_url']) && $data['base_url'] != NULL) { |
|
$data['base_url'] = json_encode($data['base_url'], true); |
|
} |
|
$result = Company::create($data); |
|
|
|
if ($result) { |
|
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); |
|
|
|
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()); |
|
} else { |
|
return response()->json(['status' => 'failed', 'message' => 'data Company not found!', 'code' => 400], 400); |
|
die(); |
|
} |
|
if ($result) { |
|
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((int)$data['id']); |
|
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($company_id) |
|
{ |
|
MenuCompany::where('company_id', $company_id)->delete(); |
|
User::where('company_id', $company_id)->delete(); |
|
$role = Role::where('company_id', $company_id)->first(); |
|
if ($role) { |
|
RoleMenu::where('role_id', $role->id)->delete(); |
|
$role->delete(); |
|
} |
|
ProductTransaction::where('company_id', $company_id)->delete(); |
|
} |
|
|
|
|
|
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); |
|
} |
|
} |
|
}
|
|
|