asked on October 11, 2017

I am having an issue with a form that has Javascript enabled.  I had to write a script to build a table that acts as a weekly time sheet with default times for M-F with Sat and Sun defaulting to no times.  This also includes sections for each day of the week for Overtime, Vacation, Holiday, and PTO hours.  The final column in each row adds up the daily total.  This is part is working.  (The additional hours fields currently do not calculate based on hours and minutes, just hours.  I will be addressing that part in the future.)

The trouble is any fields outside of this primary table.  I am trying to calculate weekly totals for each field but unable to get this to work properly.  The built in SUM() function will not total these rows nor can I seem to write out to a field outside of the table.  (Such as the Single Line Over Time Total field that I designated as .totalOT in CSS.)  I am fairly sure that the built-in functions will not work since I am dealing with times and other data types that are not numbers, is that correct?  If so, I will need to do the calculations within Javascript and until I can figure out why I cannot write outside of the able, I am quite stuck.  Any help would be appreciated since I am new to Javascript.

 

$(document).ready(function () {
  
   $.getScript('http://tbs-lfweb/Forms/js/moment.js');
	
   $('.cf-table-block').on('blur', 'input', tableCalculations);
   $(document).on('click', '.form-del-field', tableCalculations);

	
  function tableCalculations(){
    $('.totalOT input').val('test');
  	
    var dayCount = 0; // for day of the week loop
    var startTime
    var endTime
    var regularHours  // hours worked in a day
    var totalTime = 0; // total of all times types in a given day
    var	OverTimeHours;
    var HolidayHours = 0;
    var VacationHours = 0;
    var PTOHours = 0;
  	var holdOT = 0; // variable to pull hours out of time wrapped field - Over Time
    var holdHoliday = 0; // variable to pull hours out of time wrapped field - Holiday
    var holdVacation = 0; // variable to pull hours out of time wrapped field - Vacation
    var holdPTO = 0;  // variable to pull hours out of time wrapped field - Paid Time Off

    
           
 	$('.cf-table-block tbody tr').each(function(){
      
        // fill in days of the week
      	if (dayCount == 0) {   
        	$(this).find('.day input').val('Sunday'); 
          	$(this).find('.startTime input').val('0:00');
          	$(this).find('.endTime input').val('0:00');
          	dayCount++;
        } // end Sunday default data
        else if (dayCount == 1) { $(this).find('.day input').val('Monday'); dayCount++;}
      	else if (dayCount == 2) { $(this).find('.day input').val('Tuesday'); dayCount++;}
      	else if (dayCount == 3) { $(this).find('.day input').val('Wednesday'); dayCount++;}
      	else if (dayCount == 4) { $(this).find('.day input').val('Thursday'); dayCount++;}
      	else if (dayCount == 5) { $(this).find('.day input').val('Friday'); dayCount++;}
      	else if (dayCount == 6) { 
          	$(this).find('.day input').val('Saturday');
        	$(this).find('.startTime input').val('0:00');
          	$(this).find('.endTime input').val('0:00');
            dayCount++;
        } // end Saturday defualt data
   	    else $(this).find('.day input').val('other');
   
      	// get user input as moment.js items
      	startTime = moment($(this).find('.startTime input').val(), 'HHmm');
        endTime = moment($(this).find('.endTime input').val(), 'HHmm');
        OverTimeHours = moment($(this).find('.OverTimeHours input').val(), 'HHmm');        
      	HolidayHours = moment($(this).find('.HolidayHours input').val(), 'HHmm');     
        VacationHours = moment($(this).find('.VacationHours input').val(), 'HHmm');
        PTOHours = moment($(this).find('.PTOHours input').val(), 'HHmm');
        
      
        regularHours = endTime.diff(startTime, 'hours', true); // calculate hours worked
        if (regularHours != 0) {regularHours = regularHours - 1; } // reduce time for lunch hour
        $(this).find('.regularHours input').val(regularHours.toFixed(2)); // write out hours worked
   
      	// pull hours only from moment.js items
        holdOT = OverTimeHours.hours();
      	holdHoliday = HolidayHours.hours();
      	holdVacation = VacationHours.hours();
      	holdPTO = PTOHours.hours();
      
      	// convert from moment.js hours to numbers
        var tempOT = Number(holdOT);
      	var tempHoliday = Number(holdHoliday);
      	var tempVacation = Number(holdVacation);
      	var tempPTO = Number(holdPTO);
      	var tempTotal = Number(regularHours);
      
      
      	totalTime = tempOT + tempHoliday + tempVacation + tempPTO;  // total additional hours
      	totalTime = totalTime + tempTotal; // final daily total
      	
   		// write out final daily total to user
        $(this).find('.total input').val(totalTime.toFixed(2));  
      	
      	$(this).find('.totalOT input').val(totalTime.toFixed(2)); // <- won't write out
 	}); // end loop
     
   
    
  } // end tableCalculations
}) // end function


0 0