<?php if (!isset($_SESSION['FirstName'])) {
	session_start();
}
    include ('includes/db1config.php');
    include ('../includes/mysql_connect.php');
	include ('includes/admin-config.php');	//get site options
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<?php 
include('title.php'); 
echo'<link href="css/'.$stylesheet.'" rel="stylesheet" type="text/css" media="screen" />';	
echo'<link href="css/print_style.css" rel="stylesheet" type="text/css" media="print" />';
?>

<link rel="stylesheet" type="text/css" href="css/cssverticalmenu.css" />
<link rel="stylesheet" href="includes/tinyaccordion/style.css" type="text/css" />

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"> </script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script src="//code.jquery.com/ui/1.10.3/jquery-ui.js"></script>


<script type="text/javascript" src="../js/ddaccordion.js"></script>

<!-- for popup windows -->
<link href="css/popup-style.css" rel="stylesheet" type="text/css" media="all" />
<script type="text/javascript" src="js/script.js"></script>
 <!-- end popup script-->

<style type="text/css">

.mainForm{
width: 700px;
margin: auto;
}
.leftForm {
float: left;
width: 300px;
}
.rightForm{
float: left;
width: 300px;
}
.saveButton{
margin: auto;
}
#save-button{
margin-right: 115px;
width: 150px;
}

.AddMOreEdit:hover {
	cursor: pointer;
}

.arrowlistmenu{
	width: 100%; /*width of accordion menu*/
	}

	.arrowlistmenu .menuheader{ /*CSS class for menu headers in general (expanding or not!)*/
	font:12px Verdana,Arial;width:95%; border:1px solid #467182; padding:7px; 
		font-weight:bold; margin-top:5px; cursor:pointer; 
	    -webkit-border-radius: 7px;
	    -moz-border-radius: 7px;
	    border-radius: 7px;
	    border: solid 1px #e8e8e8;
	    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.4), 0 1px 1px rgba(0, 0, 0, 0.2);
	    -webkit-transition-duration: 0.2s;
	    -moz-transition-duration: 0.2s;
	    transition-duration: 0.2s;
	    -webkit-user-select:none;
	    -moz-user-select:none;
	    -ms-user-select:none;
	    user-select:none;
		color:#fff;
		background-color:#2077a1;
		padding-left:.5em;padding-right:.5em;
	}

	.arrowlistmenu .openheader{ /*CSS class to apply to expandable header when it's expanded*/
	background-image: url(titlebar-active.png);
	}

	.arrowlistmenu ul{ /*CSS for UL of each sub menu*/
	list-style-type: none;
	margin: 0;
	padding: 0;
	margin-bottom: 0px; /*bottom spacing between each UL and rest of content*/
	background: none;
	}

	.arrowlistmenu ul li{
	padding-bottom: 0px; /*bottom spacing between menu items*/
	}

	.arrowlistmenu ul li .opensubheader{ /*Open state CSS for sub menu header*/
	background: lightblue !important;
	}

	.arrowlistmenu ul li .closedsubheader{ /*Closed state CSS for sub menu header*/
	background: lightgreen !important;
	}

	.arrowlistmenu ul li a{
	color: #81BEF7;
	background: url(arrowbullet.png) no-repeat center left; /*custom bullet list image*/
	display: block;
	padding: 0px 0;
	padding-left: 19px; /*link text is indented 19px*/
	text-decoration: none;
	font-weight: bold;
	border-bottom: 1px solid #dadada;
	font-size: 90%;
	}

	.arrowlistmenu ul li a:visited{
	color: #81BEF7;
	}

	.arrowlistmenu ul li a:hover{ /*hover state CSS*/
	color: #FA5858;
	background-color: #FA5858;
	}

	.arrowlistmenu ul li a.subexpandable:hover{ /*hover state CSS for sub menu header*/
	background: lightblue;
	}
	html {
	    overflow-y: scroll;
	}
	

</style>

<style type="text/css">
	body#pop-up-window {
		background:none;
		margin-top:5px;
	}
	
body#pop-up-window #content,
body#pop-up-window #main-content {
margin:0px;
padding:0px;
width:100%;
background:none;
	}
	
