0 Replies Latest reply on Nov 13, 2014 11:04 AM by Timesheet Calculations

    NEED HELP WITH PDF JAVASCRIPT

    Timesheet Calculations

      I have created a PDF form that I need help with solving a few minor issues. If an employee needs out time is after midnight I need for it to calculate correctly. I would also like for 12:00 AM to appear as 12:00AM and not 00:00.

       

      function CalculateDay( myDay ) {

      var myColumn

       

      var myDayError = new Boolean
      var myTotalWork = new Boolean
      var myDayLeaveError = new Boolean
      var myTotalHours = new Boolean

       

      var myInAndOut = new Array(6)
      var myInAndOutTime = new Array(6)

       

      var myLeaveColumns = new Array("Vacation", "Sick", "Other")
      var myLeaveColumn
      var myLeave

       

      myDayError = false
      myTotalWork = 0

      for (myColumn = 1; myColumn <= 6; myColumn++) {
       
        myInAndOut[myColumn] = this.getField("Day" + myDay + "Column" + myColumn).value

        if (myInAndOut[myColumn].length > 0) {

         myInAndOutTime[myColumn] = new Date()

         myInAndOutTime[myColumn].setSeconds(0)
         myInAndOutTime[myColumn].setMilliseconds(0)


         if (myInAndOut[myColumn].charAt(1) == ":") {
          myInAndOutTime[myColumn].setHours(myInAndOut[myColumn].substring(0,1))
          myInAndOutTime[myColumn].setMinutes(myInAndOut[myColumn].substring(2,4))
         }
         else if (myInAndOut[myColumn].charAt(2) == ":") {
          myInAndOutTime[myColumn].setHours(myInAndOut[myColumn].substring(0,2))
          myInAndOutTime[myColumn].setMinutes(myInAndOut[myColumn].substring(3,5))
         }

         if (myInAndOut[myColumn].substring(myInAndOut[myColumn].length - 2, myInAndOut[myColumn].length).toLowerCase() == "pm" ||
             myInAndOut[myColumn].substring(myInAndOut[myColumn].length - 2, myInAndOut[myColumn].length).toLowerCase() == "am" ||
             myInAndOut[myColumn].substring(myInAndOut[myColumn].length - 1, myInAndOut[myColumn].length).toLowerCase() == "p" ||
             myInAndOut[myColumn].substring(myInAndOut[myColumn].length - 1, myInAndOut[myColumn].length).toLowerCase() == "a") {
             if (myInAndOut[myColumn].substring(0,2) == 12) {
           myInAndOutTime[myColumn].setHours(24)
          }
          if (myInAndOut[myColumn].substring(myInAndOut[myColumn].length - 2, myInAndOut[myColumn].length).toLowerCase() == "pm" ||
           myInAndOut[myColumn].substring(myInAndOut[myColumn].length - 1, myInAndOut[myColumn].length).toLowerCase() == "p") {
           myInAndOutTime[myColumn] = new Date(myInAndOutTime[myColumn].valueOf() + 12 * 60 * 60 * 1000)
          }
         }

         if ( (myColumn == 2 || myColumn == 4 || myColumn == 6) && myInAndOutTime[myColumn].getHours() == 0  && myInAndOutTime[myColumn].getMinutes() == 0 ) {
          myInAndOutTime[myColumn] = new Date(myInAndOutTime[myColumn].valueOf() + 24 * 60 * 60 * 1000)
          console.println (myInAndOutTime[myColumn])
         }


        }
      }

      for (myColumn = 1; myColumn <= 5; myColumn += 2) {
        if (myInAndOut[myColumn].length > 0 && myInAndOut[myColumn + 1].length > 0) {
         if (myInAndOutTime[myColumn + 1].valueOf() > myInAndOutTime[myColumn].valueOf()) {
          myTotalWork += ((myInAndOutTime[myColumn + 1].valueOf() - myInAndOutTime[myColumn].valueOf()) / 60 / 1000)
         }
         else {
          myDayError = true
         }
        }
      }

      for (myColumn = 2; myColumn <= 4; myColumn += 2) {
        if (myInAndOut[myColumn].length > 0 && myInAndOut[myColumn + 1].length > 0) {
         if (myInAndOutTime[myColumn].valueOf() > myInAndOutTime[myColumn + 1].valueOf()) {
          myDayError = true
         }
        }
      }

      if (myDayError == false) {
           myTotalWork = Math.round(myTotalWork / 60 * 4) / 4
           if (myTotalWork > 0) {
         this.getField("Day" + myDay + "TotalWork").value = myTotalWork.toFixed(2)
        }
        else {
         this.getField("Day" + myDay + "TotalWork").value = ""
        }
       
        myDayLeaveError = false

        myTotalHours = myTotalWork

        for (myLeaveColumn = 0; myLeaveColumn < myLeaveColumns.length; myLeaveColumn++) {
         myLeave = this.getField("Day" + myDay + myLeaveColumns[myLeaveColumn]).value

         if (myLeave.toString().length > 0) {
          if (typeof(myLeave) == "number") {
           if (myLeave >= 0) {
            myTotalHours += Math.round(myLeave * 4) / 4
           }
           else {
            myDayLeaveError = true
           }
          }
          else {
           myDayLeaveError = true
          }
         }
        }

        if (myDayLeaveError == false) {
         if (myTotalHours > 0) {
          this.getField("Day" + myDay + "TotalHours").value = myTotalHours.toFixed(2)
         }
         else {
          this.getField("Day" + myDay + "TotalHours").value = ""
         }
        }
        else {
         this.getField("Day" + myDay + "TotalHours").value = "Error!"
        }

      }
      else {
        this.getField("Day" + myDay + "TotalWork").value = "Error!"
        this.getField("Day" + myDay + "TotalHours").value = "Error!"
      }

      }

       

      Below is a screenshot of what I have currently. Any help is greatly appreciated.
      2014-11-12_16-33-01.jpg