<?php

error_reporting(-1);
mb_internal_encoding('UTF-8');

class Employee{
    public $department;
    public $profession;
    public $coffee;
    public $boss;
    public $rank;
    public $pages;
    public $basicSalary;

    public function __construct($department, $profession, $boss, $rank, $basicSalary)
    {
        $this->department = $department;
        $this->profession = $profession;
        $this->rank = $rank;
        $this->boss = $boss;
        $this->basicSalary = $basicSalary;
    }

    public function getDepartment() {
        $department = $this->department;
        return $department;
    }

    public function setRate()
    {
        $rate = 0;
        if ($this->boss == true) {
            $bossRank = 1.5;
        } else {
            $bossRank = 1;
        }
        if ($this->rank == 1) {
            $rate = 1;
        } elseif ($this->rank == 2) {
            $rate = 1.25;
        } elseif ($this->rank == 3) {
            $rate = 1.5;
        }
        $rate *= $bossRank;
        return $rate;
    }

    public function getSalary()
    {
        $salary = $this->basicSalary * $this->setRate();
        return $salary;
    }

    public function getCoffeeByProfession($coffeeByProfession){
        foreach ($coffeeByProfession as $professionName => $coffeeNumber) {
            if ($this->profession == $professionName) {
                if ($this->boss == true) {
                    $this->coffee = $coffeeNumber * 2;
                    break;
                } else {
                    $this->coffee = $coffeeNumber;
                    break;
                }
            }
        }
        return $this->coffee;
    }

    public function getPagesByProfession($pagesByProfession){
        foreach ($pagesByProfession as $professionName => $pagesNumber) {
            if ($this->profession == $professionName) {
                if ($this->boss == true) {
                    $this->pages = 0;
                    break;
                } else {
                    $this->pages = $pagesNumber;
                    break;
                }
            }
        }
        return $this->pages;
    }
}
class Department{
    public $departmentName;
    public $departmentCoffee;
    public $departmentPages;
    public $departmentSalary;

    public function getDepartmentName()
    {
        $department = $this->getDepartment();
        return $department;
    }
    public function getDepartmentCoffee($employees, $coffeeByProfession)
    {
        $departmentCoffee = 0;
        for ($i = 0; $i < count($employees); $i++) {
             $departmentCoffee += $employees[$i]->getCoffeeByProfession($coffeeByProfession);
        }
        return $departmentCoffee;
    }
    public function getDepartmentPages($employees, $pagesByProfession)
    {
        $departmentPages = 0;
        for ($i = 0; $i < count($employees); $i++) {
             $departmentPages += $employees[$i]->getPagesByProfession($pagesByProfession);
        }
        return $departmentPages;
    }
    public function getDepartmentSalary($employees)
    {
        $departmentSalary = 0;
        for ($i = 0; $i < count($employees); $i++) {
            $departmentSalary += $employees[$i]->getSalary;
        }
        return $departmentSalary;
    }
}
class Company{
    public $departments;
    public $employeesNumber;
    public $companiesCoffee;
    public $companiesPages;
    public $companiesSalary;

    public $averageEmployeesNumber;
    public $averageCompaniesCoffee;
    public $averageCompaniesPages;
    public $averageCompaniesSalaryPerPages;
    public $averageCompaniesSalary;

    public function getDepartments()
    {
    }
    public function getEmployeesNumber()
    {
    }
    public function getCompaniesCoffee()
    {
    }
    public function getCompaniesPages()
    {
    }
    public function getCompaniesSalary()
    {
    }
    public function getAverageEmployeesNumber()
    {
    }
    public function getAverageCompaniesCoffee()
    {
    }
    public function getAverageCompaniesPages()
    {
    }
    public function getAaverageCompaniesSalaryPerPages()
    {
    }
    public function getAverageCompaniesSalary()
    {
    }

}
function padRight($string, $widthOfTableCell){
    $tableCell = $string . str_repeat(" ", $widthOfTableCell - mb_strlen($string));
    return $tableCell;
}

function padLeft($string, $widthOfTableCell){
    $tableCell = str_repeat(" ", $widthOfTableCell - mb_strlen($string)) . $string;
    return $tableCell;
}

$col1 = 15;
$col2 = $col3 = $col4 = $col5 = $col6 = 12;

$coffeeByProfession = array(
    'Me' => 20,
    'Ma' => 15,
    'En' => 5,
    'An' => 50
);

$pagesByProfession = array(
    'Me' => 200,
    'Ma' => 150,
    'En' => 50,
    'An' => 5
);

