public function InsertEncounter($enc_array, $pid, $revapprove = 1)
 {
     $appTable = new ApplicationTable();
     foreach ($enc_array as $key => $value) {
         $encounter_id = $appTable->generateSequenceID();
         $query_sel_users = "SELECT * \n                              FROM users \n                              WHERE abook_type='external_provider' AND npi=?";
         $res_query_sel_users = $appTable->zQuery($query_sel_users, array($value['provider_npi']));
         if ($res_query_sel_users->count() > 0) {
             foreach ($res_query_sel_users as $value1) {
                 $provider_id = $value1['id'];
             }
         } else {
             $query_ins_users = "INSERT INTO users\n                                ( username,\n                                  fname,\n                                  npi,\n                                  organization,\n                                  street,\n                                  city,\n                                  state,\n                                  zip,\n                                  active,\n                                  abook_type\n                                )\n                                VALUES\n                                (\n                                  ?,\n                                  ?,\n                                  ?,\n                                  ?,\n                                  ?,\n                                  ?,\n                                  ?,\n                                  ?,\n                                  1,\n                                  'external_provider'\n                                )";
             $res_query_ins_users = $appTable->zQuery($query_ins_users, array('', $value['provider_name'], $value['provider_npi'], $value['represented_organization_name'], $value['provider_address'], $value['provider_city'], $value['provider_state'], $value['provider_postalCode']));
             $provider_id = $res_query_ins_users->getGeneratedValue();
         }
         //facility
         $query_sel_fac = "SELECT * \n                            FROM users \n                            WHERE abook_type='external_org' AND organization=?";
         $res_query_sel_fac = $appTable->zQuery($query_sel_fac, array($value['represented_organization_name']));
         if ($res_query_sel_fac->count() > 0) {
             foreach ($res_query_sel_fac as $value2) {
                 $facility_id = $value2['id'];
             }
         } else {
             $query_ins_fac = "INSERT INTO users\n                              ( username,\n                                organization,\n                                phonecell,\n                                street,\n                                city,\n                                state,\n                                zip,\n                                active,\n                                abook_type\n                              )\n                              VALUES\n                              (\n                                ?,\n                                ?,\n                                ?,\n                                ?,\n                                ?,\n                                ?,\n                                ?,\n                                1,\n                                'external_org'\n                              )";
             $res_query_ins_fac = $appTable->zQuery($query_ins_fac, array('', $value['represented_organization_name'], $value['represented_organization_telecom'], $value['represented_organization_address'], $value['represented_organization_city'], $value['represented_organization_state'], $value['represented_organization_zip']));
             $facility_id = $res_query_ins_fac->getGeneratedValue();
         }
         if ($value['date'] != 0 && $revapprove == 0) {
             $encounter_date = $this->formatDate($value['date'], 1);
             $encounter_date_value = fixDate($encounter_date);
         } elseif ($value['date'] != 0 && $revapprove == 1) {
             $encounter_date_value = \Application\Model\ApplicationTable::fixDate($value['date'], 'yyyy-mm-dd', 'dd/mm/yyyy');
         } elseif ($value['date'] == 0) {
             $encounter_date = $value['date'];
             $encounter_date_value = fixDate($encounter_date);
         }
         $q_sel_encounter = "SELECT * \n                               FROM form_encounter\n                               WHERE external_id=? AND pid=?";
         $res_q_sel_encounter = $appTable->zQuery($q_sel_encounter, array($value['extension'], $pid));
         if ($res_q_sel_encounter->count() == 0) {
             $query_insert1 = "INSERT INTO form_encounter\n                           (\n                            pid,\n                            encounter,\n                            date,\n                            facility,\n                            facility_id,\n                            provider_id,\n                            external_id\n                           )\n                           VALUES\n                           (\n                            ?,\n                            ?,\n                            ?,\n                            ?,\n                            ?,\n                            ?,\n                            ?\n                           )";
             $result = $appTable->zQuery($query_insert1, array($pid, $encounter_id, $encounter_date_value, $value['represented_organization_name'], $facility_id, $provider_id, $value['extension']));
             $enc_id = $result->getGeneratedValue();
         } else {
             $q_upd_encounter = "UPDATE form_encounter\n                            SET pid=?,\n                                encounter=?,\n                                date=?,\n                                facility=?,\n                                facility_id=?,\n                                provider_id=?\n                            WHERE external_id=? AND pid=?";
             $appTable->zQuery($q_upd_encounter, array($pid, $encounter_id, $encounter_date_value, $value['represented_organization_name'], $facility_id, $provider_id, $value['extension'], $pid));
             $q_sel_enc = "SELECT id FROM form_encounter WHERE external_id=?";
             $res_q_sel_enc = $appTable->zQuery($q_sel_enc, array($value['extension']));
             $res_enc_cur = $res_q_sel_enc->current();
             $enc_id = $res_enc_cur['id'];
         }
         $q_ins_forms = "INSERT INTO forms (date,encounter,form_name,form_id,pid,user,groupname,deleted,formdir) VALUES (?,?,?,?,?,?,?,?,?)";
         $appTable->zQuery($q_ins_forms, array($encounter_date_value, $encounter_id, 'New Patient Encounter', $enc_id, $pid, $_SESSION["authProvider"], 'Default', 0, 'newpatient'));
         if ($value['encounter_diagnosis_issue'] != '') {
             $query_select = "SELECT * FROM lists WHERE begdate = ? AND title = ? AND pid = ?";
             $result = $appTable->zQuery($query_select, array($value['encounter_diagnosis_date'], $value['encounter_diagnosis_issue'], $pid));
             if ($result->count() > 0) {
                 foreach ($result as $value1) {
                     $list_id = $value1['id'];
                 }
             } else {
                 //to lists
                 $query_insert = "INSERT INTO lists(pid,type,begdate,activity,title,date, diagnosis) VALUES (?,?,?,?,?,?,?)";
                 $result = $appTable->zQuery($query_insert, array($pid, 'medical_problem', $value['encounter_diagnosis_date'], 1, $value['encounter_diagnosis_issue'], date('Y-m-d H:i:s'), 'SNOMED-CT:' . $value['encounter_diagnosis_code']));
                 $list_id = $result->getGeneratedValue();
             }
             //Linking issue with encounter
             $q_sel_iss_enc = "SELECT * FROM issue_encounter WHERE pid=? and list_id=? and encounter=?";
             $res_sel_iss_enc = $appTable->zQuery($q_sel_iss_enc, array($pid, $list_id, $encounter_id));
             if ($res_sel_iss_enc->count() == 0) {
                 $insert = "INSERT INTO issue_encounter(pid,list_id,encounter,resolved) VALUES (?,?,?,?)";
                 $appTable->zQuery($insert, array($pid, $list_id, $encounter_id, 0));
             }
         }
         //to external_encounters
         $insertEX = "INSERT INTO external_encounters(ee_date,ee_pid,ee_provider_id,ee_facility_id,ee_encounter_diagnosis,ee_external_id) VALUES (?,?,?,?,?,?)";
         $appTable->zQuery($insertEX, array($encounter_date_value, $pid, $provider_id, $facility_id, $value['encounter_diagnosis_issue'], $value['extension']));
     }
 }