<?php
if ($_POST ['dataname'] && $_POST ['datavalue'] && $_POST ['datatablename']) {
	$dataname = $_POST ['dataname'];
	$datavalue = $_POST ['datavalue'];
	$tablename = $_POST ['datatablename'];
	$currentname = preg_replace ( '/[^a-zA-Z0-9\']/', '_', $datavalue );
	
	$query1 = $db->run ( "alter table `$tablename` change `$dataname` `$currentname` varchar(255)" );
	
	if (! $query1) {
		echo 1;
	} else {
		echo 2;
	}
}
if ($_POST ['dataname_deduction'] && $_POST ['datavalue_deduction'] && $_POST ['datatablename_deduction']) {
	$dataname = $_POST ['dataname_deduction'];
	$datavalue = $_POST ['datavalue_deduction'];
	$tablename = $_POST ['datatablename_deduction'];
	$currentname = preg_replace ( '/[^a-zA-Z0-9\']/', '_', $datavalue );
	
	$query1 = $db->run ( "alter table `$tablename` change `$dataname` `$currentname` varchar(255)" );
	
	if (! $query1) {
		echo 1;
	} else {
		echo 2;
	}
}
if ($_POST ['data_column'] && $_POST ['table_name']) {
	$columnname = $_POST ['data_column'];
	$tablename = $_POST ['table_name'];
	$query = $db->run ( "ALTER TABLE `$tablename` DROP `$columnname`" );
	if ($query) {
		echo 2;
	} else {
		echo 1;
	}
}
if ($_POST ['field_name'] && $_POST ['table'] && $_POST ['val'] && $_POST ['checkvalue']) {
	$table = $_POST ['table'];
	$fieldname = $_POST ['field_name'];
	$fieldname = explode ( "_", $fieldname );
	if ($fieldname [2] != '') {
		$fieldname = $fieldname [1] . '_' . $fieldname [2];
	} else {
		$fieldname = $fieldname [1];
	}
	
	$value = $_POST ['val'];
	$check = $_POST ['checkvalue'];
	if ($check == 'value') {
		$count = $db->get_count ( $table );
		
		if ($count == 1) {
			$update = $db->update ( $table, array ($fieldname => $value ), array ("id" => 1 ) );
		} else {
			$insert = $db->insert ( $table, array ($fieldname => $value ) );
		}
		
		if ($update || $insert) {
			echo 1;
		} else {
			echo 2;
		}
	} else {
		$count = $db->get_count ( $table );
		
		if ($count == 1) {
			$total_first = $db->run ( "show columns from `$table`" );
			$value1_first = $db->get_row ( "$table", array ("id" => 1 ) );
			$arr = array ();
			foreach ( $total_first as $row ) {
				$string = $value1_first [$row ['Field']];
				// type-checking comparison operator is necessary
				if (strpos ( $string, '%' ) !== false) {
					$arr [] = $string;
				}
			}
			
			$total = array_sum ( $arr );
			
			// ********************************second table data
			$countsecond = $db->get_count ( "second_salaryslip" );
			if ($countsecond == 1) {
				$total_second = $db->run ( "show columns from `second_salaryslip`" );
				$value1_second = $db->get_row ( "second_salaryslip", array ("id" => 1 ) );
				$arr1 = array ();
				foreach ( $total_second as $row ) {
					$string = $value1_second [$row ['Field']];
					// type-checking comparison operator is necessary
					if (strpos ( $string, '%' ) !== false) {
						$arr1 [] = $string;
					}
				}
				
				$total1 = array_sum ( $arr1 );
			}
			if ($countsecond == 1) {
				$totalvalue = $total + $value + $total1;
			} else {
				// after adding the value
				$totalvalue = $total + $value;
			}
			
			if ($totalvalue < 100) {
				$update = $db->update ( $table, array ($fieldname => $value ), array ("id" => 1 ) );
			} else {
				echo 2;
			}
		} else {
			$insert = $db->insert ( $table, array ($fieldname => $value ) );
		}
		
		if ($update || $insert) {
			echo 1;
		}
		/*
		 * $firstsalarydata= $db->get_all("first_salaryslip"); foreach
		 * ($firstsalarydata[0] as $value=>$detect) { $get_fieldname[]= $value;
		 * if(strpos($field_val,'%') !== false) { $detect_current= explode("%",
		 * $field_val); $percentvalue []= $detect_current[0].','; }
		 * //$get_value[]= } echo array_sum($firsttablepercent);
		 */
	}
}
if (isset ( $_POST ['columnname'] )) {
	$columnname = $_POST ['columnname'];
	$value = explode ( "_", $columnname );
	if ($value [0] == 'firsttable') {
		if ($value [2]) {
			$query = $db->run ( "ALTER TABLE `first_salaryslip` DROP `$value[1]_$value[2]`" );
			
			if ($query) {
				echo 1;
			} else {
				echo 2;
			}
		} else {
			$query = $db->run ( "ALTER TABLE `first_salaryslip` DROP `$value[1]`" );
			
			if ($query) {
				echo 1;
			} else {
				echo 2;
			}
		}
	}
	if ($value [0] == 'secondtable') {
		if ($value [2]) {
			$query = $db->run ( "ALTER TABLE `second_salaryslip` DROP `$value[1]_$value[2]`" );
			
			if ($query) {
				echo 1;
			} else {
				echo 2;
			}
		} else {
			$query = $db->run ( "ALTER TABLE `second_salaryslip` DROP `$value[1]`" );
			
			if ($query) {
				echo 1;
			} else {
				echo 2;
			}
		}
	}
}
if (isset ( $_POST ['incometax'] )) {
	$income = $_POST ['incometax'];
	$count = $db->get_count ( "calculator" );
	if ($count == 1) {
		$update = $db->update ( "calculator", array ("incometaxnull" => $income ), array ("id" => 1 ) );
		echo 1;
	} else {
		$insert = $db->insert ( "calculator", array ("incometaxnull" => $income ) );
		echo 1;
	}

}
?>