function getEmployeesOfDepartment($employeesOfDepartment, $matches, $i, $x) {
    $bossStatus = false;
    $arrayOfResults = [];
    $numberOfEmployees = (int)$matches[$i][2] + $x;
    if (isset($matches[$i][5])) {
        $bossStatus = true;
    }

    for ($x; $x < $numberOfEmployees; $x++) {
        $employeesOfDepartment[] = new Employee($matches[$i][1], $matches[$i][3], $bossStatus, $matches[$i][4], 400);
    }

    $arrayOfResults = [
        'x' => $x,
        'employeesOfDepartment' => $employeesOfDepartment
];
    return $arrayOfResults;
}

$employees = ['pu9me1', 'pu3me2', 'pu2me3', 'pu2ma1', 'pu1me2boss', 're12me1', 're6ma1', 're3an1', 're2an2', 're1ma2boss', 'ad15ma1', 'ad10ma2', 'ad8me1', 'ad2en1', 'ad1ma3boss', 'lo13me1', 'lo5me2', 'lo5en1', 'lo1me1boss'];
$regexp = '/([a-z]{2})([0-9]*)([a-z]{2})([1-3])(boss)?/';
$i = 0;
$x = 0;
$employeesOfPurchaseDepartment = [];
$employeesOfRealizationDepartment = [];
$employeesOfAdvertisingDepartment = [];
$employeesOfLogisticDepartment = [];
$matches = [];
$numberOfEmployees = 0;
foreach ($employees as $key => $employee) {
    preg_match($regexp, $employee, $matches[$i]);
    if ($matches[$i][1] == 'pu') {
        $employeesOfPurchaseDepartment = getEmployeesOfDepartment($employeesOfPurchaseDepartment, $matches, $i, $x);
        $x = $employeesOfPurchaseDepartment['x'];
        $employeesOfPurchaseDepartment = $employeesOfPurchaseDepartment['employeesOfDepartment'];
    } elseif ($matches[$i][1] == 're') {
        $employeesOfRealizationDepartment = getEmployeesOfDepartment($employeesOfRealizationDepartment, $matches, $i, $x);
        $x = $employeesOfRealizationDepartment['x'];
        $employeesOfRealizationDepartment = $employeesOfRealizationDepartment['employeesOfDepartment'];
    } elseif ($matches[$i][1] == 'ad') {
        $employeesOfAdvertisingDepartment = getEmployeesOfDepartment($employeesOfAdvertisingDepartment, $matches, $i, $x);
        $x = $employeesOfAdvertisingDepartment['x'];
        $employeesOfAdvertisingDepartment = $employeesOfAdvertisingDepartment['employeesOfDepartment'];
    } elseif ($matches[$i][1] == 'lo') {
        $employeesOfLogisticDepartment = getEmployeesOfDepartment($employeesOfLogisticDepartment, $matches, $i, $x);
        $x = $employeesOfLogisticDepartment['x'];
        $employeesOfLogisticDepartment = $employeesOfLogisticDepartment['employeesOfDepartment'];
    }
    $i++;
}

function getIndex($employees)
{
    $salary = 0;
    $pages = 0;
    $coffee = 0;
    $i = 0;
    $department = $employees[$i]->department;
    for (; $i < count($employees); $i++) {
        $salary += $employees[$i]->getSalary();
        $pages += $employees[$i]->pages;
        $coffee += $employees[$i]->coffee;
    }
    $employeesNumber = count($employees);
    $index = array(
        'salary' => $salary,
        'pages' => $pages,
        'coffee' => $coffee,
        'department' => $department,
        'employeesNumber' => $employeesNumber
    );
    return $index;
}
$departmentsIndex = [];
$purchaseDepartment = getIndex($employeesOfPurchaseDepartment);
$realizationDepartment = getIndex($employeesOfRealizationDepartment);
$advertisingDepartment = getIndex($employeesOfAdvertisingDepartment);
$logisticDepartment = getIndex($employeesOfLogisticDepartment);

$departmentsIndex[] = $purchaseDepartment;
$departmentsIndex[] = $realizationDepartment;
$departmentsIndex[] = $advertisingDepartment;
$departmentsIndex[] = $logisticDepartment;

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

// Сама таблица
foreach ($departmentsIndex as $department) {
    echo padRight($department['department'], $col1) .
        padLeft($department['employeesNumber'], $col2) .
        padLeft($department['salary'], $col3) .
        padLeft($department['coffee'], $col4) .
        padLeft($department['pages'], $col5) .
        padLeft(0, $col6) . "\n";
}

?>