where('available', 1)->whereNull('deleted_at')->pluck('name','id')->toArray(); $cost_centers = ['OPD' => 'OPD'] + $cost_centers; $cost_centers = ['all_cost_centers' => 'ALL COST CENTERS'] + $cost_centers; $cost_centers = ['' => '- select -'] + $cost_centers; return view('finance::cost_center_performance.index',compact('cost_centers','request')); } public function store(Request $request) { /* Note: remember to ignore test patients 1. get the expenses that are used by the cost center 2. get the money that they have made 3. See how much they have made */ $cost_centers = DB::table('wards')->where('available', 1)->whereNull('deleted_at')->pluck('name','id')->toArray(); $cost_centers = ['OPD' => 'OPD'] + $cost_centers; $cost_centers = ['all_cost_centers' => 'ALL COST CENTERS'] + $cost_centers; $cost_centers = ['' => '- select -'] + $cost_centers; $inpatient_bills_cash_array = []; $ward_inpatient_array_of_collections = []; $per_wards_total_amounts_array = []; $total_opd_debt_payments = 0; $display = ""; if (isset($request)) { if ($request->dates == "custom_date_range") { $end = Carbon::parse($request->end_date)->endOfDay()->toDateTimeString(); $start = Carbon::parse($request->start_date)->startOfDay()->toDateTimeString(); } else if($request->dates == "custom_date"){ $end = Carbon::parse($request->start_date)->endOfDay()->toDateTimeString(); $start = Carbon::parse($request->start_date)->startOfDay()->toDateTimeString(); } else if($request->dates == "yesterday") { $end = Carbon::yesterday()->endOfDay()->toDateTimeString(); $start = Carbon::yesterday()->startOfDay()->toDateTimeString(); } else { // includes today $end = Carbon::today()->endOfDay()->toDateTimeString(); $start = Carbon::today()->startOfDay()->toDateTimeString(); } $ward_id = $request->cost_center; //start get the payments from all debts invoices,donor invoices, debt_plan $debt_amount_paid = 0; $invoice_payments_array = $this->getCostCenterInvoicePayments('invoice_payments', 'amount_paid_history', $request); if (!empty($invoice_payments_array)) { foreach ($invoice_payments_array as $invoice_payment) { $debt_amount_paid += $invoice_payment->amount_paid; } } $donor_invoices_array = $this->getCostCenterInvoicePayments('donor_invoice_payments', 'amount_paid_history', $request); if (!empty($donor_invoices_array)) { foreach ($donor_invoices_array as $donor_payment) { $debt_amount_paid += $donor_payment->amount_paid; } } $debt_plan_payment_staffs_array = $this->getCostCenterInvoicePayments('debt_plan_payment_staffs', 'amount_paid_history', $request); if (!empty($debt_plan_payment_staffs_array)) { foreach ($debt_plan_payment_staffs_array as $debt_plan_payment) { $debt_amount_paid += $debt_plan_payment->amount_paid; } } $debtor_payments = $this->getCostCenterDebtorPaymentsSum('debtor_payments', 'amount_paid', $request); $total_opd_debt_payments = $debt_amount_paid + $debtor_payments; //end payments of all debt plans if ($request->cost_center == "all_cost_centers") { foreach ($cost_centers as $key => $value) { $inpatient_bills = DB::table('service_deposits') ->join('inpatient_info','inpatient_info.episode_id', '=', 'service_deposits.episode_id') ->where('inpatient_info.ward_id', $key) ->whereNotIn('inpatient_info.patient_id', findTestOrDemoPatients()) ->whereBetween('service_deposits.created_at',[$start, $end]) ->where('service_deposits.service_type','Inpatient_Deposit') ->select('service_deposits.*','inpatient_info.ward_id') ->get(); if (count($inpatient_bills) > 0) { $ward_inpatient_array_of_collections[$key] = $inpatient_bills; } else { $ward_inpatient_array_of_collections[$key] = []; } } //add OPD collections to the array $treatment_deposits = TreatmentDeposits::whereBetween('created_at',[$start, $end])->whereNotIn('patient_id', findTestOrDemoPatients())->get(); $ward_inpatient_array_of_collections["OPD"][] = $treatment_deposits; $investigation_deposits = InvestigationDeposit::whereBetween('created_at',[$start, $end])->whereNotIn('patient_id', findTestOrDemoPatients())->get(); $ward_inpatient_array_of_collections["OPD"][] = $investigation_deposits; $procedures_deposits = ProcedureDeposit::whereBetween('created_at',[$start, $end])->whereNotIn('patient_id', findTestOrDemoPatients())->get(); $ward_inpatient_array_of_collections["OPD"][] = $procedures_deposits; $sundries_deposits = SundryDeposit::whereBetween('created_at',[$start, $end])->whereNotIn('patient_id', findTestOrDemoPatients())->get(); $ward_inpatient_array_of_collections["OPD"][] = $sundries_deposits; $service_deposits = ServiceDeposit::where('service_type', 'Services')->whereBetween('created_at',[$start, $end])->whereNotIn('patient_id', findTestOrDemoPatients())->get(); $ward_inpatient_array_of_collections["OPD"][] = $service_deposits; $consultation_deposits = ServiceDeposit::where('service_type','Consultation')->whereBetween('created_at',[$start, $end])->whereNotIn('patient_id', findTestOrDemoPatients())->get(); $ward_inpatient_array_of_collections["OPD"][] = $consultation_deposits; }else if ($request->cost_center == "OPD") { //add OPD collections to the array $treatment_deposits = TreatmentDeposits::whereBetween('created_at',[$start, $end])->whereNotIn('patient_id', findTestOrDemoPatients())->get(); $ward_inpatient_array_of_collections["OPD"][] = $treatment_deposits; $investigation_deposits = InvestigationDeposit::whereBetween('created_at',[$start, $end])->whereNotIn('patient_id', findTestOrDemoPatients())->get(); $ward_inpatient_array_of_collections["OPD"][] = $investigation_deposits; $procedures_deposits = ProcedureDeposit::whereBetween('created_at',[$start, $end])->whereNotIn('patient_id', findTestOrDemoPatients())->get(); $ward_inpatient_array_of_collections["OPD"][] = $procedures_deposits; $sundries_deposits = SundryDeposit::whereBetween('created_at',[$start, $end])->whereNotIn('patient_id', findTestOrDemoPatients())->get(); $ward_inpatient_array_of_collections["OPD"][] = $sundries_deposits; $service_deposits = ServiceDeposit::where('service_type', 'Services')->whereBetween('created_at',[$start, $end])->whereNotIn('patient_id', findTestOrDemoPatients())->get(); $ward_inpatient_array_of_collections["OPD"][] = $service_deposits; $consultation_deposits = ServiceDeposit::where('service_type','Consultation')->whereBetween('created_at',[$start, $end])->whereNotIn('patient_id', findTestOrDemoPatients())->get(); $ward_inpatient_array_of_collections["OPD"][] = $consultation_deposits; } else { $inpatient_bills = DB::table('service_deposits') ->join('inpatient_info','inpatient_info.episode_id', '=', 'service_deposits.episode_id') ->where('inpatient_info.ward_id', $request->cost_center) ->whereNotIn('inpatient_info.patient_id', findTestOrDemoPatients()) ->whereBetween('service_deposits.created_at',[$start, $end]) ->where('service_deposits.service_type','Inpatient_Deposit') ->select('service_deposits.*','inpatient_info.ward_id') ->get(); $subtotal = 0; if (count($inpatient_bills) > 0) { foreach ($inpatient_bills as $bill_with_treatment_details) { $subtotal += $bill_with_treatment_details->patient_amount_paid; } } $per_wards_total_amounts_array[$ward_id] = $subtotal; } //do this for specifically when all cost_centers option is selected if (!empty($ward_inpatient_array_of_collections)) { foreach($ward_inpatient_array_of_collections as $ward_id => $inpatient_bill_details_collections){ $particular_ward_amounts = 0; foreach ($inpatient_bill_details_collections as $bill_record) { if ($ward_id == "OPD") { // loop through the "opd" key collection foreach ($bill_record as $record) { $particular_ward_amounts += $record->patient_amount_paid; } } else { $particular_ward_amounts += $bill_record->patient_amount_paid; } } $per_wards_total_amounts_array[$ward_id] = $particular_ward_amounts; } } $display = dateLabelSetter($request); } return view('finance::cost_center_performance.index',compact('cost_centers','request','display','inpatient_bills_cash_array','per_wards_total_amounts_array','total_opd_debt_payments')); } /* display the over view of the performance of the cost centers */ public function over_view_perfomance(Request $request) { # code.. } public function cost_center_details(Request $request) { if ($request->date_type == "today") { $end = Carbon::today()->endOfDay()->toDateTimeString(); $start = Carbon::today()->startOfDay()->toDateTimeString(); } else if($request->date_type == "yesterday"){ $end = Carbon::yesterday()->endOfDay()->toDateTimeString(); $start = Carbon::yesterday()->startOfDay()->toDateTimeString(); } else if($request->date_type == "custom_date"){ $dates = explode("/", $request->dates); $end = Carbon::parse($dates[0])->endOfDay()->toDateTimeString(); $start = Carbon::parse($dates[0])->startOfDay()->toDateTimeString(); }else if($request->date_type == "custom_date_range"){ $dates = explode("/", $request->dates); $end = Carbon::parse($dates[1])->endOfDay()->toDateTimeString(); $start = Carbon::parse($dates[0])->startOfDay()->toDateTimeString(); } $inpatient_record_results = []; if ($request->ward_id == "OPD") { //add OPD collections to the array $treatment_deposits = TreatmentDeposits::whereBetween('created_at',[$start, $end])->whereNotIn('patient_id', findTestOrDemoPatients())->get(); $ward_inpatient_array_of_collections["OPD"][] = $treatment_deposits; $investigation_deposits = InvestigationDeposit::whereBetween('created_at',[$start, $end])->whereNotIn('patient_id', findTestOrDemoPatients())->get(); $ward_inpatient_array_of_collections["OPD"][] = $investigation_deposits; $procedures_deposits = ProcedureDeposit::whereBetween('created_at',[$start, $end])->whereNotIn('patient_id', findTestOrDemoPatients())->get(); $ward_inpatient_array_of_collections["OPD"][] = $procedures_deposits; $sundries_deposits = SundryDeposit::whereBetween('created_at',[$start, $end])->whereNotIn('patient_id', findTestOrDemoPatients())->get(); $ward_inpatient_array_of_collections["OPD"][] = $sundries_deposits; $consultation_deposits = ServiceDeposit::whereBetween('created_at',[$start, $end])->whereNotIn('patient_id', findTestOrDemoPatients())->where('service_type','Consultation')->get(); $ward_inpatient_array_of_collections["OPD"][] = $consultation_deposits; $service_deposits = ServiceDeposit::whereBetween('created_at',[$start, $end])->whereNotIn('patient_id', findTestOrDemoPatients())->where('service_type','Services')->get(); $ward_inpatient_array_of_collections["OPD"][] = $service_deposits; $optic_deposits = EyeGlassesDeposits::whereBetween('created_at',[$start, $end])->whereNotIn('patient_id', findTestOrDemoPatients())->get(); } else { $inpatient_bills = DB::table('service_deposits') ->join('inpatient_info','inpatient_info.episode_id', '=', 'service_deposits.episode_id') ->where('inpatient_info.ward_id', $request->ward_id) ->whereBetween('service_deposits.created_at',[$start, $end]) ->where('service_deposits.service_type','Inpatient_Deposit') ->select('service_deposits.*','inpatient_info.ward_id') ->get(); } $drugs = []; $consultations = []; $sundries = []; $investigations = []; $procedures = []; $extras = []; $tta_cost = []; $services = []; $optics = []; if ($request->ward_id == "OPD") { foreach ($treatment_deposits as $treament) { $drugs[] = $treament->patient_amount_paid; } foreach ($investigation_deposits as $investigation) { $investigations[] = $investigation->patient_amount_paid; } foreach ($procedures_deposits as $procedure) { $procedures[] = $procedure->patient_amount_paid; } foreach ($sundries_deposits as $sundry) { $sundries[] = $sundry->patient_amount_paid; } foreach ($consultation_deposits as $service) { $consultations[] = $service->patient_amount_paid; } foreach ($service_deposits as $service) { $services[] = $service->patient_amount_paid; } foreach ($optic_deposits as $optic) { $optics[] = $optic->patient_amount_paid; } } else { $inpatient_record_results = $inpatient_bills; /*foreach ($inpatient_bills as $inpatient_bill_and_info_detail) { $inpatient_deposit = $inpatient_bill_and_info_detail->patient_amount_paid; $drugs[] = $inpatient_bill_and_info_detail->treatment_cost; $consultations[] = $inpatient_bill_and_info_detail->services_cost; $sundries[] = $inpatient_bill_and_info_detail->sundries_cost; $investigations[] = $inpatient_bill_and_info_detail->investigation_cost; $tta_cost[] = $inpatient_bill_and_info_detail->tta_cost; $procedures[] = $inpatient_bill_and_info_detail->procedures_cost; $extras[] = $inpatient_bill_and_info_detail->extras_cost; } return ;*/ } $ward_name = $request->ward_id == "OPD" ? "OPD" : get_name($request->ward_id, 'id', 'name', 'wards'); $cost_centers = DB::table('wards')->where('available', 1)->whereNull('deleted_at')->pluck('name','id')->toArray(); $cost_centers = ['all_cost_centers' => 'ALL COST CENTERS'] + $cost_centers; $cost_centers = ['' => '- select -'] + $cost_centers; return view('finance::cost_center_performance.center_performance_details',compact('request','drugs','consultations','sundries','investigations','tta_cost','procedures','services','extras','cost_centers','ward_name','inpatient_record_results', 'optics')); } public function costCenterIncomeDetailedReport(Request $request){ if (isset($request->per_staff_details)) { /* if it is viewing all collection details from particular staff then go to this function */ return $this->incomeCashDetailedReportOfParticularStaff($request); } $result = []; $inv_lab = []; $inv_ultrasound = []; $inv_x_ray = []; $discount = ""; $patient = ""; $staff_member = ""; $group = ""; $pending_donor_invoices = ""; $pending_invoices = ""; $debtors = ""; $debt_plan = ""; $patient_category_payments = []; $donor_invoice_payments = []; $debt_plan_payments = []; $debtor_payments = []; $donor_amount_paid = 0; $today = Carbon::today()->toDateString(); $yesterday = Carbon::yesterday()->toDateString(); if($request->date_type == "yesterday"){ $end = Carbon::yesterday()->endOfDay()->toDateTimeString(); $start = Carbon::yesterday()->startOfDay()->toDateTimeString(); }else if($request->date_type == "custom_date"){ $dates = explode("/", $request->dates); $end = Carbon::parse($dates[0])->endOfDay()->toDateTimeString(); $start = Carbon::parse($dates[0])->startOfDay()->toDateTimeString(); } else if($request->date_type == "custom_date_range"){ $dates = explode("/", $request->dates); $end = Carbon::parse($dates[1])->endOfDay()->toDateTimeString(); $start = Carbon::parse($dates[0])->startOfDay()->toDateTimeString(); } else { // includes today $end = Carbon::today()->endOfDay()->toDateTimeString(); $start = Carbon::today()->startOfDay()->toDateTimeString(); } $staff_members = DB::table('users')->pluck('username','id')->toArray(); if($request->comparator == "CON"){ $result = DB::table('service_deposits') ->whereNotIn('patient_id', findTestOrDemoPatients()) ->where('service_type', 'Consultation') ->whereBetween('created_at', [$start, $end]) ->groupBy('receipt_number') ->get() ->toArray(); foreach ($result as $item){ $donor_amount_paid = get_name($item->receipt_number, 'receipt_number', 'donor_to_pay', 'donor_discount_details'); $discount = get_name($item->receipt_number, 'receipt_number', 'discount_amount', 'discounts'); $patient = DB::table('patients')->where('id', $item->patient_id)->select('first_name', 'last_name')->get()->toArray(); $staff_member = DB::table('users')->where('id', $item->created_by)->select('first_name', 'last_name')->get()->toArray(); } } if($request->comparator == "OTH"){ $result = DB::table('service_deposits') ->whereNotIn('patient_id', findTestOrDemoPatients()) ->whereIn('service_type', ['Services', 'Other Services', 'Service']) ->whereBetween('created_at', [$start, $end]) ->groupBy('receipt_number') ->get() ->toArray(); foreach ($result as $item){ $donor_amount_paid = get_name($item->receipt_number, 'receipt_number', 'donor_to_pay', 'donor_discount_details'); $discount = get_name($item->receipt_number, 'receipt_number', 'discount_amount', 'discounts'); $patient = DB::table('patients')->where('id', $item->patient_id)->select('first_name', 'last_name')->get()->toArray(); $staff_member = DB::table('users')->where('id', $item->created_by)->select('first_name', 'last_name')->get()->toArray(); } } if($request->comparator == "IA"){ $result = DB::table('investigation_deposits') ->whereNotIn('patient_id', findTestOrDemoPatients()) ->whereBetween('created_at', [$start, $end]) ->groupBy('receipt_number') ->get() ->toArray(); $sorted = $this->incomeInvestigationDetail($result); $inv_lab = $sorted[0]; $inv_x_ray = $sorted[1]; $inv_ultrasound = $sorted[2]; foreach ($result as $item){ $donor_amount_paid = get_name($item->receipt_number, 'receipt_number', 'donor_to_pay', 'donor_discount_details'); $discount = get_name($item->receipt_number, 'receipt_number', 'discount_amount', 'discounts'); $patient = DB::table('patients')->where('id', $item->patient_id)->select('first_name', 'last_name')->get()->toArray(); $staff_member = DB::table('users')->where('id', $item->created_by)->select('first_name', 'last_name')->get()->toArray(); } } if($request->comparator == "IC"){ $result = DB::table('investigation_deposits') ->whereNotIn('patient_id', findTestOrDemoPatients()) ->whereBetween('created_at', [$start, $end]) ->groupBy('receipt_number') ->get() ->toArray(); $sorted = $this->incomeInvestigationDetail($result); $inv_lab = $sorted[0]; $inv_x_ray = $sorted[1]; $inv_ultrasound = $sorted[2]; foreach ($result as $item){ $donor_amount_paid = get_name($item->receipt_number, 'receipt_number', 'donor_to_pay', 'donor_discount_details'); $discount = get_name($item->receipt_number, 'receipt_number', 'discount_amount', 'discounts'); $patient = DB::table('patients')->where('id', $item->patient_id)->select('first_name', 'last_name')->get()->toArray(); $staff_member = DB::table('users')->where('id', $item->created_by)->select('first_name', 'last_name')->get()->toArray(); } } if($request->comparator == "DR"){ $result = DB::table('treatment_deposits') ->whereNotIn('patient_id', findTestOrDemoPatients()) ->whereBetween('created_at', [$start, $end]) ->groupBy('receipt_number') ->get() ->toArray(); foreach ($result as $item){ $donor_amount_paid = get_name($item->receipt_number, 'receipt_number', 'donor_to_pay', 'donor_discount_details'); $discount = get_name($item->receipt_number, 'receipt_number', 'discount_amount', 'discounts'); $patient = DB::table('patients')->where('id', $item->patient_id)->select('first_name', 'last_name')->get()->toArray(); $staff_member = DB::table('users')->where('id', $item->created_by)->select('first_name', 'last_name')->get()->toArray(); } } if($request->comparator == "CO"){ $result = DB::table('service_deposits') ->whereNotIn('patient_id', findTestOrDemoPatients()) ->where('service_type', 'Co_Payment') ->whereBetween('created_at', [$start, $end]) ->groupBy('receipt_number') ->get() ->toArray(); foreach ($result as $item){ $donor_amount_paid = get_name($item->receipt_number, 'receipt_number', 'donor_to_pay', 'donor_discount_details'); $discount = get_name($item->receipt_number, 'receipt_number', 'discount_amount', 'discounts'); $patient = DB::table('patients')->where('id', $item->patient_id)->select('first_name', 'last_name')->get()->toArray(); $staff_member = DB::table('users')->where('id', $item->created_by)->select('first_name', 'last_name')->get()->toArray(); } } if($request->comparator == "ID"){ $result = DB::table('service_deposits') ->whereNotIn('patient_id', findTestOrDemoPatients()) ->where('service_type', 'Inpatient_Deposit') ->whereBetween('created_at', [$start, $end]) ->groupBy('receipt_number') ->get() ->toArray(); return view('finance_reports::finance_reports.detail.inpatient_deposits', compact('result', 'request')); } if($request->comparator == "IBP"){ $result = DB::table('inpatient_bills') ->whereNotIn('patient_id', findTestOrDemoPatients()) ->whereBetween('created_at', [$start, $end]) ->groupBy('receipt_id') ->get() ->toArray(); return view('finance_reports::finance_reports.detail.inpatient_bill_payment', compact('result', 'request')); } if($request->comparator == "PR"){ $result = DB::table('procedure_deposits') ->whereNotIn('patient_id', findTestOrDemoPatients()) ->whereBetween('created_at', [$start, $end]) ->groupBy('receipt_number') ->get() ->toArray(); foreach ($result as $item){ $donor_amount_paid = get_name($item->receipt_number, 'receipt_number', 'donor_to_pay', 'donor_discount_details'); $discount = get_name($item->receipt_number, 'receipt_number', 'discount_amount', 'discounts'); $patient = DB::table('patients')->where('id', $item->patient_id)->select('first_name', 'last_name')->get()->toArray(); $staff_member = DB::table('users')->where('id', $item->created_by)->select('first_name', 'last_name')->get()->toArray(); } } if($request->comparator == "PRE"){ $result = DB::table('insurance_subscriptions') ->where('created_by', $staff_member) ->whereBetween('created_at', [$start, $end]) ->groupBy('receipt_number') ->get() ->toArray(); foreach ($result as $item){ $donor_amount_paid = get_name($item->receipt_number, 'receipt_number', 'donor_to_pay', 'donor_discount_details'); $discount = get_name($item->receipt_number, 'receipt_number', 'discount_amount', 'discounts'); $group = DB::table('insurance_groups')->where('id', $item->group_id)->select('name')->get()->toArray(); $staff_member = DB::table('users')->where('id', $item->created_by)->select('first_name', 'last_name')->get()->toArray(); } } if($request->comparator == "SUN"){ $result = DB::table('sundries_deposits') ->whereNotIn('patient_id', findTestOrDemoPatients()) ->whereBetween('created_at', [$start, $end]) ->groupBy('receipt_number') ->get() ->toArray(); foreach ($result as $item){ $donor_amount_paid = get_name($item->receipt_number, 'receipt_number', 'donor_to_pay', 'donor_discount_details'); $discount = get_name($item->receipt_number, 'receipt_number', 'discount_amount', 'discounts'); $patient = DB::table('patients')->where('id', $item->patient_id)->select('first_name', 'last_name')->get()->toArray(); $staff_member = DB::table('users')->where('id', $item->created_by)->select('first_name', 'last_name')->get()->toArray(); } } if($request->comparator == "ACRD"){ $debtors = DB::table('debtors') ->whereNotIn('patient_id', findTestOrDemoPatients()) ->whereBetween('created_at', [$start, $end]) ->get() ->toArray(); $debt_plan = DB::table('debt_plan') ->whereNotIn('patient_id', findTestOrDemoPatients()) ->whereBetween('created_at', [$start, $end]) ->get() ->toArray(); } if($request->comparator == "ACRI"){ $pending_invoices = DB::table('patient_category_invoices') ->whereBetween('created_at', [$start, $end]) ->get() ->toArray(); $pending_donor_invoices = DB::table('donor_discount_details') ->whereBetween('created_at', [$start, $end]) ->get() ->toArray(); } if($request->comparator == "PD"){ $debtor_payments = DB::table('debtor_payments') ->whereNotIn('patient_id', findTestOrDemoPatients()) ->whereBetween('created_at', [$start, $end]) ->groupBy('patient_id') ->get() ->toArray(); $debt_plan_payments = DB::table('debt_plan_payment_staffs') ->whereBetween('created_at', [$start, $end]) ->groupBy('created_by') ->get() ->toArray(); } if($request->comparator == "PI"){ $patient_category_payments = DB::table('invoice_payment_records') ->whereBetween('created_at', [$start, $end]) ->get() ->toArray(); $donor_invoice_payments = DB::table('donor_invoice_payments') ->whereBetween('created_at', [$start, $end]) ->get() ->toArray(); } $display = dateLabelSetter($request); return view('finance_reports::finance_reports.incomeDetailedReport', compact('result', 'request', 'donor_amount_paid', 'discount', 'patient', 'staff_member','group', 'donor_invoice_payments', 'patient_category_payments', 'debtor_payments', 'debt_plan_payments', 'pending_donor_invoices', 'pending_invoices', 'debtors', 'debt_plan', 'inv_lab', 'inv_x_ray', 'inv_ultrasound', 'staff_members', 'display')); } public function getCostCenterInvoicePayments($table, $sum_column, Request $request){ $result = 0; $today = Carbon::today()->toDateTimeString(); $yesterday = Carbon::yesterday()->toDateTimeString(); $end = Carbon::parse($request->end_date)->endOfDay()->toDateTimeString(); $start = Carbon::parse($request->start_date)->startOfDay()->toDateTimeString(); if($request->dates == "today"){ $result = DB::table($table) ->whereDate('created_at', $today) ->get() ->toArray(); } else if($request->dates == "yesterday"){ $result = DB::table($table) ->whereDate('created_at', $yesterday) ->get() ->toArray(); } else if($request->dates == "custom_date"){ $result = DB::table($table) ->whereDate('created_at', $start) ->get() ->toArray(); } else if($request->dates == "custom_date_range"){ $result = DB::table($table) ->whereBetween('created_at', [$start, $end]) ->get() ->toArray(); } return $result; } public function getCostCenterDebtorPaymentsSum($table, $sum_column, Request $request){ $result = 0; $insurance_amount = 0; $today = Carbon::today()->toDateTimeString(); $yesterday = Carbon::yesterday()->toDateTimeString(); $end = Carbon::parse($request->end_date)->endOfDay()->toDateTimeString(); $start = Carbon::parse($request->start_date)->startOfDay()->toDateTimeString(); if($table != "insurance_subscriptions"){ if($request->dates == "today"){ $result = DB::table($table) ->whereDate('created_at', $today) ->whereNotIn('patient_id', findTestOrDemoPatients()) ->get() ->sum($sum_column); } else if($request->dates == "yesterday"){ $result = DB::table($table) ->whereDate('created_at', $yesterday) ->whereNotIn('patient_id', findTestOrDemoPatients()) ->get() ->sum($sum_column); } else if($request->dates == "custom_date"){ $result = DB::table($table) ->whereDate('created_at', $request->start_date) ->whereNotIn('patient_id', findTestOrDemoPatients()) ->get() ->sum($sum_column); } else if($request->dates == "custom_date_range"){ $result = DB::table($table) ->whereBetween('created_at', [$start, $end]) ->whereNotIn('patient_id', findTestOrDemoPatients()) ->get() ->sum($sum_column); } }else{ if($request->dates == "today"){ $result = DB::table($table)->whereDate('created_at', $today)->get()->pluck('family_amount'); foreach ($result as $item){ $insurance_amount += array_sum(explode(",",$item)); } } else if($request->dates == "yesterday"){ $result = DB::table($table)->whereDate('created_at', $yesterday)->get()->pluck('family_amount'); foreach ($result as $item){ $insurance_amount += array_sum(explode(",",$item)); } } else if($request->dates == "custom_date"){ $result = DB::table($table)->whereDate('created_at', $request->start_date)->pluck('family_amount'); foreach ($result as $item){ $insurance_amount += array_sum(explode(",",$item)); } } else if($request->dates == "custom_date_range"){ $result = DB::table($table)->whereBetween('created_at', [$start, $end])->pluck('family_amount'); foreach ($result as $item){ $insurance_amount += array_sum(explode(",",$item)); } } return $insurance_amount; } return $result; } public function labPerformanceReport(Request $request) { /* opd cash */ $today = Carbon::today()->toDateTimeString(); $yesterday = Carbon::yesterday()->toDateTimeString(); $end = Carbon::parse($request->end_date)->endOfDay()->toDateTimeString(); $start = Carbon::parse($request->start_date)->startOfDay()->toDateTimeString(); $inv_lab = []; $inv_ultrasound = []; $inv_x_ray = []; if($request->date_type == "today"){ $result = DB::table('investigation_deposits') ->whereNotIn('patient_id', findTestOrDemoPatients()) ->whereDate('created_at', $today) ->groupBy('receipt_number') ->get() ->toArray(); $sorted = $this->incomeInvestigationDetail($result); $inv_lab = $sorted[0]; $inv_x_ray = $sorted[1]; $inv_ultrasound = $sorted[2]; }else if($request->date_type == "yesterday"){ $result = DB::table('investigation_deposits') ->whereNotIn('patient_id', findTestOrDemoPatients()) ->whereDate('created_at', $yesterday) ->groupBy('receipt_number') ->get() ->toArray(); $sorted = $this->incomeInvestigationDetail($result); $inv_lab = $sorted[0]; $inv_x_ray = $sorted[1]; $inv_ultrasound = $sorted[2]; }else if($request->date_type == "custom_date"){ $result = DB::table('investigation_deposits') ->whereNotIn('patient_id', findTestOrDemoPatients()) ->whereDate('created_at', $start) ->groupBy('receipt_number') ->get() ->toArray(); $sorted = $this->incomeInvestigationDetail($result); $inv_lab = $sorted[0]; $inv_x_ray = $sorted[1]; $inv_ultrasound = $sorted[2]; }else if($request->date_type == "custom_date_range"){ $result = DB::table('investigation_deposits') ->whereNotIn('patient_id', findTestOrDemoPatients()) ->whereBetween('created_at', [$start, $end]) ->groupBy('receipt_number') ->get() ->toArray(); $sorted = $this->incomeInvestigationDetail($result); $inv_lab = $sorted[0]; $inv_x_ray = $sorted[1]; $inv_ultrasound = $sorted[2]; } else { $result = DB::table('investigation_deposits') ->whereNotIn('patient_id', findTestOrDemoPatients()) ->whereDate('created_at', $today) ->groupBy('receipt_number') ->get() ->toArray(); $sorted = $this->incomeInvestigationDetail($result); $inv_lab = $sorted[0]; $inv_x_ray = $sorted[1]; $inv_ultrasound = $sorted[2]; } //dd($inv_lab); //start doing the chart things $lab_money = 0; $x_ray_money = 0; $ultrasound_money = 0; foreach ($inv_lab as $item){ $lab_money += $item['cost_price']; } foreach ($inv_x_ray as $item){ $x_ray_money += $item['cost_price']; } foreach ($inv_ultrasound as $item){ $ultrasound_money += $item['cost_price']; } $display = dateLabelSetter($request); $chartjs = app()->chartjs ->name('barChart') ->type('bar') ->size(['width' => 600, 'height' => 400]) ->labels(['Lab','Xray','Ultrasound']) ->datasets([ [ "label" => "Investigations performance", 'backgroundColor' => '#9a04f0', 'data' => [$lab_money, $x_ray_money, $ultrasound_money] ] ]) ->optionsRaw([ 'scales' => [ 'yAxes' => [ [ 'ticks' => [ 'beginAtZero' => true, ], ], ], 'xAxes' => [ [ 'ticks' => [ 'autoSkip' => false ], ] ] ] ]) ->options([]); //end doing the chart things //dd($result); /* end of opd cash */ return view('finance::cost_center_performance.lab_perfomance',compact('request','result','display','inv_lab','inv_x_ray','inv_ultrasound', 'chartjs')); } public function labPerformanceReportDetails(Request $request) { $investigations_array = $request->investigations_array; $patient_names_array = $request->patient_names_array; $patients_array = $request->patients_array; $receipt_number_array = $request->receipt_number_array; $investigations_name_array = $request->investigations_name_array; $investigations_date_array = $request->investigations_date_array; $investigations_amountpaid_array = $request->investigations_amountpaid_array; $investigations_cost_array = $request->investigations_cost_array; $invs_grand_total = $request->total; return view('finance::cost_center_performance.lab_perfomance_details',compact('request','investigations_array','patient_names_array','patients_array','receipt_number_array','investigations_name_array','investigations_date_array','investigations_amountpaid_array','investigations_cost_array','invs_grand_total')); } public function incomeInvestigationDetail($result){ $inv_lab = []; $inv_ultrasound = []; $inv_x_ray = []; $total_patient_amount_paid = 0; $is_opd_investigation = false; foreach ($result as $investigation_deposit){ $investigation_deposit_ids_array = explode(',', $investigation_deposit->investigation_items); //sort this array to help in comparison with the sorted ordered inv array then check inpatient status sort($investigation_deposit_ids_array); $investigation_deposit_amounts_array = explode(',', $investigation_deposit->investigation_amounts); $ordered_investigations = DB::table('ordered_investigations')->where('episode_id', $investigation_deposit->episode_id)->get(); foreach ($ordered_investigations as $order_record) { $investigation_ordered_ids_array = explode(",", $order_record->investigation_id); //sort this array too do comparison with the sorted inv deposits array from above sort($investigation_ordered_ids_array); /* if same investigation ids for deposits and ordered ones then enter if stmt */ if ($investigation_deposit_ids_array == $investigation_ordered_ids_array) { $investigation_ordered_inpatient_status = $order_record->inpatient; /* Further check if the inpatient status is that of OPD then enter if stmt */ if ($investigation_ordered_inpatient_status === 0) { /*=============================start original block by Benji ===========================*/ for($i = 0; $i < count($investigation_deposit_ids_array); $i++){ if( $investigation_deposit_ids_array[$i] != ""){ $inv_category = get_name( $investigation_deposit_ids_array[$i], 'id', 'category', 'investigations'); if($inv_category == 6){ $category = get_name($inv_category, 'id', 'name', 'investigation_categories'); array_push($inv_ultrasound, array("investigation"=>"labs","item"=> $investigation_deposit_ids_array[$i], "receipt_number"=>$investigation_deposit->receipt_number,"date"=>$investigation_deposit->created_at, "patient_amount_paid"=>$investigation_deposit->patient_amount_paid, "cost_price"=>$investigation_deposit_amounts_array[$i], 'patient_id'=>$investigation_deposit->patient_id, 'category'=>$category, "category_id"=>$inv_category)); } else if($inv_category == 4){ $category = get_name($inv_category, 'id', 'name', 'investigation_categories'); array_push($inv_x_ray, array("investigation"=>"labs","item"=> $investigation_deposit_ids_array[$i], "receipt_number"=>$investigation_deposit->receipt_number,"date"=>$investigation_deposit->created_at, "patient_amount_paid"=>$investigation_deposit->patient_amount_paid, "cost_price"=>$investigation_deposit_amounts_array[$i], 'patient_id'=>$investigation_deposit->patient_id, 'category'=>$category, "category_id"=>$inv_category)); } elseif ($inv_category != 4 && $inv_category != 6 && $inv_category != 7) { $category = get_name($inv_category, 'id', 'name', 'investigation_categories'); array_push($inv_lab, array("investigation"=>"labs","item"=> $investigation_deposit_ids_array[$i], "receipt_number"=>$investigation_deposit->receipt_number,"date"=>$investigation_deposit->created_at, "patient_amount_paid"=>$investigation_deposit->patient_amount_paid, "cost_price"=>$investigation_deposit_amounts_array[$i], 'patient_id'=>$investigation_deposit->patient_id, 'category'=>$category, "category_id"=>$inv_category)); } } } /* ======================== end of original block by Benji ======================*/ } } } } return array($inv_lab, $inv_x_ray, $inv_ultrasound); } /* display the top 10 performing investigations */ public function topPerformingInvestigationsReport(Request $request) { $today = Carbon::today()->toDateTimeString(); $yesterday = Carbon::yesterday()->toDateTimeString(); $end = Carbon::parse($request->end_date)->endOfDay()->toDateTimeString(); $start = Carbon::parse($request->start_date)->startOfDay()->toDateTimeString(); $result = []; $ids_and_paid_amounts_array = []; $topPerformingArray = []; if($request->date_type == "today"){ $result = DB::table('investigation_deposits') ->whereNotIn('patient_id', findTestOrDemoPatients()) ->whereDate('created_at', $today) ->groupBy('receipt_number') ->get() ->toArray(); $ids_and_paid_amounts_array = $this->investigationIdsWithTheirAmountsPaidArray($result); }else if($request->date_type == "yesterday"){ $result = DB::table('investigation_deposits') ->whereNotIn('patient_id', findTestOrDemoPatients()) ->whereDate('created_at', $yesterday) ->groupBy('receipt_number') ->get() ->toArray(); $ids_and_paid_amounts_array = $this->investigationIdsWithTheirAmountsPaidArray($result); }else if($request->date_type == "custom_date"){ $result = DB::table('investigation_deposits') ->whereNotIn('patient_id', findTestOrDemoPatients()) ->whereDate('created_at', $start) ->groupBy('receipt_number') ->get() ->toArray(); $ids_and_paid_amounts_array = $this->investigationIdsWithTheirAmountsPaidArray($result); }else if($request->date_type == "custom_date_range"){ $result = DB::table('investigation_deposits') ->whereNotIn('patient_id', findTestOrDemoPatients()) ->whereBetween('created_at', [$start, $end]) ->groupBy('receipt_number') ->get() ->toArray(); $ids_and_paid_amounts_array = $this->investigationIdsWithTheirAmountsPaidArray($result); } else { $result = DB::table('investigation_deposits') ->whereNotIn('patient_id', findTestOrDemoPatients()) ->whereDate('created_at', $today) ->groupBy('receipt_number') ->get() ->toArray(); $ids_and_paid_amounts_array = $this->investigationIdsWithTheirAmountsPaidArray($result); } //dd($ids_and_paid_amounts_array); //arrange by the money values arsort($ids_and_paid_amounts_array); //dd($ids_and_paid_amounts_array); //slice the top 3 $sliced_array = array_slice($ids_and_paid_amounts_array, 0, 10, true); //keep the true parameter and life will be ok //dd($sliced_array); $investigationIdsArray = array_keys($sliced_array); $investigationNamesArray = []; for ($i=0; $i < count($investigationIdsArray) ; $i++) { $investigationName = get_name($investigationIdsArray[$i], 'id', 'name', 'investigations'); array_push($investigationNamesArray, $investigationName); } $investigationPaidAmountsArray = array_values($sliced_array); //add an empty and a zero $chartjs = app()->chartjs ->name('barChartTest') ->type('bar') ->size(['width' => 600, 'height' => 400]) ->labels($investigationNamesArray) ->datasets([ [ "label" => "Top performing investigations (Cash)", 'backgroundColor' => '#9a04f0', 'data' => $investigationPaidAmountsArray ] ]) ->optionsRaw([ 'scales' => [ 'yAxes' => [ [ 'ticks' => [ 'beginAtZero' => true, ], ], ], 'xAxes' => [ [ 'ticks' => [ 'autoSkip' => false ], ] ] ] ]) ->options([]); /*$chartjs = app()->chartjs ->name('barChartTest') ->type('bar') ->size(['width' => 400, 'height' => 200]) ->labels(['Label x', 'Label y']) ->datasets([ [ "label" => "My First dataset", 'backgroundColor' => ['rgba(255, 99, 132, 0.2)', 'rgba(54, 162, 235, 0.2)'], 'data' => [69, 59] ], [ "label" => "My First dataset", 'backgroundColor' => ['rgba(255, 99, 132, 0.3)', 'rgba(54, 162, 235, 0.3)'], 'data' => [65, 12] ] ]) ->options([]);*/ return view('finance::cost_center_performance.top_performing_investigations',compact('chartjs','request')); } public function investigationIdsWithTheirAmountsPaidArray($result){ $inv_lab = []; $inv_ultrasound = []; $inv_x_ray = []; $investigation_id_and_amount_array = []; $investigation_ids = []; $investigation_amounts = []; $two_elements_array = []; foreach ($result as $item){ $ids = explode(',', $item->investigation_items); $items = explode(',', $item->investigation_amounts); $count = count($ids); for($i = 0; $i < $count; $i++){ if($ids[$i] != ""){ $inv_category = get_name($ids[$i], 'id', 'category', 'investigations'); if(($inv_category == 1) || ($inv_category == 2) || ($inv_category == 3) || ($inv_category == 5)){ $category = get_name($inv_category, 'id', 'name', 'investigation_categories'); array_push($inv_lab, array("investigation"=>"labs","item"=>$ids[$i], "receipt_number"=>$item->receipt_number,"date"=>$item->created_at, "patient_amount_paid"=>$item->patient_amount_paid, "cost_price"=>$items[$i], 'patient_id'=>$item->patient_id, 'category'=>$category, "category_id"=>$inv_category)); //$investigation_id_and_amount_array[$ids[$i]] = $item->patient_amount_paid; $new_item_amount = $items[$i]; if (array_key_exists($ids[$i], $investigation_id_and_amount_array)) { $existing_amount = $investigation_id_and_amount_array[$ids[$i]]; $investigation_id_and_amount_array[$ids[$i]] = $new_item_amount + $existing_amount; } else { $investigation_id_and_amount_array[$ids[$i]] = $new_item_amount; } //$investigation_ids[] = $ids[$i]; //$investigation_amounts[] = $item->patient_amount_paid; } else if($inv_category == 4){ $category = get_name($inv_category, 'id', 'name', 'investigation_categories'); array_push($inv_x_ray, array("investigation"=>"labs","item"=>$ids[$i], "receipt_number"=>$item->receipt_number,"date"=>$item->created_at, "patient_amount_paid"=>$item->patient_amount_paid, "cost_price"=>$items[$i], 'patient_id'=>$item->patient_id, 'category'=>$category, "category_id"=>$inv_category)); //$investigation_id_and_amount_array[$ids[$i]] = $item->patient_amount_paid; $new_item_amount = $items[$i]; if (array_key_exists($ids[$i], $investigation_id_and_amount_array)) { $existing_amount = $investigation_id_and_amount_array[$ids[$i]]; $investigation_id_and_amount_array[$ids[$i]] = $new_item_amount + $existing_amount; } else { $investigation_id_and_amount_array[$ids[$i]] = $new_item_amount; } //$investigation_ids[] = $ids[$i]; //$investigation_amounts[] = $item->patient_amount_paid; } else { $category = get_name($inv_category, 'id', 'name', 'investigation_categories'); array_push($inv_ultrasound, array("investigation"=>"labs","item"=>$ids[$i], "receipt_number"=>$item->receipt_number,"date"=>$item->created_at, "patient_amount_paid"=>$item->patient_amount_paid, "cost_price"=>$items[$i], 'patient_id'=>$item->patient_id, 'category'=>$category, "category_id"=>$inv_category)); //$investigation_id_and_amount_array[$ids[$i]] = $item->patient_amount_paid; $new_item_amount = $items[$i]; if (array_key_exists($ids[$i], $investigation_id_and_amount_array)) { $existing_amount = $investigation_id_and_amount_array[$ids[$i]]; $investigation_id_and_amount_array[$ids[$i]] = $new_item_amount + $existing_amount; } else { $investigation_id_and_amount_array[$ids[$i]] = $new_item_amount; } //$investigation_ids[] = $ids[$i]; //$investigation_amounts[] = $item->patient_amount_paid; } } } } $two_elements_array = [$investigation_ids,$investigation_amounts]; return $investigation_id_and_amount_array; //return $two_elements_array; } }