<?php
error_reporting(-1);

class Organisation //содержит методы для подсчета общих и средних параметров по конторе
{
	public $name;
	public $departments = [];
	
	public function __construct($name)
	{
		$this->name = $name;
	}
	
	public function countTotalWorkers()
	{
		$totalWorkers = 0;
		foreach($this->departments as $unit) {
			$totalWorkers += $unit->countDepWorkers();
		}
		return $totalWorkers;
	}
	
	public function countAveregeWorkers()
	{
		return $this->countTotalWorkers() / count($this->departments);
	}

	public function countTotalStuff($stuff)
	{
		$companyStuff = 0;
		foreach ($this->departments as $dep) {
			$companyStuff += $dep->countDepStuff($stuff);
		}
		return $companyStuff;
	}

	public function countAveregeStuff($stuff)
	{
		return $this->countTotalStuff($stuff) / count($this->departments);
	}

	public function countTotalEfficiency() 
	{
		$companyEfficiency = 0;
		foreach ($this->departments as $dep) {
			$companyEfficiency += $dep->countDepEfficiency();
		}
		return $companyEfficiency;
	}
	
	public function countAveregeEfficiency()
	{
		return $this->countTotalEfficiency() / count($this->departments);
	}
		
}

class Department  
{
	public $name;
	public $workers = [];
	
	public function __construct($name)
	{
		$this->name = $name;
	}
	
	public function addWorkers($amount, $class, $rank, $leader) {
		for($i=1; $i<=$amount; $i++ ) {
			$this->workers[] = new $class($rank, $leader); 
		}
	}
	
	public function getName()
	{
		return $this->name;
	}
	
	public function countDepWorkers()
	{
		return count($this->workers);
	}
	
	public function countDepStuff($stuff)
	{
		$totalDepStuff = 0;
		foreach ($this->workers as $worker) {
			$totalDepStuff += $worker->$stuff;
		}
		return $totalDepStuff;
	}

	function countDepEfficiency()
	{
		return $this->countDepStuff("salary") / $this->countDepStuff("docs");
	}
	
}

class Employee
{
	public $salary;
	public $coffeUsage;
	public $docs;
	public $rank;
	public $leader;
	public $defaultSalary;
	
	public function __construct($rank, $leader) {
		$this->rank = $rank;
		$this->leader = $leader;
		$this->setSalary();
		$this->setCoffeUsage();
		$this->setDocs();
	}
	
	public function setSalary ()
	{
		if ($this->rank == 2) {
			$rankBonus = 1.25;
		} elseif ($this->rank == 3) {
			$rankBonus = 1.5;
		} else {
			$rankBonus = 1;
		}
		
		if ($this->leader == true) {
			$leaderBonus = 1.5;
		} else {
			$leaderBonus = 1;
		}
		$this->salary = $this->defaultSalary * $rankBonus * $leaderBonus;
	}
	
	public function setCoffeUsage()
	{
		if ($this->leader) {
			$this->coffeUsage = $this->defaultCoffeUsage * 2;
		} else {
			$this->coffeUsage = $this->defaultCoffeUsage;
		}
	}
	
	public function setDocs ()
	{
		if ($this->leader) {
			$this->docs = 0;
		} else {
			$this->docs = $this->defaultDocs;
		}
	}
}

class Manager extends Employee 
{
	public $defaultSalary = 500;
	public $defaultCoffeUsage = 20;
	public $defaultDocs = 200;
}

class MarketGuy extends Employee
{
	public $defaultSalary = 400;
	public $defaultCoffeUsage = 15;
	public $defaultDocs = 150;
}

class Engineer extends Employee
{
	public $defaultSalary = 200;
	public $defaultCoffeUsage = 5;
	public $defaultDocs = 50;
}

class Analyst extends Employee
{
	public $defaultSalary = 800;
	public $defaultCoffeUsage = 50;
	public $defaultDocs = 5;
}



