<?php

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

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
{
    function workers()
    {
        return count($this->data) - 1;
    }
    function cofee()
    {
        $cofee = 0;
        foreach ($this->data as $key => $worker) {
            if ($key === 'name') {
                continue;
            }
            $cofee += $worker->cofee;
        }
        return $cofee;
    }
    function costs()
    {
        $costs = 0;
        foreach ($this->data as $key => $worker) {
            if ($key === 'name') {
                continue;
            }
            $costs += $worker->getSalary();
        }
        return $costs;
    }
    function pages()
    {
        $pages = 0;
        foreach ($this->data as $key => $worker) {
            if ($key === 'name') {
                continue;
            }
            $pages += $worker->pages;
        }
        return $pages;
    }
    
    function __construct($data)
    {
        $this->data = $data;
        $this->name = $data['name'];
        
    }
}


function createEmployee($type, $rank, $quantity, $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 createDepartment($data)
{
    $department  = new Department($data);
    $costPerPage = round(($department->costs()) / ($department->pages()), 1);
    
    echo padRight($department->name, 20);
    echo padLeft($department->workers(), 10);
    echo padLeft($department->costs(), 12);
    echo padLeft($department->cofee(), 10);
    echo padLeft($department->pages(), 12);
    echo padLeft($costPerPage, 15);
    echo "\n";
}

$department1['name'] = "Закупок";
$department1         = createEmployee('Manager', 1, 9, $department1);
$department1         = createEmployee('Manager', 2, 3, $department1);
$department1         = createEmployee('Manager', 3, 2, $department1);
$department1         = createEmployee('Market', 1, 2, $department1);
$department1['head'] = new Manager(2);
$department1['head']->setHead();

$department2['name'] = "Продаж";
$department2         = createEmployee('Manager', 1, 12, $department2);
$department2         = createEmployee('Market', 1, 6, $department2);
$department2         = createEmployee('Analyst', 1, 3, $department2);
$department2         = createEmployee('Analyst', 2, 2, $department2);
$department2['head'] = new Market(2);
$department2['head']->setHead();

$department3['name'] = "Рекламы";
$department3         = createEmployee('Market', 1, 15, $department3);
$department3         = createEmployee('Market', 2, 10, $department3);
$department3         = createEmployee('Manager', 1, 8, $department3);
$department3         = createEmployee('Engineer', 1, 2, $department3);
$department['head']  = new Market(3);
$department['head']->setHead();

echo padRight("Департамент", 30) . padLeft("Сотр.", 10) . padLeft("тугр.", 15) . padLeft("кофе", 15) . padLeft("стр.", 15) . padLeft("тугр./стр.", 25) . "\n";

createDepartment($department1);
createDepartment($department2);
createDepartment($department3);