<?php 
include_once('../header.php');
include_once(ROOT_PATH.'language/'.$lang_code_global.'/lang_add_fund.php');
if(!check_login()){
	header("Location: " . WEB_URL . "logout.php");
	die();
}
$success = "none";
$owner_id = '';
$month_id = '';
$xyear = '';
$total_amount = '0.00';
$f_date = '';
$purpose = '';
$branch_id = '';
$title = $_data['add_new_fund'];
$button_text = $_data['save_button_text'];
$successful_msg = $_data['added_fund_successfully'];
$form_url = WEB_URL . "fund/add_fund.php";
$id="";
$hdnid="0";

//
$owner_name = '';
$ownid = 0;
//

if(isset($_POST['ddlOwnerName'])){
	if(isset($_POST['hdn']) && $_POST['hdn'] == '0'){
		$sql = "INSERT INTO tbl_add_fund(owner_id,month_id,xyear,f_date,total_amount,purpose,branch_id) values('$_POST[ddlOwnerName]','$_POST[ddlMonth]','$_POST[ddlYear]','$_POST[txtDate]','$_POST[txtTotalAmount]','$_POST[txtPurpose]','" . $_SESSION['objLogin']['branch_id'] . "')";
		mysqli_query($link,$sql);
		$fund_id = mysqli_insert_id($link);
		mysqli_close($link);
		if(isset($_POST['btnSavePrint'])){
			$url = WEB_URL . 'fund/invoice.php?fundid='.$fund_id;
			header("Location: $url");	
		} else {
			$url = WEB_URL . 'rent/rentlist.php?m=add';
			header("Location: $url");
		}
	}else{
		$sql = "UPDATE `tbl_add_fund` SET `owner_id`='".$_POST['ddlOwnerName']."',`month_id`='".$_POST['ddlMonth']."',`xyear`='".$_POST['ddlYear']."',`f_date`='".$_POST['txtDate']."',`total_amount`='".$_POST['txtTotalAmount']."',`purpose`='".$_POST['txtPurpose']."' WHERE fund_id='".$_GET['id']."'";
		mysqli_query($link,$sql);
		mysqli_close($link);
		if(isset($_POST['btnSavePrint'])){
			$url = WEB_URL . 'fund/invoice.php?fundid='.$_GET['id'];
			header("Location: $url");	
		} else {
			$url = WEB_URL . 'rent/rentlist.php?m=up';
			header("Location: $url");
		}
	}
	$success = "block";
}

if(isset($_GET['id']) && $_GET['id'] != ''){
	$result = mysqli_query($link,"SELECT *,o.o_name,o.ownid FROM tbl_add_fund fu inner join tbl_add_owner o on o.ownid = fu.owner_id where fu.fund_id = '" . $_GET['id'] . "'");
	while($row = mysqli_fetch_array($result)){
	
		$owner_id = $row['owner_id'];
		$month_id = $row['month_id'];
		$xyear = $row['xyear'];
		$f_date = $row['f_date'];
		$total_amount = $row['total_amount'];
		$purpose = $row['purpose'];
		$hdnid = $_GET['id'];
		$owner_name = $row['o_name'];
		$ownid = $row['ownid'];
		$title = $_data['update_fund'];
		$button_text = $_data['update_button_text'] ;
		$successful_msg = $_data['update_fund_successfully'];
		$form_url = WEB_URL . "fund/add_fund.php?id=".$_GET['id'];
	}
	
	//mysqli_close($link);
}
else{
}
//	
?>
<!-- Content Header (Page header) -->

<section class="content-header">
  <h1><?= $title;?></h1>
  <ol class="breadcrumb">
    <li><a href="<?= WEB_URL?>dashboard.php"><i class="fa fa-dashboard"></i><?= $_data['home_breadcam'];?></a></li>
    <li class="active"><?= $_data['fund'];?></li>
    <li class="active"><?= $title;?></li>
  </ol>
