All files / js/pages/reports Index.vue

0% Statements 0/64
0% Branches 0/42
0% Functions 0/24
0% Lines 0/64

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
<script setup lang="ts">
import ListTableActionLabel from '@/components/ListTableActionLabel.vue';
import { useAuthAbility } from '@/composables/useAuthAbilities';
import { useDateTimeFormat } from '@/composables/useDateTimeFormat';
import AppLayout from '@/layouts/AppLayout.vue';
import { Head, Link, router } from '@inertiajs/vue3';
import { ref } from 'vue';
import { Plus, FileText, FileBarChart, Trash2, Edit, ClipboardList } from 'lucide-vue-next';
 
interface Report {
    id: number;
    name: string;
    entity_type: 'invoice' | 'quote' | 'jobcard';
    created_at: string;
    template?: ReportTemplate;
    creator?: {
        id: number;
        name: string;
    };
}
 
interface ReportTemplate {
    id: number;
    name: string;
    description: string | null;
    entity_type: 'invoice' | 'quote' | 'jobcard';
    is_default: boolean;
    created_by?: number;
    creator?: {
        id: number;
        name: string;
    };
}
 
interface Company {
    id: number;
    name: string;
}
 
interface Props {
    reports: {
        data: Report[];
        links: any[];
        meta: any;
    };
    templates: ReportTemplate[];
    currentCompany: Company;
}
 
const props = defineProps<Props>();
 
const canReportsCreate = useAuthAbility('reports', 'create');
const canReportsEdit = useAuthAbility('reports', 'edit');
const canReportsDelete = useAuthAbility('reports', 'delete');
const canReportsView = useAuthAbility('reports', 'view');
const { formatDate } = useDateTimeFormat();
 
const deleteReport = (reportId: number) => {
    if (confirm('Are you sure you want to delete this report?')) {
        router.delete(`/reports/${reportId}`);
    }
};
 
const useTemplate = (templateId: number) => {
    if (!canReportsCreate.value) {
        return;
    }
    router.get('/reports/create', { template_id: templateId });
};
 
const editTemplate = (templateId: number, event: Event) => {
    event.stopPropagation();
    router.get(`/reports/templates/${templateId}/edit`);
};
 
const deleteTemplate = (templateId: number, event: Event) => {
    event.stopPropagation();
    if (confirm('Are you sure you want to delete this template? This action cannot be undone.')) {
        router.delete(`/reports/templates/${templateId}`);
    }
};
</script>
 
