<?php

mb_internal_encoding("UTF-8");

class Worker
{
    public function __construct($salary, $coffee, $reports, $a, $b, $c) //$a - 1 если 2 ранг, 0 если нет, $b - 1 (3 ранг), $c - 1 (если директор)
    {
        $salarySecond  = $salary + $salary * 0.25 * $a;
        $salaryThird   = $salarySecond + $salarySecond * 0.5 * $b;
        $salaryDir     = $salaryThird + $salaryThird * 0.5 * $c;
        $coffeeDir     = $coffee + $coffee * $c;
        $reportsDir    = $reports * (1 - $c);
        $this->salary  = $salaryDir;
        $this->coffee  = $coffeeDir;
        $this->reports = $reportsDir;
    }
}
$me = array(
    500,
    20,
    200
);
$ma = array(
    400,
    15,
    150
);
$en = array(
    200,
    5,
    50
);
$an = array(
    800,
    50,
    5
);
function doWorkers($array, $x, $a, $b, $c)
{
    $workers = array();
    for ($i = 1; $i <= $x; $i++) {
        list($k1, $k2, $k3) = $array;
        $worker    = new Worker($k1, $k2, $k3, $a, $b, $c);
        $workers[] = $worker;
    }
    return $workers;
}
$depZakupok   = array();
$depZakupok[] = doWorkers($me, 9, 0, 0, 0);
$depZakupok[] = doWorkers($me, 3, 1, 0, 0);
$depZakupok[] = doWorkers($me, 2, 0, 1, 0);
$depZakupok[] = doWorkers($ma, 2, 0, 0, 0);
$depZakupok[] = doWorkers($ma, 1, 0, 0, 1);
$depProdaj    = array();
$depProdaj[]  = doWorkers($me, 12, 0, 0, 0);
$depProdaj[]  = doWorkers($ma, 6, 0, 0, 0);
$depProdaj[]  = doWorkers($an, 3, 0, 0, 0);
$depProdaj[]  = doWorkers($an, 2, 1, 0, 0);
$depProdaj[]  = doWorkers($ma, 1, 1, 0, 1);
$depReklamy   = array();
$depReklamy[] = doWorkers($ma, 15, 0, 0, 0);
$depReklamy[] = doWorkers($ma, 10, 1, 0, 0);
$depReklamy[] = doWorkers($me, 8, 0, 0, 0);
$depReklamy[] = doWorkers($en, 2, 0, 0, 0);
$depReklamy[] = doWorkers($ma, 1, 0, 1, 1);
$depLogist    = array();
$depLogist[]  = doWorkers($me, 13, 0, 0, 0);
$depLogist[]  = doWorkers($me, 5, 1, 0, 0);
$depLogist[]  = doWorkers($en, 5, 0, 0, 0);
$depLogist[]  = doWorkers($me, 1, 0, 0, 0);
$vektor       = array(
    $depZakupok,
    $depProdaj,
    $depReklamy,
    $depLogist
);

$countLogist = count($depLogist, 1) - count($depLogist);
$countRek    = count($depReklamy, 1) - count($depReklamy);
$countProd   = count($depProdaj, 1) - count($depProdaj);
$countZak    = count($depZakupok, 1) - count($depZakupok);
$vektorCount = $countLogist + $countRek + $countProd + $countZak;


function doTotalSalaryDep($array)
{
	$summary = 0;
    foreach ($array as $group) {
        foreach ($group as $worker) {
            $summary += $worker->salary . "\n";
        }
    }
    return $summary;
}
$zakSalary    = doTotalSalaryDep($depZakupok);
$prodSalary   = doTotalSalaryDep($depProdaj);
$rekSalary    = doTotalSalaryDep($depReklamy);
$logSalary    = doTotalSalaryDep($depLogist);
$vektorSalary = $zakSalary + $prodSalary + $rekSalary + $logSalary;


function doTotalReportsDep($array)
{
	$summary = 0;
    foreach ($array as $group) {
        foreach ($group as $worker) {
            $summary += $worker->reports;
        }
    }
    return $summary;
}
$zakRep    = doTotalReportsDep($depZakupok);
$prodRep   = doTotalReportsDep($depProdaj);
$rekRep    = doTotalReportsDep($depReklamy);
$logRep    = doTotalReportsDep($depLogist);
$vektorRep = $zakRep + $prodRep + $rekRep + $logRep;

function doTotalCoffeeDep($array)
{
	$summary = 0;
    foreach ($array as $group) {
        foreach ($group as $worker) {
            $summary += $worker->coffee;
        }
    }
    return $summary;
}
$zakCoffee    = doTotalCoffeeDep($depZakupok);
$prodCoffee   = doTotalCoffeeDep($depProdaj);
$rekCoffee    = doTotalCoffeeDep($depReklamy);
$logCoffee    = doTotalCoffeeDep($depLogist);
$vektorCoffee = $zakCoffee + $prodCoffee + $rekCoffee + $logCoffee;

$zakRepSal    = $zakSalary / $zakRep;
$prodRepSal   = $prodSalary / $prodRep;
$rekRepSal    = $rekSalary / $rekRep;
$logRepSal    = $logSalary / $logRep;
$vektorRepSal = $vektorSalary / $vektorRep;


$col1 = 20;
$col2 = 8;

function padRight($string, $arg)
{
    $count = $arg - mb_strlen($string);
    if ($count <= 0) {
        return $string;
    }
    $space = str_repeat(' ', $count);
    return $string . $space;
}
function padLeft($string, $arg)
{
    $count = $arg - mb_strlen($string);
    if ($count <= 0) {
        return $string;
    }
    $space = str_repeat(' ', $count);
    return $space . $string;
}
echo padRight('Департамент', $col1) . padLeft('сотр.', $col2) . padLeft('тугр', $col2) . padLeft('кофе', $col2) . padLeft('стр.', $col2) . padLeft('тугр./стр.', $col1) . "\n";
echo str_repeat('-', $col1 * 2 + $col2 * 4 + 5) . "\n";
echo padRight('Деп. Закупок', $col1) . padLeft("$countZak", $col2) . padLeft("$zakSalary", $col2) . padLeft("$zakCoffee", $col2) . padLeft("$zakRep", $col2) . padLeft(number_format($zakRepSal, 2), $col1) . "\n";
echo padRight('Деп. Продаж', $col1) . padLeft("$countProd", $col2) . padLeft("$prodSalary", $col2) . padLeft("$prodCoffee", $col2) . padLeft("$prodRep", $col2) . padLeft(number_format($prodRepSal, 2), $col1) . "\n";
echo padRight('Деп. Рекламы', $col1) . padLeft("$countRek", $col2) . padLeft("$rekSalary", $col2) . padLeft("$rekCoffee", $col2) . padLeft("$rekRep", $col2) . padLeft(number_format($rekRepSal, 2), $col1) . "\n";
echo padRight('Деп. Логистики', $col1) . padLeft("$countLogist", $col2) . padLeft("$logSalary", $col2) . padLeft("$logCoffee", $col2) . padLeft("$logRep", $col2) . padLeft(number_format($logRepSal, 2), $col1) . "\n";
echo str_repeat('-', $col1 * 2 + $col2 * 4 + 5) . "\n";
echo padRight('ВСЕГО:', $col1) . padLeft("$vektorCount", $col2) . padLeft("$vektorSalary", $col2) . padLeft("$vektorCoffee", $col2) . padLeft("$vektorRep", $col2) . padLeft(number_format($vektorRepSal, 2), $col1) . "\n";