<?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**************
	 */
	$current_year = date ( "Y" );
	$current_month = date ( "M" );
	$total_year = $current_year - 1;
	$dbidcheck = $db->exists ( "salaryslipdata", array ("emp_id" => $emp_id, "month" => $current_month, "year" => $total_year . '-' . $current_year ) );
	
	$empid_check = $db->exists ( "employees", array ("e_id" => $emp_id ) );
	if ($empid_check) {
		$val = $_POST ['basic_salary'];
		if ($val != '') {
			if (ctype_digit ( $val )) {
				$upper_array = array ();
				$firstarr_key = array ();
				$firstarr_val = array ();
				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));
							$firstarr_key [] = $field;
							$firstarr_val [] = $_POST ['value___' . $field];
						
						}
					}
				}
				$firstarr_key [] = "basic_salary";
				$firstarr_val [] = $val;
				$upper_array = array_combine ( $firstarr_key, $firstarr_val );
				/*
				 * echo "<pre>"; print_r($upper_array); echo "</pre>";
				 */
				
				/**
				 * *************************************GET THE EARNING
				 * INFORMATION***************************
				 */
				$count = $db->get_count ( 'first_salaryslip' );
				$table = "first_salaryslip";
				$middle_array = array ();
				$middlearr_key = array ();
				$middlearr_val = array ();
				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;
							}
							$middlearr_key [] = $row ['Field'];
							$middlearr_val [] = $string;
						}
					}
				
				}
				if ($arr1) {
					$percentage = array_sum ( $arr1 );
				}
				
				if ($arr2) {
					$numeric = array_sum ( $arr2 );
				}
				
				$earning = (($val * $percentage) / 100) + $val + $numeric;
				$middlearr_key [] = "totalearning";
				$middlearr_val [] = $earning;
				
				$middle_array = array_combine ( $middlearr_key, $middlearr_val );
				/*
				 * echo "<pre>"; print_r($middle_array); echo "</pre>";
				 */
				
				// ********************************second table data
				
				$countsecond = $db->get_count ( "second_salaryslip" );
				$middlearr_ded = array ();
				$middlearr_keyded = array ();
				$middlearr_valded = array ();
				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;
							}
							$middlearr_keyded [] = $row ['Field'];
							$middlearr_valded [] = $string;
						}
					}
				
				}
				if ($arr3) {
					$percentage1 = array_sum ( $arr3 );
				}
				
				if ($arr4) {
					$numeric1 = array_sum ( $arr4 );
				}
				
				$deduction = ($val - (($val * $percentage1) / 100)) + $numeric1;
				
				$middlearr_keyded [] = "totaldeduction";
				$middlearr_valded [] = $deduction;
				
				$middlearr_ded = array_combine ( $middlearr_keyded, $middlearr_valded );
				/*
				 * echo "<pre>"; print_r($middlearr_ded); echo "</pre>";
				 */
				/**
				 * ****************************FOOTER
				 * DATA******************************
				 */
				
				$gross_income = ($val + $earning) - $deduction;
				$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'];
				$footerarr = array ();
				$footerarr = array ("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 );
				/*
				 * echo "<pre>"; print_r($footerarr); echo "</pre>";
				 */
				
				$finalarray = array (array ($upper_array, $middle_array, $middlearr_ded, $footerarr ) );
				/*
				 * echo "<pre>"; print_r($finalarray); echo "</pre>";
				 */
				
				if ($dbidcheck) {
					$update = $db->update ( "salaryslipdata", array ("metavalue" => serialize ( $finalarray ) ), array ("emp_id" => $emp_id ) );
					if ($update) {
						?>
<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 Updated..</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 Updated...</h5>
				<div style="clear: both"></div>
			</div>
			<div class="gritter-bottom"></div>
		</div>
	</div>
</div>
<?php
					
}
				} else {
					// echo
					$insert = $db->insert ( "salaryslipdata", array ("metavalue" => serialize ( $finalarray ), "emp_id" => $emp_id, "month" => $current_month, "year" => $total_year . '-' . $current_year ) );
					if ($insert) {
						?>
<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! Basic Salary Is Not Integer Value...</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
	
}

}
?>