<?php
 abstract class Worker{
    
    protected $payment;
    protected $drink;
    protected $produce;
   protected $grade;
  protected $description;
 
      
   protected function getPayment($grade) {
       if ($grade==1){
           return $this->payment;
       }
       else if($grade ==2)
       {
           return $this->payment*(1/4)+($this->payment);
       }
       else if($grade==3){
           return $this->payment*(1/2)+($this->payment);
       }
       else if($grade ==4){ return $this->payment*(1/2)+($this->payment);}
} 
protected function getProduce($grade){
     if ($grade!=4)
     return $this->produce;
     else return $this->produce*0;
 }
protected function getDrink($grade){
    if ($grade==4) return $this->drink+((1/2)*$this->drink);
   else  return $this->drink;
}
}

class Manager extends Worker{
   
    public $payment = 500;
      public $drink = 20;
   public $produce = 200;
  public function __construct($grade) {
      $this->payment = $this->getPayment($grade);
      $this->drink = $this ->getDrink($grade);
      $this ->produce = $this->getProduce($grade);
      $this->grade = $grade;
      $this->description = "me" . $grade;
      
   }
   public function getInfo(){
       $arr=[];
       $wrk = new Manager($this->grade);
       foreach($wrk as  $value){
           $arr[]=$value;
       }
       return $arr;
   }
}

class Marketer extends Worker{
   
    public $payment = 400;
      public $drink = 15;
   public $produce = 150;
  public function __construct($grade) {
        $this->payment = $this->getPayment($grade);
      $this->drink = $this ->getDrink($grade);
      $this ->produce = $this->getProduce($grade);
      $this->grade = $grade;
      $this->description = "ma" . $grade;
      
   }
   
   public function getInfo(){
       $arr=[];
       $wrk = new Marketer($this->grade);
       foreach($wrk as  $value){
           $arr[]=$value;
       }
       return $arr;
   }
}
class Engineer extends Worker{
   public $payment = 200;
      public $drink = 5;
   public $produce = 50;

  public function __construct($grade) {
      $this->payment = $this->getPayment($grade);
      $this->drink = $this ->getDrink($grade);
      $this ->produce = $this->getProduce($grade);
      $this->grade = $grade;
      $this->description = "en" . $grade;
      
   }
   public function getInfo(){
       $arr=[];
       $wrk = new Engineer($this->grade);
       foreach($wrk as  $value){
           $arr[]=$value;
       }
       return $arr;
   }
   
}
class Analyst extends Worker{
   
    public $payment = 800;
      public $drink = 50;
   public $produce = 5;
  public function __construct($grade) {
       $this->payment = $this->getPayment($grade);
      $this->drink = $this ->getDrink($grade);
      $this ->produce = $this->getProduce($grade);
      $this->grade = $grade;
      $this->description = "an" . $grade;
      
   }
public function getInfo(){
       $arr=[];
       $wrk = new Analyst($this->grade);
       foreach($wrk as  $value){
           $arr[]=$value;
       }
       return $arr;
   }
}


class Department{
    
    public function FirstDep(){
       $dep1 = [];
        for ($i=0;$i<9;$i++){
        $a=new Manager(1);
      $dep1[] = $a->getInfo();
        
        }
        for ($i=0;$i<3;$i++){
        $a=new Manager(2);
      $dep1[] = $a->getInfo();
      }
       for ($i=0;$i<2;$i++){
        $a=new Manager(3);
      $dep1[] = $a->getInfo();
      }
       for ($i=0;$i<2;$i++){
        $a=new Marketer(1);
      $dep1[] = $a->getInfo();
      }
        return $dep1;
    }
     public function SecondDep(){
       $dep1 = [];
        for ($i=0;$i<12;$i++){
        $a=new Manager(1);
      $dep1[] = $a->getInfo();
        
        }
        for ($i=0;$i<6;$i++){
        $a=new Marketer(1);
      $dep1[] = $a->getInfo();
      }
       for ($i=0;$i<3;$i++){
        $a=new Analyst(1);
      $dep1[] = $a->getInfo();
      }
       for ($i=0;$i<2;$i++){
        $a=new Analyst(2);
      $dep1[] = $a->getInfo();
      }
        return $dep1;
    }
     public function ThirdDep(){
       $dep1 = [];
        for ($i=0;$i<15;$i++){
        $a=new Marketer(1);
      $dep1[] = $a->getInfo();
        
        }
        for ($i=0;$i<10;$i++){
        $a=new Marketer(2);
      $dep1[] = $a->getInfo();
      }
       for ($i=0;$i<8;$i++){
        $a=new Manager(1);
      $dep1[] = $a->getInfo();
      }
       for ($i=0;$i<2;$i++){
        $a=new Engineer(1);
      $dep1[] = $a->getInfo();
      }
        return $dep1;
    }
     public function FourthDep(){
       $dep1 = [];
        for ($i=0;$i<13;$i++){
        $a=new Manager(1);
      $dep1[] = $a->getInfo();
        
        }
        for ($i=0;$i<5;$i++){
        $a=new Manager(2);
      $dep1[] = $a->getInfo();
      }
       for ($i=0;$i<5;$i++){
        $a=new Engineer(1);
      $dep1[] = $a->getInfo();
      } 
      return $dep1;
    }
    
    
    public function arraysum($var, $array){
        $sum=[];
      
       
        
foreach ($array as $value ){
for ($i=0;$i<count($value[$var]);$i++){
    $sum[] =$value[$var];
}
    }
    return array_sum($sum);
    }
    
