@extends('layouts.app') @section('title', 'Incentive Report') @push('styles') @endpush @section('content')

Incentive Details Report

@include('common.alerts')
@php function ordinal($number) { if (!$number) { return '-'; } $ends = ['th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th']; if ($number % 100 >= 11 && $number % 100 <= 13) { return $number . 'th'; } return $number . $ends[$number % 10]; } // Get all main IDs first $mainIds = $get_outward_data->pluck('id')->toArray(); // Fetch all sub data at once (outside the loop for better performance) $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'); } $previous_date = null; @endphp
@php $grand_driver_total = 0; $grand_helper_total = 0; @endphp @forelse ($get_outward_data as $key => $data) @php $sub_items = $all_sub_data[$data->id] ?? collect([]); $customer_names = $sub_items ->pluck('customers_name') ->filter() ->implode('/ '); // 1. Get all unique types $all_types = $sub_items->pluck('type')->filter()->unique()->all(); // 2. Define priority (C > B > A). Add other types if needed. $type_priority = ['C', 'B', 'A']; $selected_type = null; // 3. Find the highest-ranking type foreach ($type_priority as $priority_type) { if (in_array($priority_type, $all_types)) { $selected_type = $priority_type; break; // Found the highest priority type, stop searching } } // Use the selected type for the lookup, or default to the original logic if no priority type is found (e.g., if you have other types like 'D', 'E', etc.) $customer_type = $selected_type ?? ($sub_items->pluck('type')->filter()->first() ?? null); $customer_distance = $sub_items ->pluck('distance') ->filter() ->implode('/ '); $aod = $sub_items->pluck('aod_td')->filter()->implode('/ '); $trip_no_d = $data->driver_trip_no ?? null; $trip_no_h = $data->helper_trip_no ?? null; $distance = intval($customer_distance ?? 0); $weight = intval($data->weight ?? 0); // Fetch driver amount $trip_fee_driver = 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'); // returns single value // Fetch helper amount $trip_fee_helper = 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'); $lunch_hour = '13:00'; $d_lunch_payment = 0; // Initialize lunch payment if (!empty($data->time_in) && !empty($data->time_out)) { if ( $data->time_in >= $lunch_hour && $data->time_out <= $lunch_hour ) { $d_lunch_payment = DB::table('other_payments') ->where('payment_type', 'Lunch') ->value('driver_amount'); } } $d_lunch = $d_lunch_payment > 0 ? $d_lunch_payment : '-'; $h_lunch_payment = 0; // Initialize lunch payment if (!empty($data->time_in) && !empty($data->time_out)) { if ( $data->time_in >= $lunch_hour && $data->time_out <= $lunch_hour ) { $h_lunch_payment = DB::table('other_payments') ->where('payment_type', 'Lunch') ->value('helper_amount'); } } $h_lunch = $h_lunch_payment > 0 ? $h_lunch_payment : '-'; $break_hour = '8:00'; $d_break_payment = 0; // Initialize break payment if (!empty($data->time_in) && !empty($data->time_out)) { if ( $data->time_in >= $break_hour && $data->time_out <= $break_hour ) { $d_break_payment = DB::table('other_payments') ->where('payment_type', 'Breakfast') ->value('driver_amount'); } } $d_break = $d_break_payment > 0 ? $d_break_payment : '-'; $h_break_payment = 0; // Initialize break payment if (!empty($data->time_in) && !empty($data->time_out)) { if ( $data->time_in >= $break_hour && $data->time_out <= $break_hour ) { $h_break_payment = DB::table('other_payments') ->where('payment_type', 'Breakfast') ->value('helper_amount'); } } $h_break = $h_break_payment > 0 ? $h_break_payment : '-'; $dinner_hour = '20:00'; $dinner_start = '20:00'; $dinner_end = '00:00'; // midnight $d_dinner_payment = 0; // Initialize dinner payment if (!empty($data->time_in) && !empty($data->time_out)) { $timeIn = strtotime($data->time_in); $timeOut = strtotime($data->time_out); // Convert dinner start to today $dinnerStartTime = strtotime($dinner_start); $dinnerEndTime = strtotime($dinner_end); // Handle the case where dinner_end is after midnight if ($dinnerEndTime < $dinnerStartTime) { $dinnerEndTime += 24 * 60 * 60; // add 24 hours if ($timeOut < $timeIn) { $timeOut += 24 * 60 * 60; // add 24 hours if shift goes past midnight } } // Check if any part of the shift overlaps dinner time if ($timeIn < $dinnerEndTime && $timeOut > $dinnerStartTime) { $d_dinner_payment = DB::table('other_payments') ->where('payment_type', 'Dinner') ->value('driver_amount'); } } $d_dinner = $d_dinner_payment > 0 ? $d_dinner_payment : '-'; $h_dinner_payment = 0; // Initialize dinner payment if (!empty($data->time_in) && !empty($data->time_out)) { if ( $data->time_in >= $dinner_hour && $data->time_out <= $dinner_hour ) { $h_dinner_payment = DB::table('other_payments') ->where('payment_type', 'Dinner') ->value('helper_amount'); } } $h_dinner = $h_dinner_payment > 0 ? $h_dinner_payment : '-'; $night_hour = '00:00'; $d_night_payment = 0; // Initialize night payment if (!empty($data->time_in) && !empty($data->time_out)) { if ( $data->time_in >= $night_hour && $data->time_out <= $night_hour ) { $d_night_payment = DB::table('other_payments') ->where('payment_type', 'Night allowance') ->value('driver_amount'); } } $d_night = $d_night_payment > 0 ? $d_night_payment : '-'; $h_night_payment = 0; // Initialize night payment if (!empty($data->time_in) && !empty($data->time_out)) { if ( $data->time_in >= $night_hour && $data->time_out <= $night_hour ) { $h_night_payment = DB::table('other_payments') ->where('payment_type', 'Night allowance') ->value('helper_amount'); } } $h_night = $h_night_payment > 0 ? $h_night_payment : '-'; $h_ballclay_payment = 0; if (!empty($data->inward_items)) { $items_string = trim($data->inward_items); $inward_items_array = array_map( 'trim', explode(',', $items_string), ); $target_item = '5'; if (in_array($target_item, $inward_items_array)) { $h_ballclay_payment = DB::table('other_payments') ->where('id', $target_item) ->value('helper_amount'); } } $h_ball_clay = $h_ballclay_payment > 0 ? $h_ballclay_payment : '-'; $d_ballclay_payment = 0; if (!empty($data->inward_items)) { $items_string = trim($data->inward_items); $inward_items_array = array_map( 'trim', explode(',', $items_string), ); $target_item = '5'; if (in_array($target_item, $inward_items_array)) { $d_ballclay_payment = DB::table('other_payments') ->where('id', $target_item) ->value('driver_amount'); } } $d_ball_clay = $d_ballclay_payment > 0 ? $d_ballclay_payment : '-'; $h_goodru_payment = 0; if (!empty($data->inward_items)) { $items_string = trim($data->inward_items); $inward_items_array = array_map( 'trim', explode(',', $items_string), ); $target_item = '6'; if (in_array($target_item, $inward_items_array)) { $h_goodru_payment = DB::table('other_payments') ->where('id', $target_item) ->value('helper_amount'); } } $h_goodru = $h_goodru_payment > 0 ? $h_goodru_payment : '-'; $d_goodru_payment = 0; if (!empty($data->inward_items)) { $items_string = trim($data->inward_items); $inward_items_array = array_map( 'trim', explode(',', $items_string), ); $target_item = '6'; if (in_array($target_item, $inward_items_array)) { $d_goodru_payment = DB::table('other_payments') ->where('id', $target_item) ->value('driver_amount'); } } $d_goodru = $d_goodru_payment > 0 ? $d_goodru_payment : '-'; $h_pallet_payment = 0; if (!empty($data->inward_items)) { $items_string = trim($data->inward_items); $inward_items_array = array_map( 'trim', explode(',', $items_string), ); $target_item = '8'; if (in_array($target_item, $inward_items_array)) { $h_pallet_payment = DB::table('other_payments') ->where('id', $target_item) ->value('helper_amount'); } } $h_pallet = $h_pallet_payment > 0 ? $h_pallet_payment : '-'; $d_pallet_payment = 0; if (!empty($data->inward_items)) { $items_string = trim($data->inward_items); $inward_items_array = array_map( 'trim', explode(',', $items_string), ); $target_item = '8'; if (in_array($target_item, $inward_items_array)) { $d_pallet_payment = DB::table('other_payments') ->where('id', $target_item) ->value('driver_amount'); } } $d_pallet = $d_pallet_payment > 0 ? $d_pallet_payment : '-'; $h_unload_payment = 0; if (!empty($data->inward_items)) { $items_string = trim($data->inward_items); $inward_items_array = array_map( 'trim', explode(',', $items_string), ); $target_item = '9'; if (in_array($target_item, $inward_items_array)) { $h_unload_payment = DB::table('other_payments') ->where('id', $target_item) ->value('helper_amount'); } } $h_unload = $h_unload_payment > 0 ? $h_unload_payment : '-'; $d_unload_payment = 0; if (!empty($data->inward_items)) { $items_string = trim($data->inward_items); $inward_items_array = array_map( 'trim', explode(',', $items_string), ); $target_item = '9'; if (in_array($target_item, $inward_items_array)) { $d_unload_payment = DB::table('other_payments') ->where('id', $target_item) ->value('driver_amount'); } } $d_unload = $d_unload_payment > 0 ? $d_unload_payment : '-'; // Calculate Totals $trip_fee_driver_cleaned = str_replace( ',', '', $trip_fee_driver ?? 0, ); $d_break_payment_cleaned = str_replace(',', '', $d_break_payment); $d_lunch_payment_cleaned = str_replace(',', '', $d_lunch_payment); $d_dinner_payment_cleaned = str_replace(',', '', $d_dinner_payment); $d_night_payment_cleaned = str_replace(',', '', $d_night_payment); $d_ballclay_payment_cleaned = str_replace( ',', '', $d_ballclay_payment, ); $d_pallet_payment_cleaned = str_replace(',', '', $d_pallet_payment); $d_goodru_payment_cleaned = str_replace(',', '', $d_goodru_payment); $d_unload_payment_cleaned = str_replace(',', '', $d_unload_payment); $d_total = (float) $trip_fee_driver_cleaned + (float) $d_break_payment_cleaned + (float) $d_lunch_payment_cleaned + (float) $d_dinner_payment_cleaned + (float) $d_ballclay_payment_cleaned + (float) $d_pallet_payment_cleaned + (float) $d_goodru_payment_cleaned + (float) $d_unload_payment_cleaned + (float) $d_night_payment_cleaned; $trip_fee_helper_cleaned = str_replace( ',', '', $trip_fee_helper ?? 0, ); $h_break_payment_cleaned = str_replace(',', '', $h_break_payment); $h_lunch_payment_cleaned = str_replace(',', '', $h_lunch_payment); $h_dinner_payment_cleaned = str_replace(',', '', $h_dinner_payment); $h_night_payment_cleaned = str_replace(',', '', $h_night_payment); $h_ballclay_payment_cleaned = str_replace( ',', '', $h_ballclay_payment, ); $h_pallet_payment_cleaned = str_replace(',', '', $h_pallet_payment); $h_goodru_payment_cleaned = str_replace(',', '', $h_goodru_payment); $h_unload_payment_cleaned = str_replace(',', '', $h_unload_payment); $h_total = (float) $trip_fee_helper_cleaned + (float) $h_break_payment_cleaned + (float) $h_lunch_payment_cleaned + (float) $h_dinner_payment_cleaned + (float) $h_ballclay_payment_cleaned + (float) $h_pallet_payment_cleaned + (float) $h_goodru_payment_cleaned + (float) $h_unload_payment_cleaned + (float) $h_night_payment_cleaned; $grand_driver_total += $d_total; $grand_helper_total += $h_total; @endphp {{-- Main Row --}} @empty @endforelse
# Date OUT No AOD No Customer Names D/Trip Driver D/EPF No H/Trip Helper H/EPF NO Vehicle D Trip Fee H Trip Fee D Breakfast H Breakfast D Lunch H Lunch D Dinner H Dinner D B.C./5MT H B.C./5MT D Good Return H Good Return D Pallet H Pallet D Unload H Unload D N Allowance H N Allowance D Total H Total
{{ $key + 1 }} {{ \Carbon\Carbon::parse($data->created_at)->format('d-m-Y') }} {{ $data->outward_number ?? '-' }} {{ $aod ?: '-' }} {{ $customer_names ?: '-' }} {{ ordinal($data->driver_trip_no ?? null) }} {{ $data->d_name ?? '-' }} {{ $data->d_epf ?? '-' }} {{ ordinal($data->helper_trip_no ?? null) }} {{ $data->h_name ?? '-' }} {{ $data->h_epf ?? '-' }} {{ $data->vehicle_no ?? '-' }} {{ isset($data->v_type) ? "({$data->v_type})" : '' }} {{ $trip_fee_driver ?? '-' }} {{ $trip_fee_helper ?? '-' }} {{ $d_break ?? '-' }} {{ $h_break ?? '-' }} {{ $d_lunch ?? '-' }} {{ $h_lunch ?? '-' }} {{ $d_dinner ?? '-' }} {{ $h_dinner ?? '-' }} {{ $d_ball_clay ?? '-' }} {{ $h_ball_clay ?? '-' }} {{ $d_goodru ?? '-' }} {{ $h_goodru ?? '-' }} {{ $d_pallet ?? '-' }} {{ $h_pallet ?? '-' }} {{ $d_unload ?? '-' }} {{ $h_unload ?? '-' }} {{ $d_night ?? '-' }} {{ $h_night ?? '-' }} {{ number_format($d_total, 2, '.', ',') ?? '-' }} {{ number_format($h_total, 2, '.', ',') ?? '-' }}
No data available
Grand Total {{ number_format($grand_driver_total, 2, '.', ',') }} {{ number_format($grand_helper_total, 2, '.', ',') }}
@if (method_exists($get_outward_data, 'links')) {{ $get_outward_data->onEachSide(1)->links('pagination::bootstrap-5') }} @endif
@endsection @push('scripts') @endpush