<?php
session_start();
include ('../inc/dbcon.php');
include ('../inc/functions.php');
if(isset($_SESSION["axeslogin"]) && $_SESSION["axeslogin"] == true){
$aid=$_SESSION['uid'];
$brid=$_SESSION['abrid'];    
}else{
header('Location:../index.php');
exit;     
}
$dtnow = date("Y-m-d h:i:s", time());
?>



<?php 
if(isset($_POST['addsitem'])){ 
$item=intval($_POST['item']);   
?>
<div class="col-md-12 popup_details_div addservice">
<div class="row">
<div id="server-results"><!-- For server results --></div>    
</div>
<div class="row">
<div class="col-md-12">
<div class="col-md-1"></div>
<div class="col-md-10">
<?php 
$sql="SELECT * FROM tbl_cheqman WHERE id='".$item."' ORDER BY id ASC LIMIT 1";    
$admin=mysqli_query($con,$sql) or die(mysqli_error($con));    
$adm=mysqli_fetch_array($admin);    
?>
<div class="form-group">
    <label>Select Accounts</label>  
    <select class="form-control select2" name="acid" id="acid" disabled>
        <option value="">-Select-</option>
        <?php									
            $sql="SELECT tbl_bacount.id AS id,CONCAT(tbl_bank.sort,' - ',tbl_bacount.acno) AS name FROM tbl_bacount LEFT JOIN tbl_bank ON tbl_bank.id=tbl_bacount.bid  ORDER BY tbl_bank.sort ASC";    
            $querys=mysqli_query($con,$sql)or die(mysqli_error($con));
            while ($rows=mysqli_fetch_array($querys)){
        ?>
        <?php if($rows['id']==$adm['acid']){ ?>
            <option selected value="<?php echo $rows['id'];?>"><?php echo $rows['name'];?></option>
            <?php }else{?>    
            <option value="<?php echo $rows['id'];?>"><?php echo $rows['name'];?></option>
            <?php } ?>
        <?php } ?>    
    </select>
  <input type="hidden" maxlength="11" value="<?php echo $adm['id'];?>" class="form-control" name="chkid" autocomplete="off" readonly> 
  <input type="hidden" maxlength="11" value="<?php echo $adm['refinv'];?>" class="form-control" name="refinv" autocomplete="off" readonly>   
</div>    

<input type="hidden" name="editcheck" value='1' readonly />  
<div class="row">
    <div class="col-md-6">    
        <div class="form-group">
            <label>Cheque No</label>    
            <input type="text" name="chkno" maxlength="45" value="<?php echo $adm['chkno'];?>" id="chkno" class="form-control"  readonly/>    
        </div>
    </div>    
    <div class="col-md-6">
        <div class="form-group">
            <label>Cheque Date</label>
            <input type="text" name="chkdt" maxlength="45" value="<?php echo date("d M Y", strtotime($adm['chkdt']));?>" id="chkdt" class="form-control"  readonly/>    
        </div>
    </div>     
</div> 
<div class="row">
    <div class="col-md-6">    
        <div class="form-group">
            <label>Amount</label>    
            <input type="text" name="amount" maxlength="45" value="<?php echo $adm['amount'];?>" id="amount" class="form-control"  readonly/>    
        </div>
    </div>    
    <div class="col-md-6">
        <div class="form-group">
            <label>Cheque Status</label>
            <select class="form-control select2" name="status" id="status">
                <option value="">-Select-</option>
                <!--<option value="1">Given</option>
                <option value="2">Receive</option>-->
                <?php if($adm['paymod']=='REC'){ ?><option <?php if($adm['status']==3){echo 'selected';}?> value="3">Deposit</option><?php }?>
                <?php if($adm['paymod']=='PAY'){ ?><option <?php if($adm['status']==4){echo 'selected';}?> value="4">Payment</option><?php }?>
                <option <?php if($adm['status']==5){echo 'selected';}?> value="5">Bounce</option>
                <option <?php if($adm['status']==6){echo 'selected';}?> value="6">Return</option>     
            </select>
        </div>
    </div>     
</div>  
<div class="form-group">
    <label>Select Client</label> 
        <input type="text" name="clientid" maxlength="100" value="<?php if($adm['type']=='SU'){ echo get_fild_data('tbl_supplier',$adm['clientid'],'name'), get_fild_data('tbl_supplier',$adm['clientid'],'code');  }else{ echo get_fild_data('tbl_customer',$adm['clientid'],'name'), get_fild_data('tbl_customer',$adm['clientid'],'code'); }?>" id="clientid" class="form-control"  readonly/>    
</div> 
<div class="row">
    <div class="col-md-6">    
        <div class="form-group">
        <label>Cheque Mode</label>
            <select class="form-control select2" name="paymod" id="paymod" disabled>
                <option value="">-Select-</option>
                <option <?php if($adm['paymod']=="REC"){echo 'selected';}?> value="3">Received</option>
                <option <?php if($adm['paymod']=="PAY"){echo 'selected';}?> value="4">Payment</option>    
            </select>
        </div>
    </div>    
    <div class="col-md-6">
        <div class="form-group">
            <label>Cheque Bank</label>
            <select class="form-control select2" name="chbnkid" id="chbnkid">
            <option value="">-Select-</option>
            <?php									
            $queryd=mysqli_query($con,"SELECT * FROM tbl_bank ORDER BY id ASC")or die(mysqli_error($con));
            while ($rowd=mysqli_fetch_array($queryd)){
            ?>
            <?php if($rowd['id']==$adm['chbnkid']){ ?>
            <option selected value="<?php echo $rowd['id'];?>"><?php echo $rowd['name'];?></option>
            <?php }else{?>    
            <option value="<?php echo $rowd['id'];?>"><?php echo $rowd['name'];?></option>
            <?php } ?>
            <?php } ?> 
            </select>
        </div>
    </div>     
</div>   
<div class="form-group">
    <label>Note</label> 
    <textarea class="form-control" name="note" id="note" maxlength="250" rows="2" placeholder="e.g. Note"><?php echo $adm['note'];?></textarea>    
</div> 
</div>    
<div class="col-md-1"></div>    
</div>    
</div>    

</div>
<div class="clearfix" ></div>
<div class="col-md-12 nopadding widgets_area"></div>    
<div class="row"style="margin-top: 15px" >
<div class="col-md-8"></div>
<div class="col-md-4 text-right" >
<input type="button" id="additem" class="btn btn-flat bg-purple btn-sm " value="Update"/>
</div> 
</div>
<script type="text/javascript">
$(".select2").select2();  
function chek_error(){
var chbnkid = $('#chbnkid').val();
$('.LV_validation_message').remove();
    
    
if(chbnkid == '-Select-' || chbnkid == ''){
$('#chbnkid').addClass('LV_invalid_field');   
$('#chbnkid').after("<span class='LV_validation_message LV_invalid'>Can't be empty!</span>").addClass('has-error'); } else {
$('#chbnkid').removeClass('LV_invalid_field');     
}

if(chbnkid == ''){
return false;    
}else{
return true;     
}    
}  
   
    
$(document).on('blur', '#chbnkid', function() {
chek_error();    
});    
</script> 

<?php }
if(isset($_POST['editcheck'])){
   
$chkid = remove_junk(escape($_POST['chkid']));;
$chbnkid = remove_junk(escape($_POST['chbnkid']));
$status = remove_junk(escape($_POST['status']));  
$chkdt = remove_junk(escape($_POST['chkdt']));  
$chkno = remove_junk(escape($_POST['chkno']));   
$note = remove_junk(escape($_POST['note'])); 
$amount = remove_junk(escape($_POST['amount']));
$refinv = remove_junk(escape($_POST['refinv']));

$second=filter_var($refinv, FILTER_SANITIZE_STRING);
$type = preg_replace("/[^A-Za-z\-]/",'', $second);

if($status==3 || $status==4){
    
    if($type=='SEL'){
       
        $sqls="UPDATE tbl_sales SET rawcash=$amount WHERE invno='$refinv'";
        $results = mysqli_query($con,$sqls) or die(mysqli_error($con));  

    }elseif($type=='REV'){
        $sqls="UPDATE tbl_trarecord SET amo=$amount WHERE invno='$refinv' AND chkno='$chkno'";
        $results = mysqli_query($con,$sqls) or die(mysqli_error($con));
    }elseif($type=='PUR'){
        $sqls="UPDATE tbl_purchase SET rawcash=$amount WHERE invno='$refinv'";
        $results = mysqli_query($con,$sqls) or die(mysqli_error($con));
       
    }elseif($type=='PAV'){
        $sqls="UPDATE tbl_trarecord SET amo=$amount WHERE invno='$refinv' AND chkno='$chkno'";
        $results = mysqli_query($con,$sqls) or die(mysqli_error($con));
      
    }elseif($type=='SER'){
        $sqls="UPDATE tbl_sersales SET rawcash=$amount WHERE invno='$refinv' ";
        $results = mysqli_query($con,$sqls) or die(mysqli_error($con));
      
    }elseif($type=='SRE'){
        $sqls="UPDATE tbl_sreturn SET rawcash=$amount WHERE invno='$refinv'";
        $results = mysqli_query($con,$sqls) or die(mysqli_error($con));
      
    }elseif($type=='PRE'){
        $sqls="UPDATE tbl_preturn SET rawcash=$amount WHERE invno='$refinv'";
        $results = mysqli_query($con,$sqls) or die(mysqli_error($con));
      
    }
}

$sql="UPDATE tbl_cheqman SET chbnkid='$chbnkid',status=$status,note='$note' WHERE id='$chkid'";
$result = mysqli_query($con,$sql) or die(mysqli_error($con));
$pid=$con->insert_id;    
$efid=mysqli_affected_rows($con);    
if($efid>0){    
$act =remove_junk(escape('Cheque Details: '.$chkno.'||'.$chkdt));    
write_activity($aid,'CHK','Cheque Entry has been Updated',$act);    
echo json_encode(array(
'status' => 'success',
'message'=> 'Data Update Successfully!!'   
));
exit;
}else{
echo json_encode(array(
'status' => 'error',
'message'=> 'Sorry! Data Fail to Update!!'
));    
}   
}


?> 

