<?php
abstract class Employee {
    protected $salary;
    protected $rank;
    protected $coffeeConsumption;
    protected $isBoss;
    protected $production;
    public function setRank($rank){
        switch ($rank){
            case 1:
                $this->rank = 1;
                break;
            case 2:
                $this->rank = 1.25;
                break;
            case 3:
                $this->rank = 1.75;
                break;
            default:
                //die("Employee rank must be between 1 and 3");
                $this->rank = 1;
        }
    }
    public function setSalary($baseSalary){
        $this->salary = $baseSalary;
    }
    public function setCoffeeConsumption($coffee){
        $this->coffeeConsumption = $coffee;
    }
    public function setProduction($prod){
        $this->production = $prod;
    }
    public function getSalary(){
        return ($this->isBoss)?$this->salary*$this->rank*1.5:$this->salary*$this->rank;
    }
    public function getCoffeeConsumption(){
        return ($this->isBoss)?$this->coffeeConsumption*2:$this->coffeeConsumption;
    }
    public function getProduction(){
        return $this->production;
    }
    public function getRank(){
        return $this->rank;
    }
    public function __construct($rank,$isBoss)
    {
        //$this->salary = $baseSalary;
        //$this->coffeeConsumption = $coffee;
        $this->setRank($rank);
        $this->isBoss = $isBoss;
        //$this->production = $production;
    }
}

class Manager extends Employee{
    protected $coffeeConsumption = 20;
    protected $salary = 500;
    protected $production = 200;
    public function __construct($rank, $isBoss)
    {
        parent::__construct($rank, $isBoss);
    }
}
class Marketer extends Employee{
    protected $coffeeConsumption = 15;
    protected $salary = 400;
    protected $production = 150;
    public function __construct($rank, $isBoss)
    {

        parent::__construct($rank, $isBoss);
    }
}
class Engineer extends Employee{
    protected $coffeeConsumption = 5;
    protected $salary = 200;
    protected $production = 50;
    public function __construct($rank, $isBoss)
    {

        parent::__construct($rank, $isBoss);
    }
}
class Analyst extends Employee{
    protected $coffeeConsumption = 50;
    protected $salary = 800;
    protected $production = 5;
    public function __construct($rank, $isBoss)
    {

        parent::__construct($rank, $isBoss);
    }
}

