<?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 function __construct($isDirector)
    {
        switch ($isDirector) {
            case 1:
                $this->salary *= 1.5;
                $this->coffee *= 2;
                $this->reports = 0;
                break;
            case 0:
                break;
        }
    }
}
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 $workers = array();
    public function __construct($array)
    {
        $this->workers = $array;
    }
    public function doCount()
    {
        $c = count($this->workers, 2);
        return $c;
    }
    public function doSalary()
    {
    	$sumSalary = 0;
        foreach ($this->workers as $object) {
            foreach ($object as $k => $v) {
                if ($k === 'salary') {
                    $salary = $v;
                }
            }
            $sumSalary += $salary;
        }
        return $sumSalary;
    }
    public function doCoffee()
    {
    	$sumCoffee = 0;
        foreach ($this->workers as $object) {
            foreach ($object as $k => $v) {
                if ($k === 'coffee') {
                    $coffee = $v;
                }
            }
            $sumCoffee += $coffee;
        }
        return $sumCoffee;
    }
    public function doReports()
    {
    	$sumReports = 0;
        foreach ($this->workers as $object) {
            foreach ($object as $k => $v) {
                if ($k === 'reports') {
                    $reports = $v;
                }
            }
            $sumReports += $reports;
        }
        return $sumReports;
    }
    public function doSalaryReports()
    {
        return $this->doSalary() / $this->doReports();
    }
    public function doShow()
    {
        $show   = array();
        $show[] = get_class($this);
        $show[] = $this->doCount();
        $show[] = $this->doSalary();
        $show[] = $this->doCoffee();
        $show[] = $this->doReports();
        $show[] = $this->doSalaryReports();
        return $show;
    }
    
}

class Procurement extends Department
{
}
class Sales extends Department
{
}
class Advertising extends Department
{
}
class Logistic extends Department
{
}
//Создание работников(объектов) внутри департаментов(объектов) >>
function doEmployeeFirstRank($class, $count, $isDirector = 0)
{
    $employees = array();
    for ($i = 1; $i <= $count; $i++) {
        $employee    = new $class($isDirector);
        $employees[] = $employee;
    }
    return $employees;
}
function doEmployeeSecondRank($class, $count, $isDirector = 0)
{
    $employees = array();
    for ($i = 1; $i <= $count; $i++) {
        $employee = new $class($isDirector);
        $employee->salary *= 1.25;
        $employees[] = $employee;
    }
    return $employees;
}
function doEmployeeThirdRank($class, $number, $isDirector = 0)
{
    $employees = array();
    for ($i = 1; $i <= $number; $i++) {
        $employee = new $class($isDirector);
        $employee->salary *= 1.5;
        $employees[] = $employee;
    }
    return $employees;
}
$ma          = 'Marketolog';
$me          = 'Manager';
$an          = 'Analyst';
$in          = 'Engineer';
$procurement = new Procurement(array_merge(doEmployeeFirstRank($me, 9), doEmployeeSecondRank($me, 3), doEmployeeThirdRank($me, 2), doEmployeeFirstRank($ma, 2), doEmployeeSecondRank($me, 1, 1)));
$sales       = new Sales(array_merge(doEmployeeFirstRank($me, 12), doEmployeeFirstRank($ma, 6), doEmployeeFirstRank($an, 3), doEmployeeSecondRank($an, 2), doEmployeeSecondRank($ma, 1, 1)));
$advertising = new Advertising(array_merge(doEmployeeFirstRank($ma, 15), doEmployeeSecondRank($ma, 10), doEmployeeFirstRank($me, 8), doEmployeeFirstRank($in, 2), doEmployeeThirdRank($ma, 1, 1)));
$logistic    = new Logistic(array_merge(doEmployeeFirstRank($me, 13), doEmployeeSecondRank($me, 5), doEmployeeFirstRank($in, 5), doEmployeeFirstRank($me, 1, 1)));
$vektor      = 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);


function letShow($array)
{
    $col1 = 22;
    $col2 = 11;
    foreach ($array as $k => $str) {
        if ($k == 0) {
            echo padRight($str, $col1);
        } elseif (is_numeric($str) && $k == 5) {
            
            echo padLeft((number_format($str, 2)), $col2);
        } else {
            echo padLeft($str, $col2);
        }
        
    }
    echo "\n";
}
function doAll()
{
    
}

$proc = $procurement->doShow();
$log  = $logistic->doShow();
$adv  = $advertising->doShow();
$sal  = $sales->doShow();
$vek  = array(
    $proc,
    $log,
    $adv,
    $sal
);

$sumWorkers = 0;
$sumSal     = 0;
$sumCoffee  = 0;
$sumReports = 0;
$sumRepSal  = 0;
$end        = array();
foreach ($vek as $dep) {
    foreach ($dep as $k => $v) {
        switch ($k) {
            case 1:
                $sumWorkers += $v;
                break;
            case 2:
                $sumSal += $v;
                break;
            case 3:
                $sumCoffee += $v;
                break;
            case 4:
                $sumReports += $v;
                break;
            case 5:
                $sumRepSal += $v;
                break;
        }
    }
    
}
$end[] = 'Всего';
$end[] = $sumWorkers;
$end[] = $sumSal;
$end[] = $sumCoffee;
$end[] = $sumReports;
$end[] = $sumRepSal / 4;

//Вывод на экран
letShow($textAr);
echo str_repeat('-', 80) . "\n";
letShow($procurement->doShow());
letShow($logistic->doShow());
letShow($advertising->doShow());
letShow($sales->doShow());
echo str_repeat('-', 80) . "\n";
letShow($end);









