Files
aso/Modules/Internal/Transformers/ReportPrescriptionResource.php
2025-04-21 15:56:58 +07:00

67 lines
2.3 KiB
PHP
Executable File

<?php
namespace Modules\Internal\Transformers;
use App\Helpers\Helper;
use Illuminate\Http\Resources\Json\JsonResource;
use Carbon\Carbon;
use GuzzleHttp\Client;
class ReportPrescriptionResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
$patientName = $this->user ? $this->user->sFirstName .' '. $this->user->sLastName : '-';
$client = new Client();
$statusUrl = env('K24_URL') . "/partners/lms/v1/order/trackingOrder?order_id=" . $this->sKodeResep;
try {
$response = $client->request('GET', $statusUrl, [
'headers' => [
'Accept' => 'application/json',
// Tambahkan token kalau perlu:
// 'Authorization' => 'Bearer your_token_here',
],
'timeout' => 30,
]);
$body = $response->getBody();
$data = json_decode($body, true);
$statusString = '';
if (isset($data['data']) && is_array($data['data'])) {
$statusString = collect($data['data'])->map(function ($item) {
return "{$item['updated_time']} - {$item['status']}";
});
} else {
$statusString = [];
}
} catch (\Exception $e) {
$statusString = '-';
}
$data = [
'id' => $this->nID,
'patient_name' => $patientName,
'livechat' => $this->livechat,
'prescription_code' => $this->sKodeResep,
'date_consultation' => $this->dTanggalResep ? Carbon::parse($this->dTanggalResep)->format('Y-m-d H:i:s') : null,
'doctor_name' => $this->sDokterName ? $this->sDokterName : '-',
'items' => $this->items ? $this->items : [],
'status_prescription' => $this->status_prescription,
'price_delivery' => $this->ongkir,
'payment_status' => $this->sPaymentStatus,
'status_resep' => $statusString,
'apotek_name' => $this->nIDApotek ? Helper::getOrganization($this->nIDApotek) : '-',
];
return $data;
}
}