search_date_by){ case 'yesterday': $end_date = Carbon::yesterday()->endOfDay(); $start_date = Carbon::yesterday()->startOfDay(); $search_text .= "Yesterday "; break; case 'custom_date': $end_date = Carbon::parse($request->start_date)->endOfDay(); $start_date = Carbon::parse($request->start_date)->startOfDay(); $search_text .= "From: " . streamline_date($start_date) . " "; break; case 'custom_date_range': $end_date = Carbon::parse($request->end_date)->endOfDay(); $start_date = Carbon::parse($request->start_date)->startOfDay(); $search_text .= "From: " . streamline_date($start_date) . " to " . streamline_date($end_date) . " "; break; case 'today': default: $end_date = Carbon::today()->endOfDay(); $start_date = Carbon::today()->startOfDay(); $search_text .= "Today "; break; } $records = PointOfSaleRecord::join('patients', 'point_of_sale_records.patient_id', '=', 'patients.id') ->whereBetween('point_of_sale_records.created_at', [$start_date, $end_date]) ->limit(500)->get(['point_of_sale_records.*', 'patients.first_name', 'patients.last_name', 'patients.number']); return view('patients::point_of_sale.index', compact('records', 'search_text')); } public function order_items(){ $drugs = Drug::get(); $sundries = Sundry::where('available', 1)->get(); $services = Services::where('available', 1)->get(); $eye_glasses = EyeGlasses::get(); $referral_hospitals = ReferralHospital::orderBy('name')->get(); return view('patients::point_of_sale.order_items', compact('drugs', 'eye_glasses', 'sundries', 'referral_hospitals', 'services')); } public function confirm_items(Request $request) { $pre_ordered_eye_glasses = []; $manual_patient_prescriptions = []; $automatic_patient_prescriptions = []; $pre_ordered_sundries = []; $pre_ordered_services = []; if($request->patient_id) { $patient_id = $request->patient_id; $patient = Patient::find($patient_id); // double check if for existing patient_id if($patient){ $patient_number = Patient::where('id', $patient_id)->pluck('number')->first(); $episode_id = PatientEpisode::where('patient_id',$patient_id)->whereDate('created_at', Carbon::today()->toDateString())->pluck('id')->first(); if(!$episode_id){ $episode = new PatientEpisode; $episode->patient_id = $patient_id; $episode->paid_over = "pos"; $episode->created_by = Auth::id(); $episode->updated_by = Auth::id(); $episode->save(); flash('A new episode for patient with patient number ' . $patient_number . ' has been initiated.'); $episode_id = $episode->id; } } else { flash('Patient not found')->error(); redirect('point_of_sale'); } } else { $patient = new Patient; $patient->first_name = $request->first_name; $patient->last_name = $request->last_name; $patient->phone = $request->phone_number ?? ""; $patient->referred_from = $request->referral_hospital ?? 1; $patient->category_id = 1; $patient->created_by = Auth::id(); $patient->gender = $request->gender ?? 2; if (is_null($request->date_of_birth)) { $age_in_years = $request->age_in_years ?? 18; $calculated_dob = \Carbon\Carbon::now()->subYears($age_in_years); $calculated_date_of_birth = $calculated_dob->toDateString(); $patient->date_of_birth = $calculated_date_of_birth; } else { $patient->date_of_birth = Carbon::createFromFormat('d/m/Y', $request->date_of_birth)->toDateString(); } if ($patient->save()): $prefix = DB::table('hospital_information')->where('id', 1)->value('patient_number_abbr'); $patient_id = $patient->id; $new_id = quadLimit($patient_id); $patient_number = $prefix . "-" . $new_id; DB::table('patients')->where('id', $new_id)->update(['number' => $patient_number]); // Updating the patient number else: flash("There was an error")->error(); return back()->withInput(); endif; $episode = new PatientEpisode; $episode->patient_id = $patient_id; $episode->clinic_id = get_default_hospital_clinic(); $episode->paid_over = "pos"; $episode->created_by = Auth::id(); $episode->updated_by = Auth::id(); try { $episode->save(); $episode_id = $episode->id; flash('Patient with patient number ' . $patient_number . ' has been successfully registered.')->success(); } catch (QueryException $e) { flash("This episode already exists!")->error(); return back()->withInput(); } } if ($request->selected_eye_glasses) { $pre_ordered_eye_glasses = EyeGlasses::whereIn('id', $request->selected_eye_glasses)->get(); if (stock_levels_to_consider() == 0) {//getting total stock for optical from both stores and pharmacy foreach($pre_ordered_eye_glasses as $eye) {$eye->total_stock = $this->itemStockService->getItemAllQuantityByTotal($eye->id, 7);} } else{//getting total stock for opticals from pharmacy foreach($pre_ordered_eye_glasses as $eye) {$eye->total_stock = $this->itemStockService->getItemQuantity('pharmacy_stock',$eye->id, 7);} } } if ($request->selected_drugs) { if($request->manual_drug_select == 1){ $manual_patient_prescriptions = Drug::whereIn('id', $request->selected_drugs)->get(); if (stock_levels_to_consider() == 0) {//getting total stock for drugs from both stores and pharmacy foreach($manual_patient_prescriptions as $drug) {$drug->total_stock = $this->itemStockService->getItemAllQuantityByTotal($drug->id, 1);} } else {//getting total stock for drugs from pharmacy foreach($manual_patient_prescriptions as $drug) {$drug->total_stock = $this->itemStockService->getItemQuantity('pharmacy_stock',$drug->id, 1);} } }else if($request->automatic_drug_select == 1){ $automatic_patient_prescriptions = Drug::whereIn('id', $request->selected_drugs)->get(); if (stock_levels_to_consider() == 0) {//getting total stock for drugs from both stores and pharmacy foreach($automatic_patient_prescriptions as $drug) {$drug->total_stock = $this->itemStockService->getItemAllQuantityByTotal($drug->id, 1);} } else{//getting total stock for optical from pharmacy foreach($automatic_patient_prescriptions as $drug) {$drug->total_stock = $this->itemStockService->getItemQuantity('pharmacy_stock',$drug->id, 1);} } } } if ($request->selected_sundries) { $pre_ordered_sundries = Sundry::whereIn('id', $request->selected_sundries)->get(); if (stock_levels_to_consider() == 0) {//getting total stock for sundry from both stores and pharmacy foreach($pre_ordered_sundries as $sundry) {$sundry->total_stock = $this->itemStockService->getItemAllQuantityByTotal($sundry->id, 2);} } else{//getting total stock for optical from pharmacy foreach($pre_ordered_sundries as $sundry) {$sundry->total_stock = $this->itemStockService->getItemQuantity('pharmacy_stock',$sundry->id, 2);} } } if ($request->selected_services) { $pre_ordered_services = Services::whereIn('id', $request->selected_services)->get(); } $allergies = DB::table('allergies')->where(['patient_id' => $patient_id])->pluck('patient_id', 'names'); return view('patients::point_of_sale.confirm_items', compact('patient_id', 'episode_id', 'pre_ordered_eye_glasses', 'manual_patient_prescriptions', 'automatic_patient_prescriptions', 'allergies', 'patient', 'pre_ordered_sundries', 'pre_ordered_services')); } public function confirm_pricing(Request $request){ $order_sundries_id = NULL; $treatment_id = NULL; $order_optical_id = NULL; $order_service_id = NULL; if($request->treatment_item){ $treatment = new Treatment; $treatment->patient_id = $request->patient_id; $treatment->episode_id = $request->episode_id; $treatment->drugs = implode(',', $request->treatment_item); $drugs_array = $request->treatment_item; $duration_array = $request->duration; $time_array = $request->time; $time_duration = []; $doses = $request->dose ?? []; $frequencies = $request->frequency ?? []; $dose_array = []; $frequencies_array = []; $instructions_array = []; for ($i = 0; $i < count($drugs_array); $i++) { if (is_drug_chronic($drugs_array[$i])) { register_chronic_patient($request->patient_id, $request->episode_id, $drugs_array[$i]); } if (isset($duration_array[$i]) && isset($time_array[$i])) { $time_duration[] = $duration_array[$i] . " " . $time_array[$i]; } else { $time_duration[] = "1 Days"; } if (isset($doses[$i])) { $dose_array[] = $doses[$i]; } else { $dose_array[] = "1"; } if (isset($frequencies[$i])) { $frequencies_array[] = $frequencies[$i]; } else { $frequencies_array[] = "2"; } $instructions_array[] = ""; } $treatment->doses = implode(',', $dose_array); $treatment->frequencies = implode(',', $frequencies_array); $treatment->instruction = implode(',', $instructions_array); $treatment->durations = implode(',', $time_duration); $treatment->quantities_dispensed = implode(',', $request->treatment_quantity); $treatment->dispense_status = 0; $treatment->is_pos = 1; $treatment->created_by = Auth::id(); $treatment->save(); $treatment_id = $treatment->id; } if($request->eye_glass_item){ $new_ordered_eye_glasses = new OrderedEyeGlasses; $new_ordered_eye_glasses->patient_id = $request->patient_id; $new_ordered_eye_glasses->episode_id = $request->episode_id; $new_ordered_eye_glasses->eye_glasses_id = implode(",", $request->eye_glass_item); $new_ordered_eye_glasses->quantity = implode(",", $request->eye_glass_quantity); $new_ordered_eye_glasses->payment_status = 0; //0 by default to mean not paid $new_ordered_eye_glasses->created_by = auth()->id(); $new_ordered_eye_glasses->is_pos = 1; $new_ordered_eye_glasses->save(); $order_optical_id = $new_ordered_eye_glasses->id; } if($request->pos_sundry_ids){ $new_ordered_sundries = new OrderedSundry; $new_ordered_sundries->patient_id = $request->patient_id; $new_ordered_sundries->episode_id = $request->episode_id; $new_ordered_sundries->sundries_id = implode(",", $request->pos_sundry_ids); $new_ordered_sundries->quantity = implode(",", $request->sundry_quantity); $new_ordered_sundries->created_by = auth()->id(); $new_ordered_sundries->is_pos = 1; $new_ordered_sundries->save(); $order_sundries_id = $new_ordered_sundries->id; } if ($request->service_id && $request->service_id[0] != null) { $new_ordered_service = new OrderedService; $new_ordered_service->patient_id = $request->patient_id; $new_ordered_service->episode_id = $request->episode_id; $new_ordered_service->service_id = implode(",", $request->service_id); $new_ordered_service->quantity = implode(",", $request->quantity); $new_ordered_service->performed = 0; $new_ordered_service->performed_id = 0; $new_ordered_service->created_by = auth()->id(); $new_ordered_service->is_pos = 1; $new_ordered_service->save(); $order_service_id = $new_ordered_service->id; } $treatment_item = $treatment_quantity = $treatment_subtotal = []; // save for treatment if($request->treatment_item){ $treatment_item = $request->treatment_item; $treatment_quantity = $request->treatment_quantity; $treatment_subtotal = $request->treatment_subtotal; } $eye_glasses_prices_array = $eye_glasses_quantity_array = $eye_glasses_ids_array = []; // save for eye_glasses arrays if($request->eye_glass_item){ $eye_glasses_prices_array = $request->eye_glass_subtotal; $eye_glasses_quantity_array = $request->eye_glass_quantity; $eye_glasses_ids_array = $request->eye_glass_item; } $sundry_item = $sundry_quantity = $sundry_subtotal = []; // save for sundries array if($request->pos_sundry_ids){ $sundry_item = $request->pos_sundry_ids; $sundry_quantity = $request->sundry_quantity; $sundry_subtotal = $request->sundry_subtotal; } // save for service arrays $service_ids_array = []; $service_prices_array = []; $service_quantity_array = []; if($request->service_id){ $service_prices_array = $request->service_item_subtotal; $service_ids_array = $request->service_id; $service_quantity_array = $request->quantity; } $pos_record = new PointOfSaleRecord(); $pos_record->patient_id = $request->patient_id; $pos_record->episode_id = $request->episode_id; $pos_record->treatments = count($treatment_item) > 0 ? json_encode([ "ids" => $treatment_item, "quantity" => $treatment_quantity, "subtotal" => $treatment_subtotal, "order_id" => $treatment_id ]) : NULL; $pos_record->eye_glasses = count($eye_glasses_ids_array) > 0 ? json_encode([ "ids" => $eye_glasses_ids_array, "quantity" => $eye_glasses_quantity_array, "subtotal" => $eye_glasses_prices_array, "order_id" => $order_optical_id ]) : NULL; $pos_record->sundries = count($sundry_item) > 0 ? json_encode([ "ids" => $sundry_item, "quantity" => $sundry_quantity, "subtotal" => $sundry_subtotal, "order_id" => $order_sundries_id ]) : NULL; $pos_record->services = count($service_ids_array) > 0 ? json_encode([ "ids" => $service_ids_array, "quantity" => $service_quantity_array, "subtotal" => $service_prices_array, "order_id" => $order_service_id ]) : NULL; $pos_record->created_by = Auth::id(); $pos_record->save(); return redirect('point_of_sale/print/' . $pos_record->id); } public function add_referral(Request $request) { $logged_in_user_id = Auth::id(); $referral_hospital = new ReferralHospital; $referral_hospital->name = $request->name; $referral_hospital->created_by = $logged_in_user_id; $referral_hospital->updated_by = $logged_in_user_id; if ($referral_hospital->save()) { //insert successful return $referral_hospital->id; } else { return 0; } } public function get_patient(Request $request){ $patient = Patient::where('id', $request->patient_id)->first(); return $patient; } public function print($id) { $record = PointOfSaleRecord::find($id); if ($record) { if (is_cashier_receipt_type_print_html()) { $hospital_information = HospitalInformation::first(); $patient = Patient::find($record->patient_id); $receipt_date = $record->created_at; $receipt_reprint_date = date('Y-m-d h:i:s'); $first_printed_by = $record->created_by; $treatments_array = json_decode($record->treatments, true); $sundries_array = json_decode($record->sundries, true); $eye_glasses_array = json_decode($record->eye_glasses, true); $services_array = json_decode($record->services, true); $treatment_item = $treatments_array ? $treatments_array["ids"] : []; $treatment_quantity = $treatments_array ? $treatments_array["quantity"] : []; $treatment_subtotal = $treatments_array ? $treatments_array["subtotal"] : []; $treatment_number = $treatments_array ? ($treatments_array["order_id"] ?? 0) : []; $eye_glasses_ids_array = $eye_glasses_array ? $eye_glasses_array["ids"] : []; $eye_glasses_quantity_array = $eye_glasses_array ? $eye_glasses_array["quantity"] : []; $eye_glasses_prices_array = $eye_glasses_array ? $eye_glasses_array["subtotal"] : []; $eye_glasses_number = $eye_glasses_array ? ($eye_glasses_array["order_id"] ?? 0) : []; $sundry_item = $sundries_array ? $sundries_array["ids"] : []; $sundry_quantity = $sundries_array ? $sundries_array["quantity"] : []; $sundry_subtotal = $sundries_array ? $sundries_array["subtotal"] : []; $sundry_number = $sundries_array ? ($sundries_array["order_id"] ?? 0) : []; $service_ids_array = $services_array ? $services_array["ids"] : []; $service_quantity_array = $services_array ? $services_array["quantity"] : []; $service_prices_array = $services_array ? $services_array["subtotal"] : []; $service_number = $services_array ? ($services_array["order_id"] ?? 0) : []; return view('patients::point_of_sale.receipt', compact('treatment_item', 'treatment_quantity', 'treatment_subtotal', 'eye_glasses_prices_array', 'eye_glasses_quantity_array', 'eye_glasses_ids_array', 'hospital_information', 'patient', 'receipt_date', 'first_printed_by', 'sundry_item','sundry_quantity','sundry_subtotal', 'service_ids_array', 'service_prices_array', 'service_quantity_array', 'receipt_reprint_date', 'eye_glasses_number', 'treatment_number', 'sundry_number', 'service_number')); } else { // set up the redirect link for html session()->put('print_pos_pdf', 1); session()->put('print_pos_pdf_id', $id); return redirect('/point_of_sale'); } } else { return redirect('/point_of_sale'); } } public function print_pos_pdf() { $id = session()->get("print_pos_pdf_id"); // add check for when the people try to reload the page if (!$id) { return redirect('/point_of_sale'); } // lest i forget Thy love for me session()->forget('print_pos_pdf'); session()->forget('print_pos_pdf_id'); $record = PointOfSaleRecord::find($id); if ($record) { $hospital_information = HospitalInformation::first(); $patient = Patient::find($record->patient_id); $receipt_date = $record->created_at; $receipt_reprint_date = date('Y-m-d h:i:s'); $treatments_array = json_decode($record->treatments, true); $sundries_array = json_decode($record->sundries, true); $eye_glasses_array = json_decode($record->eye_glasses, true); $services_array = json_decode($record->services, true); $treatment_item = $treatments_array ? $treatments_array["ids"] : []; $treatment_quantity = $treatments_array ? $treatments_array["quantity"] : []; $treatment_subtotal = $treatments_array ? $treatments_array["subtotal"] : []; $treatment_number = $treatments_array ? ($treatments_array["order_id"] ?? 0) : []; $eye_glasses_ids_array = $eye_glasses_array ? $eye_glasses_array["ids"] : []; $eye_glasses_quantity_array = $eye_glasses_array ? $eye_glasses_array["quantity"] : []; $eye_glasses_prices_array = $eye_glasses_array ? $eye_glasses_array["subtotal"] : []; $eye_glasses_number = $eye_glasses_array ? ($eye_glasses_array["order_id"] ?? 0) : []; $sundry_item = $sundries_array ? $sundries_array["ids"] : []; $sundry_quantity = $sundries_array ? $sundries_array["quantity"] : []; $sundry_subtotal = $sundries_array ? $sundries_array["subtotal"] : []; $sundry_number = $sundries_array ? ($sundries_array["order_id"] ?? 0) : []; $service_ids_array = $services_array ? $services_array["ids"] : []; $service_quantity_array = $services_array ? $services_array["quantity"] : []; $service_prices_array = $services_array ? $services_array["subtotal"] : []; $service_number = $services_array ? ($services_array["order_id"] ?? 0) : []; $data = [ "patient" => $patient, "receipt_date" => $receipt_date, "receipt_reprint_date" => $receipt_reprint_date, "hospital_information" => $hospital_information, "treatment_item" => $treatment_item, "treatment_quantity" => $treatment_quantity, "treatment_subtotal" => $treatment_subtotal, "eye_glasses_ids_array" => $eye_glasses_ids_array, "eye_glasses_quantity_array" => $eye_glasses_quantity_array, "eye_glasses_prices_array" => $eye_glasses_prices_array, "sundry_item" => $sundry_item, "sundry_quantity" => $sundry_quantity, "sundry_subtotal" => $sundry_subtotal, "service_ids_array" => $service_ids_array, "service_quantity_array" => $service_quantity_array, "service_prices_array" => $service_prices_array, "treatment_number" => $treatment_number, "eye_glasses_number" => $eye_glasses_number, "sundry_number" => $sundry_number, "service_number" => $service_number, ]; $pdf = SnappyPDF::loadView('patients::point_of_sale.print_pos_pdf', $data) ->setOrientation('portrait') ->setPaper('a4') ->setOption('margin-bottom', 5) ->setOption('margin-top', 5) ->setOption('footer-html', '© ' . date('Y') . ' Stre@mline'); return $pdf->inline('Patient Receipt' . date(" d-m-y h:ia") . '.pdf'); } else { return redirect('/point_of_sale'); } } }