@php // Preload status codes with leads and dynamically join only the necessary tables based on conditions $leadsQuery = DB::table('lead_sales') ->leftJoin('status_codes', 'lead_sales.status', '=', 'status_codes.status_code') ->select( 'lead_sales.*', 'status_codes.status_name', 'elife_telesales_plans.plan_name as elife_telesales_plan_name', 'home_wifi_plans.plan_name as home_wifi_plan_name', 'postpaid_mnp_plans.plan_name as postpaid_mnp_plan_name', 'postpaid_new_plans.plan_name as postpaid_new_plan_name', ); // Dynamic joins based on conditions with unique aliases $leadsQuery->when( fn($query) => $query ->where('lead_sales.sim_type', 'Elife') ->whereIn('lead_sales.sale_type', ['Elife Telesales', 'Elife DTD']), fn($query) => $query->leftJoin( 'elife_plans as elife_telesales_plans', 'lead_sales.select_plan', '=', 'elife_telesales_plans.id', ), ); $leadsQuery->when( fn($query) => $query->whereIn('lead_sales.sim_type', ['HomeWifiTelesales', 'HomeWifiDTD']), fn($query) => $query->leftJoin( 'elife_plans as home_wifi_plans', 'lead_sales.select_plan', '=', 'home_wifi_plans.id', ), ); $leadsQuery->when( fn($query) => $query ->where('lead_sales.sim_type', 'MNP') ->whereIn('lead_sales.sale_type', ['Postpaid DTD', 'Postpaid Telesales']), fn($query) => $query->leftJoin( 'plans as postpaid_mnp_plans', 'lead_sales.select_plan', '=', 'postpaid_mnp_plans.id', ), ); $leadsQuery->when( fn($query) => $query->where('lead_sales.sim_type', 'New')->where('lead_sales.sale_type', 'Postpaid Telesales'), fn($query) => $query->leftJoin( 'plans as postpaid_new_plans', 'lead_sales.select_plan', '=', 'postpaid_new_plans.id', ), ); // Execute the query and get results $leads = $leadsQuery->get(); // Post-process to only show the relevant plan name based on sim_type $leads->transform(function ($lead) { switch ($lead->sim_type) { case 'Elife': $lead->display_plan_name = $lead->elife_telesales_plan_name; break; case 'HomeWifiTelesales': case 'HomeWifiDTD': $lead->display_plan_name = $lead->home_wifi_plan_name; break; case 'MNP': $lead->display_plan_name = $lead->postpaid_mnp_plan_name; break; case 'New': $lead->display_plan_name = $lead->postpaid_new_plan_name; break; default: $lead->display_plan_name = null; break; } return $lead; }); // dd($leads); @endphp @foreach ($leads as $lead_sale) @endforeach
Customer Name Gender Emirates Pay Status Pay Charges Customer Number Age Nationality Number Commitment Sim Type Select Plan Contract Commitment Benefits Lead No Selected Number Device Commitment Period Language Dob Later Date Saler Name Saler Id Monthly Payment Total Monthly Payment Remarks Emirate Id Emirate Num Etisalat Number Additional Document Mobile Payment Date Time Date Time Follow Share With Pre Check Remarks Pre Check Status Pre Check Agent Status Created At Updated At Lead Type Channel Type Sale Type Generic Id Addon Zone Flat Street Area Elife Makani Number Location Aff Number Number Test Email Num Commit Shared With Lead Date Customer Provider Start Date Start Time Elife Monthly Charges Contract Commitement Elife Address Lat Lng Assign to Closed Date Reason For Cancellation Sr Number Plan Type Promotion Offers Secondary Documents Reference Request Number Landline Account Number Alternate Mobile Number Lead Refrence Number Email Id Additional Requirements Other Requirement Lead Attempt Team Leader Id Channel Partner Name Channel Partner Id
{{ $lead_sale->customer_name }} {{ $lead_sale->gender }} {{ $lead_sale->emirates }} {{ $lead_sale->pay_status }} {{ $lead_sale->pay_charges }} {{ $lead_sale->customer_number }} {{ $lead_sale->age }} {{ $lead_sale->nationality }} {{ $lead_sale->number_commitment }} {{ $lead_sale->sim_type }} {{ $lead_sale->display_plan_name ?? 'N/A' }} {{ $lead_sale->contract_commitment }} {{ $lead_sale->benefits }} {{ $lead_sale->lead_no }} {{ $lead_sale->selected_number }} {{ $lead_sale->device }} {{ $lead_sale->commitment_period }} {{ $lead_sale->language }} {{ $lead_sale->dob }} {{ $lead_sale->later_date }} {{ $lead_sale->saler_name }} {{ $lead_sale->saler_id }} {{ $lead_sale->monthly_payment }} {{ $lead_sale->total_monthly_payment }} {{ $lead_sale->remarks }} {{ $lead_sale->emirate_id }} {{ $lead_sale->emirate_num }} {{ $lead_sale->etisalat_number }} {{ $lead_sale->additional_document }} {{ $lead_sale->mobile_payment }} {{ $lead_sale->date_time }} {{ $lead_sale->date_time_follow }} {{ $lead_sale->share_with }} {{ $lead_sale->pre_check_remarks }} {{ $lead_sale->pre_check_status }} {{ $lead_sale->pre_check_agent }} {{ $lead_sale->status_name ?? 'N/A' }} {{ $lead_sale->created_at }} {{ $lead_sale->updated_at }} {{ $lead_sale->lead_type }} {{ $lead_sale->channel_type }} {{ $lead_sale->sale_type }} {{ $lead_sale->generic_id }} {{ $lead_sale->addon }} {{ $lead_sale->zone }} {{ $lead_sale->flat }} {{ $lead_sale->street }} {{ $lead_sale->area }} {{ $lead_sale->elife_makani_number }} {{ $lead_sale->location }} {{ $lead_sale->aff_number }} {{ $lead_sale->number_test }} {{ $lead_sale->email }} {{ $lead_sale->numcommit }} {{ $lead_sale->shared_with }} {{ $lead_sale->lead_date }} {{ $lead_sale->customer_provider }} {{ $lead_sale->start_date }} {{ $lead_sale->start_time }} {{ $lead_sale->elife_monthly_charges }} {{ $lead_sale->contract_commitement_elife }} {{ $lead_sale->address }} {{ $lead_sale->lat }} {{ $lead_sale->lng }} {{ $lead_sale->assign_to }} {{ $lead_sale->closed_date }} {{ $lead_sale->reason_for_cancellation }} {{ $lead_sale->sr_number }} {{ $lead_sale->plantype }} {{ $lead_sale->promotion }} {{ $lead_sale->offers }} {{ $lead_sale->secondary_documents }} {{ $lead_sale->reference }} {{ $lead_sale->request_number }} {{ $lead_sale->landline_account_number }} {{ $lead_sale->alternate_mobile_number }} {{ $lead_sale->lead_refrence_number }} {{ $lead_sale->email_id }} {{ $lead_sale->additional_requirements }} {{ $lead_sale->other_requirement }} {{ $lead_sale->lead_attempt }} {{ $lead_sale->team_leader_id }} {{ $lead_sale->channel_partner_name }} {{ $lead_sale->channel_partner_id }}