master
parent
8e318a041b
commit
ec1a2cc27c
|
|
@ -59,10 +59,13 @@ class MobileApiController extends Controller
|
|||
return response()->json($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sendApplication(Request $request): JsonResponse {
|
||||
$data = $request->all();
|
||||
$data['started_at'] = Carbon::parse($data['started_at'])?->format('Y-m-d H:i:s');
|
||||
$data['ended_at'] = Carbon::parse($data['ended_at'])?->format('Y-m-d H:i:s');
|
||||
$data['started_at'] = Carbon::createFromFormat('d-m-Y H:i', $data['started_at']);
|
||||
$data['ended_at'] = Carbon::createFromFormat('d-m-Y H:i', $data['ended_at']);
|
||||
$authToken = null;
|
||||
$data['car_id'] = null;
|
||||
if ($request->header('Authorization')) {
|
||||
|
|
@ -113,7 +116,12 @@ class MobileApiController extends Controller
|
|||
}
|
||||
$address_end = UniModel::model('pipi_address')->where('name', $data['address_end'])->first();
|
||||
$address_start = UniModel::model('pipi_address')->where('name', $data['address_start'])->first();
|
||||
|
||||
$service = new DepositService;
|
||||
$sums = $service->calculateSummary($data['mark_id'], $data['started_at'], $data['ended_at']);
|
||||
$sum = $sums['discounted_sum'];
|
||||
if ($data['deposit'] != $sums['deposit_base']) {
|
||||
$sum += $data['deposit'];
|
||||
}
|
||||
UniModel::model('pipi_applications')->create([
|
||||
'rent_day' => $data['rent_day'],
|
||||
'started_at' => $data['started_at'],
|
||||
|
|
@ -127,7 +135,8 @@ class MobileApiController extends Controller
|
|||
'address_end' => $address_end,
|
||||
'address_start' => $address_start,
|
||||
'deposit' => $data['deposit'] ?? null,
|
||||
'status' => ApplicationStatus::pending->value
|
||||
'status' => ApplicationStatus::pending->value,
|
||||
'sum' => $sum
|
||||
]);
|
||||
|
||||
return response()->json($response);
|
||||
|
|
@ -159,6 +168,7 @@ class MobileApiController extends Controller
|
|||
'rent_day' => $app->rent_day,
|
||||
'started_at' => $app->started_at,
|
||||
'ended_at' => $app->ended_at,
|
||||
'status' => ApplicationStatus::from($app->status)->getName(),
|
||||
'car' => [
|
||||
'name' => $model->name,
|
||||
'year' => $model->year,
|
||||
|
|
@ -363,53 +373,14 @@ class MobileApiController extends Controller
|
|||
|
||||
$start = Carbon::createFromFormat('d-m-Y H:i', $started_at);
|
||||
$end = Carbon::createFromFormat('d-m-Y H:i', $ended_at);
|
||||
$days = $start->diffInDays($end);
|
||||
try {
|
||||
$service = new DepositService();
|
||||
$summary = $service->calculateSummary($mark_id, $start, $end);
|
||||
|
||||
// Получаем тарифы
|
||||
$tariffs = UniModel::model('pipi_auto_tariffs')
|
||||
->where('model_id', $mark_id)
|
||||
->get();
|
||||
if (!isset($tariffs)) {
|
||||
return response()->json(__('Отсутсвуют данные по машине'));
|
||||
return response()->json($summary);
|
||||
} catch (\Exception $e) {
|
||||
return response()->json(['error' => $e->getMessage()], 400);
|
||||
}
|
||||
|
||||
$discountRate = null;
|
||||
$basePrice = null;
|
||||
$deposit = null;
|
||||
|
||||
foreach ($tariffs as $range) {
|
||||
if ($range->day_range_start == 1 && $range->day_range_end == 2) {
|
||||
$basePrice = $range->base_rate;
|
||||
}
|
||||
|
||||
if (isset($range->deposit)) {
|
||||
$deposit = $range->deposit;
|
||||
}
|
||||
if ($days >= $range->day_range_start && $days <= $range->day_range_end) {
|
||||
$discountRate = $range->base_rate;
|
||||
}
|
||||
}
|
||||
|
||||
$deposit_without = new DepositService();
|
||||
|
||||
// Если не нашли подходящий тариф, но дней больше 30 — применяем скидку
|
||||
if (is_null($discountRate) && $days > 30 && count($tariffs) > 0) {
|
||||
$discountRate = round($basePrice * 0.6);
|
||||
}
|
||||
|
||||
// Считаем суммы
|
||||
$baseSum = $basePrice ? $basePrice * $days : null;
|
||||
$discountedSum = $discountRate ? $discountRate * $days : null;
|
||||
|
||||
return response()->json([
|
||||
'days' => $days,
|
||||
'base_price_per_day' => $basePrice,
|
||||
'discount_price_per_day' => $discountRate,
|
||||
'base_sum' => $baseSum,
|
||||
'discounted_sum' => $discountedSum,
|
||||
'deposit_base' => $deposit,
|
||||
'without_deposit' => $deposit_without->calcWithoutDeposit($deposit, $days),
|
||||
]);
|
||||
}
|
||||
|
||||
public function closeOrder(Request $request): JsonResponse
|
||||
|
|
|
|||
|
|
@ -2,6 +2,9 @@
|
|||
|
||||
namespace App\Service;
|
||||
|
||||
use A7kz\Platform\Models\UniModel;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
class DepositService
|
||||
{
|
||||
public function calcWithoutDeposit($deposit, $day): float {
|
||||
|
|
@ -26,4 +29,56 @@ class DepositService
|
|||
$sum = $deposit * ($percent / 100) * (1 + $ndsRate);
|
||||
return round($sum, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function calculateSummary(int $markId, $start, $end): array
|
||||
{
|
||||
$days = $start->diffInDays($end);
|
||||
|
||||
$tariffs = UniModel::model('pipi_auto_tariffs')
|
||||
->where('model_id', $markId)
|
||||
->get();
|
||||
|
||||
if ($tariffs->isEmpty()) {
|
||||
throw new \Exception(__('Отсутствуют данные по машине'));
|
||||
}
|
||||
|
||||
$basePrice = null;
|
||||
$discountRate = null;
|
||||
$deposit = null;
|
||||
|
||||
foreach ($tariffs as $range) {
|
||||
if ($range->day_range_start == 1 && $range->day_range_end == 2) {
|
||||
$basePrice = $range->base_rate;
|
||||
}
|
||||
|
||||
if (isset($range->deposit)) {
|
||||
$deposit = $range->deposit;
|
||||
}
|
||||
|
||||
if ($days >= $range->day_range_start && $days <= $range->day_range_end) {
|
||||
$discountRate = $range->base_rate;
|
||||
}
|
||||
}
|
||||
|
||||
// Если не нашли скидку, но дней > 30
|
||||
if (is_null($discountRate) && $days > 30 && $basePrice) {
|
||||
$discountRate = round($basePrice * 0.6);
|
||||
}
|
||||
|
||||
$baseSum = $basePrice ? $basePrice * $days : null;
|
||||
$discountedSum = $discountRate ? $discountRate * $days : null;
|
||||
|
||||
return [
|
||||
'days' => $days,
|
||||
'base_price_per_day' => $basePrice,
|
||||
'discount_price_per_day' => $discountRate,
|
||||
'base_sum' => $baseSum,
|
||||
'discounted_sum' => $discountedSum,
|
||||
'deposit_base' => $deposit,
|
||||
'without_deposit' => $this->calcWithoutDeposit($deposit, $days),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ $approvedApplications = UniModel::model('pipi_applications', Acl::connection())
|
|||
// Заявки с ожидающим статусом
|
||||
$pendingApplications = UniModel::model('pipi_applications', Acl::connection())
|
||||
->where('user_id', auth()->user()->id)
|
||||
->where('status', ApplicationStatus::reserved)
|
||||
->where('status', ApplicationStatus::approved)
|
||||
->get();
|
||||
?>
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,17 @@ enum ApplicationStatus: string
|
|||
case pending = 'pending';
|
||||
case approved = 'approved';
|
||||
case rejected = 'rejected';
|
||||
case reserved = 'reserved'; //статус брони
|
||||
case completed = 'completed';
|
||||
|
||||
|
||||
function getName(): string
|
||||
{
|
||||
return match ($this) {
|
||||
self::pending => 'В обработке',
|
||||
self::approved => 'Утверждена',
|
||||
self::rejected => 'Отклонена',
|
||||
self::completed => 'Завершена',
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -48,12 +48,25 @@ return new class extends \A7kz\Platform\Commands\InstallScript {
|
|||
$table->json('photos')->nullable();
|
||||
});
|
||||
}
|
||||
|
||||
if (!Schema::connection($segment->connector)->hasColumn('pipi_applications', 'address_start')) {
|
||||
Schema::connection($segment->connector)->table('pipi_applications', static function (Blueprint $table) {
|
||||
$table->unsignedBigInteger('address_end')->nullable();
|
||||
$table->unsignedBigInteger('address_start')->nullable();
|
||||
});
|
||||
}
|
||||
|
||||
if (!Schema::connection($segment->connector)->hasColumn('pipi_applications', 'sum')) {
|
||||
Schema::connection($segment->connector)->table('pipi_applications', static function (Blueprint $table) {
|
||||
$table->integer('sum')->nullable();
|
||||
});
|
||||
}
|
||||
|
||||
if (!Schema::connection($segment->connector)->hasColumn('pipi_applications', 'deposit')) {
|
||||
Schema::connection($segment->connector)->table('pipi_applications', static function (Blueprint $table) {
|
||||
$table->integer('deposit')->nullable();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue