<?php 
session_start();
include_once '../config.php';
$user_id = $_SESSION['Admin']['id'];
$CurrDate = date('dmY');

if($_REQUEST['action'] == 'Save'){
$PatientId = $_REQUEST['PatientId'];
$MrdNo = addslashes(trim($_REQUEST['MrdNo']));
$PatientName = addslashes(trim($_REQUEST['PatientName']));
//$ConsultantId = addslashes(trim($_REQUEST['ConsultantId']));
$ConsultantId = 16;
$Amount = addslashes(trim($_REQUEST['Amount']));
//$Amount = addslashes(trim($_REQUEST['Amount']));
$PayMode = addslashes(trim($_REQUEST['PayMode']));
$ChequeNo = addslashes(trim($_REQUEST['ChequeNo']));
$Narration = addslashes(trim($_REQUEST['Narration']));
$CreatedDate = date('Y-m-d');
$Status = 1;
$ModifiedBy = 0;
$ModifiedDate = null;
if($_REQUEST['ProcedureId'] != ''){
$Procedure = implode(",",$_REQUEST['ProcedureId']);
}
$number = count($_REQUEST['ProcedureChk']);

$sql4 = "SELECT * FROM tbl_users WHERE id='$ConsultantId'";
$row4 = getRecord($sql4);
$ConsultantName = $row4['Name'];

if($number > 0){
$sql4_1 = "SELECT SUM(Amount) As Amount FROM tbl_procedures WHERE id IN($Procedure)";
$row4_1 = getRecord($sql4_1);
$ProcAmount = $row4_1['Amount'];
}
 /*$sql = "INSERT INTO tbl_opd_receipts SET PatientId='$PatientId',MrdNo='$MrdNo',PatientName='$PatientName',ConsultantId='$ConsultantId',Consultant='$ConsultantName',Procedures='$Procedure',Amount='$ProcAmount',PayMode='$PayMode',ChequeNo='$ChequeNo',Narration='$Narration',CreatedBy='$CreatedBy',CreatedDate='$CreatedDate'";
	$conn->query($sql);
$postid = mysqli_insert_id($conn);*/

  try {
   $sql = "INSERT INTO tbl_opd_receipts (PatientId,MrdNo,PatientName,ConsultantId,Consultant,Procedures,Amount,PayMode,ChequeNo,Narration,Status,CreatedBy,CreatedDate,ModifiedBy,ModifiedDate) VALUES (:PatientId,:MrdNo,:PatientName,:ConsultantId,:Consultant,:Procedures,:Amount,:PayMode,:ChequeNo,:Narration,:Status,:CreatedBy,:CreatedDate,:ModifiedBy,:ModifiedDate)";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':PatientId', $PatientId);
$stmt->bindParam(':MrdNo', $MrdNo);
$stmt->bindParam(':PatientName', $PatientName);
$stmt->bindParam(':ConsultantId', $ConsultantId);
$stmt->bindParam(':Consultant', $ConsultantName);
$stmt->bindParam(':Procedures', $Procedure);
$stmt->bindParam(':Amount', $ProcAmount);
$stmt->bindParam(':PayMode', $PayMode);
$stmt->bindParam(':ChequeNo', $ChequeNo);
$stmt->bindParam(':Narration', $Narration);
$stmt->bindParam(':Status', $Status);
$stmt->bindParam(':CreatedBy', $user_id);
$stmt->bindParam(':CreatedDate', $CreatedDate);
$stmt->bindParam(':ModifiedBy', $ModifiedBy);
$stmt->bindParam(':ModifiedDate', $ModifiedDate);
if ($stmt->execute()) {
  $postid = $conn->lastInsertId();
$currDate = date('Y-m-d');
$sql2 = "SELECT * FROM tbl_opd_receipts WHERE CreatedDate='$currDate'";
$rncnt2 = getRow($sql2);  
  echo json_encode(array('status'=>1,'count'=>$rncnt2,'id'=>$postid));
}
}
catch (PDOException $e) {
   echo 'Connection failed: ' . $e->getMessage();
}


}

if($_POST['action'] == 'fetch_record'){
 $id = $_POST['id'];
    $query = "SELECT * FROM tbl_opd_receipts WHERE id = '$id'";
    $row = getRecord($query);
    echo json_encode($row);



}

