<?php 
session_start();
include_once '../config.php';
$user_id = $_SESSION['Admin']['id'];
if($_POST['action'] == 'getItemDetails'){
$Name = addslashes(trim($_REQUEST['id']));
$sql = "SELECT * FROM tbl_item_masters WHERE Name='$Name'";
$row = getRecord($sql);
echo json_encode($row);
}

if($_POST['action'] == 'addItem'){
	$MrdNo = addslashes(trim($_REQUEST['MrdNo']));
	$BillNo = addslashes(trim($_REQUEST['BillNo']));
	$FrameName = addslashes(trim($_REQUEST['FrameName']));
	$Qty = addslashes(trim($_REQUEST['Qty']));
	$FrameRate = addslashes(trim($_REQUEST['FrameRate']));
	$GlassName = addslashes(trim($_REQUEST['GlassName']));
	$GlassRate = addslashes(trim($_REQUEST['GlassRate']));
	$Amount = addslashes(trim($_REQUEST['Amount']));
	$CreatedDate = date('Y-m-d');
  $sql = "DELETE FROM tbl_temp_item_ledger WHERE BillNo='$BillNo' AND FrameName='$FrameName'";
  $conn->query($sql);

	$sql = "INSERT INTO tbl_temp_item_ledger SET BillNo='$BillNo',MrdNo='$MrdNo',FrameName='$FrameName',Qty='$Qty',Rate='$FrameRate',GlassName='$GlassName',GlassRate='$GlassRate',TotalAmount='$Amount',CrDr='Cr',Status=1,CreatedBy='$user_id',CreatedDate='$CreatedDate'";
	$conn->query($sql);
}

if($_POST['action'] == 'itemLists'){?>
<table>
  <tr>
    <th>Frame Name</th>
    <th>Qty</th>
    <th>Frame Rate</th>
    <th>Glass Name</th>
    <th>Glass Rate</th>
    <th>Amount</th>
    <th>Action</th>
  </tr>
  <?php 
  $MrdNo = addslashes(trim($_REQUEST['MrdNo']));
  $BillNo = addslashes(trim($_REQUEST['BillNo']));
  //$sql = "SELECT til.*,tim.Name As Frame_Name FROM tbl_temp_item_ledger til LEFT JOIN tbl_item_masters tim ON tim.id=til.FrameName WHERE til.BillNo='$BillNo' AND til.MrdNo='$MrdNo'";
  $sql = "SELECT til.* FROM tbl_temp_item_ledger til WHERE til.BillNo='$BillNo' AND til.MrdNo='$MrdNo'";
  $row = getList($sql);
  foreach($row as $result){
  ?>
  <tr>
    <td><?php echo $result['FrameName']; ?></td>
    <td><?php echo $result['Qty']; ?></td>
    <td>&#8377;<?php echo number_format($result['Rate'],2); ?></td>
    <td><?php echo $result['GlassName']; ?></td>
    <td>&#8377;<?php echo number_format($result['GlassRate'],2); ?></td>
    <td>&#8377;<?php echo number_format($result['TotalAmount'],2); ?></td>
    <input type="hidden" class="txt2" value="<?php echo $result['TotalAmount'];?>">
    <td><a data-id="<?php echo $result['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 } ?>
</table>  
<?php }	

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

if($_POST['action'] == 'getSubTotal') {
	$MrdNo = addslashes(trim($_REQUEST['MrdNo']));
  	$BillNo = addslashes(trim($_REQUEST['BillNo']));
    $query = "SELECT SUM(TotalAmount) As TotalAmount FROM tbl_temp_item_ledger WHERE BillNo='$BillNo' AND MrdNo='$MrdNo'";
  	$row = getRecord($query);
    echo $row['TotalAmount'];
}
?>	