patternphpMinor
Labor percentage and wages from clock in/out and sales records
Viewed 0 times
recordslaborclockandsalesfromoutpercentagewages
Problem
I'm developing a PHP function for calculating labor percentage (labor cost/sales) accounting for daily and weekly overtime rules, if applicable, based on the payroll period; the wages for each employee in the time input search range are calculated then summed to determine total labor cost.
The function takes start/end date/times from datetime pickers as POST data for the search range of sales and shift records, but can be hardcoded in as UTC stamps.
Intended for use with a DB containing tables for sales records (UNIX timestamp,amount) and employee clock in/out records (
I'm looking for any criticisms or suggestions, tried to use verbose variable names for clarity.
Updated: I added the logic for calculating the employees' earnings for each shift, using the same payroll/overtime considerations. Response now returns (
```
post('startDate')); //unix timestamp of query start date
$endDate = strtotime($this->post('endDate')); //unix timestamp of query end date
if ($this->post('startTime')){ //optional
$startTime = strtotime($this->post('startTime')); //unix timestamp of query start time
$startminute = date('i', $startTime); //0-59
$starthour = date('H', $startTime); //0-23
$startseconds = date('s', $startTime); // 1-12
}
else{ //defaults to 00:00
$startTime = 0;
$startminute = 0;
$starthour = 0;
$startseconds = 0;
}
if($this->post('endTime')){ //end time provided
$endTime = strtotime($this->post('endTime')); //unix timestamp of query end time
$endminute = date('i', $endTime);
$endhour = date('H', $endTime);
$endseconds = date('s', $endTime);
}else{ //defaults to 00:00
$endTime = 0;
$endminute = 0;
$endhour = 0;
$endseconds =
The function takes start/end date/times from datetime pickers as POST data for the search range of sales and shift records, but can be hardcoded in as UTC stamps.
Intended for use with a DB containing tables for sales records (UNIX timestamp,amount) and employee clock in/out records (
StaffID, ClockIn, ClockOut, RegRate, OvertimeRate) but similarly structured arrays will work just fine.I'm looking for any criticisms or suggestions, tried to use verbose variable names for clarity.
Updated: I added the logic for calculating the employees' earnings for each shift, using the same payroll/overtime considerations. Response now returns (
StaffID,TotalEarnings,TotalTime) records as well as the original clock in/out records in the search range with the ShiftEarnings appended. ```
post('startDate')); //unix timestamp of query start date
$endDate = strtotime($this->post('endDate')); //unix timestamp of query end date
if ($this->post('startTime')){ //optional
$startTime = strtotime($this->post('startTime')); //unix timestamp of query start time
$startminute = date('i', $startTime); //0-59
$starthour = date('H', $startTime); //0-23
$startseconds = date('s', $startTime); // 1-12
}
else{ //defaults to 00:00
$startTime = 0;
$startminute = 0;
$starthour = 0;
$startseconds = 0;
}
if($this->post('endTime')){ //end time provided
$endTime = strtotime($this->post('endTime')); //unix timestamp of query end time
$endminute = date('i', $endTime);
$endhour = date('H', $endTime);
$endseconds = date('s', $endTime);
}else{ //defaults to 00:00
$endTime = 0;
$endminute = 0;
$endhour = 0;
$endseconds =
Solution
Use Classes
Documentation.
Using classes will not only make your code more readable, but also more easier to be maintainable and re-usable. It will help you separate the HTML logic and the PHP logic, instead of having a lot of messy PHP code in HTML files. My recommendation is, learn OOP Principals.
Readability
Variables like
This looks more read-able.
Also, looking at the length of your single function, I don't think anyone will bother even looking at it, they will simply give up. I suggest you to separate it into different smaller functions which perform ONLY ONE TASK.
One function, one job
When you look at your function, it is more like as if it is Hardcoded only for your task. A good programmer always code's it in a way, where it is re-usable. Have different functions for different tasks
Documentation.
Using classes will not only make your code more readable, but also more easier to be maintainable and re-usable. It will help you separate the HTML logic and the PHP logic, instead of having a lot of messy PHP code in HTML files. My recommendation is, learn OOP Principals.
Readability
Variables like
$ONEWEEKSECONDS and $ONEDAYSECONDS can be simplified as:$ONEWEEKSECONDS = 60 * 60 * 24 * 7;
$ONEDAYSECONDS = 60 * 60 * 24;This looks more read-able.
Also, looking at the length of your single function, I don't think anyone will bother even looking at it, they will simply give up. I suggest you to separate it into different smaller functions which perform ONLY ONE TASK.
One function, one job
When you look at your function, it is more like as if it is Hardcoded only for your task. A good programmer always code's it in a way, where it is re-usable. Have different functions for different tasks
Code Snippets
$ONEWEEKSECONDS = 60 * 60 * 24 * 7;
$ONEDAYSECONDS = 60 * 60 * 24;Context
StackExchange Code Review Q#61674, answer score: 2
Revisions (0)
No revisions yet.