
@extends('layouts.admin')
@section('pageTitle', 'Vega Soft')
@section('page-content')



<style>
    .cmn_btn{
        display: none;
    }
#dynamic_ajax_table_length ,.dt-buttons ,#dynamic_ajax_table_filter{
    //float: left;
}
.custom-design-modal{
max-width:70%;
}
.dt-buttons{
    height: 34px;
}
.filter-row{
    margin-bottom: 10px;
}
.custom-table td{
padding: 0px;
}
.table th {
    padding: 0.25rem 0.25rem;
    font-size: smaller;
background-color: #34444c;
color: white;
text-align: right;
}
#tab_html .form-control{
    height: 30px;
padding: 0px;
font-size: small;
text-align: right;
border-radius: unset;
}
.com_th{
    width: 75px;
}


.table-scroll{

  display: block;
  empty-cells: show;

  border-spacing: 0;
  border: 1px solid;
}

.table-scroll thead,.table-scroll tfoot{
  background-color: #f1f1f1;  
  position:relative;
  display: block;
  width:100%;
  overflow-y: scroll;
}

.table-scroll tbody{
  /* Position */
  display: block; position:relative;
  width:100%; overflow-y:scroll;
  /* Decoration */
  border-top: 1px solid rgba(0,0,0,0.2);
}
.table-scroll tr{
  width: 100%;
  display:flex;
}

.table-scroll td,.table-scroll th,.table-scroll th{
  flex-basis:inherit;
  flex-grow:2;
  display: block;
  text-align:left;
}

/* Other options */

.col_fr{
    width: 300px;
}
.body-half-screen{
// height: 420px;
}

</style>

   <input type="hidden" id="table_settings_field" branch_id="" branch_name=""  column_heading="<?= $rows->column_heading;?>" column_width="<?= $rows->column_width;?>" column_alighn="<?= $rows->column_alighn;?>"  column_conversion="<?= $rows->column_conversion;?>" settings_row_id="<?= $id;?>" sp_date_picker="<?= $rows->sp_date_picker;?>"  header_from_date="<?=date('d-m-Y', strtotime('-7 days')); ?>"   header_till_date="<?=date('d-m-Y'); ?>"  sp_sort="<?= $rows->menu_id; ?>"  active_mn="<?= $rows->root;?>"  active_mnt="<?= $rows->menu_type;?>">
    @if ($rows->sedp != "")
  @foreach(explode(',', $rows->sedp) as  $z=>$info) 
     @if ($info == "1" && $z==1)
       <input type="hidden" class="additional_btns" btn_icon="fa fa-pencil" btn_text="Edit" btn_click_function="call_edit_modal" btn_class="btn btn-success btn-sm btn-dtbl" col_width="30" >
     @endif 
     @if ($info == "1" && $z==2)
        <input type="hidden" class="additional_btns" btn_icon="fa fa-trash-o " btn_text="Remove" btn_click_function="call_remove_modal" btn_class="btn btn-danger btn-sm btn-dtbl" col_width="40"  >
     @endif 
     @if ($info == "1" && $z==3)
          <input type="hidden" class="additional_btns" btn_icon="fa fa-print" btn_text="Print" btn_click_function="call_print_function2" btn_class="btn btn-info btn-sm btn-dtbl" col_width="40"  >
     @endif
  @endforeach
@endif



<div class="page-header">
<div class="row align-items-center">
<div class="col">
<h3 class="page-title"><?= $rows->menu_name;?> </h3>
</div>
<div class="col-auto float-end ms-auto"></div>
</div>
</div>
<div class="row ">
<div class="col-sm-6 col-md-1">
<div class="form-group form-focus">
<input type="text" class="form-control floating" id="this_month" onchange="check_m_atten();">
<label class="focus-label">Month</label>
</div>
</div>
<div class="col-sm-6 col-md-1">
<div class="form-group form-focus">
<input type="text" class="form-control floating" id="noday" value="0">
<label class="focus-label">Total Days</label>
</div>
</div>
<div class="col-sm-6 col-md-1">
<div class="form-group form-focus">
<input type="text" class="form-control floating" id="sunday" value="0">
<label class="focus-label"> Sunday</label>
</div>
</div>
<div class="col-sm-6 col-md-1">
<div class="form-group form-focus">
<input type="text" class="form-control floating" id="hday" value="0">
<label class="focus-label"> Holiday</label>
</div>
</div>
<div class="col-sm-6 col-md-1">
<div class="form-group form-focus">
<input type="text" class="form-control floating" id="wday" value="0" readonly>
<label class="focus-label"> Working Day</label>
</div>
</div>
<div class="col-sm-6 col-md-6">
<div class="form-group form-focus select-focus focused">
    
