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

if($_REQUEST['action'] == 'Save'){
$Name = addslashes(trim($_REQUEST['ChargesName']));
$Days = addslashes(trim($_REQUEST['Days']));
$Rate = addslashes(trim($_REQUEST['Rate']));
$Amount = addslashes(trim($_REQUEST['Amount']));
$PackageName = addslashes(trim($_REQUEST['PackageName']));
$PackageAmt = addslashes(trim($_REQUEST['PackageAmt']));
$BalanceAmt = addslashes(trim($_REQUEST['BalanceAmt']));
$CreatedDate = date('Y-m-d');
$sql = "SELECT * FROM tbl_temp_bill_charges WHERE PackageName='$PackageName' AND Name='$Name' AND SessionId='$SessionId'";
$rncnt = getRow($sql);
if($rncnt > 0){
  echo 0;
}
else{
   $sql = "INSERT INTO tbl_temp_bill_charges SET SessionId='$SessionId',PackageName='$PackageName',PackageAmt='$PackageAmt',BalanceAmt='$BalanceAmt',Name='$Name',Amount='$Amount',Days='$Days',Rate='$Rate',CreatedBy='$user_id'";
  $conn->query($sql);
  echo 1;
}
}

if($_POST['action'] == 'getBillPackageDetails') {
    $PackageName = addslashes(trim($_REQUEST['PackageName']));

    $query2 = "DELETE FROM tbl_temp_bill_charges WHERE SessionId = '$SessionId'";
    $conn->query($query2);

    $query = "SELECT * FROM tbl_charges WHERE PackageName = '$PackageName'";
    $row = getRecord($query);
    $row2 = getList($query);
    foreach($row2 as $result){
      $sql = "INSERT INTO tbl_temp_bill_charges SET SessionId='$SessionId',PackageName='$PackageName',PackageAmt='".$result['PackageAmt']."',BalanceAmt='".$result['BalanceAmt']."',Name='".$result['Name']."',Amount='".$result['Amount']."',Days='".$result['Days']."',Rate='".$result['Rate']."',Status=1,CreatedBy='$user_id'";
      $conn->query($sql);
    }
    echo json_encode($row);

   }

    if($_POST['action'] == 'calBalanceAmt') {
    $PackageName = addslashes(trim($_REQUEST['PackageName']));
    $PackageAmt = addslashes(trim($_REQUEST['PackageAmt']));
    $query = "SELECT SUM(Amount) As Amount,PackageAmt FROM tbl_temp_bill_charges WHERE PackageName = '$PackageName' AND SessionId='$SessionId'";
    $row = getRecord($query);
    //$PackageAmt = $row['PackageAmt'];
    $TotAmt = $row['Amount'];
    $BalanceAmt = $PackageAmt - $TotAmt;
    echo $BalanceAmt;

   }

   if($_POST['action'] == 'fetch_record'){
 $id = $_POST['id'];
    $query = "SELECT * FROM tbl_temp_bill_charges WHERE Name = '$id'";
    $result = $conn->query($query);
    $row = $result->fetch_assoc();
    echo json_encode($row);
}

if($_POST['action'] == 'Edit') {
     $id = $_POST['id'];
$Name = addslashes(trim($_REQUEST['ChargesName']));
$Days = addslashes(trim($_REQUEST['Days']));
$Rate = addslashes(trim($_REQUEST['Rate']));
$Amount = addslashes(trim($_REQUEST['Amount']));
$PackageName = addslashes(trim($_REQUEST['PackageName']));
$PackageAmt = addslashes(trim($_REQUEST['PackageAmt']));
$BalanceAmt = addslashes(trim($_REQUEST['BalanceAmt']));
$ModifiedDate = date('Y-m-d');
$query = "SELECT * FROM tbl_temp_bill_charges WHERE Name='$Name' AND Name != '$id'";
$result = $conn->query($query);
$row_cnt = mysqli_num_rows($result);
if($row_cnt > 0){
  echo 0;
}
else{
  $query2 = "UPDATE tbl_temp_bill_charges SET PackageName='$PackageName',PackageAmt='$PackageAmt',BalanceAmt='$BalanceAmt',Name='$Name',Amount='$Amount',Days='$Days',Rate='$Rate' WHERE Name = '$id' AND SessionId='$SessionId'";
  $conn->query($query2);
  echo 1;
}
}

  if($_POST['action'] == 'delete') {
   
      $id = $_POST['id'];
      $query = "DELETE FROM tbl_temp_bill_charges WHERE Name = '$id' AND SessionId='$SessionId'";
      $conn->query($query);
      echo "Delete Successfully";

  }

   if($_POST['action']=='billChargesView'){?>
<table id="example" class="table table-striped table-bordered dt-responsive nowrap" style="width:100%">
        <thead>
            <tr>
             <th>Sr.No</th>
             <th>Charges Name</th>
              <th>Days / Unit</th>
             <th>Rate</th>
             <th>Amount</th>
             <th>Action</th>
            </tr>
        </thead>
        <tbody>
          <?php 
 $srno = 1;
 $PackageName = addslashes(trim($_REQUEST['PackageName']));
  $sql = "SELECT DISTINCT(Name) As Name,Amount,Days,Rate FROM tbl_temp_bill_charges WHERE PackageName='$PackageName' AND SessionId='$SessionId'";
   $rx = $conn->query($sql);
  while($nx = $rx->fetch_assoc()){

  ?>
           <tr>
             <td><?php echo $srno; ?></td>
           
              <td><?php echo $nx['Name']; ?></td>
              <td><?php echo $nx['Days']; ?></td>
              <td><?php echo $nx['Rate']; ?></td>
        
             <td>&#8377;<?php echo number_format($nx['Amount'],2); ?></td>
             <td><a data-id="<?php echo $nx['Name']; ?>" 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['Name']; ?>" 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>
            </tr>
             <?php $srno++;} ?>
        </tbody>
    </table>
    <script type="text/javascript">
      $(document).ready(function() {
      $('#example').DataTable( {
        responsive: true
      });
      });
    </script>
 <?php } 

   if($_POST['action'] == 'totSumBills'){
    $query = "SELECT SUM(Amount) As Amount FROM tbl_temp_bill_charges WHERE SessionId = '$SessionId'";
    $row = getRecord($query);
    echo $row['Amount'];
}

