mursal
mursal

Reputation: 35

Sum the leave hours from the presence in an array

I want to calculate the leave hours of each employee.

I have the following Tables:

attendance with (id, empid, check_in, check_out, date) columns

leave with(id,empid,reason,time_long from_date, to_date) columns

employee with (id,name,....) columns

This is my query:

select `emp`.*, `p`.*, `a`.*, `l`.`id` as `leaveId`, `l`.`time_long` as `leaveLong`, `l`.`from_date` as `leaveFrom`, `l`.`to_date` as `leaveTo` from `employee` as `emp` inner join `attendance` as `a` on `emp`.`id` = `a`.`empid`  left join `leave` as `l` on `emp`.`id` = `l`.`empid` where `a`.`date` between 2019-03-01 and 2019-03-31 order by `emp`.`id` asc)

the query returns the following records.

[
   {
      "log_id": 1310,
      "name": "ahmad",
      "empid": 3,
      "check_in": "11:56",
      "check_out": "17:25",
      "date": "2019-03-23",
      "time_long": "5:28",
      "leaveId": 5,
      "leaveLong": 16,
      "leaveFrom": "2019-03-15",
      "leaveTo": "2019-03-17"
    },
    {
      "log_id": 1311,
      "name": "ahmad",
      "empid": 3,
      "check_in": "07:14",
      "check_out": "17:24",
      "date": "2019-03-24",
      "time_long": "10:9",
      "leaveId": 5,
      "leaveLong": 16,
      "leaveFrom": "2019-03-15",
      "leaveTo": "2019-03-17"
    },{
      "log_id": 1312,
      "name": "ahmad",
      "empid": 3,
      "check_in": "06:58",
      "check_out": "17:21",
      "date": "2019-03-25",
      "time_long": "10:23"
      "leaveId": 5,
      "leaveLong": 16,
      "leaveFrom": "2019-03-15",
      "leaveTo": "2019-03-17"
    },{
      "log_id": 1313,
      "name": "ahmad",
      "empid": 3,
      "check_in": "07:58",
      "check_out": "17:21",
      "date": "2019-03-26",
      "time_long": "9:23"
      "leaveId": 15,
      "leaveLong": 8.0,
      "leaveFrom": "2019-03-28",
      "leaveTo": "2019-03-29"
    },
    {
      "log_id": 1314,
      "name": "ahmad",
      "empid": 3,
      "check_in": "07:58",
      "check_out": "17:21",
      "date": "2019-03-26",
      "time_long": "9:23"
      "leaveId": 15,
      "leaveLong": 8.0,
      "leaveFrom": "2019-03-28",
      "leaveTo": "2019-03-29"
    },
    {
      "log_id": 1315,
      "name": "ahmad",
      "empid": 3,
      "check_in": "08:00",
      "check_out": "16:00",
      "date": "2019-03-27",
      "time_long": "8:00"
      "leaveId": 15,
      "leaveLong": 8.0,
      "leaveFrom": "2019-03-28",
      "leaveTo": "2019-03-29"
    }
    { ... }
  ]

So I expect the following result from this output:

Vacation for employee with ID 3 = 24 hours

Upvotes: 3

Views: 133

Answers (3)

claasic
claasic

Reputation: 1050

Javascript

EDIT: Changed the code to fit the newest requirements.

What is the concept? We use reduce to accumulate the results in an object. Basically, we take an object's empid and make a key inside the object out of it. If the key already exists we take the existing value of the key and add the current leaveLong to it OR if the key doesn't exist yet we start our value at 0 but nonetheless still add the current leaveLong and by that create a key-value pair.

(a[c.empid] || 0) can be read as: use the value of the given key if defined else use 0 as a value.

So when reduce ran through our whole array of objects we have an object that holds all empid's as keys and their respective leaveLong values as a sum value.

EDIT: Before that we have to filter. We simply find the first occurrence of the leaveId and filter out all the rest.

