<?php
header("Content-Type: text/plain; charset=utf-8");
?> 
<?php
mb_internal_encoding("UTF-8");
//объявление классов >>
abstract class Employee
{
    public $salary;
    public $coffee;
    public $reports;
    public $rank;
    public $isDirector;
    public function __construct($rank, $isDirector=0)
    {
        $this->rank       = $rank;
        $this->isDirector = $isDirector;
    }
}
class Engineer extends Employee
{
    public $salary = 200;
    public $coffee = 5;
    public $reports = 50;
}
class Marketolog extends Employee
{
    public $salary = 400;
    public $coffee = 15;
    public $reports = 150;
}
class Manager extends Employee
{
    public $salary = 500;
    public $coffee = 20;
    public $reports = 200;
}
class Analyst extends Employee
{
    public $salary = 800;
    public $coffee = 50;
    public $reports = 5;
}
class Department
{
    public $name;
    public $workers = array();
    public function __construct($name, $array)
    {
        $this->workers = $array;
        $this->name    = $name;
        /* $this->count   = count($this->workers);
        $this->salary  = $this->getSalary();
        $this->coffee  = $this->getCoffee();
        $this->reports = $this->getReports();
        $this->repSal  = number_format($this->getSalaryReports(), 2); */
    }
    public function getCount()
    {
        return count($this->workers);
    }
    public function getSalary()
    {
        $sumSalary = 0;
        foreach ($this->workers as $worker) {
            $money = $worker->salary;
            if ($worker->rank == 2) {
                $money = $worker->salary * 1.25;
            } elseif ($worker->rank == 3) {
                $money = $worker->salary * 1.5;
            }
            if ($worker->isDirector) {
                $money *= 1.5;
            }
            $sumSalary += $money;
        }
        return $sumSalary;
    }
    public function getCoffee()
    {
        $sumCoffee = 0;
        foreach ($this->workers as $worker) {
            $coffee = $worker->coffee;
            if ($worker->isDirector) {
                $coffee = $worker->coffee * 2;
            }
            $sumCoffee += $coffee;
        }
        return $sumCoffee;
    }
    public function getReports()
    {
        $sumReports = 0;
        foreach ($this->workers as $worker) {
            $reports = $worker->reports;
            if ($worker->isDirector) {
                $reports = $worker->reports * 0;
            }
            $sumReports += $reports;
        }
        return $sumReports;
    }
    public function getSalaryReports()
    {
        return number_format(($this->getSalary() / $this->getReports()), 2);
    }
}
//Создание работников(объектов) внутри департамента >>
function getEmployee($array)
{
    $employees = array();
    foreach ($array as $workers) {
        list($rank, $class, $count, $isDirector) = (explode(', ', $workers));
        for ($i = 1; $i <= $count; $i++) {
            $employee    = new $class($rank, $isDirector);
            $employees[] = $employee;
        }
    }
    return $employees;
}
$ma            = 'Marketolog';
$me            = 'Manager';
$an            = 'Analyst';
$in            = 'Engineer';
$procurWorkers = array(
    "1, $me, 9, 0",
    "2, $me, 3, 0",
    "3, $me, 2, 0",
    "1, $ma, 2, 0",
    "2, $me, 1, 1"
);
$saleWorkers   = array(
    "1, $me, 12, 0",
    "1, $ma, 6, 0",
    "1, $an, 3, 0",
    "2, $an, 2, 0",
    "2, $ma, 1, 1"
);
$adverWorkers  = array(
    "1, $ma, 15, 0",
    "2, $ma, 10, 0",
    "1, $me, 8, 0",
    "1, $in, 2, 0",
    "3, $ma, 1, 1"
);
$logWorkers    = array(
    "1, $me, 13, 0",
    "2, $me, 5, 0",
    "1, $in, 5, 0",
    "1, $me, 1, 1"
);
$procurement   = new Department('Procurement', getEmployee($procurWorkers));
$sales         = new Department('Sales dp', getEmployee($saleWorkers));
$advertising   = new Department('Advertising', getEmployee($adverWorkers));
$logistic      = new Department('Logistic', getEmployee($logWorkers));
$departments   = array(
    $procurement,
    $sales,
    $advertising,
    $logistic
);
//Создание функций для вывода таблицы на экран и подсчет суммы по департаментам
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;
}
$text   = 'Департамент сотр. тугр кофе стр. туг/стр.';
$textAr = explode(' ', $text);
$col1   = 22;
$col2   = 11;
function getShow($array)
{
    global $col1;
    global $col2;
    foreach ($array as $k => $str) {
        if ($k == 0) {
            echo padRight($str, $col1);
        } else {
            echo padLeft($str, $col2);
        }
    }
    echo "\n";
}
//Вывод на экран>>
getShow($textAr);
echo str_repeat('-', 80) . "\n";
$count   = 0;
$coffee  = 0;
$salary  = 0;
$reports = 0;
$repSal  = 0;
foreach ($departments as $department) {
    echo padRight($department->name, $col1);
    echo padLeft($department->getCount(), $col2);
    $count += $department->getCount();
    echo padLeft($department->getSalary(), $col2);
    $salary += $department->getSalary();
    echo padLeft($department->getCoffee(), $col2);
    $coffee += $department->getCoffee();
    echo padLeft($department->getReports(), $col2);
    $reports += $department->getReports();
    echo padLeft($department->getSalaryReports(), $col2) . "\n";
    $repSal += $department->getSalaryReports();
}
echo str_repeat('-', 80) . "\n";
$repSal /= 4;
$total = array(
    'Всего',
    $count,
    $salary,
    $coffee,
    $reports,
    $repSal
);
getShow($total);

