Example #1
0
    function process_mc() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
            //print_r($arg_list);
        }
        if ($post_vars["submitmc"]) {
            $patient_id = healthcenter::get_patient_id($get_vars["consult_id"]);
            $patient_age = patient::get_age($patient_id);
            if ($post_vars["lmp_date"]) {
                list($month, $day, $year) = explode("/", $post_vars["lmp_date"]);
                $lmp_date = "$year-".str_pad($month,2,"0",STR_PAD_LEFT)."-".str_pad($day,2,"0",STR_PAD_LEFT);
            }
            switch($post_vars["submitmc"]) {

			case "Save Risk Factor":

				if(empty($_POST[risk_date])):
					echo "<script language='Javascript'>";
					echo "window.alert('Please supply date of when the risk factor was detected.')";
					echo "</script>";
				
				elseif(empty($get_vars[mc_id])):
					echo "<script language='Javascript'>";
					echo "window.alert('Open a maternal record first!')";
					echo "</script>";

				else:					
					list($month,$date,$year) = explode('/',$_POST[risk_date]);
					$risk_date = $year.'-'.$month.'-'.$date;

					$q_risk = mysql_query("SELECT visit_risk_id FROM m_consult_mc_visit_risk WHERE mc_id='$get_vars[mc_id]' AND visit_risk_id='$_POST[risk_code]' AND date_detected='$risk_date'") or die("Cannot query: 582");					

					if(mysql_num_rows($q_risk)==0):
												
						$insert_risk = mysql_query("INSERT INTO m_consult_mc_visit_risk SET consult_id='$get_vars[consult_id]',patient_id='$patient_id',mc_id='$get_vars[mc_id]',visit_risk_id='$_POST[risk_code]',date_detected='$risk_date',risk_timestamp=sysdate(),user_id='$_SESSION[userid]'") or die(mysql_error());

						if($insert_risk):
							echo "<font color='red'>Record successfully added.</font>";
						endif;

					else:
						echo "<font color='red'>Risk code and date already exists.</font>";
					endif;
				endif;


				break;

			case "Delete Risk Factor";				

				if(isset($_POST[riskcode]) && $_POST[confirm_del]==1):
						
					while(list($risk_key,$risk_value)=each($_POST[riskcode])){
						list($riskid,$mcid,$datedetected) = explode('*',$risk_value);
							
						$del_risk = mysql_query("DELETE FROM m_consult_mc_visit_risk WHERE mc_id='$mcid' AND visit_risk_id='$riskid' AND date_detected='$datedetected'") or die("Cannot query: 615");
					}

					if($del_risk):
						echo "<font color='red'>Risk factor was successfully deleted.</font>";
						header("Location: $_SERVER[PHP_SELF]?page=CONSULTS&menu_id=$get_vars[menu_id]&consult_id=$get_vars[consult_id]&ptmenu=DETAILS&module=mc&mc=RF&mc_id=$get_vars[mc_id]");						
					endif;	
				endif;
				
				break;
				
			case "Save Prenatal Remarks":
				$update_prenatal = mysql_query("UPDATE m_patient_mc SET prenatal_remarks='$_POST[prenatal_remarks]' WHERE mc_id='$get_vars[mc_id]'") or die("Cannot query: 634"); 

				if($update_prenatal):
					header("Location: $_SERVER[PHP_SELF]?page=CONSULTS&menu_id=$get_vars[menu_id]&consult_id=$get_vars[consult_id]&ptmenu=DETAILS&module=mc&mc=PREN&mc_id=$get_vars[mc_id]#prerem");
				endif;

				break;

			case "Save Postpartum Remarks":
				$update_post = mysql_query("UPDATE m_patient_mc SET postpartum_remarks='$_POST[postpartum_remarks]' WHERE mc_id='$get_vars[mc_id]'") or die("Cannot query: 645");


				if($update_post):
					header("Location: $_SERVER[PHP_SELF]?page=CONSULTS&menu_id=$get_vars[menu_id]&consult_id=$get_vars[consult_id]&ptmenu=DETAILS&module=mc&mc=POSTP&mc_id=$get_vars[mc_id]#postrem");					
				endif;
				
				break;


            case "Save Postpartum Visit":
                if ($post_vars["visit_date"] && $post_vars["visit_type"] &&
                    $post_vars["patient_systolic"] && $post_vars["patient_diastolic"]) {
		
		    print_r($post_vars);
		    $wk_count = mc::get_pp_week($post_vars["mc_id"],$post_vars["visit_date"]);		    

		    if($wk_count>0):
	                    list($month, $day, $year) = explode("/", $post_vars["visit_date"]);
        	            $visit_date = "$year-".str_pad($month,2,"0",STR_PAD_LEFT)."-".str_pad($day,2,"0",STR_PAD_LEFT);
        	            $breastfeeding_flag = ($post_vars["breastfeeding_flag"]?"Y":"N");
        	            $family_planning_flag = ($post_vars["family_planning_flag"]?"Y":"N");
        	            $fever_flag = ($post_vars["fever_flag"]?"Y":"N");
        	            $vaginal_infection_flag = ($post_vars["vaginal_infection_flag"]?"Y":"N");
        	            $vaginal_bleeding_flag = ($post_vars["vaginal_bleeding_flag"]?"Y":"N");
        	            $pallor_flag = ($post_vars["pallor_flag"]?"Y":"N");
        	            $cord_ok_flag = ($post_vars["cord_ok_flag"]?"Y":"N");
	
        	            $sql = "insert into m_consult_mc_postpartum (mc_id, consult_id, ".
        	                   "patient_id, postpartum_timestamp, postpartum_date, user_id, ".
        	                   "postpartum_week, visit_sequence, visit_type, ".
        	                   "breastfeeding_flag, family_planning_flag, fever_flag, ".
        	                   "vaginal_infection_flag, vaginal_bleeding_flag, ".
        	                   "pallor_flag, cord_ok_flag, blood_pressure_systolic, ".
        	                   "blood_pressure_diastolic) ".
        	                   "values ('".$post_vars["mc_id"]."', '".$get_vars["consult_id"]."', ".
        	                   "'$patient_id', sysdate(), '$visit_date', '".$_SESSION["userid"]."', ".
        	                   "'".$wk_count."', '".$post_vars["visit_sequence"]."', ".
        	                   "'".$post_vars["visit_type"]."', '$breastfeeding_flag', ".
        	                   "'$family_planning_flag', '$fever_flag', '$vaginal_infection_flag', ".
        	                   "'$vaginal_bleeding_flag', '$pallor_flag', '$cord_ok_flag', ".
        	                   "'".$post_vars["patient_systolic"]."', '".$post_vars["patient_diastolic"]."')"; 
                    if ($result = mysql_query($sql)):
                        header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=".$get_vars["menu_id"]."&consult_id=".$get_vars["consult_id"]."&ptmenu=".$get_vars["ptmenu"]."&module=mc&mc=POSTP&mc_id=".$get_vars["mc_id"]);
                    endif;

		    else:
			echo "<font color='red'>Postpartum visit date should be after date of delivery.</font>";
		    endif;
                }
		else{
			echo "<font color='red'>Postpartum visit not saved. Please check visit date, type of visit and systolic/diastolic.</font>";
		}
                break;


		case "Update Postpartum Data Form":
			break;

		case "Update Postpartum":
				
                if ($post_vars["visit_date"] && $post_vars["visit_type"] &&
                    $post_vars["patient_systolic"] && $post_vars["patient_diastolic"]) {
		    
		    $wk_count = mc::get_pp_week($get_vars["mc_id"],$post_vars["visit_date"]);

                    list($month, $day, $year) = explode("/", $post_vars["visit_date"]);
                    $visit_date = "$year-".str_pad($month,2,"0",STR_PAD_LEFT)."-".str_pad($day,2,"0",STR_PAD_LEFT);
                    $breastfeeding_flag = ($post_vars["breastfeeding_flag"]?"Y":"N");
                    $family_planning_flag = ($post_vars["family_planning_flag"]?"Y":"N");
                    $fever_flag = ($post_vars["fever_flag"]?"Y":"N");
                    $vaginal_infection_flag = ($post_vars["vaginal_infection_flag"]?"Y":"N");
                    $vaginal_bleeding_flag = ($post_vars["vaginal_bleeding_flag"]?"Y":"N");
                    $pallor_flag = ($post_vars["pallor_flag"]?"Y":"N");
                    $cord_ok_flag = ($post_vars["cord_ok_flag"]?"Y":"N");

		    if($wk_count>0):

                    $sql = "update m_consult_mc_postpartum set ".
                           "postpartum_date = '$visit_date', ".
			   "postpartum_week = '$wk_count', ".
                           "visit_type = '".$post_vars["visit_type"]."', ".
                           "blood_pressure_systolic = '".$post_vars["patient_systolic"]."', ".
                           "blood_pressure_diastolic = '".$post_vars["patient_diastolic"]."', ".
                           "breastfeeding_flag = '$breastfeeding_flag', ".
                           "family_planning_flag = '$family_planning_flag', ".
                           "fever_flag = '$fever_flag', ".
                           "vaginal_infection_flag = '$vaginal_infection_flag', ".
                           "vaginal_bleeding_flag = '$vaginal_bleeding_flag', ".
                           "pallor_flag = '$pallor_flag', ".
                           "cord_ok_flag = '$cord_ok_flag' ".
                           "where mc_id = '".$get_vars["mc_id"]."' and ".
                           "visit_sequence = '".$post_vars["visit_sequence"]."' and ".
                           "consult_id = '".$get_vars["consult_id"]."'";

    		     $result = mysql_query($sql) or die("Cannot query: 721");
                    
		     if ($result):
                        header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=".$get_vars["menu_id"]."&consult_id=".$get_vars["consult_id"]."&ptmenu=DETAILS&module=mc&mc=POSTP&mc_id=".$get_vars["mc_id"]."&ppvisitseq=".$get_vars["ppvisitseq"]);				
                     endif;
		    else:
			echo "<font color='red'>Postpartum visit date should be after date of delivery.</font>";			
		    endif;
                }
		else{
			echo "<font color='red'>Postpartum visit not updated. Please check visit date, type of visit and systolic/diastolic.</font>";
		}
                break;
            case "Delete Postpartum":
                if (module::confirm_delete($menu_id, $post_vars, $get_vars)) {
                    $sql = "delete from m_consult_mc_postpartum ".
                           "where mc_id = '".$get_vars["mc_id"]."' and ".
                           "consult_id = '".$get_vars["consult_id"]."' and ".
                           "visit_sequence = '".$post_vars["visit_sequence"]."'";
                    if ($result = mysql_query($sql)) {
                        header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=".$get_vars["menu_id"]."&consult_id=".$get_vars["consult_id"]."&ptmenu=DETAILS&module=mc&mc=POSTP&mc_id=".$get_vars["mc_id"]);
                    }
                } else {
                    if ($post_vars["confirm_delete"]=="No") {
                        header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=".$get_vars["menu_id"]."&consult_id=".$get_vars["consult_id"]."&ptmenu=DETAILS&module=mc&mc=POSTP&mc_id=".$get_vars["mc_id"]);
                    }
                }
                break;
            case "Import Data":
                if ($post_vars["vaccines"]) {
                    // remember, no need to update m_consult_vaccine
                    // since these data are coming from that source!
                    foreach($post_vars["vaccines"] as $key=>$value) {
                        // get complete data from m_consult_vaccine
                        $sql_vaccine = "select patient_id, consult_id, vaccine_id, actual_vaccine_date, adr_flag, vaccine_timestamp ".
                                       "from m_consult_vaccine ".
                                       "where patient_id = '".$post_vars["patient_id"]."' and ".
                                       "vaccine_id = '$value'";
                        if ($result_vaccine = mysql_query($sql_vaccine)) {
                            if (mysql_num_rows($result_vaccine)) {
                                while ($vaccine = mysql_fetch_array($result_vaccine)) {
                                    //$sql_import = "insert into m_consult_mc_vaccine (mc_id, consult_id, user_id, patient_id, vaccine_timestamp, actual_vaccine_date, vaccine_id) "."values ('".$post_vars["mc_id"]."', '".$vaccine["consult_id"]."', '".$_SESSION["userid"]."', '".$vaccine["patient_id"]."', sysdate(), '".$vaccine["actual_vaccine_date"]."', '".$vaccine["vaccine_id"]."')";
									
									$sql_import = "INSERT INTO m_consult_mc_vaccine SET mc_id='$post_vars[mc_id]',consult_id='$vaccine[consult_id]',user_id='$_SESSION[userid]',patient_id='$vaccine[patient_id]',vaccine_timestamp='$vaccine[vaccine_timestamp]',actual_vaccine_date='$vaccine[actual_vaccine_date]',vaccine_id='$vaccine[vaccine_id]'";
// updating of imported vaccine!
                                    $result_import = mysql_query($sql_import) or die("Cannot query: 669");
                                }
                            }
                        }
                    }
                }
                header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=".$get_vars["menu_id"]."&consult_id=".$get_vars["consult_id"]."&ptmenu=DETAILS&module=mc&mc=SVC&mc_id=".$get_vars["mc_id"]);
                break;
            case "Update Services":
                if ($post_vars["services"]) {
                    foreach($post_vars["services"] as $key=>$value) {
												
                        $sql = "insert into m_consult_mc_services (mc_id, consult_id, user_id, patient_id, mc_timestamp, service_id, visit_type) ".
                               "values ('".$post_vars["mc_id"]."', '".$get_vars["consult_id"]."', '".$_SESSION["userid"]."', '$patient_id', sysdate(), '$value', '".$post_vars["visit_type"]."')";
                        $result = mysql_query($sql);
                    }
                }
                if ($post_vars["vaccines"]) {
                    foreach($post_vars["vaccines"] as $key=>$value) {
                        $sql = "insert into m_consult_mc_vaccine (mc_id, consult_id, user_id, patient_id, vaccine_timestamp, actual_vaccine_date, vaccine_id) ".
                               "values ('".$post_vars["mc_id"]."', '".$get_vars["consult_id"]."', '".$_SESSION["userid"]."', '$patient_id', sysdate(), sysdate(), '$value')";
                        $result = mysql_query($sql);

                        // update patient vaccine record also
                        $sql_vaccine = "insert into m_consult_vaccine (consult_id, patient_id, vaccine_id, user_id, vaccine_timestamp, actual_vaccine_date, source_module) ".
                                       "values ('".$get_vars["consult_id"]."', '$patient_id', '$value', '".$_SESSION["userid"]."', sysdate(), sysdate(), 'mc')";
                        $result_vaccine = mysql_query($sql_vaccine);
                    }
                }
                header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=".$get_vars["menu_id"]."&consult_id=".$get_vars["consult_id"]."&ptmenu=DETAILS&module=mc&mc=SVC&mc_id=".$post_vars["mc_id"]);
                break;
            case "Save Data":
                if ($post_vars["lmp_date"] && $post_vars["obscore_gp"] && $post_vars["obscore_fpal"] && $post_vars["patient_height"]) {
                    $sql = "insert into m_patient_mc (patient_id, consult_id, ".
                           "mc_timestamp, mc_consult_date, patient_lmp, patient_edc, ".
                           "trimester1_date, trimester2_date, trimester3_date, postpartum_date, ".
                           "obscore_gp, obscore_fpal, ".
                           "user_id, blood_type, patient_age, patient_height) ".
                           "values ('$patient_id', '".$get_vars["consult_id"]."', ".
                           "sysdate(), sysdate(), '$lmp_date', from_days(to_days('$lmp_date')+280), ".
                           "from_days(to_days('$lmp_date')+93.3), from_days(to_days('$lmp_date')+186.67), from_days(to_days('$lmp_date')+280), from_days(to_days('$lmp_date')+322), ".
                           "'".$post_vars["obscore_gp"]."', '".$post_vars["obscore_fpal"]."', '".$_SESSION["userid"]."', ".
                           "'".$post_vars["bloodtype"]."', '$patient_age', ".
                           "'".$post_vars["patient_height"]."')";

                    if ($result = mysql_query($sql)) {
                        $insert_id = mysql_insert_id();
						/*$tt_vacc = array('TT1','TT2','TT3','TT4','TT5');
						
						for($j=0;$j<count($tt_vacc);$j++){
						
						$check_TT = mysql_query("SELECT actual_vaccine_date, vaccine_id, consult_id,vaccine_timestamp,adr_flag FROM m_consult_vaccine WHERE patient_id='$patient_id' AND vaccine_id='$tt_vacc[$j]'") or die("Cannot query: 713");
						
						if(mysql_num_rows($check_TT)>0):
							while($r_check_TT=mysql_fetch_array($check_TT))
							{
								$q_mc_vacc = mysql_query("SELECT consult_id,patient_id,mc_id FROM m_consult_mc_vaccine WHERE consult_id='$r_check_TT[consult_id]' AND vaccine_timestamp='$r_check_TT[vaccine_timestamp]' AND vaccine_id='$r_check_TT[vaccine_id]'") or die(mysql_error());

								if(mysql_num_rows($q_mc_vacc)==0):
									$insert_vacc = mysql_query("INSERT INTO m_consult_mc_vaccine SET mc_id='$insert_id',consult_id='$r_check_TT[consult_id]',patient_id='$patient_id',user_id='$_SESSION[userid]',vaccine_timestamp='$r_check_TT[vaccine_timestamp]',actual_vaccine_date='$r_check_TT[actual_vaccine_date]',adr_flag='$r_check_TT[adr_flag]',vaccine_id='$r_check_TT[vaccine_id]'") or die("Cannot query: 725");

									$update_to_mc = mysql_query("UPDATE SET m_consult_vaccine SET source_module='mc' WHERE patient_id='$patient_id' AND consult_id='$r_check_TT[consult_id]' AND vaccine_id='$r_check_TT[vaccine_id]' AND vaccine_timestamp='$r_check_TT[vaccine_timestamp]'") or die(mysql_error());

								else:
									echo 'test1';
								endif;
							}

						else:
							echo 'test2';

						endif;

						}*/

						$date_ngayon = date('Y-m-d');
                        foreach($post_vars["risk"] as $key=>$value) {

                            $sql_risk = "insert into m_consult_mc_visit_risk (consult_id , ".
                                        "patient_id, mc_id, visit_risk_id, risk_timestamp,date_detected ".
                                        "user_id) values ('".$get_vars["consult_id"]."', ".
                                        "'$patient_id', '$insert_id', '$value', ".
                                        "sysdate(),$date_ngayon '".$_SESSION["userid"]."')";
                            $result_risk = mysql_query($sql_risk);
                        }
                        header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=".$get_vars["menu_id"]."&consult_id=".$get_vars["consult_id"]."&ptmenu=DETAILS&module=mc&mc=VISIT1&mc_id=$insert_id");
                    }
                } else {
                    print "<font color='red'>Incomplete entries.</font><br/>";
                }
                break;
            case "Update Data":
                if ($post_vars["lmp_date"] && $post_vars["obscore_gp"] && $post_vars["obscore_fpal"] && $post_vars["patient_height"]) {
                    $sql = "update m_patient_mc set ".
                           "patient_lmp = '$lmp_date', ".
                           "patient_edc = from_days(to_days('$lmp_date')+280), ".
                           "trimester1_date = from_days(to_days('$lmp_date')+93.3), ".
                           "trimester2_date = from_days(to_days('$lmp_date')+186.67), ".
                           "trimester3_date = from_days(to_days('$lmp_date')+280), ".
                           "postpartum_date = from_days(to_days('$lmp_date')+322), ".
                           "mc_timestamp = sysdate(), ".
                           "obscore_gp = '".$post_vars["obscore_gp"]."', ".
                           "obscore_fpal = '".$post_vars["obscore_fpal"]."', ".
                           "patient_height = '".$post_vars["patient_height"]."', ".
                           "blood_type = '".$post_vars["bloodtype"]."' ".
                           "where mc_id = '".$post_vars["mc_id"]."'";
                    if ($result = mysql_query($sql)) {
                        // delete risk factors first
                        $sql_delete = "delete from m_consult_mc_visit_risk ".
                                      "where consult_id = '".$get_vars["consult_id"]."' and mc_id = '".$post_vars["mc_id"]."'";
                        $result_delete = mysql_query($sql_delete);
                        // ...then update from form
						$date_ngayon = date('Y-m-d');
                        foreach($post_vars["risk"] as $key=>$value) {
                            $sql_risk = "insert into m_consult_mc_visit_risk (consult_id , ".
                                        "patient_id, mc_id, visit_risk_id, risk_timestamp,date_detected,".
                                        "user_id) values ('".$get_vars["consult_id"]."', ".
                                        "'$patient_id', '".$post_vars["mc_id"]."', '$value', ".
                                        "sysdate(),$date_ngayon, '".$_SESSION["userid"]."')";
                            $result_risk = mysql_query($sql_risk);
                        }
                        header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=".$get_vars["menu_id"]."&consult_id=".$get_vars["consult_id"]."&ptmenu=DETAILS&module=mc&mc=VISIT1".($post_vars["mc_id"]?"&mc_id=".$post_vars["mc_id"]:""));
                    }
                } else {
                    print "<font color='red'>Incomplete entries.</font><br/>";
                }
                break;
            case "Delete Data":
                if (module::confirm_delete($menu_id, $post_vars, $get_vars)) {
                    $sql = "delete from m_patient_mc where mc_id = '".$post_vars["mc_id"]."'";
                    if ($result = mysql_query($sql)) {
                        header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=".$get_vars["menu_id"]."&consult_id=".$get_vars["consult_id"]."&ptmenu=DETAILS&module=mc&mc=VISIT1".($post_vars["mc_id"]?"&mc_id=".$post_vars["mc_id"]:""));
                    }
                } else {
                    if ($post_vars["confirm_delete"]=="No") {
                        header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=".$get_vars["menu_id"]."&consult_id=".$get_vars["consult_id"]."&ptmenu=DETAILS&module=mc&mc=VISIT1".($post_vars["mc_id"]?"&mc_id=".$post_vars["mc_id"]:""));
                    }
                }
                break;
            case "Save Prenatal Data":
				$exists = mc::check_visit_date($post_vars["visit_date"],$post_vars[mc_id]);

                if ($post_vars["patient_systolic"] && $post_vars["patient_diastolic"] &&
                    $post_vars["patient_weight"] && $post_vars["visit_date"] && $exists!=1) {
								
                    //if ($post_vars["data_import_flag"]) {
                        list($month, $day, $year) = explode("/", $post_vars["visit_date"]);
                        $visit_date = "$year-".str_pad($month,2,"0",STR_PAD_LEFT)."-".str_pad($day,2,"0",STR_PAD_LEFT);
                        $trimester = mc::get_trimester($post_vars["mc_id"], $visit_date);
                        list($aog_weeks, $days) = mc::get_aog($post_vars["mc_id"], $visit_date);
                        $aog_total = $aog_weeks + ($days/7);
                        $data_type = "EXT";
                    //} else {
                    /*    $visit_date = healthcenter::get_consult_date($get_vars["consult_id"]);
                        $trimester = mc::get_trimester($post_vars["mc_id"], $visit_date);
                        $trimester = $post_vars["trimester"];
                        $aog_total = $post_vars["aog_weeks"];
                        $data_type = "INT";
				     */
                    //}
                    $sql = "insert into m_consult_mc_prenatal (mc_id, consult_id, patient_id, prenatal_timestamp, ".
                           "prenatal_date, user_id, aog_weeks, trimester, visit_sequence, patient_weight, ".
                           "blood_pressure_systolic, blood_pressure_diastolic, fhr, fhr_location, ".
                           "fundic_height, presentation, data_type) values ('".$post_vars["mc_id"]."', ".
                           "'".$get_vars["consult_id"]."', '$patient_id', '$visit_date', ".
                           "'$visit_date', '".$_SESSION["userid"]."', '$aog_total', ".
                           "'$trimester', '".$post_vars["visit_sequence"]."', ".
                           "'".$post_vars["patient_weight"]."', '".$post_vars["patient_systolic"]."', ".
                           "'".$post_vars["patient_diastolic"]."', '".$post_vars["fhr"]."', ".
                           "'".$post_vars["fhr_location"]."', '".$post_vars["fundic_height"]."', ".
                           "'".$post_vars["presentation"]."', '$data_type')";					
		
		    $result = mysql_query($sql) or die("Cannot query: 840");
		    mc::reorder_prenatal_visits($post_vars["mc_id"]);
                    if ($result) {		

                        // do not update for visit 1
                        if ($post_vars["visit_sequence"]>1) {
                            // delete risk factors first but only the M flagged
                            // caution use this only with MySQL version 4.+
                            $sql_delete = "delete from m_consult_mc_visit_risk ".
                                          "using m_consult_mc_visit_risk, m_lib_mc_risk_factors ".
                                          "where m_consult_mc_visit_risk.consult_id = '".$get_vars["consult_id"]."' ".
                                          "and m_consult_mc_visit_risk.mc_id = '".$post_vars["mc_id"]."' and ".
                                          "m_consult_mc_visit_risk.visit_risk_id = m_lib_mc_risk_factors.risk_id and ".
                                          "m_lib_mc_risk_factors.monitor_flag = 'Y'";
                            $result_delete = mysql_query($sql_delete);
                            // ...then update from form
                            foreach($post_vars["risk"] as $key=>$value) {

                                /*$sql_risk = "insert into m_consult_mc_visit_risk (consult_id , ".
                                            "patient_id, mc_id, visit_risk_id, risk_timestamp, ".
                                            "user_id) values ('".$get_vars["consult_id"]."', ".
                                            "'$patient_id', '".$post_vars["mc_id"]."', '$value', ".
                                            "sysdate(), '".$_SESSION["userid"]."')";

								 */
								$date_ngayon = date('Y-m-d');
								$sql_risk = "insert into m_consult_mc_visit_risk (consult_id , ".
                                            "patient_id, mc_id, visit_risk_id, risk_timestamp,date_detected,".
                                            "user_id) values ('".$get_vars["consult_id"]."', ".
                                            "'$patient_id', '".$post_vars["mc_id"]."', '$value', ".
                                            "sysdate(),$date_ngayon,'".$_SESSION["userid"]."')";		
                            }
                        }

                        header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=".$get_vars["menu_id"]."&consult_id=".$get_vars["consult_id"]."&ptmenu=DETAILS&module=mc&mc=PREN".($post_vars["mc_id"]?"&mc_id=".$post_vars["mc_id"]:""));
                    }
                }
                break;
            case "Update Prenatal":
				//print_r($post_vars);
				$exists = mc::check_visit_date($post_vars["visit_date"],$post_vars[mc_id]);
				
                if ($post_vars["patient_systolic"] && $post_vars["patient_diastolic"] &&
                    $post_vars["patient_weight"] && $post_vars["fundic_height"] && $post_vars["fhr"] &&
                    $post_vars["fhr_location"] && $post_vars["presentation"] && $post_vars["visit_date"]) {
					
					list($month, $day, $year) = explode("/", $post_vars["visit_date"]);									
					$visit_date = "$year-".str_pad($month,2,"0",STR_PAD_LEFT)."-".str_pad($day,2,"0",STR_PAD_LEFT);					
					$trimester = mc::get_trimester($post_vars["mc_id"], $visit_date);
										

                    $sql = "update m_consult_mc_prenatal set ".
                           "fundic_height = '".$post_vars["fundic_height"]."', ".
                           "fhr = '".$post_vars["fhr"]."', ".
						   "prenatal_date = '".$visit_date."', ".
                           "fhr_location = '".$post_vars["fhr_location"]."', ".
						   "trimester = '".$trimester."', ".
                           "presentation = '".$post_vars["presentation"]."', ".
						   "patient_weight = '".$post_vars["patient_weight"]."', ".
						   "blood_pressure_systolic = '".$post_vars["patient_systolic"]."', ".
						   "blood_pressure_diastolic = '".$post_vars["patient_diastolic"]."' ".
                           "where mc_id = '".$post_vars["mc_id"]."' and visit_sequence = '".$post_vars["visit_sequence"]."'";

					$result = mysql_query($sql) or die("Cannot update: 855");
                    
					if ($result) {

                        // do not update for visit 1
                        if ($post_vars["visit_sequence"]>1) {
                            // delete risk factors first but only the M flagged
                            // caution use this only with MySQL version 4.+
                            /*$sql_delete = "delete from m_consult_mc_visit_risk ".
                                          "using m_consult_mc_visit_risk, m_lib_mc_risk_factors ".
                                          "where m_consult_mc_visit_risk.consult_id = '".$post_vars["consult_id"]."' ".
                                          "and m_consult_mc_visit_risk.mc_id = '".$post_vars["mc_id"]."' and ".
                                          "m_consult_mc_visit_risk.visit_risk_id = m_lib_mc_risk_factors.risk_id and ".
                                          "m_lib_mc_risk_factors.monitor_flag = 'Y'";
                            $result_delete = mysql_query($sql_delete);
							
                            // ...then update from form							

							foreach($post_vars["risk"] as $key=>$value) {								
                                $sql_risk = "insert into m_consult_mc_visit_risk (consult_id , ".
                                            "patient_id, mc_id, visit_risk_id, risk_timestamp, ".
                                            "user_id) values ('".$get_vars["consult_id"]."', ".
                                            "'$patient_id', '".$post_vars["mc_id"]."', '$value', ".
                                            "sysdate(), '".$_SESSION["userid"]."')";
                                $result_risk = mysql_query($sql_risk);
                            }*/
                        }
                    header("location:  ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=".$get_vars["menu_id"]."&consult_id=".$get_vars["consult_id"]."&ptmenu=DETAILS&module=mc&mc=PREN".($post_vars["mc_id"]?"&mc_id=".$post_vars["mc_id"]:""));	
                    }
					else{
						echo 'Record was not updated';
					}
                }
				else{
					echo "Cannot update. Please complete the form.";
				}
                break;
            case "Delete Prenatal":
                if (module::confirm_delete($menu_id, $post_vars, $get_vars)) {
                    $sql = "delete from m_consult_mc_prenatal where mc_id = '".$post_vars["mc_id"]."' and visit_sequence = '".$post_vars["visit_sequence"]."'";
                    if ($result = mysql_query($sql)) {
                        // delete associated risk data for this prenatal visit
                        // there is no referential integrity definition here...
                        $sql_risk = "delete from m_consult_mc_visit_risk where mc_id = '".$post_vars["mc_id"]."' and consult_id = '".$post_vars["consult_id"]."'";
                        $result_risk = mysql_query($sql_risk);
                        header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=".$get_vars["menu_id"]."&consult_id=".$get_vars["consult_id"]."&ptmenu=DETAILS&module=mc&mc=PREN".($post_vars["mc_id"]?"&mc_id=".$post_vars["mc_id"]:""));
                    }
                } else {
                    if ($post_vars["confirm_delete"]=="No") {
                        header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=".$get_vars["menu_id"]."&consult_id=".$get_vars["consult_id"]."&ptmenu=DETAILS&module=mc&mc=PREN".($post_vars["mc_id"]?"&mc_id=".$post_vars["mc_id"]:""));
                    }
                }
                break;
            case "Save Postpartum Data":
                if ($post_vars["delivery_date"] && $post_vars["delivery_location"] &&
                    $post_vars["outcome"] && $post_vars["birth_weight"] && $post_vars["attendant"]) {
					

					if(isset($_POST[breastfeeding_flag])): //naka-check
							
						if(empty($_POST[date_breastfed])): //no date but checked
							$datebreastfeed = 'n';
						elseif($this->get_day_diff($_POST[date_breastfed],$post_vars["delivery_date"]) < 0): //check if date of breastfeeding occurs after the delivery date
							$datebreastfeed = 'w';
						else:
							$datebreastfeed = 'y'; //set to yes is date_breastfed is not empty and not exceeding delivery date
						endif;

					else:
						$datebreastfeed = 'y'; //not checked, no need to check for date
					endif;



					if($datebreastfeed=='y'){
						list($month, $day, $year) = explode("/", $post_vars["delivery_date"]);
						list($bmonth,$bday,$byr) = explode("/",$_POST["date_breastfed"]);

						$delivery_date = "$year-".str_pad($month,2,"0",STR_PAD_LEFT)."-".str_pad($day,2,"0",STR_PAD_LEFT);
						
						echo $delivery_date;

						if(isset($_POST[breastfeeding_flag])):
							$bfeed_date = "$byr-".str_pad($bmonth,2,"0",STR_PAD_LEFT)."-".str_pad($bday,2,"0",STR_PAD_LEFT);
						else:
							$bfeed_date = '';
						endif;

						
						$healthy_baby = ($post_vars["healthy_baby_flag"]?"Y":"N");
						$breastfeeding = ($post_vars["breastfeeding_flag"]?"Y":"N");
						$end_pregnancy = ($post_vars["end_pregnancy_flag"]?"Y":"N");
						

						// NOTE: update postpartum date from delivery date
						$sql = "update m_patient_mc set ".
							   "delivery_date = '$delivery_date', ".
							   "postpartum_date = from_days(to_days('$delivery_date')+42), ".
							   "obscore_gp = '".$post_vars["obscore_gp"]."', ".
							   "obscore_fpal = '".$post_vars["obscore_fpal"]."', ".
							   "birthweight = '".$post_vars["birth_weight"]."', ".
							   "child_patient_id = '".$post_vars["child_patient_id"]."', ".
							   "delivery_location = '".$post_vars["delivery_location"]."', ".
							   "outcome_id = '".$post_vars["outcome"]."', ".
							   "healthy_baby = '$healthy_baby', ".
							   "end_pregnancy_flag = '$end_pregnancy', ".
							   "breastfeeding_asap = '$breastfeeding', ".
							   "date_breastfed = '$bfeed_date', ".
							   "birthmode = '".$post_vars["attendant"]."' ".
							   "where mc_id = '".$get_vars["mc_id"]."'"; 

						$result = mysql_query($sql) or die("Cannot query: 1068");

						if ($result) {
							header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=".$get_vars["menu_id"]."&consult_id=".$get_vars["consult_id"]."&ptmenu=DETAILS&module=mc&mc=POSTP&mc_id=".$get_vars["mc_id"]);
						}
					}

					else{
						echo "<script language='Javascript'>";
						if($datebreastfeed=='n'):
							echo "window.alert('Record not saved. Please indicate the date of breastfeeding if the baby was breastfed ASAP.')";
						else: //w ito
						echo "window.alert('Record not saved. Date of breastfeeding should be on or after date of delivery.')";							
						endif;
						echo "</script>";
					}
                }else{
						echo "<script language='Javascript'>";
						echo "window.alert('Record not saved. Please complete REQUIRED fields.')";
						echo "</script>";
				
				
				}
                break;
            }
        }
    }
