<?php session_start(); ?> <!-- session start -->
<?php include("../include/config.php"); ?> <!-- database connection -->
<?php include("../include/connect_url.php"); ?> <!-- connection url/link folder -->
<?php include("../include/date_time.php"); ?> <!-- date & time format -->
<?php include("../database/account.php"); ?> <!-- query database account -->
<?php include("../include/lang.php"); ?> <!-- language -->
<?php include("../include/session_login.php"); ?> <!-- session login -->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
<title><?php echo $lang['Item & Service']; ?></title>

<link rel="shortcut icon" href="<?php echo $connect_url_data; ?>/favicon.ico" />

<link href="<?php echo $connect_url_data; ?>/css/styles.css" rel="stylesheet" type="text/css" />
<!--[if IE]> <link href="<?php echo $connect_url_data; ?>/css/ie.css" rel="stylesheet" type="text/css"> <![endif]-->
<link rel="stylesheet" href="<?php echo $connect_url_data; ?>/font/font-awesome-4.3.0/css/font-awesome.min.css">

<script type="text/javascript" src="<?php echo $connect_url_data; ?>/js/download/jquery183.js"></script> 
<script type="text/javascript" src="<?php echo $connect_url_data; ?>/js/download/jquery192.js"></script>
<script type="text/javascript" src="<?php echo $connect_url_data; ?>/js/plugins/charts/jquery.sparkline.min.js"></script>
<script type="text/javascript" src="<?php echo $connect_url_data; ?>/js/plugins/ui/jquery.collapsible.min.js"></script>
<script type="text/javascript" src="<?php echo $connect_url_data; ?>/js/plugins/ui/jquery.breadcrumbs.js"></script>
<script type="text/javascript" src="<?php echo $connect_url_data; ?>/js/plugins/ui/jquery.easytabs.min.js"></script>
<script type="text/javascript" src="<?php echo $connect_url_data; ?>/js/files/functions_blank_no_sidebar.js"></script>

<script type="text/javascript" src="<?php echo $connect_url_data ?>/datatable/js/jquery.dataTables.js"></script>
<script type="text/javascript" src="<?php echo $connect_url_data ?>/datatable/js/columnFilter.js"></script>

<script type="text/javascript" src="<?php echo $connect_url_data ?>/js/files/bootstrap.js"></script>

</head>

<body>

	
<?php
$query_transaction2 = mysql_query("SELECT * FROM tbl_transaction WHERE id='$_GET[transaction_id]'"); 
while($row= mysql_fetch_assoc($query_transaction2))
{
    $db_transaction2_id = $row['id'];
    $db_transaction2_transaction_no = $row['transaction_no'];
    $db_transaction2_patient_id = $row['patient_id'];
    $db_transaction2_pt_type_id = $row['pt_type_id'];
    $db_transaction2_remark = $row['remark'];
    $db_transaction2_user_id = $row['user_id'];
    $db_transaction2_date = $row['date'];
    $db_transaction2_time = $row['time'];
    $db_transaction2_a_status = $row['a_status'];
}
?>


