<?php
class Company
{
	const NAME_COMPANY = 'vector';
	private $departaments = array();

	public function add_departament($departament)
	{
		$this->departaments [] = $departament;
	}
	
	public function get_Count_Workers()
	{
		foreach ($this->departaments as $departament) {
			$count_workers += $departament->get_Sum_Workers();
		}
		return $count_workers;
	}
	
	public function get_Average_Workers()
	{
		return $this->get_Count_Workers()/count($this->departaments);
	}
	
	public function get_Count_Salary()
	{
		foreach ($this->departaments as $departament) {
			$count_salary += $departament->get_Sum_Salary();
		}
		return $count_salary;
	}
	
	public function get_Average_Salary()
	{
		return $this->get_Count_Salary()/count($this->departaments);
	}
	
	public function get_Count_Coffee()
	{
		foreach ($this->departaments as $departament) {
			$count_coffee += $departament->get_Sum_Coffee();
		}
		return $count_coffee;
	}
	
	public function get_Average_Coffee()
	{
		return $this->get_Count_Coffee()/count($this->departaments);
	}
	
	public function get_Count_Pages()
	{
		foreach ($this->departaments as $departament) {
			$count_pages += $departament->get_Sum_Pages();
		}
		return $count_pages;
	}
	
	public function get_Average_Pages()
	{
		return $this->get_Count_Pages()/count($this->departaments);
	}
	
		public function get_Count_Salary_For_Page()
	{
		foreach ($this->departaments as $departament) {
			$count_salary_for_page += $departament->get_Salary_For_Page();
		}
		return $count_salary_for_page;
	}
	
	public function get_Average_Salary_For_Page()
	{
		return $this->get_Count_Salary_For_Page()/count($this->departaments);
	}
	
	public function print_Info()
	{
		$col2 = 12;
		$col3 = 15;
		foreach ($this->departaments as $departament) {
			echo padRight($departament->get_Departament_Name(), $col2);
			echo padLeft($departament->get_Sum_Workers(), $col2);
			echo padLeft($departament->get_Sum_Salary(), $col2);
			echo padLeft($departament->get_Sum_Coffee(), $col2);
			echo padLeft($departament->get_Sum_Pages(), $col2);
			echo padLeft($departament->get_Salary_For_Page(), $col3)."\n";
		}
	}
	
	//Cократить в каждом департаменте 40% (округляя в большую сторону) инженеров, 
	//преимущественно самого низкого ранга. Если инженер является боссом, вместо него 
	//надо уволить другого инженера, не босса.
	public function Destruction_Workers($count_in_percent, $proffesion)
	{
		foreach ($this->departaments as $departament) {
			$count_workers = $departament->get_Workers_For_Destruction($count_in_percent, $proffesion);
		}
	}
}


class Departament
{
	private $departament_name;
	private $workers = array();
	
	public function __construct($name)
	{
		$this->set_Departament_Name($name);
	}
	
	public function get_Departament_Name()
	{
		return $this->departament_name;
	}
	
	public function set_Departament_Name($name)
	{
		$this->departament_name = $name;
	}
	
	public function add_Workers($count, $rank, $proffesion, $is_Head)
	{
		for ($i = 0; $i < $count; $i++) {
			$worker = new $proffesion($rank, $is_Head);
			$this->workers[] = $worker;
		}
	}
	
	public function get_Sum_Workers()
	{
		return count($this->workers);
		echo count($this->workers->salary);
	}
	
	public function get_Sum_Salary()
	{
		foreach ($this->workers as $worker) {
			$sum_salary = $sum_salary+$worker->get_Salary($worker);
		}
		return $sum_salary;
	}
	
	public function get_Sum_Coffee()
	{
		foreach ($this->workers as $worker) {
			$sum_coffee += $worker->get_Coffee($worker);
		}
		return $sum_coffee;
	}
	
	public function get_Sum_Pages()
	{
		foreach ($this->workers as $worker) {
			$sum_pages += $worker->get_Pages($worker);
		}
		return $sum_pages;
	}
	
	public function get_Salary_For_Page()
	{
		 $salary = $this->get_Sum_Salary();
		 $pages  = $this->get_Sum_Pages();
		 return round($salary/$pages, 3);
	}
	
	public function get_Workers_For_Destruction($count_in_percent,$proffesion)
	{
		foreach ($this->workers as $worker) {
			if ($worker->get_Proffesion() == $proffesion) {
				$count_workers ++;
			}
		}
		$sum_to_destroy = $count_workers - (floor($count_workers/$count_in_percent));
		$i = 0;
		foreach ($this->workers as $worker) {
			if ($worker->get_Proffesion() == $proffesion && $i < $sum_to_destroy) {
				unset($worker);
			}
		}
	}
}


abstract class Worker 
{
	private $rank;
	private $proffesion;
	private $is_Head;
	private $salary;
	private $consumed_coffee;
	private $made_pages;
	
	public function __construct($rank, $proffesion, $is_Head, $salary, $consumed_coffe, $made_pages)
	{
		$this->set_Rank($rank);
		$this->set_Proffesion($proffesion);
		$this->set_Is_Head($is_Head);
		$this->set_Salary($salary);
		$this->set_Consumed_Coffee($consumed_coffe);
		$this->set_Made_Pages($made_pages);
	}
	
	public function set_Rank($rank)
	{
		$this->rank = $rank;
	}
	
	public function set_Proffesion($proffesion)
	{
		$this->proffesion = $proffesion;	
	}
	
	public function set_Is_Head($is_Head)
	{
		$this->is_Head = $is_Head;
	}
	
