<?php
if (isset ( $_POST ['submit'] )) {
	// echo "ssgsfd".$_POST['value___name_vala'];
	$firsttable = 'DESCRIBE `main_salaryslip`';
	$totaldata_firsttable = $db->run ( $firsttable )->fetchAll ();
	
	$assign = 1;
	foreach ( $totaldata_firsttable as $row ) {
		if ($row ['Field'] != 'id') {
			if ($assign == 2) {
				$a = $row ['Field'];
			}
		}
		$assign ++;
	}
	$emp_id = $_POST ['value___' . $a];
	/**
	 * ************************check the user id exist on that database
	 * table**************
	 */
	$dbidcheck = $db->exists ( "salaryslipdata", array ("emp_id" => $emp_id ) );
	if ($dbidcheck) {
		$db->delete ( "salaryslipdata", array ("emp_id" => $emp_id ) );
	}
	
	$empid_check = $db->exists ( "employees", array ("e_id" => $emp_id ) );
	if ($empid_check) {
		$val = $_POST ['basic_salary'];
		if ($val != '') {
			foreach ( $totaldata_firsttable as $row ) {
				if ($row ['Field'] != 'id') {
					{
						$field = $row ['Field'];
						$insert = $db->insert ( "salaryslipdata", array ("metaname" => $row ['Field'], "metavalue" => $_POST ['value___' . $field], "emp_id" => $emp_id ) );
					}
				}
			}
			$count = $db->get_count ( 'first_salaryslip' );
			$table = "first_salaryslip";
			if ($count == 1) {
				$total_first = $db->run ( "show columns from `$table`" );
				$value1_first = $db->get_row ( "$table", array ("id" => 1 ) );
				$arr1 = array ();
				$arr2 = array ();
				foreach ( $total_first as $row ) {
					if ($row ['Field'] != "id" && $row ['Field'] != "earning") {
						$string = $value1_first [$row ['Field']];
						// type-checking comparison operator is necessary
						if (strpos ( $string, '%' ) !== false) {
							$arr1 [] = $string;
						} else {
							$arr2 [] = $string;
						}
						$first_keys [] = $row ['Field'];
						$first_values [] = $string;
					}
				}
				$earningarray = array_combine ( $first_keys, $first_values );
			}
			if ($arr1) {
				$percentage = array_sum ( $arr1 );
			}
			
			if ($arr2) {
				$numeric = array_sum ( $arr2 );
			}
			
			$earning = (($val * $percentage) / 100) + $val + $numeric;
			/*
			 * $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 ) );
				$arr3 = array ();
				$arr4 = array ();
				foreach ( $total_second as $row ) {
					if ($row ['Field'] != "id" && $row ['Field'] != 'deduction') {
						$string = $value1_second [$row ['Field']];
						// type-checking comparison operator is necessary
						if (strpos ( $string, '%' ) !== false) {
							$arr3 [] = $string;
						} else {
							$arr4 [] = $string;
						}
						$second_keys [] = $row ['Field'];
						$second_values [] = $string;
					}
				}
				$deduction_array = array_combine ( $second_keys, $second_values );
			}
			
			if ($arr3) {
				$percentage1 = array_sum ( $arr3 );
			}
			
			if ($arr4) {
				$numeric1 = array_sum ( $arr4 );
			}
			
			$deduction = ($val - (($val * $percentage1) / 100)) + $numeric1;
			
			$gross_income = ($val + $earning) - $deduction;
			
			$passvalue = $earning . ',' . $deduction . ',' . $gross_income;
			$incometaxpercent = $_POST ['a_income_tax'];
			/**
			 * ******************************GET THE TAX
			 * VALUE****************************************
			 */
			
			$getnullvalue = $db->get_col ( "calculator", array ("id" => 1 ), "incometaxnull" );
			if ($val > $getnullvalue [0]) {
				$difference = $gross_income - $getnullvalue [0];
				$incometax = ($difference * $incometaxpercent) / 100;
				$netsalary = $gross_income - $incometax;
			}
			$preparedby = $_POST ['a_Prepared_By'];
			$authorised = $_POST ['a_Authorised_Sign'];
			$employeesign = $_POST ['a_Employee_sign'];
			/**
			 * *******************************************INSERT THE
			 * DATA*************************************************
			 */
			
			$insertarray = array ("basic_salary" => $val, "totalearning" => $earning, "totaldeduction" => $deduction, "a_annual_gross_income" => $gross_income, "a_income_tax" => $incometax, "a_Net_Salary_Rs" => $netsalary, "a_Prepared_By" => $preparedby, "a_Authorised_Sign" => $authorised, "a_Employee_sign" => $employeesign );
			
			$finalarray = array (array ($earningarray, $insertarray, $deduction_array ) );
			
			$insert1 = $db->insert ( "salaryslipdata", array ("metaname" => 'serialize', "metavalue" => serialize ( $finalarray ), "emp_id" => $emp_id ) );
			if ($insert && $insert1) {
				?>
<div id="hide">
	<div id="gritter-notice-wrapper">
		<div id="gritter-item-1" class="gritter-item-wrapper gritter-success"
			style="">
			<div class="gritter-top"></div>
			<div class="gritter-item">

				<div class="gritter-without-image">
					<span class="gritter-title">Cheers..
						<div class="gritter-close" id="gritterremove"
							style="display: inline;"></div>
					</span>
					<p>Your Record Is Inserted..</p>
				</div>
				<div style="clear: both"></div>
			</div>
			<div class="gritter-bottom"></div>
		</div>
	</div>
</div>
<?php
			
} else {
				?>
<div class="hide1">
	<div id="gritter-notice-wrapper">
		<div id="gritter-item-2" class="gritter-item-wrapper gritter-warning"
			style="">
			<div class="gritter-top"></div>
			<div class="gritter-item">
				<div class="gritter-close" id="gritterremove"
					style="display: inline;"></div>
				<h5>Oops! There Is Some Problem In Inserted...</h5>
				<div style="clear: both"></div>
			</div>
			<div class="gritter-bottom"></div>
		</div>
	</div>
</div>
<?php
			
}
		} 

		else {
			?>
<div class="hide1">
	<div id="gritter-notice-wrapper">
		<div id="gritter-item-2" class="gritter-item-wrapper gritter-warning"
			style="">
			<div class="gritter-top"></div>
			<div class="gritter-item">
				<div class="gritter-close" id="gritterremove"
					style="display: inline;"></div>
				<h5>Oops! Please Fill The Basic Salary...</h5>
				<div style="clear: both"></div>
			</div>
			<div class="gritter-bottom"></div>
		</div>
	</div>
</div>
<?php
		
}
	
	} else {
		?>
<div class="hide1">
	<div id="gritter-notice-wrapper">
		<div id="gritter-item-2" class="gritter-item-wrapper gritter-warning"
			style="">
			<div class="gritter-top"></div>
			<div class="gritter-item">
				<div class="gritter-close" id="gritterremove"
					style="display: inline;"></div>
				<h5>Oops! User Are Not Exist In Database...</h5>
				<div style="clear: both"></div>
			</div>
			<div class="gritter-bottom"></div>
		</div>
	</div>
</div>
<?php
	
}

}
?>