<?php

abstract class Employee
{
    
    public $salary;
    public $pages;
    public $cofee;
    public $name;
    public $isHead = false;
    
    
    function __construct($rank)
    {
        
        
        $this->rank = $rank;
    }
    
    function getSalary()
    {
        $extra = 1;
        if ($this->isHead == true) {
            $extra = 1.50;
        }
        $multiply = 1;
        switch ($this->rank) {
            case 2:
                $multiply = 1.25;
                break;
            case 3:
                $multiply = 1.50;
                break;
        }
        return $this->salary * $multiply * $extra;
        
    }
    
    function setHead()
    {
        $this->isHead = true;
        
    }
    
}

class Manager extends Employee
{
    public $name = "Менеджер";
    public $salary = 500;
    public $cofee = 20;
    public $pages = 200;
}

class Market extends Employee
{
    public $name = "Маркетолог";
    public $salary = 400;
    public $cofee = 15;
    public $pages = 150;
}

class Engineer extends Employee
{
    public $name = "Инженер";
    public $salary = 200;
    public $cofee = 5;
    public $pages = 50;
}

class Analyst extends Employee
{
    public $name = "Аналитик";
    public $salary = 800;
    public $cofee = 50;
    public $pages = 5;
}


class Department
{
    public $data;
    public $name;
    function workers()
    {
        return count($this->data);
    }
    function cofee()
    {
        $cofee = 0;
        foreach ($this->data as $key => $worker) {
            if ($worker->isHead == true) {
                $cofee += $worker->cofee * 2;
            } else {
                $cofee += $worker->cofee;
            }
        }
        return $cofee;
    }
    function costs()
    {
        $costs = 0;
        foreach ($this->data as $key => $worker) {
            
            $costs += $worker->getSalary();
        }
        return $costs;
    }
    function pages()
    {
        $pages = 0;
        foreach ($this->data as $key => $worker) {
            if ($worker->isHead == true) {
                continue;
            }
            $pages += $worker->pages;
        }
        return $pages;
    }
    function costPerPage()
    {
        return round(($this->costs() / $this->pages()), 1);
    }
    
    function __construct($name)
    {
        
        $this->name = $name;
        
    }
    
    function addEmployee($data)
    {
        if (is_array($data)) {
            foreach ($data as $employee) {
                $this->addEmployee($employee);
            }
        } else {
            return $this->data[] = $data;
        }
    }
    function addBoss($boss)
    {
        $boss->setHead();
        return $this->data[] = $boss;
    }
}


function createEmployee($type, $rank, $quantity)
{
    $array = array();
    for ($i = 0; $i < $quantity; $i++) {
        $array[] = new $type($rank);
    }
    return $array;
    
}

function padRight($name, $col)
{
    while (mb_strlen($name) < $col) {
        $name .= " ";
    }
    return $name;
}

function padLeft($name, $col)
{
    while (mb_strlen($name) < $col) {
        $name = " " . $name;
    }
    return $name;
}


function showDepartment($department)
{
    
    $col1 = 20;
    $col2 = 6;
    $col3 = 10;
    
    
    echo padRight($department->name, $col1);
    echo padLeft($department->workers(), $col2);
    echo padLeft($department->costs(), $col3);
    echo padLeft($department->cofee(), $col2);
    echo padLeft($department->pages(), $col3);
    echo padLeft($department->costPerPage(), $col3);
    echo "\n";
}

$department1 = new Department('Закупок');
$department1->addEmployee(createEmployee('Manager', 1, 9));
$department1->addEmployee(createEmployee('Manager', 2, 3));
$department1->addEmployee(createEmployee('Manager', 3, 2));
$department1->addEmployee(createEmployee('Market', 1, 2));
$department1->addBoss(new Manager(2));

$department2 = new Department("Продаж");
$department2->addEmployee(createEmployee('Manager', 1, 12));
$department2->addEmployee(createEmployee('Market', 1, 6));
$department2->addEmployee(createEmployee('Analyst', 1, 3));
$department2->addEmployee(createEmployee('Analyst', 2, 2));
$department2->addBoss(new Market(2));


$department3 = new Department("Рекламы");
$department3->addEmployee(createEmployee('Market', 1, 15));
$department3->addEmployee(createEmployee('Market', 2, 10));
$department3->addEmployee(createEmployee('Manager', 1, 8));
$department3->addEmployee(createEmployee('Engineer', 1, 2));
$department3->addBoss(new Market(3));



$col1 = 20;
$col2 = 12;

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

showDepartment($department1);
showDepartment($department2);
showDepartment($department3);