</div>
</div>
<div class="col-sm-6 col-md-1">
<a href="javascript:void(0)" class="btn btn-sm btn-info download-offer cmn_btn" onclick="save_salary_prep();" id="save_btn"><span><i class="fa fa-save me-1" ></i>Save</span></a>
<a href="javascript:void(0)" class="btn btn-sm btn-danger download-offer cmn_btn" onclick="remove_salary_prep();" id="edit_btn"><span><i class="fa fa-trash-o me-1"></i>Remove</span></a>
</div>
</div>

<div class="row">
<div class="col-lg-12">
<div class="table-responsive">
<table class="table table-bordered  custom-table table-nowrap mb-0 table-scroll">
<thead>
<tr>

<th style="text-align: left;" class="col_fr">Employee</th>
<th class="com_th">S.Type</th>
<th class="com_th">Basic Salary</th>
<th class="com_th">Day</th>
<th class="com_th">Gross Salary</th>
<th class="com_th">OT Day</th>
<th class="com_th">OT Wages</th>
<th class="com_th">Incentive</th>
<th class="com_th">Total Additions</th>
<th class="com_th">Total Salary</th>
<th class="com_th">Deductions</th>
<th class="com_th">Payable</th>
<th class="com_th">Advance</th>
<th class="com_th">Advance Adjusted</th>
<th class="com_th">Net Payable</th>

</tr>
</thead>
<tbody id="tab_html" class="body-half-screen">
    
</tbody>
<tfoot>

    <th  class="col_fr">Total</th>
    <th  class="com_th"></th>
    <th  class="com_th" id="tbsalary"></th>
    <th  class="com_th"></th>
    <th  class="com_th " id="tgsalary"></th>
    <th  class="com_th"></th>
    <th  class="com_th " id="tot_wage"></th>
    <th  class="com_th " id="tincentive"></th>
    <th  class="com_th " id="ttot_addon"></th>
    <th  class="com_th " id="ttsalary"></th>
    <th  class="com_th " id="tdedection"></th>
    <th  class="com_th " id="tpayable"></th>
    <th  class="com_th " id="tadvance"></th>
    <th  class="com_th " id="tadvance_adj"></th>
    <th  class="com_th " id="tnpayable"></th>
</tfoot>
</table>
</div>
</div>
</div>


<input type="hidden" id="hidden_status" value="0">
<input type="hidden" id="row_id" value="0">
<input type="hidden" id="prep" value="">
<input type="hidden" id="serverValue2" value="">



@endsection




@section('page-script')
<script src="{{ asset('project-js\vega-front-scripts.js')}}"></script>
<script type="text/javascript">

//fetch_view_table();
   $(function() {

$( "#toggle_btn" ).trigger( "click" );
set_mn();
set_month();
  check_m_atten();
    });

  $('#this_month').MonthPicker({ Button: false , MonthFormat: 'M, yy', AltFormat: 'yy-mm-dd', // ODBC time stamp.
        AltField: '#serverValue2',
 OnAfterChooseMonth: function() { 
       check_m_atten();
       // get_m_atten();
    } 
    });
function set_month() {
  var date = new Date();
  $('#this_month').val(moment(date).format('MMM,YYYY'));
  $('#serverValue2').val(moment(date).format('YYYY-MM')+'-01');

       //
        calc_sal_details();
      //  alert( $('#serverValue2').val());
}


