
<?php $__env->startSection('title','Receiving Voucher'); ?>
<?php $__env->startSection('main'); ?>

<div class="container d-flex justify-content-center mt-3">    
    <div class="card text-left" style="width: 50rem;">
        <div class="card-header">
            Add New Record
        </div>
        <form  id="Rvoucher">

        <div class="card-body">
            <h5 class="card-title pb-2">Receiving Voucher</h5>
            <!-- Voucher ID -->
            <div class="row g-1 mb-3">
                <div class="col-6">
                    <input type="text" class="form-control  rounded-0" placeholder="Voucher ID#" id="voucher_id" disabled readonly>
                </div>
                <div class="col-6">
                <input type="text"  class="rounded-0 form-control" name="manual_no" placeholder="Manual Voucher ID#">
                </div> 
            </div>
            <!-- Date - Project -->
            <div class="row g-1 mb-3">
                <div class="col-4">
                <input type="date" value="<?php echo e(now()->format('Y-m-d')); ?>" class="form-control rounded-0" name="date">
                <span class="text-danger" id="error_date"></span>
                </div>
                <div class="col-2">
                <input type="text" id="projectid" name="projectid" value="<?php echo e(old('projectid')); ?>" class="form-control rounded-0" placeholder="Project ID">      
                <span class="text-danger" id="error_projectid"></span>
                </div>
                <div class="col-6">
                    
                <input list="project_name_options" id="project_name" name="project_name" placeholder="Project Name"  autocomplete="off" class="form-control">
                
                        <datalist id="project_name_options">
                                <?php $__currentLoopData = $projectdata; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $data): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option data-id="<?php echo e($data->id); ?>" value="<?php echo e($data->project_name); ?>"> 
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </datalist>
                        

                </div> 

            </div>     
            <!-- Account Name and ID        -->
            <div class="row g-1 mb-3">

                <div class="col-3">
                    <input type="text" id="headercode" value="<?php echo e(old('headercode')); ?>" name="headercode" class="form-control rounded-0" placeholder="Header Code">
                    <span class="text-danger" id="error_headercode"></span>
                </div>
                <div class="col-3">
                    <input type="text" id="accountid" value="<?php echo e(old('accountid')); ?>"name="accountid" class="form-control rounded-0" placeholder="Account ID">
                     <span class="text-danger" id="error_accountid"></span>
                </div>
                <div class="col-6">
                    
                        <input list="account_name_options" id="account_name" placeholder="Account Name" name="account_name" autocomplete="off" class="form-control">
                        
                        <datalist id="account_name_options">
                        </datalist>
                        
                        <span class="text-danger">
                            <?php $__errorArgs = ['accountid'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                            <p>Please select your Account</p>
                            <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                        </span>
                </div> 

            </div>

            <!-- Total amount -->
            <div class="row g-1">

                <div class="col-6 mb-3 ">
                <input type="text" class="form-control rounded-0" value="<?php echo e(old('amount')); ?>" name="amount" placeholder="Total Amount" onchange="inWords(this.value);">
                <p id="inword"></p>
                </div>
                <div class="col-6 md-3">
                <input type="text" class="form-control rounded-0" name="desc" placeholder="Description" value="<?php echo e(old('desc')); ?>"> 
                </div>

            </div>  


        
                <button type="button" id="savebtn" class="btn btn-sm btn-primary " style="width:20%;">Save/Close</button>  
                <a href="<?php echo e(route('Rvoucher.add')); ?>" class="btn btn-sm btn-success" style="width:20%;">Add New</a>        
                <a href="<?php echo e(route('Rvoucher.index')); ?>" class="btn btn-sm btn-warning" style="width:20%;">Cancel/Close</a>
                <a onclick="print()" class="btn btn-info btn-sm " style="width:20%;">Print</a>           

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

$("input,select").bind("keydown", function(event) {
    if (event.which === 13) {
        event.stopPropagation();
        event.preventDefault();
       $(':input:eq(' + ($(':input').index(this) + 1) +')').focus();
    }
});



$('#project_name').change(function(){

    $("#account_name_options").empty();
    var value = $('#project_name').val();
    projectid = $('#project_name_options [value="' + value + '"]').data('id');
    $('#projectid').val( projectid );
    
    
    

     //append into account dropdown start here
     var pid=jQuery(this).val();
            $.ajax({
                url: '<?php echo e(route("Rvoucher.accounts")); ?>',
                type: 'POST',
                data: 'projectid='+projectid+'&_token=<?php echo e(csrf_token()); ?>',

                success:function(result){
                    
                        $.each(result.accounts, function (key, value) {
                            $("#account_name_options").append('<option data-headercode="' + value.header_code + '" data-accountid="' + value.id + '" value="' + value.account_name + '">' + value.account_name + '</option>');
                        });

                }
            });
        //end 
    
});

function print(){
    
    var id = $('#voucher_id').val();
    
    if (id > 0){
        
       
        return;
    }
    

    var url = "http://taha-kashif-builders-sukkur.pk/Rvoucher/show/" + id;
    window.open(url);
}

function inWords (num) {

    var a = ['','One ','Two ','Three ','Four ', 'Five ','Six ','Seven ','Eight ','Nine ','Ten ','Eleven ','Twelve ','Thirteen ','Fourteen ','Fifteen ','Sixteen ','Seventeen ','Eighteen ','Nineteen '];
    var b = ['', '', 'Twenty','Thirty','Forty','Fifty', 'Sixty','Seventy','Eighty','Ninety'];

    if ((num = num.toString()).length > 9) return 'overflow';
    n = ('000000000' + num).substr(-9).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})$/);
    if (!n) return; var str = '';
    str += (n[1] != 0) ? (a[Number(n[1])] || b[n[1][0]] + ' ' + a[n[1][1]]) + 'Crore ' : '';
    str += (n[2] != 0) ? (a[Number(n[2])] || b[n[2][0]] + ' ' + a[n[2][1]]) + 'Lakh ' : '';
    str += (n[3] != 0) ? (a[Number(n[3])] || b[n[3][0]] + ' ' + a[n[3][1]]) + 'Thousand ' : '';
    str += (n[4] != 0) ? (a[Number(n[4])] || b[n[4][0]] + ' ' + a[n[4][1]]) + 'Hundred ' : '';
    str += (n[5] != 0) ? ((str != '') ? 'and ' : '') + (a[Number(n[5])] || b[n[5][0]] + ' ' + a[n[5][1]]) : '';

    $('#inword').val(str);
    
    
    $('#inword').empty();
                
    document.getElementById("inword")
                .innerHTML += str;

    
}






