<?php
ini_set('display_errors', 0);
//error_reporting(E_ALL);
//ini_set('display_errors', 1);

session_start();
ob_start();

require_once('configuration/server_connect.php');
require_once('configuration/all_function.php');

$active_id=$_REQUEST['active_id'];
$menu_id=$_REQUEST['menu_id'];

// Common Information

$page_title="Account Sub Ledger";
$page="07_acc_sub_ledger.php?active_id=".$active_id."&menu_id=".$menu_id."";
$table='accounts_reference';	
$unique='ref_no';

$request_id=$_REQUEST['ref_no'];

$ms_data=mysqli_fetch_object(mysqli_query($conn,'select * from '.$table.' where ref_no="'.$request_id.'"'));

// End Common Information

if(isset($_POST['insert']))

{		

$_POST['entry_by'] = $_SESSION['user']['id'];
		 
$_POST['entry_at'] = $now=date('Y-m-d H:i:s');

$full_name = $_POST['full_name'];
$ref_type = $_POST['ref_type'];

$cy_id=mysqli_fetch_object(mysqli_query($conn,'select max(ref_id) as ref_id from '.$table.' where ref_type="'.$ref_type.'"'));
$rf_id=$cy_id->ref_id+1;
$ref_id = sprintf("%05d", $rf_id);

$ref_no = $ref_type.''.$ref_id;

$display_name=$full_name.' - [Code: '.$ref_no.']';

  $ins_sql = 'INSERT INTO accounts_reference (ref_no, ref_id, full_name, ref_type, entry_by, entry_at) VALUES

("'.$ref_no.'", "'.$ref_id.'", "'.$full_name.'", "'.$ref_type.'", "'.$_POST['entry_by'].'", "'.$_POST['entry_at'].'")';
	
mysqli_query($conn,$ins_sql);

?>

<script language="javascript">
window.location.href = "<?php echo $page;?>";
</script>

<?php

}

if(isset($_POST['update']))

{		

$_POST['entry_by'] = $_SESSION['user']['id'];
		 
$_POST['entry_at'] = $now=date('Y-m-d H:i:s');

$full_name = $_POST['full_name'];

$ref_no=$request_id;

$display_name=$full_name.' - [Code: '.$ref_no.']';

$up_sql = 'update accounts_reference set full_name="'.$full_name.'", display_name="'.$display_name.'", edit_by="'.$_POST['entry_by'].'", edit_at="'.$_POST['entry_at'].'"  where ref_no='.$ref_no;
mysqli_query($conn,$up_sql);


?>

<script language="javascript">
window.location.href = "<?php echo $page;?>";
</script>

<?php

}

?>


<!DOCTYPE html>
<html lang="en">

<!-- ======= <head> ======= -->
 <?php require_once('inc_files/inc_head.php'); ?>
<!-- ======= </head> ======= -->

