<?php 

    $this->load->view('admin/master/header_2');

    $modul=$this->uri->segment(3);

    $menuid=$this->uri->segment(4);

?>

<div class="content-wrapper">
    <section class="content-header">
        <h1>
            Transaction
            <small>Control panel</small>
        </h1>
        <ol class="breadcrumb">
            <li><a href="<?php echo base_url();?>admin/home/index/8"><i class="fa fa-dashboard"></i> Home</a></li>
            <li><a href="<?php echo base_url();?>admin/transaction/stock/<?php echo $menuid;?>">Stock</a></li>
            <li class="active">Insert Transaction</li>
        </ol>
    </section>
    <section class="content">
        <?php echo $this->session->flashdata('message') ?>
        <?php echo $this->session->flashdata('mdt') ?>
        <div class="row">
            <div class="col-md-12">
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title">Stock Input</h3>
                        <hr>
                        <div class="box-tools pull-right">
                            <button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
                            <button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-remove"></i></button>
                        </div>
                        <form action="<?php echo base_url();?>admin/transaction/insert_stock/<?php echo $menuid;?>" method="post" enctype="multipart/form-data">
                            <div class="row clearfix">
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <div class="form-line">
                                            <label>Transaction No *</label>
                                            <?php
                                                $stock_no=$this->master_model->stock_no();
                                            ?>
                                            <input type="text" name="purchase" class="form-control" placeholder="Purchase Order No" disabled="disabled" required="required" value="<?php echo $stock_no;?>">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <div class="form-line">
                                            <label>Date</label>
                                                <?php
                                                    $today=strtotime(date('Y-m-d'));
                                                    $today=date('m/d/Y', $today);
                                                ?>
                                            <input type="text" name="date" id="datepicker" class="form-control" placeholder="Date" value="<?php echo $today;?>">
                                        </div>
                                        <script type="text/javascript">
                                            $(function(){
                                                $("#datepicker").datepicker();
                                            });
                                        </script>
                                    </div>
                                </div>

                                <div class="col-md-3">
                                    <div class="form-group">
                                        <script>
                                            $(document).ready(function()
                                            {
                                                $(".supplier_code").bind('input', function(e)
                                                {
                                                    var code = $(this).val();
                                                    $.ajax({
                                                         type: "GET",
                                                         dataType: "html",
                                                         url: "<?php echo base_url();?>admin/transaction/get_supplier/<?php echo $menuid;?>",
                                                         data:"code="+code,
                                                         success: function(msg)
                                                         {
                                                            $("#supplier_code").html(msg);
                                                         }
                                                    });                   
                                                });
                                                
                                                $(".supplier_get_po_2").focusout(function(e)
                                                {
                                                    var code = $(this).val();
                                                    
                                                    if(code=='') 
                                                    {
                                                        $(".po").html('<label>PO No.</label><select name="purchase" class="form-control purchase_get_detail" disabled="disabled"><option value="">Choose purchase number</option></select>');
                                                    }else
                                                    {
                                                        $.ajax({
                                                             type: "POST",
                                                             dataType: "html",
                                                             url: "<?php echo base_url();?>admin/transaction/supplier_get_po_2/<?php echo $menuid;?>",
                                                             data:"code="+code,
                                                             success: function(msg)
                                                             {
                                                                $(".po").html(msg);
                                                             }
                                                        }); 
                                                    }
                                                    
                                                    $.ajax({
                                                         type: "POST",
                                                         dataType: "html",
                                                         url: "<?php echo base_url();?>admin/transaction/supplier_get_detail/<?php echo $menuid;?>",
                                                         data:"code="+code,
                                                         success: function(msg)
                                                         {
                                                            if(code=='')
                                                            {
                                                                $("#supplier_name_2").val('');
                                                                $(".payment_terms").val('');
                                                            }else
                                                            {
                                                                var data_supplier = msg;
                                                                var explode = data_supplier.split("%");
                                                                
                                                                var supplier_name  = explode[0];
                                                                var payment_terms  = explode[1];
                                                                var check             = explode[2];
                                                                var st             = explode[3];
                                                                
                                                                if(check=='exist')
                                                                {
                                                                    $("#supplier_name_2").val(supplier_name);
                                                                    $(".payment_terms").val(payment_terms);
                                                                    if(st == 1)
                                                                                    {
                                                                                        $(".st").val('Yes');
                                                                                    } else {
                                                                                        $(".st").val('NO');
                                                                                    }
                                                                }else
                                                                {
                                                                    $("#supplier_code_2").val('');
                                                                    $("#supplier_name_2").val('');
                                                                    $(".payment_terms").val('');
                                                                    
                                                                    var x = document.getElementById("snackbar3")
                                                                    x.className = "show";
                                                                    x.innerHTML = "Supplier code is missing!";
                                                                    setTimeout(function(){ x.className = x.className.replace("show", ""); }, 3000);
                                                                }
                                                            }
                                                         }
                                                    }); 
                                                    
                                                    $.ajax({url: '#', success: function(result)
                                                    {
                                                        $('.ajax-content').html('<hr>Ajax Request Completed !');
                                                    }});
                                                });
                                            });
                                        </script>
                                        <!-- <div class="form-group"> -->
                                        <div class="form-line">
                                            <label>Supplier</label>
                                            <input type="text" name="supplier_code" list="supplier_code" id="supplier_code_2" class="form-control supplier_code supplier_get_po_2 ajax" placeholder="Supplier Code" required="required">
                                            <datalist id="supplier_code"></datalist>
                                        </div>
                                        <!-- </div> -->
                                    </div>
                                    <div class="form-group">
                                        <div class="form-line">
                                            <label>Type</label>
                                                <script>
                                                    $(document).ready(function()
                                                    {
                                                        $(".type").change(function(e)
                                                        {
                                                            var type = $(this).val();
                                                            var number = $("#purchase_numbers").val();
                                                            
                                                            if(number=='')
                                                            {
                                                                $("#purchase_numbers").focus();
                                                            }else
                                                            {
                                                                if(type=='')
                                                                {
                                                                    var x = document.getElementById("snackbar")
                                                                    x.className = "show";
                                                                    x.innerHTML = "Type must be filed!";
                                                                    setTimeout(function(){ x.className = x.className.replace("show", ""); }, 3000);
                                                                }else
                                                                {
                                                                    $("#number-purchase-result").show();
                                                                    $.ajax({
                                                                         type: "POST",
                                                                         dataType: "html",
                                                                         url: "<?php echo base_url();?>admin/transaction/purchase_get_detail/"+type+"/<?php echo $menuid;?>",
                                                                         data:"number="+number,
                                                                         success: function(msg)
                                                                         {
                                                                            $(".purchase_detail").html(msg);
                                                                         }
                                                                    });     
                                                                }
                                                            }
                                                        });
                                                    });
                                                </script>

                                            <select name="type" id="type" class="form-control type" required>
                                            
                                                <option value="Incoming" selected="selected">
                                                        Incoming
                                                </option>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <script>
                                        $(document).ready(function()
                                        {
                                            $(".supplier_name").bind('input', function(e)
                                            {
                                                var name = $(this).val();
                                                $.ajax({
                                                     type: "GET",
                                                     dataType: "html",
                                                     url: "<?php echo base_url();?>admin/transaction/get_supplier_2/<?php echo $menuid;?>",
                                                     data:"name="+name,
                                                     success: function(msg)
                                                     {
                                                        $("#supplier_name").html(msg);
                                                     }
                                                });                   
                                            });
                                            
                                            $(".supplier_get_po").focusout(function(e)
                                            {
                                                var name = $(this).val();
                                                
                                                if(name=='') 
                                                {
                                                    $(".po").html('<label>PO No.</label><select name="purchase" class="form-control purchase_get_detail" disabled="disabled"><option value="">Choose purchase number</option></select>');
                                                }else
                                                {
                                                    $.ajax({
                                                         type: "POST",
                                                         dataType: "html",
                                                         url: "<?php echo base_url();?>admin/transaction/supplier_get_po/<?php echo $menuid;?>",
                                                         data:"name="+name,
                                                         success: function(msg)
                                                         {
                                                            $(".po").html(msg);
                                                         }
                                                    }); 
                                                }
                                                
                                                
                                                $.ajax({
                                                     type: "POST",
                                                     dataType: "html",
                                                     url: "<?php echo base_url();?>admin/transaction/supplier_get_detail_2/<?php echo $menuid;?>",
                                                     data:"name="+name,
                                                     success: function(msg)
                                                     {
                                                        if(name=='')
                                                        {
                                                            $("#supplier_code_2").val('');
                                                            $(".payment_terms").val('');
                                                        }else
                                                        {
                                                            var data_supplier = msg;
                                                            var explode = data_supplier.split("%");
                                                            
                                                            var supplier_code  = explode[0];
                                                            var payment_terms  = explode[1];
                                                            var check             = explode[2];
                                                            var st             = explode[3];
                                                            
                                                            if(check=='exist')
                                                            {
                                                                $("#supplier_code_2").val(supplier_code);
                                                                $(".payment_terms").val(payment_terms);
                                                                if(st == 1)
                                                                                {
                                                                                    $(".st").val('Yes');
                                                                                } else {
                                                                                    $(".st").val('NO');
                                                                                }
                                                            }else
                                                            {
                                                                $("#supplier_name_2").val('');
                                                                $("#supplier_code_2").val('');
                                                                $(".payment_terms").val('');
                                                                
                                                                var x = document.getElementById("snackbar3")
                                                                x.className = "show";
                                                                x.innerHTML = "Supplier name is missing!";
                                                                setTimeout(function(){ x.className = x.className.replace("show", ""); }, 3000);
                                                            }
                                                        }
                                                     }
                                                }); 
                                            });
                                            
                                        });
                                    </script>
                                    <div class="form-group">
                                    <div class="form-line">
                                        <label>Supplier Name</label>
                                        <span id="name-supplier-result" style="display:none"><i class="fa fa-refresh fa-spin"></i></span>
                                        <input type="text" name="supplier" list="supplier_name" id="supplier_name_2" class="form-control supplier_name supplier_get_po" placeholder="Supplier Name" required="required">
                                        <datalist id="supplier_name"></datalist>
                                    </div></div>
                                    <div class="form-group">
                                        <div class="form-line">
                                            <label>PPN :</label>
                                            
                                            <input type="text" name="status" class="form-control st" value=""readonly="readonly">
                                        </div>
                                    </div>
                                </div>

                                <div class="col-md-3">
                                    <div class="form-group po">
                                        
                                        <label>PO No.</label>
                                        <script>
                                            $(document).ready(function()
                                            {
                                                $(".purchase_number").bind('input', function(e)
                                                {
                                                    var number = $(this).val();
                                                    $.ajax({
                                                         type: "GET",
                                                         dataType: "html",
                                                         url: "<?php echo base_url();?>admin/transaction/get_purchase_number/<?php echo $menuid;?>",
                                                         data:"number="+number,
                                                         success: function(msg)
                                                         {
                                                            $("#purchase_number").html(msg);
                                                         }
                                                    });                   
                                                });
                                                

                                                
                                            });
                                        </script>
                                        <!--<span id="number-purchase-result" style="display:none"><i class="fa fa-refresh fa-spin"></i></span>
                                        <input type="text" name="purchase" list="purchase_number" id="purchase_numbers" class="form-control purchase_number purchase_get_detail" placeholder="Choose supplier first!" required="required" readonly="readonly">
                                        <datalist id="purchase_number"></datalist>
                                        -->
                                        <select name="purchase" class="form-control purchase_get_detail ajax" disabled="disabled">
                                            <option value="">
                                                Choose purchase number
                                            </option>
                                        </select>
                                    </div>
                                    <div class="form-group">
                                        <div class="form-line">
                                            <label>Ref No.</label>
                                            <input type="text" name="ref_no" class="form-control" placeholder="Ref No" />
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <!-- </div> -->
                            <div class="table-responsive">
                                <table class="table table-bordered table-striped purchase_detail">
                                    <thead>
                                        <tr>
                                            <th>No.</th>
                                           <!--  <th style="text-align:center">
                                                <span></span>
                                                <div class="switch">
                                                   <label><input type="checkbox" checked><span class="lever"></span></label>
                                                </div>
                                            </th> -->
                                            <th>Code</th>
                                            <th>Item</th>
                                            <th>Order Qty</th>
                                            <th>Available Qty</th>
                                            <th>In Qty</th>
                                            <th>In Qty Total</th>
                                            <!-- <th>Status</th> -->
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                            <td colspan="9">
                                                No data available in table
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- <div id="snackbar2">Purchase order detail does not exist!</div>
    <div id="snackbar3"></div>
    <div id="snackbar4">Next select purchase order number!</div>
    <div id="snackbar">In qty should not be greater than order qty!</div> -->

    <div class="modal fade" id="edit-modal" role="dialog">
        <div class="modal-dialog">
            <div class="modal-content" style="z-index:9">
                <div class="modal-header">
                </div>
            </div>
        </div>
    </div>

</div>

            
<script type="text/javascript">
    $(document).on('change', '.purchase_get_detail', function(e){
    
        var number = $(this).val();
        var type = $("#type").val();
        
        if(type=='')
        {
            var x = document.getElementById("snackbar3")
            x.className = "show";
            setTimeout(function(){ x.className = x.className.replace("show", ""); }, 3000);
        }else
        {
            $.ajax({
                 type: "POST",
                 dataType: "html",
                 url: "<?php echo base_url();?>admin/transaction/purchase_get_detail/"+type+"/<?php echo $menuid;?>",
                 data:"number="+number,
                 success: function(msg)
                 {
                    $(".purchase_detail").html(msg);
                 }
            });     
        }
        
        $.ajax({
             type: "POST",
             dataType: "html",
             url: "<?php echo base_url();?>admin/transaction/purchase_get_customer/<?php echo $menuid;?>",
             data:"number="+number,
             success: function(msg)
             {
                var data_supplier = msg;
                var explode = data_supplier.split("%");
                
                var supplier_code  = explode[0];
                var supplier_name  = explode[1];
                                
                $("#supplier_name_2").val(supplier_name);
             }
        });     
                
    });
</script>
    
<?php $this->load->view('admin/master/footer_2');?>