⚝
One Hat Cyber Team
⚝
Your IP:
216.73.216.19
Server IP:
178.33.27.10
Server:
Linux cpanel.dev-unit.com 3.10.0-1160.108.1.el7.x86_64 #1 SMP Thu Jan 25 16:17:31 UTC 2024 x86_64
Server Software:
Apache/2.4.57 (Unix) OpenSSL/1.0.2k-fips
PHP Version:
8.2.11
Buat File
|
Buat Folder
Eksekusi
Dir :
~
/
home
/
id
/
hospital.dev-unit.com
/
app
/
Repositories
/
View File Name :
PackageRepository.php
<?php namespace App\Repositories; use App\Models\Package; use App\Models\PackageService; use App\Models\Service; use Arr; use Exception; use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException; use Validator; /** * Class PackageRepository * * @version February 25, 2020, 1:10 pm UTC */ class PackageRepository extends BaseRepository { /** * @var array */ protected $fieldSearchable = [ 'name', 'description', 'discount', 'total_amount', ]; /** * Return searchable fields */ public function getFieldsSearchable(): array { return $this->fieldSearchable; } /** * Configure the Model **/ public function model() { return Package::class; } public function getServicesList() { $service = Service::whereStatus(1)->get()->pluck('name', 'id')->sort(); return $service; } public function getServices(): array { $services = Service::whereStatus(1)->where('tenant_id', auth()->user()->tenant_id)->orderBy('name', 'asc')->get()->pluck('name', 'id')->toArray(); return $services; } public function store(array $input): Package { $servicePackageItemInputArray = Arr::only($input, ['service_id', 'quantity', 'rate']); /** @var Package $package */ $package = $this->create(Arr::except($input, ['service_id', 'quantity', 'rate'])); $totalAmount = 0; $packageServiceItemInput = $this->prepareInputForServicePackageItem($servicePackageItemInputArray); foreach ($packageServiceItemInput as $key => $data) { $validator = Validator::make($data, PackageService::$rules); if ($validator->fails()) { throw new UnprocessableEntityHttpException($validator->errors()->first()); } $data['amount'] = $data['rate'] * $data['quantity']; $totalAmount += $data['amount']; /** @var PackageService $packageServiceItem */ $packageServiceItem = new PackageService($data); $package->packageServicesItems()->save($packageServiceItem); } $package->total_amount = $totalAmount - (($totalAmount * $input['discount']) / 100); $package->save(); return $package; } public function prepareInputForServicePackageItem(array $input): array { $items = []; foreach ($input as $key => $data) { foreach ($data as $index => $value) { $items[$index][$key] = $value; if (! (isset($items[$index]['rate']) && $key == 'rate')) { continue; } $items[$index]['rate'] = removeCommaFromNumbers($items[$index]['rate']); } } return $items; } /** * @throws Exception */ public function updatePackage($packageId, $input): Package { $servicePackageItemInputArr = Arr::only($input, ['service_id', 'quantity', 'rate', 'id']); /** @var Package $package */ $package = $this->update($input, $packageId); $totalAmount = 0; $packageServiceItemInput = $this->prepareInputForServicePackageItem($servicePackageItemInputArr); foreach ($packageServiceItemInput as $key => $data) { $validator = Validator::make($data, PackageService::$rules, [ 'service_id.integer' => 'Please select service', ]); if ($validator->fails()) { throw new UnprocessableEntityHttpException($validator->errors()->first()); } $data['amount'] = $data['rate'] * $data['quantity']; $packageServiceItemInput[$key] = $data; $totalAmount += $data['amount']; } /** @var PackageServiceItemsRepository $packageServiceItemRepo */ $packageServiceItemRepo = app(PackageServiceItemsRepository::class); $packageServiceItemRepo->updatePackageServiceItem($packageServiceItemInput, $package->id); $package->total_amount = $totalAmount - (($totalAmount * $input['discount']) / 100); $package->save(); return $package; } }