<?php

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

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;
		}
		if ($this->chief) {
			return $this->paid  * $cof * 2;
		} else {
			return $this->paid * $cof;
		} 									
	}
	public function getUseCof() { // Получаем отношения деньги/страницы для отдельно работника. По сути мусор, но вдруг пригодиться кому-то?
		if ($this->countPages() == 0) {
			return $this->getPaid();
		} else {
		return $this->getPaid() / $this->countPages();
		}
	}
}
class Manager extends Worker {
	public $coffe = 20;
	public $paid = 500;
	public $pages = 200;

	public function __construct($name, $level, $chief) {
		$this->name = $name;
		if (($level == 1) OR ($level == 2) OR ($level == 3)) {
			$this->level = $level;
		} else {
			echo "The object with $level =! (1,2,3) can't exists. Please create a new object with these levels.\n";
			return False;
		}
		$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;
		if (($level == 1) OR ($level == 2) OR ($level == 3)) {
			$this->level = $level;
		} else {
			echo "The object with $level =! (1,2,3) can't exists. Please create a new object with these levels.\n";
			return False;
		}
		$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;
		if (($level == 1) OR ($level == 2) OR ($level == 3)) {
			$this->level = $level;
		} else {
			echo "The object with $level =! (1,2,3) can't exists. Please create a new object with these levels.\n";
			return False;
		}
		$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;
		if (($level == 1) OR ($level == 2) OR ($level == 3)) {
			$this->level = $level;
		} else {
			echo "The object with $level =! (1,2,3) can't exists. Please create a new object with these levels.\n";
			return False;
		}
		$this->chief = $chief;
	}
}
function rightPad($string, $symbols) {
	$len = mb_strlen($string);
	if ($len < $symbols) {
		$add = $symbols - $len;
		return ($string.str_repeat(" ", $add));
	} else {
		return $string;
	}
}
function leftPad($string, $symbols) {
	$len = mb_strlen($string);
	if ($len < $symbols) {
		$add = $symbols - $len;
		return (str_repeat(" ", $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[] = clone $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]->countCoffe();
		}
		return $totalyCoffe;
	}
	public function totalyPages() {
		$totalyPages = 0;
		foreach ($this->workers as $key => $value) {
			$totalyPages += $this->workers[$key]->countPages();
		}
		return $totalyPages;
	}
	public function averagePrice() {
		return $this->totalySalary() / $this->totalyPages();
	}
} 

$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";

$all = array(
	'name' => "Итого",
	'workers' => 0,
	'salary' => 0,
	'coffe' => 0,
	'pages' => 0,
	'price' => 0
	);

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

foreach ($all as $key => $value){
	if ($key == 'price'){
		echo rightPad(ceil($value/count($departaments)), $col);
	} else {
	echo rightPad($value, $col);
	}

}

echo "\n";

?>