<?php
include('../headertop.php');
include('../db.php');
//include('../datepicker.php');

$disp = '';
$pagetitle = 'Add Employee';

if(!isset($_POST['addsalary2submit']))
{
echo '<meta http-equiv="refresh" content="0;URL=addsalary.php">';
exit;
}

$doe = $_POST['doe'];
$salary_month = $_POST['salary_month'];
$salary_year = $_POST['salary_year'];
$salary_paid = $_POST['salary_paid'];

if(empty($doe) or empty($salary_month) or empty($salary_year))
{
$disp .= '<span class="error">Please go back and fill all required fields</span>';
include('../template.php');
exit;
}

$qwt = "select id from work_type where work_type='salary'";
$rwt = mysqli_query($dbcon,$qwt) or die(mysqli_error($dbcon));
$rwwt = mysqli_fetch_array($rwt);
$work_type = $rwwt['id'];

mysqli_autocommit($dbcon, false);

foreach($salary_paid as $k=>$v)
{
if(!empty($v))
{
$qemp = "select name from employee where id='$k'";
$remp = mysqli_query($dbcon,$qemp) or die(mysqli_error($dbcon));
$rwemp = mysqli_fetch_array($remp);

$expense_details = 'Salary to '.$rwemp['name'].' for '.$salary_month.', '.$salary_year;
$remarks = 'Salary for '.$rwemp['name'];

$q = "insert into expenses (project, date, amount,particulars,work_type) values('1','$doe','$v','$expense_details','$work_type')";
mysqli_query($dbcon,$q) or die(mysqli_error($dbcon));

//$exp_id = mysqli_insert_id($dbcon);
//$q2[] = "insert into salary (emp_id,salary_month,salary_year,amount,payment_date,exp_id) values('$k','$salary_month','$salary_year','$v','$doe','$exp_id')";
}
}//end of foreach





mysqli_commit($dbcon);

$sm = "Employee salaries saved for Month: ".$salary_month.", ".$salary_year;
echo '<meta http-equiv="refresh" content="0;URL=addsalary.php?sm='.$sm.'">';