if($_POST['action'] == 'Edit') {
     $id = $_POST['id'];
$PatientId = $_REQUEST['PatientId'];
$MrdNo = addslashes(trim($_REQUEST['MrdNo']));
$PatientName = addslashes(trim($_REQUEST['PatientName']));
$ConsultantId = addslashes(trim($_REQUEST['ConsultantId']));
$Amount = addslashes(trim($_REQUEST['Amount']));
//$Procedure = addslashes(trim($_REQUEST['Procedure']));
$Amount = addslashes(trim($_REQUEST['Amount']));
$PayMode = addslashes(trim($_REQUEST['PayMode']));
$ChequeNo = addslashes(trim($_REQUEST['ChequeNo']));
$Narration = addslashes(trim($_REQUEST['Narration']));
$CreatedDate = date('Y-m-d');
if($_REQUEST['ProcedureId'] != ''){
$Procedure = implode(",",$_REQUEST['ProcedureId']);
}
$number = count($_REQUEST['ProcedureChk']);

$sql4 = "SELECT * FROM tbl_users WHERE id='$ConsultantId'";
$row4 = getRecord($sql4);
$ConsultantName = $row4['Name'];

if($number > 0){
$sql4_1 = "SELECT SUM(Amount) As Amount FROM tbl_procedures WHERE id IN($Procedure)";
$row4_1 = getRecord($sql4_1);
$ProcAmount = $row4_1['Amount'];
}
try{
  $query2 = "UPDATE tbl_opd_receipts SET PatientId='$PatientId',MrdNo='$MrdNo',PatientName='$PatientName',ConsultantId='$ConsultantId',Consultant='$ConsultantName',Procedures='$Procedure',Amount='$ProcAmount',PayMode='$PayMode',ChequeNo='$ChequeNo',Narration='$Narration',ModifiedDate='$ModifiedDate',ModifiedBy='$user_id' WHERE id = '$id'";
 	$conn->query($query2);
$postid = $id;
$currDate = date('Y-m-d');
$sql2 = "SELECT * FROM tbl_opd_receipts WHERE CreatedDate='$currDate'";
$rncnt2 = getRow($sql2);  
  echo json_encode(array('status'=>1,'count'=>$rncnt2,'id'=>$postid));
}
catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

}

  if($_POST['action'] == 'delete') {
   
      $id = $_POST['id'];
      $query = "DELETE FROM tbl_opd_receipts WHERE id = '$id'";
      $conn->query($query);

      $currDate = date('Y-m-d');
      $sql2 = "SELECT * FROM tbl_opd_receipts WHERE CreatedDate='$currDate'";
      $rncnt2 = getRow($sql2); 
      echo json_encode(array('status'=>'Delete Successfully','count'=>$rncnt2)); 

  }

 if($_POST['action']=='view'){?>
<table id="example" class="table-striped table-bordered">
        <thead>
            <tr>
              <th>Sr.No</th>
              <th>Receipt No</th>
              <th>Mrd. No</th>
              <th>Name</th>
              <th>Procedure</th>
             <!--  <th>Department</th> -->
              <th>Amount</th>
              <th>Payment Mode</th>
             <th>Action</th>
             <th>Print</th>
            </tr>
        </thead>
        <tbody>
          <?php 
 $srno = 1;
  $sql = "SELECT * FROM tbl_opd_receipts ORDER BY id DESC";
 $rx = getList($sql);
  foreach($rx as $nx){
    $Procedures = $nx['Procedures'];
       $sql2 = "SELECT GROUP_CONCAT(Name) As ProcedureName FROM tbl_procedures WHERE id IN($Procedures)";
      $row2 = getRecord($sql2);
  ?>
           <tr>
             <td><?php echo $srno; ?></td>
            <td><?php echo $nx['id']; ?></td>
             <td><?php echo $nx['MrdNo']; ?></td>
             <td><?php echo $nx['PatientName']; ?></td>
             <td><?php echo $row2['ProcedureName']; ?></td>
             <!-- <td><?php echo $nx['Consultant']; ?></td> -->
        
             <td>&#8377;<?php echo number_format($nx['Amount'],2); ?></td>
             <td><?php echo $nx['PayMode']; ?></td>
             <td><a data-id="<?php echo $nx['id']; ?>" href='javascript:void(0);' data-toggle="tooltip" data-placement="top" title="Edit" data-original-title="Edit" class="update"><i class="lnr lnr-pencil mr-2"></i></a>
             <!--&nbsp;&nbsp;<a data-id="<?php echo $nx['id']; ?>" href='javascript:void(0);' data-toggle="tooltip" data-placement="top" title="Delete" data-original-title="Delete" class="delete" id="bootbox-confirm"><i class="lnr lnr-trash text-danger"></i></a>-->
             </td>
             <td><a href='opd-receipt-print.php?id=<?php echo $nx['id']; ?>' class="btn btn-info btn-sm" target="_new"><i class="feather icon-printer"></i>&nbsp;Print </a></td>
            </tr>
             <?php $srno++;} ?>
        </tbody>
    </table>
    <script type="text/javascript">
      $(document).ready(function() {
      $('#example').DataTable( {
        responsive: true,
        "pageLength": 1000
      });
      });
    </script>
 <?php }

