<?php

error_reporting(-1);
mb_internal_encoding('utf-8');

class Employee
{
    public $name;
    public $rate;
    public $hours = array();

    public function __construct($name, $rate)
    {
        $this->name = $name;
        $this->rate = $rate;
    }

    public function getTotalHoursWorked()
    {
        return array_sum($this->hours);
    }

    //без переработок
    public function getNormalHours()
    {
        $normalHours = 0;
        foreach($this->hours as $weeks)
        {
            if($weeks <= 40) {
                $normalHours += $weeks;
            } elseif($weeks > 40) {
                $normalHours = $normalHours + 40;
            }
        }
        return $normalHours;
    }

    //c переработками
    public function getOvertimeHours()
    {
        $overtimeHours = 0;
        foreach($this->hours as $weeks)
        {
            if($weeks > 40) {
                $overtimeHours = $overtimeHours + ($weeks - 40);
            }
        }
        return $overtimeHours;
    }

    public function getSalary()
    {
        $hours = $this->getNormalHours();
        $salary = $hours * $this->rate;
        $overtimeHours = $this->getOvertimeHours();
        $salary = $salary + $overtimeHours*$this->rate*2;
        return $salary;
    }

    //получаю сокращённое имя
    public function getShortName()
    {
        $name = $this->name;
        $reg = "! ([a-яёА-ЯЁ])([a-яёА-ЯЁ])+!u";
        $shortenedName = preg_replace($reg, " $1.", $name);
        return $shortenedName;
    }


}


$ivan = new Employee("Иванов Иван", 10);
$ivan->hours = array(40, 40, 40, 40);

$peter = new Employee("Петров Петр", 8);
$peter->hours = array(40, 10, 40, 50);

$employees = array($ivan, $peter);
/*
foreach ($employees as $employee) {
    echo "Имя: {$employee->name}\n";
    echo "Ставка, тугриков в час: {$employee->rate}\n";
    echo "Отработал, часов: {$employee->getTotalHoursWorked()}\n";
    echo "Заработал, тугриков: {$employee->getSalary()}\n\n";
}
*/

//пишу функцию для вставки пробелов справа
function padRight($string, $length)
{
    $spaces = $length - mb_strlen($string);
    if($spaces>0)
    {
        $string=mb_str_pad($string, mb_strlen($string) + $spaces, " ", STR_PAD_RIGHT);
    }
    return $string;
}

//пишу функцию для вставки пробелов слева
function padLeft($string, $length)
{
    $spaces = $length - mb_strlen($string);
    if($spaces>0)
    {
        $string=mb_str_pad($string, mb_strlen($string) + $spaces, " ", STR_PAD_LEFT);
    }
    return $string;
}

//функция аналог str_pad, только для кириллицы
function mb_str_pad($str, $pad_len, $pad_str = ' ', $dir = STR_PAD_RIGHT, $encoding = NULL)
{
    $encoding = $encoding === NULL ? mb_internal_encoding() : $encoding;
    $padBefore = $dir === STR_PAD_BOTH || $dir === STR_PAD_LEFT;
    $padAfter = $dir === STR_PAD_BOTH || $dir === STR_PAD_RIGHT;
    $pad_len -= mb_strlen($str, $encoding);
    $targetLen = $padBefore && $padAfter ? $pad_len / 2 : $pad_len;
    $strToRepeatLen = mb_strlen($pad_str, $encoding);
    $repeatTimes = ceil($targetLen / $strToRepeatLen);
    $repeatedString = str_repeat($pad_str, max(0, $repeatTimes));
    $before = $padBefore ? mb_substr($repeatedString, 0, floor($targetLen), $encoding) : '';
    $after = $padAfter ? mb_substr($repeatedString, 0, ceil($targetLen), $encoding) : '';
    return $before . $str . $after;
}

$col1 = 30;
$col2 = 10;
$col3 = 10;
$col4 = 10;

echo padRight("Сотрудник", $col1) .
    padLeft("Часы", $col2) .
    padLeft("Овертайм", $col2) .
    padLeft("Ставка", $col3) .
    padLeft("З/п", $col4) . "\n";

$totalHours = $totalSalary = $totalOvertime = 0;
foreach ($employees as $employee) {
    echo padRight($employee->getShortName(), $col1) .
        padLeft($employee->getTotalHoursWorked(), $col2) .
        padLeft($employee->getOvertimeHours(), $col2) .
        padLeft($employee->rate, $col3) .
        padLeft($employee->getSalary(), $col4) . "\n";
    $totalHours += $employee->getTotalHoursWorked();
    $totalSalary += $employee->getSalary();
    $totalOvertime += $employee->getOvertimeHours();

}

//колонка ВСЕГО
echo padRight("Всего", $col1) .
    padLeft("$totalHours", $col2) .
    padLeft("$totalOvertime", $col2) .
    padLeft(" ", $col3) .
    padLeft("$totalSalary", $col4) . "\n";