Example #2
0
 function process_mc()
 {
     if (func_num_args() > 0) {
         $arg_list = func_get_args();
         $menu_id = $arg_list[0];
         $post_vars = $arg_list[1];
         $get_vars = $arg_list[2];
         $validuser = $arg_list[3];
         $isadmin = $arg_list[4];
         //print_r($arg_list);
     }
     if ($post_vars["submitmc"]) {
         $patient_id = healthcenter::get_patient_id($get_vars["consult_id"]);
         $patient_age = patient::get_age($patient_id);
         if ($post_vars["lmp_date"]) {
             list($month, $day, $year) = explode("/", $post_vars["lmp_date"]);
             $lmp_date = "{$year}-" . str_pad($month, 2, "0", STR_PAD_LEFT) . "-" . str_pad($day, 2, "0", STR_PAD_LEFT);
         }
         switch ($post_vars["submitmc"]) {
             case "Save Risk Factor":
                 if (empty($_POST[risk_date])) {
                     echo "<script language='Javascript'>";
                     echo "window.alert('Please supply date of when the risk factor was detected.')";
                     echo "</script>";
                 } elseif (empty($get_vars[mc_id])) {
                     echo "<script language='Javascript'>";
                     echo "window.alert('Open a maternal record first!')";
                     echo "</script>";
                 } else {
                     list($month, $date, $year) = explode('/', $_POST[risk_date]);
                     $risk_date = $year . '-' . $month . '-' . $date;
                     $q_risk = mysql_query("SELECT visit_risk_id FROM m_consult_mc_visit_risk WHERE mc_id='{$get_vars['mc_id']}' AND visit_risk_id='{$_POST['risk_code']}' AND date_detected='{$risk_date}'") or die("Cannot query: 582");
                     if (mysql_num_rows($q_risk) == 0) {
                         $insert_risk = mysql_query("INSERT INTO m_consult_mc_visit_risk SET consult_id='{$get_vars['consult_id']}',patient_id='{$patient_id}',mc_id='{$get_vars['mc_id']}',visit_risk_id='{$_POST['risk_code']}',date_detected='{$risk_date}',risk_timestamp=sysdate(),user_id='{$_SESSION['userid']}'") or die(mysql_error());
                         if ($insert_risk) {
                             echo "<font color='red'>Record successfully added.</font>";
                         }
                     } else {
                         echo "<font color='red'>Risk code and date already exists.</font>";
                     }
                 }
                 break;
             case "Delete Risk Factor":
                 if (isset($_POST[riskcode]) && $_POST[confirm_del] == 1) {
                     while (list($risk_key, $risk_value) = each($_POST[riskcode])) {
                         list($riskid, $mcid, $datedetected) = explode('*', $risk_value);
                         $del_risk = mysql_query("DELETE FROM m_consult_mc_visit_risk WHERE mc_id='{$mcid}' AND visit_risk_id='{$riskid}' AND date_detected='{$datedetected}'") or die("Cannot query: 615");
                     }
                     if ($del_risk) {
                         echo "<font color='red'>Risk factor was successfully deleted.</font>";
                         header("Location: {$_SERVER['PHP_SELF']}?page=CONSULTS&menu_id={$get_vars['menu_id']}&consult_id={$get_vars['consult_id']}&ptmenu=DETAILS&module=mc&mc=RF&mc_id={$get_vars['mc_id']}");
                     }
                 }
                 break;
             case "Save Prenatal Remarks":
                 $update_prenatal = mysql_query("UPDATE m_patient_mc SET prenatal_remarks='{$_POST['prenatal_remarks']}' WHERE mc_id='{$get_vars['mc_id']}'") or die("Cannot query: 634");
                 if ($update_prenatal) {
                     header("Location: {$_SERVER['PHP_SELF']}?page=CONSULTS&menu_id={$get_vars['menu_id']}&consult_id={$get_vars['consult_id']}&ptmenu=DETAILS&module=mc&mc=PREN&mc_id={$get_vars['mc_id']}#prerem");
                 }
                 break;
             case "Save Postpartum Remarks":
                 $update_post = mysql_query("UPDATE m_patient_mc SET postpartum_remarks='{$_POST['postpartum_remarks']}' WHERE mc_id='{$get_vars['mc_id']}'") or die("Cannot query: 645");
                 if ($update_post) {
                     header("Location: {$_SERVER['PHP_SELF']}?page=CONSULTS&menu_id={$get_vars['menu_id']}&consult_id={$get_vars['consult_id']}&ptmenu=DETAILS&module=mc&mc=POSTP&mc_id={$get_vars['mc_id']}#postrem");
                 }
                 break;
             case "Save Postpartum Visit":
                 if ($post_vars["visit_date"] && $post_vars["visit_type"] && $post_vars["patient_systolic"] && $post_vars["patient_diastolic"]) {
                     //print_r($post_vars);
                     $wk_count = mc::get_pp_week($post_vars["mc_id"], $post_vars["visit_date"]);
                     if ($wk_count > 0) {
                         list($month, $day, $year) = explode("/", $post_vars["visit_date"]);
                         $visit_date = "{$year}-" . str_pad($month, 2, "0", STR_PAD_LEFT) . "-" . str_pad($day, 2, "0", STR_PAD_LEFT);
                         $breastfeeding_flag = $post_vars["breastfeeding_flag"] ? "Y" : "N";
                         $family_planning_flag = $post_vars["family_planning_flag"] ? "Y" : "N";
                         $fever_flag = $post_vars["fever_flag"] ? "Y" : "N";
                         $vaginal_infection_flag = $post_vars["vaginal_infection_flag"] ? "Y" : "N";
                         $vaginal_bleeding_flag = $post_vars["vaginal_bleeding_flag"] ? "Y" : "N";
                         $pallor_flag = $post_vars["pallor_flag"] ? "Y" : "N";
                         $cord_ok_flag = $post_vars["cord_ok_flag"] ? "Y" : "N";
                         $sql = "insert into m_consult_mc_postpartum (mc_id, consult_id, " . "patient_id, postpartum_timestamp, postpartum_date, user_id, " . "postpartum_week, visit_sequence, visit_type, " . "breastfeeding_flag, family_planning_flag, fever_flag, " . "vaginal_infection_flag, vaginal_bleeding_flag, " . "pallor_flag, cord_ok_flag, blood_pressure_systolic, " . "blood_pressure_diastolic) " . "values ('" . $post_vars["mc_id"] . "', '" . $get_vars["consult_id"] . "', " . "'{$patient_id}', sysdate(), '{$visit_date}', '" . $_SESSION["userid"] . "', " . "'" . $wk_count . "', '" . $post_vars["visit_sequence"] . "', " . "'" . $post_vars["visit_type"] . "', '{$breastfeeding_flag}', " . "'{$family_planning_flag}', '{$fever_flag}', '{$vaginal_infection_flag}', " . "'{$vaginal_bleeding_flag}', '{$pallor_flag}', '{$cord_ok_flag}', " . "'" . $post_vars["patient_systolic"] . "', '" . $post_vars["patient_diastolic"] . "')";
                         if ($result = mysql_query($sql)) {
                             header("location: " . $_SERVER["PHP_SELF"] . "?page=" . $get_vars["page"] . "&menu_id=" . $get_vars["menu_id"] . "&consult_id=" . $get_vars["consult_id"] . "&ptmenu=" . $get_vars["ptmenu"] . "&module=mc&mc=POSTP&mc_id=" . $get_vars["mc_id"]);
                         }
                     } else {
                         echo "<font color='red'>Postpartum visit date should be after date of delivery.</font>";
                     }
                 } else {
                     echo "<font color='red'>Postpartum visit not saved. Please check visit date, type of visit and systolic/diastolic.</font>";
                 }
                 break;
             case "Update Postpartum Data Form":
                 break;
             case "Update Postpartum":
                 if ($post_vars["visit_date"] && $post_vars["visit_type"] && $post_vars["patient_systolic"] && $post_vars["patient_diastolic"]) {
                     $wk_count = mc::get_pp_week($get_vars["mc_id"], $post_vars["visit_date"]);
                     list($month, $day, $year) = explode("/", $post_vars["visit_date"]);
                     $visit_date = "{$year}-" . str_pad($month, 2, "0", STR_PAD_LEFT) . "-" . str_pad($day, 2, "0", STR_PAD_LEFT);
                     $breastfeeding_flag = $post_vars["breastfeeding_flag"] ? "Y" : "N";
                     $family_planning_flag = $post_vars["family_planning_flag"] ? "Y" : "N";
                     $fever_flag = $post_vars["fever_flag"] ? "Y" : "N";
                     $vaginal_infection_flag = $post_vars["vaginal_infection_flag"] ? "Y" : "N";
                     $vaginal_bleeding_flag = $post_vars["vaginal_bleeding_flag"] ? "Y" : "N";
                     $pallor_flag = $post_vars["pallor_flag"] ? "Y" : "N";
                     $cord_ok_flag = $post_vars["cord_ok_flag"] ? "Y" : "N";
                     if ($wk_count > 0) {
                         $sql = "update m_consult_mc_postpartum set " . "postpartum_date = '{$visit_date}', " . "postpartum_week = '{$wk_count}', " . "visit_type = '" . $post_vars["visit_type"] . "', " . "blood_pressure_systolic = '" . $post_vars["patient_systolic"] . "', " . "blood_pressure_diastolic = '" . $post_vars["patient_diastolic"] . "', " . "breastfeeding_flag = '{$breastfeeding_flag}', " . "family_planning_flag = '{$family_planning_flag}', " . "fever_flag = '{$fever_flag}', " . "vaginal_infection_flag = '{$vaginal_infection_flag}', " . "vaginal_bleeding_flag = '{$vaginal_bleeding_flag}', " . "pallor_flag = '{$pallor_flag}', " . "cord_ok_flag = '{$cord_ok_flag}' " . "where mc_id = '" . $get_vars["mc_id"] . "' and " . "visit_sequence = '" . $post_vars["visit_sequence"] . "' and " . "consult_id = '" . $get_vars["consult_id"] . "'";
                         $result = mysql_query($sql) or die("Cannot query: 721");
                         if ($result) {
                             header("location: " . $_SERVER["PHP_SELF"] . "?page=" . $get_vars["page"] . "&menu_id=" . $get_vars["menu_id"] . "&consult_id=" . $get_vars["consult_id"] . "&ptmenu=DETAILS&module=mc&mc=POSTP&mc_id=" . $get_vars["mc_id"] . "&ppvisitseq=" . $get_vars["ppvisitseq"]);
                         }
                     } else {
                         echo "<font color='red'>Postpartum visit date should be after date of delivery.</font>";
                     }
                 } else {
                     echo "<font color='red'>Postpartum visit not updated. Please check visit date, type of visit and systolic/diastolic.</font>";
                 }
                 break;
             case "Delete Postpartum":
                 if (module::confirm_delete($menu_id, $post_vars, $get_vars)) {
                     $sql = "delete from m_consult_mc_postpartum " . "where mc_id = '" . $get_vars["mc_id"] . "' and " . "consult_id = '" . $get_vars["consult_id"] . "' and " . "visit_sequence = '" . $post_vars["visit_sequence"] . "'";
                     if ($result = mysql_query($sql)) {
                         header("location: " . $_SERVER["PHP_SELF"] . "?page=" . $get_vars["page"] . "&menu_id=" . $get_vars["menu_id"] . "&consult_id=" . $get_vars["consult_id"] . "&ptmenu=DETAILS&module=mc&mc=POSTP&mc_id=" . $get_vars["mc_id"]);
                     }
                 } else {
                     if ($post_vars["confirm_delete"] == "No") {
                         header("location: " . $_SERVER["PHP_SELF"] . "?page=" . $get_vars["page"] . "&menu_id=" . $get_vars["menu_id"] . "&consult_id=" . $get_vars["consult_id"] . "&ptmenu=DETAILS&module=mc&mc=POSTP&mc_id=" . $get_vars["mc_id"]);
                     }
                 }
                 break;
             case "Import Data":
                 if ($post_vars["vaccines"]) {
                     // remember, no need to update m_consult_vaccine
                     // since these data are coming from that source!
                     foreach ($post_vars["vaccines"] as $key => $value) {
                         // get complete data from m_consult_vaccine
                         $sql_vaccine = "select patient_id, consult_id, vaccine_id, actual_vaccine_date, adr_flag, vaccine_timestamp " . "from m_consult_vaccine " . "where patient_id = '" . $post_vars["patient_id"] . "' and " . "vaccine_id = '{$value}'";
                         if ($result_vaccine = mysql_query($sql_vaccine)) {
                             if (mysql_num_rows($result_vaccine)) {
                                 while ($vaccine = mysql_fetch_array($result_vaccine)) {
                                     //$sql_import = "insert into m_consult_mc_vaccine (mc_id, consult_id, user_id, patient_id, vaccine_timestamp, actual_vaccine_date, vaccine_id) "."values ('".$post_vars["mc_id"]."', '".$vaccine["consult_id"]."', '".$_SESSION["userid"]."', '".$vaccine["patient_id"]."', sysdate(), '".$vaccine["actual_vaccine_date"]."', '".$vaccine["vaccine_id"]."')";
                                     $sql_import = "INSERT INTO m_consult_mc_vaccine SET mc_id='{$post_vars['mc_id']}',consult_id='{$vaccine['consult_id']}',user_id='{$_SESSION['userid']}',patient_id='{$vaccine['patient_id']}',vaccine_timestamp='{$vaccine['vaccine_timestamp']}',actual_vaccine_date='{$vaccine['actual_vaccine_date']}',vaccine_id='{$vaccine['vaccine_id']}'";
                                     // updating of imported vaccine!
                                     $result_import = mysql_query($sql_import) or die("Cannot query: 669");
                                 }
                             }
                         }
                     }
                 }
                 header("location: " . $_SERVER["PHP_SELF"] . "?page=" . $get_vars["page"] . "&menu_id=" . $get_vars["menu_id"] . "&consult_id=" . $get_vars["consult_id"] . "&ptmenu=DETAILS&module=mc&mc=SVC&mc_id=" . $get_vars["mc_id"]);
                 break;
             case "Update Services":
                 if ($post_vars["services"]) {
                     foreach ($post_vars["services"] as $key => $value) {
                         $sql = "insert into m_consult_mc_services (mc_id, consult_id, user_id, patient_id, mc_timestamp, service_id, visit_type) " . "values ('" . $post_vars["mc_id"] . "', '" . $get_vars["consult_id"] . "', '" . $_SESSION["userid"] . "', '{$patient_id}', sysdate(), '{$value}', '" . $post_vars["visit_type"] . "')";
                         $result = mysql_query($sql);
                     }
                 }
                 if ($post_vars["vaccines"]) {
                     foreach ($post_vars["vaccines"] as $key => $value) {
                         $sql = "insert into m_consult_mc_vaccine (mc_id, consult_id, user_id, patient_id, vaccine_timestamp, actual_vaccine_date, vaccine_id) " . "values ('" . $post_vars["mc_id"] . "', '" . $get_vars["consult_id"] . "', '" . $_SESSION["userid"] . "', '{$patient_id}', sysdate(), sysdate(), '{$value}')";
                         $result = mysql_query($sql);
                         // update patient vaccine record also
                         $sql_vaccine = "insert into m_consult_vaccine (consult_id, patient_id, vaccine_id, user_id, vaccine_timestamp, actual_vaccine_date, source_module) " . "values ('" . $get_vars["consult_id"] . "', '{$patient_id}', '{$value}', '" . $_SESSION["userid"] . "', sysdate(), sysdate(), 'mc')";
                         $result_vaccine = mysql_query($sql_vaccine);
                     }
                 }
                 header("location: " . $_SERVER["PHP_SELF"] . "?page=" . $get_vars["page"] . "&menu_id=" . $get_vars["menu_id"] . "&consult_id=" . $get_vars["consult_id"] . "&ptmenu=DETAILS&module=mc&mc=SVC&mc_id=" . $post_vars["mc_id"]);
                 break;
             case "Save Data":
                 //include here the LMP checking/verification
                 if ($post_vars["lmp_date"] == $post_vars["confirm_lmp_date"]) {
                     if ($post_vars["lmp_date"] && $post_vars["obscore_gp"] && $post_vars["obscore_fpal"] && $post_vars["patient_height"]) {
                         $edc = $this->compute_edc($post_vars["lmp_date"]);
                         $sql = "INSERT INTO m_patient_mc SET patient_id='{$patient_id}',consult_id='{$get_vars['consult_id']}',mc_timestamp=sysdate(),mc_consult_date=sysdate(),patient_lmp='{$lmp_date}',patient_edc='{$edc}',trimester1_date=from_days(to_days('{$lmp_date}')+84),trimester2_date=from_days(to_days('{$lmp_date}')+189),trimester3_date=from_days(to_days('{$lmp_date}')+280),postpartum_date=from_days(to_days('{$lmp_date}')+322),obscore_gp='{$post_vars['obscore_gp']}',obscore_fpal='{$post_vars['obscore_fpal']}',user_id='{$_SESSION['userid']}',blood_type='{$post_vars['bloodtype']}',patient_age='{$patient_age}',patient_height='{$post_vars['patient_height']}'";
                         $result = mysql_query($sql) or die("Cannot query 845" . mysql_error());
                         if ($result) {
                             $insert_id = mysql_insert_id();
                             $date_ngayon = date('Y-m-d');
                             foreach ($post_vars["risk"] as $key => $value) {
                                 $sql_risk = "insert into m_consult_mc_visit_risk (consult_id , " . "patient_id, mc_id, visit_risk_id, risk_timestamp,date_detected " . "user_id) values ('" . $get_vars["consult_id"] . "', " . "'{$patient_id}', '{$insert_id}', '{$value}', " . "sysdate(),{$date_ngayon} '" . $_SESSION["userid"] . "')";
                                 $result_risk = mysql_query($sql_risk);
                             }
                             header("location: " . $_SERVER["PHP_SELF"] . "?page=" . $get_vars["page"] . "&menu_id=" . $get_vars["menu_id"] . "&consult_id=" . $get_vars["consult_id"] . "&ptmenu=DETAILS&module=mc&mc=VISIT1&mc_id={$insert_id}");
                         }
                     } else {
                         print "<font color='red'>Incomplete entries.</font><br/>";
                     }
                 } else {
                     print "<font color='red'>LMP Date does not match</font><br/>";
                 }
                 break;
             case "Update Data":
                 if ($post_vars["lmp_date"] && $post_vars["obscore_gp"] && $post_vars["obscore_fpal"] && $post_vars["patient_height"]) {
                     $edc = $this->compute_edc($post_vars["lmp_date"]);
                     $sql = "update m_patient_mc set " . "patient_lmp = '{$lmp_date}', " . "patient_edc = '" . $edc . "', " . "trimester1_date = from_days(to_days('{$lmp_date}')+84), " . "trimester2_date = from_days(to_days('{$lmp_date}')+189), " . "trimester3_date = from_days(to_days('{$lmp_date}')+280), " . "postpartum_date = from_days(to_days('{$lmp_date}')+322), " . "mc_timestamp = sysdate(), " . "obscore_gp = '" . $post_vars["obscore_gp"] . "', " . "obscore_fpal = '" . $post_vars["obscore_fpal"] . "', " . "patient_height = '" . $post_vars["patient_height"] . "', " . "blood_type = '" . $post_vars["bloodtype"] . "' " . "where mc_id = '" . $post_vars["mc_id"] . "'";
                     if ($result = mysql_query($sql)) {
                         // delete risk factors first
                         $sql_delete = "delete from m_consult_mc_visit_risk " . "where consult_id = '" . $get_vars["consult_id"] . "' and mc_id = '" . $post_vars["mc_id"] . "'";
                         $result_delete = mysql_query($sql_delete);
                         // ...then update from form
                         $date_ngayon = date('Y-m-d');
                         foreach ($post_vars["risk"] as $key => $value) {
                             $sql_risk = "insert into m_consult_mc_visit_risk (consult_id , " . "patient_id, mc_id, visit_risk_id, risk_timestamp,date_detected," . "user_id) values ('" . $get_vars["consult_id"] . "', " . "'{$patient_id}', '" . $post_vars["mc_id"] . "', '{$value}', " . "sysdate(),{$date_ngayon}, '" . $_SESSION["userid"] . "')";
                             $result_risk = mysql_query($sql_risk);
                         }
                         header("location: " . $_SERVER["PHP_SELF"] . "?page=" . $get_vars["page"] . "&menu_id=" . $get_vars["menu_id"] . "&consult_id=" . $get_vars["consult_id"] . "&ptmenu=DETAILS&module=mc&mc=VISIT1" . ($post_vars["mc_id"] ? "&mc_id=" . $post_vars["mc_id"] : ""));
                     }
                 } else {
                     print "<font color='red'>Incomplete entries.</font><br/>";
                 }
                 break;
             case "Delete Data":
                 if (module::confirm_delete($menu_id, $post_vars, $get_vars)) {
                     $sql = "delete from m_patient_mc where mc_id = '" . $post_vars["mc_id"] . "'";
                     if ($result = mysql_query($sql)) {
                         header("location: " . $_SERVER["PHP_SELF"] . "?page=" . $get_vars["page"] . "&menu_id=" . $get_vars["menu_id"] . "&consult_id=" . $get_vars["consult_id"] . "&ptmenu=DETAILS&module=mc&mc=VISIT1" . ($post_vars["mc_id"] ? "&mc_id=" . $post_vars["mc_id"] : ""));
                     }
                 } else {
                     if ($post_vars["confirm_delete"] == "No") {
                         header("location: " . $_SERVER["PHP_SELF"] . "?page=" . $get_vars["page"] . "&menu_id=" . $get_vars["menu_id"] . "&consult_id=" . $get_vars["consult_id"] . "&ptmenu=DETAILS&module=mc&mc=VISIT1" . ($post_vars["mc_id"] ? "&mc_id=" . $post_vars["mc_id"] : ""));
                     }
                 }
                 break;
             case "Save Prenatal Data":
                 $exists = mc::check_visit_date($post_vars["visit_date"], $post_vars[mc_id]);
                 //if ($post_vars["patient_systolic"] && $post_vars["patient_diastolic"] &&
                 //    $post_vars["patient_weight"] && $post_vars["visit_date"] && $exists!=1) {
                 if ($post_vars["visit_date"]) {
                     //allow the entry of the prenatal visit minus the other vital signs
                     //if ($post_vars["data_import_flag"]) {
                     list($month, $day, $year) = explode("/", $post_vars["visit_date"]);
                     $visit_date = "{$year}-" . str_pad($month, 2, "0", STR_PAD_LEFT) . "-" . str_pad($day, 2, "0", STR_PAD_LEFT);
                     $trimester = mc::get_trimester($post_vars["mc_id"], $visit_date);
                     list($aog_weeks, $days) = mc::get_aog($post_vars["mc_id"], $visit_date);
                     $aog_total = $aog_weeks + $days / 7;
                     $data_type = "EXT";
                     $private = isset($post_vars["check_private"]) ? 'Y' : '';
                     //} else {
                     /*    $visit_date = healthcenter::get_consult_date($get_vars["consult_id"]);
                                          $trimester = mc::get_trimester($post_vars["mc_id"], $visit_date);
                                          $trimester = $post_vars["trimester"];
                                          $aog_total = $post_vars["aog_weeks"];
                                          $data_type = "INT";
                     	     */
                     //}
                     $sql = "insert into m_consult_mc_prenatal (mc_id, consult_id, patient_id, prenatal_timestamp, " . "prenatal_date, user_id, aog_weeks, trimester, visit_sequence, patient_weight, " . "blood_pressure_systolic, blood_pressure_diastolic, fhr, fhr_location, " . "fundic_height, presentation, data_type, flag_private) values ('" . $post_vars["mc_id"] . "', " . "'" . $get_vars["consult_id"] . "', '{$patient_id}', '{$visit_date}', " . "'{$visit_date}', '" . $_SESSION["userid"] . "', '{$aog_total}', " . "'{$trimester}', '" . $post_vars["visit_sequence"] . "', " . "'" . $post_vars["patient_weight"] . "', '" . $post_vars["patient_systolic"] . "', " . "'" . $post_vars["patient_diastolic"] . "', '" . $post_vars["fhr"] . "', " . "'" . $post_vars["fhr_location"] . "', '" . $post_vars["fundic_height"] . "', " . "'" . $post_vars["presentation"] . "', '{$data_type}', '{$private}')";
                     $result = mysql_query($sql) or die("Cannot query: 840" . mysql_error());
                     mc::reorder_prenatal_visits($post_vars["mc_id"]);
                     if ($result) {
                         // do not update for visit 1
                         if ($post_vars["visit_sequence"] > 1) {
                             // delete risk factors first but only the M flagged
                             // caution use this only with MySQL version 4.+
                             $sql_delete = "delete from m_consult_mc_visit_risk " . "using m_consult_mc_visit_risk, m_lib_mc_risk_factors " . "where m_consult_mc_visit_risk.consult_id = '" . $get_vars["consult_id"] . "' " . "and m_consult_mc_visit_risk.mc_id = '" . $post_vars["mc_id"] . "' and " . "m_consult_mc_visit_risk.visit_risk_id = m_lib_mc_risk_factors.risk_id and " . "m_lib_mc_risk_factors.monitor_flag = 'Y'";
                             $result_delete = mysql_query($sql_delete);
                             // ...then update from form
                             foreach ($post_vars["risk"] as $key => $value) {
                                 /*$sql_risk = "insert into m_consult_mc_visit_risk (consult_id , ".
                                                                             "patient_id, mc_id, visit_risk_id, risk_timestamp, ".
                                                                             "user_id) values ('".$get_vars["consult_id"]."', ".
                                                                             "'$patient_id', '".$post_vars["mc_id"]."', '$value', ".
                                                                             "sysdate(), '".$_SESSION["userid"]."')";
                                 
                                 								 */
                                 $date_ngayon = date('Y-m-d');
                                 $sql_risk = "insert into m_consult_mc_visit_risk (consult_id , " . "patient_id, mc_id, visit_risk_id, risk_timestamp,date_detected," . "user_id) values ('" . $get_vars["consult_id"] . "', " . "'{$patient_id}', '" . $post_vars["mc_id"] . "', '{$value}', " . "sysdate(),{$date_ngayon},'" . $_SESSION["userid"] . "')";
                             }
                         }
                         header("location: " . $_SERVER["PHP_SELF"] . "?page=" . $get_vars["page"] . "&menu_id=" . $get_vars["menu_id"] . "&consult_id=" . $get_vars["consult_id"] . "&ptmenu=DETAILS&module=mc&mc=PREN" . ($post_vars["mc_id"] ? "&mc_id=" . $post_vars["mc_id"] : ""));
                     }
                 }
                 break;
             case "Update Prenatal":
                 //print_r($post_vars);
                 $exists = mc::check_visit_date($post_vars["visit_date"], $post_vars[mc_id]);
                 /*if ($post_vars["patient_systolic"] && $post_vars["patient_diastolic"] &&
                                     $post_vars["patient_weight"] && $post_vars["fundic_height"] && $post_vars["fhr"] &&
                                     $post_vars["fhr_location"] && $post_vars["presentation"] && $post_vars["visit_date"]) {
                 		*/
                 if ($post_vars["visit_date"]) {
                     list($month, $day, $year) = explode("/", $post_vars["visit_date"]);
                     $flag_private = isset($post_vars["check_private"]) ? 'Y' : '';
                     $visit_date = "{$year}-" . str_pad($month, 2, "0", STR_PAD_LEFT) . "-" . str_pad($day, 2, "0", STR_PAD_LEFT);
                     $trimester = mc::get_trimester($post_vars["mc_id"], $visit_date);
                     $sql = "update m_consult_mc_prenatal set " . "fundic_height = '" . $post_vars["fundic_height"] . "', " . "fhr = '" . $post_vars["fhr"] . "', " . "prenatal_date = '" . $visit_date . "', " . "fhr_location = '" . $post_vars["fhr_location"] . "', " . "trimester = '" . $trimester . "', " . "presentation = '" . $post_vars["presentation"] . "', " . "patient_weight = '" . $post_vars["patient_weight"] . "', " . "blood_pressure_systolic = '" . $post_vars["patient_systolic"] . "', " . "blood_pressure_diastolic = '" . $post_vars["patient_diastolic"] . "', " . "flag_private = '" . $flag_private . "' " . "where mc_id = '" . $post_vars["mc_id"] . "' and visit_sequence = '" . $post_vars["visit_sequence"] . "'";
                     $result = mysql_query($sql) or die("Cannot update: 855");
                     if ($result) {
                         // do not update for visit 1
                         if ($post_vars["visit_sequence"] > 1) {
                             // delete risk factors first but only the M flagged
                             // caution use this only with MySQL version 4.+
                             /*$sql_delete = "delete from m_consult_mc_visit_risk ".
                                                                       "using m_consult_mc_visit_risk, m_lib_mc_risk_factors ".
                                                                       "where m_consult_mc_visit_risk.consult_id = '".$post_vars["consult_id"]."' ".
                                                                       "and m_consult_mc_visit_risk.mc_id = '".$post_vars["mc_id"]."' and ".
                                                                       "m_consult_mc_visit_risk.visit_risk_id = m_lib_mc_risk_factors.risk_id and ".
                                                                       "m_lib_mc_risk_factors.monitor_flag = 'Y'";
                                                         $result_delete = mysql_query($sql_delete);
                             							
                                                         // ...then update from form							
                             
                             							foreach($post_vars["risk"] as $key=>$value) {								
                                                             $sql_risk = "insert into m_consult_mc_visit_risk (consult_id , ".
                                                                         "patient_id, mc_id, visit_risk_id, risk_timestamp, ".
                                                                         "user_id) values ('".$get_vars["consult_id"]."', ".
                                                                         "'$patient_id', '".$post_vars["mc_id"]."', '$value', ".
                                                                         "sysdate(), '".$_SESSION["userid"]."')";
                                                             $result_risk = mysql_query($sql_risk);
                                                         }*/
                         }
                         header("location:  " . $_SERVER["PHP_SELF"] . "?page=" . $get_vars["page"] . "&menu_id=" . $get_vars["menu_id"] . "&consult_id=" . $get_vars["consult_id"] . "&ptmenu=DETAILS&module=mc&mc=PREN" . ($post_vars["mc_id"] ? "&mc_id=" . $post_vars["mc_id"] : ""));
                     } else {
                         echo 'Record was not updated';
                     }
                 } else {
                     echo "Cannot update. Please complete the form.";
                 }
                 break;
             case "Delete Prenatal":
                 if (module::confirm_delete($menu_id, $post_vars, $get_vars)) {
                     $sql = "delete from m_consult_mc_prenatal where mc_id = '" . $post_vars["mc_id"] . "' and visit_sequence = '" . $post_vars["visit_sequence"] . "'";
                     if ($result = mysql_query($sql)) {
                         // delete associated risk data for this prenatal visit
                         // there is no referential integrity definition here...
                         $sql_risk = "delete from m_consult_mc_visit_risk where mc_id = '" . $post_vars["mc_id"] . "' and consult_id = '" . $post_vars["consult_id"] . "'";
                         $result_risk = mysql_query($sql_risk);
                         header("location: " . $_SERVER["PHP_SELF"] . "?page=" . $get_vars["page"] . "&menu_id=" . $get_vars["menu_id"] . "&consult_id=" . $get_vars["consult_id"] . "&ptmenu=DETAILS&module=mc&mc=PREN" . ($post_vars["mc_id"] ? "&mc_id=" . $post_vars["mc_id"] : ""));
                     }
                 } else {
                     if ($post_vars["confirm_delete"] == "No") {
                         header("location: " . $_SERVER["PHP_SELF"] . "?page=" . $get_vars["page"] . "&menu_id=" . $get_vars["menu_id"] . "&consult_id=" . $get_vars["consult_id"] . "&ptmenu=DETAILS&module=mc&mc=PREN" . ($post_vars["mc_id"] ? "&mc_id=" . $post_vars["mc_id"] : ""));
                     }
                 }
                 break;
             case "Save Postpartum Data":
                 if ($post_vars["delivery_date"] && $post_vars["delivery_location"] && $post_vars["outcome"] && $post_vars["birth_weight"] && $post_vars["attendant"]) {
                     if (isset($_POST[breastfeeding_flag])) {
                         //naka-check
                         if (empty($_POST[date_breastfed])) {
                             //no date but checked
                             $datebreastfeed = 'n';
                         } elseif ($this->get_day_diff($_POST[date_breastfed], $post_vars["delivery_date"]) < 0) {
                             //check if date of breastfeeding occurs after the delivery date
                             $datebreastfeed = 'w';
                         } else {
                             $datebreastfeed = 'y';
                             //set to yes is date_breastfed is not empty and not exceeding delivery date
                         }
                     } else {
                         $datebreastfeed = 'y';
                         //not checked, no need to check for date
                     }
                     if ($datebreastfeed == 'y') {
                         list($month, $day, $year) = explode("/", $post_vars["delivery_date"]);
                         list($bmonth, $bday, $byr) = explode("/", $_POST["date_breastfed"]);
                         $delivery_date = "{$year}-" . str_pad($month, 2, "0", STR_PAD_LEFT) . "-" . str_pad($day, 2, "0", STR_PAD_LEFT);
                         echo $delivery_date;
                         if (isset($_POST[breastfeeding_flag])) {
                             $bfeed_date = "{$byr}-" . str_pad($bmonth, 2, "0", STR_PAD_LEFT) . "-" . str_pad($bday, 2, "0", STR_PAD_LEFT);
                         } else {
                             $bfeed_date = '';
                         }
                         $healthy_baby = $post_vars["healthy_baby_flag"] ? "Y" : "N";
                         $breastfeeding = $post_vars["breastfeeding_flag"] ? "Y" : "N";
                         $end_pregnancy = $post_vars["end_pregnancy_flag"] ? "Y" : "N";
                         // NOTE: update postpartum date from delivery date
                         if (empty($get_vars["mc_id"])) {
                             $patient_id = healthcenter::get_patient_id($_GET[consult_id]);
                             if ($insert_new_patient) {
                                 $sql = "insert into m_patient_mc SET patient_id='{$patient_id}',consult_id='{$_GET['consult_id']}',patient_lmp=from_days(to_days('{$delivery_date}')-280),trimester1_date=from_days(to_days('{$delivery_date}')-196),trimester2_date=from_days(to_days('{$delivery_date}')-91),trimester3_date=from_days(to_days('{$delivery_date}')),mc_timestamp=sysdate(),mc_consult_date=sysdate(),patient_edc='{$delivery_date}',delivery_date='{$delivery_date}',postpartum_date=from_days(to_days('{$delivery_date}')+42),obscore_gp='{$post_vars['obscore_gp']}',obscore_fpal='{$post_vars['obscore_fpal']}',birthweight='{$post_vars['birth_weight']}',child_patient_id='{$child_id}',delivery_location='{$post_vars['delivery_location']}',outcome_id='{$post_vars['outcome']}',healthy_baby='{$health_baby}',end_pregnancy_flag='{$end_pregnancy}',breastfeeding_asap='{$breastfeeding}',date_breastfed='{$bfeed_date}',birthmode='{$post_vars['attendant']}'";
                                 //$sql = "insert into m_patient_mc SET patient_id='$patient_id',consult_id='$_GET[consult_id]',patient_lmp=from_days(to_days('$delivery_date')-280),trimester1_date=from_days(to_days('$delivery_date')-186.7),trimester2_date=from_days(to_days('$delivery_date')-93.3),trimester3_date=from_days(to_days('$delivery_date')),mc_timestamp=sysdate(),mc_consult_date=sysdate(),delivery_date='$delivery_date',patient_edc='$date_delivery',postpartum_date=from_days(to_days('$delivery_date')+42),obscore_gp='$post_vars[obscore_gp]',obscore_fpal='$post_vars[obscore_fpal]',birthweight='$post_vars[birth_weight]',child_patient_id='$post_vars[child_patient_id]',delivery_location='$post_vars[delivery_location]',outcome_id='$post_vars[outcome]',healthy_baby='$health_baby',end_pregnancy_flag='$end_pregnancy',breastfeeding_asap='$breastfeeding',date_breastfed='$bfeed_date',birthmode='$post_vars[attendant]'";
                             }
                         } else {
                             $mothers_name = mysql_query("SELECT patient_lastname, patient_firstname, family_id, child_patient_id FROM m_patient a JOIN m_patient_mc b ON a.patient_id = b.patient_id JOIN m_family_members c ON a.patient_id = c.patient_id WHERE b.mc_id ='" . $get_vars["mc_id"] . "' AND c.patient_id='{$patient_id}'") or die("Cannot query 1158: " . mysql_error());
                             $name_result = mysql_fetch_assoc($mothers_name);
                             $lastname = ucwords($post_vars[child_lastname]);
                             $firstname = ucwords($post_vars[child_firstname]);
                             $middlename = ucwords($post_vars[child_middlename]);
                             if ($name_result['child_patient_id'] == 0) {
                                 //INSERT new patient if no child patient id found
                                 if ($post_vars[child_lastname] != null && $post_vars[child_firstname] != null && $post_vars[child_lastname] != null) {
                                     $mom_name = $name_result['patient_firstname'] . " " . $name_result['patient_lastname'];
                                     $child_family_id = $name_result['family_id'];
                                     $get_pxid = mysql_query("SELECT patient_id FROM m_patient ORDER BY patient_id DESC LIMIT 1") or die("Cannot query 1163: " . mysql_error());
                                     $pxid_result = mysql_fetch_assoc($get_pxid);
                                     $child_id = $pxid_result['patient_id'] + 1;
                                     echo $post_vars[px_gender];
                                     $insert_new_patient = mysql_query("INSERT INTO m_patient SET patient_id='{$child_id}',patient_lastname='{$lastname}', patient_firstname='{$firstname}',patient_middle='{$middlename}',patient_dob='{$delivery_date}',patient_gender='{$post_vars['px_gender']}',registration_date=sysdate(),user_id='{$_SESSION['userid']}',healthcenter_id='{$_SESSION['datanode']}[code]',patient_mother='{$mom_name}',patient_cellphone=''") or die("Cannot query: 1168 " . mysql_error());
                                     if ($child_family_id != 0) {
                                         $check_family = mysql_query("SELECT family_id FROM m_family_members WHERE patient_id ='{$child_id}'") or die("Cannot Query 1154: " . mysql_error());
                                         if (mysql_num_rows($check_family) == 0) {
                                             $insert_into_family = mysql_query("INSERT INTO m_family_members SET family_id ='{$child_family_id}',family_role='member', patient_id = '{$child_id}'") or die("Cannot query 1177: " . mysql_error());
                                         }
                                     }
                                 }
                             } else {
                                 //UPDATE child information if child_patient_id value is more than 0
                                 $child_id = $name_result['child_patient_id'];
                                 $mom_name = $name_result['patient_firstname'] . " " . $name_result['patient_lastname'];
                                 $update_patient = mysql_query("UPDATE m_patient SET patient_lastname='{$lastname}',patient_firstname='{$firstname}',patient_middle='{$middlename}',patient_dob='{$delivery_date}',patient_gender='{$post_vars['px_gender']}',registration_date=sysdate(),user_id='{$_SESSION['userid']}',healthcenter_id='{$_SESSION['datanode']}[code]',patient_mother='{$mom_name}',patient_cellphone='' WHERE patient_id ='{$child_id}'") or die("Cannot query: 1182 " . mysql_error());
                             }
                             $sql = "update m_patient_mc set " . "delivery_date = '{$delivery_date}', " . "postpartum_date = from_days(to_days('{$delivery_date}')+42), " . "obscore_gp = '" . $post_vars["obscore_gp"] . "', " . "obscore_fpal = '" . $post_vars["obscore_fpal"] . "', " . "birthweight = '" . $post_vars["birth_weight"] . "', " . "delivery_location = '" . $post_vars["delivery_location"] . "', " . "child_patient_id = '{$child_id}', " . "outcome_id = '" . $post_vars["outcome"] . "', " . "healthy_baby = '{$healthy_baby}', " . "end_pregnancy_flag = '{$end_pregnancy}', " . "breastfeeding_asap = '{$breastfeeding}', " . "date_breastfed = '{$bfeed_date}', " . "birthmode = '" . $post_vars["attendant"] . "' " . "where mc_id = '" . $get_vars["mc_id"] . "'";
                         }
                         $result = mysql_query($sql) or die("Cannot query: 1068 " . mysql_error());
                         if ($result) {
                             header("location: " . $_SERVER["PHP_SELF"] . "?page=" . $get_vars["page"] . "&menu_id=" . $get_vars["menu_id"] . "&consult_id=" . $get_vars["consult_id"] . "&ptmenu=DETAILS&module=mc&mc=POSTP&mc_id=" . $get_vars["mc_id"]);
                         }
                     } else {
                         echo "<script language='Javascript'>";
                         if ($datebreastfeed == 'n') {
                             echo "window.alert('Record not saved. Please indicate the date of breastfeeding if the baby was breastfed ASAP.')";
                         } else {
                             //w ito
                             echo "window.alert('Record not saved. Date of breastfeeding should be on or after date of delivery.')";
                         }
                         echo "</script>";
                     }
                 } else {
                     echo "<script language='Javascript'>";
                     echo "window.alert('Record not saved. Please complete REQUIRED fields.')";
                     echo "</script>";
                 }
                 break;
             case "Delete Postpartum Data Form":
                 if (module::confirm_delete($menu_id, $post_vars, $get_vars)) {
                     //this would remove the initial postpartum data (i.e. dob,location, attendant)that has been encoded
                     echo "<script language='javascript'>";
                     echo "window.alert('Deleting the initial postpartum data will also clear out the details of the postpartum visits. Please think twice before doing so.')";
                     echo "</script>";
                     if ($this->clear_postpartum_data($_GET["mc_id"], $patient_id)) {
                         header("location: " . $_SERVER["PHP_SELF"] . "?page=" . $get_vars["page"] . "&menu_id=" . $get_vars["menu_id"] . "&consult_id=" . $get_vars["consult_id"] . "&ptmenu=DETAILS&module=mc&mc=POSTP&mc_id=" . $get_vars["mc_id"]);
                     } else {
                         if ($post_vars["confirm_delete"] == "No") {
                             header("location: " . $_SERVER["PHP_SELF"] . "?page=" . $get_vars["page"] . "&menu_id=" . $get_vars["menu_id"] . "&consult_id=" . $get_vars["consult_id"] . "&ptmenu=DETAILS&module=mc&mc=POSTP&mc_id=" . $get_vars["mc_id"]);
                         }
                     }
                 }
                 break;
             case "Terminate Pregnancy":
                 if (!empty($_POST["cause_termination"])) {
                     /*echo "<script language='javascript'>";
                     		echo "window.alert('Once a pregnancy is terminated, the record will be closed. Please ensure that the details of the pregnancy termination is correct.')";
                     		echo "<script>";*/
                     echo "<script language='javascript'>";
                     echo "window.alert('Once a pregnancy is terminated, the record will be closed. Please ensure that the details of the pregnancy termination is correct.')";
                     echo "</script>";
                     echo "You are terminating this pregnancy due to: <b>" . $_POST["cause_termination"] . "</b> on " . $_POST["txt_date_termination"] . "<br>";
                     if (module::confirm_delete($menu_id, $post_vars, $get_vars)) {
                         list($m, $d, $y) = explode('/', $_POST["txt_date_termination"]);
                         $date_term = $y . '/' . $m . '/' . $d;
                         $close_record = mysql_query("UPDATE m_patient_mc SET end_pregnancy_flag='Y',date_terminated_abortion='{$date_term}',termination_code='{$_POST['sel_cause_term']}', pregnancy_termination_cause='{$_POST['cause_termination']}' WHERE mc_id='{$_GET['mc_id']}' AND patient_id='{$patient_id}'") or die("Cannot query 1199 " . mysql_error());
                         if ($close_record) {
                             header("location: " . $_SERVER["PHP_SELF"] . "?page=" . $get_vars["page"] . "&menu_id=" . $get_vars["menu_id"] . "&consult_id=" . $get_vars["consult_id"] . "&ptmenu=DETAILS&module=mc&mc=VISIT1&mc_id=" . $get_vars["mc_id"]);
                             echo "</script>";
                         }
                     } else {
                         if ($post_vars["confirm_delete"] == "No") {
                             header("location: " . $_SERVER["PHP_SELF"] . "?page=" . $get_vars["page"] . "&menu_id=" . $get_vars["menu_id"] . "&consult_id=" . $get_vars["consult_id"] . "&ptmenu=DETAILS&module=mc&mc=VISIT1&mc_id=" . $get_vars["mc_id"]);
                         }
                     }
                 } else {
                     echo "<script language='javascript'>";
                     echo "window.alert('Please fill in the cause of the termination of this pregnancy!')";
                     echo "</script>";
                 }
                 break;
             case 'Open Pregnancy Record':
                 echo "<script>";
                 echo "window.alert('Opening this pregnancy record will allow you to edit its existing contents.')";
                 echo "</script>";
                 $update_mc = mysql_query("UPDATE m_patient_mc SET end_pregnancy_flag='N' WHERE mc_id='{$_GET['mc_id']}'") or die("Cannot query 1249: " . mysql_error());
                 break;
             default:
                 break;
         }
     }
 }