<?php

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

$rates = array(
	'Me'=>500,
	'Ma'=>400,
	'En'=>200,
	'An'=>800
	);
$coffe = array(
	'Me'=>20,
	'Ma'=>15,
	'En'=>5,
	'An'=>50
	);
$documentation = array(
	'Me'=>200,
	'Ma'=>150,
	'En'=>50,
	'An'=>5
	);
	
class Employees
{
	public $department;
	public $position;
	public $rank;
	public $boss;
	public $salary;
	public $quantity;
	
	public function __construct($department, $position, $rank, $boss, $quantity, $rates)
	{
		if ($rank == 1){
			$factor = 1;
		}elseif($rank == 2){
			$factor = 1.25;
		}elseif($rank == 3){
			$factor = 1.5;
		}
		$this->department = $department;
		$this->position = $position;
		$this->rank = $rank;
		$this->boss = $boss;
		if ($this->boss == true){
			$this->salary = ($rates[$position] * $factor) * 1.5;
		}else{
			$this->salary = $rates[$position] * $factor;
		}
		$this->quantity = $quantity;
	}
}

class Department 
{
	public $departmentName;
	public $personelAmount;
	public $departmentSalary;
	public $departmentCoffe;
	public $departmentDocumentation;
	public $salaryPerPages;
	
	public function __construct($employees, $coffe, $documentation)
	{
		foreach ($employees as $employee)
			{
			$this->departmentName = $employee->department;
		
			$this->personelAmount =  $employee->quantity + $this->personelAmount;
			$this->departmentSalary =  $employee->salary + $this->departmentSalary;
			if ($employee->boss == true){
				$this->departmentCoffe =  $coffe[$employee->position] * 2 + $this->departmentCoffe;
				$this->departmentDocumentation =  0 + $this->departmentDocumentation;
			}else{
				$this->departmentCoffe =  $coffe[$employee->position] + $this->departmentCoffe;
				$this->departmentDocumentation =  $documentation[$employee->position] + $this->departmentDocumentation ;
			}
		}$this->salaryPerPages = round($this->departmentSalary / $this->departmentDocumentation, 2);
	}
}

class Company
{
	public $total = 'Всего';
	public $totalPersonelAmount;
	public $totalSalary;
	public $totalCoffe;
	public $totalDocumentation;
	public $totalSalaryPerPages;
	
	public $average = 'Среднее';
	public $averagePersonelAmount;
	public $averageSalary;
	public $averageCoffe;
	public $averageDocumentation;
	public $averageSalaryPerPages;
	
	public function __construct($departments)
	{
		foreach ($departments as $department)
		{
			$this->totalPersonelAmount = $department->personelAmount + $this->totalPersonelAmount;
	 		$this->totalSalary = $department->departmentSalary + $this->totalSalary;
	 		$this->totalCoffe = $department->departmentCoffe + $this->totalCoffe;
			$this->totalDocumentation = $department->departmentDocumentation + $this->totalDocumentation;
			$this->totalSalaryPerPages = $department->salaryPerPages + $this->totalSalaryPerPages;
		}
		$this->averagePersonelAmount = $this->totalPersonelAmount / count($departments);
		$this->averageSalary = round($this->totalSalary / count($departments), 2);
		$this->averageCoffe = round($this->totalCoffe / count($departments), 2);
		$this->averageDocumentation = round($this->totalDocumentation / count($departments), 2);
		$this->averageSalaryPerPages = round($this->totalSalaryPerPages / count($departments), 2);
	}
}

  function padRight($q, $w){
 return implode("", (array_merge(preg_split('//u', $q, 0, PREG_SPLIT_NO_EMPTY), array_fill(0, $w-mb_strlen($q), " "))));
 }
  function padLeft($q, $w){
 return implode("", (array_merge(array_fill(0, $w-mb_strlen($q), " "), preg_split('//u', $q, 0, PREG_SPLIT_NO_EMPTY))));
 }
 
$workers1 = array(
new Employees ('Закупок', 'Me', 1, false, 9, $rates),
new Employees ('Закупок', 'Me', 2, false, 3, $rates),
new Employees ('Закупок', 'Me', 3, false, 2, $rates),
new Employees ('Закупок', 'Ma', 1, false, 1, $rates),
new Employees ('Закупок', 'Me', 2, true, 1, $rates)
);
$workers2 = array(
new Employees ('Продаж', 'Me', 1, false, 12, $rates),
new Employees ('Продаж', 'Ma', 1, false, 6, $rates),
new Employees ('Продаж', 'An', 1, false, 3, $rates),
new Employees ('Продаж', 'An', 2, false, 2, $rates),
new Employees ('Продаж', 'Ma', 2, true, 1, $rates)
);
$workers3 = array(
new Employees ('Рекламы', 'Ma', 1, false, 15, $rates),
new Employees ('Рекламы', 'Ma', 2, false, 10, $rates),
new Employees ('Рекламы', 'Me', 1, false, 8, $rates),
new Employees ('Рекламы', 'En', 1, false, 2, $rates),
new Employees ('Рекламы', 'Ma', 3, true, 1, $rates)
);
$workers4 = array(
new Employees ('Логистики', 'Me', 1, false, 13, $rates),
new Employees ('Логистики', 'Me', 2, false, 5, $rates),
new Employees ('Логистики', 'En', 1, false, 5, $rates),
new Employees ('Логистики', 'Me', 1, true, 1, $rates)
);

$departments = array(
new Department($workers1, $coffe, $documentation),
new Department($workers2, $coffe, $documentation),
new Department($workers3, $coffe, $documentation),
new Department($workers4, $coffe, $documentation)
);
$company = new Company ($departments);
	
$col1 = 20;
$col2 = 8;
$col3 = 10;
$col4 = 8;
$col5 = 8;
$col6 = 12;

echo padRight("Департамент", $col1) .
     padLeft("сотр.", $col2) . 
     padLeft("тугр.", $col3) . 
     padLeft("кофе", $col4) .
     padLeft("стр.", $col5) .
     padLeft("тугр./стр.", $col6) . "\n" . 
     implode("", array_fill(0, 40, '--')) . "\n";

foreach ($departments as $department) {
    echo padRight($department->departmentName, $col1) .
         padLeft($department->personelAmount, $col2) . 
         padLeft($department->departmentSalary, $col3) . 
         padLeft($department->departmentCoffe, $col4) . 
         padLeft($department->departmentDocumentation, $col5) .
         padLeft($department->salaryPerPages, $col6) . "\n" ;
         
}   echo implode("", array_fill(0, 40, '--')) . "\n";

    echo padRight($company->average, $col1) .
         padLeft($company->averagePersonelAmount, $col2) . 
         padLeft($company->averageSalary, $col3) . 
         padLeft($company->averageCoffe, $col4) .
         padLeft($company->averageDocumentation, $col5) .
         padLeft($company->averageSalaryPerPages, $col6) . "\n";
         
    echo padRight($company->total, $col1) .
         padLeft($company->totalPersonelAmount, $col2) . 
         padLeft($company->totalSalary, $col3) . 
         padLeft($company->totalCoffe, $col4) .
         padLeft($company->totalDocumentation, $col5) .
         padLeft($company->totalSalaryPerPages, $col6);
