Files
exchange-2.0/app/Classes/Modules/Bookings/ControllerLogic/CreateBookingLogic.php
T
CheeSiong c3f153fe4b Create Booking Logic Class
Update Booking Logic Class
 Delete Booking Logic Class
2020-12-04 09:49:47 +08:00

116 lines
4.0 KiB
PHP

<?php
namespace App\Classes\Modules\Bookings\ControllersLogic;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Classes\Modules\CompanyBanks\Services\FetchesCompanyBank;
use App\Classes\Modules\Currencies\Services\FetchesCurrency;
use App\Classes\Modules\Bookings\Standards\Rules\CanCreateBooking;
use App\Classes\Modules\Bookings\Services\CreatesBooking;
use App\Classes\Modules\Bookings\Services\GeneratesBookingMarking;
use App\Classes\Modules\Bookings\DataTransferObjects\BookingObject;
use App\Http\Resources\BookingResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class CreateBookingLogic extends AbstractControllerLogic
{
/**
* @return array
*/
protected function notification():array {
return [
'title' => 'Created Booking',
'message' => 'You have successfully created a new Booking'
];
}
/** @var CanCreateBooking */
private $canCreateBooking;
/** @var CreatesBooking */
private $createsBooking;
/** @var GeneratesBookingMarking */
private $generatesBookingMarking;
/** @var FetchesCompany */
private $fetchesCompany;
/** @var FetchesCompanyBank */
private $fetchesCompanyBank;
/** @var FetchesCurrency */
private $fetchesCurrency;
/**
* CreateSegmentLogic constructor.
* @param CanCreateBooking $canCreateBooking
* @param CreatesBooking $createsBooking
* @param FetchesCompany $fetchesCompany
* @param FetchesCompanyBank $fetchesCompanyBank
* @param FetchesCurrency $fetchesCurrency
*/
public function __construct(
CanCreateBooking $canCreateBooking,
CreatesBooking $createsBooking,
GeneratesBookingMarking $generatesBookingMarking,
FetchesCompany $fetchesCompany,
FetchesCompanyBank $fetchesCompanyBank,
FetchesCurrency $fetchesCurrency
)
{
$this->canCreateBooking = $canCreateBooking;
$this->createsBooking = $createsBooking;
$this->generatesBookingMarking = $generatesBookingMarking;
$this->fetchesCompany = $fetchesCompany;
$this->fetchesCompanyBank = $fetchesCompanyBank;
$this->fetchesCurrency = $fetchesCurrency;
}
/**
* @param Request $request
* @return JsonResponse
* @throws ErrorException
*/
public function logic(Request $request) : JsonResponse
{
try {
DB::beginTransaction();
$booking_object = new BookingObject(
$request->input('company_id'),
$request->input('transferable_bank_id'),
$this->generatesBookingMarking->execute(),
$request->input('reference'),
number_format( (float) $request->input('fix_amount'), 5, '.', ''),
$request->input('fix_currency_id'),
$request->input('convertible_currency_id'),
$request->input('conversion_currency_id')
);
$this->canCreateBooking->passes($booking_object);
$company = $this->fetchesCompany->execute(['id' => $request->input('company_id')]);
$transferable_bank = $this->fetchesCompanyBank->execute(['id' => $request->input('transferable_bank_id')]);
$fix_currency = $this->fetchesCurrency->execute(['id' => $request->input('fix_currency_id')]);
$convertible_currency = $this->fetchesCurrency->execute(['id' => $request->input('convertible_currency_id')]);
$conversion_currency = $this->fetchesCurrency->execute(['id' => $request->input('conversion_currency_id')]);
$booking = $this->createsBooking->execute($booking_object);
DB::commit();
return $this->resourceResponse(new BookingResource($booking));
} catch (\Exception $exception) {
throw new ErrorException($exception->getMessage(), $exception->getCode());
}
}
}