<?php
mb_internal_encoding('utf-8');
class Employee
{
    public $salary;
    public $cofee;
    public $pages;
    
    public function __construct($salary, $cofee, $pages)
    {
        $this->salary = $salary;
        $this->cofee  = $cofee;
        $this->pages  = $pages;
    }
}

$me = new Employee(500, 20, 200);
$ma = new Employee(400, 15, 150);
$in = new Employee(200, 5, 50);
$an = new Employee(800, 50, 5);

class Department
{
    public $name;
    public $meEmployees = array(0, 0, 0); //array(lvl1,lvl2,lvl3)
    public $maEmployees = array(0, 0, 0);
    public $inEmployees = array(0, 0, 0);
    public $anEmployees = array(0, 0, 0);
    public $head = array("" => 0); //array(type=>lvl)
}

$purch                 = new Department;
$purch->name           = "Закупок";
$purch->meEmployees[0] = 9;
$purch->meEmployees[1] = 3;
$purch->meEmployees[2] = 2;
$purch->maEmployees[0] = 2;
$purch->head           = array(
    "me" => 2
);

$sale                 = new Department;
$sale->name           = "Продаж";
$sale->meEmployees[0] = 12;
$sale->maEmployees[0] = 6;
$sale->anEmployees[0] = 3;
$sale->anEmployees[1] = 12;
$sale->head           = array(
    "ma" => 2
);

$advert                 = new Department;
$advert->name           = "Рекламы";
$advert->maEmployees[0] = 15;
$advert->maEmployees[1] = 10;
$advert->meEmployees[0] = 8;
$advert->inEmployees[0] = 2;
$advert->head           = array(
    "ma" => 3
);

$logist                 = new Department;
$logist->name           = "Логистики";
$logist->meEmployees[0] = 13;
$logist->meEmployees[1] = 5;
$logist->meEmployees[0] = 5;
$logist->head           = array(
    "me" => 1
);


function drawTable($array)
{
    //ПОИСК МАКСИМАЛЬНОЙ ШИРИНЫ ЯЧЕЙКИ
    $maxWidth = 0;
    $cells    = 0;
    foreach ($array as $key => $value) {
        $cells = $cells < count($value) ? count($value) : $cells;
        foreach ($value as $word)
            $maxWidth = $maxWidth < (mb_strlen($word)) ? mb_strlen($word) : $maxWidth;
    }
    
    //ОТРИСОВКА ТАБЛИЦЫ
    drawLine($maxWidth, $cells);
    foreach ($array as $key => $value) {
        echo "|";
        
        foreach ($value as $word) {
            $word = mb_strlen($word) < $maxWidth ? $word . str_repeat(" ", $maxWidth - mb_strlen($word)) : $word;
            echo $word . "|";
        }
        echo "\n";
        drawLine($maxWidth, $cells);
    }
}

function drawLine($maxWidth, $cells)
{
    echo "+";
    for ($i = 1; $i <= $cells; $i++)
        echo str_repeat("-", $maxWidth) . "+";
    echo "\n";
}

$table[0] = array(
    "Департамент",
    "сотр.",
    "з/п",
    "кофе",
    "стр.",
    "постр.з/п"
);
$table[1] = countDep($purch, $me, $ma, $in, $an);
$table[2] = countDep($sale, $me, $ma, $in, $an);
$table[3] = countDep($advert, $me, $ma, $in, $an);
$table[4] = countDep($logist, $me, $ma, $in, $an);

function countDep($obj, $me, $ma, $in, $an)
{
    $result[0] = $obj->name;
    $result[1] = array_sum($obj->meEmployees) + array_sum($obj->maEmployees) + array_sum($obj->inEmployees) + array_sum($obj->anEmployees);
    
    $sum      = 0;
    $sumCofee = 0;
    $sumPages = 0;
    for ($typeEmplCounter = 1; $typeEmplCounter <= 4; $typeEmplCounter++) {
        switch ($typeEmplCounter) {
            case "1":
                $typeEmpl = 'meEmployees';
                $emplCl   = $me;
                break;
            case "2":
                $typeEmpl = 'maEmployees';
                $emplCl   = $ma;
                break;
            case "3":
                $typeEmpl = 'inEmployees';
                $emplCl   = $in;
                break;
            case "4":
                $typeEmpl = 'anEmployees';
                $emplCl   = $an;
                break;
        }
        foreach ($obj->$typeEmpl as $key => $empl) {
            $k = $key > 0 ? ($key > 1 ? 1.5 : 1.25) : 1; //  :3 УЧЁТ ЛЕВЕЛА :3
            $sum += $k * $empl * $emplCl->salary;
            $sumCofee += $empl * $emplCl->cofee;
            $sumPages += $empl * $emplCl->pages;
        }
        
    }
    
    $result[2] = $sum;
    $result[3] = $sumCofee;
    $result[4] = $sumPages;
    $result[5] = round($sum / $sumPages, 2);
    return $result;
}

//ПОКА ЧТО БЕЗ УЧЕТА НАЧАЛЬНИКОВ ОТДЕЛА И ГРАФ "СРЕДНЕГО"-"ВСЕГО"
drawTable($table);