</section>
<!-- Main content -->
<section class="content">
<!-- Full Width boxes (Stat box) -->
<div class="row">
  <div class="col-md-12">
    <div align="right" style="margin-bottom:1%;"></div>
    <div class="box box-success">
      <div class="box-header">
        <h3 class="box-title"><?= $_data['add_new_fund_entry_form'];?></h3>
      </div>
      <form onSubmit="return validateMe();" action="<?= $form_url; ?>" method="post" enctype="multipart/form-data">
        <div class="box-body row">
          <div class="form-group col-md-6">
            <label for="ddlOwnerName"><span class="errorStar">*</span> <?= $_data['add_new_form_field_text_1'];?> :</label>
            <select name="ddlOwnerName" id="ddlOwnerName" class="form-control">
              <option value="">--<?= $_data['add_new_form_field_text_2'];?>--</option>
              <?php 
				  	$result_owner = mysqli_query($link,"SELECT * FROM tbl_add_owner where branch_id = " . (int)$_SESSION['objLogin']['branch_id'] . " order by ownid ASC");
					while($row_owner = mysqli_fetch_array($result_owner)){?>
              <option <?php if($owner_id == $row_owner['ownid']){echo 'selected';}?> value="<?= $row_owner['ownid'];?>"><?= $row_owner['o_name'];?></option>
              <?php } ?>
            </select>
          </div>
          <div class="form-group col-md-6">
            <label for="ddlMonth"><span class="errorStar">*</span> <?= $_data['add_new_form_field_text_3'];?> :</label>
            <select name="ddlMonth" id="ddlMonth" class="form-control">
              <option value="">--<?= $_data['add_new_form_field_text_3'];?>--</option>
              <?php 
				  	$result_month = mysqli_query($link,"SELECT * FROM tbl_add_month_setup order by m_id ASC");
					while($row_month = mysqli_fetch_array($result_month)){?>
              <option <?php if($month_id == $row_month['m_id']){echo 'selected';}?> value="<?= $row_month['m_id'];?>"><?= $row_month['month_name'];?></option>
              <?php } ?>
            </select>
          </div>
          <div class="form-group col-md-6">
            <label for="ddlYear"><span class="errorStar">*</span> <?= $_data['add_new_form_field_text_5'];?> :</label>
            <select name="ddlYear" id="ddlYear" class="form-control">
              <option value="">--<?= $_data['add_new_form_field_text_5'];?>--</option>
              <?php 
				  	$result_month = mysqli_query($link,"SELECT * FROM tbl_add_year_setup order by y_id ASC");
					while($row_month = mysqli_fetch_array($result_month)){?>
              <option <?php if($xyear == $row_month['y_id']){echo 'selected';}?> value="<?= $row_month['y_id'];?>"><?= $row_month['xyear'];?></option>
              <?php } ?>
            </select>
          </div>
          <div class="form-group col-md-6">
            <label for="txtDate"><span class="errorStar">*</span> <?= $_data['add_new_form_field_text_6'];?> :</label>
            <input type="text" name="txtDate" value="<?= $f_date;?>" id="txtDate" class="form-control datepicker"/>
          </div>
          <div class="form-group col-md-12">
            <label for="txtTotalAmount"><span class="errorStar">*</span> <?= $_data['add_new_form_field_text_7'];?> :</label>
            <div class="input-group">
              <input type="text" name="txtTotalAmount" value="<?= $total_amount;?>" id="txtTotalAmount" class="form-control" />
              <div class="input-group-addon"> <?= CURRENCY;?> </div>
            </div>
          </div>
          <div class="form-group col-md-12">
            <label for="txtPurpose"><span class="errorStar">*</span> <?= $_data['add_new_form_field_text_8'];?> :</label>
            <textarea name="txtPurpose" id="txtPurpose" class="form-control"><?= $purpose;?></textarea>
          </div>
        </div>
		<div class="box-footer">
          <div class="form-group pull-right">
            <button type="submit" name="btnSavePrint" class="btn btn-default"><i class="fa fa-floppy-o"></i> <?= $_data['save_and_print_invoice'];?></button>
			<button type="submit" name="button" class="btn btn-success"><i class="fa fa-floppy-o"></i> <?= $button_text; ?></button>
            <a class="btn btn-warning" href="<?= WEB_URL; ?>fund/fund_list.php"><i class="fa fa-reply"></i> <?= $_data['back_text'];?></a> </div>
        </div>
        <input type="hidden" value="<?= $hdnid; ?>" name="hdn"/>
      </form>
      <!-- /.box-body -->
    </div>
    <!-- /.box -->
  </div>
</div>
<!-- /.row -->
<script type="text/javascript">

function validateMe(){
	if($("#ddlOwnerName").val() == ''){
		alert("<?= $_data['required_1']; ?>");
		$("#ddlOwnerName").focus();
		return false;
	}
	else if($("#ddlMonth").val() == ''){
		alert("<?= $_data['required_2']; ?>");
		$("#ddlMonth").focus();
		return false;
	}
	else if($("#ddlYear").val() == ''){
		alert("<?= $_data['required_3']; ?>");
		$("#ddlYear").focus();
		return false;
	}
	else if($("#txtDate").val() == ''){
		alert("<?= $_data['required_4']; ?>");
		$("#txtDate").focus();
		return false;
	}
	else if($("#txtTotalAmount").val() == '0.00'){
		alert("<?= $_data['required_5']; ?>");
		$("#txtMCPreAddress").focus();
		return false;
	}
	else if($("#txtPurpose").val() == ''){
		alert("<?= $_data['required_6']; ?>");
		$("#txtPurpose").focus();
		return false;
	}
	else{
		return true;
	}
}
</script>
<?php include_once('../footer.php'); ?>
