Files
api-extranetwork/app/Core/Service/OfferPriceService.php
ExtraNetwork e5c4b6aa13 first commit
2026-05-12 17:04:54 +03:00

233 lines
7.9 KiB
PHP

<?php
namespace App\Core\Service;
use App\Core\Repository\OfferPrice\OfferPriceRepository;
use App\Core\Validator\OfferPrice\OfferPriceCreateValidator;
use App;
use Illuminate\Support\Facades\Log;
use Exception;
use App\Exceptions\ApiErrorException;
class OfferPriceService
{
private $offerPriceRepository;
private $offerPriceCreateValidator;
public function __construct(
OfferPriceRepository $offerPriceRepository,
OfferPriceCreateValidator $offerPriceCreateValidator
)
{
$this->offerPriceRepository = $offerPriceRepository;
$this->offerPriceCreateValidator = $offerPriceCreateValidator;
}
public function create($param = [])
{
$response = ['status' => -1, 'message' => '', 'data' => null];
try {
$validationResult = $this->offerPriceCreateValidator->validate($param);
if ($validationResult->errors()->first()) {
$errors = $validationResult->errors()->all();
throw new ApiErrorException($errors);
}
$insertData =
[
"offer_id" => fillOnUndefined($param, "offer_id"),
"date" => fillOnUndefined($param, "date"),
"property_room_id" => fillOnUndefined($param, "property_room_id"),
"property_accommodation_id" => fillOnUndefined($param, "property_accommodation_id"),
"number_of_rooms" => fillOnUndefined($param, "number_of_rooms"),
"currency" => fillOnUndefined($param, "currency"),
"amount" => fillOnUndefined($param, "amount"),
"total_amount" => fillOnUndefined($param, "total_amount"),
"status" => fillOnUndefined($param, "status", 0),
"created_by" => fillOnUndefined($param, "created_by"),
"updated_by" => fillOnUndefined($param, "updated_by"),
"created_at" => time(),
"updated_at" => time(),
];
$userCreateResult = $this->offerPriceRepository->create($insertData);
if ($userCreateResult['status'] != 'success') {
throw new Exception('api-unknown_error');
}
$userData = $userCreateResult["data"];
$response = [
'status' => true,
'data' => $userData,
];
} catch (ApiErrorException $e) {
$response['message'] = implode(', ', $e->getMessageArr());
} catch (Exception $e) {
$message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
Log::error($message);
$response['message'] = $e->getMessage();
}
return output($response);
}
public function select($param = [], $column = ['*'])
{
$response = ['status' => -1, 'message' => '', 'data' => null];
try {
$data = $this->offerPriceRepository->findByCriteria($param, $column);
$response = [
'status' => true,
'data' => $data,
];
} catch (ApiErrorException $e) {
$response['message'] = $e->getMessage();
} catch (Exception $e) {
$message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
Log::error($message);
$response['message'] = $e->getMessage();
}
return output($response);
}
public function update($id, $param = [])
{
$response = ['status' => -1, 'message' => '', 'data' => null];
try {
$updateResult = $this->offerPriceRepository->update($id, $param);
if ($updateResult['status'] != 'success') {
throw new Exception('api-unknown_error');
}
$updateData = $updateResult["data"];
$response = [
'status' => true,
'data' => $updateData,
];
} catch (ApiErrorException $e) {
$response['message'] = implode(', ', $e->getMessageArr());
} catch (Exception $e) {
$message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
Log::error($message);
$response['message'] = $e->getMessage();
}
return output($response);
}
public function updateOrCreate($criteria = [], $saveData = [])
{
$response = ['status' => -1, 'message' => '', 'data' => null];
try {
$data = $this->offerPriceRepository->updateOrCreate($criteria, $saveData);
if ($data['status'] != 'success') {
throw new Exception('api-unknown_error');
}
$response = [
'status' => true,
'data' => $data,
];
} catch (ApiErrorException $e) {
$response['message'] = $e->getMessage();
} catch (Exception $e) {
$message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
Log::error($message);
$response['message'] = $e->getMessage();
}
return output($response);
}
public function insert($params){
return $this->offerPriceRepository->insert($params);
}
public function destroy($params){
return $this->offerPriceRepository->destroy($params);
}
public function deleteThisOfferPrices($params = [])
{
$response = ['status' => -1, 'message' => '', 'data' => null];
try {
$deleteThisIds = [] ;
$requestData = [
'criteria' => [
['field' => 'offer_id', 'condition' => '=', 'value' => fillOnUndefined($params, "offer_id")],
],
"whereIn" =>
[
["field" => "property_room_id", "value" => fillOnUndefined($params, "property_room_id", [])]
]
];
$eraseMappingData = $this->offerPriceRepository->findByCriteria($requestData, ['id']);
$eraseMappingData = $eraseMappingData ? $eraseMappingData : [] ;
$delete = collect($eraseMappingData)->keyBy('id')->keys()->all();
$deleteThisIds = array_merge($deleteThisIds, $delete) ;
$requestData = [
'criteria' => [
['field' => 'offer_id', 'condition' => '=', 'value' => fillOnUndefined($params, "offer_id")],
],
"whereIn" =>
[
["field" => "property_accommodation_id", "value" => fillOnUndefined($params, "property_accommodation_id", [])]
]
];
$eraseMappingData = $this->offerPriceRepository->findByCriteria($requestData, ['id']);
$eraseMappingData = $eraseMappingData ? $eraseMappingData : [] ;
$delete = collect($eraseMappingData)->keyBy('id')->keys()->all();
$deleteThisIds = array_merge($deleteThisIds, $delete) ;
if($deleteThisIds){
$deleteThisArray = $this->offerPriceRepository->destroy($deleteThisIds);
if ($deleteThisArray['status'] != 'success') {
throw new Exception('api-unknown_error');
}
}
$response = [
'status' => true,
'data' => null,
];
} catch (ApiErrorException $e) {
$response['message'] = implode(', ', $e->getMessageArr());
} catch (Exception $e) {
$message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
Log::error($message);
$response['message'] = $e->getMessage();
}
return output($response);
}
}