<?php
class Employee // employee значит «сотрудник»
{
public $name; // имя-фамилия
public $rate; // часовая ставка (сколько он получает тугриков за час работы)
public $hours = array(); // массив, содержащий отработанные часы по неделям public $overtimeHours = 0; // переменная, которая будет содержать данные о переработанных часах
/* Считает общее число отработанных часов */
public function getTotalHoursWorked()
{
// Просто складываем значения часов в массиве
}
/* Считает зарплату */
public function getSalary()
{
// Получаем число отработанных часов
$hours = $this->getTotalHoursWorked();
// и умножаем на часовую ставку
$salary = $hours * $this->rate;
return $salary;
}
/* "Волшебный" метод */
public function __construct($name, $rate)
{
// задаем имя и часовую ставку
$this->name = $name;
$this->rate = $rate;
}
public function getNormalHours() {
$normalHours = 0;
foreach ($this->hours as $hoursAWeek) {
if ($hoursAWeek <= 40) {
$normalHours += $hoursAWeek;
} if ($hoursAWeek > 40) {
$normalHours += $hoursAWeek - ($hoursAWeek - 40);
}
}
return $normalHours;
}
public function getOvertimeHours() {
$overtime = 0;
foreach ($this->hours as $hoursAWeek) {
if ($hoursAWeek > 40) {
$overtime += ($hoursAWeek - 40);
}
}
return $overtime;
}
}
function getNormalSalary($normalHours, $rate)
{
// Получаем число нормально отработанных часов
$salary = $normalHours * $rate;
return $salary;
}
function getOvertimeSalary($normalHours, $rate)
{
// Получаем число переработанных часов и умножаем на удвоенную часовую ставку
$salary = $normalHours * ($rate * 2);
return $salary;
}
function padRight($x, $y){
return $tableCell;
}
function padLeft($x, $y){
return $tableCell;
}
// Ширина колонок
$col1 = 15;
$col2 = $col4 = $col5 = 8;
$col3 = 10;
$ivan = new Employee("Иванов Иван", 10);
$ivan->hours = array(40, 40, 40, 40); $allHours[] = $ivan->hours;
$peter = new Employee("Петров Петр", 8);
$peter->hours = array(40, 10, 40, 50); // но в последнюю неделю решил поработать побольше $allHours[] = $peter->hours;
$sidor = new Employee("Сидоров Сидор", 10);
$sidor->hours = array(40, 60, 40, 40); $allHours[] = $sidor->hours;
$employees = array($ivan, $peter, $sidor);
// Цикл для удаления части имени работника
foreach ($employees as $employee) {
$employee->name = preg_replace('/( )([А-ЯЁ])([а-яё]+)/u', ' $2.', $employee->name); }
$hours = 0;
$salary = 0;
$overtime = 0;
for ($i = 0; $i < count($allHours); $i++) { if ($employees[$i]->getNormalHours() > 0) {
$normalHours = $employees[$i]->getNormalHours();
$rate = $employees[$i]->rate;
}
if ($employees[$i]->getOvertimeHours() > 0) {
$normalHours = $employees[$i]->getOvertimeHours();
$rate = $employees[$i]->rate;
$overtimeSalary = getOvertimeSalary($normalHours, $rate);
$employees[$i]->overtimeHours = $overtimeSalary;
}
}
// Осталось посчитать $normalHours умноженные на нормальную ставку и $overtimeHours умноженные на ставку за переработку
// Заголовок таблицы
echo padRight("Сотрудник", $col1) .
padLeft("Часы", $col2) .
padLeft("Овертайм", $col3) .
padLeft("Ставка", $col4) .
padLeft("З/п", $col5) . "\n";
// Сама таблица
foreach ($employees as $employee) {
echo padRight($employee->name, $col1) .
padLeft($employee->getTotalHoursWorked(), $col2) .
padLeft($employee->getOvertimeHours(), $col3) .
padLeft($employee->rate, $col4) .
padLeft(getNormalSalary($employee->getNormalHours(), $employee->rate) + $employee->overtimeHours, $col5) . "\n";
if ($employee->getOvertimeHours() > 0) {
$overtime += $employee->getOvertimeHours();
}
$salary += getNormalSalary($employee->getNormalHours(), $employee->rate) + $employee->overtimeHours;
}
echo padRight("Всего:", $col1) .
padLeft($hours, $col2) .
padLeft($overtime, $col3) .
padLeft(null, $col4) .
padLeft($salary, $col5) . "\n";
PD9waHAKCmVycm9yX3JlcG9ydGluZygtMSk7Cm1iX2ludGVybmFsX2VuY29kaW5nKCJVVEYtOCIpOwoKY2xhc3MgRW1wbG95ZWUgICAgICAgICAgICAgIC8vIGVtcGxveWVlINC30L3QsNGH0LjRgiDCq9GB0L7RgtGA0YPQtNC90LjQusK7CnsKICAgIHB1YmxpYyAkbmFtZTsgICAgICAgICAgICAgICAvLyDQuNC80Y8t0YTQsNC80LjQu9C40Y8KICAgIHB1YmxpYyAkcmF0ZTsgICAgICAgICA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