# All files / js/pages/client-zone DocumentView.vue

0% Statements 0/154
0% Branches 0/136
0% Functions 0/37
0% Lines 0/132

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

     <script setup lang="ts">
    import AppLayout from '@/layouts/AppLayout.vue';
    import { useNumberFormat } from '@/composables/useNumberFormat';
    import { Head, useForm } from '@inertiajs/vue3';
    import { computed, onMounted, ref } from 'vue';
    import { useDateTimeFormat } from '@/composables/useDateTimeFormat';

    const props = defineProps<{
        documentType: 'invoice' | 'quote' | 'jobcard';
        documentTitle: string;
        document: Record<string, any>;
        statusLabel: string;
        downloadPdfUrl: string;
        signUrl: string;
        documentSigningEnabled: boolean;
    }>();

    const canvasRef = ref<HTMLCanvasElement | null>(null);
    const drawing = ref(false);
    const hasDrawnSignature = ref(false);
    const ctx = computed(() => canvasRef.value?.getContext('2d') ?? null);
    const { formatDateTime } = useDateTimeFormat();
    const { formatCurrency } = useNumberFormat();

    const form = useForm({
        signer_name: '',
        signature_data: '',
    });

    function isRoundingLine(item: Record<string, any>): boolean {
        return String(item?.description ?? '')
            .trim()
            .toLowerCase() === 'rounding adjustment';
    }

    const lineItemsRaw = computed(() => props.document.line_items ?? props.document.lineItems ?? []);

    const lineGroupsRaw = computed(() => props.document.line_groups ?? props.document.lineGroups ?? []);

    interface ResolvedGroup {
        name: string | null;
        items: Record<string, any>[];
        subtotal: number;
    }

    /** Match PDF grouping: groups with items, then ungrouped under "Items". */
    const resolvedLineGroups = computed((): ResolvedGroup[] => {
        const items = [...lineItemsRaw.value].filter((i) => !isRoundingLine(i));
        items.sort((a, b) => (Number(a.sort_order) || 0) - (Number(b.sort_order) || 0));

        const groups = [...lineGroupsRaw.value].sort(
            (a, b) => (Number(a.sort_order) || 0) - (Number(b.sort_order) || 0),
        );

        const renderedIds = new Set<number>();
        const result: ResolvedGroup[] = [];

        for (const g of groups) {
            const gid = Number(g.id);
            const groupItems = items.filter((i) => Number(i.line_group_id) === gid);
            if (groupItems.length === 0) {
                continue;
            }
            const subtotal = groupItems.reduce((s, i) => s + (Number(i.total) || 0), 0);
            result.push({ name: g.name ?? null, items: groupItems, subtotal });
            groupItems.forEach((i) => renderedIds.add(Number(i.id)));
        }

        const ungrouped = items.filter((i) => !renderedIds.has(Number(i.id)));

        if (result.length === 0 && items.length > 0) {
            const subtotal = items.reduce((s, i) => s + (Number(i.total) || 0), 0);
            result.push({ name: 'Items', items, subtotal });
        } else if (ungrouped.length > 0) {
            const subtotal = ungrouped.reduce((s, i) => s + (Number(i.total) || 0), 0);
            result.push({ name: 'Items', items: ungrouped, subtotal });
        }

        return result;
    });

    const roundingAdjustment = computed(() => {
        return lineItemsRaw.value.reduce((sum, item) => {
            if (!isRoundingLine(item)) {
                return sum;
            }
            return (
                sum +
                (Number(item.total) ||
                    (Number(item.quantity) || 0) * (Number(item.unit_price) || 0))
            );
        }, 0);
    });

    const subtotalExcludingRounding = computed(() => {
        return lineItemsRaw.value.reduce((sum, item) => {
            if (isRoundingLine(item)) {
                return sum;
            }
            return sum + (Number(item.total) || 0);
        }, 0);
    });

    const taxAmount = computed(() => Number(props.document.tax_amount) || 0);
    const discountAmount = computed(() => Number(props.document.discount_amount) || 0);
    const discountPercentage = computed(() => Number(props.document.discount_percentage) || 0);
    const documentTotal = computed(() => Number(props.document.total) || 0);
    const taxRate = computed(() => Number(props.document.tax_rate) || 0);

    const taxLabel = computed(() => (taxRate.value > 0 ? `Tax (${taxRate.value}%)` : 'Tax'));

    /** Map pointer position to canvas bitmap coordinates (canvas may be CSS-scaled vs width/height attributes). */
    function getPos(event: MouseEvent | TouchEvent): { x: number; y: number } {
        const canvas = canvasRef.value;
        if (!canvas) {
            return { x: 0, y: 0 };
        }
        const rect = canvas.getBoundingClientRect();
        const scaleX = canvas.width / rect.width;
        const scaleY = canvas.height / rect.height;

        let clientX: number;
        let clientY: number;
        if ('touches' in event && event.touches.length > 0) {
            clientX = event.touches[0].clientX;
            clientY = event.touches[0].clientY;
        } else if ('clientX' in event) {
            clientX = event.clientX;
            clientY = event.clientY;
        } else {
            return { x: 0, y: 0 };
        }

        return {
            x: (clientX - rect.left) * scaleX,
            y: (clientY - rect.top) * scaleY,
        };
    }

    function startDraw(event: MouseEvent | TouchEvent) {
        if (!ctx.value) return;
        drawing.value = true;
        const p = getPos(event);
        ctx.value.beginPath();
        ctx.value.moveTo(p.x, p.y);
    }

    function moveDraw(event: MouseEvent | TouchEvent) {
        if (!drawing.value || !ctx.value || !canvasRef.value) return;
        const canvas = canvasRef.value;
        const rect = canvas.getBoundingClientRect();
        const scale = (canvas.width / rect.width + canvas.height / rect.height) / 2;
        const p = getPos(event);
        const context = ctx.value;
        context.strokeStyle = '#111827';
        context.lineWidth = Math.max(2, 2 * scale);
        context.lineCap = 'round';
        context.lineJoin = 'round';
        context.lineTo(p.x, p.y);
        context.stroke();
        context.beginPath();
        context.moveTo(p.x, p.y);
        hasDrawnSignature.value = true;
    }

    function endDraw() {
        drawing.value = false;
    }

    function clearSignature() {
        const canvas = canvasRef.value;
        const context = ctx.value;
        if (!canvas || !context) return;
        context.clearRect(0, 0, canvas.width, canvas.height);
        hasDrawnSignature.value = false;
        form.signature_data = '';
    }

    function submitSignature() {
        if (!canvasRef.value || !hasDrawnSignature.value) return;
        form.signature_data = canvasRef.value.toDataURL('image/png');
        form.post(props.signUrl, {
            preserveScroll: true,
            onSuccess: () => clearSignature(),
        });
    }

    onMounted(() => {
        const context = ctx.value;
        if (!context) return;
        context.fillStyle = '#ffffff';
        context.fillRect(0, 0, 800, 220);
    });
    </script>

    <template>
        <Head :title="`${documentType.toUpperCase()} ${documentTitle}`" />
        <AppLayout :breadcrumbs="[{ title: 'Client Zone', href: '/client-zone' }, { title: documentTitle, href: '#' }]">
            <div class="space-y-6 p-4">
                <div class="flex items-center justify-between">
                    <h1 class="text-2xl font-semibold">{{ documentType.toUpperCase() }} {{ documentTitle }}</h1>
                    <a :href="downloadPdfUrl" class="rounded bg-slate-700 px-4 py-2 text-sm text-white hover:bg-slate-800">Download PDF</a>
                </div>

                <div class="rounded border p-4">
                    <div class="mb-2 text-sm text-gray-700">Customer: {{ document.customer?.name }}</div>
                    <div class="mb-2 text-sm text-gray-700">Status: {{ statusLabel }}</div>
                    <div class="mb-4 text-sm text-gray-700">Company: {{ document.company?.name }}</div>

                    <h2 class="mb-2 text-lg font-medium">Line items</h2>
                    <div class="overflow-x-auto">
                        <table class="min-w-full border text-sm">
                            <thead>
                                <tr class="bg-gray-50">
                                    <th class="border p-2 text-left">Description</th>
                                    <th class="border p-2 text-right">Qty</th>
                                    <th class="border p-2 text-right">Unit</th>
                                    <th class="border p-2 text-right">Line total</th>
                                </tr>
                            </thead>
                            <tbody>
                                <template v-for="(group, gi) in resolvedLineGroups" :key="gi">
                                    <tr v-if="group.name" class="bg-gray-100">
                                        <td class="border p-2 font-semibold text-gray-900" colspan="4">{{ group.name }}</td>
                                    </tr>
                                    <tr v-for="line in group.items" :key="line.id">
                                        <td class="border p-2">{{ line.description }}</td>
                                        <td class="border p-2 text-right">{{ line.quantity }}</td>
                                        <td class="border p-2 text-right">{{ formatCurrency(Number(line.unit_price) || 0) }}</td>
                                        <td class="border p-2 text-right">{{ formatCurrency(Number(line.total) || 0) }}</td>
                                    </tr>
                                    <tr v-if="resolvedLineGroups.length > 1 || (group.name && group.name !== 'Items')" class="bg-slate-50">
                                        <td class="border p-2 text-right font-medium text-gray-700" colspan="3">Subtotal ({{ group.name || 'Section' }})</td>
                                        <td class="border p-2 text-right font-medium text-gray-900">{{ formatCurrency(group.subtotal) }}</td>
                                    </tr>
                                </template>
                            </tbody>
                        </table>
                    </div>

                    <div class="mt-6 border-t border-gray-200 pt-4">
                        <h3 class="mb-3 text-base font-medium text-gray-900">Totals</h3>
                        <dl class="max-w-md space-y-2 text-sm">
                            <div class="flex justify-between gap-4">
                                <dt class="text-gray-600">Subtotal</dt>
                                <dd class="text-right font-medium text-gray-900">{{ formatCurrency(subtotalExcludingRounding) }}</dd>
                            </div>
                            <div v-if="discountAmount > 0" class="flex justify-between gap-4">
                                <dt class="text-gray-600">
                                    Discount
                                    <span v-if="discountPercentage > 0" class="text-gray-500"> ({{ discountPercentage }}%)</span>
                                </dt>
                                <dd class="text-right font-medium text-red-700">-{{ formatCurrency(discountAmount) }}</dd>
                            </div>
                            <div v-if="taxAmount > 0" class="flex justify-between gap-4">
                                <dt class="text-gray-600">{{ taxLabel }}</dt>
                                <dd class="text-right font-medium text-gray-900">{{ formatCurrency(taxAmount) }}</dd>
                            </div>
                            <div v-if="Math.abs(roundingAdjustment) > 0.0001" class="flex justify-between gap-4">
                                <dt class="text-gray-600">Rounding adjustment</dt>
                                <dd class="text-right font-medium text-gray-900">{{ formatCurrency(roundingAdjustment) }}</dd>
                            </div>
                            <div class="flex justify-between gap-4 border-t border-gray-200 pt-2 text-base">
                                <dt class="font-semibold text-gray-900">Total</dt>
                                <dd class="text-right font-semibold text-gray-900">{{ formatCurrency(documentTotal) }}</dd>
                            </div>
                        </dl>
                    </div>
                </div>

                <div class="rounded border p-4">
                    <h2 class="mb-3 text-lg font-medium">Signatures</h2>
                    <div v-if="(document.signatures || []).length" class="mb-4 space-y-2 text-sm">
                        <div v-for="sig in document.signatures" :key="sig.id" class="rounded border p-2">
                            <div><strong>{{ sig.signer_name }}</strong> - {{ sig.signed_at ? formatDateTime(sig.signed_at) : '-' }}</div>
                            <img v-if="sig.signature_url" :src="sig.signature_url" alt="Signature" class="mt-2 h-16" />
                        </div>
                    </div>

                    <div v-if="documentSigningEnabled" class="space-y-3">
                        <input v-model="form.signer_name" class="w-full rounded border px-3 py-2" placeholder="Your name" />
                        <div class="rounded border bg-white p-2">
                            <canvas
                                ref="canvasRef"
                                width="800"
                                height="220"
                                class="w-full touch-none border"
                                @mousedown="startDraw"
                                @mousemove="moveDraw"
                                @mouseup="endDraw"
                                @mouseleave="endDraw"
                                @touchstart.prevent="startDraw"
                                @touchmove.prevent="moveDraw"
                                @touchend.prevent="endDraw"
                            />
                        </div>
                        <div class="flex gap-2">
                            <button type="button" class="rounded border px-3 py-2" @click="clearSignature">Clear</button>
                            <button type="button" class="rounded bg-blue-600 px-4 py-2 text-white" :disabled="form.processing || !hasDrawnSignature || !form.signer_name" @click="submitSignature">
                                {{ form.processing ? 'Signing...' : 'Sign Document' }}
                            </button>
                        </div>
                    </div>
                    <p v-else class="text-sm text-gray-600">Document signing is not enabled for this company.</p>
                </div>
            </div>
        </AppLayout>
    </template>