added form entertaiment and fix file upload

This commit is contained in:
Jagad R R
2024-12-18 12:56:16 +07:00
parent 69ab2b056d
commit 24b9a82856
12 changed files with 836 additions and 62 deletions
+80 -7
View File
@@ -4,6 +4,7 @@ namespace App\Helpers;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Support\Str;
class NextCloudHelper
{
@@ -19,17 +20,78 @@ class NextCloudHelper
public static function createFolder($baseUrl, $username, $password, $folderPath)
{
$client = new Client();
$url = rtrim($baseUrl, '/') . "/remote.php/dav/files/{$username}/" . ltrim($folderPath, '/');
$folders = explode('/', ltrim($folderPath, '/')); // Split the folder path into an array
$currentPath = ''; // Initialize an empty string to build the path progressively
foreach ($folders as $folder) {
$currentPath .= '/' . $folder; // Add the current folder to the path
// Build the URL to create the folder
$url = rtrim($baseUrl, '/') . "/mktia/remote.php/dav/files/{$username}" . $currentPath;
try {
// Attempt to create the current folder
$response = $client->request('MKCOL', $url, [
'auth' => [$username, $password],
'verify' => false // Disable SSL verification (only for testing purposes)
]);
// If folder creation is successful, proceed to the next folder
if ($response->getStatusCode() === 201) {
echo "Folder '$currentPath' created successfully.\n";
}
} catch (RequestException $e) {
// Handle errors, such as folder already existing
if ($e->getCode() === 409) {
echo "Folder '$currentPath' already exists.\n";
} else {
// For other errors
echo "Failed to create folder '$currentPath': " . $e->getMessage() . "\n";
}
}
}
return [
'success' => true,
'status' => 200,
'message' => 'All folders processed.'
];
}
public static function uploadFile($folderPath, $fileObject, $fileName)
{
$baseUrl = env('NEXT_CLOUD_URL');
$username = env('NEXT_CLOUD_USERNAME');
$password= env('NEXT_CLOUD_PASSWORD');
// Ensure the folder exists (or create it)
$folderResponse = self::createFolder($baseUrl, $username, $password, $folderPath);
// Check if folder creation was successful or if it already exists
if (!$folderResponse['success']) {
return [
'success' => false,
'status' => $folderResponse['status'],
'message' => 'Failed to create folder: ' . $folderResponse['message']
];
}
$client = new Client();
$url = rtrim($baseUrl, '/') . "/mktia/remote.php/dav/files/{$username}/" . ltrim($folderPath, '/') . '/' . $fileName;
try {
$response = $client->request('MKCOL', $url, [
'auth' => [$username, $password]
// If fileObject is a stream, we can use it directly
// If it's raw content, ensure it's being properly passed
$response = $client->request('PUT', $url, [
'auth' => [$username, $password],
'body' => $fileObject, // File content or stream
'verify' => false // Disable SSL verification (only for testing purposes)
]);
return [
'success' => true,
'status' => $response->getStatusCode(),
'message' => 'Folder created successfully.'
'message' => 'File uploaded successfully.'
];
} catch (RequestException $e) {
return [
@@ -39,8 +101,19 @@ class NextCloudHelper
];
}
}
public static function getFileUrl($filePath)
{
$baseUrl = env('NEXT_CLOUD_URL');
$username = env('NEXT_CLOUD_USERNAME');
// Build the user-specific WebDAV file URL
$relativePath = "/mktia/remote.php/dav/files/{$username}/" . ltrim($filePath, '/');
// Return the full accessible URL
return rtrim($baseUrl, '/') . '/' . $relativePath;
}
/**
* Create a user in Nextcloud.
@@ -0,0 +1,229 @@
<?php
namespace App\Http\Controllers\Forms;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\FormEntertaimentPresentation;
use App\Models\Rayon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use App\Models\Region;
use App\Models\Cabang;
use App\Models\UserHasCabang;
use App\Models\Kategori;
use App\Helpers\NextCloudHelper;
use Illuminate\Support\Str;
class FormEntertainmentPresentationController extends Controller
{
public function index()
{
$this->checkAuthorization(auth()->user(), ['forms.entertainment.view']);
$forms = FormEntertaimentPresentation::where('user_id', auth()->user()->id)->get();
return view('backend.pages.forms.entertainment.index', [
'forms' => $forms
]);
}
public function create()
{
$this->checkAuthorization(auth()->user(), ['forms.entertainment.create']);
return view('backend.pages.forms.entertainment.create');
}
public function store(Request $request)
{
$this->checkAuthorization(auth()->user(), ['forms.entertainment.create']);
$request->validate([
'tanggal' => 'required',
'jenis' => 'required|in:entertainment,presentation,sponsorship',
'keterangan' => 'required',
'total' => 'required|integer',
'name' => 'required',
'alamat' => 'required',
'nik_or_npwp' => 'required',
'bukti1' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
'bukti2' => 'nullable|file|mimes:xlsx,xls,csv',
'bukti3' => 'nullable|file|mimes:pdf',
]);
$cabang = UserHasCabang::with('cabang')->where('user_id', auth()->user()->id)->first()->cabang;
$region = Region::where('id', $cabang->region_id)->first();
$kategori = Kategori::where('name', 'Entertainment & Presentation')->first();
$filePaths = [];
$folderPath = '/expense/' . $region->code . '/' . $cabang->code . '/entertainment';
if ($request->hasFile('bukti1')) {
$bukti1 = $request->file('bukti1');
$filename = Str::uuid() . '.' . $bukti1->extension();
$filePaths['bukti1'] = $folderPath . '/' . $filename;
NextCloudHelper::uploadFile(
$folderPath,
$bukti1->getContent(),
$filename
);
}
if ($request->hasFile('bukti2')) {
$bukti2 = $request->file('bukti2');
$filename = Str::uuid() . '.' . $bukti2->extension();
$filePaths['bukti2'] = $folderPath . '/' . $filename;
NextCloudHelper::uploadFile(
$folderPath,
$bukti2->getContent(),
$filename
);
}
if ($request->hasFile('bukti3')) {
$bukti3 = $request->file('bukti3');
$filename = Str::uuid() . '.' . $bukti3->extension();
$filePaths['bukti3'] = $folderPath . '/' . $filename;
NextCloudHelper::uploadFile(
$folderPath,
$bukti3->getContent(),
$filename
);
}
// Generate sequence number and expense number
$sequence_number = FormEntertaimentPresentation::withTrashed()->where('expense_number', 'like', $region->code . '-' . $cabang->code . '-ETY-' . date('ym') . '%')->count() + 1;
$formatted_sequence_number = str_pad($sequence_number, 4, '0', STR_PAD_LEFT);
$expense_number = $region->code . '-' . $cabang->code . '-ETY-' . date('ym') . $formatted_sequence_number;
// Save the form data
FormEntertaimentPresentation::create([
'expense_number' => $expense_number,
'user_id' => auth()->user()->id,
'tanggal' => $request->tanggal,
'jenis' => $request->jenis,
'keterangan' => $request->keterangan,
'total' => $request->total,
'name' => $request->name,
'alamat' => $request->alamat,
'nik_or_npwp' => $request->nik_or_npwp,
'bukti1' => $filePaths['bukti1'] ?? null,
'bukti2' => $filePaths['bukti2'] ?? null,
'bukti3' => $filePaths['bukti3'] ?? null,
'account_number' => $kategori->account_number,
'status' => 'On Progress',
]);
session()->flash('success', 'Form has been created.');
return redirect()->route('forms.entertainment');
}
public function edit($id)
{
$this->checkAuthorization(auth()->user(), ['forms.entertainment.edit']);
$form = FormEntertaimentPresentation::findOrfail($id);
return view('backend.pages.forms.entertainment.edit', [
'form' => $form
]);
}
public function update(Request $request, $id)
{
$this->checkAuthorization(auth()->user(), ['forms.entertainment.edit']);
$request->validate([
'tanggal' => 'required',
'jenis' => 'required|in:entertainment,presentation,sponsorship',
'keterangan' => 'required',
'total' => 'required|integer',
'name' => 'required',
'alamat' => 'required',
'nik_or_npwp' => 'required',
'bukti1' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
'bukti2' => 'nullable|file|mimes:xlsx,xls,csv',
'bukti3' => 'nullable|file|mimes:pdf',
]);
$cabang = UserHasCabang::with('cabang')->where('user_id', auth()->user()->id)->first()->cabang;
$region = Region::where('id', $cabang->region_id)->first();
$kategori = Kategori::where('name', 'Entertainment & Presentation')->first();
$filePaths = [];
$folderPath = '/expense/' . $region->code . '/' . $cabang->code . '/entertainment/';
if ($request->hasFile('bukti1')) {
$bukti1 = $request->file('bukti1');
$filename = Str::uuid() . '.' . $bukti1->extension();
$filePaths['bukti1'] = $folderPath . '/' . $filename;
NextCloudHelper::uploadFile(
$folderPath,
$bukti1->getContent(),
$filename
);
}
if ($request->hasFile('bukti2')) {
$bukti2 = $request->file('bukti2');
$filename = Str::uuid() . '.' . $bukti2->extension();
$filePaths['bukti2'] = $folderPath . '/' . $filename;
NextCloudHelper::uploadFile(
$folderPath,
$bukti2->getContent(),
$filename
);
}
if ($request->hasFile('bukti3')) {
$bukti3 = $request->file('bukti3');
$filename = Str::uuid() . '.' . $bukti3->extension();
$filePaths['bukti3'] = $folderPath . '/' . $filename;
NextCloudHelper::uploadFile(
$folderPath,
$bukti3->getContent(),
$filename
);
}
// Save the form data
$form = FormEntertaimentPresentation::findOrfail($id);
$form->update([
'tanggal' => $request->tanggal,
'jenis' => $request->jenis,
'keterangan' => $request->keterangan,
'total' => $request->total,
'name' => $request->name,
'alamat' => $request->alamat,
'nik_or_npwp' => $request->nik_or_npwp,
'bukti1' => $filePaths['bukti1'] ?? null,
'bukti2' => $filePaths['bukti2'] ?? null,
'bukti3' => $filePaths['bukti3'] ?? null,
'account_number' => $kategori->account_number,
'status' => 'On Progress',
]);
session()->flash('success', 'Form has been updated.');
return redirect()->route('forms.entertainment');
}
public function destroy($id)
{
$this->checkAuthorization(auth()->user(), ['forms.entertainment.delete']);
$form = FormEntertaimentPresentation::findOrfail($id);
$form->delete();
session()->flash('success', 'Form has been deleted.');
return redirect()->route('forms.entertainment');
}
}
@@ -12,6 +12,8 @@ use App\Models\Region;
use App\Models\Cabang;
use App\Models\UserHasCabang;
use App\Models\Kategori;
use App\Helpers\NextCloudHelper;
use Illuminate\Support\Str;
class FormUpCountryController extends Controller
{
@@ -60,20 +62,42 @@ class FormUpCountryController extends Controller
$filePaths = [];
$folderPath = '/expense/' . $region->code . '/' . $cabang->code . '/upcountry/';
// check if folder not exists
// if (!Storage::disk('nextcloud')->exists(trim($folderPath, '/'))) {
// Storage::disk('nextcloud')->makeDirectory(trim($folderPath, '/'));
// }
if ($request->hasFile('bukti1')) {
$bukti1 = $request->file('bukti1');
$filename = Str::uuid() . '.' . $bukti1->extension();
$filePaths['bukti1'] = $folderPath . '/' . $filename;
NextCloudHelper::uploadFile(
$folderPath,
$bukti1->getContent(),
$filename
);
// Store files to Nextcloud and collect their paths
if ($request->hasFile('bukti1')) {
$filePaths['bukti1'] = Storage::disk('nextcloud')->putFileAs($folderPath, $request->file('bukti1'), $request->file('bukti1')->getClientOriginalName());
}
if ($request->hasFile('bukti2')) {
$filePaths['bukti2'] = Storage::disk('nextcloud')->putFileAs($folderPath, $request->file('bukti2'), $request->file('bukti2')->getClientOriginalName());
$bukti2 = $request->file('bukti2');
$filename = Str::uuid() . '.' . $bukti2->extension();
$filePaths['bukti2'] = $folderPath . '/' . $filename;
NextCloudHelper::uploadFile(
$folderPath,
$bukti2->getContent(),
$filename
);
}
if ($request->hasFile('bukti3')) {
$filePaths['bukti3'] = Storage::disk('nextcloud')->putFileAs($folderPath, $request->file('bukti3'), $request->file('bukti3')->getClientOriginalName());
$bukti3 = $request->file('bukti3');
$filename = Str::uuid() . '.' . $bukti3->extension();
$filePaths['bukti3'] = $folderPath . '/' . $filename;
NextCloudHelper::uploadFile(
$folderPath,
$bukti3->getContent(),
$filename
);
}
// Generate sequence number and expense number
@@ -142,20 +166,42 @@ class FormUpCountryController extends Controller
$filePaths = [];
$folderPath = '/expense/' . $region->code . '/' . $cabang->code . '/upcountry/';
// check if folder not exists
// if (!Storage::disk('nextcloud')->exists(trim($folderPath, '/'))) {
// Storage::disk('nextcloud')->makeDirectory(trim($folderPath, '/'));
// }
// Store files to Nextcloud and collect their paths
if ($request->hasFile('bukti1')) {
$filePaths['bukti1'] = Storage::disk('nextcloud')->putFileAs($folderPath, $request->file('bukti1'), $request->file('bukti1')->getClientOriginalName());
$bukti1 = $request->file('bukti1');
$filename = Str::uuid() . '.' . $bukti1->extension();
$filePaths['bukti1'] = $folderPath . '/' . $filename;
NextCloudHelper::uploadFile(
$folderPath,
$bukti1->getContent(),
$filename
);
}
if ($request->hasFile('bukti2')) {
$filePaths['bukti2'] = Storage::disk('nextcloud')->putFileAs($folderPath, $request->file('bukti2'), $request->file('bukti2')->getClientOriginalName());
$bukti2 = $request->file('bukti2');
$filename = Str::uuid() . '.' . $bukti2->extension();
$filePaths['bukti2'] = $folderPath . '/' . $filename;
NextCloudHelper::uploadFile(
$folderPath,
$bukti2->getContent(),
$filename
);
}
if ($request->hasFile('bukti3')) {
$filePaths['bukti3'] = Storage::disk('nextcloud')->putFileAs($folderPath, $request->file('bukti3'), $request->file('bukti3')->getClientOriginalName());
$bukti3 = $request->file('bukti3');
$filename = Str::uuid() . '.' . $bukti3->extension();
$filePaths['bukti3'] = $folderPath . '/' . $filename;
NextCloudHelper::uploadFile(
$folderPath,
$bukti3->getContent(),
$filename
);
}
// Save the form data
@@ -12,6 +12,8 @@ use App\Models\Region;
use App\Models\Cabang;
use App\Models\UserHasCabang;
use App\Models\Kategori;
use App\Helpers\NextCloudHelper;
use Illuminate\Support\Str;
class FormVehicleController extends Controller
{
@@ -56,20 +58,42 @@ class FormVehicleController extends Controller
$filePaths = [];
$folderPath = '/expense/' . $region->code . '/' . $cabang->code . '/vehicle/';
// check if folder not exists
// if (!Storage::disk('nextcloud')->exists(trim($folderPath, '/'))) {
// Storage::disk('nextcloud')->makeDirectory(trim($folderPath, '/'));
// }
if ($request->hasFile('bukti1')) {
$bukti1 = $request->file('bukti1');
$filename = Str::uuid() . '.' . $bukti1->extension();
$filePaths['bukti1'] = $folderPath . '/' . $filename;
NextCloudHelper::uploadFile(
$folderPath,
$bukti1->getContent(),
$filename
);
// Store files to Nextcloud and collect their paths
if ($request->hasFile('bukti1')) {
$filePaths['bukti1'] = Storage::disk('nextcloud')->putFileAs($folderPath, $request->file('bukti1'), $request->file('bukti1')->getClientOriginalName());
}
if ($request->hasFile('bukti2')) {
$filePaths['bukti2'] = Storage::disk('nextcloud')->putFileAs($folderPath, $request->file('bukti2'), $request->file('bukti2')->getClientOriginalName());
$bukti2 = $request->file('bukti2');
$filename = Str::uuid() . '.' . $bukti2->extension();
$filePaths['bukti2'] = $folderPath . '/' . $filename;
NextCloudHelper::uploadFile(
$folderPath,
$bukti2->getContent(),
$filename
);
}
if ($request->hasFile('bukti3')) {
$filePaths['bukti3'] = Storage::disk('nextcloud')->putFileAs($folderPath, $request->file('bukti3'), $request->file('bukti3')->getClientOriginalName());
$bukti3 = $request->file('bukti3');
$filename = Str::uuid() . '.' . $bukti3->extension();
$filePaths['bukti3'] = $folderPath . '/' . $filename;
NextCloudHelper::uploadFile(
$folderPath,
$bukti3->getContent(),
$filename
);
}
// Generate sequence number and expense number
@@ -133,20 +157,42 @@ class FormVehicleController extends Controller
$filePaths = [];
$folderPath = '/expense/' . $region->code . '/' . $cabang->code . '/vehicle/';
// check if folder not exists
// if (!Storage::disk('nextcloud')->exists(trim($folderPath, '/'))) {
// Storage::disk('nextcloud')->makeDirectory(trim($folderPath, '/'));
// }
// Store files to Nextcloud and collect their paths
if ($request->hasFile('bukti1')) {
$filePaths['bukti1'] = Storage::disk('nextcloud')->putFileAs($folderPath, $request->file('bukti1'), $request->file('bukti1')->getClientOriginalName());
$bukti1 = $request->file('bukti1');
$filename = Str::uuid() . '.' . $bukti1->extension();
$filePaths['bukti1'] = $folderPath . '/' . $filename;
NextCloudHelper::uploadFile(
$folderPath,
$bukti1->getContent(),
$filename
);
}
if ($request->hasFile('bukti2')) {
$filePaths['bukti2'] = Storage::disk('nextcloud')->putFileAs($folderPath, $request->file('bukti2'), $request->file('bukti2')->getClientOriginalName());
$bukti2 = $request->file('bukti2');
$filename = Str::uuid() . '.' . $bukti2->extension();
$filePaths['bukti2'] = $folderPath . '/' . $filename;
NextCloudHelper::uploadFile(
$folderPath,
$bukti2->getContent(),
$filename
);
}
if ($request->hasFile('bukti3')) {
$filePaths['bukti3'] = Storage::disk('nextcloud')->putFileAs($folderPath, $request->file('bukti3'), $request->file('bukti3')->getClientOriginalName());
$bukti3 = $request->file('bukti3');
$filename = Str::uuid() . '.' . $bukti3->extension();
$filePaths['bukti3'] = $folderPath . '/' . $filename;
NextCloudHelper::uploadFile(
$folderPath,
$bukti3->getContent(),
$filename
);
}
// Save the form data
@@ -26,4 +26,9 @@ class FormEntertaimentPresentation extends Model
'account_number',
'status',
];
public function user()
{
return $this->belongsTo('App\Models\Admin');
}
}