<?php echo $__env->make('dashboard.include.header', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>

  
  <div class="content-wrapper">
    <div class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1 class="m-0">GRN</h1>
          </div>
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="/home">Home</a></li>
              <li class="breadcrumb-item active">GRN</li>
            </ol>
          </div>
        </div>
      </div>
    </div>
   
    <section class="content">
      <div class="container-fluid">







    <form class="form-horizontal" method="POST" action="/addpurchasing">
    <?php echo e(csrf_field()); ?>


      <div class="card">
        <div class="card-body">

          <div class="row">
            <div class="col-md-3">
                <div class="form-group">
                    <label>Supplier Name</label>
                    <select class="form-control select2" name="supplier_name" id="supplier_name" style="width: 100%;" required>
                        <option value="" selected="selected" disabled>Select</option>
                        <?php $__currentLoopData = $item; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $itemd): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <option value="<?php echo e($itemd->id); ?>"><?php echo e($itemd->Sname); ?></option>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?> 
                    </select>
                </div>
            </div>
            <div class="col-md-3">
                <div class="form-group">
                    <label for="">Supplier Balance Payment</label><input type="text" class="form-control" id="supbalance" name="supbalance" placeholder="Supplier Balance Payment" readonly required>
                </div>
            </div>
    
            <div class="col-md-3">
                <div class="form-group">
                    <label for="">Supplier Invoice Number</label><input type="text" class="form-control" id="invoiceno" name="invoiceno" value="<?php echo e($invno); ?>"  required>
                </div>
            </div>
          
          </div>
          <br><br>

            <body>       
                <div class="row">
                <div class="col-md-4">
                    <div class="form-group">
                        <label>Product Name</label>
                        <select class="form-control select2" name="product" id="product" style="width: 100%;">
                            <option value="" selected="selected" disabled>Select</option>
                            <?php $__currentLoopData = $product; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $itemd): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <option value="<?php echo e($itemd->id); ?>"><?php echo e($itemd->name); ?></option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?> 
                        </select>
                    </div> 
                    </div>
                    <div class="col-md-1">
                        <div class="form-group">
                            <label for="">Quantity</label><input type="number" id="Quantity" class="form-control" name="Quantity" placeholder="Quantity" >
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="form-group">
                            <label for="">Unit Price</label><input type="number"id="Unit_Price" class="form-control" name="Unit_Price" placeholder="Unit Price" >
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="form-group">
                            <label for="">Total</label><input type="number" class="form-control" value="0" id="Total" name="Total" placeholder="Total"  readonly>
                        </div>
                    </div>
                    <div class="col-md-2">
                    <div class="form-group radiovalue">
                        <label for="">Genarate Barcode</label><br>
                        <div class="form-check form-check-inline">
                            <input class="form-check-input radiochange" type="radio" name="radiochange" id="radiochange" value="only product">
                            <label class="form-check-label" for="">Asset</label>
                        </div>
                        <div class="form-check form-check-inline">
                            <input class="form-check-input radiochange" type="radio" name="radiochange" id="radiochange" value="All Quantity" checked>
                            <label class="form-check-label" for="">Inventory</label>
                        </div>
                        </div>
                    </div>
                    <div class="col-md-1" style="padding-top:32px;">
                    <button type="button" class="btn btn-block btn-primary btn-sm add-row">Add</button>
                    </div>
                    

                    </div>

                        <table id="tbUser">
                            <thead>
                                <tr>
                                    <th>Product Name</th>
                                    <th>Quantity</th>
                                    <th>Unit Price</th>
                                    <th>Total</th>
                                    <th>Genarate Barcode</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                    
                            </tbody>
                        </table>
    
                </body> <br>
<div class="row">
<div class="col-md-10"></div>
<div class="col-md-2">
    <div class="form-group">
        <label for=""> Net Amount</label><input type="number" class="form-control" value="0" id="nettotal" name="nettotal" placeholder="Net Amount"  readonly>
    </div>
</div>
</div>
<div class="row">
<div class="col-md-10"></div>
<div class="col-md-2"><button type="submit" class="btn btn-block btn-primary">Purchase</button> </div>
</div>

        </div>
        </div>

</form>














      </div>
    </section>
  </div>
 



