Best way to generate Holerite, I look for a more viable solution

1

I developed an application that generates Holerites, and for that I need to treat data coming from 4 tables.

  

Officials - > Companies - > Maturities - > Discounts

I want to show my user this way for example:

  

Rafael works in company X and has 10 salaries and 20 discounts.

Sample Code:

$total_vencimento= 0;
$total_desconto = 0;

// Aqui em funcionários realizei um join com empresas.
foreach ($funcionarios as $funcionario) {

  foreach ($vencimentos as $vencimento) {
    //VERIFICA SE PERTENCE AO FUNCIONARIO ATUAL E PRINTA
    $total_vencimento += $vencimento->valor;
  }

  foreach ($descontos as $desconto) {
    //VERIFICA SE PERTENCE AO FUNCIONARIO ATUAL E PRINTA
    $total_desconto += $desconto->valor;
  }

  // Aqui apresentaria o valor liquido entra Vencimentos - Descontos

}

Sample image:

I would like to know the best way to perform a query or procedure by PHP itself, in order to generate these Holerites. I'm going to generate the Holerites based on the company ID. The user will select the company and will generate the Holerites of all the employees, describing the names of the Maturities and Discounts and their values.

I am currently bringing all employees belonging to the company, and their respective salaries and discounts. All this in a foreach and Czech if that expiration / discount belongs to the current employee of the loop and printo.

    
asked by anonymous 04.01.2018 / 19:45

1 answer

1

If you use the relationship through the Model you get this result with only one line.

To do this, you need to do the relationship between the tables by creating foreign keys when creating the tables via migration, if you did not have no problem, just create the relationship manually through the SQL editor you use.

Then enter the Models and create the relationship, example :

Na Model "Officials"

public function descontos(){

    return $this->hasMany("App\Models\Desconto");
}

Na Model Discounts

public function funcionarios(){

    return $this->belongsTo("App\Models\Funcionarios");
}

Then you can extract the data with something like:

$result = Funcionarios::has('descontos')->get();

I hope I have helped, you can check out documentation

    
07.01.2018 / 02:16