# All files / js/pages/reports Create.vue

0% Statements 0/169
0% Branches 0/132
0% Functions 0/64
0% Lines 0/150

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
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580

     <script setup lang="ts">
    import AppLayout from '@/layouts/AppLayout.vue';
    import { Head, useForm } from '@inertiajs/vue3';
    import { ref, computed, watch } from 'vue';
    import { Save, Eye, Plus, X } from 'lucide-vue-next';

    interface ReportTemplate {
        id: number;
        name: string;
        description: string | null;
        entity_type: 'invoice' | 'quote' | 'jobcard';
        config: any;
        filters?: {
            date_from?: string;
            date_to?: string;
            status?: string[];
            customer_id?: number[];
            product_id?: number[];
        };
        is_default: boolean;
    }

    interface Company {
        id: number;
        name: string;
    }

    interface Customer {
        id: number;
        name: string;
        account_code?: string | null;
    }

    interface Product {
        id: number;
        name: string;
        sku?: string | null;
    }

    interface Props {
        templates: ReportTemplate[];
        currentCompany: Company;
        entityType?: 'invoice' | 'quote' | 'jobcard';
        template?: ReportTemplate | null;
        customers?: Customer[];
        products?: Product[];
    }

    const props = withDefaults(defineProps<Props>(), {
        entityType: 'invoice',
        template: null,
        customers: () => [],
        products: () => [],
    });

    // Available columns for each entity type
    const availableColumns = {
        invoice: [
            { value: 'invoice_number', label: 'Invoice Number' },
            { value: 'customer.name', label: 'Customer' },
            { value: 'salesperson.name', label: 'Salesperson' },
            { value: 'formatted_date', label: 'Invoice Date' },
            { value: 'due_date', label: 'Due Date' },
            { value: 'status', label: 'Status' },
            { value: 'subtotal', label: 'Subtotal' },
            { value: 'tax_amount', label: 'Tax Amount' },
            { value: 'discount_amount', label: 'Discount' },
            { value: 'formatted_total', label: 'Total' },
            { value: 'payment_count', label: 'Payments Count', groupable: false, sortable: false },
            { value: 'last_payment_date', label: 'Last Payment Date', groupable: false, sortable: false },
            { value: 'payments_summary', label: 'Payments (Type + Amount)', groupable: false, sortable: false },
            { value: 'total_paid', label: 'Total Paid', groupable: false, sortable: false },
            { value: 'remaining_balance', label: 'Balance Due', groupable: false, sortable: false },
        ],
        quote: [
            { value: 'quote_number', label: 'Quote Number' },
            { value: 'customer.name', label: 'Customer' },
            { value: 'formatted_date', label: 'Date' },
            { value: 'expiry_date', label: 'Expiry Date' },
            { value: 'status', label: 'Status' },
            { value: 'subtotal', label: 'Subtotal' },
            { value: 'tax_amount', label: 'Tax Amount' },
            { value: 'discount_amount', label: 'Discount' },
            { value: 'formatted_total', label: 'Total' },
        ],
        jobcard: [
            { value: 'job_number', label: 'Job Number' },
            { value: 'customer.name', label: 'Customer' },
            { value: 'formatted_date', label: 'Start Date' },
            { value: 'due_date', label: 'Due Date' },
            { value: 'completed_date', label: 'Completed Date' },
            { value: 'status', label: 'Status' },
            { value: 'subtotal', label: 'Subtotal' },
            { value: 'tax_amount', label: 'Tax Amount' },
            { value: 'discount_amount', label: 'Discount' },
            { value: 'formatted_total', label: 'Total' },
        ],
    };

    const entityType = ref(props.entityType);
    const selectedColumns = ref<string[]>(props.template?.config?.columns || availableColumns[entityType.value].slice(0, 5).map(c => c.value));
    const groupBy = ref(props.template?.config?.group_by || '');
    const sortBy = ref(props.template?.config?.sort_by || 'created_at');
    const sortDirection = ref<'asc' | 'desc'>(props.template?.config?.sort_direction || 'desc');
    const showTotals = ref(props.template?.config?.show_totals ?? true);
    const groupableColumns = computed(() => availableColumns[entityType.value].filter(column => column.groupable !== false));
    const sortableColumns = computed(() => availableColumns[entityType.value].filter(column => column.sortable !== false));

    // Filters - these will be saved to template when "Save as Template" is clicked
    const dateFrom = ref(props.template?.filters?.date_from || '');
    const dateTo = ref(props.template?.filters?.date_to || '');
    const statusFilter = ref<string[]>(props.template?.filters?.status || []);
    const customerFilter = ref<number[]>(props.template?.filters?.customer_id || []);
    const productFilter = ref<number[]>(props.template?.filters?.product_id || []);

    // Customer search
    const customerSearchQuery = ref('');
    const customerSearchFocused = ref(false);
    const filteredCustomers = ref<Customer[]>([]);
    const selectedCustomers = ref<Customer[]>([]);

    // Product search
    const productSearchQuery = ref('');
    const productSearchFocused = ref(false);
    const filteredProducts = ref<Product[]>([]);
    const selectedProducts = ref<Product[]>([]);

    // Initialize selected customers/products from template filters
    if (customerFilter.value.length > 0) {
        selectedCustomers.value = props.customers.filter(c => customerFilter.value.includes(c.id));
    }

    if (productFilter.value.length > 0) {
        selectedProducts.value = props.products.filter(p => productFilter.value.includes(p.id));
    }

    const form = useForm({
        name: props.template?.name || '',
        entity_type: entityType.value,
        config: {
            columns: selectedColumns.value,
            group_by: groupBy.value,
            sort_by: sortBy.value,
            sort_direction: sortDirection.value,
            show_totals: showTotals.value,
        },
        filters: {
            date_from: dateFrom.value,
            date_to: dateTo.value,
            status: statusFilter.value,
            customer_id: customerFilter.value,
            product_id: productFilter.value,
        },
        report_template_id: props.template?.id || null,
    });

    // Watch for entity type changes
    watch(entityType, (newType) => {
        form.entity_type = newType;
        selectedColumns.value = availableColumns[newType].slice(0, 5).map(c => c.value);
        form.config.columns = selectedColumns.value;
        groupBy.value = '';
        form.config.group_by = '';
    });

    // Watch for config changes
    watch([selectedColumns, groupBy, sortBy, sortDirection, showTotals], () => {
        form.config = {
            columns: selectedColumns.value,
            group_by: groupBy.value,
            sort_by: sortBy.value,
            sort_direction: sortDirection.value,
            show_totals: showTotals.value,
        };
    });

    // Customer search functions
    const handleCustomerSearch = async () => {
        if (!customerSearchQuery.value.trim()) {
            filteredCustomers.value = [];
            return;
        }

        try {
            const response = await fetch(`/customers/search?q=${encodeURIComponent(customerSearchQuery.value)}`, {
                headers: {
                    'Accept': 'application/json',
                    'X-Requested-With': 'XMLHttpRequest',
                },
            });

            if (response.ok) {
                filteredCustomers.value = await response.json();
            } else {
                filteredCustomers.value = [];
            }
        } catch (error) {
            console.error('Error searching customers:', error);
            filteredCustomers.value = [];
        }
    };

    const toggleCustomer = (customer: Customer) => {
        const index = selectedCustomers.value.findIndex(c => c.id === customer.id);
        if (index > -1) {
            selectedCustomers.value.splice(index, 1);
        } else {
            selectedCustomers.value.push(customer);
        }
        customerFilter.value = selectedCustomers.value.map(c => c.id);
        customerSearchQuery.value = '';
        customerSearchFocused.value = false;
    };

    const removeCustomer = (customerId: number) => {
        selectedCustomers.value = selectedCustomers.value.filter(c => c.id !== customerId);
        customerFilter.value = selectedCustomers.value.map(c => c.id);
    };

    // Product search functions
    const handleProductSearch = () => {
        if (!productSearchQuery.value.trim()) {
            filteredProducts.value = [];
            return;
        }

        const query = productSearchQuery.value.toLowerCase();
        filteredProducts.value = props.products.filter(product => {
            const nameMatch = product.name?.toLowerCase().includes(query);
            const skuMatch = product.sku?.toLowerCase().includes(query);
            return nameMatch || skuMatch;
        }).slice(0, 10);
    };

    const toggleProduct = (product: Product) => {
        const index = selectedProducts.value.findIndex(p => p.id === product.id);
        if (index > -1) {
            selectedProducts.value.splice(index, 1);
        } else {
            selectedProducts.value.push(product);
        }
        productFilter.value = selectedProducts.value.map(p => p.id);
        productSearchQuery.value = '';
        productSearchFocused.value = false;
    };

    const removeProduct = (productId: number) => {
        selectedProducts.value = selectedProducts.value.filter(p => p.id !== productId);
        productFilter.value = selectedProducts.value.map(p => p.id);
    };

    // Watch for filter changes
    watch([dateFrom, dateTo, statusFilter, customerFilter, productFilter], () => {
        form.filters = {
            date_from: dateFrom.value,
            date_to: dateTo.value,
            status: statusFilter.value,
            customer_id: customerFilter.value,
            product_id: productFilter.value,
        };
    });

    const toggleColumn = (columnValue: string) => {
        const index = selectedColumns.value.indexOf(columnValue);
        if (index > -1) {
            selectedColumns.value.splice(index, 1);
        } else {
            selectedColumns.value.push(columnValue);
        }
    };

    const getStatusOptions = (type: string): string[] => {
        switch (type) {
            case 'invoice':
                return ['draft', 'sent', 'paid', 'overdue', 'cancelled'];
            case 'quote':
                return ['draft', 'sent', 'accepted', 'rejected', 'expired'];
            case 'jobcard':
                return ['pending', 'in_progress', 'completed', 'cancelled'];
            default:
                return [];
        }
    };

    const submit = () => {
        // Remove filters from form when creating report (reports don't have filters)
        const reportForm = { ...form.data() };
        delete reportForm.filters;
        form.transform(() => reportForm).post('/reports');
    };

    const saveAsTemplate = () => {
        // Save as template includes filters
        form.post('/reports/save-template');
    };
    </script>

    <template>
        <Head title="Run Report" />

        <AppLayout :breadcrumbs="[
            { title: 'Reports', href: '/reports' },
            { title: 'Create', href: '#' }
        ]">
            <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">Run Report</h1>
                    <div class="flex items-center gap-2">
                        <button
                            @click="saveAsTemplate"
                            class="rounded bg-gray-600 px-4 py-2 text-white hover:bg-gray-700 flex items-center gap-2"
                        >
                            <Save class="w-4 h-4" />
                            Save as Template
                        </button>
                        <button
                            @click="submit"
                            class="rounded bg-blue-600 px-4 py-2 text-white hover:bg-blue-700 flex items-center gap-2"
                        >
                            <Eye class="w-4 h-4" />
                            Run Report
                        </button>
                    </div>
                </div>

                <form @submit.prevent="submit" class="space-y-6">
                    <!-- Basic Information -->
                    <div class="bg-white rounded-lg border p-6">
                        <h2 class="text-lg font-semibold text-gray-900 mb-4">Basic Information</h2>
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                            <div>
                                <label class="block text-sm font-medium text-gray-700 mb-1">Report Name *</label>
                                <input
                                    v-model="form.name"
                                    type="text"
                                    class="w-full rounded border px-3 py-2"
                                    :class="{ 'border-red-500': form.errors.name }"
                                    required
                                />
                                <div v-if="form.errors.name" class="text-red-500 text-sm mt-1">
                                    {{ form.errors.name }}
                                </div>
                            </div>

                            <div>
                                <label class="block text-sm font-medium text-gray-700 mb-1">Entity Type *</label>
                                <select
                                    v-model="entityType"
                                    class="w-full rounded border px-3 py-2"
                                    :class="{ 'border-red-500': form.errors.entity_type }"
                                    required
                                >
                                    <option value="invoice">Invoice</option>
                                    <option value="quote">Quote</option>
                                    <option value="jobcard">Job Card</option>
                                </select>
                                <div v-if="form.errors.entity_type" class="text-red-500 text-sm mt-1">
                                    {{ form.errors.entity_type }}
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Column Selection -->
                    <div class="bg-white rounded-lg border p-6">
                        <h2 class="text-lg font-semibold text-gray-900 mb-4">Select Columns</h2>
                        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-3">
                            <label
                                v-for="column in availableColumns[entityType]"
                                :key="column.value"
                                class="flex items-center gap-2 p-3 border rounded cursor-pointer hover:bg-gray-50"
                                :class="{ 'bg-blue-50 border-blue-500': selectedColumns.includes(column.value) }"
                            >
                                <input
                                    type="checkbox"
                                    :value="column.value"
                                    :checked="selectedColumns.includes(column.value)"
                                    @change="toggleColumn(column.value)"
                                    class="rounded border-gray-300 text-blue-600 focus:ring-blue-500"
                                />
                                <span class="text-sm font-medium text-gray-700">{{ column.label }}</span>
                            </label>
                        </div>
                    </div>

                    <!-- Filters (for templates) -->
                    <div class="bg-white rounded-lg border p-6">
                        <h2 class="text-lg font-semibold text-gray-900 mb-4">Filters</h2>
                        <p class="text-sm text-gray-600 mb-4">These filters will be saved to the template when you click "Save as Template".</p>
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                            <div>
                                <label class="block text-sm font-medium text-gray-700 mb-1">Date From</label>
                                <input
                                    v-model="dateFrom"
                                    type="date"
                                    class="w-full rounded border px-3 py-2"
                                />
                            </div>

                            <div>
                                <label class="block text-sm font-medium text-gray-700 mb-1">Date To</label>
                                <input
                                    v-model="dateTo"
                                    type="date"
                                    class="w-full rounded border px-3 py-2"
                                />
                            </div>

                            <div class="relative">
                                <label class="block text-sm font-medium text-gray-700 mb-1">Customer</label>
                                <div class="relative">
                                    <input
                                        v-model="customerSearchQuery"
                                        @input="handleCustomerSearch"
                                        @focus="customerSearchFocused = true"
                                        @blur="() => setTimeout(() => customerSearchFocused = false, 200)"
                                        type="text"
                                        placeholder="Search customer..."
                                        class="w-full rounded border px-3 py-2"
                                    />
                                </div>
                                <div
                                    v-if="customerSearchFocused && filteredCustomers.length > 0"
                                    class="absolute z-50 mt-1 w-full bg-white border border-gray-300 rounded-md shadow-lg max-h-60 overflow-auto"
                                >
                                    <div
                                        v-for="customer in filteredCustomers"
                                        :key="customer.id"
                                        @mousedown.prevent="toggleCustomer(customer)"
                                        class="px-4 py-2 hover:bg-gray-100 cursor-pointer"
                                        :class="{ 'bg-blue-50': selectedCustomers.some(c => c.id === customer.id) }"
                                    >
                                        <div class="font-medium">{{ customer.name }}</div>
                                        <div class="text-xs text-gray-500" v-if="customer.account_code">{{ customer.account_code }}</div>
                                    </div>
                                </div>
                                <!-- Selected Customers -->
                                <div v-if="selectedCustomers.length > 0" class="mt-2 flex flex-wrap gap-2">
                                    <span
                                        v-for="customer in selectedCustomers"
                                        :key="customer.id"
                                        class="inline-flex items-center gap-1 px-2 py-1 bg-blue-100 text-blue-800 rounded text-sm"
                                    >
                                        {{ customer.name }}
                                        <button
                                            @click.prevent="removeCustomer(customer.id)"
                                            type="button"
                                            class="text-blue-600 hover:text-blue-800"
                                        >
                                            <X class="w-3 h-3" />
                                        </button>
                                    </span>
                                </div>
                            </div>

                            <div class="relative">
                                <label class="block text-sm font-medium text-gray-700 mb-1">Product</label>
                                <div class="relative">
                                    <input
                                        v-model="productSearchQuery"
                                        @input="handleProductSearch"
                                        @focus="productSearchFocused = true"
                                        @blur="() => setTimeout(() => productSearchFocused = false, 200)"
                                        type="text"
                                        placeholder="Search product..."
                                        class="w-full rounded border px-3 py-2"
                                    />
                                </div>
                                <div
                                    v-if="productSearchFocused && filteredProducts.length > 0"
                                    class="absolute z-50 mt-1 w-full bg-white border border-gray-300 rounded-md shadow-lg max-h-60 overflow-auto"
                                >
                                    <div
                                        v-for="product in filteredProducts"
                                        :key="product.id"
                                        @mousedown.prevent="toggleProduct(product)"
                                        class="px-4 py-2 hover:bg-gray-100 cursor-pointer"
                                        :class="{ 'bg-blue-50': selectedProducts.some(p => p.id === product.id) }"
                                    >
                                        <div class="font-medium">{{ product.name }}</div>
                                        <div class="text-xs text-gray-500" v-if="product.sku">SKU: {{ product.sku }}</div>
                                    </div>
                                </div>
                                <!-- Selected Products -->
                                <div v-if="selectedProducts.length > 0" class="mt-2 flex flex-wrap gap-2">
                                    <span
                                        v-for="product in selectedProducts"
                                        :key="product.id"
                                        class="inline-flex items-center gap-1 px-2 py-1 bg-green-100 text-green-800 rounded text-sm"
                                    >
                                        {{ product.name }}
                                        <button
                                            @click.prevent="removeProduct(product.id)"
                                            type="button"
                                            class="text-green-600 hover:text-green-800"
                                        >
                                            <X class="w-3 h-3" />
                                        </button>
                                    </span>
                                </div>
                            </div>

                            <div>
                                <label class="block text-sm font-medium text-gray-700 mb-1">Status</label>
                                <div class="space-y-2">
                                    <label
                                        v-for="status in getStatusOptions(entityType)"
                                        :key="status"
                                        class="flex items-center gap-2"
                                    >
                                        <input
                                            type="checkbox"
                                            :value="status"
                                            v-model="statusFilter"
                                            class="rounded border-gray-300 text-blue-600 focus:ring-blue-500"
                                        />
                                        <span class="text-sm text-gray-700">{{ status }}</span>
                                    </label>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Grouping & Sorting -->
                    <div class="bg-white rounded-lg border p-6">
                        <h2 class="text-lg font-semibold text-gray-900 mb-4">Grouping & Sorting</h2>
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                            <div>
                                <label class="block text-sm font-medium text-gray-700 mb-1">Group By</label>
                                <select v-model="groupBy" class="w-full rounded border px-3 py-2">
                                    <option value="">None</option>
                                    <option
                                        v-for="column in groupableColumns"
                                        :key="column.value"
                                        :value="column.value"
                                    >
                                        {{ column.label }}
                                    </option>
                                </select>
                            </div>

                            <div>
                                <label class="block text-sm font-medium text-gray-700 mb-1">Sort By</label>
                                <select v-model="sortBy" class="w-full rounded border px-3 py-2">
                                    <option
                                        v-for="column in sortableColumns"
                                        :key="column.value"
                                        :value="column.value"
                                    >
                                        {{ column.label }}
                                    </option>
                                </select>
                            </div>

                            <div>
                                <label class="block text-sm font-medium text-gray-700 mb-1">Sort Direction</label>
                                <select v-model="sortDirection" class="w-full rounded border px-3 py-2">
                                    <option value="asc">Ascending</option>
                                    <option value="desc">Descending</option>
                                </select>
                            </div>

                            <div class="flex items-end">
                                <label class="flex items-center gap-2">
                                    <input
                                        v-model="showTotals"
                                        type="checkbox"
                                        class="rounded border-gray-300 text-blue-600 focus:ring-blue-500"
                                    />
                                    <span class="text-sm font-medium text-gray-700">Show Totals</span>
                                </label>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </AppLayout>
    </template>