# All files / js/pages/stock-movements Show.vue

0% Statements 0/78
0% Branches 0/105
0% Functions 0/15
0% Lines 0/72

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

     <script setup lang="ts">
    import AppLayout from '@/layouts/AppLayout.vue';
    import { Head, Link } from '@inertiajs/vue3';
    import { ArrowLeft, ArrowUp, ArrowDown, Settings, Package, User, Calendar, FileText, DollarSign, Hash } from 'lucide-vue-next';
    import stockMovements from '@/routes/stock-movements';

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

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

    interface Reference {
        id: number;
        [key: string]: any;
    }

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

    interface SerialNumber {
        id: number;
        serial_number: string;
        status: string;
    }

    interface StockMovement {
        id: number;
        product: Product;
        type: 'in' | 'out' | 'adjustment' | 'transfer';
        quantity: number;
        unit_cost: number | null;
        stock_before: number;
        stock_after: number;
        reference: string | null;
        reference_type: string | null;
        reference_id: number | null;
        reference?: Reference | null;
        to_company_id: number | null;
        to_company?: Company | null;
        serial_number?: SerialNumber | null;
        serialNumbers?: SerialNumber[];
        notes: string | null;
        user: User | null;
        created_at: string;
        updated_at: string;
    }

    interface Props {
        movement: StockMovement;
    }

    const props = defineProps<Props>();

    function getTypeIcon(type: string) {
        return type === 'in' ? ArrowUp : type === 'out' ? ArrowDown : Settings;
    }

    function getTypeColor(type: string) {
        return {
            'in': 'text-green-600 bg-green-50',
            'out': 'text-red-600 bg-red-50',
            'adjustment': 'text-blue-600 bg-blue-50',
            'transfer': 'text-yellow-600 bg-yellow-50',
        }[type] || 'text-gray-600 bg-gray-50';
    }

    function formatCurrency(amount: number | null | undefined): string {
        if (amount === null || amount === undefined) return 'N/A';
        return `R${Number(amount).toFixed(2)}`;
    }

    function formatDate(dateString: string): string {
        return new Date(dateString).toLocaleDateString('en-ZA', {
            year: 'numeric',
            month: 'long',
            day: 'numeric',
            hour: '2-digit',
            minute: '2-digit',
        });
    }

    function getReferenceLink(): string | null {
        if (!props.movement.reference_type || !props.movement.reference_id) {
            return null;
        }

        const type = props.movement.reference_type;
        const id = props.movement.reference_id;

        // Map reference types to routes
        const routeMap: Record<string, (id: number) => { url: string }> = {
            'purchase_order': (id: number) => ({ url: `/purchase-orders/${id}` }),
            'invoice': (id: number) => ({ url: `/invoices/${id}` }),
            'quote': (id: number) => ({ url: `/quotes/${id}` }),
            'jobcard': (id: number) => ({ url: `/jobcards/${id}` }),
        };

        const routeFn = routeMap[type];
        return routeFn ? routeFn(id).url : null;
    }

    function getReferenceDisplayName(): string {
        if (!props.movement.reference_type || !props.movement.reference) {
            return props.movement.reference || 'N/A';
        }

        const ref = props.movement.reference;
        const type = props.movement.reference_type;

        // Try to get a display name from the reference object
        if (ref.po_number) return `PO-${ref.po_number}`;
        if (ref.invoice_number) return ref.invoice_number;
        if (ref.quote_number) return ref.quote_number;
        if (ref.job_number) return ref.job_number;
        if (ref.title) return ref.title;

        return props.movement.reference || `${type} #${props.movement.reference_id}`;
    }
    </script>

    <template>
        <Head :title="`Stock Movement #${props.movement.id}`" />

        <AppLayout :breadcrumbs="[
            { title: 'Stock Movements', href: stockMovements.index().url },
            { title: `Movement #${props.movement.id}`, href: '#' }
        ]">
            <div class="space-y-6 p-4">
                <!-- Header Section -->
                <div class="rounded-lg bg-white border border-gray-200 p-6 shadow-sm">
                    <div class="flex items-center justify-between">
                        <div>
                            <h1 class="text-2xl font-bold text-gray-900">Stock Movement #{{ props.movement.id }}</h1>
                            <p class="text-sm text-gray-500 mt-1">Movement Details</p>
                        </div>
                        <div class="flex items-center gap-2">
                            <Link
                                :href="stockMovements.index().url"
                                class="flex items-center gap-2 rounded-md border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2"
                            >
                                <ArrowLeft class="h-4 w-4" />
                                Back
                            </Link>
                        </div>
                    </div>
                </div>

                <!-- Movement Information -->
                <div class="rounded-lg bg-white border border-gray-200 shadow-sm">
                    <div class="border-b border-gray-200 bg-gray-50 px-6 py-4">
                        <h2 class="text-lg font-semibold text-gray-900">Movement Information</h2>
                        <p class="text-sm text-gray-600">Details about this stock movement</p>
                    </div>
                    <div class="p-6">
                        <div class="grid gap-6 md:grid-cols-2">
                            <!-- Type -->
                            <div class="flex items-center gap-3">
                                <div class="flex-shrink-0">
                                    <div class="flex h-10 w-10 items-center justify-center rounded-lg bg-gray-100">
                                        <component
                                            :is="getTypeIcon(props.movement.type)"
                                            class="h-5 w-5 text-gray-600"
                                        />
                                    </div>
                                </div>
                                <div class="flex-1">
                                    <div class="text-sm font-medium text-gray-500">Type</div>
                                    <span
                                        :class="[
                                            'inline-flex items-center gap-1 rounded-full px-3 py-1 text-sm font-semibold capitalize mt-1',
                                            getTypeColor(props.movement.type)
                                        ]"
                                    >
                                        <component
                                            :is="getTypeIcon(props.movement.type)"
                                            class="h-3 w-3"
                                        />
                                        {{ props.movement.type }}
                                    </span>
                                </div>
                            </div>

                            <!-- Product -->
                            <div class="flex items-center gap-3">
                                <Package class="h-5 w-5 text-gray-400 flex-shrink-0" />
                                <div class="flex-1">
                                    <div class="text-sm font-medium text-gray-500">Product</div>
                                    <div class="mt-1">
                                        <Link
                                            v-if="props.movement.product"
                                            :href="`/products/${props.movement.product.id}`"
                                            class="text-sm font-medium text-blue-600 hover:text-blue-800"
                                        >
                                            {{ props.movement.product.name }}
                                        </Link>
                                        <span v-if="props.movement.product?.sku" class="text-sm text-gray-500 ml-2">
                                            ({{ props.movement.product.sku }})
                                        </span>
                                    </div>
                                </div>
                            </div>

                            <!-- Quantity -->
                            <div class="flex items-center gap-3">
                                <Hash class="h-5 w-5 text-gray-400 flex-shrink-0" />
                                <div class="flex-1">
                                    <div class="text-sm font-medium text-gray-500">Quantity</div>
                                    <div class="text-lg font-semibold text-gray-900 mt-1">
                                        {{ props.movement.type === 'in' ? '+' : props.movement.type === 'out' ? '-' : '' }}{{ Math.abs(props.movement.quantity) }}
                                    </div>
                                </div>
                            </div>

                            <!-- Unit Cost -->
                            <div v-if="props.movement.unit_cost" class="flex items-center gap-3">
                                <DollarSign class="h-5 w-5 text-gray-400 flex-shrink-0" />
                                <div class="flex-1">
                                    <div class="text-sm font-medium text-gray-500">Unit Cost</div>
                                    <div class="text-lg font-semibold text-gray-900 mt-1">
                                        {{ formatCurrency(props.movement.unit_cost) }}
                                    </div>
                                </div>
                            </div>

                            <!-- Stock Before -->
                            <div class="flex items-center gap-3">
                                <div class="flex-shrink-0">
                                    <div class="flex h-10 w-10 items-center justify-center rounded-lg bg-gray-100">
                                        <span class="text-sm font-medium text-gray-600">Before</span>
                                    </div>
                                </div>
                                <div class="flex-1">
                                    <div class="text-sm font-medium text-gray-500">Stock Before</div>
                                    <div class="text-lg font-semibold text-gray-900 mt-1">
                                        {{ props.movement.stock_before }}
                                    </div>
                                </div>
                            </div>

                            <!-- Stock After -->
                            <div class="flex items-center gap-3">
                                <div class="flex-shrink-0">
                                    <div class="flex h-10 w-10 items-center justify-center rounded-lg bg-green-100">
                                        <span class="text-sm font-medium text-green-700">After</span>
                                    </div>
                                </div>
                                <div class="flex-1">
                                    <div class="text-sm font-medium text-gray-500">Stock After</div>
                                    <div class="text-lg font-semibold text-gray-900 mt-1">
                                        {{ props.movement.stock_after }}
                                    </div>
                                </div>
                            </div>

                            <!-- Transfer Destination (for transfers) -->
                            <div v-if="props.movement.type === 'transfer' && props.movement.to_company" class="flex items-center gap-3">
                                <div class="flex-shrink-0">
                                    <div class="flex h-10 w-10 items-center justify-center rounded-lg bg-yellow-100">
                                        <span class="text-sm font-medium text-yellow-700">To</span>
                                    </div>
                                </div>
                                <div class="flex-1">
                                    <div class="text-sm font-medium text-gray-500">Transferred To</div>
                                    <div class="text-sm font-medium text-gray-900 mt-1">
                                        {{ props.movement.to_company.name }}
                                    </div>
                                </div>
                            </div>

                            <!-- Reference -->
                            <div v-if="props.movement.reference && props.movement.type !== 'transfer'" class="flex items-center gap-3">
                                <FileText class="h-5 w-5 text-gray-400 flex-shrink-0" />
                                <div class="flex-1">
                                    <div class="text-sm font-medium text-gray-500">Reference</div>
                                    <div class="mt-1">
                                        <Link
                                            v-if="getReferenceLink()"
                                            :href="getReferenceLink()"
                                            class="text-sm font-medium text-blue-600 hover:text-blue-800"
                                        >
                                            {{ getReferenceDisplayName() }}
                                        </Link>
                                        <span v-else class="text-sm text-gray-900">
                                            {{ getReferenceDisplayName() }}
                                        </span>
                                    </div>
                                </div>
                            </div>

                            <!-- User -->
                            <div v-if="props.movement.user" class="flex items-center gap-3">
                                <User class="h-5 w-5 text-gray-400 flex-shrink-0" />
                                <div class="flex-1">
                                    <div class="text-sm font-medium text-gray-500">Recorded By</div>
                                    <div class="text-sm font-medium text-gray-900 mt-1">
                                        {{ props.movement.user.name }}
                                    </div>
                                </div>
                            </div>

                            <!-- Date -->
                            <div class="flex items-center gap-3">
                                <Calendar class="h-5 w-5 text-gray-400 flex-shrink-0" />
                                <div class="flex-1">
                                    <div class="text-sm font-medium text-gray-500">Date & Time</div>
                                    <div class="text-sm text-gray-900 mt-1">
                                        {{ formatDate(props.movement.created_at) }}
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Serial Numbers -->
                <div v-if="(props.movement.serialNumbers && props.movement.serialNumbers.length > 0) || props.movement.serial_number" class="rounded-lg bg-white border border-gray-200 shadow-sm">
                    <div class="border-b border-gray-200 bg-gray-50 px-6 py-4">
                        <h2 class="text-lg font-semibold text-gray-900">Serial Numbers</h2>
                        <p class="text-sm text-gray-600">Serial numbers associated with this movement</p>
                    </div>
                    <div class="p-6">
                        <div v-if="props.movement.serialNumbers && props.movement.serialNumbers.length > 0" class="flex flex-wrap gap-2">
                            <span
                                v-for="serial in props.movement.serialNumbers"
                                :key="serial.id"
                                class="inline-flex items-center rounded-full px-3 py-1 text-sm font-medium"
                                :class="{
                                    'bg-green-100 text-green-800': serial.status === 'available',
                                    'bg-blue-100 text-blue-800': serial.status === 'sold',
                                    'bg-gray-100 text-gray-800': !serial.status || serial.status === 'returned',
                                    'bg-red-100 text-red-800': serial.status === 'damaged' || serial.status === 'scrapped',
                                }"
                            >
                                {{ serial.serial_number }}
                                <span class="ml-2 text-xs opacity-75">({{ serial.status }})</span>
                            </span>
                        </div>
                        <div v-else-if="props.movement.serial_number" class="flex flex-wrap gap-2">
                            <span
                                class="inline-flex items-center rounded-full px-3 py-1 text-sm font-medium"
                                :class="{
                                    'bg-green-100 text-green-800': props.movement.serial_number.status === 'available',
                                    'bg-blue-100 text-blue-800': props.movement.serial_number.status === 'sold',
                                    'bg-gray-100 text-gray-800': !props.movement.serial_number.status || props.movement.serial_number.status === 'returned',
                                    'bg-red-100 text-red-800': props.movement.serial_number.status === 'damaged' || props.movement.serial_number.status === 'scrapped',
                                }"
                            >
                                {{ props.movement.serial_number.serial_number }}
                                <span class="ml-2 text-xs opacity-75">({{ props.movement.serial_number.status }})</span>
                            </span>
                        </div>
                    </div>
                </div>

                <!-- Notes -->
                <div v-if="props.movement.notes" class="rounded-lg bg-white border border-gray-200 shadow-sm">
                    <div class="border-b border-gray-200 bg-gray-50 px-6 py-4">
                        <h2 class="text-lg font-semibold text-gray-900">Notes</h2>
                        <p class="text-sm text-gray-600">Additional information about this movement</p>
                    </div>
                    <div class="p-6">
                        <p class="text-sm text-gray-900 whitespace-pre-wrap">{{ props.movement.notes }}</p>
                    </div>
                </div>
            </div>
        </AppLayout>
    </template>