<div class="modal fade" id="supmodel" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
      <div class="modal-content">
      <div class="modal-header">
          <h5 class="modal-title" id="exampleModalLongTitle">Purchasing Details Change</h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
          </button>
      </div>
      <form class="form-horizontal" method="POST" action="/purchasingupdate2">
          <?php echo e(csrf_field()); ?>

      <div class="modal-body"> 
          <input type="hidden" name="id" id="id">    
          <input type="hidden" name="paidamount" id="paidamount">    
          <input type="hidden" name="supname" id="supname">    
                <div class="form-group">
                    <label>Supplier Name</label>
                    <select class="form-control select2" name="supplier_name" id="supplier_name" style="width: 100%;" required>
                        <option value="" selected="selected" disabled>Select</option>
                        <?php $__currentLoopData = $item; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $itemd): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <option value="<?php echo e($itemd->id); ?>"><?php echo e($itemd->Sname); ?></option>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?> 
                    </select>
                </div>

            <div class="form-group">
                <label for="">Cost</label><input type="number" class="form-control" id="cost" name="cost" placeholder="cost" required>
            </div>
            <div class="form-group">
                <label for="">Invoice Number</label><input type="number" class="form-control" id="invoiceno" name="invoiceno" placeholder="Invoice Number" required>
            </div>
            <div class="form-group">
                <label for="">Paid Status</label>
                <div class="form-check">
                    <input class="form-check-input" type="checkbox" name="paidstatus" value="" id="flexCheckChecked" checked>
                    <label class="form-check-label" for="flexCheckChecked">Paid</label>
                </div>
            </div>
            <div class="form-group">
                  <label>Note</label>
                  <textarea type="text" placeholder="" value="" id="note" rows="2" name="note" class="form-control" required></textarea>
              </div>


      </div>
      <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
          <button type="submit" class="btn btn-primary">Save changes</button>
      </div>
      </form>
      </div>
  </div>
