<?php

// your code goes here<?php

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

class Worker
{
	public $occupation; // профессия
	public $ifChief;
	public $rank; // ранг

	public $coffeeAmount;
	public $salary;
	public $reportPages;

	public function __construct($occupation, $ifChief, $rank)
	{
		$this->occupation = $occupation;
		$this->ifChief = $ifChief;
		$this->rank = $rank;
	}

	public function getSalary()
	{
		$salary = 0;

		switch ($this->occupation) { // зависимость зарплаты от профессии
			case 'me':
				$salary += 500;
				break;
			case 'ma':
				$salary += 400;
				break;
			case 'en':
				$salary += 200;
				break;
			case 'an':
				$salary += 800;
				break;
		}

		switch ($this->rank) { // зависимость зарплаты от ранга
			case 2:
				$salary *= 1.25;
				break;
			case 3:
				$salary *= 1.5;
				break;
			default:
				break;
		}

		if ($this->ifChief) {
			$salary *= 1.5;
		}

		return $salary;
	}

	public function getCoffeeAmount()
	{
		$coffeeAmount = 0;

		switch ($this->occupation) {
			case 'me':
				$coffeeAmount += 20;
				break;
			case 'ma':
				$coffeeAmount += 15;
				break;
			case 'en':
				$coffeeAmount += 5;
				break;
			case 'an':
				$coffeeAmount += 50;
				break;
		}

		if ($this->ifChief) {
			$coffeeAmount *= 2;
		}

		return $coffeeAmount;
	}

	public function getReportPages()
	{
		$reportPages = 0;

		switch ($this->occupation) {
			case 'me':
				$reportPages += 200;
				break;
			case 'ma':
				$reportPages += 150;
				break;
			case 'en':
				$reportPages += 50;
				break;
			case 'an':
				$reportPages += 5;
				break;
		}

		if ($this->ifChief) {
			$reportPages = 0;
		}

		return $reportPages;
	}
}

function fillArrayWithWorkers($array, $number, $occupation, $ifChief, $rank) 
{
	$last = count($array);
	for ($i = 0; $i < $number; $i++) {
		$array[$last+$i] = new Worker($occupation, $ifChief, $rank);
	}
	return $array;
}

$procurementDept = array(); // департамент закупок
$procurementDept = fillArrayWithWorkers($procurementDept, 9, 'me', false, 1);
$procurementDept = fillArrayWithWorkers($procurementDept, 3, 'me', false, 2);
$procurementDept = fillArrayWithWorkers($procurementDept, 2, 'me', false, 3);
$procurementDept = fillArrayWithWorkers($procurementDept, 2, 'ma', false, 1);
$procurementDept = fillArrayWithWorkers($procurementDept, 1, 'me', true, 2);

$sailsDept = array();
$sailsDept = fillArrayWithWorkers($sailsDept, 12, 'me', false, 1);
$sailsDept = fillArrayWithWorkers($sailsDept, 6, 'ma', false, 1);
$sailsDept = fillArrayWithWorkers($sailsDept, 3, 'an', false, 1);
$sailsDept = fillArrayWithWorkers($sailsDept, 2, 'an', false, 2);
$sailsDept = fillArrayWithWorkers($sailsDept, 1, 'ma', true, 2);

$adsDept = array();
$adsDept = fillArrayWithWorkers($adsDept, 15, 'ma', false, 1);
$adsDept = fillArrayWithWorkers($adsDept, 10, 'ma', false, 2);
$adsDept = fillArrayWithWorkers($adsDept, 8, 'me', false, 1);
$adsDept = fillArrayWithWorkers($adsDept, 2, 'en', false, 1);
$adsDept = fillArrayWithWorkers($adsDept, 1, 'ma', true, 3);

$logisticDept = array();
$logisticDept = fillArrayWithWorkers($logisticDept, 13, 'me', false, 1);
$logisticDept = fillArrayWithWorkers($logisticDept, 5, 'me', false, 2);
$logisticDept = fillArrayWithWorkers($logisticDept, 5, 'en', false, 1);
$logisticDept = fillArrayWithWorkers($logisticDept, 1, 'me', true, 1);


function getDeptSalary ($dept) 
{
	$deptSalary = 0;

	foreach ($dept as $worker) {
		$deptSalary += $worker->getSalary();
	}
	return $deptSalary;
}
$procurementDeptSalary = getDeptSalary($procurementDept);
$sailsDeptSalary = getDeptSalary($sailsDept);
$adsDeptSalary = getDeptSalary($adsDept);
$logisticDeptSalary = getDeptSalary($logisticDept);
$totalSalary = $procurementDeptSalary + $sailsDeptSalary +
					$adsDeptSalary + $logisticDeptSalary;
