public function prescribeCreate()
 {
     $validator = Validator::make(Input::all(), array('HN' => 'min:8|hn_exist|have_appointment_with_me|already_prescribe'), array('HN.min' => 'ท่านกรอก HN ของผู้ป่วยไม่ครบ', 'HN.hn_exist' => 'HN ที่ท่านกรอกไม่ตรงกับผู้ป่วยใดของโรงพยาบาล', 'HN.have_appointment_with_me' => 'ผู้ป่วยคนนี้ไม่ได้นัดกับท่านไว้ในช่วงเวลานี้', 'HN.already_prescribe' => 'ท่านได้ทำการสั่งยาให้ผู้ป่วยคนนี้ไปแล้ว'));
     if ($validator->passes()) {
         $min = -1;
         $pharmacists = DB::table('users')->where('type', 'pharmacist')->get();
         foreach ($pharmacists as $pharmacist) {
             $amount = count(DB::table('appointments')->where('pharmacistEmpID', $pharmacist->username)->whereNull('dispensedTime')->get());
             if ($amount < $min || $min == -1) {
                 $min = $amount;
                 $pharmacistEmpID = $pharmacist->username;
             }
         }
         date_default_timezone_set('Asia/Bangkok');
         $date = date("Y-m-d", time());
         $time = date("H:i:s", time());
         $morning = 1;
         if ((int) date("H", time()) < 12) {
             $morning = 0;
         }
         DB::table('appointments')->where('HN', Input::get('HN'))->where('doctorEmpID', Session::get('username'))->where('appointmentDate', $date)->where('morning', $morning)->update(array('prescribedTime' => $time, 'pharmacistEmpID' => $pharmacistEmpID));
         $Drugs = Input::get('D');
         if (count($Drugs) > 0) {
             foreach ($Drugs as $Drug) {
                 $i = 0;
                 foreach ($Drug as $D) {
                     $i++;
                     if ($i == 1) {
                         $drugName = $D;
                     } else {
                         if ($i == 2) {
                             $quantity = $D;
                         } else {
                             if ($i == 3) {
                                 $description = $D;
                             }
                         }
                     }
                 }
                 $addPrescribedDrug = new PrescribedDrug();
                 $addPrescribedDrug->HN = Input::get('HN');
                 $addPrescribedDrug->doctorEmpID = Session::get('username');
                 date_default_timezone_set('Asia/Bangkok');
                 $addPrescribedDrug->date = $date;
                 $addPrescribedDrug->time = $time;
                 $addPrescribedDrug->drugName = $drugName;
                 $addPrescribedDrug->quantity = $quantity;
                 $addPrescribedDrug->description = $description;
                 $addPrescribedDrug->save();
             }
         }
         return Redirect::to('doctor/prescribe')->with('flash_notice', 'ดำเนินการสั่งยาสำเร็จ');
     } else {
         return Redirect::to('doctor/prescribe')->withErrors($validator)->withInput();
     }
 }
 public function changePrescribedDrugsCreate()
 {
     date_default_timezone_set('Asia/Bangkok');
     $date = date("Y-m-d", time());
     $time = date("H:i:s", time());
     $morning = 1;
     if ((int) date("H", time()) < 12) {
         $morning = 0;
     }
     DB::table('prescribeddrugs')->where('HN', Input::get("patientHN"))->where('doctorEmpID', Session::get('username'))->where('date', $date)->where('time', $_POST['prescribedTime'])->delete();
     $Drugs = Input::get('D');
     if (count($Drugs) > 0) {
         foreach ($Drugs as $Drug) {
             $i = 0;
             foreach ($Drug as $D) {
                 $i++;
                 if ($i == 1) {
                     $HN = $D;
                 } else {
                     if ($i == 6) {
                         $drugName = $D;
                     } else {
                         if ($i == 7) {
                             $quantity = $D;
                         } else {
                             if ($i == 8) {
                                 $description = $D;
                             }
                         }
                     }
                 }
             }
             $addPrescribedDrug = new PrescribedDrug();
             $addPrescribedDrug->HN = $HN;
             $addPrescribedDrug->doctorEmpID = Session::get('username');
             $addPrescribedDrug->date = $date;
             $addPrescribedDrug->time = $time;
             $addPrescribedDrug->drugName = $drugName;
             $addPrescribedDrug->quantity = $quantity;
             $addPrescribedDrug->description = $description;
             $addPrescribedDrug->save();
         }
     }
     DB::table('appointments')->where('HN', Input::get("patientHN"))->where('doctorEmpID', Session::get('username'))->where('appointmentDate', $date)->where('morning', $morning)->update(array('prescribedTime' => $time, 'dispensedStatus' => 0));
     return Redirect::to('doctor/changePrescribedDrugs')->with('flash_notice', 'ดำเนินการสั่งยาใหม่สำเร็จ');
 }
 public function addMedicalRecordAndPrescribeCreate()
 {
     date_default_timezone_set('Asia/Bangkok');
     $date = date("Y-m-d", time());
     $time = date("H:i:s", time());
     $morning = 1;
     if ((int) date("H", time()) < 12) {
         $morning = 0;
     }
     $Drugs = Input::get('D');
     if (count($Drugs) > 0) {
         foreach ($Drugs as $Drug) {
             $i = 0;
             foreach ($Drug as $D) {
                 $i++;
                 if ($i == 1) {
                     $HN = $D;
                 } else {
                     if ($i == 6) {
                         $drugName = $D;
                     } else {
                         if ($i == 7) {
                             $quantity = $D;
                         } else {
                             if ($i == 8) {
                                 $description = $D;
                             }
                         }
                     }
                 }
             }
             $addPrescribedDrug = new PrescribedDrug();
             $addPrescribedDrug->HN = $HN;
             $addPrescribedDrug->doctorEmpID = Session::get('username');
             $addPrescribedDrug->date = $date;
             $addPrescribedDrug->time = $time;
             $addPrescribedDrug->drugName = $drugName;
             $addPrescribedDrug->quantity = $quantity;
             $addPrescribedDrug->description = $description;
             $addPrescribedDrug->save();
         }
     }
     $pharmacists = DB::table('pharmacistQueue')->get();
     $prev = 0;
     foreach ($pharmacists as $pharmacist) {
         $firstPharmacist = $pharmacist->pharmacistEmpID;
         break;
     }
     foreach ($pharmacists as $pharmacist) {
         if ($prev == 1) {
             $pharmacistEmpID = $pharmacist->pharmacistEmpID;
             $prev = 0;
             break;
         }
         $prev = $pharmacist->queue;
     }
     if ($prev == 1) {
         $pharmacistEmpID = $firstPharmacist;
     }
     DB::table('pharmacistQueue')->where('queue', 1)->update(array('queue' => 0));
     DB::table('pharmacistQueue')->where('pharmacistEmpID', $pharmacistEmpID)->update(array('queue' => 1));
     DB::table('appointments')->where('HN', Input::get("patientHN"))->where('doctorEmpID', Session::get('username'))->where('appointmentDate', $date)->where('morning', $morning)->update(array('addMedicalRecordTime' => $time, 'prescribedTime' => $time, 'pharmacistEmpID' => $pharmacistEmpID));
     $addMedicalRecord = new MedicalRecord();
     $addMedicalRecord->HN = Input::get("patientHN");
     $addMedicalRecord->doctorEmpID = Session::get('username');
     $addMedicalRecord->date = $date;
     $addMedicalRecord->time = $time;
     $addMedicalRecord->symptom = Input::get('symptom');
     $addMedicalRecord->ICD10 = substr(Input::get('ICD10'), 0, strpos(Input::get('ICD10'), " "));
     $addMedicalRecord->save();
     return Redirect::to('doctor/addMedicalRecordAndPrescribe')->with('flash_notice', 'ดำเนินการบันทึกการรักษาและสั่งยาสำเร็จ');
 }