<?php
if($_POST){
$item_id = $_POST['item_id'];
$qty = $_POST['qty'];

 
if($qty){
    
//1
$query_transaction_charge1 = mysql_query("SELECT * FROM tbl_transaction_charge WHERE transaction_id='$db_transaction2_id' AND user_id='$db_user_id' AND a_status='2'");
$numrows_transaction_charge1 = mysql_numrows($query_transaction_charge1);
while($row= mysql_fetch_assoc($query_transaction_charge1))
{
    $db_transaction_charge1_id =  $row['id'];
}

//2
$query_transaction_charge2 = mysql_query("SELECT * FROM tbl_transaction_charge");
while($row= mysql_fetch_assoc($query_transaction_charge2))
{
    $db_transaction_charge2_id =  $row['id'];
}

$db_transaction_charge2_id_add = $db_transaction_charge2_id + 1;
$db_transaction_charge2_id_add_comp = "$date_year$db_transaction_charge2_id_add";     


$query_item_price1 = mysql_query("SELECT * FROM tbl_item_price WHERE item_id='$item_id' AND a_status='1'");
while($row= mysql_fetch_assoc($query_item_price1))
{
    $db_item_price1_price =  $row['price'];
    $db_item_price1_price_nf =str_replace(' ','', str_replace(',00','', $db_item_price1_price));
}
  
$price_total = $qty * $db_item_price1_price_nf;
//number format
include("../include/number_format.php");



//exist transaction charge
if($numrows_transaction_charge1)
{
    
$query_transaction_charge_item1 = mysql_query("SELECT * FROM tbl_transaction_charge_item WHERE transaction_charge_id='$db_transaction_charge1_id' AND item_id='$item_id' AND a_status='1'");
$numrows_transaction_charge_item1 = mysql_numrows($query_transaction_charge_item1);
while($row= mysql_fetch_assoc($query_transaction_charge_item1))
{
    $db_transaction_charge_item1_qty =  $row['qty'];
    $db_transaction_charge_item1_qty_add = $db_transaction_charge_item1_qty + $qty;
}
$price_total2  = $db_transaction_charge_item1_qty_add * $db_item_price1_price_nf;
//number format
include("../include/number_format.php");




if($numrows_transaction_charge_item1){
$sql = "UPDATE tbl_transaction_charge_item SET qty='$db_transaction_charge_item1_qty_add', price='$db_item_price1_price', total='$price_total2_nf' WHERE transaction_charge_id='$db_transaction_charge1_id' AND item_id='$item_id' AND a_status='1'";
$result = mysql_query($sql) or die("Error: Failed to Update!");
}else{
$query_insert = mysql_query("
 INSERT INTO tbl_transaction_charge_item VALUES ('','$db_transaction_charge2_id','$item_id','$qty','$db_item_price1_price','$price_total_nf','$db_user_id','$date','$time','1')
"); 
}
    
    
    
}
//new 
else{
    
$query_insert = mysql_query("
 INSERT INTO tbl_transaction_charge VALUES ('','$db_transaction2_id','$db_transaction_charge2_id_add_comp','','$db_user_id','$date','$time','2')
");
$mysql_insert_id = mysql_insert_id();
$query_insert = mysql_query("
 INSERT INTO tbl_transaction_charge_item VALUES ('','$mysql_insert_id','$item_id','$qty','$db_item_price1_price','$price_total_nf','$db_user_id','$date','$time','1')
");
}








}
//end if qty





}
?>

<form   name="form" id="form" autocomplete="OFF" enctype="multipart/form-data" method="POST">

<script type="text/javascript" charset="utf-8">
function SubmitForm() {
<?php
$query_item = mysql_query("SELECT * FROM tbl_item");
while($row= mysql_fetch_assoc($query_item))
{
      $db_item_id =  $row['id'];
?>
    
var item_id = $("#item_id<?php echo $db_item_id; ?>").val();
var qty = $("#qty<?php echo $db_item_id; ?>").val();

if(qty){
$.post("<?php echo $connect_url_data; ?>/charge/item/<?php echo $db_transaction2_id; ?>", { item_id: item_id,  qty: qty },
   function(data) {
       $('.form-control').val('');
   });
}

<?php } ?>

}
</script>

        <fieldset style="margin-top:-35px;">
                <div class="widget fluid">
                    <div class="whead"><h6><span class="fa fa-dropbox" style="margin-top:-2px;"></span>&nbsp; <?php echo $lang['Item & Service']; ?></h6></div>
                    


<div style="position:relative;height:1px;left:-15px;float:right;top:20px;z-index:10;">
  <a class="buttonM bBlue" href="javascript:void(0);" onclick="oTable.fnReloadAjax();" style="border-radius:5px;"><span class="fa fa-refresh"></span><span>Refresh</span></a>
</div>

<script type="text/javascript" charset="utf-8">      
$(document).ready(function () {
  
  oTable =$('#item-datatable').dataTable(
  {
    
"fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
      
if ( aData[0]){$('td:eq(0)', nRow).html( '<center><i class="fa fa-dropbox"></i></center>');}

   
     },

"sPaginationType": "full_numbers",
"aaSorting": [[ 2, "asc" ]],
"sDom": '<"top"f>rt<"bottom"i><"bottom"p>',
"bAutoWidth": false,
"oLanguage": {
"sSearch": "<i class='fa fa-search' style='margin-left:20px;'></i>"
},

"sAjaxSource": "<?php echo $connect_url_data; ?>/charge/charge-item-datatable.php",
"aoColumns": [
    { "mData": null },
           null,
           null,
           null,
	   null,
	   null,
	   null,
   
           
         ]          
  });

$('.dataTables_filter input').attr("placeholder", "<?php echo $lang['Search Keywords']; ?>");

});
//end ready function

 $.fn.dataTableExt.oApi.fnReloadAjax = function ( oSettings, sNewSource, fnCallback, bStandingRedraw )
{
    if ( sNewSource !== undefined && sNewSource !== null ) {
        oSettings.sAjaxSource = sNewSource;
    }
 
    // Server-side processing should just call fnDraw
    if ( oSettings.oFeatures.bServerSide ) {
        this.fnDraw();
        return;
    }
 
    this.oApi._fnProcessingDisplay( oSettings, true );
    var that = this;
    var iStart = oSettings._iDisplayStart;
    var aData = [];
 
    this.oApi._fnServerParams( oSettings, aData );
 
    oSettings.fnServerData.call( oSettings.oInstance, oSettings.sAjaxSource, aData, function(json) {
        /* Clear the old information from the table */
        that.oApi._fnClearTable( oSettings );
 
        /* Got the data - add it to the table */
        var aData =  (oSettings.sAjaxDataProp !== "") ?
            that.oApi._fnGetObjectDataFn( oSettings.sAjaxDataProp )( json ) : json;
 
        for ( var i=0 ; i<aData.length ; i++ )
        {
            that.oApi._fnAddData( oSettings, aData[i] );
        }
         
        oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
 
        that.fnDraw();
 
        if ( bStandingRedraw === true )
        {
            oSettings._iDisplayStart = iStart;
            that.oApi._fnCalculateEnd( oSettings );
            that.fnDraw( false );
        }
 
        that.oApi._fnProcessingDisplay( oSettings, false );
 
        /* Callback item function - for event handlers etc */
        if ( typeof fnCallback == 'function' && fnCallback !== null )
        {
            fnCallback( oSettings );
        }
    }, oSettings );
};
</script>

<div class="shownpars">					
<table width="100%" cellpadding="0" cellspacing="0" border="0" class="dTable" id="item-datatable">
<thead>
    <tr>
	<th width="3%"></th>
	<th width="14%"><?php echo $lang['Item No/ Barcode']; ?></th>
	<th width="18%"><?php echo $lang['Item Name']; ?></th>
	<th width="27%"><?php echo $lang['Description']; ?></th>
	<th width="12%"><?php echo $lang['Category']; ?></th>
	<th width="10%"><?php echo $lang['Price']; ?></th>
	<th width="18%"><?php echo $lang['Qty']; ?></th>
	
    </tr>	
</thead>
<tbody>

</tbody>
</table>
</div>

           
	    
                    <div class="formRow">
                    <div class="grid12"  style="margin-top:10px;">
			<a href="#" class="buttonM bBlue" onclick="SubmitForm();"><span class="fa fa-plus"></span><span><?php echo $lang['Add Item']; ?></span></a>
		        <a href="#" class="buttonM bBlue" onclick="window.close();"><span class="fa fa-times-circle"></span><span><?php echo $lang['Cancel']; ?></span></a>
		    </div>
		    </div>
           </fieldset>

</div>
                
</form>
             
</body>
</html>
