NIM_E31222518/app/Services/OngkirService.php

164 lines
5.4 KiB
PHP

<?php
namespace App\Services;
use App\Models\Ongkir;
class OngkirService
{
public function getProvinces()
{
return Ongkir::select('province_id', 'province_name')
->distinct()
->orderBy('province_name')
->get();
}
public function getCities($provinceId)
{
return Ongkir::where('province_id', $provinceId)
->select('city_id', 'city_name')
->orderBy('city_name')
->get();
}
public function calculateCost($origin, $destination, $weight, $courier)
{
// Ambil data ongkir berdasarkan kota tujuan
$ongkir = Ongkir::where('city_id', $destination)->first();
if (!$ongkir) {
throw new \Exception('Data ongkir tidak ditemukan');
}
// Hitung ongkir berdasarkan kurir dan layanan
$costs = [];
switch ($courier) {
case 'jne':
$costs = [
[
'service' => 'REG',
'description' => 'Layanan Reguler',
'cost' => [
[
'value' => $ongkir->jne_reg,
'etd' => '2-3',
'note' => 'Estimasi pengiriman 2-3 hari'
]
]
],
[
'service' => 'OKE',
'description' => 'Layanan Ekonomis',
'cost' => [
[
'value' => $ongkir->jne_oke,
'etd' => '3-5',
'note' => 'Estimasi pengiriman 3-5 hari'
]
]
],
[
'service' => 'YES',
'description' => 'Layanan Express',
'cost' => [
[
'value' => $ongkir->jne_yes,
'etd' => '1-2',
'note' => 'Estimasi pengiriman 1-2 hari'
]
]
]
];
break;
case 'pos':
$costs = [
[
'service' => 'REG',
'description' => 'Layanan Reguler',
'cost' => [
[
'value' => $ongkir->pos_reg,
'etd' => '3-4',
'note' => 'Estimasi pengiriman 3-4 hari'
]
]
],
[
'service' => 'EXPRESS',
'description' => 'Layanan Express',
'cost' => [
[
'value' => $ongkir->pos_express,
'etd' => '1-2',
'note' => 'Estimasi pengiriman 1-2 hari'
]
]
]
];
break;
case 'tiki':
$costs = [
[
'service' => 'REG',
'description' => 'Layanan Reguler',
'cost' => [
[
'value' => $ongkir->tiki_reg,
'etd' => '2-3',
'note' => 'Estimasi pengiriman 2-3 hari'
]
]
],
[
'service' => 'ECO',
'description' => 'Layanan Ekonomis',
'cost' => [
[
'value' => $ongkir->tiki_eco,
'etd' => '3-5',
'note' => 'Estimasi pengiriman 3-5 hari'
]
]
],
[
'service' => 'EXPRESS',
'description' => 'Layanan Express',
'cost' => [
[
'value' => $ongkir->tiki_express,
'etd' => '1-2',
'note' => 'Estimasi pengiriman 1-2 hari'
]
]
]
];
break;
}
return [
'rajaongkir' => [
'query' => [
'origin' => $origin,
'destination' => $destination,
'weight' => $weight,
'courier' => $courier
],
'status' => [
'code' => 200,
'description' => 'OK'
],
'results' => [
[
'code' => $courier,
'name' => strtoupper($courier),
'costs' => $costs
]
]
]
];
}
}