<?php

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

class invalidLevel extends Exception {
}

abstract class Worker {
	public $name;
	public $level;
	public $chief;
	public $coffe;
	public $paid;
	public $pages;

	public function countPages() {
		if ($this->chief){
			return 0;
		} else {
			return $this->pages;
		}
	}
	
	public function countCoffe() {
		if ($this->chief) {
			return ($this->coffe *2);
		} else {
			return $this->coffe;
		} // Считаем выпитый кофе
	}
	public function getPaid() {
		if ($this->level == 3) {
			$cof = 1.5; 
		} elseif ($this->level == 2) { 
			$cof = 1.25;
		} elseif ($this->level == 1) {
			$cof = 1;
		} else {
			throw new invalidLevel("Не существует такого уровня должности.\n Введите 1, 2 или 3.\n");
		}
		if ($this->chief) {
			return $this->paid  * $cof * 2;
		} else {
			return $this->paid * $cof;
		} // Получаем зарплату
	}
	public function getUseCof() { // Получаем отношения деньги/страницы
		if ($this->pages == 0) {
			return $this->getPaid();
		} else {
		return $this->getPaid() / $this-> pages;
		}
	}
}
class Manager extends Worker {
	public $coffe = 20;
	public $paid = 500;
	public $pages = 200;

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

class Engeneer extends Worker {
	public $coffe = 5;
	public $paid = 200;
	public $pages = 50;

	public function __construct($name, $level, $chief) {
		$this->name = $name;
		$this->level = $level;
		$this->chief = $chief;
	}
}
class Marketing extends Worker {
	public $coffe = 15;
	public $paid = 400;
	public $pages = 150;

	public function __construct($name, $level, $chief) {
		$this->name = $name;
		$this->level = $level;
		$this->chief = $chief;
	}
}
class Analyst extends Worker {
	public $coffe = 50;
	public $paid = 800;
	public $pages = 5;

	public function __construct($name, $level, $chief) {
		$this->name = $name;
		$this->level = $level;
		$this->chief = $chief;
	}
}
function rightPad($string, $symbols) {
	$len = mb_strlen($string);
	if ($len < $symbols) {
		$add = $symbols - $len;
		return ($string.str_pad("", $add));
	} else {
		return $string;
	}
}
function leftPad($string, $symbols) {
	$len = mb_strlen($string);
	if ($len < $symbols) {
		$add = $symbols - $len;
		return (str_pad("", $add).$string);
	} else {
		return $string;
	}
}
class Departament {
	public $name;
	public $workers;
	
	public function __construct($name){
		$this->name = $name;
		$this->workers = array();
	}
	public function addWorkers($example, $count) {
		$array_count = count($this->workers);
		for ($i = $array_count; $i <= ($array_count + $count); $i++) {
			$this->workers[] = $example;
		}
	}
	public function totalySalary() {
		$total = 0;
		foreach ($this->workers as $key => $value) {
			$total += $this->$workers[$key]->getPaid();
		}
		return $total;
	}
	public function totalyCoffe() {
		$totalyCoffe = 0;
		foreach ($this->workers as $key => $value) {
			$totalyCoffe += $this->workers[$key]->coffe;
		}
		return $totalyCoffe;
	}
	public function totalyPages() {
		$totalyPages = 0;
		foreach ($this->workers as $key => $value) {
			$totalyPages += $this->workers[$key]->pages;
		}
		return $totalyPages;
	}
	public function averagePrice() {
		$price = 0;
		foreach ($this->workers as $key => $value) {
			$price += $this->workers[$key]->getUseCof();
		}
		return $price/count($this->workers);
	}
} 

$steve = new Manager('Steve Jobs', 2, True);
echo $steve->getPaid()."\n";
$steve->chief = False;
echo $steve->getPaid()."\n";


$manager1L = new Manager('Петров И.', 1, False);
$manager2L = new Manager('Иванов П.', 2, False);
$manager3L = new Manager('Рустамович Ш.', 3, False);
$chiefMa2 = new Manager("Шеф", 2, True);
$chiefMarket2 = new Marketing("Шеф", 2, True);
$marketing1L = new Marketing('Шухратович Р', 3, False);
$analyst1L = new Analyst('Доброевский Р.', 3, False);
$analyst2L = new Analyst('Вербицкий М.', 2, False);

$buying = new Departament('Закупок');
$buying->addWorkers($manager1L, 9);
$buying->addWorkers($manager2L, 3);
$buying->addWorkers($manager3L, 2);
$buying->addWorkers($marketing1L, 1);
$buying->addWorkers($chiefMa2, 1);

$selling = new Departament('Продаж');
$selling->addWorkers($manager1L, 12);
$selling->addWorkers($marketing1L, 6);
$selling->addWorkers($analyst1L, 3);
$selling->addWorkers($analyst2L, 2);
$selling->addWorkers($chiefMarket2, 1);

$departaments = array();
$departaments[] = $buying;
$departaments[] = $selling;

$col = 20;
$table = array ('Департамент', 'Сотрудников', 'Тугр.', 'Кофе', 'Стр', 'Т/СТР');
foreach ($table as $key => $value) {
	echo rightPad($table[$key], $col);
}
echo "\n";

foreach ($departaments as $key => $value) {
	echo rightPad($departaments[$key]->name, $col);
	echo rightPad(count($departaments[$key]->workers), $col);
	echo rightPad($departaments[$key]->totalySalary(), $col);
	echo rightPad($departaments[$key]->totalyCoffe(), $col);
	echo rightPad($departaments[$key]->totalyPages(), $col);
	echo rightPad(ceil($departaments[$key]->averagePrice()), $col);
	echo "\n";
}

?>