Calculate total quantity, price and discount and total totals

Asked

Viewed 113 times

0

cannot create the total result of Quantity * value_unitario - discount, and also the sum of these totals, of the table that is related.

Model

    public function servico()
{
    return $this->hasMany(Servico::class);
}

public function cliente()
{
    return $this->hasOne(Cliente::class, 'id', 'cliente_id');
}

public function ordemservico()
{
    return $this->hasOne(OrdemServico::class);
}

Repository

    public function exibir($id)
{
    $orcamento = Orcamento::where('id', $id)->with('cliente')->with('servico')->first();
    return $orcamento;
}

In the related table with('servico'), the columns, "quantity, value_unitario(decimal:8,2)", "discount(decimal:8,2)" need to multiply quantity by value_unitario and subtract by discount if it exists, and sum all the total values, to show in the VIEW the total of each, and the total of all.

1 answer

0


You will need to use a query with the selectRaw or select(DB::raw sum(seucampo) as seurotulo).

An example of the way I did in a project;

$products = DB::table('products')
    ->join('order_product', 'products.id', '=', 'order_product.product_id')
    ->selectRaw('products.id, products.sku, products.name, products.brand, sum(order_product.quantity) as sales_quantity, products.stock')
    ->groupBy('products.sku')
    ->orderBy('sales_quantity', 'DESC')
    ->get();

return response()->json($products);

Laravel Documentation - Raw Expressions

https://laravel.com/docs/5.8/queries#raw-Expressions

  • top understood, I’ll adapt to my code, thanks bro

  • Shooow man! Anything we are there.

Browser other questions tagged

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