$('#account_name').change(function(){


    var value = $('#account_name').val();
    accountid = $('#account_name_options [value="' + value + '"]').data('accountid');
    headercode = $('#account_name_options [value="' + value + '"]').data('headercode');
    
    
    

    $('#headercode').val( headercode );
    $('#accountid').val( accountid );
    //alert(headercode + accountid);

});


$('#savebtn').click(function(){
    
    var lastid = $('#voucher_id').val();
    
    console.log(lastid);
    if (lastid > 0){
        
        return;
    }
    
    var form = $('#Rvoucher')[0];
    var formdata = new FormData(form);
    
    

    
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

    $.ajax({
                url: '<?php echo e(route("Rvoucher.add")); ?>',
                type: 'POST',
                processData: false, contentType: false,
                data: formdata,

                success: function(result){
                    
                            $('#voucher_id').val(result);

                },
                error: function(error){
                    if(error){
                        $('#error_projectid').html(error.responseJSON.errors.projectid);
                        $('#error_date').html(error.responseJSON.errors.date);
                        $('#error_headercode').html(error.responseJSON.errors.headercode);
                        $('#accountid').html(error.responseJSON.errors.accountid);
                    }
                }
            });


});

</script>

<?php $__env->stopSection(); ?>


<?php echo $__env->make('header', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /home/tahakash/public_html/resources/views/Rvoucher/add.blade.php ENDPATH**/ ?>