function check_m_atten() {
        $.ajax({
              type: 'POST',
               async: false,
                url: '/get_data',
                data: {
                     '_token': "{{ csrf_token() }}",
                    'cid':"{{ Session::get('cid')}}",
                     'b_id': "{{ Session::get('b_id')}}",
              'CID':"{{ Session::get('CID')}}",
                     row_id: $.trim($('#serverValue2').val()),
              url1:'prl_check_sal_prep.php'
                },
                error: function (request, error) {
                    alert("Please Wait, Loading...");
                },
                success: function (response) {
$('#noday').val(response.edit[0]['ndays']);
$('#sunday').val(response.edit[0]['wcount']);
$('#hday').val(response.edit[0]['hday']);
$('#prep').val(response.edit[0]['prep']);
if(response.edit[0]['prep']==0){
    calc_work_days();
$('#save_btn').show();
$('#edit_btn').hide();
}else{
$('#edit_btn').show();
$('#save_btn').hide();

}


 get_m_atten();

        }
          
     });  
}
$(document).on('keyup','#noday,#sunday,#hday,#wday',function() {
    if(this.value==''){
        this.value=0;
    }
    calc_work_days();
});
function calc_work_days(){
  noday =$('#noday').val();
sunday=$('#sunday').val();
hday=$('#hday').val();
tday=parseFloat(noday)-(parseFloat(sunday)+parseFloat(hday));
if(tday<0) tday=0;
$('#wday').val(tday);
$('.gwday').val(tday);
calc_sal_details();
}
function get_m_atten() {
     arr={
         'row_id': $('#serverValue2').val(),
};
        $.ajax({
              type: 'POST',
               async: false,
                url: '/get_data',
                data: {
                     '_token': "{{ csrf_token() }}",
                    'cid':"{{ Session::get('cid')}}",
                    'CID': "{{ Session::get('CID')}}",
                    'b_id': "{{ Session::get('b_id')}}",
                     row_id: JSON.stringify(arr),
              url1:'prl_get_sal_prep.php'
                },
                error: function (request, error) {
                    alert("Please Wait, Loading...");
                },
                success: function (response) {
emp_list=response.edit;
main=response.main;
tab='';
   wday=$('#wday').val();
$(emp_list).each(function(i,val) {
if($('#prep').val()>0){
    wday=val['gwday'];
    $('#noday').val(main[0]['sal_tot_day']);
    $('#sunday').val(main[0]['sal_sunday']);
    $('#hday').val(main[0]['sal_holiday']);
    $('#wday').val(main[0]['sal_work_day']);
}
tr="'tr_id"+i+"'";

   tab+='<tr id="tr_id'+i+'" count="'+i+'" class="tr_row">';

tab+='<td class="col_fr emp_name" emp_id="'+val['emp_id']+'">'+val['emp_name']+'</td>';

tab+='<td class="com_th Monthly"  Monthly="'+val['Monthly']+'">'+val['Monthly_text']+'</td>';
tab+='<td class="com_th bsalary">'+val['salary']+'</td>';
tab+='<td class="com_th" ><input class="form-control focusable vmn_row'+i+' cmn_select allownumbers gwday" autocomplete="off" type="text" id="" value="'+wday+'" onkeyup="calc_sal_details();" onkeypress="return isNumber(event, this.value);"></td>';
tab+='<td class="com_th" ><input class="form-control focusable vmn_row'+i+' cmn_select gsalary" autocomplete="off" type="text" id="" value="'+val['gsalary']+'" readonly></td>';
tab+='<td class="com_th" ><input class="form-control focusable vmn_row'+i+' cmn_select allownumbers otday" onkeyup="calc_ot('+tr+');" autocomplete="off" type="text" id="" value="'+val['ot_day']+'"></td>';
tab+='<td class="com_th" ><input class="form-control focusable vmn_row'+i+' cmn_select ot_wage" onkeyup="calc_addon('+tr+');" autocomplete="off" type="text" id="" value="'+val['ot_wage']+'"  onkeypress="return isNumber(event, this.value);"></td>';
tab+='<td class="com_th" ><input class="form-control focusable vmn_row'+i+' cmn_select incentive" onkeyup="calc_addon('+tr+');" autocomplete="off" type="text" id="" value="'+val['incentive']+'"  onkeypress="return isNumber(event, this.value);"></td>';
tab+='<td class="com_th" ><input class="form-control focusable vmn_row'+i+' cmn_select tot_addon" autocomplete="off" type="text" id="" value="'+val['tot_addon']+'" readonly></td>';
tab+='<td class="com_th" ><input class="form-control focusable vmn_row'+i+' cmn_select tsalary" autocomplete="off" type="text" id="" value="'+val['tsalary']+'" readonly></td>';
tab+='<td class="com_th" ><input class="form-control focusable vmn_row'+i+' cmn_select dedection" autocomplete="off" type="text" id="" value="'+val['deduction']+'" onkeyup="calc_addon('+tr+');" onkeypress="return isNumber(event, this.value);"></td>';
tab+='<td class="com_th" ><input class="form-control focusable vmn_row'+i+' cmn_select payable" autocomplete="off" type="text" id="" value="'+val['payable']+'" readonly></td>';
tab+='<td class="com_th" ><input class="form-control focusable vmn_row'+i+' cmn_select advance" autocomplete="off" type="text" id="" value="'+val['advance']+'" readonly ></td>';
tab+='<td class="com_th" ><input class="form-control focusable vmn_row'+i+' cmn_select advance_adj" autocomplete="off" type="text" value="'+val['advance_adj']+'"  readonly></td>';
tab+='<td class="com_th" ><input class="form-control focusable vmn_row'+i+' cmn_select npayable" autocomplete="off" type="text" id="" value="'+val['npayable']+'" readonly></td>';

});
$('#tab_html').html(tab);
$( ".cmn_amt" ).trigger( "blur" );
if($('#prep').val()==0){
calc_sal_details();
}
        }
          
     });  
}