class Department{
    private $employees = [];
    private $name;
    public function setName($name){
        $this->name = $name;
    }
    public function getName(){
        return $this->name;
    }
    public function addEmployee(Employee $e){
        array_push($this->employees,$e);
    }
    public function getEmployeesCount(){
        return sizeof($this->employees);
    }
    public function getEmployeesSalary(){
        $total = 0;
        if ($this->employees)
            foreach ($this->employees as $employee){
                $total+=$employee->getSalary();
            }
        return $total;
    }
    public function getEmployeesCoffeeConsumption(){
        $total = 0;
        if ($this->employees)
            foreach ($this->employees as $employee){
                $total+=$employee->getCoffeeConsumption();
            }
        return $total;
    }
    public function getEmployeesProduction(){
        $total = 0;
        if ($this->employees)
            foreach ($this->employees as $employee){
                $total+=$employee->getProduction();
            }
        return $total;
    }
    public function __construct($name)
    {
        $this->setName($name);
    }
}
class Writer {
    private function padLeft($title,$length){
        $strlen = strlen($title);
        if ($strlen<$length){
            $result = $title.str_repeat(' ',$length-$strlen);
        } else {
            $result = $title;
        }
        return $result;
    }
    private function padRight($title,$length){
        $strlen = strlen($title);
        if ($strlen<$length){
            $result = str_repeat(' ',$length-$strlen).$title;
        } else {
            $result = $title;
        }
        return $result;
    }
    public function writeDepartmentsInfo($depts){
        $totalSal = 0;
        $totalCount = 0;
        $totalCoffee = 0;
        $totalProd = 0;
        $totalPageCost = 0;
        $deptsCount = sizeof($depts);
        echo $this->padLeft("Department",30).
            $this->padRight("Employees",12).
            $this->padRight("Salary",12).
            $this->padRight("Coffee",12).
            $this->padRight("Pages",12).
            $this->padRight("Sal\\pgs",12)."\n";
        foreach ($depts as $d){
            $salary =$d->getEmployeesSalary() ;
            $prod = $d->getEmployeesProduction();

            $totalCoffee+=$d->getEmployeesCoffeeConsumption();
            $totalCount+=$d->getEmployeesCount();
            $totalProd+=$prod;
            $totalSal+=$salary;
            $totalPageCost+= $salary/$prod;
            echo $this->padLeft($d->getName(),30).
                $this->padRight($d->getEmployeesCount(),12).
                $this->padRight($salary,12).
                $this->padRight($d->getEmployeesCoffeeConsumption(),12).
                $this->padRight($prod,12).
                $this->padRight(round($salary/$prod,2),12)."\n";

        }
        echo $this->padLeft("Average",30).
            $this->padRight(round($totalCount/$deptsCount,2),12).
            $this->padRight(round($totalSal/$deptsCount,2),12).
            $this->padRight(round($totalCoffee/$deptsCount,2),12).
            $this->padRight(round($totalProd/$deptsCount,2),12).
            $this->padRight(round($totalPageCost/$deptsCount,2),12)."\n";
        echo $this->padLeft("Total",30).
            $this->padRight(round($totalCount,2),12).
            $this->padRight(round($totalSal,2),12).
            $this->padRight(round($totalCoffee,2),12).
            $this->padRight(round($totalProd,2),12).
            $this->padRight(round($totalPageCost,2),12)."\n";
    }
}

$dpts = [];
$deptZ = new Department('Buy');
for ($i = 0;$i<9;$i++){
    $deptZ->addEmployee(new Manager(1,0));
}
for ($i = 0;$i<3;$i++){
    $deptZ->addEmployee(new Manager(2,0));
}
for ($i = 0;$i<2;$i++){
    $deptZ->addEmployee(new Manager(3,0));
}
for ($i = 0;$i<2;$i++){
    $deptZ->addEmployee(new Marketer(1,0));
}
$deptZ->addEmployee(new Manager(2,1));
$dpts[]=$deptZ;

$deptZ = new Department('Sales');
for ($i = 0;$i<12;$i++){
    $deptZ->addEmployee(new Manager(1,0));
}
for ($i = 0;$i<6;$i++){
    $deptZ->addEmployee(new Marketer(1,0));
}
for ($i = 0;$i<3;$i++){
    $deptZ->addEmployee(new Analyst(1,0));
}
for ($i = 0;$i<2;$i++){
    $deptZ->addEmployee(new Analyst(2,0));
}
$deptZ->addEmployee(new Manager(2,1));
$dpts[]=$deptZ;

$deptZ = new Department('Marketing');
for ($i = 0;$i<15;$i++){
    $deptZ->addEmployee(new Marketer(1,0));
}
for ($i = 0;$i<10;$i++){
    $deptZ->addEmployee(new Marketer(2,0));
}
for ($i = 0;$i<8;$i++){
    $deptZ->addEmployee(new Manager(1,0));
}
for ($i = 0;$i<2;$i++){
    $deptZ->addEmployee(new Engineer(1,0));
}
$deptZ->addEmployee(new Manager(3,1));
$dpts[]=$deptZ;

$deptZ = new Department('Logistyc');
for ($i = 0;$i<13;$i++){
    $deptZ->addEmployee(new Manager(1,0));
}
for ($i = 0;$i<5;$i++){
    $deptZ->addEmployee(new Manager(2,0));
}
for ($i = 0;$i<5;$i++){
    $deptZ->addEmployee(new Engineer(1,0));
}
$deptZ->addEmployee(new Manager(1,1));
$dpts[]=$deptZ;

$writer = new Writer();
//echo phpinfo();

$writer->writeDepartmentsInfo($dpts);