$averageSalary = $totalSalary / 4;

function getDeptCoffeeAmount ($dept) 
{
	$deptCoffee = 0;
	foreach ($dept as $worker) {
		$deptCoffee += $worker->getCoffeeAmount();
	}
	return $deptCoffee;
}
$procurementDeptCoffeeAmount = getDeptCoffeeAmount($procurementDept);
$sailsDeptCoffeeAmount = getDeptCoffeeAmount($sailsDept);
$adsDeptCoffeeAmount = getDeptCoffeeAmount($adsDept);
$logisticDeptCoffeeAmount = getDeptCoffeeAmount($logisticDept);
$totalCoffeeAmount = $procurementDeptCoffeeAmount + $sailsDeptCoffeeAmount +
					 $adsDeptCoffeeAmount + $logisticDeptCoffeeAmount;
$averageCoffeeAmount = $totalCoffeeAmount / 4;

function getDeptReportPages ($dept) 
{
	$deptReportPages = 0;
	foreach ($dept as $worker) {
		$deptReportPages += $worker->getReportPages();
	}
	return $deptReportPages;
}
$procurementDeptRP = getDeptReportPages($procurementDept);
$sailsDeptRP = getDeptReportPages($sailsDept);
$adsDeptRP = getDeptReportPages($adsDept);
$logisticDeptRP = getDeptReportPages($logisticDept);
$totalReportPages = $procurementDeptRP + $sailsDeptRP + $adsDeptRP + $logisticDeptRP;
$averageReportPages = $totalReportPages / 4;

function getSalaryPerPage ($dept)
{
	$salary = getDeptSalary($dept);
	$reportPages = getDeptReportPages($dept);
	$salaryPerPage = $salary / $reportPages;
	$salaryPerPage = round($salaryPerPage, 2);

	return $salaryPerPage;
}
$procurementDeptSPP = getSalaryPerPage($procurementDept);
$sailsDeptSPP = getSalaryPerPage($sailsDept);
$adsDeptSPP = getSalaryPerPage($adsDept);
$logisticDeptSPP = getSalaryPerPage($logisticDept);
$totalSalaryPerPage = $procurementDeptSPP + $sailsDeptSPP + $adsDeptSPP + $logisticDeptSPP;
$averageSalaryPerPage = $totalSalaryPerPage / 4;

$totalWorkers = count($procurementDept) + count($sailsDept) + 
				count($adsDept) + count($logisticDept);
$averageWorkers = $totalWorkers / 4;

// Функции для генерирования таблиц
function padLeft($string, $length)
{
	$strLength = mb_strlen($string);
	$spaces = 0;

	if ($strLength < $length) {
		$spaces = $length - $strLength;
	}

	for ($i = 0; $i < $spaces; $i++) {
		echo " ";
	}

	echo $string;
}
function padRight($string, $length)
{
	$strLength = mb_strlen($string);
	$spaces = 0;

	if ($strLength < $length) {
		$spaces = $length - $strLength;
	}
	
	echo $string;

	for ($i = 0; $i < $spaces; $i++) {
		echo " ";
	}
}
// Ширина колонок
$col1 = 15;
$col2 = 10;

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

// таблица
function printDeptStats ($deptName, $dept)
{
	$col1 = 15;
	$col2 = 10;

	echo padRight($deptName, $col1) . 
	padLeft(count($dept), $col2) . // число сотрудников
	padLeft(getDeptSalary($dept), $col2) . 
	padLeft(getDeptCoffeeAmount($dept), $col2) . 
	padLeft(getDeptReportPages($dept), $col2) . 
	padLeft(getSalaryPerPage($dept), $col1) . "\n"; // 
}

printDeptStats("Депт. закупок", $procurementDept);
printDeptStats("Депт. продаж", $sailsDept);
printDeptStats("Депт. рекламы", $adsDept);
printDeptStats("Депт. логистики", $logisticDept);
echo "-----------------\n";

// Среднее
echo padRight("Среднее", $col1) . 
	 padLeft($averageWorkers, $col2) . 
	 padLeft($averageSalary, $col2) . 
	 padLeft($averageCoffeeAmount, $col2) . 
	 padLeft($averageReportPages, $col2) . 
	 padLeft($averageSalaryPerPage, $col1) . "\n-----------------\n";

// Всего
echo padRight("Всего", $col1) . 
	 padLeft($totalWorkers, $col2) . 
	 padLeft($totalSalary, $col2) . 
	 padLeft($totalCoffeeAmount, $col2) . 
	 padLeft($totalReportPages, $col2) . 
	 padLeft($totalSalaryPerPage, $col1) . "\n-----------------\n";