function generate_monthly_attn() {
  $('.tr_row').each(function(){
    cnt=$(this).attr('count');
  });
}
$(document).on("focus", ".cmn_select", function() {
      if(this.value=='' || this.value=='0'){
    this.value='0.00';
    this.select();
   }
});
$(document).on("blur", ".cmn_amt", function() {
       if(this.value=='' || this.value=='0'){
    this.value='0.00';
    this.select();
   }
});

$('.allownumbers').keyup(function () {
   this.value = this.value.replace(/[^0-9\.]/g,'');
   if(this.value==''){
    this.value=0;
    this.select();
    $(this).trigger('keyup');
   }
});


function calc_sal_details(){
    $('.tr_row').each(function() {
     noday =parseFloat($('#noday').val());
     sunday=$('#sunday').val();
     hday=$('#hday').val();
     bsalary=parseFloat($(this).find('.bsalary').html());
     gwday=$(this).find('.gwday').val();
     if(gwday=='') gwday=0;
     perday=bsalary/noday;
     tday=(parseFloat(sunday)+parseFloat(hday)+parseFloat(gwday));
     gsalary=perday*tday;
      console.log(tday+'-'+perday);
     $(this).find('.gsalary').val(gsalary.toFixed(2));
     tr_id=$(this).attr('id');
     calc_addon(tr_id);
     
    })
}
function calc_ot(tr_id) { 
     noday =parseFloat($('#noday').val());
      bsalary=parseFloat($('#'+tr_id).find('.bsalary').text());
       perday=bsalary/noday;
          otday= $('#'+tr_id).find('.otday').val();     
       if(otday=='') otday=0;
       ot_wage=parseFloat(otday)*perday;
    
    $('#'+tr_id).find('.ot_wage').val(ot_wage.toFixed(2)); 
    calc_addon(tr_id,0);
}
function calc_addon(tr_id){ 
   
       ot_wage=$('#'+tr_id).find('.ot_wage').val();
       gsalary=$('#'+tr_id).find('.gsalary').val();
       dedection=$('#'+tr_id).find('.dedection').val();
       advance=$('#'+tr_id).find('.advance').val();
       advance_adj=$('#'+tr_id).find('.advance_adj').val();
       incentive=$('#'+tr_id).find('.incentive').val(); //console.log($(' #tab_html #'+tr_id).html());
       if(incentive=='') incentive=0; 
       if(ot_wage=='') ot_wage=0; 
       if(gsalary=='') gsalary=0; 
       if(advance_adj=='') advance_adj=0; 
       if(advance=='') advance=0; 
       if(dedection=='') dedection=0; 
    tot_addon=parseFloat(ot_wage)+parseFloat(incentive);
    $('#'+tr_id).find('.tot_addon').val(tot_addon.toFixed(2)); 

    tsalary=parseFloat(gsalary)+parseFloat(tot_addon);
     $('#'+tr_id).find('.tsalary').val(tsalary.toFixed(2));
    payable=parseFloat(tsalary)-parseFloat(dedection);
     $('#'+tr_id).find('.payable').val(payable.toFixed(2));
    npayable=parseFloat(payable)-parseFloat(advance_adj);
     $('#'+tr_id).find('.npayable').val(npayable.toFixed(2));
     sum_col();
}
function sum_col(){
    bsalary=0;
    gsalary=0;
    ot_wage=0;
    incentive=0;
    tot_addon=0;
    dedection=0;
    tsalary=0;
    payable=0;
    advance=0;
    advance_adj=0;
    npayable=0;
    $('.tr_row').each(function() {
        bsalary+=parseFloat($(this).find('.bsalary').html());
        gsalary+=parseFloat($(this).find('.gsalary').val());
        ot_wage+=parseFloat($(this).find('.ot_wage').val());
        incentive+=parseFloat($(this).find('.incentive').val());
        tot_addon+=parseFloat($(this).find('.tot_addon').val());
        tsalary+=parseFloat($(this).find('.tsalary').val());
        dedection+=parseFloat($(this).find('.dedection').val());
        payable+=parseFloat($(this).find('.payable').val());
        advance+=parseFloat($(this).find('.advance').val());
        advance_adj+=parseFloat($(this).find('.advance_adj').val());
        npayable+=parseFloat($(this).find('.npayable').val());
    });
   
    $('#tbsalary').html(bsalary);
    $('#tgsalary').html(gsalary);
    $('#tot_wage').html(ot_wage);
    $('#tincentive').html(incentive);
    $('#ttot_addon').html(tot_addon);
    $('#ttsalary').html(tsalary);
    $('#tdedection').html(dedection);
    $('#tpayable').html(payable);
    $('#tadvance').html(advance);
    $('#tadvance_adj').html(advance_adj);
    $('#tnpayable').html(npayable);
}
h=$(document).height();
wh=h-240; //alert(wh);
$('.body-half-screen').css('height',wh+'px');

