Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
memiljamel committed Oct 14, 2024
1 parent 3bfa27e commit 08e516e
Show file tree
Hide file tree
Showing 67 changed files with 2,613 additions and 800 deletions.
16 changes: 16 additions & 0 deletions app/Enums/StatusTypeEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Enums;

enum StatusTypeEnum: string
{
/**
* define the status type of asset as active.
*/
case Active = 'active';

/**
* define the status type of asset as inactive.
*/
case Inactive = 'inactive';
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/AssetActiveController.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function store(StoreAssetRequest $request): RedirectResponse
QrCode::format('png')
->margin(1)
->size(512)
->generate("http://localhost:8080/{$asset->id}"),
->generate(route('asset-details.scans', $asset->id)),
);

if ($request->hasFile('attachments')) {
Expand Down
17 changes: 17 additions & 0 deletions app/Http/Controllers/AssetDetailController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Http\Controllers;

use App\Models\Asset;
use Illuminate\View\View;

class AssetDetailController extends Controller
{
/**
* Handle the incoming request.
*/
public function __invoke(Asset $asset): View
{
return view('asset-details.index', compact('asset'));
}
}
3 changes: 1 addition & 2 deletions app/Http/Controllers/AssetFinanceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ public function index(Request $request): View
->orWhere('date', 'LIKE', "%{$search}%")
->orWhere('amount', 'LIKE', "%{$search}%")
->orWhereHas('asset', function (Builder $query) use ($search) {
$query->where('active', true)
->orWhere('name', 'LIKE', "%{$search}%");
$query->where('name', 'LIKE', "%{$search}%");
});
})
->latest()
Expand Down
3 changes: 1 addition & 2 deletions app/Http/Controllers/AssetHistoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public function index(Request $request): View
->orWhere('condition_percentage', 'LIKE', "%{$search}%")
->orWhere('completeness_percentage', 'LIKE', "%{$search}%")
->orWhereHas('asset', function (Builder $query) use ($search) {
$query->where('active', true)
->orWhere('name', 'LIKE', "%{$search}%");
$query->where('name', 'LIKE', "%{$search}%");
})
->orWhereHas('responsiblePerson', function (Builder $query) use ($search) {
$query->where('name', 'LIKE', "%{$search}%");
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function index(Request $request): View
$page = $request->query('page');
$search = $request->query('search');

$categories = Category::when($search, function (Builder $query, ?string $search) {
$categories = Category::withoutNoneName()->when($search, function (Builder $query, ?string $search) {
$query->where('name', 'LIKE', "%{$search}%")
->orWhere('description', 'LIKE', "%{$search}%");
})
Expand Down
251 changes: 251 additions & 0 deletions app/Http/Controllers/ExportController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
<?php

namespace App\Http\Controllers;

use alhimik1986\PhpExcelTemplator\params\ExcelParam;
use alhimik1986\PhpExcelTemplator\PhpExcelTemplator;
use alhimik1986\PhpExcelTemplator\setters\CellSetterArrayValue;
use alhimik1986\PhpExcelTemplator\setters\CellSetterStringValue;
use App\Enums\PermissionEnum;
use App\Enums\RoleEnum;
use App\Enums\StatusTypeEnum;
use App\Models\Asset;
use Illuminate\Http\Request;
use PhpOffice\PhpWord\TemplateProcessor;
use Spatie\LaravelPdf\Enums\Format;
use Spatie\LaravelPdf\Enums\Orientation;
use Spatie\LaravelPdf\Enums\Unit;
use Spatie\LaravelPdf\PdfBuilder;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

use function Spatie\LaravelPdf\Support\pdf;

class ExportController extends Controller
{
/**
* Instantiate a new controller instance.
*/
public function __construct()
{
$this->middleware('role:'.RoleEnum::Administrator->value.'|'.RoleEnum::Custom->value);
$this->middleware('permission:'.PermissionEnum::DownloadAssets->value);
}

/**
* Export data from database to pdf.
*/
public function pdf(Request $request, StatusTypeEnum $status): PdfBuilder
{
$assets = Asset::active($status->value === StatusTypeEnum::Active->value)
->with(['category', 'brand', 'latestHistory', 'assetArchive'])
->get();

$filename = $status->value === StatusTypeEnum::Active->value
? time().'_Asset Active.pdf'
: time().'_Asset Inactive.pdf';

return pdf()->view('asset-exports.pdf.main', compact('assets', 'status'))
->headerView('asset-exports.pdf.header', compact('status'))
->footerView('asset-exports.pdf.footer')
->orientation(Orientation::Landscape)
->format(Format::Tabloid)
->margins(128, 16, 56, 16, Unit::Pixel)
->name($filename)
->download();
}

/**
* Export data from database to xlsx.
*/
public function xlsx(Request $request, StatusTypeEnum $status): BinaryFileResponse
{
$assets = Asset::active($status->value === StatusTypeEnum::Active->value)
->with(['category', 'brand', 'latestHistory', 'assetArchive'])
->get();

$template = $status->value === StatusTypeEnum::Active->value
? 'excel-active.xlsx'
: 'excel-inactive.xlsx';

$filename = $status->value === StatusTypeEnum::Active->value
? time().'_Asset Active.xlsx'
: time().'_Asset Inactive.xlsx';

PhpExcelTemplator::outputToFile(
resource_path("templates/$template"),
storage_path("app/public/$filename"),
[
'{date}' => new ExcelParam(
CellSetterStringValue::class,
now()->toDateString(),
),
'[no]' => new ExcelParam(
CellSetterArrayValue::class,
range(1, $assets->count()),
),
'[asset_code]' => new ExcelParam(
CellSetterArrayValue::class,
$assets->pluck('code')->toArray(),
),
'[asset_name]' => new ExcelParam(
CellSetterArrayValue::class,
$assets->pluck('name')->toArray(),
),
'[asset_category]' => new ExcelParam(
CellSetterArrayValue::class,
$assets->map(fn (Asset $asset) => $asset->category?->name)->toArray(),
),
'[asset_brand]' => new ExcelParam(
CellSetterArrayValue::class,
$assets->map(fn (Asset $asset) => $asset->brand?->name)->toArray(),
),
'[asset_type]' => new ExcelParam(
CellSetterArrayValue::class,
$assets->pluck('type')->toArray(),
),
'[asset_manufacturer]' => new ExcelParam(
CellSetterArrayValue::class,
$assets->pluck('manufacturer')->toArray(),
),
'[asset_serial_number]' => new ExcelParam(
CellSetterArrayValue::class,
$assets->pluck('serial_number')->toArray(),
),
'[asset_production_year]' => new ExcelParam(
CellSetterArrayValue::class,
$assets->pluck('production_year')->toArray(),
),
'[asset_description]' => new ExcelParam(
CellSetterArrayValue::class,
$assets->pluck('description')->toArray(),
),
'[asset_purchase_date]' => new ExcelParam(
CellSetterArrayValue::class,
$assets->pluck('purchase_date')->toArray(),
),
'[asset_distributor]' => new ExcelParam(
CellSetterArrayValue::class,
$assets->map(fn (Asset $asset) => $asset->distributor?->name)->toArray(),
),
'[asset_invoice_number]' => new ExcelParam(
CellSetterArrayValue::class,
$assets->pluck('invoice_number')->toArray(),
),
'[asset_qty]' => new ExcelParam(
CellSetterArrayValue::class,
$assets->pluck('qty')->toArray(),
),
'[asset_unit_price]' => new ExcelParam(
CellSetterArrayValue::class,
$assets->pluck('unit_price')->toArray(),
),
'[asset_total_price]' => new ExcelParam(
CellSetterArrayValue::class,
$assets->pluck('total_price')->toArray(),
),
'[archive_inactive_date]' => new ExcelParam(
CellSetterArrayValue::class,
$assets->map(fn (Asset $asset) => $asset->assetArchive?->inactive_date)->toArray(),
),
'[archive_reason]' => new ExcelParam(
CellSetterArrayValue::class,
$assets->map(fn (Asset $asset) => $asset->assetArchive?->reason->label())->toArray(),
),
'[archive_notes]' => new ExcelParam(
CellSetterArrayValue::class,
$assets->map(fn (Asset $asset) => $asset->assetArchive?->notes)->toArray(),
),
'[history_date_from]' => new ExcelParam(
CellSetterArrayValue::class,
$assets->map(fn (Asset $asset) => $asset->latestHistory?->date_from)->toArray(),
),
'[history_responsible_person]' => new ExcelParam(
CellSetterArrayValue::class,
$assets->map(fn (Asset $asset) => $asset->latestHistory?->responsiblePerson?->name)->toArray(),
),
'[history_location]' => new ExcelParam(
CellSetterArrayValue::class,
$assets->map(fn (Asset $asset) => $asset->latestHistory?->location?->name)->toArray(),
),
'[history_qty]' => new ExcelParam(
CellSetterArrayValue::class,
$assets->map(fn (Asset $asset) => $asset->latestHistory?->qty)->toArray(),
),
'[history_condition]' => new ExcelParam(
CellSetterArrayValue::class,
$assets->map(fn (Asset $asset) => $asset->latestHistory?->condition_percentage)->toArray(),
),
'[history_completeness]' => new ExcelParam(
CellSetterArrayValue::class,
$assets->map(fn (Asset $asset) => $asset->latestHistory?->completeness_percentage)->toArray(),
),
'[history_notes]' => new ExcelParam(
CellSetterArrayValue::class,
$assets->map(fn (Asset $asset) => $asset->latestHistory?->notes)->toArray(),
),
'[asset_notes]' => new ExcelParam(
CellSetterArrayValue::class,
$assets->pluck('notes')->toArray(),
),
],
);

return response()->download(storage_path("app/public/$filename"))
->deleteFileAfterSend();
}

/**
* Export data from database to docx.
*/
public function docx(Request $request, StatusTypeEnum $status): BinaryFileResponse
{
$assets = Asset::active($status->value === StatusTypeEnum::Active->value)
->with(['category', 'brand', 'latestHistory', 'assetArchive'])
->get();

$template = $status->value === StatusTypeEnum::Active->value
? 'word-active.docx'
: 'word-inactive.docx';

$filename = $status->value === StatusTypeEnum::Active->value
? time().'_Asset Active.docx'
: time().'_Asset Inactive.docx';

$phpWord = new TemplateProcessor(resource_path("templates/$template"));
$phpWord->setValue('date', now()->toDateString());
$phpWord->cloneRowAndSetValues('asset_code', $assets->map(fn (Asset $asset, int $key) => [
'no' => $key + 1,
'asset_code' => $asset->code,
'asset_name' => $asset->name,
'asset_category' => $asset->category?->name,
'asset_brand' => $asset->brand?->name,
'asset_type' => $asset->type,
'asset_manufacturer' => $asset->manufacturer,
'asset_serial_number' => $asset->serial_number,
'asset_production_year' => $asset->production_year,
'asset_description' => $asset->description,
'asset_purchase_date' => $asset->purchase_date,
'asset_distributor' => $asset->distributor?->name,
'asset_invoice_number' => $asset->invoice_number,
'asset_qty' => $asset->qty,
'asset_unit_price' => $asset->unit_price,
'asset_total_price' => $asset->total_price,
'archive_inactive_date' => $asset->assetArchive?->inactive_date,
'archive_reason' => $asset->assetArchive?->reason->label(),
'archive_notes' => $asset->assetArchive?->notes,
'history_date_from' => $asset->latestHistory?->date_from,
'history_responsible_person' => $asset->latestHistory?->responsiblePerson?->name,
'history_location' => $asset->latestHistory?->location?->name,
'history_qty' => $asset->latestHistory?->qty,
'history_condition' => $asset->latestHistory?->condition_percentage,
'history_completeness' => $asset->latestHistory?->completeness_percentage,
'history_notes' => $asset->latestHistory?->notes,
'asset_notes' => $asset->notes,
])->toArray());

$phpWord->saveAs(storage_path("app/public/$filename"));

return response()->download(storage_path("app/public/$filename"))
->deleteFileAfterSend();
}
}
7 changes: 4 additions & 3 deletions app/Http/Controllers/PrintController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Spatie\LaravelPdf\Enums\Orientation;
use Spatie\LaravelPdf\Enums\Unit;
use Spatie\LaravelPdf\PdfBuilder;

use function Spatie\LaravelPdf\Support\pdf;

class PrintController extends Controller
Expand All @@ -21,8 +22,8 @@ class PrintController extends Controller
*/
public function __construct()
{
$this->middleware('role:' . RoleEnum::Administrator->value . '|' . RoleEnum::Custom->value);
$this->middleware('permission:' . PermissionEnum::CreatePrints->value);
$this->middleware('role:'.RoleEnum::Administrator->value.'|'.RoleEnum::Custom->value);
$this->middleware('permission:'.PermissionEnum::CreatePrints->value);
}

/**
Expand Down Expand Up @@ -69,6 +70,6 @@ public function store(StorePrintRequest $request): PdfBuilder
->orientation(Orientation::Portrait)
->format(Format::A4)
->margins(56, 16, 56, 16, Unit::Pixel)
->name(time() . "- {$asset->name}.pdf");
->name(time()."- {$asset->name}.pdf");
}
}
43 changes: 43 additions & 0 deletions app/Http/Controllers/ProfileController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Http\Controllers;

use App\Http\Requests\UpdateProfileRequest;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\View\View;

class ProfileController extends Controller
{
/**
* Show the form for editing the specified resource.
*/
public function edit(): View
{
$user = Auth::user();

return view('profile.edit', compact('user'));
}

/**
* Update the specified resource in storage.
*/
public function update(UpdateProfileRequest $request): RedirectResponse
{
$user = Auth::user();
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->description = $request->input('description');

if ($request->filled('password')) {
$user->password = Hash::make($request->input('password'));
}

$user->save();

return redirect()->route('profile.edit')
->with('message', 'The profile has been updated.');

}
}
Loading

0 comments on commit 08e516e

Please sign in to comment.