<?php
session_start();
include './connection_sql.php';
?>

<!-- Main content -->
<section class="content">

    <div class="box box-primary">
        <div class="box-header with-border">
            <h3 class="box-title">Invoice</h3>
        </div>
        <form name= "form1" role="form" class="form-horizontal">
            <div class="box-body">

                <input type="hidden" id="tmpno" class="form-control">

                <input type="hidden" id="item_count" class="form-control">

                <div class="form-group">

                    <a onclick="new_inv();" class="btn btn-default btn-sm">
                        <span class="fa fa-user-plus"></span> &nbsp; New
                    </a>
 
                    <a onclick="save_inv();" class="btn btn-success btn-sm">
                        <span class="fa fa-save"></span> &nbsp; Save
                    </a>
 
                    <a onclick="print_inv('');" class="btn btn-default btn-sm">
                        <span class="fa fa-print"></span> &nbsp; Print
                    </a>
                    <?php if($_SESSION["User_Type"] == '1'){?>
                    <a onclick="cancel_inv();" class="btn btn-danger btn-sm">
                        <span class="fa fa-trash-o"></span> &nbsp; Cancel
                    </a>
                    <?php } ?>
                </div>

                <div id="msg_box"  class="span12 text-center"  ></div>
                <div class="form-group">
                    <label class="col-sm-1 control-label" for="invno">Invoice No</label>
                    <div class="col-sm-2">
                        <input type="text" placeholder="PO NO" disabled  id="txt_entno" class="form-control  input-sm">
                    </div>
                    <?php if($_SESSION["User_Type"] == '1'){?>
                    <div class="col-sm-2">
                        <a onfocus="this.blur()" onclick="NewWindow('serach_inv.php', 'mywin', '800', '700', 'yes', 'center');
                                return false" href="">
                            <input type="button" class="btn btn-primary btn-sm " value="..." id="searchcust" name="searchcust">
                        </a>
                    </div>
                    <?php } ?>
                    <label class="col-sm-2 control-label" for="invdate">Date</label>
                    <div class="col-sm-2">
                        <input type="date"  onchange="getno1();"   placeholder="Date" id="invdate" value="<?php echo date('Y-m-d'); ?>" class="form-control  input-sm">
                    </div>
                    <label class="col-sm-1 control-label" for="invno">Order No</label>
                    <div class="col-sm-2">
                        <input type="text" placeholder="Order No" id="DANO" class="form-control  input-sm" disabled>
                    </div>
                    <div class="col-sm-1 hidden">
                        <a onfocus="this.blur()" onclick="NewWindow('serach_inv_do.php', 'mywin', '800', '700', 'yes', 'center');
                                return false" href="">
                            <input type="button" class="btn btn-primary btn-sm " value="..." id="searchcust" name="searchcust">
                        </a>
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-sm-1 control-label" for="c_code">Customer</label>
                    <div class="col-sm-2">
                        <input type="text" placeholder="Code" name="c_code" id="c_code" disabled class="form-control  input-sm">
                    </div>
                    <div class="col-sm-3">
                        <input type="text" placeholder="Name" name = "c_name" id="c_name" disabled class="form-control input-sm">
                    </div>
                    <div class="col-sm-1">


                        <a onfocus="this.blur()" onclick="NewWindow('serach_customer.php?stname=inv', 'mywin', '800', '700', 'yes', 'center');
                                return false" href="">
                            <input type="button" class="btn btn-primary btn-sm " value="..." id="searchcust" name="searchcust">
                        </a>
                    </div>
                    
                    <div class="col-sm-5 hidden">

                        <label  class="col-sm-4 control-label" ><input type="radio" onclick="del_item('.');"  id="paymethod_0" name="paymethod" value="credit" checked="">&nbsp;Credit</label>

                        <label  class="col-sm-3 control-label" ><input type="radio" onclick="del_item('.');" id="paymethod_1" name="paymethod" value="cash">&nbsp;Cash</label>

                    </div>
                    <div class="col-sm-5 hidden">

                        <label  class="col-sm-4 control-label" ><input type="radio" onclick="del_item('.');"  id="non" name="optradio" value="non" disabled="" checked="">&nbsp;Non Vat</label>

                        <label  class="col-sm-3 control-label" ><input type="radio" onclick="del_item('.');" id="svat"name="optradio" value="svat" disabled="">&nbsp;VAT</label>

                    </div>
                </div>

                <div class="form-group">
                    <label class="col-sm-1 control-label" for="cus_address">Address</label>
                    <div class="col-sm-5">
                        <input type="text" placeholder="Name" id="cus_address" class="form-control input-sm">
                    </div>
                    <label class="col-sm-1 control-label" for="txt_remarks">Remark</label>
                    <div class="col-sm-5">
                        <input type="text" placeholder="Remarks" id="txt_remarks" class="form-control input-sm">
                    </div>
                </div>
 
                <input id='currency' type='hidden' value='LKR'>
                <input id='txt_rate' type='hidden' value='1'>

                <div class="form-group">
                    <label class="col-sm-1 control-label" for="invno">Sales Ex.</label>

                    <div class="col-sm-2">
                        <select id="salesrep" class="form-control input-sm" >

                            <?php
                             if ($_SESSION['salEx'] =="")  {			
                                    $sql = "select * from s_salrep order by REPCODE";
                             }else{
                                 $sql = "select * from s_salrep where REPCODE='".$_SESSION['salEx']."'   order by REPCODE";
                             }
                            foreach ($conn->query($sql) as $row) {
                                echo "<option value='" . $row["REPCODE"] . "'>" . $row["REPCODE"] . " " . $row["Name"] . "</option>";
                            }
                            ?>
                        </select>
                    </div>
                     <label class="col-sm-1 control-label" for="invno">Department</label>

                    <div class="col-sm-2">
                        <select id="department" class="form-control input-sm" >

                            <?php
                            
                            
    //             if (!is_numeric($_SESSION["CURRENT_DEP"]))  {							 						
    //                 $sql="select * from s_stomas where   company='".$_SESSION['company']."'  order by CODE";
    //                 foreach ($conn->query($sql) as $row) {
    //                     echo "<option value='".$row["CODE"]."'>".$row["CODE"]." ".$row["DESCRIPTION"]."</option>";
    //                 }
				// } else {
    //                 $sql="select * from s_stomas  where CODE = '" . $_SESSION["CURRENT_DEP"] ."' and company='".$_SESSION['company']."' order by CODE";
				// 	foreach ($conn->query($sql) as $row) {
    //                 echo "<option value='".$row["CODE"]."'>".$row["CODE"]." ".$row["DESCRIPTION"]."</option>";
    //                 }
    //             }
    
    // if (!is_numeric($_SESSION["CURRENT_DEP"]))  {							 						
                    $sql="select * from s_stomas    order by CODE";
                    foreach ($conn->query($sql) as $row) {
                        echo "<option value='".$row["CODE"]."'>".$row["CODE"]." ".$row["DESCRIPTION"]."</option>";
                    }
				// } else {
    //                 $sql="select * from s_stomas  where CODE = '" . $_SESSION["CURRENT_DEP"] ."' and company='".$_SESSION['company']."' order by CODE";
				// 	foreach ($conn->query($sql) as $row) {
    //                 echo "<option value='".$row["CODE"]."'>".$row["CODE"]." ".$row["DESCRIPTION"]."</option>";
    //                 }
    //             }
                            
                            
                            
                            ?>
                        </select>
                    </div>
                    <label class="col-sm-1 control-label" for="c_code">DownPayment</label>
                    <div class="col-sm-2">
                        <input type="text" placeholder="DownPayment" name="dwnPmnt" id="dwnPmnt" value="0" class="form-control  input-sm">
                    </div>
                    <label class="col-sm-1 control-label" for="c_code">Installments</label>
                    <div class="col-sm-2">
                        <input type="text" placeholder="Installments" name="insmnt" id="insmnt" value="1" class="form-control  input-sm">
                    </div>      
                </div> 
                <div class="form-group">
                    <label class="col-sm-1 control-label" for="c_code">Additional Discount</label>
                    <div class="col-sm-2">
                        <input type="text" placeholder="Additional Discount" name="addDisc" id="addDisc" value="0" onblur="changeGrandTot()" class="form-control  input-sm">
                    </div>                    
                </div>
                <table class="table table-striped">
                    <tr class='info'>
                        <th style="width: 120px;">Item</th>
                        <th>Description</th>
                        <th style="width: 10px;"></th>
                        <th style="width: 160px;">Barcode</th>
                        <th style="width: 120px;">Qty</th>
                        <th style="width: 120px;">Rate</th>
                        <th style="width: 120px;">Line Discount %</th>
                        <th style="width: 100px;"></th>
                    </tr>

                    <tr>
                        <td>
                            <input type="text" placeholder="Item" id="itemCode" value="Z" class="form-control input-sm" disabled="">
                        </td>
                        <td>
                            <input type="text" placeholder="Description" id="itemDesc" class="form-control input-sm">
                        </td>
                        <td>
                            <a href="" onclick="NewWindow('../serach_item.php?stname=spinv', 'mywin', '800', '700', 'yes', 'center');
                                    return false" onfocus="this.blur()">
                                <input type="button" name="searchcusti" id="searchcusti" value="..." class="btn btn-primary btn-sm ">
                            </a>
                        </td>
                        <td>
                            <input type="text" placeholder="barcode" id="barcode" onkeyup="checkbarcode();" class="form-control input-sm">
                        </td>
                        <td>
                            <input type="number" placeholder="Qty" id="qty" class="form-control input-sm">
                        </td>
                        <td>
                            <input type="number" placeholder="Rate" id="itemPrice" class="form-control input-sm">
                        </td>
                        <td>
                            <input type="number" placeholder="Line Discount %" id="dis_rt" class="form-control input-sm">
                        </td>
                        <td><a onclick="add_tmp();" class="btn btn-default btn-sm"> <span class="fa fa-plus"></span> &nbsp; </a></td>
                    </tr>

                </table>

                <div id="itemdetails" >

                </div>

                <table id='subtotal' class="table">
                    <tr style="visibility: hidden;">
                        <td></td>
                        <td></td>
                        <th>Sub Total</th>

                        <td></td>                        
                        <td style="width: 150px;"><input type="text" disabled placeholder="Sub Total" id="subtot" class="form-control input-sm"></td>
                    </tr>
                    <tr style="visibility: hidden;">
                        <td></td>
                        <td></td>
                        <th>NBT</th>
                        <td></td>                        
                        <td style="width: 150px;"><input type="text" disabled placeholder="NBT" id="nbt" class="form-control input-sm"></td>
                    </tr>
                    <tr style="visibility: hidden;">
                        <td></td>
                        <td></td>
                        <th>VAT</th>
                        <td></td>                        
                        <td style="width: 150px;"><input type="text" disabled placeholder="SVAT" id="svattot" class="form-control input-sm"></td>
                    </tr>
                    <tr>
                        <td></td>
                        <td></td>
                        <th>Grand Total</th>

                        <td></td>                        
                        <td style="width: 150px;"><input type="text" disabled placeholder="Grand Total" id="gtot" class="form-control input-sm"></td>
                    </tr>
                </table>		

            </div>
        </form>
    </div>

</section>
<script src="js/invoice.js"></script>
<script>
                            new_inv();
</script>
<?php
include 'login.php';
include './cancell.php';
?>
    