function padLeft ($string, $length) {
	$strLength = mb_strlen($string);
	$spases = str_repeat(" ", ($length - $strLength - 1));
	$string = "|" . $spases . $string;
	return $string;
}
 
function padRight ($string, $length) {
	$strLength = mb_strlen($string);
	$spases = str_repeat(" ", ($length - $strLength));
	$string .= $spases;
	return $string;
}






//создаю организацию:
$vector = new Organisation("Вектор");

//департаменты
$purchase = new Department("Департамент закупок"); 
//заполняю планктоном
$purchase->addWorkers(9, "Manager", 1, false); //9 менеджеров 1 ранга, не лидеры
$purchase->addWorkers(3, "Manager", 2, false);
$purchase->addWorkers(2, "Manager", 3, false);
$purchase->addWorkers(2, "MarketGuy", 1, false);
$purchase->addWorkers(1, "Manager", 2, true); //1 менеджер 2 ранга, начальник
$vector->departments[] = $purchase;

$sales = new Department("Департамент продаж");
$sales->addWorkers(12, "Manager", 1, false);
$sales->addWorkers(16, "MarketGuy", 1, false);
$sales->addWorkers(3, "Analyst", 1, false);
$sales->addWorkers(2, "Analyst", 2, false);
$sales->addWorkers(1, "MarketGuy", 2, true);
$vector->departments[] = $sales;

$advertising = new Department("Департамент рекламы");
$advertising->addWorkers(15, "MarketGuy", 1, false);
$advertising->addWorkers(10, "MarketGuy", 2, false);
$advertising->addWorkers(8, "Manager", 1, false);
$advertising->addWorkers(2, "Engineer", 1, false);
$advertising->addWorkers(1, "MarketGuy", 3, true);
$vector->departments[] = $advertising;

$logistics = new Department("Департамент логистики");
$logistics->addWorkers(13, "Manager", 1, false);
$logistics->addWorkers(5, "Manager", 2, false);
$logistics->addWorkers(5, "Engineer", 1, false);
$logistics->addWorkers(1, "Manager", 1, true);
$vector->departments[] = $logistics;

//var_dump($departments);

$col1 = 22;
$col2 = 13;
$col3 = 11;


echo padRight("Департамент", $col1) .
	padLeft("сотрудников", $col2) .
	padLeft("тугрики", $col3) .
	padLeft("кофе", $col3) .
	padLeft("документы", $col2) .
	padLeft("тугр./документы", $col1) .
	"\n";

echo "--------------------------------------------------------------------------------\n";

foreach ($vector->departments as $dep) {
	echo padRight($dep->getName(), $col1) .
		padLeft($dep->countDepWorkers(), $col2) .
		padLeft($dep->countDepStuff("salary"), $col3) .
		padLeft($dep->countDepStuff("coffeUsage"), $col3) .
		padLeft($dep->countDepStuff("docs"), $col2) .
		padLeft($dep->countDepEfficiency(), $col1) .
		"\n";
}

echo "--------------------------------------------------------------------------------\n";

echo padRight("Среднее", $col1) . 
		padLeft($vector->countAveregeWorkers(), $col2) .
		padLeft($vector->countAveregeStuff("salary"), $col3) .
		padLeft($vector->countAveregeStuff("coffeUsage"), $col3) .
		padLeft($vector->countAveregeStuff("docs"), $col2) .
		padLeft($vector->countAveregeEfficiency(), $col1) .
		"\n" ;
		
echo padRight("Всего", $col1) . 
		padLeft($vector->countTotalWorkers(), $col2) .
		padLeft($vector->countTotalStuff("salary"), $col3) .
		padLeft($vector->countTotalStuff("coffeUsage"), $col3) .
		padLeft($vector->countTotalStuff("docs"), $col2) .
		padLeft($vector->countTotalEfficiency(), $col1) .
		"\n";
		
		