if($_POST['action'] == 'balBillAmt'){
    $query = "SELECT SUM(Amount) As Amount,PackageAmt FROM tbl_temp_bill_charges WHERE SessionId = '$SessionId'";
    $row = getRecord($query);
    $balBillAmt = $row['PackageAmt'] - $row['Amount'];
    echo $balBillAmt;
}

if($_POST['saveaction'] == 'savebills'){
  $BillNo = addslashes(trim($_REQUEST['BillNo']));
  $MrdNo = addslashes(trim($_REQUEST['MrdNo']));
  $IpdNo = addslashes(trim($_REQUEST['IpdNo']));
  $PatientName = addslashes(trim($_REQUEST['PatientName']));
  $AgeGender = addslashes(trim($_REQUEST['AgeGender']));
  $BillDate = addslashes(trim($_REQUEST['BillDate']));
  $AdmissionDate = addslashes(trim($_REQUEST['AdmissionDate']));
  $DischargeDate = addslashes(trim($_REQUEST['DischargeDate']));
  $Consultant = addslashes(trim($_REQUEST['Consultant']));
  $Diagnosis = addslashes(trim($_REQUEST['Diagnosis']));
  $SurgeryDone = addslashes(trim($_REQUEST['SurgeryDone']));
  $InsuranceComp = addslashes(trim($_REQUEST['InsuranceComp']));
  $ClainNo = addslashes(trim($_REQUEST['ClainNo']));
  $PackageName = addslashes(trim($_REQUEST['PackageName']));
  $PackageAmt = addslashes(trim($_REQUEST['PackageAmt']));
  $BalanceAmt = addslashes(trim($_REQUEST['BalanceAmt']));
  $SubTotal = addslashes(trim($_REQUEST['SubTotal']));
  $Discount = addslashes(trim($_REQUEST['Discount']));
  $GrossAmt = addslashes(trim($_REQUEST['GrossAmt']));
  $BalAmt = addslashes(trim($_REQUEST['BalAmt']));
  $PaidAmt = addslashes(trim($_REQUEST['PaidAmt']));
  $PayMode = addslashes(trim($_REQUEST['PayMode']));
  $Narration = addslashes(trim($_REQUEST['Narration']));
  $CreatedDate = date('Y-m-d');

$sql = "INSERT INTO tbl_bills SET BillNo='$BillNo',MrdNo='$MrdNo',IpdNo='$IpdNo',PatientName='$PatientName',AgeGender='$AgeGender',BillDate='$BillDate',AdmissionDate='$AdmissionDate',DischargeDate='$DischargeDate',Consultant='$Consultant',Diagnosis='$Diagnosis',SurgeryDone='$SurgeryDone',InsuranceComp='$InsuranceComp',ClainNo='$ClainNo',PackageName='$PackageName',PackageAmt='$PackageAmt',BalanceAmt='$BalanceAmt',SubTotal='$SubTotal',Discount='$Discount',GrossAmt='$GrossAmt',BalAmt='$BalAmt',PaidAmt='$PaidAmt',PayMode='$PayMode',Narration='$Narration',CreatedBy='$user_id',CreatedDate='$CreatedDate'";
$conn->query($sql);
$BillId = mysqli_insert_id($conn);

$sql2 = "SELECT * FROM tbl_temp_bill_charges WHERE SessionId='$SessionId'";
$row2 = getList($sql2);
foreach($row2 as $result){
  $PackageName = addslashes(trim($result['PackageName']));
  $PackageAmt = addslashes(trim($result['PackageAmt']));
  $BalanceAmt = addslashes(trim($result['BalanceAmt']));
  $Name = addslashes(trim($result['Name']));
  $Days = addslashes(trim($result['Days']));
  $Rate = addslashes(trim($result['Rate']));
  $Amount = addslashes(trim($result['Amount']));

  $sql3 = "INSERT INTO tbl_bill_charges SET BillNo='$BillId',MrdNo='$MrdNo',IpdNo='$IpdNo',PackageName='$PackageName',PackageAmt='$PackageAmt',BalanceAmt='$BalanceAmt',Name='$Name',Amount='$Amount',Days='$Days',Rate='$Rate',Status=1,CreatedBy='$user_id',CreatedDate='$CreatedDate'";
  $conn->query($sql3);
}

$sql = "DELETE FROM tbl_temp_bill_charges WHERE SessionId='$SessionId'";
$conn->query($sql);
?>  
<script>
  window.location.href="../bill.php";
    setTimeout(function () {
    window.open(
        '../ipd-bill-print.php?id=<?php echo $BillId;?>', 'name', 'toolbar=1, scrollbars=1, location=1,statusbar=0, menubar=1, resizable=1, width=800, height=600'
    );
}, 1);
</script>
  <?php
//echo "<script>alert('Bill Generated Successfully!');window.location.href='../bill.php';</script>";
  }
?>