/**
  * NOSH ChartingSystem Login Ajax Functions
  */
 public function postPractices()
 {
     $practices = Practiceinfo::all();
     $data['message'] = array();
     if ($practices) {
         foreach ($practices as $practice) {
             $data['message'][$practice->practice_id] = $practice->practice_name;
         }
     }
     echo json_encode($data);
 }
 /**
  * NOSH ChartingSystem Reminder System, to be run as a cron job
  */
 public function reminder()
 {
     $start = time();
     $end = time() + 2 * 24 * 60 * 60;
     $query1 = DB::table('schedule')->join('demographics', 'schedule.pid', '=', 'demographics.pid')->select('demographics.reminder_to', 'demographics.reminder_method', 'schedule.appt_id', 'schedule.provider_id', 'schedule.start')->where('schedule.status', '=', 'Pending')->whereBetween('schedule.start', array($start, $end))->get();
     $j = 0;
     $i = 0;
     if ($query1) {
         foreach ($query1 as $row) {
             $to = $row->reminder_to;
             if ($to != '') {
                 $row0 = User::where('id', '=', $row->provider_id)->first();
                 $row2 = Practiceinfo::where('practice_id', '=', $row0->practice_id)->first();
                 if ($row2->timezone != null) {
                     date_default_timezone_set($row2->timezone);
                 }
                 $data_message['startdate'] = date("F j, Y, g:i a", $row->start);
                 $data_message['displayname'] = $row0->displayname;
                 $data_message['phone'] = $row2->phone;
                 $data_message['email'] = $row2->email;
                 $data_message['additional_message'] = $row2->additional_message;
                 if ($row->reminder_method == 'Cellular Phone') {
                     $this->send_mail(array('text' => 'emails.remindertext'), $data_message, 'Appointment Reminder', $to, $row0->practice_id);
                 } else {
                     $this->send_mail('emails.reminder', $data_message, 'Appointment Reminder', $to, $row0->practice_id);
                 }
                 $data = array('status' => 'Reminder Sent');
                 DB::table('schedule')->where('appt_id', '=', $row->appt_id)->update($data);
                 $this->audit('Add');
                 $i++;
             }
             $j++;
         }
     }
     $arr = "Number of appointments: " . $j . "<br>";
     $arr .= "Number of appointment reminders sent: " . $i . "<br>";
     $query3 = Practiceinfo::all();
     foreach ($query3 as $practice_row) {
         $results_scan = 0;
         $birthday_count = 0;
         $appointment_count = 0;
         $appointment_count1 = 0;
         $arr .= "<strong>Practice " . $practice_row->practice_id . "</strong><br>";
         //$updox = $this->check_extension('updox_extension', $practice_row->practice_id);
         //if ($updox) {
         //$this->updox_sync($practice_row->practice_id);
         //}
         $rcopia = $this->check_extension('rcopia_extension', $practice_row->practice_id);
         if ($rcopia) {
             $this->rcopia_sync($practice_row->practice_id);
         }
         $results_scan = $this->get_scans($practice_row->practice_id);
         $birthday = $this->check_extension('birthday_extension', $practice_row->practice_id);
         if ($birthday) {
             $birthday1 = DB::table('practiceinfo')->where('practice_id', '=', $practice_row->practice_id)->first();
             if ($birthday1->timezone != null) {
                 date_default_timezone_set($birthday1->timezone);
             }
             $date = date('Y-m-d');
             if ($birthday1->birthday_sent_date != $date) {
                 $birthday_count = $this->birthday_reminder($practice_row->practice_id);
             }
         }
         $appointment = $this->check_extension('appointment_extension', $practice_row->practice_id);
         if ($appointment) {
             $appointment1 = DB::table('practiceinfo')->where('practice_id', '=', $practice_row->practice_id)->first();
             if ($appointment1->timezone != null) {
                 date_default_timezone_set($appointment1->timezone);
             }
             $date1 = date('Y-m-d');
             $appointment_count = $this->appointment_screen($practice_row->practice_id);
             if ($appointment1->appointment_sent_date != $date1) {
                 $appointment_count1 = $this->appointment_reminder($practice_row->practice_id);
             }
         }
         $arr .= "Number of documents scanned: " . $results_scan . "<br>";
         $arr .= "Number of birthday announcements: " . $birthday_count . "<br>";
         $arr .= "Number of patients screened needing apointments: " . $appointment_count1 . "<br>";
         $arr .= "Number of patients reminders sent to make appointment: " . $appointment_count1 . "<br>";
     }
     $results_count = $this->get_results();
     $results_alert = $this->alert_message_send();
     $results_practice_clean = $this->clean_practice();
     $results_api = $this->process_api();
     $arr .= "Number of results obtained: " . $results_count . "<br>";
     $arr .= "Number of alerts sent: " . $results_alert . "<br>";
     $arr .= "Number of unused practices cleaned: " . $results_practice_clean . "<br>";
     $arr .= "Number of commands sent via API: " . $results_api . "<br><br>";
     return $arr;
 }
 public function action()
 {
     $errors = new MessageBag();
     if ($old = Input::old("errors")) {
         $errors = $old;
     }
     $data = array("errors" => $errors);
     if (Input::server("REQUEST_METHOD") == "POST") {
         $default_practice = DB::table('practiceinfo')->where('practice_id', '=', '1')->first();
         if ($default_practice->patient_centric == 'y') {
             $validator_array = array("username" => "required", "password" => "required");
         } else {
             $validator_array = array("username" => "required", "password" => "required", "practice_id" => "required");
         }
         $validator = Validator::make(Input::all(), $validator_array);
         if ($validator->passes()) {
             $username = Input::get('username');
             $password = Input::get('password');
             if ($default_practice->patient_centric == 'y') {
                 $credentials = array("username" => $username, "password" => $password, "active" => '1');
                 $user = User::where('username', '=', $username)->where('active', '=', '1')->first();
             } else {
                 $practice_id = Input::get('practice_id');
                 $credentials = array("username" => $username, "password" => $password, "active" => '1', "practice_id" => $practice_id);
                 $user = User::where('username', '=', $username)->where('active', '=', '1')->where('practice_id', '=', $practice_id)->first();
             }
             if (Auth::attempt($credentials)) {
                 $practice = Practiceinfo::find($user->practice_id);
                 Session::put('user_id', $user->id);
                 Session::put('group_id', $user->group_id);
                 Session::put('practice_id', $user->practice_id);
                 Session::put('version', $practice->version);
                 Session::put('practice_active', $practice->active);
                 Session::put('displayname', $user->displayname);
                 Session::put('documents_dir', $practice->documents_dir);
                 Session::put('rcopia', $practice->rcopia_extension);
                 Session::put('mtm_extension', $practice->mtm_extension);
                 Session::put('patient_centric', $practice->patient_centric);
                 setcookie("login_attempts", 0, time() + 900, '/');
                 if ($practice->patient_centric == 'n') {
                     return Redirect::intended('mobile');
                 } else {
                     if ($user->group_id != '100' && $user->group_id != '1') {
                         $pid = DB::table('demographics')->first();
                         $this->setpatient($pid->pid);
                         return Redirect::intended('chart');
                     } else {
                         return Redirect::intended('mobile');
                     }
                 }
             }
         }
         $attempts = $_COOKIE['login_attempts'] + 1;
         setcookie("login_attempts", $attempts, time() + 900, '/');
         $data["errors"] = new MessageBag(array("password" => "Username and/or password invalid."));
         $data["username"] = Input::get("username");
         return Redirect::to("login_mobile")->withInput($data);
     } else {
         $practice1 = Practiceinfo::find(1);
         Session::put('version', $practice1->version);
         $practice_id = Session::get('practice_id');
         if ($practice_id == FALSE) {
             $data['practice_id'] = '1';
         } else {
             $data['practice_id'] = $practice_id;
         }
         $data['patient_centric'] = $practice1->patient_centric;
         $practices = Practiceinfo::all();
         $practices_array = array();
         if ($practices) {
             foreach ($practices as $practice_row) {
                 $practices_array[$practice_row->practice_id] = $practice_row->practice_name;
             }
         }
         $data['practices'] = Form::select('practice_id', $practices_array, null, array('id' => 'practice_id'));
         if (array_key_exists('login_attempts', $_COOKIE) && $_COOKIE['login_attempts'] >= 5) {
             $data['attempts'] = "You have reached the number of limits to login.  Wait 15 minutes then try again.";
             $this->layout->style = HTML::style('css/mobile.css');
             $this->layout->script = $this->js_assets('base', true);
             //$this->layout->script .= HTML::script('/js/login.js');
             $this->layout->content = View::make('mobile.login', $data);
         } else {
             if (!array_key_exists('login_attempts', $_COOKIE)) {
                 setcookie("login_attempts", 0, time() + 900, '/');
             }
             $this->layout->style = HTML::style('css/mobile.css');
             $this->layout->script = $this->js_assets('base', true);
             //$this->layout->script .= HTML::script('/js/login.js');
             $this->layout->content = View::make('mobile.login', $data);
         }
     }
 }
 public function update180()
 {
     $orderslist1_array = array();
     $orderslist1_array[] = array('orders_code' => '11550', 'aoe_code' => 'CHM1^FASTING STATE:', 'aoe_field' => 'aoe_fasting_code');
     $orderslist1_array[] = array('orders_code' => '12500', 'aoe_code' => 'CHM1^FASTING STATE:', 'aoe_field' => 'aoe_fasting_code');
     $orderslist1_array[] = array('orders_code' => '24080', 'aoe_code' => 'MIC1^SOURCE:', 'aoe_field' => 'aoe_source_code');
     $orderslist1_array[] = array('orders_code' => '30000', 'aoe_code' => 'CHM1^FASTING STATE:', 'aoe_field' => 'aoe_fasting_code');
     $orderslist1_array[] = array('orders_code' => '30740', 'aoe_code' => 'CHM1^FASTING STATE:', 'aoe_field' => 'aoe_fasting_code');
     $orderslist1_array[] = array('orders_code' => '30820', 'aoe_code' => 'GLUFAST^HOURS FASTING:', 'aoe_field' => 'aoe_fasting_hours_code');
     $orderslist1_array[] = array('orders_code' => '31300', 'aoe_code' => 'CHM1^FASTING STATE:', 'aoe_field' => 'aoe_fasting_code');
     $orderslist1_array[] = array('orders_code' => '33320', 'aoe_code' => 'TDM1^LAST DOSE DATE:;TDM2^LAST DOSE TIME:', 'aoe_field' => 'aoe_dose_date_code;aoe_dose_time_code');
     $orderslist1_array[] = array('orders_code' => '43540', 'aoe_code' => 'CHM1^FASTING STATE:', 'aoe_field' => 'aoe_fasting_code');
     $orderslist1_array[] = array('orders_code' => '43542', 'aoe_code' => 'CHM1^FASTING STATE:', 'aoe_field' => 'aoe_fasting_code');
     $orderslist1_array[] = array('orders_code' => '43546', 'aoe_code' => 'CHM1^FASTING STATE:', 'aoe_field' => 'aoe_fasting_code');
     $orderslist1_array[] = array('orders_code' => '60109', 'aoe_code' => 'BFL1^SOURCE:', 'aoe_field' => 'aoe_source1_code');
     $orderslist1_array[] = array('orders_code' => '61500', 'aoe_code' => 'MIC1^SOURCE:;MIC2^ADD. INFORMATION:', 'aoe_field' => 'aoe_source_code;aoe_additional_code');
     $orderslist1_array[] = array('orders_code' => '68329', 'aoe_code' => 'MIC1^SOURCE:', 'aoe_field' => 'aoe_source_code');
     foreach ($orderslist1_array as $row1) {
         $orders_query = DB::table('orderslist1')->where('orders_code', '=', $row1['orders_code'])->get();
         foreach ($orders_query as $row2) {
             $orders_data = array('aoe_code' => $row1['aoe_code'], 'aoe_field' => $row1['aoe_field']);
             DB::table('orderslist1')->where('orderslist1_id', '=', $row2->orderslist1_id)->update($orders_data);
         }
     }
     // Update referral templates
     $template_query = DB::table('templates')->where('category', '=', 'referral')->first();
     if (!$template_query) {
         $template_array = array();
         $template_array[] = array('category' => 'referral', 'json' => '{"html":[{"type":"hidden","class":"ref_hidden","value":"Referral - Please provide primary physician with summaries of subsequent visits.","id":"ref_referral_hidden"},{"type":"checkbox","id":"ref_referral_1","class":"ref_other ref_intro","value":"Assume management for this particular problem and return patient after conclusion of care.","name":"ref_referral_1","caption":"Return patient after managing particular problem"},{"type":"br"},{"type":"checkbox","id":"ref_referral_2","class":"ref_other ref_intro","value":"Assume future management of patient within your area of expertise.","name":"ref_referral_2","caption":"Future ongoing management"},{"type":"br"},{"type":"checkbox","id":"ref_referral_3","class":"ref_other ref_after","value":"Please call me when you have seen the patient.","name":"ref_referral_3","caption":"Call back"},{"type":"br"},{"type":"checkbox","id":"ref_referral_4","class":"ref_other ref_after","value":"I would like to receive periodic status reports on this patient.","name":"ref_referral_4","caption":"Receive periodic status reports"},{"type":"br"},{"type":"checkbox","id":"ref_referral_5","class":"ref_other ref_after","value":"Please send a thorough written report when the consultation is complete.","name":"ref_referral_5","caption":"Receive thorough written report"}]}', 'group' => 'referral', 'sex' => 'm');
         $template_array[] = array('category' => 'referral', 'json' => '{"html":[{"type":"hidden","class":"ref_hidden","value":"Referral - Please provide primary physician with summaries of subsequent visits.","id":"ref_referral_hidden"},{"type":"checkbox","id":"ref_referral_1","class":"ref_other ref_intro","value":"Assume management for this particular problem and return patient after conclusion of care.","name":"ref_referral_1","caption":"Return patient after managing particular problem"},{"type":"br"},{"type":"checkbox","id":"ref_referral_2","class":"ref_other ref_intro","value":"Assume future management of patient within your area of expertise.","name":"ref_referral_2","caption":"Future ongoing management"},{"type":"br"},{"type":"checkbox","id":"ref_referral_3","class":"ref_other ref_after","value":"Please call me when you have seen the patient.","name":"ref_referral_3","caption":"Call back"},{"type":"br"},{"type":"checkbox","id":"ref_referral_4","class":"ref_other ref_after","value":"I would like to receive periodic status reports on this patient.","name":"ref_referral_4","caption":"Receive periodic status reports"},{"type":"br"},{"type":"checkbox","id":"ref_referral_5","class":"ref_other ref_after","value":"Please send a thorough written report when the consultation is complete.","name":"ref_referral_5","caption":"Receive thorough written report"}]}', 'group' => 'referral', 'sex' => 'f');
         $template_array[] = array('category' => 'referral', 'json' => '{"html":[{"type":"hidden","class":"ref_hidden","value":"Consultation - Please send the patient back for follow-up and treatment.","id":"ref_consultation_hidden"},{"type":"checkbox","id":"ref_consultation_1","class":"ref_other ref_intro","value":"Confirm the diagnosis.","name":"ref_consultation_1","caption":"Confirm the diagnosis"},{"type":"br"},{"type":"checkbox","id":"ref_consultation_2","class":"ref_other ref_intro","value":"Advise as to the diagnosis.","name":"ref_consultation_2","caption":"Advise as to the diagnosis"},{"type":"br"},{"type":"checkbox","id":"ref_consultation_3","class":"ref_other ref_intro","value":"Suggest medication or treatment for the diagnosis.","name":"ref_consultation_3","caption":"Suggest medication or treatment"},{"type":"br"},{"type":"checkbox","id":"ref_consultation_4","class":"ref_other ref_after","value":"Please call me when you have seen the patient.","name":"ref_consultation_4","caption":"Call back"},{"type":"br"},{"type":"checkbox","id":"ref_consultation_5","class":"ref_other ref_after","value":"I would like to receive periodic status reports on this patient.","name":"ref_consultation_5","caption":"Receive periodic status reports"},{"type":"br"},{"type":"checkbox","id":"ref_consultation_6","class":"ref_other ref_after","value":"Please send a thorough written report when the consultation is complete.","name":"ref_consultation_6","caption":"Receive thorough written report"}]}', 'group' => 'consultation', 'sex' => 'm');
         $template_array[] = array('category' => 'referral', 'json' => '{"html":[{"type":"hidden","class":"ref_hidden","value":"Consultation - Please send the patient back for follow-up and treatment.","id":"ref_consultation_hidden"},{"type":"checkbox","id":"ref_consultation_1","class":"ref_other ref_intro","value":"Confirm the diagnosis.","name":"ref_consultation_1","caption":"Confirm the diagnosis"},{"type":"br"},{"type":"checkbox","id":"ref_consultation_2","class":"ref_other ref_intro","value":"Advise as to the diagnosis.","name":"ref_consultation_2","caption":"Advise as to the diagnosis"},{"type":"br"},{"type":"checkbox","id":"ref_consultation_3","class":"ref_other ref_intro","value":"Suggest medication or treatment for the diagnosis.","name":"ref_consultation_3","caption":"Suggest medication or treatment"},{"type":"br"},{"type":"checkbox","id":"ref_consultation_4","class":"ref_other ref_after","value":"Please call me when you have seen the patient.","name":"ref_consultation_4","caption":"Call back"},{"type":"br"},{"type":"checkbox","id":"ref_consultation_5","class":"ref_other ref_after","value":"I would like to receive periodic status reports on this patient.","name":"ref_consultation_5","caption":"Receive periodic status reports"},{"type":"br"},{"type":"checkbox","id":"ref_consultation_6","class":"ref_other ref_after","value":"Please send a thorough written report when the consultation is complete.","name":"ref_consultation_6","caption":"Receive thorough written report"}]}', 'group' => 'consultation', 'sex' => 'f');
         $template_array[] = array('category' => 'referral', 'json' => '{"html":[{"type":"hidden","class":"ref_hidden","value":"Physical therapy referral details:","id":"ref_pt_hidden"},{"type":"div","class":"ref_buttonset","id":"ref_pt_1_div","html":[{"type":"span","html":"Objectives:"},{"type":"br"},{"type":"checkbox","id":"ref_pt_1a","class":"ref_other ref_intro","value":"Decrease pain.","name":"ref_pt_1","caption":"Decrease pain"},{"type":"checkbox","id":"ref_pt_1b","class":"ref_other ref_intro","value":"Increase strength.","name":"ref_pt_1","caption":"Increase strength"},{"type":"checkbox","id":"ref_pt_1c","class":"ref_other ref_intro","value":"Increase mobility.","name":"ref_pt_1","caption":"Increase mobility"}]},{"type":"br"},{"type":"div","class":"ref_buttonset","id":"ref_pt_2_div","html":[{"type":"span","html":"Modalities:"},{"type":"br"},{"type":"select","multiple":"multiple","id":"ref_pt_2","class":"ref_select ref_intro","css":{"width":"200px"},"name":"ref_pt_2","caption":"","options":{"Hot or cold packs. ":"Hot or cold packs.","TENS unit. ":"TENS unit.","Back program. ":"Back program.","Joint mobilization. ":"Joint mobilization.","Home program. ":"Home program.","Pool therapy. ":"Pool therapy.","Feldenkrais method. ":"Feldenkrais method.","Therapeutic exercise. ":"Therapeutic exercise.","Myofascial release. ":"Myofascial release.","Patient education. ":"Patient education.","Work hardening. ":"Work hardening."}}]},{"type":"br"},{"type":"text","id":"ref_pt_3","css":{"width":"200px"},"class":"ref_other ref_detail_text ref_intro","name":"ref_pt_3","placeholder":"Precautions"},{"type":"br"},{"type":"text","id":"ref_pt_4","css":{"width":"200px"},"class":"ref_other ref_detail_text ref_intro","name":"ref_pt_4","placeholder":"Frequency"},{"type":"br"},{"type":"text","id":"ref_pt_5","css":{"width":"200px"},"class":"ref_other ref_detail_text ref_intro","name":"ref_pt_5","placeholder":"Duration"}]}', 'group' => 'pt', 'sex' => 'm');
         $template_array[] = array('category' => 'referral', 'json' => '{"html":[{"type":"hidden","class":"ref_hidden","value":"Physical therapy referral details:","id":"ref_pt_hidden"},{"type":"div","class":"ref_buttonset","id":"ref_pt_1_div","html":[{"type":"span","html":"Objectives:"},{"type":"br"},{"type":"checkbox","id":"ref_pt_1a","class":"ref_other ref_intro","value":"Decrease pain.","name":"ref_pt_1","caption":"Decrease pain"},{"type":"checkbox","id":"ref_pt_1b","class":"ref_other ref_intro","value":"Increase strength.","name":"ref_pt_1","caption":"Increase strength"},{"type":"checkbox","id":"ref_pt_1c","class":"ref_other ref_intro","value":"Increase mobility.","name":"ref_pt_1","caption":"Increase mobility"}]},{"type":"br"},{"type":"div","class":"ref_buttonset","id":"ref_pt_2_div","html":[{"type":"span","html":"Modalities:"},{"type":"br"},{"type":"select","multiple":"multiple","id":"ref_pt_2","class":"ref_select ref_intro","css":{"width":"200px"},"name":"ref_pt_2","caption":"","options":{"Hot or cold packs. ":"Hot or cold packs.","TENS unit. ":"TENS unit.","Back program. ":"Back program.","Joint mobilization. ":"Joint mobilization.","Home program. ":"Home program.","Pool therapy. ":"Pool therapy.","Feldenkrais method. ":"Feldenkrais method.","Therapeutic exercise. ":"Therapeutic exercise.","Myofascial release. ":"Myofascial release.","Patient education. ":"Patient education.","Work hardening. ":"Work hardening."}}]},{"type":"br"},{"type":"text","id":"ref_pt_3","css":{"width":"200px"},"class":"ref_other ref_detail_text ref_intro","name":"ref_pt_3","placeholder":"Precautions"},{"type":"br"},{"type":"text","id":"ref_pt_4","css":{"width":"200px"},"class":"ref_other ref_detail_text ref_intro","name":"ref_pt_4","placeholder":"Frequency"},{"type":"br"},{"type":"text","id":"ref_pt_5","css":{"width":"200px"},"class":"ref_other ref_detail_text ref_intro","name":"ref_pt_5","placeholder":"Duration"}]}', 'group' => 'pt', 'sex' => 'f');
         $template_array[] = array('category' => 'referral', 'json' => '{"html":[{"type":"hidden","class":"ref_hidden","value":"Massage therapy referral details:","id":"ref_massage_hidden"},{"type":"div","class":"ref_buttonset","id":"ref_massage_1_div","html":[{"type":"span","html":"Objectives:"},{"type":"br"},{"type":"checkbox","id":"ref_massage_1a","class":"ref_other ref_intro","value":"Decrease pain.","name":"ref_massage_1","caption":"Decrease pain"},{"type":"checkbox","id":"ref_massage_1b","class":"ref_other ref_intro","value":"Increase mobility.","name":"ref_massage_1","caption":"Increase mobility"}]},{"type":"br"},{"type":"text","id":"ref_massage_2","css":{"width":"200px"},"class":"ref_other ref_detail_text ref_intro","name":"ref_massage_2","placeholder":"Precautions"},{"type":"br"},{"type":"text","id":"ref_massage_3","css":{"width":"200px"},"class":"ref_other ref_detail_text ref_intro","name":"ref_massage_3","placeholder":"Frequency"},{"type":"br"},{"type":"text","id":"ref_massage_4","css":{"width":"200px"},"class":"ref_other ref_detail_text ref_intro","name":"ref_massage_4","placeholder":"Duration"}]}', 'group' => 'massage', 'sex' => 'm');
         $template_array[] = array('category' => 'referral', 'json' => '{"html":[{"type":"hidden","class":"ref_hidden","value":"Massage therapy referral details:","id":"ref_massage_hidden"},{"type":"div","class":"ref_buttonset","id":"ref_massage_1_div","html":[{"type":"span","html":"Objectives:"},{"type":"br"},{"type":"checkbox","id":"ref_massage_1a","class":"ref_other ref_intro","value":"Decrease pain.","name":"ref_massage_1","caption":"Decrease pain"},{"type":"checkbox","id":"ref_massage_1b","class":"ref_other ref_intro","value":"Increase mobility.","name":"ref_massage_1","caption":"Increase mobility"}]},{"type":"br"},{"type":"text","id":"ref_massage_2","css":{"width":"200px"},"class":"ref_other ref_detail_text ref_intro","name":"ref_massage_2","placeholder":"Precautions"},{"type":"br"},{"type":"text","id":"ref_massage_3","css":{"width":"200px"},"class":"ref_other ref_detail_text ref_intro","name":"ref_massage_3","placeholder":"Frequency"},{"type":"br"},{"type":"text","id":"ref_massage_4","css":{"width":"200px"},"class":"ref_other ref_detail_text ref_intro","name":"ref_massage_4","placeholder":"Duration"}]}', 'group' => 'massage', 'sex' => 'f');
         $template_array[] = array('category' => 'referral', 'json' => '{"html":[{"type":"hidden","class":"ref_hidden","value":"Sleep study referral details:","id":"ref_sleep_study_hidden"},{"type":"div","class":"ref_buttonset","id":"ref_sleep_study_1_div","html":[{"type":"span","html":"Type:"},{"type":"br"},{"type":"select","multiple":"multiple","id":"ref_sleep_study_1","class":"ref_select ref_other ref_intro","css":{"width":"200px"},"name":"ref_sleep_study_1","caption":"","options":{"Diagnostic Sleep Study Only.\\n":"Diagnostic Sleep Study Only.","Diagnostic testing with Continuous Positive Airway Pressure.\\n":"Diagnostic testing with Continuous Positive Airway Pressure.","Diagnostic testing with BiLevel Positive Airway Pressure.\\n":"Diagnostic testing with BiLevel Positive Airway Pressure.","Diagnostic testing with BiLevel Positive Airway Pressure.\\n":"Diagnostic testing with BiLevel Positive Airway Pressure.","Diagnostic testing with Oxygen.\\n":"Diagnostic testing with Oxygen.","Diagnostic testing with Oral Device.\\n":"Diagnostic testing with Oral Device.","MSLT (Multiple Sleep Latency Test).\\n":"MSLT (Multiple Sleep Latency Test).","MWT (Maintenance of Wakefulness Test).\\n":"MWT (Maintenance of Wakefulness Test).","Titrate BiPAP settings.\\n":"Titrate BiPAP settings.","Patient education. ":"Patient education.","Work hardening. ":"Work hardening."}}]},{"type":"br"},{"type":"div","class":"ref_buttonset","id":"ref_sleep_study_2_div","html":[{"type":"span","html":"BiPAP pressures:"},{"type":"br"},{"type":"text","id":"ref_sleep_study_2a","css":{"width":"200px"},"class":"ref_other ref_detail_text ref_intro","name":"ref_sleep_study_2a","placeholder":"Inspiratory Pressure (IPAP), cm H20"},{"type":"br"},{"type":"text","id":"ref_sleep_study_2b","css":{"width":"200px"},"class":"ref_other ref_detail_text ref_intro","name":"ref_sleep_study_2b","placeholder":"Expiratory Pressure (EPAP), cm H20"}]},{"type":"br"},{"type":"div","class":"ref_buttonset","id":"ref_sleep_study_3_div","html":[{"type":"span","html":"BiPAP Mode:"},{"type":"br"},{"type":"checkbox","id":"ref_sleep_study_3a","class":"ref_other ref_intro","value":"Spontaneous mode.","name":"ref_sleep_study_3","caption":"Spontaneous"},{"type":"checkbox","id":"ref_sleep_study_3b","class":"ref_other ref_intro","value":"Spontaneous/Timed mode","name":"ref_sleep_study_3","caption":"Spontaneous/Timed"},{"type":"br"},{"type":"text","id":"ref_sleep_study_3c","css":{"width":"200px"},"class":"ref_other ref_detail_text ref_intro","name":"ref_sleep_study_3","placeholder":"Breaths per minute"}]}]}', 'group' => 'sleep_study', 'sex' => 'm');
         $template_array[] = array('category' => 'referral', 'json' => '{"html":[{"type":"hidden","class":"ref_hidden","value":"Sleep study referral details:","id":"ref_sleep_study_hidden"},{"type":"div","class":"ref_buttonset","id":"ref_sleep_study_1_div","html":[{"type":"span","html":"Type:"},{"type":"br"},{"type":"select","multiple":"multiple","id":"ref_sleep_study_1","class":"ref_select ref_other ref_intro","css":{"width":"200px"},"name":"ref_sleep_study_1","caption":"","options":{"Diagnostic Sleep Study Only.\\n":"Diagnostic Sleep Study Only.","Diagnostic testing with Continuous Positive Airway Pressure.\\n":"Diagnostic testing with Continuous Positive Airway Pressure.","Diagnostic testing with BiLevel Positive Airway Pressure.\\n":"Diagnostic testing with BiLevel Positive Airway Pressure.","Diagnostic testing with BiLevel Positive Airway Pressure.\\n":"Diagnostic testing with BiLevel Positive Airway Pressure.","Diagnostic testing with Oxygen.\\n":"Diagnostic testing with Oxygen.","Diagnostic testing with Oral Device.\\n":"Diagnostic testing with Oral Device.","MSLT (Multiple Sleep Latency Test).\\n":"MSLT (Multiple Sleep Latency Test).","MWT (Maintenance of Wakefulness Test).\\n":"MWT (Maintenance of Wakefulness Test).","Titrate BiPAP settings.\\n":"Titrate BiPAP settings.","Patient education. ":"Patient education.","Work hardening. ":"Work hardening."}}]},{"type":"br"},{"type":"div","class":"ref_buttonset","id":"ref_sleep_study_2_div","html":[{"type":"span","html":"BiPAP pressures:"},{"type":"br"},{"type":"text","id":"ref_sleep_study_2a","css":{"width":"200px"},"class":"ref_other ref_detail_text ref_intro","name":"ref_sleep_study_2a","placeholder":"Inspiratory Pressure (IPAP), cm H20"},{"type":"br"},{"type":"text","id":"ref_sleep_study_2b","css":{"width":"200px"},"class":"ref_other ref_detail_text ref_intro","name":"ref_sleep_study_2b","placeholder":"Expiratory Pressure (EPAP), cm H20"}]},{"type":"br"},{"type":"div","class":"ref_buttonset","id":"ref_sleep_study_3_div","html":[{"type":"span","html":"BiPAP Mode:"},{"type":"br"},{"type":"checkbox","id":"ref_sleep_study_3a","class":"ref_other ref_intro","value":"Spontaneous mode.","name":"ref_sleep_study_3","caption":"Spontaneous"},{"type":"checkbox","id":"ref_sleep_study_3b","class":"ref_other ref_intro","value":"Spontaneous/Timed mode","name":"ref_sleep_study_3","caption":"Spontaneous/Timed"},{"type":"br"},{"type":"text","id":"ref_sleep_study_3c","css":{"width":"200px"},"class":"ref_other ref_detail_text ref_intro","name":"ref_sleep_study_3","placeholder":"Breaths per minute"}]}]}', 'group' => 'sleep_study', 'sex' => 'f');
         foreach ($template_array as $template_ind) {
             $template_array = serialize(json_decode($template_ind['json']));
             $template_data = array('user_id' => '0', 'template_name' => 'Global Default', 'default' => 'default', 'category' => $template_ind['category'], 'sex' => $template_ind['sex'], 'group' => $template_ind['group'], 'array' => $template_array);
             DB::table('templates')->insert($template_data);
         }
     }
     // Update image links and create scans and received faxes directories if needed
     $practices = Practiceinfo::all();
     foreach ($practices as $practice) {
         $practice->practice_logo = str_replace("/var/www/nosh/", "", $practice->practice_logo);
         $practice->save();
         $scans_dir = $practice->documents_dir . 'scans/' . $practice->practice_id;
         if (!file_exists($scans_dir)) {
             mkdir($scans_dir, 0777);
         }
         $received_dir = $practice->documents_dir . 'received/' . $practice->practice_id;
         if (!file_exists($received_dir)) {
             mkdir($received_dir, 0777);
         }
     }
     $providers = Providers::all();
     foreach ($providers as $provider) {
         $provider->signature = str_replace("/var/www/nosh/", "", $provider->signature);
         $provider->save();
     }
     // Assign standard encounter templates
     DB::table('encounters')->update(array('encounter_template' => 'standardmedical'));
     // Move scans and received faxes
     $scans = DB::table('scans')->get();
     if ($scans) {
         foreach ($scans as $scan) {
             $practice1 = Practiceinfo::find($scan->practice_id);
             $new_scans_dir = $practice1->documents_dir . 'scans/' . $scan->practice_id;
             $scans_data['filePath'] = str_replace('/var/www/nosh/scans', $new_scans_dir, $scan->filePath);
             rename($scan->filePath, $scans_data['filePath']);
             DB::table('scans')->where('scans_id', '=', $scan->scans_id)->update($scans_data);
         }
     }
     $received = DB::table('received')->get();
     if ($received) {
         foreach ($received as $fax) {
             $fax_practice_id = '1';
             if ($fax->practice_id != '' && $fax->practice_id != '0') {
                 $fax_practice_id = $fax->practice_id;
             }
             $practice2 = Practiceinfo::find($fax_practice_id);
             $new_received_dir = $practice2->documents_dir . 'received/' . $fax_practice_id;
             $received_data['filePath'] = str_replace('/var/www/nosh/received', $new_received_dir, $fax->filePath);
             if (file_exists($fax->filePath)) {
                 rename($fax->filePath, $received_data['filePath']);
             }
             DB::table('received')->where('received_id', '=', $fax->received_id)->update($received_data);
         }
     }
     // Migrate bill_complex field to encounters
     $encounters = DB::table('encounters')->get();
     if ($encounters) {
         foreach ($encounters as $encounter) {
             $billing = DB::table('billing')->where('eid', '=', $encounter->eid)->where(function ($query_array1) {
                 $query_array1->where('bill_complex', '!=', "")->orWhereNotNull('bill_complex');
             })->first();
             $data['bill_complex'] = '';
             if ($billing) {
                 $data['bill_complex'] = $billing->bill_complex;
             }
             DB::table('encounters')->where('eid', '=', $encounter->eid)->update($data);
         }
     }
     $db_name = $_ENV['mysql_database'];
     $db_username = $_ENV['mysql_username'];
     $db_password = $_ENV['mysql_password'];
     DB::table('meds_full')->truncate();
     $meds_sql_file = __DIR__ . "/../../import/meds_full.sql";
     $meds_command = "mysql -u " . $db_username . " -p" . $db_password . " " . $db_name . " < " . $meds_sql_file;
     system($meds_command);
     DB::table('meds_full_package')->truncate();
     $meds1_sql_file = __DIR__ . "/../../import/meds_full_package.sql";
     $meds1_command = "mysql -u " . $db_username . " -p" . $db_password . " " . $db_name . " < " . $meds1_sql_file;
     system($meds1_command);
     DB::table('supplements_list')->truncate();
     $supplements_file = __DIR__ . "/../../import/supplements_list.sql";
     $supplements_command = "mysql -u " . $db_username . " -p" . $db_password . " " . $db_name . " < " . $supplements_file;
     system($supplements_command);
     DB::table('icd9')->truncate();
     $icd_file = __DIR__ . "/../../import/icd9.sql";
     $icd_command = "mysql -u " . $db_username . " -p" . $db_password . " " . $db_name . " < " . $icd_file;
     system($icd_command);
     $alpha_array = array('1' => 'A', '2' => 'B', '3' => 'C', '4' => 'D', '5' => 'E', '6' => 'F', '7' => 'G', '8' => 'H');
     // Update ICD pointers to reflect new HCFA-1500
     $billing_core = DB::table('billing_core')->whereNotNull('icd_pointer')->get();
     if ($billing_core) {
         foreach ($billing_core as $billing_core_row) {
             if ($billing_core_row->icd_pointer != '') {
                 $icd_pointer = $billing_core_row->icd_pointer;
                 foreach ($alpha_array as $key => $value) {
                     $icd_pointer = str_replace($key, $value, $icd_pointer);
                 }
                 $billing_core_data['icd_pointer'] = $icd_pointer;
                 DB::table('billing_core')->where('billing_core_id', '=', $billing_core_row->billing_core_id)->update($billing_core_data);
             }
         }
     }
     // Update calendar
     $calendar['provider_id'] = '0';
     DB::table('calendar')->update($calendar);
     // Update version
     DB::table('practiceinfo')->update(array('version' => '1.8.0'));
 }