<?php
// your code goes here

/*
Департамент закупок: 9×ме1, 3×ме2, 2×ме3, 2×ма1 + руководитель департамента ме2
Департамент продаж: 12×ме1, 6×ма1, 3×ан1, 2×ан2 + руководитель ма2
Департамент рекламы: 15×ма1, 10×ма2, 8×ме1, 2×ин1 + руководитель ма3
Департамент логистики: 13×ме1, 5×ме2, 5×ин1 + руководитель ме1
*/

error_reporting(-1);

class Employee {
    protected $salary;
    protected $coffee;
    protected $reports;
    protected $rank;
    protected $isHead;

    public function __construct($rank, $isHead) {
        $this->rank = $rank;
        $this->isHead = $isHead;
        $this->setSalary($this->salary);
        $this->setCoffee($this->coffee);
        $this->setReports($this->reports);
    }

    public function getSalary() {
        return $this->salary;
    }
    public function setSalary($salary) {
        switch ($this->rank) {
            case 1:
                $this->salary = $salary;
                break;
            case 2:
                $this->salary = $salary * 1.25;
                break;
            case 3:
                $this->salary = $salary * 1.5;
                break;
            default:
                $this->salary = "Ошибка";
                break;
        }
        if ($this->isHead) {
            $this->salary *= 2;
        }
    }
    public function getCoffee() {
        return $this->coffee;
    }
    public function setCoffee($coffee) {
        $this->coffee = $coffee;
        if ($this->isHead) {
            $this->coffee *= 2;
        }
    }
    public function getReports() {
        return $this->reports;
    }
    public function setReports($reports) {
        if ($this->isHead) {
            $this->reports = 0;
        } else {
            $this->reports = $reports;
        }
    }
    public function getRank()
    {
        return $this->rank;
    }
    public function setRank($rank)
    {
        $this->rank = $rank;
    }
    public function getIsHead()
    {
        return $this->isHead;
    }
    public function setIsHead($isHead)
    {
        $this->isHead = $isHead;
    }

}

class Manager extends Employee {
    public $name = "manager";
    protected $salary = 500;
    protected $coffee = 20;
    protected $reports = 200;
}

class Marketer extends Employee {
    public $name = "marketer";
    protected $salary = 400;
    protected $coffee = 15;
    protected $reports = 150;
}

class Engineer extends Employee {
    protected $salary = 200;
    protected $coffee = 5;
    protected $reports = 50;
}

class Analyst extends Employee {
    protected $salary = 800;
    protected $coffee = 50;
    protected $reports = 5;
}

class Department {
    private $name;
    private $employees;

    public function __construct($name, $employees) {
        $this->name = $name;
        $this->employees = $employees;
    }

    public function getName() {
        return $this->name;
    }
    public function setName($name) {
        $this->name = $name;
    }

    public function getAllEmployees() {
        return count($this->employees);
    }

    public function getAllSalary() {
        $allSalary = 0;
        foreach ($this->employees as $employee) {
            $allSalary += $employee->getSalary();
        }
        return $allSalary;
    }

    public function getAllCoffee() {
        $allCoffee = 0;
        foreach ($this->employees as $employee) {
            $allCoffee += $employee->getCoffee();
        }
        return $allCoffee;
    }

    public function getAllReports() {
        $allReports = 0;
        foreach ($this->employees as $employee) {
            $allReports += $employee->getReports();
        }
        return $allReports;
    }
}

function createManagers($quantity, $rank, $isHead) {
    $employees = array();
    for ($i = 0; $i < $quantity; $i++) {
        array_push($employees, new Manager($rank, $isHead));
    }
    return $employees;
}

function createMarketers($quantity, $rank, $isHead) {
    $employees = array();
    for ($i = 0; $i < $quantity; $i++) {
        array_push($employees, new Marketer($rank, $isHead));
    }
    return $employees;
}

function createEngineers($quantity, $rank, $isHead) {
    $employees = array();
    for ($i = 0; $i < $quantity; $i++) {
        array_push($employees, new Engineer($rank, $isHead));
    }
    return $employees;
}

function createAnalysts($quantity, $rank, $isHead) {
    $employees = array();
    for ($i = 0; $i < $quantity; $i++) {
        array_push($employees, new Analyst($rank, $isHead));
    }
    return $employees;
}

function padRight($string, $length) {
    $spacesCount = $length - mb_strlen($string);
	//echo strlen($string) . $length;
    for ($i = 0; $i < $spacesCount; $i++) {
        $string .= " ";
    }
    return $string;
}

function padLeft($string, $length) {
    $modString = "";
    $spacesCount = $length - mb_strlen($string);
    for ($i = 0; $i < $spacesCount; $i++) {
        $modString .= " ";
    }
    $modString .= $string;
    return $modString;
}

function outputString($str1, $str2, $str3, $str4, $str5, $str6) {
    $col = 11;
    echo padRight($str1, $col);
    echo padLeft($str2, $col);
    echo padLeft($str3, $col);
    echo padLeft($str4, $col);
    echo padLeft($str5, $col);
    echo padLeft($str6, $col) . "\n";
}

$totalEmployees = 0;
$totalSalary = 0;
$totalCoffee = 0;
$totalReports = 0;
$totalConsumption = 0;
$countDepartments = 0;

outputString("Департамент", "сотр.", "тугр.", "кофе", "стр.", "тугр./стр.");

for ($i = 0; $i < 70; $i++) {
    echo "-";
}
echo "\n";

$employees = array();
$departments = array();

$employees = array_merge($employees, createManagers(9, 1, false), createManagers(3, 2, false), 
	createManagers(2, 3, false), createMarketers(2, 1, false), createManagers(1, 2, true));

$purchases = new Department("Закупок", $employees);
array_push($departments, $purchases);
$countDepartments++;

$employees = array();
$employees = array_merge($employees, createManagers(12, 1, false), createMarketers(6, 1, false), 
	createAnalysts(3, 1, false), createAnalysts(2, 2, false), createMarketers(1, 2, true));

$sales = new Department("Продаж", $employees);
array_push($departments, $sales);
$countDepartments++;

$employees = array();
$employees = array_merge($employees, createMarketers(15, 1, false), createMarketers(10, 2, false), 
	createManagers(8, 1, false), createEngineers(2, 1, false), createMarketers(1, 3, true));

$ad = new Department("Рекламы", $employees);
array_push($departments, $ad);
$countDepartments++;

$employees = array();
$employees = array_merge($employees, createManagers(13, 1, false), createManagers(5, 2, false), 
	createEngineers(5, 1, false), createManagers(1, 1, true));

$logistics = new Department("Логистики", $employees);
array_push($departments, $logistics);
$countDepartments++;

foreach ($departments as $department) {

    $employeesCount = $department->getAllEmployees();
    $totalEmployees += $employeesCount;

    $salary = $department->getAllSalary();
    $totalSalary += $salary;

    $coffee = $department->getAllCoffee();
    $totalCoffee += $coffee;

    $reports = $department->getAllReports();
    $totalReports += $reports;

    $consumption = round($salary / $reports, 2);
    $totalConsumption += $consumption;

    outputString($department->getName(), $employeesCount, $salary, $coffee, $reports, $consumption);
}

echo "\n";

outputString("Среднее", $totalEmployees / $countDepartments, $totalSalary / $countDepartments, $totalCoffee / $countDepartments,
    $totalReports / $countDepartments, $totalConsumption / $countDepartments);

outputString("Всего", $totalEmployees, $totalSalary, $totalCoffee, $totalReports, $totalConsumption);