Best way to generate Holerite, I seek a more viable solution

Asked

Viewed 420 times

2

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

Employees -> Companies -> Salaries -> Discounts

I want to show my user the following way for example:

Rafael works in the company X and has 10 salaries and 20 Discounts.

Code Example:

$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: inserir a descrição da imagem aqui

I would like to know how best to perform a query or procedure by PHP itself, to be able to generate these Holerites. I’ll generate the Holerites based on the company ID. The user will select the company and will generate the Holerites of all employees, decriminalizing the names of Salaries and Discounts and their values.

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

  • Which structure of your tables? You can use JOIN

  • Join would bring all the records of salaries and discounts ? It does not bring only one ?

  • Utilize Left Join. It will bring several results. Depending on what you want, you don’t even need to make a foreach, just add it all up with a query sql.

  • I added a photo of how I want to show, I want to show the employee, the company he works for and discriminate each salary and discount.

1 answer

1

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

For this you need to have done the relationship between the tables by creating foreign keys at the time of creation of the tables via Migration, if you did not have no problem, just manually create the relationship through the SQL editor that you use.

Then he goes into Models and creates a relationship, example:

In the Model "Employees"

public function descontos(){

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

At 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 helped, you can also take a look at documentation

  • I’ll run some tests, my bank is already set up following the Eloquent model. The biggest problem I see is the following, as in the example image I am generating a PDF that contains the information of employees and all their credits and discounts discriminating value and name. I tried to perform an Inner Join but only brings 1 result each and not all. I would like to know how best to bring from the bank each employee showing his company and discriminating his credits and discounts.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.