var arr = [{"log_id":1310,"name":"ahmad","fname":"Mohammad","photo":"images/user_profile//1550473469.jpg","title":"Doctor","description":null,"empid":3,"check_in":"11:56","check_out":"17:25","date":"2019-03-23","time_long":"5:28","leaveId":5,"leaveLong":16,"leaveFrom":"2019-03-15","leaveTo":"2019-03-17"},{"log_id":1311,"name":"ahmad","fname":"Mohammad","photo":"images/user_profile//1550473469.jpg","title":"Doctor","description":null,"empid":3,"check_in":"07:14","check_out":"17:24","date":"2019-03-24","time_long":"10:9","leaveId":5,"leaveLong":16,"leaveFrom":"2019-03-15","leaveTo":"2019-03-17"},{"log_id":1312,"name":"ahmad","fname":"Mohammad","photo":"images/user_profile//1550473469.jpg","title":"Doctor","description":null,"empid":3,"check_in":"06:58","check_out":"17:21","date":"2019-03-25","time_long":"10:23","leaveId":5,"leaveLong":16,"leaveFrom":"2019-03-15","leaveTo":"2019-03-17"},{"log_id":1313,"name":"ahmad","fname":"Mohammad","photo":"images/user_profile//1550473469.jpg","title":"Doctor","description":null,"empid":3,"check_in":"07:58","check_out":"17:21","date":"2019-03-26","time_long":"9:23","leaveId":15,"leaveLong":8.0,"leaveFrom":"2019-03-28","leaveTo":"2019-03-29"},{"log_id":1314,"name":"ahmad","fname":"Mohammad","photo":"images/user_profile//1550473469.jpg","title":"Doctor","description":null,"empid":3,"check_in":"07:58","check_out":"17:21","date":"2019-03-26","time_long":"9:23","leaveId":5,"leaveLong":8.0,"leaveFrom":"2019-03-28","leaveTo":"2019-03-29"},{"log_id":1315,"name":"ahmad","fname":"Mohammad","photo":"images/user_profile//1550473469.jpg","title":"Doctor","description":null,"empid":3,"check_in":"08:00","check_out":"16:00","date":"2019-03-27","time_long":"8:00","leaveId":5,"leaveLong":8.0,"leaveFrom":"2019-03-28","leaveTo":"2019-03-29"},{"log_id":1316,"name":"Neda Mohammad","fname":"Gada Mohammad","photo":"images/user_profile//1550473758.jpg","title":"Pharmacist","description":null,"empid":8,"check_in":"07:36","check_out":"17:57","date":"2019-03-25","time_long":"10:20","leaveId":null,"leaveLong":null,"leaveFrom":null,"leaveTo":null,},{"log_id":1317,"name":"Neda Mohammad","fname":"Gada Mohammad","photo":"images/user_profile//1550473758.jpg","title":"Pharmacist","description":null,"empid":8,"check_in":"08:00","check_out":"16:00","date":"2019-03-26","time_long":"8:00","leaveId":null,"leaveLong":null,"leaveFrom":null,"leaveTo":null,},{"log_id":1318,"name":"Neda Mohammad","fname":"Gada Mohammad","photo":"images/user_profile//1550473758.jpg","title":"Pharmacist","description":null,"empid":8,"check_in":"08:00","check_out":"16:00","date":"2019-03-27","time_long":"8:00","leaveId":null,"leaveLong":null,"leaveFrom":null,"leaveTo":null,}];

let res = arr.filter((v,i) => arr.findIndex(o => o.leaveId == v.leaveId) == i)  
             .reduce((a,c) => {a[c.empid] = (a[c.empid] || 0) + c.leaveLong; return a},{})

console.log(res)

If you need an array as an answer, put the object into Object.entries.