body#pop-up-window #content {
	}
	
	#wrapper.custom {
		-webkit-border-radius: none;
		-moz-border-radius: none;
	    border-radius: 0;
		border: none;
		background: none;
		width: 770px;
		padding:0px;
		margin:0px;
		margin:16px;
	}

	.disabled {
		background: #48a3db none repeat scroll 0 0;
	    border: 1px solid #777777;
	    border-radius: 4px;
	    color: #fff;
	    opacity: 0.34;
	    padding: 3px 6px;
	}

	.disabled:hover {
		background: #48a3db none repeat scroll 0 0 !important;
	    border: 1px solid #777777 !important;
	    border-radius: 4px !important;
	    color: #fff !important;
	    opacity: 0.34 !important;
	    padding: 3px 6px !important;
	}
</style>

</head>
<body id="pop-up-window">

<div id="wrapper" class="custom">



<!-- start page -->



<!-- start content -->
<div id="content">
<div id="main-content">
   
		<div class="post">
		  <div class="entry">

<h1>Update Invoice</h1>
                
<?php 
if($_SESSION['adminkey']=="security"){ 
	include ('includes/db1config.php');
	include ('../includes/mysql_connect.php');

    $query = "SELECT * from customer_invoices where recordkey = ".$_GET['id'];
	$rt    = mysql_query($query); // query executed
	echo mysql_error(); // if any error is there that will be printed to the screen
	$row   = mysql_fetch_array($rt);

	if($row['paid'] == 0 ){
		$readonly = '' ;
		$btn      = '';
	} else {
		$readonly = 'readonly' ;
		$btn      = 'disabled';
	}
?>
	
	<div class="mainForm">
                <form action="updateInvoice.php" method="post">
                    <input type="hidden" name="record_id" value="<?php echo $_GET['id']; ?>">
                    <div class="leftForm">
                    <table>    
                        <tr><td>Invoice Number:</td><td><input readonly required type="text" name="invoice_number" value="<?php echo $row['invoice_number']; ?>" id="Item" size="20"></td></tr>

                        <tr><td>Invoice Date:</td><td><input  required type="text" name="invoice_date" class="" id="datepicker1" size="20" value="<?php echo $row['invoice_date']; ?>" ></td></tr>

                        <tr><td>Due Date :</td><td><input  value="<?php echo $row['due_date']; ?>" required type="text" name="due_date" class="" id="datepicker2" ></td></tr>

                        <tr>
                        	<td>Paid</td>
                        	<td>
                        		<select required name="paid">
                            		<option value="1" <?php if( $row['paid'] == 1 ){ echo "selected"; } ?> >Paid</option>
                            		<option value="0" <?php if( $row['paid'] == 0 ){ echo "selected"; } ?> >Unpaid</option>
                        		</select>
                        	</td>
                        </tr>
                    	<tr>
                            <td>One Time  : <input <?php if( $row['recurring'] == 0 ){ echo "checked"; } ?> required type="radio" name="recurring" value="0" ></td>
                            <!--<td>Recurring : <input <?php if( $row['recurring'] == 1 ){ echo "checked"; } ?> required type="radio" name="recurring" value="1" ></td>-->
                        </tr>
                    </table>
				    <!-- // Getting subinvoices linked to Master record -->
				    <?php  
						$Invoices     = "SELECT * from customer_invoices where customer_id =".$row['customer_id']." and invoice_number = '".$row['invoice_number']."' and master_record = 0";  
						$invoicedata  = mysql_query($Invoices); // query executed 
					    echo mysql_error();      // if any error is there that will be printed to the screen
				    	// $chldinvoices = mysql_fetch_assoc($invoicedata);
					    // echo"<pre>";print_r($chldinvoices);die;
				    ?>
                    <center>
                    	<tr>
			                <td colspan="4" style="text-align:center";>
			                	<?php  
			                	if( $row['paid'] == 1 ){
			                	?>
			                    	<a <?php echo $btn; ?> style="float: left;margin-bottom: 10px;margin-top: 9px;width: 57px;" class="<?php echo $btn; ?> AddMOreEdit" id="save-button" >Add More</a>
			                	<?php  } else { ?>

				                    <a style="float: left;margin-bottom: 10px;margin-top: 9px;width: 57px;" onclick="return addRow();" class="<?php echo $btn; ?> AddMOreEdit" id="save-button" >Add More</a>

			                	<?php  } ?>
			                </td>
			                <td></td>
			            </tr>
                        <table width=100% id="invoiceTable" class="chklength" >
                        <tr>
                           
                            <td>
                                <strong>Item Description</strong>
                            </td>

                            <td>
                                <strong>Quantity</strong>
                            </td>

                            <td>
                                <strong>Item Charge</strong>
                            </td>

                            <td>
                                <strong>Extended Charge</strong>
                            </td>

                            <td>
                                <strong>Action(s)</strong>
                            </td>
                        </tr>
                        <tr class="appendInvoice">
                            <td colspan="8"><hr /></td>
                        </tr>
                    	<?php  $i=0; while ( $inv = mysql_fetch_assoc($invoicedata)) {  $i++; ?>

                        <input type="hidden" name="oldinvoiceid[]" value="<?php echo $inv['recordkey'];?>">
                        <input type="hidden" name="customer_id" value="<?php echo $_GET['custid'];?>">

                        <input type="hidden" class="is_last_record" name="is_last_record" value="">

                        <tr class="rowNum<?php echo $i;?> countrowNum">
                            <td width=10%>
                                <input <?php echo $readonly; ?> required type="text" name="itemdesc[]" id="Item" value="<?php echo $inv['item_description'];?>" size="10" ></td>
                                <input type="hidden" name="subinvoiceid[]" value="<?php echo $inv['recordkey'];?>">
                            <td width=10%>
                                <input <?php echo $readonly; ?> required type="text" class="quantity" data-id="<?php echo $i; ?>" name="quantity[]" id="quantity<?php echo $i; ?>" value="<?php echo $inv['quantity'];?>" onkeypress="return isNumberKey(event)" size="10" ></td>
                            <td width=10%>
                                <input <?php echo $readonly; ?> required type="text" class="itemcharge" data-id="<?php echo $i; ?>" name="itemcharge[]" value="<?php echo $inv['item_charge'];?>" onkeypress="return isNumberKey(event)" id="itemcharge<?php echo $i; ?>" size="10" ></td>
                            <td width=10%>
                            	<input required type="text" id="extendedcharge<?php echo $i; ?>" onkeypress="return isNumberKey(event)" class="extendedcharge" readonly name="extendedcharge[]" value="<?php echo $inv['extended_charge'];?>" id="Item" size="10" >
                            </td>
                            <?php  
		                		if( $row['paid'] == 1 ){ ?>

		                            <td width=10%>
		                            	<a class="removerows disabled" disabled id="save-button" href="javascript:void(0);" title="">Remove Row</a>
		                            </td>

		                	<?php  	} else { ?>

									<td width=10%>
		                            	<a class="removerows <?php echo $disabled; ?>" <?php echo $disabled; ?> onclick="removeRow(<?php echo $i; ?>);" id="save-button" href="javascript:void(0);" title="">Remove Row</a>
		                            </td>
	                		<?php }	?>
                        <tr>
                    	</tr>
                        </tr>
                    <?php } ?>
                        <tr class="addfunc"></tr>
                        <tr>
                            <td colspan="4"></td>
                        </tr>
                        </table>
                        </table>
                    </center>
                    <tr>
                        <td colspan="6">
                            Invoice Total:- <br>
                            <input required readonly id="invoicetotal" type="text" size="10" name="invoice_total" value="<?php echo $row['invoice_total'] ?>" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <input <?php echo $btn; ?> class="<?php echo $btn; ?> notUpdate" id="save-button" type="submit" name="edit" value="UPDATE" />
                        </td>
                    </tr>
                </div>
            </div>

<?php } else {
	echo '<font color="red"><div align="center">You are not authorized to view this page. <br>Please <a href="index.php"><font color="blue">login here</font></a>.</div></font>';
}
?>
            
 <br />

