D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
forge
/
lactobokashi.com
/
app
/
Services
/
Filename :
TaxService.php
back
Copy
<?php namespace App\Services; use Exception; use App\Models\Tax; use App\Http\Requests\TaxRequest; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; use App\Http\Requests\PaginateRequest; use App\Libraries\QueryExceptionLibrary; class TaxService { protected array $taxFilter = [ 'name', 'code', 'tax_rate', 'type', 'status' ]; /** * @throws Exception */ public function list(PaginateRequest $request) { try { $requests = $request->all(); $method = $request->get('paginate', 0) == 1 ? 'paginate' : 'get'; $methodValue = $request->get('paginate', 0) == 1 ? $request->get('per_page', 10) : '*'; $orderColumn = $request->get('order_column') ?? 'id'; $orderType = $request->get('order_type') ?? 'desc'; return Tax::where(function ($query) use ($requests) { foreach ($requests as $key => $request) { if (in_array($key, $this->taxFilter)) { $query->where($key, 'like', '%' . $request . '%'); } } })->orderBy($orderColumn, $orderType)->$method( $methodValue ); } catch (Exception $exception) { Log::info($exception->getMessage()); throw new Exception($exception->getMessage(), 422); } } /** * @throws Exception */ public function store(TaxRequest $request) { try { return Tax::create($request->validated()); } catch (Exception $exception) { Log::info($exception->getMessage()); throw new Exception($exception->getMessage(), 422); } } /** * @throws Exception */ public function update(TaxRequest $request, Tax $tax) { try { return tap($tax)->update($request->validated()); } catch (Exception $exception) { Log::info($exception->getMessage()); throw new Exception($exception->getMessage(), 422); } } /** * @throws Exception */ public function destroy(Tax $tax): void { try { $tax->delete(); } catch (Exception $exception) { Log::info($exception->getMessage()); throw new Exception(QueryExceptionLibrary::message($exception), 422); } } /** * @throws Exception */ public function show(Tax $tax): Tax { try { return $tax; } catch (Exception $exception) { Log::info($exception->getMessage()); throw new Exception($exception->getMessage(), 422); } } }