OiO.lk Blog PHP Calculation problems in PHP Laravel
PHP

Calculation problems in PHP Laravel


I’m developing a room registration and reservation system in Laravel and I’m having trouble updating a room’s information when I add products in the Register_update function.

Context:

The room price is stored in the database and is fixed, but is dynamically adjusted in the system to calculate the cost based on the hours or days of the stay.

Additionally, products consumed in the room (such as drinks or snacks) are added and must be added to the final bill total.

The problem arises when I add a product in the Register_update function. Instead of correctly adding up to the total, the price of the product appears to incorrectly add or modify the value of the room.

            foreach($detalle_first as $product) {
                if ($product['impuesto'] == 1) {
                    $igv_first        +=  number_format((((float) $product['precio_unitario'] - (float) $product['precio_unitario'] / 1.18) * (int) $product['cantidad']), 2, ".", "");
                    $igv_first        = $this->redondeado($igv_first);
                }
    
                if ($product["codigo_igv"] == "10") {
                    $gravada_first    += number_format((((float) $product['precio_unitario'] / 1.18) * (int) $product['cantidad']), 2, ".", "");
                    $gravada_first     = $this->redondeado($gravada_first);
                }
    
                if ($product["codigo_igv"] == "20") {
                    $exonerada_first   += number_format(((float) $product['precio_unitario'] * (int) $product['cantidad']), 2, ".", "");
                    $exonerada_first   = $this->redondeado($exonerada_first);
                }
    
                if ($product["codigo_igv"] == "30") {
                    $inafecta_first    += number_format(((float) $product['precio_unitario'] * (int) $product['cantidad']), 2, ".", "");
                    $inafecta_first     = str_replace(',', '', $inafecta_first);
                    $inafecta_first     = $this->redondeado($inafecta_first);
                }
                $subtotal_first   = $exonerada_first + $gravada_first + $inafecta_first;
            }
    
            if(!empty($detalle_last)) {
                foreach($detalle_last as $product) {
                    if ($product['impuesto'] == 1) {
                        $igv_last        +=  number_format((((float) $product['precio_unitario'] - (float) $product['precio_unitario'] / 1.18) * (int) $product['cantidad']), 2, ".", "");
                        $igv_last        = $this->redondeado($igv_last);
                    }
        
                    if ($product["codigo_igv"] == "10") {
                        $gravada_last    += number_format((((float) $product['precio_unitario'] / 1.18) * (int) $product['cantidad']), 2, ".", "");
                        $gravada_last     = $this->redondeado($gravada_last);
                    }
        
                    if ($product["codigo_igv"] == "20") {
                        $exonerada_last   += number_format(((float) $product['precio_unitario'] * (int) $product['cantidad']), 2, ".", "");
                        $exonerada_last   = $this->redondeado($exonerada_last);
                    }
        
                    if ($product["codigo_igv"] == "30") {
                        $inafecta_last    += number_format(((float) $product['precio_unitario'] * (int) $product['cantidad']), 2, ".", "");
                        $inafecta_last     = str_replace(',', '', $inafecta_last);
                        $inafecta_last     = $this->redondeado($inafecta_last);
                    }
                    $subtotal_last   = $exonerada_last + $gravada_last + $inafecta_last;
                }
            }
    
            Reception::where('id', $idrecepcion)->update([
                'fecha_salida'  => $fecha_salida,
                'exonerada'     => $exonerada_first + $exonerada_last,
                'inafecta'      => $inafecta_first + $inafecta_last,
                'gravada'       => $gravada_first + $gravada_last,
                'anticipo'      => "0.00",
                'igv'           => $igv_first + $igv_last,
                'gratuita'      => "0.00",
                'otros_cargos'  => "0.00",
                'total'         => $subtotal_first + $subtotal_last,
                'observaciones' => mb_strtoupper($observaciones),
            ]);

That when adding product does not add to the cost of the room.



You need to sign in to view this answers

Exit mobile version