Browse Source

feat: adding transaction in company management

pull/1/head
wahyun 6 months ago
parent
commit
1fffcd4fd5
  1. 61
      app/Http/Controllers/CompanyController.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)
{ {

Loading…
Cancel
Save