<?php 
$page_name="add_expense";  
include ('../common/library.php');  
include ('../common/helper.php');  
include ('../template/header.php'); 
include ('../template/sidebar.php');
$page_name='customer_setup';

if($_GET['exp_no']>0){
	$_SESSION['exp_no']=$_GET['exp_no'];
	//change location
	echo "<script>window.location.href='details_expense.php'</script>";
}

if(isset($_POST['save']) || isset($_POST['update'])){


$status = 'Paid';
$tr_from = 'Expense';
$tr_no = $_SESSION['exp_no'];

$max_jv = find_a_field('journal','max(jv_no)','1');
if($max_jv=='' || $max_jv==0){
$max_jv = date('Ymd');
}else{
	$max_jv = $max_jv+1;
}

$delquery = "DELETE FROM journal WHERE tr_from like 'Expense' and  tr_no =".$_SESSION['exp_no'];
$conn->query($delquery);

$select = $conn->query("select sum(expense_amount) as total_amt, exp_no,expense_date,payment_type,ledger_id 
from expenses where  exp_no='".$_SESSION['exp_no']."' group by ledger_id ");
while($row = $select->fetch_object()){


journal($max_jv,$row->expense_date,$row->ledger_id,$row->total_amt,0,$tr_from,$tr_no,$status);

journal($max_jv,$row->expense_date,$row->payment_type,0,$row->total_amt,$tr_from,$tr_no,$status);
}

unset($_SESSION['exp_no']);
// chage location
echo "<script>window.location.href='add_expense.php';</script>";  

}



//=========>>>>>> Delete <<<<<<<<<<=======\\\\\\

if(isset($_POST['exp_delete'])&&$_SESSION['exp_no']>0){
	$del = "DELETE FROM journal WHERE tr_from like 'Expense' and tr_no = ".$_SESSION['exp_no']."";
	$delCon = $conn->query($del); 

	$del = "DELETE FROM expenses WHERE exp_no = ".$_SESSION['exp_no']."";
	$delCon = $conn->query($del);

	if($delCon){
		unset($_SESSION['exp_no']);
		$msg = "Successfully Deleted !";
	}
}

//=========>>>>>> Add <<<<<<<<<<=======\\\\\\
if(isset($_POST['add'])){
	$expense_head = $_POST['expense_head'];
	$details = $_POST['details'];
	$expense_date = $_POST['expense_date'];
	$expense_amount = $_POST['expense_amount'];
	$payment_type = $_POST['payment_type'];
	$cr_ledger = $payment_type;
	$previous_due = $_POST['business_name'];
	$previous_advance = $_POST['business_name'];
	$entry_by = $_SESSION['user_id'];
	$ledger_group = 1002; //income
	$check_ledger = find_a_field('ledger','ledger_id','group_for='.$_SESSION['group_for'].' and ledger_name like "%'.$expense_head.'%"');
	if($check_ledger==''){
	$head_type = 'Expense';
	$ledger =$conn->query("INSERT INTO `ledger` (`ledger_name`, `ledger_type`,`head_type`,`group_for`) VALUES ('".$expense_head."', '".$ledger_group."','".$head_type."','".$_SESSION['group_for']."')"); 
	$ledger_id = mysqli_insert_id($conn);
	}else{
	$ledger_id = $check_ledger ;
	}

	if($_SESSION['exp_no']==''){
		$max_payment = find_a_field('expenses','max(exp_no)','1');
		if($max_payment=='' || $max_payment==0){
		$max_payment = date('Ymd');
		}else{
		$max_payment = $max_payment+1;
		}
	}else{
		$max_payment = $_SESSION['exp_no'];
	}

	$expense_sql = "INSERT INTO `expenses`(`exp_no`, `expense_head`, `details`, `expense_date`, `expense_amount`, `payment_type`, `entry_by`, `ledger_id`)
	VALUES ('".$max_payment."','".$expense_head."','".$details."','".$expense_date."','".$expense_amount."','".$payment_type."','".$entry_by."','".$ledger_id."')";
	$expenses =$conn->query($expense_sql);

	if($expenses==true){
		$msg =  '<span style="color:green; font-weight:bold; font-size:18px;">Success</span>';
		$_SESSION['exp_no'] = $max_payment;
	}else{
		$msg =  '<span style="color:red; font-weight:bold; font-size:18px;">Plesae Try Again!</span>';
	}
		

}

//=========>>>>>> exp delte <<<<<<<<<<=======\\\\\\

if($_GET['del']>0){
	$del = "DELETE FROM expenses WHERE id = ".$_GET['del']."";
	$delCon = $conn->query($del); 

	if($delCon){
		$msg = "Successfully Deleted !";
	}
	echo "<script>window.location.href='details_expense.php';</script>";
}

/*while($c_data = $select->fetch_assoc()){
 $p++;
 $code[$p] = $c_data['id'];
 $business_name[$p] = $c_data['business_name'];
 $propritor_name[$p] = $c_data['propritor_name'];
 $mobile_no[$p] = $c_data['mobile_no'];
 $credit_limit[$p] = $c_data['credit_limit'];
 $entry_by[$p] = $c_data['entry_by'];
} 
*/ ?>

		
			<div class="main-panel">
			    <div class="content">
                    <div class="row">
						<div class="col-md-12">
						<div class="container">
				<div class="card">
					<div class="card-header">
						<div class="d-flex align-items-center">
							<h4 class="card-title">Expense Entry. <?php echo $msg ;?></h4>
						</div>
					</div>

					<div class="card-body">
						<form method="post" id="sales" autocomplete="off">
							<div class="row">
								<div class="col-sm-2">
									<div class="form-group row">
									<label>Payment Type <span style="color:#FF0000; font-size:15px;">*</span></label>
									<select name="payment_type" id="payment_type"  class="form-control"  required>
										<option value="1000007">Cash</option>
										<option value="1000008">Bank</option>
										<option value="1000010">Wire Transfer</option>
									</select>
									</div>
								</div>
								<div class="col-sm-2">
									<div class="form-group row">
										<label>Expense Date <span style="color:#FF0000; font-size:15px;">*</span></label>
										<input type="date" name="expense_date"  id="expense_date" value="<? echo date('Y-m-d');?>"  class="form-control" required>
									</div>
								</div>

								<div class="col-sm-2">
									<div class="form-group row">
										<label>Expense Head <span style="color:#FF0000; font-size:15px;">*</span></label>
										
										<input type="text" list="browsers" name="expense_head"  id="expense_head" value=""  class="form-control" placeholder="Expense Head" required>
										<datalist id="browsers">
											<? 
											$s = $conn->query("select ledger_name from ledger where head_type='Expense'");
											while($expense_head = $s->fetch_assoc()){
											?>
											<option value="<?=$expense_head['ledger_name']?>">
											<? } ?>
											</datalist> 
										
									</div>
								</div>

								<div class="col-sm-2">
									<div class="form-group row">
										<label>Details <span style="color:#FF0000; font-size:15px;">*</span></label>
										<input type="text" name="details"  id="details" value=""  class="form-control" placeholder="Details" required>
									</div>
								</div>

								<div class="col-sm-2">
									<div class="form-group row">
										<label>Amount</label>
										<input name="expense_amount" id="expense_amount" value="" type="text" class="form-control" placeholder="Amount">
									</div>
								</div>
								<div class="col-sm-2">
									<div class="form-group row">
										<input style="margin-top: 27px;" type="submit" name="add" value="Add" class="btn btn-primary sm">
									</div>
								</div>
							</div>



							<!-- Registered Customer Start-->
							<div class="row" id="rc" style="display:none;">
								<div class="col-6">
									
								</div>

								<div class="col-12">
									
								</div>
							</div>
						</form>	
					</div>
				</div>



				<div class="card">
					<div class="card-body">
						<table class="text-center" style="width:100%">
							<thead>
								<th>Exp.No</th>
								<th>Payment Type</th>
								<th>Exp.Date</th>
								<th>Exp.Head</th>
								<th>Dtails</th>
								<th>Amount</th>
								<th colspan="2">Action </th>
							</thead>

							<tbody>
							<tbody id="show">
								<?php
										 $select = $conn->query("select * from expenses where exp_no='".$_SESSION['exp_no']."' ");
										 $total = 0;
                                         while( $item_data = $select->fetch_object()){
											$total+=$item_data->expense_amount;
										?>
								<tr>


									<td><?=$item_data->exp_no;?></td>
									<td><? 
										if($item_data->payment_type == 1000007){
											echo "Cash";
										}elseif($item_data->payment_type == 1000008){
											echo "Bank";
										}elseif($item_data->payment_type == 1000010){
											echo "Wire Transfer";
										}

									?></td>
									<td><?=$item_data->expense_date;?></td>
									<td><?=$item_data->expense_head;?></td>
									<td><?=$item_data->details;?></td>
									<td><?=$item_data->expense_amount;?></td>
									<td><a href="?del=<?php echo $item_data->id;?>">
									<button id="del_<?php echo $item_data->id;?>" class="btn btn-danger">X</button></a>
								</td>
									
								</tr>
								<?php } ?>
								<tr>
									<td colspan="5" style="text-align:right"><strong>Total:</strong></td>
									<td  style="text-align:center"><strong><?=number_format($total,2);?></strong></td>
								</tr>
							</tbody>
						</table>
					</div>
				</div>

				<form action="" method="post">
				<div class="card-footer text-center">
					
					<?php if($_SESSION['exp_no']>0){?>
					<input type="submit" name="save" class="btn btn-secondary">
					<input type="submit" name="exp_delete" value="Delete" class="btn btn-warning">
					<?php } ?>
				</div>
				</form>
				
			</div>

						</div>
					</div>
				</div>
			</div>


<?php include('../template/footer.php');?>