if($_POST['action']=='viewProcRec'){?>
<table id="example2" class="table table-striped table-bordered" style="width:100%">
        <thead>
            <tr>
             <th>#</th>
             <th>Procedure Name</th>
             <th>Fees</th>
            
            </tr>
        </thead>
        <tbody>
          <?php 
 $srno = 1;
  $sql = "SELECT * FROM tbl_procedures ORDER BY id DESC";
$rx = getList($sql);
  foreach($rx as $nx){

  ?>
           <tr>
             <td><input type="checkbox" name="ProcedureChk[]" value="<?php echo $nx['id'];?>" class="common_selector Proccedures"></td>
              <input type="hidden" name="ProcId[]" value="<?php echo $nx['id'];?>">
              <td><?php echo $nx['Name']; ?></td>
        
             <td>&#8377;<?php echo number_format($nx['Amount'],2); ?></td>
            
            </tr>
             <?php $srno++;} ?>
        </tbody>
    </table>
    <input type="hidden" id="ProcId" name="ProcedureId[]" value="">
    <script type="text/javascript">
      $(document).ready(function() {
      $('#example2').DataTable( {
        responsive: true,
        "pageLength": 1000
      });

       function get_filter(class_name)
    {
        var filter = [];
        $('.'+class_name+':checked').each(function(){
            filter.push($(this).val());
        });
        return filter;
    }

    $('.common_selector').click(function(){
        //filter_data();
        var ProccedureId = get_filter('Proccedures');
         if(ProccedureId == ''){
              $('#ProcId').val(0);
         }
         else{
        $('#ProcId').val(ProccedureId);
         }
        
    });
      });
    </script>
 <?php }

 if($_POST['action']=='viewProcRec2'){?>
<table id="example2" class="table table-striped table-bordered" style="width:100%">
        <thead>
            <tr>
             <th>#</th>
             <th>Procedure Name</th>
             <th>Fees</th>
            
            </tr>
        </thead>
        <tbody>
          <?php 
 $srno = 1;
 $id = $_REQUEST['id'];
 $sql2 = "SELECT Procedures FROM tbl_opd_receipts WHERE id='$id'";
 $row2 = getRecord($sql2);
 $Procedures = $row2['Procedures'];

  //$sql = "SELECT * FROM tbl_procedures WHERE id NOT IN($Procedures) ORDER BY id DESC";
 $sql = "SELECT * FROM tbl_procedures ORDER BY id DESC";
  $rx = getList($sql);
  foreach($rx as $nx){

  ?>
           <tr>
             <td><input type="checkbox" name="ProcedureChk[]" value="<?php echo $nx['id'];?>" class="common_selector Proccedures"></td>
              <input type="hidden" name="ProcId[]" value="<?php echo $nx['id'];?>">
              <td><?php echo $nx['Name']; ?></td>
        
             <td>&#8377;<?php echo number_format($nx['Amount'],2); ?></td>
            
            </tr>
             <?php $srno++;} 

             $sql = "SELECT * FROM tbl_procedures WHERE id IN($Procedures) ORDER BY id DESC";
   $rx = $conn->query($sql);
  while($nx = $rx->fetch_assoc()){

  ?>
           <tr>
             <td><input type="checkbox" name="ProcedureChk[]" value="<?php echo $nx['id'];?>" class="common_selector Proccedures" checked></td>
              <input type="hidden" name="ProcId[]" value="<?php echo $nx['id'];?>">
              <td><?php echo $nx['Name']; ?></td>
        
             <td>&#8377;<?php echo number_format($nx['Amount'],2); ?></td>
            
            </tr>
             <?php $srno++;} ?>
        </tbody>
    </table>
    <input type="hidden" id="ProcId" name="ProcedureId[]" value="<?php echo $Procedures;?>">
    <script type="text/javascript">
      $(document).ready(function() {
      $('#example2').DataTable( {
        responsive: true,
        "pageLength": 1000
      });

       function get_filter(class_name)
    {
        var filter = [];
        $('.'+class_name+':checked').each(function(){
            filter.push($(this).val());
        });
        return filter;
    }

    $('.common_selector').click(function(){
        //filter_data();
        var ProccedureId = get_filter('Proccedures');
         if(ProccedureId == ''){
              $('#ProcId').val(0);
         }
         else{
        $('#ProcId').val(ProccedureId);
         }
        
    });
    });

    </script>
 <?php }
?>