public function PEPS($linea) { $total = 0; //variable auxiliar while ($total != $linea->cantidad) { //consulta que regresa del inv la cosecha mas vieja $oldInv = DB::table('inventarios')->where('status', '>=', 3)->where('cantidad', '>', 0)->where('id_producto', $linea->producto->id)->orderBy('fechacosecha', 'asc')->take(1)->get(); $inv = Inventario::find($oldInv[0]->id); if ($inv->cantidad <= $linea->cantidad - $total) { $total = $total + $inv->cantidad; $inv->cantidad = 0; } else { $inv->cantidad = $inv->cantidad - ($linea->cantidad - $total); $total = $linea->cantidad; } $inv->save(); } }