</div>
  <?php echo $__env->make('dashboard.include.footer', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>;
  <style>
    table{
        width: 100%;
        margin: 20px 0;
        border-collapse: collapse;
    }
    table, th, td{
        border: 1px solid #cdcdcd;
    }
    table th, table td{
        padding: 5px;
        text-align: left;
    }
</style>

<script>
 $(document).ready(function() {
$('#flexCheckChecked').change(function() {
    if($(this).is(":checked")) {
        $("#pdate2").html("*Paid Date");
    }
    else{
        var valupricw = $('#payment2').val();
        if(valupricw=="cash"){
        $("#pdate2").html("Paid Date");
        }else if(valupricw=="cheque"){
        $("#pdate2").html("Cheque Date");
        }
        else if(valupricw=="credit"){
        $("#pdate2").html("Credit Date");
        } 
    }

});
});


$(function () {
    $('.select2').select2()
})

$('#reservationdate').datetimepicker({
format: 'YYYY-MM-DD HH:mm:ss'
});
$('#reservationdate2').datetimepicker({
format: 'YYYY-MM-DD HH:mm:ss'
});
// $('#reservationdate2').datetimepicker({
// format: 'DD/MM/YYYY'
// });


$('#payment').on('change', function(){
  var id = $(this).val();

  if(id=="cash"){
    $("#pdate").html("Paid Date");
}else if(id=="cheque"){
  $("#pdate").html("Cheque Date");
}
else if(id=="credit"){
  $("#pdate").html("Credit Date");
}    
});

$(function () {
    $("#example").DataTable({
      "responsive": true, "autoWidth": false,"order": [[ 0, "desc" ]],
    }).buttons().container().appendTo('#example1_wrapper .col-md-6:eq(0)');
  });


$(document).on('click', '.purchasingdelete', function (e) {
    e.preventDefault();
    var id = $(this).data('id');
    let ele = this;
    console.log(id);

                swal({
                title: "Are you sure?",
                text: "Are you Sure to Remove Purchassing Recipt",
                icon: "warning",
                showCancelButton: true,
    confirmButtonColor: '#3085d6',
    cancelButtonColor: '#d33',
    confirmButtonText: 'Yes, delete it!',
                dangerMode: true,
            }).then(function(isConfirm) {
                
                if (isConfirm.value===true) {

                        $.ajax({
                            url: "<?php echo e(url('/deletepurchassing')); ?>/"+id ,
                            method: "POST",
                            headers: {
                                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                            },
                            cache: false,
                            contentType: false,
                            processData: false,
                            dataType: 'JSON',
                            data:{
                                "id":id
                            },
                            success: function (result, code, ex) {
                                swal("Done!", "It was successfully deleted!", "success");
                                $(ele).closest('tr').hide();
                                // $("#here").load(location.href + " #here");
                                // window.location.href = "/supplier";
                            },
                            error: function (jqXHR, textStatus, errorThrown) {
                                swal("Warning", "Unable to Process your request", "error");
                            },
                        });

                } else {
                    swal("Cancelled", "No Removed", "error");
                }
            })
});

$('#supmodel').on('show.bs.modal', function (event) {

var button = $(event.relatedTarget) // Button that triggered the modal
var recipient = button.data('id') 
var supplier = button.data('supid') 
var product = button.data('proid') 
var qty1 = button.data('qtyss') 
var paymethod = button.data('payment') 
var paiddate = button.data('pdate') 
var cccdate = button.data('cdate') 
var cost1 = button.data('cost') 
var staa = button.data('statuss') 
var amount = button.data('amount') 
var checkno1 = button.data('checkno') 
var note1 = button.data('note') 
var supname = button.data('supname') 
var invoiceid = button.data('invoiceid') 

var modal = $(this)
modal.find('.modal-body #id').val(recipient)
modal.find('.modal-body #paidamount').val(amount)
modal.find('.modal-body #cost').val(cost1)
modal.find('.modal-body #supname').val(supname)
modal.find('.modal-body #invoiceno').val(invoiceid)
$("#supplier_name").val(supplier).change();
$("#product_name").val(product).change();
$("#payment2").val(paymethod).change();

modal.find('.modal-body #checkno').val(checkno1)
modal.find('.modal-body #note').val(note1)


if(paymethod=="cash"){
    modal.find('.modal-body #paid_date').val(paiddate)
    $("#pdate2").html("Paid Date");
}else if(paymethod=="cheque"){
    modal.find('.modal-body #paid_date').val(cccdate)
    $("#pdate2").html("Cheque Date");
}else{
    modal.find('.modal-body #paid_date').val(cccdate)
    $("#pdate2").html("Credit Date");
}
if(staa==1){
    $( "#flexCheckChecked" ).prop( "checked", true );
}else{
    $( "#flexCheckChecked" ).prop( "checked", false  );
}

})

$('#payment2').on('change', function(){
  var id = $(this).val();

  if(id=="cash"){
    $("#pdate2").html("Paid Date");
}else if(id=="cheque"){
  $("#pdate2").html("Cheque Date");
}
else if(id=="credit"){
  $("#pdate2").html("Credit Date");
}    
});

$('#Quantity').on('input',function(e){
    var dis = $(this).val();
    var n1 = parseInt($('#Unit_Price').val());
    $('#Total').val(dis*n1);
    
});

$('#Unit_Price').on('input',function(e){
    var dis = $(this).val();
    var n1 = parseInt($('#Quantity').val());
    $('#Total').val(dis*n1);
});
</script>
 
<script>
    $(document).ready(function(){
    
        $(".add-row").click(function(){
            var name = $( "#product option:selected" ).text();
            var conceptName = $('#product').val();
            var qty = $("#Quantity").val();
            var uni = $("#Unit_Price").val();
            var tot = $("#Total").val();
            var radio = $("input[name='radiochange']:checked").val();

            var markup = "<tr><td><input type='hidden' name='pro[]' value='"+conceptName+"'>"+name+"</td><td><input type='hidden' name='qty[]' value='"+qty+"'>"+qty+"</td><td><input type='hidden' name='unit[]' value='"+uni+"'>"+uni+"</td><td><input type='hidden' name='tot[]' id='tota' value='"+tot+"'>"+tot+"</td><td><input type='hidden' name='radiovalu[]' value='"+radio+"'>"+radio+"</td><td class='text-center'><a class='btn btn-danger btn-xs delete-row'><i class='far fa-trash-alt'></i></a></td></tr>";
            $("#tbUser tbody").append(markup);
            

            var values = $("input[name='tot[]']").map(function(){return $(this).val();}).get();
            var weeklyPay= getArraySum(values);
            $('#nettotal').val(weeklyPay);

            $('#Quantity').val("");
            $('#Unit_Price').val("");
            $('#Total').val("");
            $('#product').val(''); 
            
        });
        
        // Find and remove selected table rows
        $(".delete-row").click(function(){

                if($(this).closest("tr")){
                    $(this).parents("tr").remove();
                    var values = $("input[name='tot[]']").map(function(){return $(this).val();}).get();
            var weeklyPay= getArraySum(values);
            $('#nettotal').val(weeklyPay);

                }
   
        });

        $("#tbUser").on('click', '.delete-row', function () {
        $(this).closest('tr').remove();

                    var values = $("input[name='tot[]']").map(function(){return $(this).val();}).get();
            var weeklyPay= getArraySum(values);
            $('#nettotal').val(weeklyPay);

});
    });    



function getArraySum(a){
    var total=0;
    for(var i in a) { 
        total += parseInt(a[i]);
    }
    return total;
}



    $('#reservation').daterangepicker({
        locale: {
            format: 'YYYY/MM/DD'
        }
    });




<?php if(session('success')): ?>
        // swal("Success!","<?php echo e(session('success')); ?>","success");
        toastr.success('Your work has been successfully!')

<?php endif; ?>
<?php if(session('error')): ?>
    swal({
            title: "Error!",
            text: "<?php echo e(session('error')); ?>",
            icon: "error",
            button: "OK",
        });
<?php endif; ?>


$('#supplier_name').on('change', function(){
    var id = $(this).val();
    $.ajax({
            url: "<?php echo e(url('/getsupplierbalance')); ?>/"+id ,
            method: "get",
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            cache: false,
            contentType: false,
            processData: false,
            dataType: 'JSON',
            data:{
                "id":id
            },
            success: function (data) {
                // swal("Done!", "It was successfully deleted!", "success");

                    jQuery.each(data, function(key,value){
                        for(i=0; i<value.length; i++){
                            var rid = value[i].id;
                            $('#supbalance').val(value[i].balance);
                            // $("#branch").val(value[i].balance).change();
                        }

                    });

            },
            error: function (jqXHR, textStatus, errorThrown) {
                swal("Warning", "Unable to Process your request", "error");
            },
        });
});
</script>