<body>

  <!-- ======= Header ======= -->
  <?php require_once('inc_files/inc_header.php'); ?><!-- End Header -->

  <!-- ======= Sidebar ======= -->
   <?php  require_once('inc_files/inc_menu.php'); ?>
   <!-- End Sidebar-->

  <main id="main" class="main">

    <div class="pagetitle">
      <h1><?php echo $page_title;?></h1>
      <nav>
        <ol class="breadcrumb">
          <li class="breadcrumb-item"><a href="home.php">Home</a></li>
          <li class="breadcrumb-item">General Ledger</li>
          <li class="breadcrumb-item active"><?php echo $page_title;?></li>
        </ol>
      </nav>
    </div><!-- End Page Title -->
    <section class="section">
      <div class="row">
        <div class="col-lg-8"> 

          <div class="card">
            <div class="card-body">
              <h5 class="card-title"><?php echo $page_title;?></h5>
              

              <!-- Table with stripped rows -->
              <table class="table" id="data_table" style="font-size:14px;">
                <thead>
                  <tr>
                    <th  scope="col"><span class="card-title">Code</span></th>
                    <th  scope="col"><span class="card-title">Account Sub Ledger </span></th>
                    <th  scope="col"><span class="card-title">Sub Ledger Type</span>  </th>
                  </tr>
                </thead>
                <tbody>
					<?php
					
		 $sql = "select id, reference_type from reference_type where 1 group by id";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $reference_type[$info->id]=$info->reference_type;
	
		}
					
					
					
						$sql = 'select * from '.$table.' where 1';
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {

					?>
				
                 <tr>
                  <td><a href="<?php echo $page;?>&<?php echo $unique;?>=<?php echo $data->ref_no;?>" style="color:#000000"><?php echo $data->ref_no;?></a></td>
                  <td><a href="<?php echo $page;?>&<?php echo $unique;?>=<?php echo $data->ref_no;?>" style="color:#000000"><?php echo $data->full_name;?></a></td>
                  <td><a href="<?php echo $page;?>&<?php echo $unique;?>=<?php echo $data->ref_no;?>" style="color:#000000"><?php echo $reference_type[$data->ref_type]?></a></td>
                  </tr>
				  <?php }?>
                </tbody>
              </table>
              <!-- End Table with stripped rows -->

            </div>
          </div>

        </div>

        <div class="col-lg-4">

          <div class="card">
            <div class="card-body">
              <h5 class="card-title"><?php echo $page_title;?></h5>

              <!-- Multi Columns Form -->
              <form class="row g-3" action="" method="post" name="form1" id="form1" onSubmit="return check()">
                <div class="col-md-12">
                  <label for="inputName5" class="form-label">Account Sub Ledger</label>
                   <input type="text" class="form-control" id="full_name" name="full_name" value="<?php if($request_id>0){ echo $ms_data->full_name; } ?>" required>
                </div>
				
				<div class="col-12">
                  <label for="inputAddress2" class="form-label">Sub Ledger Type</label>
                 
			<?php if($request_id>0){?>  
                    
			<select id="ref_type" name="ref_type"  class="form-select"  required  >
			
			<?php foreign_relation($conn,'reference_type','id','reference_type',$_POST['ref_type'],'id="'.$ms_data->ref_type.'" order by reference_type');?>
		
        	</select>
			
			<?php } else {?>
			
			<select id="ref_type" name="ref_type"  class="form-select"  required  >
			
			<option></option>
			
			<?php foreign_relation($conn,'reference_type','id','reference_type',$_POST['ref_type'],'1 order by reference_type');?>
		
        	</select>
			<?php }?>	
			
			
                </div>
				
				
                
            
                
                
                <div class="text-center">
				<?php if($request_id>0) {?>
                 <button type="submit" class="btn btn-success btn-sm" name="update" id="update" ><i class="bi bi-check-all"></i> Update &nbsp;</button>
				 <?php }else {?>
				 <button type="submit" class="btn btn-primary btn-sm" name="insert" id="insert" ><i class="bi bi-plus"></i> Save &amp; New &nbsp;</button>
				 <?php }?>
                  <button type="reset" class="btn btn-secondary btn-sm" onClick="parent.location='<?php echo $page?>'" >Reset</button>
                </div>
              </form><!-- End Multi Columns Form -->

            </div>
          </div>

          

          

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

  </main><!-- End #main -->

<!-- ======= Footer ======= -->
 <?php require_once('inc_files/inc_footer.php'); ?>
<!-- ======= Footer ======= -->

<script>
var printCounter = 0;
 
new DataTable('#data_table', {
    caption: "&copy; Copyright <?php echo $grp_data->group_name?> All Rights Reserved",
    layout: {
        topStart: {
            buttons: [
                'copy',
                {
                    extend: 'excel',
messageTop: 'For the period from <?php echo date('d-M-Y',strtotime($_REQUEST['fdate']));?> To <?php echo date('d-M-Y',strtotime($_REQUEST['tdate']));?>',
                },
                {
                    extend: 'pdf',
					
messageTop: 'For the period from <?php echo date('d-M-Y',strtotime($_REQUEST['fdate']));?> To <?php echo date('d-M-Y',strtotime($_REQUEST['tdate']));?>',			                    
                    //messageBottom: null
					messageBottom: "Copyright <?php echo $grp_data->group_name?> All Rights Reserved"
                },
                {
                    extend: 'print',
									
messageTop: 'For the period from <?php echo "<strong>" ?><?php echo date('d-M-Y',strtotime($_REQUEST['fdate']));?> To <?php echo date('d-M-Y',strtotime($_REQUEST['tdate']));?><?php echo "</strong>" ?>',
                    //messageTop: function () {
//                        printCounter++;
// 
//                        if (printCounter === 1) {
//					
//                            return 'This is the first time you have printed this document. ttt';
//                        }
//                        else {
//                            return (
//                                'You have printed this document ' + printCounter + ' times'
//                            );
//                        }
//                    },
                    messageBottom: "&copy; Copyright <?php echo $grp_data->group_name?> All Rights Reserved"
                }
            ]
        }
    }
});
</script>



<script type="text/javascript">
$(document).ready(function() {
    // Check if DataTable is already initialized
    if ($.fn.DataTable.isDataTable('#data_table')) {
        // If DataTable is already initialized, destroy it
        $('#data_table').DataTable().destroy();
    }
    
    // Reinitialize DataTable with new options
    $('#data_table').dataTable({
        'iDisplayLength': 25
    });
});
</script>

</body>

</html>