Questions and discussion about developing processes and programming in PHP, JavaScript, web services & REST API.
Forum rules: Please search to see if a question has already asked before creating a new topic. Please don't post the same question in multiple forums.
User avatar
By ebryant
#821988
I'm trying to populate a field on a dynaform that would calculate the number of hours an employee works for two weeks. The formula will need to subtract lunch times from the total. The fields are as follows for Day 1: start, lunchOut, lunchIn and end...these are datetime controls. sick, annual, otherLeave and compTime are floats. The issue is converting types, figuring out a formula and can an array be created to do this for two work weeks?

function calculateDay1(){
var startTime = parsFloat($('#start').getValue());
var lunchTime = parsFloat($('#lunchOut').getValue());
var lunchReturn = parsFloat($('#lunchIn').getValue());
var endTime = parsFloat($('#end').getValue());
var sickTime = $('#sick').getValue();
var annualTime = $('#annual').getValue();
var otherTime = $('#otherLeave').getValue();
var compTimeOut = $('#compTime').getValue();
User avatar
By amosbatto
#821991
You can use moment.js to get the difference between two datetimes. See this example:
https://www.pmusers.com/index.php/Diffe ... _datetimes

Since you didn't explain the time units that you are using the last four fields or the purpose of those fields, I can't tell you how to convert them, but here would be the code to add the number minutes worked in the day:
Code: Select all
function workTimeInDay() {
  var startTime   = moment( $('#start').getValue()    );
  var lunchTime   = moment( $('#lunchOut').getValue() ); 
  var lunchReturn = moment( $('#lunchIn').getValue()  );
  var endTime     = moment( $('#end').getValue()      );
  
  if (!startTime.isValid()) {
    alert(Enter a valid time in the 'Start Time' field");
    return false;
  }  
  if (!lunchTime.isValid()) {
    alert(Enter a valid time in the 'Lunch Out' field");
    return false;
  }         
  if (!lunchReturn.isValid()) {
    alert(Enter a valid time in the 'Lunch Return' field");
    return false;
  }
  if (!endTime.isValid()) {
    alert(Enter a valid time in the 'End Time' field");
    return false;
  }
          
  var minutesInDay = moment.duration( lunchTime.diff(startTime) ).asMinutes() +
      moment.duration( endTime.diff(lunchReturn) ).asMinutes();
      
  var sickTime   = parseFloat( $('#sick').getValue() );
  var annualTime = parseFloat( $('#annual').getValue() );
  var otherTime  = parseFloat( $('#otherLeave').getValue() );
  var compTimeOut= parseFloat( $('#compTime').getValue() );     
}   


var formId = $("form").prop("id");
$("#"+formId).setOnSubmit( function() {
  workTimeInDay();
}  
 
You can divide minutesInDay by 60 to get hours.
User avatar
By ebryant
#822003
Thank you Amos,

The four other variables are for the instance a user does not work a full day or if out on vacation, he/she could use annual/sick/comp/other for that time.

An example would be a user may work from 9am (#start) go to lunch at 1pm (#lunchOut) return from lunch at 2pm (#lunchIn) get sick, leave at 4pm (#end) and use 1 hour (#sick) to make up the difference.

Thanks
User avatar
By amosbatto
#822005
Let's assume that "sickTime" is a decimal number that represents hours, so 10.25 represents 10 hours and 15 minutes. Then multiple by 60 to get minutes and subtract from minutesInDay.
The code would be:
Code: Select all
  var minutesInDay = moment.duration( lunchTime.diff(startTime) ).asMinutes() +
      moment.duration( endTime.diff(lunchReturn) ).asMinutes();
      
  var sickTimeMinutes  = Math.floor( parseFloat( $('#sick').getValue() ) * 60 );
  if (isNaN(sickTimeMinutes)) {
    alert(Enter a valid time in the 'Sick Time' field");
    return false;
  }

  minutesInDay = minutesInDay + sickTimeMinutes;
  hoursInDay = minutesInDay / 60;
  extraMinutesInDay = minutesInDay % 60;
  alert("Worked "+hoursInDay+":"+extraMinutesInDay+" in day.");
User avatar
By ebryant
#822011
Thanks again.

One thing...I have a field (#total1) which I would like to be populated with the results. I've tried $('#total1').getvalue(minutesInDay) to dynamically show the compulations as the user inputs the data with no results. There will then be a field which will sum all of the totals for the week.

Your help is greatly appreciated.
User avatar
By amosbatto
#822013
You need to read ProcesMaker's JavaScript documentation.

To set the value in a field:
Code: Select all
$('#total1').setValue(minutesInDay);
To execute code when the value of a field changes:
Code: Select all
function workTimeInDay(newVal, oldVal) {
    //add your code here
}

$('#start').setOnchange(workTimeInDay);
$('#lunchOut').setOnchange(workTimeInDay);
$('#lunchIn').setOnchange(workTimeInDay);
$('#end').setOnchange(workTimeInDay);

A 1xbet clone script is a pre-designed software so[…]

4rabet clone script is enabling entrepreneurs to e[…]

Parimatch clone script is enabling entrepreneurs t[…]

In the world of cryptocurrency, a wallet is an app[…]