<?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 function __construct($rank, $isDirector)
    {
        $this->rank = $rank;
        if ($isDirector == 1) {
            $this->salary *= 1.5;
            $this->coffee *= 2;
            $this->reports *= 0;
        }
        if ($rank == 2) {
            $this->salary *= 1.25;
        } elseif ($rank == 3) {
            $this->salary *= 1.5;
        }
    }
}
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 $count;
    public $salary;
    public $coffee;
    public $reports;
    public $repSal;
    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) {
            $sumSalary += $worker->salary;
        }
        return $sumSalary;
    }
    public function getCoffee()
    {
        $sumCoffee = 0;
        foreach ($this->workers as $worker) {
            $sumCoffee += $worker->coffee;
        }
        return $sumCoffee;
    }
    public function getReports()
    {
        $sumReports = 0;
        foreach ($this->workers as $worker) {
            $sumReports += $worker->reports;
        }
        return $sumReports;
    }
    public function getSalaryReports()
    {
        return $this->getSalary() / $this->getReports();
    }
    
}

//Создание работников(объектов) внутри департамента >>
function getEmployee($rank, $class, $count, $isDirector = 0)
{
    $employees = array();
    for ($i = 1; $i <= $count; $i++) {
        $employee    = new $class($rank, $isDirector);
        $employees[] = $employee;
    }
    return $employees;
}

$ma = 'Marketolog';
$me = 'Manager';
$an = 'Analyst';
$in = 'Engineer';

$procurement = new Department('Procurement', array_merge(getEmployee(1, $me, 9), getEmployee(2, $me, 3), getEmployee(3, $me, 2), getEmployee(1, $ma, 2), getEmployee(2, $me, 1, 1)));
$sales       = new Department('Sales dp', array_merge(getEmployee(1, $me, 12), getEmployee(1, $ma, 6), getEmployee(1, $an, 3), getEmployee(2, $an, 2), getEmployee(2, $ma, 1, 1)));
$advertising = new Department('Advertising', array_merge(getEmployee(1, $ma, 15), getEmployee(2, $ma, 10), getEmployee(1, $me, 8), getEmployee(1, $in, 2), getEmployee(3, $ma, 1, 1)));
$logistic    = new Department('Logistic', array_merge(getEmployee(1, $me, 13), getEmployee(2, $me, 5), getEmployee(1, $in, 5), getEmployee(1, $me, 1, 1)));
$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->count, $col2);
    $count += $department->count;
    echo padLeft($department->salary, $col2);
    $salary += $department->salary;
    echo padLeft($department->coffee, $col2);
    $coffee += $department->coffee;
    echo padLeft($department->reports, $col2);
    $reports += $department->reports;
    echo padLeft($department->repSal, $col2) . "\n";
    $repSal += $department->repSal;
}
echo str_repeat('-', 80) . "\n";
$repSal /= 4;
$total = array(
    'Всего',
    $count,
    $salary,
    $coffee,
    $reports,
    $repSal
);
getShow($total);