	public function set_Salary($salary)
	{
		$this->salary = $salary;
	}
	
	public function set_Consumed_Coffee($consumed_coffee)
	{
		$this->consumed_coffee = $consumed_coffee;
	}
	
	public function set_Made_Pages($made_pages)
	{
		$this->made_pages = $made_pages;
	}

	public function get_Salary($worker)
	{
		$rank = $this->rank;
		if ($rank == 1) {
			$coef_salary = 1;
		}elseif ($rank == 2) {
			$coef_salary = 1.25;
		}elseif ($rank == 3) {
			$coef_salary = 1.5;
		}
		if ($this->is_Head == true) {
			$coef_head = 1.5;
		}else{
			$coef_head = 1;
		}
		return $this->salary*$coef_salary*$coef_head;
	}
	
	public function get_Coffee($worker)
	{
		if ($this->is_Head == true) {
			$coef_head = 2;
		}else{
			$coef_head = 1;
		}
		return $this->consumed_coffee*$coef_head;
	}
	
		public function get_Pages($worker)
	{
		if ($this->is_Head == true) {
			$coef_head = 0;
		}else{
			$coef_head = 1;
		}
		return $this->made_pages*$coef_head;
	}
	
	public function get_Proffesion()
	{
	 	return $this->proffesion;
	}
}

class Manager extends Worker 
{
	public function __construct($rank, $is_Head)
	{
		parent::__construct($rank, 'Manager', $is_Head, 500, 20, 200);
	}
}

class Marketer extends Worker 
{
	public function __construct($rank, $is_Head)
	{
		parent::__construct($rank, 'Marketer', $is_Head, 400, 15, 150);
	}
}
class Engineer extends Worker 
{
	public function __construct($rank, $is_Head)
	{
		parent::__construct($rank, 'Engineer', $is_Head, 200, 5, 50);
	}
}
class Analyst extends Worker 
{
	public function __construct($rank, $is_Head)
	{
		parent::__construct($rank, 'Analyst', $is_Head, 800, 50, 5);
	}
}

$company = new Company;

$departament_purchase = new Departament("Purchase");
$departament_purchase->add_Workers(9, 1, 'Manager', false);
$departament_purchase->add_Workers(3, 2, 'Manager', false);
$departament_purchase->add_Workers(2, 3, 'Manager', false);
$departament_purchase->add_Workers(2, 1, 'Marketer', false);
$departament_purchase->add_Workers(1, 2, 'Manager', true);
$company->add_departament($departament_purchase);

$departament_sales = new Departament("Sales");
$departament_sales->add_Workers(12, 1, 'Manager', false);
$departament_sales->add_Workers(6, 1, 'Marketer', false);
$departament_sales->add_Workers(3, 1, 'Analyst', false);
$departament_sales->add_Workers(2, 2, 'Analyst', false);
$departament_sales->add_Workers(1, 2, 'Marketer', true);
$company->add_departament($departament_sales);

$departament_advertising = new Departament("advertising");
$departament_advertising->add_Workers(15, 1, 'Marketer', false);
$departament_advertising->add_Workers(10, 2, 'Marketer', false);
$departament_advertising->add_Workers(8, 1, 'Manager', false);
$departament_advertising->add_Workers(2, 1, 'Engineer', false);
$departament_advertising->add_Workers(1, 3, 'Marketer', true);
$company->add_departament($departament_advertising);

$departament_logistics = new Departament("Logistics");
$departament_logistics->add_Workers(13, 1, 'Manager', false);
$departament_logistics->add_Workers(5, 2, 'Manager', false);
$departament_logistics->add_Workers(5, 1, 'Engineer', false);
$departament_logistics->add_Workers(1, 1, 'Manager', true);
$company->add_departament($departament_logistics);

function padRight($string, $length)
{
	$long_string = strlen ($string);
	$x = $length-$long_string;
	$space = str_repeat(" ",$x);
	echo $string.$space;
}
 
function padLeft($string, $length)
{
	$long_string = strlen ($string);
	$x = $length-$long_string;
	$space = str_repeat(" ",$x);
	echo $space.$string;
}
$col1 = 12;
$col2 = 12;
$col3 = 16;
 
$company->Destruction_Workers(1.4, Engineer);
echo "ВСего работников ".$company->get_Count_Workers();


/**
// Заголовок таблицы
echo 	padRight ("Departaments",$col1).
   		padLeft("Workers", $col2) . 
     	padLeft("Salary", $col2) . 
     	padLeft("Coffee", $col2) . 
     	padLeft("Pages", $col2) . 
     	padLeft("Salary/Pages", $col3) ."\n";
     	
echo "----------------------------------------------------------------------\n";
$company->print_Info();
echo "----------------------------------------------------------------------\n";
echo padRight('Average', $col2);
echo padLeft($company->get_Average_Workers(), $col2);
echo padLeft($company->get_Average_Salary(), $col2);
echo padLeft($company->get_Average_Coffee(), $col2);
echo padLeft($company->get_Average_Pages(), $col2);
echo padLeft($company->get_Average_Salary_For_Page(), $col3)."\n";
echo "----------------------------------------------------------------------\n";
echo padRight('VSEGO', $col2);
echo padLeft($company->get_Count_Workers(), $col2);
echo padLeft($company->get_Count_Salary(), $col2);
echo padLeft($company->get_Count_Coffee(), $col2);
echo padLeft($company->get_Count_Pages(), $col2);
echo padLeft($company->get_Count_Salary_For_Page(), $col3)."\n";
**/