<template>
    <Head title="Reports" />
 
    <AppLayout :breadcrumbs="[{ title: 'Reports', href: '/reports' }]">
        <div class="p-4">
            <!-- Header -->
            <div class="flex items-center justify-between gap-3 mb-6">
                <h1 class="text-2xl font-bold text-gray-900">Reports</h1>
                <Link
                    v-if="canReportsCreate"
                    href="/reports/create"
                    class="rounded bg-blue-600 px-4 py-2 text-white hover:bg-blue-700 flex items-center gap-2"
                >
                    <Plus class="w-4 h-4" />
                    Run Report
                </Link>
            </div>
 
            <!-- Built-in Reports -->
            <div class="mb-8">
                <h2 class="text-lg font-semibold text-gray-900 mb-4">Built-in Reports</h2>
                <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
                    <Link
                        v-if="canReportsView"
                        href="/reports/jobcard-detail"
                        class="bg-white rounded-lg border p-4 hover:shadow-md transition-shadow flex items-start gap-3"
                    >
                        <div class="rounded-full bg-primary/10 p-2 mt-0.5">
                            <ClipboardList class="w-5 h-5 text-primary" />
                        </div>
                        <div>
                            <h3 class="font-semibold text-gray-900">Detailed Jobcards Report</h3>
                            <p class="text-sm text-gray-500 mt-1">Comprehensive jobcard report with line items, booked time entries, financial summaries, and export to CSV</p>
                        </div>
                    </Link>
                </div>
            </div>
 
            <!-- Templates Section -->
            <div class="mb-8">
                <h2 class="text-lg font-semibold text-gray-900 mb-4">Report Templates</h2>
                <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
                    <div
                        v-for="template in props.templates"
                        :key="template.id"
                        class="bg-white rounded-lg border p-4 hover:shadow-md transition-shadow"
                    >
                        <div class="flex items-start justify-between mb-2">
                            <div 
                                class="flex items-center gap-2 flex-1 cursor-pointer"
                                @click="useTemplate(template.id)"
                            >
                                <FileBarChart class="w-5 h-5 text-blue-600" />
                                <h3 class="font-semibold text-gray-900">{{ template.name }}</h3>
                            </div>
                            <div class="flex items-center gap-2">
                                <span
                                    v-if="template.is_default"
                                    class="px-2 py-1 text-xs font-semibold rounded bg-blue-100 text-blue-800"
                                >
                                    Default
                                </span>
                                <Link
                                    v-if="canReportsEdit"
                                    :href="`/reports/templates/${template.id}/edit`"
                                    @click="editTemplate(template.id, $event)"
                                    class="inline-flex items-center justify-center px-2 py-1.5 md:px-3 md:py-1 border border-transparent text-xs font-medium rounded-md text-indigo-700 bg-indigo-100 hover:bg-indigo-200 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
                                    title="Edit template"
                                >
                                    <ListTableActionLabel label="Edit">
                                        <Edit class="w-4 h-4" />
                                    </ListTableActionLabel>
                                </Link>
                                <button
                                    v-if="canReportsDelete"
                                    type="button"
                                    @click="deleteTemplate(template.id, $event)"
                                    class="inline-flex items-center justify-center px-2 py-1.5 md:px-3 md:py-1 border border-transparent text-xs font-medium rounded-md text-red-700 bg-red-100 hover:bg-red-200 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-red-500"
                                    title="Delete template"
                                >
                                    <ListTableActionLabel label="Delete">
                                        <Trash2 class="w-4 h-4" />
                                    </ListTableActionLabel>
                                </button>
                            </div>
                        </div>
                        <div 
                            class="cursor-pointer"
                            @click="useTemplate(template.id)"
                        >
                            <p v-if="template.description" class="text-sm text-gray-600 mb-2">
                                {{ template.description }}
                            </p>
                            <div class="flex items-center gap-2 text-xs text-gray-500">
                                <span class="px-2 py-1 rounded bg-gray-100">
                                    {{ template.entity_type }}
                                </span>
                                <span v-if="template.creator">
                                    by {{ template.creator.name }}
                                </span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
 
            <!-- Reports Section -->
            <div>
                <h2 class="text-lg font-semibold text-gray-900 mb-4">Saved Reports</h2>
                <div v-if="props.reports.data.length === 0" class="bg-white rounded-lg border p-8 text-center">
                    <FileText class="w-12 h-12 text-gray-400 mx-auto mb-4" />
                    <p class="text-gray-600">No reports yet. Create your first report to get started.</p>
                </div>
                <div v-else class="bg-white rounded-lg border overflow-hidden">
                    <table class="w-full">
                        <thead class="bg-gray-50 border-b">
                            <tr>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                    Name
                                </th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                    Type
                                </th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                    Template
                                </th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                    Created By
                                </th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                    Created At
                                </th>
                                <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">
                                    Actions
                                </th>
                            </tr>
                        </thead>
                        <tbody class="bg-white divide-y divide-gray-200">
                            <tr
                                v-for="report in props.reports.data"
                                :key="report.id"
                                class="cursor-pointer hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-inset focus:ring-blue-500"
                                tabindex="0"
                                role="link"
                                @click="router.visit(`/reports/${report.id}`)"
                                @keydown.enter.prevent="router.visit(`/reports/${report.id}`)"
                                @keydown.space.prevent="router.visit(`/reports/${report.id}`)"
                            >
                                <td class="px-6 py-4 whitespace-nowrap">
                                    <div class="text-sm font-medium text-gray-900">{{ report.name }}</div>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap">
                                    <span class="px-2 py-1 text-xs font-semibold rounded bg-gray-100 text-gray-800">
                                        {{ report.entity_type }}
                                    </span>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap">
                                    <div class="text-sm text-gray-500">
                                        {{ report.template?.name || 'Custom' }}
                                    </div>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap">
                                    <div class="text-sm text-gray-500">
                                        {{ report.creator?.name || 'N/A' }}
                                    </div>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap">
                                    <div class="text-sm text-gray-500">
                                        {{ formatDate(report.created_at) }}
                                    </div>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium" @click.stop>
                                    <div class="flex items-center justify-end gap-2">
                                        <Link
                                            v-if="canReportsEdit"
                                            :href="`/reports/${report.id}/edit`"
                                            class="inline-flex items-center justify-center px-2 py-1.5 md:px-3 md:py-1 border border-transparent text-xs font-medium rounded-md text-indigo-700 bg-indigo-100 hover:bg-indigo-200 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
                                        >
                                            <ListTableActionLabel label="Edit">
                                                <Edit class="w-4 h-4" />
                                            </ListTableActionLabel>
                                        </Link>
                                        <button
                                            v-if="canReportsDelete"
                                            type="button"
                                            @click="deleteReport(report.id)"
                                            class="inline-flex items-center justify-center px-2 py-1.5 md:px-3 md:py-1 border border-transparent text-xs font-medium rounded-md text-red-700 bg-red-100 hover:bg-red-200 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-red-500"
                                        >
                                            <ListTableActionLabel label="Delete">
                                                <Trash2 class="w-4 h-4" />
                                            </ListTableActionLabel>
                                        </button>
                                    </div>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </AppLayout>
</template>