function save_salary_prep() { 
       allo=[];
          $(".tr_row").each(function(){
           tarray={
              'emp_name':$(this).find('.emp_name').html(),
              'emp_id':$(this).find('.emp_name').attr('emp_id'),
              'Monthly':$(this).find('.Monthly').attr('Monthly'),
              'bsalary':$(this).find('.bsalary').html(),
              'gwday':$(this).find('.gwday').val(),
              'gsalary':$(this).find('.gsalary').val(),
              'ot_wage':$(this).find('.ot_wage').val(),
              'ot_day':$(this).find('.otday').val(),
              'incentive':$(this).find('.incentive').val(),
              'tot_addon':$(this).find('.tot_addon').val(),
              'deduction':$(this).find('.dedection').val(),
              'tsalary':$(this).find('.tsalary').val(),
              'payable':$(this).find('.payable').val(),
              'advance':$(this).find('.advance').val(),
              'advance_adj':$(this).find('.advance_adj').val(),
              'npayable':$(this).find('.npayable').val(),
          }
          allo.push(tarray);
      });
     arr={
               
               'cid':"{{ Session::get('cid')}}",
              'b_id': "{{ Session::get('b_id')}}",
              'CID':"{{ Session::get('CID')}}",
              'added_by':"{{ Session::get('user_id')}}",

              'sal_month':$.trim($('#serverValue2').val()),
              'sal_tot_day':$.trim($('#noday').val()),
              'sal_sunday':$.trim($('#sunday').val()),
              'sal_holiday':$.trim($('#hday').val()),
              'sal_work_day':$.trim($('#wday').val()),
              'tbsalary':$('#tbsalary').html(),
              'tgsalary':$('#tgsalary').html(),
              'tot_wage':$('#tot_wage').html(),
              'tincentive':$('#tincentive').html(),
              'ttot_addon':$('#ttot_addon').html(),
              'tdedection':$('#tdedection').html(),
              'ttsalary':$('#ttsalary').html(),
              'tpayable':$('#tpayable').html(),
              'tadvance':$('#tadvance').html(),
              'tadvance_adj':$('#tadvance_adj').html(),
              'tnpayable':$('#tnpayable').html(),
              'allo':allo
};


 $.ajax({

          url: "/store_data",
          type: "POST",
          data:{
             '_token': "{{ csrf_token() }}",
              json_data: JSON.stringify(arr),
              url1:'prl_save_sal_prep.php'
             
          },
          success:function(response){
            
            console.log(response);
            if (response) {
              alert(response['msg']);
            
               $('.modal').modal('hide');
               fetch_view_table();
            }else{

            }
          },
          error: function(response) {
          }
         });

}
function remove_salary_prep() {
           arr={
               
               'cid':"{{ Session::get('cid')}}",
              'b_id': "{{ Session::get('b_id')}}",
              'company_id':"{{ Session::get('CID')}}",
              'added_by':"{{ Session::get('user_id')}}",

              'sal_month':$.trim($('#serverValue2').val())
};
    $.ajax({
          url: "/store_data",
          type: "POST",
          data:{
             '_token': "{{ csrf_token() }}",
              json_data: JSON.stringify(arr),
              url1:'prl_remove_sal_prep.php'
             
          },
          success:function(response){
            
            console.log(response);
            if (response) {
              alert(response['msg']);
            
               $('.modal').modal('hide');
               fetch_view_table();
            }else{

            }
          },
          error: function(response) {
          }
         });
}
</script>
@endsection