<?php

error_reporting(-1);

class Department
{
	public function createEmployee($amount, $profession, $rank, $chief = 0) 
    {
        for($i=1; $i <= $amount; $i++) {
            $this->employees[] = new Employee($profession, $rank, $chief);
        }
    }
    
    public function __construct($name) 
    {
    	$this->name = $name;
    }
    
    public function DepartmentEmployeersNumber()
    {
    	return count($this->employees);
    }
    
    public function DepartmentPayment()
    {
    	$departmentPayment = 0;
    	foreach ($this->employees as $employee) {
    		$departmentPayment += $employee->payment;
    	}
    	return $departmentPayment;
    }
    
    public function DepartmentCoffeeSpending() 
    {
    	$departmentCoffee = 0;
    	foreach ($this->employees as $employee) {
    		$departmentCoffee += $employee->coffee;
    	}
    	return $departmentCoffee;
    }
    
    public function DepartmentPages() 
    {
    	$departmentPages = 0;
    	foreach ($this->employees as $employee) {
    		$departmentPages += $employee->pages;
    	}
    	return $departmentPages;
    }
    
    public function DepartmentEfficiency() 
    {
    	return round ($this->DepartmentPayment() / $this->DepartmentPages(), 1);
    }
}

class Employee
{
	public $payment;
	public $coffee;
	public $pages;
	public $rank;
	
	public function __construct($profession, $rank, $chief = 0)
	{
		$this->rank = $rank;
		
		if ($profession == 'manager') {
			$this->payment = 500;
			$this->coffee = 20;
			$this->pages = 200;
		} elseif ($profession == 'marketer') {
			$this->payment = 400;
			$this->coffee = 15;
			$this->pages = 150;
		} elseif ($profession == 'engineer') {
			$this->payment = 200;
			$this->coffee = 5;
			$this->pages = 50;
		} elseif ($profession == 'analyst') {
			$this->payment = 800;
			$this->coffee = 50;
			$this->pages = 5;
		}
			
		if ($this->rank == 2) {
			$this->payment *= 1.25;
		} elseif ($this->rank == 3) {
			$this->payment *= 1.5;
		}
			
		if ($chief == 1) {
			$this->payment *= 1.5;
			$this->coffee *= 2;
			$this->pages = 0;
		}
	}
}

$employees = array();

$purchases = new Department('Закупок');
$purchases->createEmployee(9, 'manager', 1);
$purchases->createEmployee(3, 'manager', 2); 
$purchases->createEmployee(2, 'manager', 3); 
$purchases->createEmployee(2, 'marketer', 1);
$purchases->createEmployee(1, 'manager', 2, 1);
$employees[] = $purchases;

$sales = new Department('Продаж');
$sales->createEmployee(12, 'manager', 1);
$sales->createEmployee(6, 'marketer', 1);
$sales->createEmployee(3, 'analyst', 1);
$sales->createEmployee(2, 'analyst', 2);
$sales->createEmployee(1, 'marketer', 2, 1);
$employees[] = $sales;

$advertising = new Department('Рекламы');
$advertising->createEmployee(15, 'marketer', 1);
$advertising->createEmployee(10, 'marketer', 2);
$advertising->createEmployee(8, 'manager', 1);
$advertising->createEmployee(2, 'engineer', 1);
$advertising->createEmployee(1, 'marketer', 3, 1);
$employees[] = $advertising;

$logistics = new Department('Логистики');
$logistics->createEmployee(13, 'manager', 1);
$logistics->createEmployee(5, 'manager', 2);
$logistics->createEmployee(5, 'engineer', 1);
$logistics->createEmployee(1, 'manager', 1, 1);
$employees[] = $logistics;

$col1 = 14;
$col2 = 5;
$col3 = 10;
$col4 = 9;
$col5 = 7;
$col6 = 13;

function padLeft($string, $length)
{
	$count = $length - mb_strlen($string);
	$space = str_repeat(' ', $count);
	echo $space . $string;
}

function padRight($string, $length)
{
	$count = $length - mb_strlen($string);
	$space = str_repeat(' ', $count);
	echo $string . $space;
}

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

echo str_repeat("-", 58)."\n";

$totalEmployees = 0;
$totalPayment = 0;
$totalCoffeeSpending = 0;
$totalPages = 0;
$totalEfficiency = 0;
foreach ($employees as $department) {
	echo padRight($department->name, $col1) .
		 padLeft($department->DepartmentEmployeersNumber(), $col2) .
		 padLeft($department->DepartmentPayment(), $col3) .
		 padLeft($department->DepartmentCoffeeSpending(), $col4) .
		 padLeft($department->DepartmentPages(), $col5) .
		 padLeft($department->DepartmentEfficiency(), $col6) .
		 "\n";
		 $totalEmployees += $department->DepartmentEmployeersNumber();
		 $totalPayment += $department->DepartmentPayment();
		 $totalCoffeeSpending += $department->DepartmentCoffeeSpending();
		 $totalPages += $department->DepartmentPages();
		 $totalEfficiency += $department->DepartmentEfficiency();
}

echo str_repeat("-", 58)."\n";

	echo padRight("Среднее", $col1) .
		 padLeft(floor($totalEmployees / count($employees)), $col2) .
		 padLeft(floor($totalPayment / count($employees)), $col3) .
		 padLeft(floor($totalCoffeeSpending / count($employees)), $col4) .
		 padLeft(floor($totalPages / count($employees)), $col5) .
		 padLeft(floor($totalEfficiency / count($employees)), $col6) .
		 "\n";

	echo padRight("Всего", $col1) .
		 padLeft($totalEmployees, $col2) .
		 padLeft($totalPayment, $col3) .
		 padLeft($totalCoffeeSpending, $col4) .
		 padLeft($totalPages, $col5) .
		 padLeft($totalEfficiency, $col6) .
		 "\n";
	 