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

if($_REQUEST['action'] == 'Save'){
$Code = addslashes(trim($_REQUEST['Code']));
$ItemType = addslashes(trim($_REQUEST['ItemType']));
$Name = addslashes(trim($_REQUEST['Name']));
$Details = addslashes(trim($_REQUEST['Details']));
$Qty = addslashes(trim($_REQUEST['Qty']));
$Rate = addslashes(trim($_REQUEST['Rate']));
$AddMinus = addslashes(trim($_REQUEST['AddMinus']));
$Status = addslashes(trim($_REQUEST['Status']));
$CreatedDate = date('Y-m-d');
/*$sql = "SELECT * FROM tbl_item_masters WHERE Code='$Code'";
$rncnt = getRow($sql);
if($rncnt > 0){
	echo 0;
}
else{*/
	$sql = "INSERT INTO tbl_item_masters SET Code='$Code',ItemType='$ItemType',Name='$Name',Details='$Details',Qty='$Qty',Rate='$Rate',AddMinus='$AddMinus',Status='$Status',CreatedBy='$user_id',CreatedDate='$CreatedDate'";
	$conn->query($sql);
  $PostId = mysqli_insert_id($conn);
  if($AddMinus == '+'){
  $sql2 = "INSERT INTO tbl_item_ledger SET PostId='$PostId',FrameName='$Name',Qty='$Qty',Rate='$Rate',CrDr='Dr',Status='1',CreatedBy='$user_id',CreatedDate='$CreatedDate',Narration='Stock Added',Type='Frame'";
  $conn->query($sql2);
  }
  else{
   $sql2 = "INSERT INTO tbl_item_ledger SET PostId='$PostId',FrameName='$Name',Qty='$Qty',Rate='$Rate',CrDr='Cr',Status='1',CreatedBy='$user_id',CreatedDate='$CreatedDate',Narration='Stock Added',Type='Frame'";
  $conn->query($sql2); 
  }
	echo 1;
/*}*/
}

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


}

if($_POST['action'] == 'Edit') {
     $id = $_POST['id'];
$Code = addslashes(trim($_REQUEST['Code']));
$ItemType = addslashes(trim($_REQUEST['ItemType']));
$Name = addslashes(trim($_REQUEST['Name']));
$Details = addslashes(trim($_REQUEST['Details']));
$Qty = addslashes(trim($_REQUEST['Qty']));
$Rate = addslashes(trim($_REQUEST['Rate']));
$AddMinus = addslashes(trim($_REQUEST['AddMinus']));
$Status = addslashes(trim($_REQUEST['Status']));
$ModifiedDate = date('Y-m-d');
/*$query = "SELECT * FROM tbl_item_masters WHERE Code='$Code' AND id != '$id'";
$result = $conn->query($query);
$row_cnt = mysqli_num_rows($result);
if($row_cnt > 0){
  echo 0;
}
else{*/
  $query2 = "UPDATE tbl_item_masters SET ItemType='$ItemType',Name='$Name',Details='$Details',Qty='$Qty',Rate='$Rate',AddMinus='$AddMinus',Status='$Status',ModifiedDate='$ModifiedDate',ModifiedBy='$user_id' WHERE id = '$id'";
 	$conn->query($query2);
  $PostId = $id;
  if($AddMinus == '+'){
  $sql2 = "UPDATE tbl_item_ledger SET Qty='$Qty',Rate='$Rate',CrDr='Dr',Status='1',CreatedBy='$user_id',CreatedDate='$CreatedDate',Narration='Stock Added' WHERE FrameName='$PostId'";
  $conn->query($sql2);
  }
  else{
   $sql2 = "UPDATE tbl_item_ledger SET Qty='$Qty',Rate='$Rate',CrDr='Cr',Status='1',CreatedBy='$user_id',CreatedDate='$CreatedDate',Narration='Stock Added' WHERE FrameName='$PostId'";
  $conn->query($sql2); 
  }
  echo 1;
/*}*/
}

  if($_POST['action'] == 'delete') {
   
      $id = $_POST['id'];
      $query = "DELETE FROM tbl_item_masters WHERE id = '$id'";
      $conn->query($query);
       $query2 = "DELETE FROM tbl_item_ledger WHERE PostId = '$id' AND Type='Frame'";
      $conn->query($query2);
      echo "Delete Successfully";

  }


  if($_POST['action'] == 'itemCode') {
     $sql = "SELECT Max(id) As MaxId FROM tbl_item_masters";
$row = getRecord($sql);
if($row['MaxId'] == ''){
    $Code = "PI1";
}
else{
    $MaxId = $row['MaxId']+1;
    $Code = "PI".$MaxId;
}
echo $Code;

  }

  if($_POST['action'] == 'getFrameDetails') {
    $Name = addslashes(trim($_REQUEST['Name']));
     $sql = "SELECT * FROM tbl_item_masters WHERE Name='$Name'";
    $row = getRecord($sql);
    echo json_encode($row);
  }


 if($_POST['action']=='view'){?>
<table id="example" class="table table-striped table-bordered dt-responsive nowrap" style="width:100%">
        <thead>
            <tr>
             <th>Code</th>
             <!-- <th>Item Type</th> -->
             <th>Item Name</th>
             <th>Qty</th>
             <th>Rate</th>
             <th>CrDr</th>
             <!-- <th>Action</th> -->
            </tr>
        </thead>
        <tbody>
          <?php 
 $srno = 1;
  $sql = "SELECT tim.*,tit.Name As Item_Type FROM tbl_item_masters tim LEFT JOIN tbl_item_type tit ON tim.ItemType=tit.id ORDER BY tim.id DESC";
   $rx = $conn->query($sql);
  while($nx = $rx->fetch_assoc()){

  ?>
           <tr>
             <td><?php echo $nx['Code']; ?></td>
           <!--  <td><?php echo $nx['Item_Type']; ?></td> -->
              <td><?php echo $nx['Name']; ?></td>
        <td><?php echo $nx['Qty']; ?></td>
             <td>&#8377;<?php echo number_format($nx['Rate'],2); ?></td>
             <td><?php if($nx['AddMinus']=='+'){echo "<span style='color:green;'>Credit</span>";} else { echo "<span style='color:red;'>Debit</span>";} ?></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>-->
            </tr>
             <?php $srno++;} ?>
        </tbody>
    </table>
    <script type="text/javascript">
      $(document).ready(function() {
      $('#example').DataTable( {
        responsive: true
      });
      });
    </script>
 <?php }
?>