<?php

error_reporting(-1);

class Employee
{
	public $name;
	public $rate;
	public $hours = array(); //hours worked in a week
	
	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 $weekHours) {
			if ($weekHours <= 40) {
				$normalHours += $weekHours;
			} else {
				$normalHours += 40;
			}
		}
		return $normalHours;
	}
	
	public function getOvertimeHours()
	{
		$overtimeHours = $this->getTotalHoursWorked() - $this->getNormalHours();
		return $overtimeHours;
	}
	
	public function getSalary()
	{
		$normalHours = $this->getNormalHours();
		$overtimeHours = $this->getOvertimeHours();
		$salary = $normalHours * $this->rate;
		$salary += $overtimeHours * $this->rate*2;
		return $salary;
	}
	
	public function getShortName()
	{
		$shortName = preg_replace("/([а-яёА-ЯЁ]+\s[а-яёА-ЯЁ])[а-яёА-ЯЁ]+/ui", '$1.', $this->name);
		return $shortName;
	}
}

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

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

$employees = array($ivan, $petr);

$col1 = 20;
$col2 = 8;
$col3 = 8;
$col4 = 8;
$col5 = 8;

function padRight($string, $length)
{
	$padding = $length - mb_strlen($string);
	$string .= str_repeat(" ", $padding);
	return $string;
}

function padLeft($string, $length) 
{
	$padding = $length - mb_strlen($string)+2;
	$string = str_repeat(" ", $padding) . $string;
	return $string;
}

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

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

//footer 
echo padRight("Всего", $col1) . 
padLeft($totalHours, $col2) . 
padLeft($totalOvertimeHours, $col3) . 
padLeft(" ", $col4) . 
padLeft($totalSalary, $col5) . "\n";

