@php
use Carbon\Carbon;
function capitalizeFirstChar($itemType): string
{
if ($itemType === null || $itemType === "") {
return "";
}
if ($itemType === "BED") {
return "Accommodation";
}
return ucfirst(strtolower($itemType));
}
function compareDate($createdDate): string
{
if ($createdDate == null) {
return "";
}
$createdDate = Carbon::parse($createdDate)->format('Y-m-d');
$currentDate = Carbon::now()->format('Y-m-d');
if ($createdDate == $currentDate) {
return "";
}
return "(Reprint)";
}
function dateFormatter($createdDate)
{
if ($createdDate == null || $createdDate == "") {
return "";
}
$dateTime = substr($createdDate, 0, 23);
return Carbon::parse($dateTime)->timezone('Asia/Dhaka')->format('d-M-Y');
}
function getDeliveryTime($deliveryTime, $deliveryUom, $createdDate)
{
if ($deliveryUom == null || $deliveryTime == null || $createdDate == null) {
return "";
}
$dateTime = substr($createdDate, 0, 23);
$date = Carbon::parse($dateTime)->timezone('Asia/Dhaka');
if ($deliveryUom === "minute") {
return $date->addMinutes($deliveryTime);
}
if ($deliveryUom === "hour") {
return $date->addHours($deliveryTime);
}
if ($deliveryUom === "day") {
return $date->addDays($deliveryTime);
}
if ($deliveryUom === "week") {
return $date->addWeeks($deliveryTime);
}
if ($deliveryUom === "month") {
return $date->addMonths($deliveryTime);
}
if ($deliveryUom === "year") {
return $date->addYears($deliveryTime);
}
return "";
}
function getLineDiscountAmount($item): float
{
// Prefer explicit line total discount when available
$totalDiscount = (float)($item->totalDiscount ?? 0);
if ($totalDiscount > 0) {
return (float) round($totalDiscount, 2);
}
$quantity = (float)($item->quantity ?? 0);
$unitPrice = (float)($item->unitPrice ?? $item->saleAmount ?? 0);
// If discountAmount exists, decide whether it's per-unit or already a line total
if (isset($item->discountAmount) && $item->discountAmount !== null && $item->discountAmount !== '') {
$disc = (float)$item->discountAmount;
if ($disc <= 0) {
// explicitly zero
// fall through to percentage fallback
} else {
// If discount amount is larger than unit price, assume it's a line total
if ($disc > ($unitPrice * 1.01) || $disc > ($unitPrice * $quantity * 0.5)) {
return (float) round($disc, 2);
}
// Otherwise treat as per-unit discount
return (float) round($disc * $quantity, 2);
}
}
// Fallback to percentage of total sales amount
$totalSales = (float)($item->totalSalesAmount ?? ($unitPrice * $quantity));
$percent = (float)($item->overAllDiscountPercentage ?? 0);
$calc = ($totalSales * $percent) / 100;
return (float) round($calc, 2);
}
function getLineNetAmount($item): float
{
// Compute gross as unit price * quantity, then subtract the line discount
$unitPrice = (float)($item->unitPrice ?? $item->saleAmount ?? 0);
$quantity = (float)($item->quantity ?? 0);
$gross = $unitPrice * $quantity;
$lineDiscount = getLineDiscountAmount($item);
return (float) round($gross - $lineDiscount, 2);
}
function groupItemsByName(array $items): array
{
$grouped = [];
foreach ($items as $item) {
$itemName = $item->productItem->name ?? $item->itemName;
$lineDiscountAmount = getLineDiscountAmount($item);
$lineNetAmount = getLineNetAmount($item);
if (!isset($grouped[$itemName])) {
$grouped[$itemName] = json_decode(json_encode($item));
$grouped[$itemName]->quantity = (float)($item->quantity ?? 0);
$grouped[$itemName]->netSales = $lineNetAmount;
$grouped[$itemName]->totalSalesAmount = (float)($item->totalSalesAmount ?? 0);
$grouped[$itemName]->discountAmount = $lineDiscountAmount;
$grouped[$itemName]->totalDiscount = $lineDiscountAmount;
$grouped[$itemName]->totalVat = (float)(($item->vatAmount ?? 0) * ($item->quantity ?? 0));
$grouped[$itemName]->totalSd = (float)(($item->sdAmount ?? 0) * ($item->quantity ?? 0));
} else {
$grouped[$itemName]->quantity += (float)($item->quantity ?? 0);
$grouped[$itemName]->netSales += $lineNetAmount;
$grouped[$itemName]->totalSalesAmount += (float)($item->totalSalesAmount ?? 0);
$grouped[$itemName]->discountAmount += $lineDiscountAmount;
$grouped[$itemName]->totalDiscount += $lineDiscountAmount;
$grouped[$itemName]->totalVat += (float)(($item->vatAmount ?? 0) * ($item->quantity ?? 0));
$grouped[$itemName]->totalSd += (float)(($item->sdAmount ?? 0) * ($item->quantity ?? 0));
}
}
return array_values($grouped);
}
@endphp
@if(isset($data->bannerUrl) && $data->bannerUrl != "")
@endif
|
| Name |
: |
@if(isset($data->customerModel->patient->dob))
{{$data->customerModel->patient->name}}
@endif
|
Reg |
: |
@if(isset($data->customerModel->patient->registrationNo))
{{$data->customerModel->patient->registrationNo}}
@endif
|
| Gender |
: |
@if(isset($data->customerModel->patient->gender))
{{$data->customerModel->patient->gender}}
@endif
|
Age |
: |
@if(isset($data->customerModel->patient->dob))
{{\Carbon\Carbon::parse($data->customerModel->patient->dob)->diff(\Carbon\Carbon::now())->format('%y Years, %m Months')}}
@endif
|
| Admission Date |
: |
@if(isset($data->customerModel->admissionDate))
{{dateFormatter($data->customerModel->admissionDate)}}
@endif
|
Admission ID |
: |
@if(isset($data->customerModel->admissionId))
{{($data->customerModel->admissionId)}}
@endif
|
| Bed/Cabin |
: |
@if(isset($data->customerModel->bed->bedNo))
{{($data->customerModel->bed->bedNo)}}
@endif
|
Consolidate Billing Summary
@if($data->netPayable > 0)
Draft
@else
Paid
@endif
| Total Bill Amount |
: |
{{number_format($data->totalBillAmount, 2)}} |
| Refund Amount |
: |
{{number_format($data->totalRefundAmount, 2)}} |
| Discount Amount |
: |
{{number_format($data->totalDiscountAmount, 2)}} |
| Net Bill Amount |
: |
{{number_format($data->netBillAmount, 2)}} |
| Paid Amount |
: |
{{number_format($data->paidAmount, 2)}} |
| Deposit Amount |
: |
{{number_format($data->depositAmount, 2)}} |
@if($data->netPayable >= 0)
| Payable |
: |
{{number_format($data->netPayable, 2)}} |
@endif
@if($data->netPayable < 0)
| Return |
: |
{{number_format($data->returnAmount, 2)}} |
@endif
Category Wise Summary
@php
$calculatedSummary = [];
foreach ($data->billingDetails as $billingType => $items) {
$catTotal = 0;
foreach ($items as $it) {
$catTotal += getLineNetAmount($it);
}
$calculatedSummary[] = (object)[ 'billingType' => $billingType, 'amount' => $catTotal ];
}
$zoneTotal = array_reduce($calculatedSummary, function ($carry, $c) { return $carry + ($c->amount ?? 0); }, 0);
@endphp
| SL |
Revenue Head |
Amount |
@foreach($calculatedSummary as $index => $item)
| {{$index + 1}} |
{{capitalizeFirstChar($item->billingType)}} |
{{number_format($item->amount, 2)}} |
@endforeach
|
Zone Total:
|
{{ number_format($zoneTotal, 2) }}
|
Category Wise Details
| SL |
Item Name |
Qty |
Unit Price |
Discount |
Net Amount |
@foreach($data->billingDetails as $billingType => $items)
|
{{ ucfirst(strtolower($billingType)) }}
|
@php
$sl = 1;
$categoryTotal = 0;
$groupedItems = groupItemsByName($items);
@endphp
@foreach($groupedItems as $item)
@php
$lineDiscountAmount = getLineDiscountAmount($item);
$lineNetAmount = getLineNetAmount($item);
$categoryTotal += $lineNetAmount;
@endphp
| {{ $sl++ }} |
{{ $item->productItem->name ?? $item->itemName }}
|
{{ $item->quantity }}
|
{{ number_format($item->unitPrice, 2) }}
|
{{ number_format($lineDiscountAmount, 2) }}
|
{{ number_format($lineNetAmount, 2) }}
|
@endforeach
|
{{ ucfirst(strtolower($billingType)) }} Total :
|
{{ number_format($categoryTotal, 2) }}
|
@endforeach
|
D&T - {{ now()->timezone('Asia/Dhaka')->format('d-M-Y g:i A') }}
Powered By - {{$data->poweredBy}}
{PAGENO}/{nbpg}
|