var arr = [{"log_id":1310,"name":"ahmad","fname":"Mohammad","photo":"images/user_profile//1550473469.jpg","title":"Doctor","description":null,"empid":3,"check_in":"11:56","check_out":"17:25","date":"2019-03-23","time_long":"5:28","leaveId":5,"leaveLong":16,"leaveFrom":"2019-03-15","leaveTo":"2019-03-17"},{"log_id":1311,"name":"ahmad","fname":"Mohammad","photo":"images/user_profile//1550473469.jpg","title":"Doctor","description":null,"empid":3,"check_in":"07:14","check_out":"17:24","date":"2019-03-24","time_long":"10:9","leaveId":5,"leaveLong":16,"leaveFrom":"2019-03-15","leaveTo":"2019-03-17"},{"log_id":1312,"name":"ahmad","fname":"Mohammad","photo":"images/user_profile//1550473469.jpg","title":"Doctor","description":null,"empid":3,"check_in":"06:58","check_out":"17:21","date":"2019-03-25","time_long":"10:23","leaveId":5,"leaveLong":16,"leaveFrom":"2019-03-15","leaveTo":"2019-03-17"},{"log_id":1313,"name":"ahmad","fname":"Mohammad","photo":"images/user_profile//1550473469.jpg","title":"Doctor","description":null,"empid":3,"check_in":"07:58","check_out":"17:21","date":"2019-03-26","time_long":"9:23","leaveId":15,"leaveLong":8.0,"leaveFrom":"2019-03-28","leaveTo":"2019-03-29"},{"log_id":1314,"name":"ahmad","fname":"Mohammad","photo":"images/user_profile//1550473469.jpg","title":"Doctor","description":null,"empid":3,"check_in":"07:58","check_out":"17:21","date":"2019-03-26","time_long":"9:23","leaveId":5,"leaveLong":8.0,"leaveFrom":"2019-03-28","leaveTo":"2019-03-29"},{"log_id":1315,"name":"ahmad","fname":"Mohammad","photo":"images/user_profile//1550473469.jpg","title":"Doctor","description":null,"empid":3,"check_in":"08:00","check_out":"16:00","date":"2019-03-27","time_long":"8:00","leaveId":5,"leaveLong":8.0,"leaveFrom":"2019-03-28","leaveTo":"2019-03-29"},{"log_id":1316,"name":"Neda Mohammad","fname":"Gada Mohammad","photo":"images/user_profile//1550473758.jpg","title":"Pharmacist","description":null,"empid":8,"check_in":"07:36","check_out":"17:57","date":"2019-03-25","time_long":"10:20","leaveId":null,"leaveLong":null,"leaveFrom":null,"leaveTo":null,},{"log_id":1317,"name":"Neda Mohammad","fname":"Gada Mohammad","photo":"images/user_profile//1550473758.jpg","title":"Pharmacist","description":null,"empid":8,"check_in":"08:00","check_out":"16:00","date":"2019-03-26","time_long":"8:00","leaveId":null,"leaveLong":null,"leaveFrom":null,"leaveTo":null,},{"log_id":1318,"name":"Neda Mohammad","fname":"Gada Mohammad","photo":"images/user_profile//1550473758.jpg","title":"Pharmacist","description":null,"empid":8,"check_in":"08:00","check_out":"16:00","date":"2019-03-27","time_long":"8:00","leaveId":null,"leaveLong":null,"leaveFrom":null,"leaveTo":null,}];

let res = Object.entries(
              arr.filter((v,i) => arr.findIndex(o => o.leaveId == v.leaveId) == i)
                 .reduce((a,c) => {a[c.empid] = (a[c.empid] || 0) + c.leaveLong; return a},{}))

console.log(res)

In both cases you can replace (a,c) with (a, { empid, leaveLong}) and adjust the function accordingly. But that is just personal preference.

Upvotes: 2

Rahul
Rahul

Reputation: 18577

In that case you can use foreach to group data with empid,

$temp = [];
foreach ($arr as $key => $value) {
    // fetching all data as per empid and leave id 
    $temp[$value['empid']][$value['leaveId']][] = $value['leaveLong'];
}
$result = [];
foreach ($temp as $key => $value) {
    foreach ($value as $key1 => $value1) {
        // fetching max value for empid and leave id
        $result[$key][$key1] = max($value1);        
    }   
}
// summing per emp id
$result = array_map("array_sum", $result);
print_r($result);

Demo.

Output

Array
(
    [3] => 24
    [8] => 0
)

Upvotes: 2

Shaurya Vardhan Singh
Shaurya Vardhan Singh

Reputation: 684

for javascript

suppose we stored the data in empLeave

// Assuming we stored the data in a variable empLeave

// fetch the data in a object format
let empLeaveArray = JSON.parse(empLeave)

// declare empty object
let result = {} 

// loop through every object in the data i.e every leave sanctioned
empLeaveArray.forEach((ele) => {
// if the employee has already taken a leave add the leave to it
if(ele.empid in result)
   result[ele.empid] += ele.leavelong
// if employee has not taken the leave already add the employee to the object
else
   result[ele.empid] = ele.leavelong
})

for (let key in result) {
  console.log("employee " + key + " has taken " + result[key] + "leaves");
}

Upvotes: 0

Related Questions