</div><!-- end entry-->

<br />

</div><!-- end post-->

</div><!-- end main-content-->

</div><!-- end content -->


<div style="clear: both;">&nbsp;</div>
<div id="page-bg-btm">
</div><!-- end page-bg-btm div -->





</div><!-- end wrapper div-->

<!-- end page -->


<!--footer-->

<script type="text/javascript">
    	<?php 
    		$row = $i+1; 
    		if( isset($row['paid']) && $row['paid'] == 1 ) {
    			$jsdisabled  = 'disabled';
    		} else{
    			$jsdisabled  = '';
    		}
    	?>
    	var rowNum    = "<?php echo $row; ?>";
    	var toDisable = "<?php echo $jsdisabled; ?>";

        function addRow() {

			rowNum ++;

			var row = '<tr class="rowNum'+rowNum+' countrowNum" ><td width=10%><input required type="text" name="itemdesc[]" class="itemdesc" id="itemdesc'+rowNum+'" size="10" ></td><td width=10%><input required type="text" name="quantity[]" class="quantity" onkeypress="return isNumberKey(event)" data-id="'+rowNum+'" id="quantity'+rowNum+'" size="10" ></td><td width=10%><input required type="text" name="itemcharge[]" class="itemcharge" onkeypress="return isNumberKey(event)" data-id="'+rowNum+'" id="itemcharge'+rowNum+'" size="10" ></td><td width=10%><input required type="text" class="extendedcharge" id="extendedcharge'+rowNum+'" name="extendedcharge[]" readonly onkeypress="return isNumberKey(event)" id="Item" size="10" ></td><td width=10%><a onclick="removeRow('+rowNum+');" '+toDisable+' class="removerows '+toDisable+' " id="save-button" href="javascript:void(0);" title="">Remove Row</a></td></tr>';

		  	jQuery('.addfunc').after(row);
			var i = $('.chklength').find('tr.countrowNum').length;
        	if(i == 0 ){
    			$('.is_last_record').val(1);
        	} else{
        		$('.is_last_record').val(0);
    			$('.notUpdate').removeClass('disabled').removeAttr('disabled','disabled');
        	}
		}

		function removeRow(rnum) {

	        jQuery('.rowNum'+rnum).remove();
	        $('#invoicetotal').val( getSum() );

        	var i = $('.chklength').find('tr.countrowNum').length;
        	if(i == 0 ){
        		// confir
    			$('.is_last_record').val(1);
        	} else{
        		$('.is_last_record').val(0);
    			$('.notUpdate').removeClass('disabled').removeAttr('disabled','disabled');
        	}

		}

		$(document).ready(function() {
			
			$('#datepicker1').datepicker({ dateFormat: 'yy-mm-dd' });
			$('#datepicker2').datepicker({ dateFormat: 'yy-mm-dd' });

			$(document).on('keyup', '.itemcharge', function(event) {
				event.preventDefault();
				/* Act on the event */
				var id    		= $(this).attr('data-id') ;
				var qty   		= $('#quantity'+id+'').val();
				var price 		= $(this).val();
				var Totalprice  = qty * price;

 				$('#extendedcharge'+id+'').val(Totalprice.toFixed(2));

 				$('#invoicetotal').val( getSum() );

			});

			$(document).on('keyup', '.quantity', function(event) {
				event.preventDefault();
				/* Act on the event */
				var id    		= $(this).attr('data-id') ;
				var price   	= $('#itemcharge'+id+'').val();
				var qty 		= $(this).val();
				var Totalprice  = qty * price;

 				$('#extendedcharge'+id+'').val(Totalprice.toFixed(2));

 				$('#invoicetotal').val( getSum() );

			});

			// $(document).on('click', '.removerows', function(event) {
			// 	event.preventDefault();
			// 	getSum();
			// 	alert( $('.countrowNum').length() );
			// 	if( $('.countrowNum').length() == 0 ){
			// 		$('.notUpdate').addClass('disabled');
			// 	} 
			// });

		});


		function getSum() {

		    var sum = 0;
		    
		    $('.extendedcharge').each(function() {
		        sum += parseFloat( $(this).val() );
		    });

		    return sum.toFixed(2);

		}

		function isNumberKey(evt){

   		//  var charCode = (evt.which) ? evt.which : event.keyCode
	  	//  if (charCode > 31 && (charCode < 48 || charCode > 57) || $(this).val().indexOf('.') != -1)  )
			// // if ((eve.which != 46 || $(this).val().indexOf('.') != -1) && (eve.which < 48 || eve.which > 57) )
	  	//           return false;
	  	//       return true;
		  	var charCode = (evt.which) ? evt.which : evt.keyCode;
        	if (charCode != 46 && charCode > 31 
	            && (charCode < 48 || charCode > 57))
	            return false;

	        return true;

	    }

	    $('form').submit(function(event) {
	    	var id ='<?php echo $_GET["custid"];?>';
			window.top.location.href = "/cmsadmin/index.php?admin-page=13&custid="+id+""; 
	    });

</script>

<?php 
	if( isset($row['paid']) && $row['paid'] == 1 ) {
?>
	<script>
		$(document).ready(function() {
			$(document).on('click', '#save-button', function(event) {
				event.preventDefault();
				alert('You cannot update this invoice as it was already paid ?');
    			return false;
			});
		});
	</script>
<?php
	} 
?>
</body>
</html>

