<?php

abstract class Employee {
    public $amount;
    public $rang;
    public $leader;
    public $salary;
    public $coffee;
    public $reports;


    public function __construct ($amount, $rang, $leader) {
        $this->amount = $amount;
        $this->rang = $rang;
        $this->leader = $leader;

        if ($this->leader == true) {
            $this->salary  *= 1.5;
            $this->coffee *= 2;
            $this->reports = 0;
        }
    }

    // Возвращает кол-во людей (ме) или (ма) или (ин) или (ан).
    public function getAmount () {
        return $this->amount;
    }

    public function getSalary () {
        $realSalary = $this->amount * $this->salary;
        if ($this->rang == 1) {
            $realSalary  = $realSalary;
        } elseif ($this->rang = 2) {
            $realSalary *= 1.25;
        } else {
            $realSalary *= 1.5;
        }
        return $realSalary;
    }

    public function getCoffee () {
        return $this->coffee;
    }

    public function getReports () {
        return $this->reports;
    }
}

class EmployeeList {
    public $depName;
    public $employeeList;

    public function  __construct ($depName, $employeeList) {
        $this->depName = $depName;
        $this->employeeList = $employeeList;
    }

    public function getDepName () {
        return $this->depName;
    }

    public function getEmployeeList() {
        return $this->employeeList;
    }

    public function returnAmount() {
        $number = 0;
        foreach ($this->employeeList as $employee){
            $number  += $employee->getAmount();
        }
        return $number;
    }

    public function returnTotalSalary () {
        $number = 0;
        foreach ($this->employeeList as $employee){
            $number += $employee->getSalary();
        }
        return $number;
    }

    public function returnCoffee () {
        $number = 0;
        foreach ($this->employeeList as $employee){
            $number += $employee->getCoffee();
        }
        return $number;
    }

    public function returnReports () {
        $number = 0;
        foreach ($this->employeeList as $employee){
            $number += $employee->getReports();
        }
        return $number;
    }

    public function returnAvgSandR() {
        $avg = $this->returnTotalSalary() / $this->returnAmount();
        return round($avg);
    }
}

class Manager extends Employee {
    public $salary = 500;
    public $coffee = 20;
    public $reports = 200;
}

class Marketer extends Employee {
    public $salary = 400;
    public $coffee = 15;
    public $reports = 150;
}

class Engineer extends Employee {
    public $salary = 200;
    public $coffee = 5;
    public $reports = 50;
}

class Analyst extends Employee {
    public $salary = 800;
    public $coffee = 50;
    public $reports = 5;
}

//Департамент закупок: 9×ме1, 3×ме2, 2×ме3, 2×ма1 + руководитель департамента ме2
$obj1 = new Manager(9,1,false);
$obj2 = new Manager(3,2,false);
$obj3 = new Manager(2,3,false);
$obj4 = new Marketer(2,1,false);
$obj5 = new Manager(1,2,true);
$DepOfPurchases = new EmployeeList("Закупок", array($obj1, $obj2, $obj3, $obj4, $obj5));

//Департамент продаж: 12×ме1, 6×ма1, 3×ан1, 2×ан2 + руководитель ма2
$obj6 = new Manager(12,1,false);
$obj7 = new Marketer(6,1,false);
$obj8 = new Analyst(3,1,false);
$obj9 = new Analyst(2,2,false);
$obj10 = new Marketer(1,2,true);
$DepOfSales = new EmployeeList("Продаж", array($obj6, $obj7, $obj8, $obj9, $obj10));

//Департамент рекламы: 15×ма1, 10×ма2, 8×ме1, 2×ин1 + руководитель ма3
$obj11 = new Marketer(15,1,false);
$obj12 = new Marketer(10,2,false);
$obj13 = new Manager(8,1,false);
$obj14 = new Engineer(2,1,false);
$obj15 = new Marketer(1,3,true);
$DepOfAdvertising = new EmployeeList("Рекламы", array($obj11, $obj12, $obj13, $obj14, $obj15));

//Департамент логистики: 13×ме1, 5×ме2, 5×ин1 + руководитель ме1
$obj16 = new Manager(13,1, false);
$obj17 = new Manager(5,2,false);
$obj18 = new Engineer(5,1,false);
$obj19 = new Manager(1,1,true);
$DepOfLogistics = new EmployeeList("Логистики", array($obj16, $obj17, $obj18, $obj19));

$departaments = array($DepOfPurchases, $DepOfSales, $DepOfAdvertising, $DepOfLogistics);

function padRight($string, $length) {
    $string = strval($string);
    $lengthStr = (mb_strlen($string));
    $spaces = $length - $lengthStr;
    if ($spaces >= 0) {
        return $string . str_repeat(' ', $spaces);
    } else {
        return ' ' . mb_substr($string, 0, $length - 2) . '.';
    }
}
function padLeft ($string, $length) {
    $string = strval($string);
    $lengthStr = (mb_strlen($string));
    $spaces = $length - $lengthStr;
    if ($spaces >= 0) {
        return $string . str_repeat(' ', $spaces);
    } else {
        return '.' . mb_substr($string, 0, $length - 2) . ' ';
    }
}

$sumAmount = 0;
$sumTotalSalary = 0;
$sumCoffee = 0;
$sumReports = 0;
$sumSandR = 0;

// Ширина колонок
$col1 = 30;
$col2 = 12;

// Заголовок таблицы
echo padRight("Департамент", $col1) .
    padLeft("сотр.", $col2) .
    padLeft("тугр.", $col2) .
    padLeft("кофе", $col2) .
    padLeft("стр.", $col2) .
    padLeft("тугр./стр.", $col2) ."\n";

// Сама таблица
foreach ($departaments as $departament) {
    echo padRight($departament->getDepName(), $col1) .
        padLeft($departament->returnAmount(), $col2) .
        padLeft($departament->returnTotalSalary(), $col2) .
        padLeft($departament->returnCoffee(), $col2) .
        padLeft($departament->returnReports(), $col2) .
        padLeft($departament->returnAvgSandR(), $col2) .
        "\n";
    $sumAmount += $departament->returnAmount();
    $sumTotalSalary += $departament->returnTotalSalary();
    $sumCoffee += $departament->returnCoffee();
    $sumReports += $departament->returnReports();
    $sumSandR += $departament->returnAvgSandR();
}

echo padRight("Среднее", $col1) .
    padLeft($sumAmount / count($departaments), $col2) .
    padLeft($sumTotalSalary / count($departaments), $col2) .
    padLeft($sumCoffee / count($departaments), $col2) .
    padLeft($sumReports / count($departaments), $col2) .
    padLeft($sumSandR / count($departaments), $col2) . "\n";

echo padRight("Всего", $col1) .
    padLeft($sumAmount, $col2) .
    padLeft($sumTotalSalary, $col2) .
    padLeft($sumCoffee, $col2) .
    padLeft($sumReports, $col2) .
    padLeft($sumSandR, $col2) . "\n";