    public function ArrayMiddle($number){
         
      
       $middle = new Department();
       $a= $middle->arraysum($number,$middle->FirstDep());
       $b= $middle->arraysum($number,$middle->SecondDep());
       $c= $middle->arraysum($number,$middle->ThirdDep());
       $d= $middle->arraysum($number,$middle->FourthDep());
       $sum = $a+$b+$c+$d;
    return $sum;
    
        
    }
    
}

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

$firstdep = new Department();


$col1 = 30;
$col2 = 10;
echo"<pre>";
  echo   padRight("Департамент", $col1).
            padLeft("сотр", $col2).
          padLeft("тугр.", $col2).
            padLeft("кофе", $col2).
            padLeft("стр.", $col2). 
            padLeft("тугр./стр", $col2). " <br>";
             
for ($i=0;$i<50;$i++){
    echo "_ ";
}  
echo"<br>";
echo"<br>";
    echo padRight("Закупок", $col1).
            padLeft(count($firstdep->FirstDep()), $col2).
          padLeft($firstdep->arraysum(0,$firstdep->FirstDep()), $col2).
            padLeft($firstdep->arraysum(1,$firstdep->FirstDep()), $col2).
            padLeft($firstdep->arraysum(2,$firstdep->FirstDep()), $col2). 
            padLeft(round($firstdep->arraysum(0,$firstdep->FirstDep())/$firstdep->arraysum(2,$firstdep->FirstDep())), $col2). " <br>";
    
echo"<br>";
    echo padRight("Продаж", $col1).
            padLeft(count($firstdep->SecondDep()), $col2).
          padLeft($firstdep->arraysum(0,$firstdep->SecondDep()), $col2).
            padLeft($firstdep->arraysum(1,$firstdep->SecondDep()), $col2).
            padLeft($firstdep->arraysum(2,$firstdep->SecondDep()), $col2). 
            padLeft(round($firstdep->arraysum(0,$firstdep->SecondDep())/$firstdep->arraysum(2,$firstdep->SecondDep())), $col2). " <br>";
    
    echo"<br>";
    echo padRight("Рекламы", $col1).
            padLeft(count($firstdep->ThirdDep()), $col2).
          padLeft($firstdep->arraysum(0,$firstdep->ThirdDep()), $col2).
            padLeft($firstdep->arraysum(1,$firstdep->ThirdDep()), $col2).
            padLeft($firstdep->arraysum(2,$firstdep->ThirdDep()), $col2). 
            padLeft(round($firstdep->arraysum(0,$firstdep->ThirdDep())/$firstdep->arraysum(2,$firstdep->ThirdDep())), $col2). " <br>";
    
     echo"<br>";
    echo padRight("Логистики", $col1).
            padLeft(count($firstdep->FourthDep()), $col2).
          padLeft($firstdep->arraysum(0,$firstdep->FourthDep()), $col2).
            padLeft($firstdep->arraysum(1,$firstdep->FourthDep()), $col2).
            padLeft($firstdep->arraysum(2,$firstdep->FourthDep()), $col2). 
            padLeft(round($firstdep->arraysum(0,$firstdep->FourthDep())/$firstdep->arraysum(2,$firstdep->FourthDep())), $col2). " <br>";
    
     echo"<br>";
     $a =    (count($firstdep->FirstDep())+count($firstdep->SecondDep()) +count($firstdep->ThirdDep())+count($firstdep->FourthDep()));
            echo padRight("Cреднее", $col1).
            padLeft($a/4, $col2).
          padLeft($firstdep->ArrayMiddle(0)/4, $col2).
            padLeft($firstdep->ArrayMiddle(1)/4, $col2).
            padLeft($firstdep->ArrayMiddle(2)/4, $col2). 
            padLeft((round($firstdep->ArrayMiddle(0)/$firstdep->ArrayMiddle(2))), $col2). " <br>";

    echo padRight("Общее", $col1).
            padLeft($a, $col2).
          padLeft($firstdep->ArrayMiddle(0), $col2).
            padLeft($firstdep->ArrayMiddle(1), $col2).
            padLeft($firstdep->ArrayMiddle(2), $col2). 
            padLeft(12, $col2). " <br>";

    
