@php
// Get all main IDs first
$mainIds = $get_outward_data->pluck('id')->toArray();
// Fetch all sub data at once
$all_sub_data = [];
if (!empty($mainIds)) {
$all_sub_data = DB::table('Outwardmodel_type1_t2s')
->whereIn('outward_id', $mainIds)
->leftJoin(
'customers',
'customers.customers',
'=',
'Outwardmodel_type1_t2s.customer_se',
)
->select(
'Outwardmodel_type1_t2s.*',
'customers.customers_name',
'customers.type',
'customers.distance',
)
->get()
->groupBy('outward_id');
}
// Helper function to clean and convert to float
function cleanNumber($value)
{
if (empty($value) || $value === null || $value === '-') {
return 0;
}
// Remove commas and any other non-numeric characters except decimal point and minus
$cleaned = preg_replace('/[^0-9.\-]/', '', str_replace(',', '', $value));
return (float) $cleaned;
}
// Initialize summary arrays
$driver_daily_summary = [];
$helper_daily_summary = [];
foreach ($get_outward_data as $data) {
$date = \Carbon\Carbon::parse($data->created_at)->format('Y-m-d');
$date_formatted = \Carbon\Carbon::parse($data->created_at)->format('d-m-Y');
$sub_items = $all_sub_data[$data->id] ?? collect([]);
// Get customer type with priority
$all_types = $sub_items->pluck('type')->filter()->unique()->all();
$type_priority = ['C', 'B', 'A'];
$selected_type = null;
foreach ($type_priority as $priority_type) {
if (in_array($priority_type, $all_types)) {
$selected_type = $priority_type;
break;
}
}
$customer_type =
$selected_type ?? ($sub_items->pluck('type')->filter()->first() ?? null);
$customer_distance = $sub_items->pluck('distance')->filter()->first() ?? 0;
$trip_no_d = $data->driver_trip_no ?? null;
$trip_no_h = $data->helper_trip_no ?? null;
$distance = intval($customer_distance);
$weight = intval($data->weight ?? 0);
// Fetch driver trip fee
$trip_fee_driver_raw = DB::table('payment_cons')
->where('type', $customer_type)
->where('trip', $trip_no_d)
->where('km_min', '<=', $distance)
->where('km_max', '>=', $distance)
->where('weight_min', '<=', $weight)
->where('weight_max', '>=', $weight)
->value('driver_amount');
$trip_fee_driver = cleanNumber($trip_fee_driver_raw);
// Fetch helper trip fee
$trip_fee_helper_raw = DB::table('payment_cons')
->where('type', $customer_type)
->where('trip', $trip_no_h)
->where('km_min', '<=', $distance)
->where('km_max', '>=', $distance)
->where('weight_min', '<=', $weight)
->where('weight_max', '>=', $weight)
->value('helper_amount');
$trip_fee_helper = cleanNumber($trip_fee_helper_raw);
// Calculate meal payments
$d_lunch_payment = 0;
$h_lunch_payment = 0;
$d_break_payment = 0;
$h_break_payment = 0;
$d_dinner_payment = 0;
$h_dinner_payment = 0;
$d_night_payment = 0;
$h_night_payment = 0;
if (!empty($data->time_in) && !empty($data->time_out)) {
if ($data->time_in >= '13:00' && $data->time_out <= '13:00') {
$d_lunch_payment = cleanNumber(
DB::table('other_payments')
->where('payment_type', 'Lunch')
->value('driver_amount'),
);
$h_lunch_payment = cleanNumber(
DB::table('other_payments')
->where('payment_type', 'Lunch')
->value('helper_amount'),
);
}
if ($data->time_in >= '8:00' && $data->time_out <= '8:00') {
$d_break_payment = cleanNumber(
DB::table('other_payments')
->where('payment_type', 'Breakfast')
->value('driver_amount'),
);
$h_break_payment = cleanNumber(
DB::table('other_payments')
->where('payment_type', 'Breakfast')
->value('helper_amount'),
);
}
if ($data->time_in >= '20:00' && $data->time_out <= '20:00') {
$d_dinner_payment = cleanNumber(
DB::table('other_payments')
->where('payment_type', 'Dinner')
->value('driver_amount'),
);
$h_dinner_payment = cleanNumber(
DB::table('other_payments')
->where('payment_type', 'Dinner')
->value('helper_amount'),
);
}
if ($data->time_in >= '00:00' && $data->time_out <= '00:00') {
$d_night_payment = cleanNumber(
DB::table('other_payments')
->where('payment_type', 'Night allowance')
->value('driver_amount'),
);
$h_night_payment = cleanNumber(
DB::table('other_payments')
->where('payment_type', 'Night allowance')
->value('helper_amount'),
);
}
}
// Calculate other payments from inward_items
$d_ballclay_payment = 0;
$h_ballclay_payment = 0;
$d_goodru_payment = 0;
$h_goodru_payment = 0;
$d_pallet_payment = 0;
$h_pallet_payment = 0;
$d_unload_payment = 0;
$h_unload_payment = 0;
if (!empty($data->inward_items)) {
$inward_items_array = array_map(
'trim',
explode(',', trim($data->inward_items)),
);
if (in_array('5', $inward_items_array)) {
$d_ballclay_payment = cleanNumber(
DB::table('other_payments')->where('id', 5)->value('driver_amount'),
);
$h_ballclay_payment = cleanNumber(
DB::table('other_payments')->where('id', 5)->value('helper_amount'),
);
}
if (in_array('6', $inward_items_array)) {
$d_goodru_payment = cleanNumber(
DB::table('other_payments')->where('id', 6)->value('driver_amount'),
);
$h_goodru_payment = cleanNumber(
DB::table('other_payments')->where('id', 6)->value('helper_amount'),
);
}
if (in_array('8', $inward_items_array)) {
$d_pallet_payment = cleanNumber(
DB::table('other_payments')->where('id', 8)->value('driver_amount'),
);
$h_pallet_payment = cleanNumber(
DB::table('other_payments')->where('id', 8)->value('helper_amount'),
);
}
if (in_array('9', $inward_items_array)) {
$d_unload_payment = cleanNumber(
DB::table('other_payments')->where('id', 9)->value('driver_amount'),
);
$h_unload_payment = cleanNumber(
DB::table('other_payments')->where('id', 9)->value('helper_amount'),
);
}
}
// Calculate totals - all values are already cleaned floats
$d_total =
$trip_fee_driver +
$d_break_payment +
$d_lunch_payment +
$d_dinner_payment +
$d_ballclay_payment +
$d_pallet_payment +
$d_goodru_payment +
$d_unload_payment +
$d_night_payment;
$h_total =
$trip_fee_helper +
$h_break_payment +
$h_lunch_payment +
$h_dinner_payment +
$h_ballclay_payment +
$h_pallet_payment +
$h_goodru_payment +
$h_unload_payment +
$h_night_payment;
// Build driver daily summary
$driver_key = $date . '_' . $data->driver;
if (!isset($driver_daily_summary[$driver_key])) {
$driver_daily_summary[$driver_key] = [
'date' => $date,
'date_formatted' => $date_formatted,
'name' => $data->d_name ?? '-',
'epf' => $data->d_epf ?? '-',
'total' => 0,
];
}
$driver_daily_summary[$driver_key]['total'] += $d_total;
// Build helper daily summary
$helper_key = $date . '_' . $data->helper;
if (!isset($helper_daily_summary[$helper_key])) {
$helper_daily_summary[$helper_key] = [
'date' => $date,
'date_formatted' => $date_formatted,
'name' => $data->h_name ?? '-',
'epf' => $data->h_epf ?? '-',
'total' => 0,
];
}
$helper_daily_summary[$helper_key]['total'] += $h_total;
}
// Sort by date descending
usort($driver_daily_summary, function ($a, $b) {
return strcmp($b['date'], $a['date']);
});
usort($helper_daily_summary, function ($a, $b) {
return strcmp($b['date'], $a['date']);
});
// Calculate grand totals
$grand_driver_total = array_sum(array_column($driver_daily_summary, 'total'));
$grand_helper_total = array_sum(array_column($helper_daily_summary, 'total'));
@endphp
Total Driver Incentives
{{ number_format($grand_driver_total, 2) }}
Total Helper Incentives
{{ number_format($grand_helper_total, 2) }}
| # |
Date |
Driver Name |
EPF No |
Daily Total |
@forelse ($driver_daily_summary as $index => $driver)
| {{ $index + 1 }} |
{{ $driver['date_formatted'] }} |
{{ $driver['name'] }} |
{{ $driver['epf'] }} |
{{ number_format($driver['total'], 2) }} |
@empty
| No data
available |
@endforelse
| Grand Total: |
{{ number_format($grand_driver_total, 2) }} |
| # |
Date |
Helper Name |
EPF No |
Daily Total |
@forelse ($helper_daily_summary as $index => $helper)
| {{ $index + 1 }} |
{{ $helper['date_formatted'] }} |
{{ $helper['name'] }} |
{{ $helper['epf'] }} |
{{ number_format($helper['total'], 2) }} |
@empty
| No data
available |
@endforelse
| Grand Total: |
{{ number_format($grand_helper_total, 2) }} |
| # |
Date |
Driver Name |
Driver EPF |
Driver Daily Total |
Helper Name |
Helper EPF |
Helper Daily Total |
@php
$max_rows = max(
count($driver_daily_summary),
count($helper_daily_summary),
);
$driver_arr = array_values($driver_daily_summary);
$helper_arr = array_values($helper_daily_summary);
@endphp
@for ($i = 0; $i < $max_rows; $i++)
| {{ $i + 1 }} |
{{ $driver_arr[$i]['date_formatted'] ?? ($helper_arr[$i]['date_formatted'] ?? '-') }}
|
{{ $driver_arr[$i]['name'] ?? '-' }} |
{{ $driver_arr[$i]['epf'] ?? '-' }} |
{{ isset($driver_arr[$i]) ? number_format($driver_arr[$i]['total'], 2) : '-' }}
|
{{ $helper_arr[$i]['name'] ?? '-' }} |
{{ $helper_arr[$i]['epf'] ?? '-' }} |
{{ isset($helper_arr[$i]) ? number_format($helper_arr[$i]['total'], 2) : '-' }}
|
@endfor
| Grand Totals: |
{{ number_format($grand_driver_total, 2) }}
|
|
{{ number_format($grand_helper_total, 2) }}
|