modified logic in chat bot
Some checks are pending
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Waiting to run
Gemini PR Review / Gemini PR Review (pull_request) Waiting to run
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Waiting to run
Laravel Larastan / larastan (pull_request) Waiting to run
Laravel Pint / pint (pull_request) Waiting to run

This commit is contained in:
dhanabalan
2026-05-20 12:20:22 +05:30
parent 203d09712b
commit ccd922b5bb
3 changed files with 309 additions and 69 deletions

View File

@@ -51,9 +51,18 @@ class ChatBot extends Component
// ── Basic mode — Invoice status (scan status) ─────────────────────────────
public string $invoiceNumber = '';
public string $invoiceStatusResult = '';
public string $invoiceStatusResult = ''; // kept for simple error strings
public bool $hasInvoiceStatusResult = false;
/**
* Structured result from ChatbotService::getInvoiceData().
* Shape: type, message, invoice_number, total, scanned, not_scanned, unscanned_serials[]
*/
public array $invoiceStatusData = [];
/** Controls whether all unscanned serials are shown (vs the first 10). */
public bool $showAllUnscanned = false;
// ── Advanced mode ─────────────────────────────────────────────────────────
public string $advancedQuestion = '';
public string $advancedResult = '';
@@ -102,6 +111,8 @@ class ChatBot extends Component
$this->hasInvoiceResult = false;
$this->invoiceStatusResult = '';
$this->hasInvoiceStatusResult = false;
$this->invoiceStatusData = [];
$this->showAllUnscanned = false;
}
// ── Basic mode — Production helpers ──────────────────────────────────────
@@ -304,11 +315,14 @@ class ChatBot extends Component
{
$this->invoiceStatusResult = '';
$this->hasInvoiceStatusResult = false;
$this->invoiceStatusData = [];
$this->showAllUnscanned = false;
}
/**
* Looks up how many serials within an invoice have been scanned / not scanned.
* Delegates to ChatbotService so the DB query and formatting logic live in one place.
* Stores structured data in $invoiceStatusData so the blade can render
* a "show more" serial-number list without dumping 70+ serials in one blob.
*/
public function fetchInvoiceStatus(): void
{
@@ -316,26 +330,48 @@ class ChatBot extends Component
if (empty($invoiceNumber)) {
$this->invoiceStatusResult = 'Please enter a valid invoice number.';
$this->invoiceStatusData = [];
$this->showAllUnscanned = false;
$this->hasInvoiceStatusResult = true;
return;
}
try {
/** @var ChatbotService $service */
$service = app(ChatbotService::class);
$this->invoiceStatusResult = $service->ask("invoice = {$invoiceNumber}");
/** @var \App\Services\ChatbotService $service */
$service = app(\App\Services\ChatbotService::class);
$data = $service->getInvoiceData($invoiceNumber);
} catch (\Throwable $e) {
Log::error('ChatBot: invoice status fetch failed', [
'invoice' => $invoiceNumber,
'error' => $e->getMessage(),
]);
$this->invoiceStatusResult = "Sorry, I couldn't fetch data for invoice {$invoiceNumber}. "
. 'Please try again or contact support.';
$data = [
'type' => 'error',
'message' => "Sorry, I couldn't fetch data for invoice {$invoiceNumber}. "
. 'Please try again or contact support.',
'invoice_number' => $invoiceNumber,
'total' => 0,
'scanned' => 0,
'not_scanned' => 0,
'unscanned_serials' => [],
];
}
$this->invoiceStatusData = $data;
$this->invoiceStatusResult = $data['message']; // fallback plain-text copy
$this->showAllUnscanned = false;
$this->hasInvoiceStatusResult = true;
}
/**
* Toggles the "show all / show less" state for unscanned serial numbers
* in the Basic Invoice Status result card.
*/
public function toggleShowAllUnscanned(): void
{
$this->showAllUnscanned = ! $this->showAllUnscanned;
}
// ── Advanced mode (Gemini-powered) ────────────────────────────────────────
/**
@@ -429,6 +465,8 @@ class ChatBot extends Component
$this->invoiceNumber = '';
$this->invoiceStatusResult = '';
$this->hasInvoiceStatusResult = false;
$this->invoiceStatusData = [];
$this->showAllUnscanned = false;
// Advanced mode
$this->clearAdvancedChat();

View File

@@ -83,14 +83,87 @@ class ChatbotService
/**
* Looks up scan status for an invoice number in invoice_validations.
* Returns a plain-English string (used by the Advanced / free-text path).
* Structured callers should use getInvoiceData() directly.
*/
private function handleInvoice(string $invoiceNumber, string $_unused = ''): string
{
// Strip any whitespace within the invoice number itself
$data = $this->getInvoiceData($invoiceNumber);
// For the plain-text path (advanced mode / ChatbotService::ask()),
// reassemble a human-readable sentence from the structured data.
if (in_array($data['type'], ['invalid', 'error', 'not_found'], true)) {
return $data['message'];
}
if ($data['type'] === 'all_scanned') {
$n = $data['total'];
$itemWord = $n === 1 ? 'serial number' : 'serial numbers';
return "For invoice number {$data['invoice_number']}, all {$n} {$itemWord} "
. ($n === 1 ? 'has' : 'have') . ' been scanned. ✅';
}
// partial or none_scanned
$total = $data['total'];
$scanned = $data['scanned'];
$notScan = $data['not_scanned'];
$inv = $data['invoice_number'];
$itemWord = $total === 1 ? 'serial number' : 'serial numbers';
if ($scanned === 0) {
$msg = "For invoice number {$inv}, there "
. ($total === 1 ? 'is' : 'are') . " {$total} {$itemWord} "
. 'and none have been scanned.';
} else {
$msg = "For invoice number {$inv}, there "
. ($total === 1 ? 'is' : 'are') . " {$total} {$itemWord} in total. "
. "Out of which {$scanned} "
. ($scanned === 1 ? 'has' : 'have') . ' been scanned and '
. "{$notScan} "
. ($notScan === 1 ? 'has' : 'have') . ' not been scanned.';
}
if (! empty($data['unscanned_serials'])) {
$msg .= ' Unscanned serial numbers are: '
. implode(', ', $data['unscanned_serials']) . '.';
}
return $msg;
}
// ─────────────────────────────────────────────────────────────────────────
// Public structured accessor — used by ChatBot (Basic mode)
// ─────────────────────────────────────────────────────────────────────────
/**
* Returns structured scan-status data for an invoice number.
*
* Return shape:
* [
* 'type' => 'all_scanned' | 'partial' | 'none_scanned'
* | 'not_found' | 'error' | 'invalid',
* 'message' => string, // one-line human summary (no serial list)
* 'invoice_number' => string,
* 'total' => int,
* 'scanned' => int,
* 'not_scanned' => int,
* 'unscanned_serials' => string[], // full list — may be large
* ]
*/
public function getInvoiceData(string $invoiceNumber): array
{
$invoiceNumber = preg_replace('/\s+/', '', $invoiceNumber);
if (empty($invoiceNumber)) {
return 'Please provide a valid invoice number. Example: invoice = 3RA0013333';
return [
'type' => 'invalid',
'message' => 'Please provide a valid invoice number. Example: invoice = 3RA0013333',
'invoice_number' => '',
'total' => 0,
'scanned' => 0,
'not_scanned' => 0,
'unscanned_serials' => [],
];
}
try {
@@ -114,71 +187,88 @@ class ChatbotService
'error' => $e->getMessage(),
]);
return "Sorry, I couldn't fetch data for invoice {$invoiceNumber}. "
. 'Please try again or contact support if this keeps happening.';
return [
'type' => 'error',
'message' => "Sorry, I couldn't fetch data for invoice {$invoiceNumber}. "
. 'Please try again or contact support if this keeps happening.',
'invoice_number' => $invoiceNumber,
'total' => 0,
'scanned' => 0,
'not_scanned' => 0,
'unscanned_serials' => [],
];
}
if (empty($rows)) {
return "No records found for invoice number {$invoiceNumber}. "
. 'Please double-check the invoice number and try again.';
return [
'type' => 'not_found',
'message' => "No records found for invoice number {$invoiceNumber}. "
. 'Please double-check the invoice number and try again.',
'invoice_number' => $invoiceNumber,
'total' => 0,
'scanned' => 0,
'not_scanned' => 0,
'unscanned_serials' => [],
];
}
return $this->formatInvoiceResponse($invoiceNumber, $rows);
}
/**
* Turn the raw DB rows into a plain-English summary.
*/
private function formatInvoiceResponse(string $invoiceNumber, array $rows): string
{
// ── Aggregate rows ────────────────────────────────────────────────────
$totalScanned = 0;
$totalNotScanned = 0;
$unscannedList = null;
$unscannedSerials = [];
foreach ($rows as $row) {
if ($row->status === 'not scanned') {
$totalNotScanned = (int) $row->total_count;
$unscannedList = $row->serial_numbers_not_scanned;
if (! empty($row->serial_numbers_not_scanned)) {
$unscannedSerials = array_values(
array_filter(
array_map('trim', explode(',', $row->serial_numbers_not_scanned))
)
);
}
} else {
$totalScanned += (int) $row->total_count;
}
}
$grandTotal = $totalScanned + $totalNotScanned;
$itemWord = $grandTotal === 1 ? 'serial number' : 'serial numbers';
// ── All scanned ───────────────────────────────────────────────────────
if ($totalNotScanned === 0) {
return "For invoice number {$invoiceNumber}, all {$grandTotal} {$itemWord} "
. ($grandTotal === 1 ? 'has' : 'have') . ' been scanned. ✅';
$n = $grandTotal;
$itemWord = $n === 1 ? 'serial number' : 'serial numbers';
return [
'type' => 'all_scanned',
'message' => "All {$n} {$itemWord} scanned for invoice {$invoiceNumber}. ✅",
'invoice_number' => $invoiceNumber,
'total' => $grandTotal,
'scanned' => $totalScanned,
'not_scanned' => 0,
'unscanned_serials' => [],
];
}
// ── None scanned ──────────────────────────────────────────────────────
// ── None / partial scanned ────────────────────────────────────────────
$type = $totalScanned === 0 ? 'none_scanned' : 'partial';
$itemWord = $grandTotal === 1 ? 'serial number' : 'serial numbers';
if ($totalScanned === 0) {
$msg = "For invoice number {$invoiceNumber}, there "
. ($grandTotal === 1 ? 'is' : 'are') . " {$grandTotal} {$itemWord} "
. 'and none have been scanned.';
if ($unscannedList) {
$msg .= " Unscanned serial numbers are: {$unscannedList}.";
}
return $msg;
$summary = "Invoice {$invoiceNumber}{$grandTotal} {$itemWord}, none scanned yet.";
} else {
$summary = "Invoice {$invoiceNumber}{$grandTotal} {$itemWord} total: "
. "{$totalScanned} scanned, {$totalNotScanned} not scanned.";
}
// ── Mixed ─────────────────────────────────────────────────────────────
$msg = "For invoice number {$invoiceNumber}, there "
. ($grandTotal === 1 ? 'is' : 'are') . " {$grandTotal} {$itemWord} in total. "
. "Out of which {$totalScanned} "
. ($totalScanned === 1 ? 'has' : 'have') . ' been scanned and '
. "{$totalNotScanned} "
. ($totalNotScanned === 1 ? 'has' : 'have') . ' not been scanned.';
if ($unscannedList) {
$msg .= " Unscanned serial numbers are: {$unscannedList}.";
}
return $msg;
return [
'type' => $type,
'message' => $summary,
'invoice_number' => $invoiceNumber,
'total' => $grandTotal,
'scanned' => $totalScanned,
'not_scanned' => $totalNotScanned,
'unscanned_serials' => $unscannedSerials,
];
}
// ─────────────────────────────────────────────────────────────────────────

View File

@@ -174,7 +174,7 @@
{{-- ── BASIC MODE ── --}}
{{-- ══════════════════════════════════════════════════════════════════ --}}
@if($mode === 'basic')
<div style="padding:1rem;display:flex;flex-direction:column;gap:0.875rem;">
<div style="padding:1rem;display:flex;flex-direction:column;gap:0.875rem;max-height:480px;overflow-y:auto;">
{{-- ── Report Type Selector (dropdown) ──────────────────────────── --}}
<div style="position:relative;">
@@ -431,25 +431,138 @@
{{-- Invoice Status Result --}}
@if($hasInvoiceStatusResult)
<div style="background:#111827;border:1px solid {{ str_contains($invoiceStatusResult, '✅') ? '#10b981' : (str_contains($invoiceStatusResult, 'No records') || str_contains($invoiceStatusResult, 'valid') ? '#f59e0b' : '#3b82f6') }};border-radius:0.5rem;padding:0.875rem;display:flex;gap:0.75rem;align-items:flex-start;animation:fadeIn .25s ease;">
@if(str_contains($invoiceStatusResult, '✅'))
{{-- All scanned green check --}}
<svg style="width:1.25rem;height:1.25rem;color:#10b981;flex-shrink:0;margin-top:0.1rem;" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75 11.25 15 15 9.75M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z" />
</svg>
@elseif(str_contains($invoiceStatusResult, 'No records') || str_contains($invoiceStatusResult, 'valid') || str_contains($invoiceStatusResult, "couldn't"))
{{-- Not found / error warning --}}
<svg style="width:1.25rem;height:1.25rem;color:#f59e0b;flex-shrink:0;margin-top:0.1rem;" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" d="M12 9v3.75m-9.303 3.376c-.866 1.5.217 3.374 1.948 3.374h14.71c1.73 0 2.813-1.874 1.948-3.374L13.949 3.378c-.866-1.5-3.032-1.5-3.898 0L2.697 16.126ZM12 15.75h.007v.008H12v-.008Z" />
</svg>
@else
{{-- Partial / none scanned barcode --}}
<svg style="width:1.25rem;height:1.25rem;color:#3b82f6;flex-shrink:0;margin-top:0.1rem;" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" d="M3.75 4.875c0-.621.504-1.125 1.125-1.125h4.5c.621 0 1.125.504 1.125 1.125v4.5c0 .621-.504 1.125-1.125 1.125h-4.5A1.125 1.125 0 0 1 3.75 9.375v-4.5ZM3.75 14.625c0-.621.504-1.125 1.125-1.125h4.5c.621 0 1.125.504 1.125 1.125v4.5c0 .621-.504 1.125-1.125 1.125h-4.5a1.125 1.125 0 0 1-1.125-1.125v-4.5ZM13.5 4.875c0-.621.504-1.125 1.125-1.125h4.5c.621 0 1.125.504 1.125 1.125v4.5c0 .621-.504 1.125-1.125 1.125h-4.5A1.125 1.125 0 0 1 13.5 9.375v-4.5Z" />
<path stroke-linecap="round" stroke-linejoin="round" d="M6.75 6.75h.75v.75h-.75v-.75ZM6.75 16.5h.75v.75h-.75v-.75ZM16.5 6.75h.75v.75h-.75v-.75ZM13.5 13.5h.75v.75h-.75v-.75ZM13.5 19.5h.75v.75h-.75v-.75ZM19.5 13.5h.75v.75h-.75v-.75ZM19.5 19.5h.75v.75h-.75v-.75ZM16.5 16.5h.75v.75h-.75v-.75Z" />
</svg>
@php
$sd = $invoiceStatusData;
$sdType = $sd['type'] ?? 'error';
$sdTotal = $sd['total'] ?? 0;
$sdScanned = $sd['scanned'] ?? 0;
$sdNot = $sd['not_scanned'] ?? 0;
$sdSerials = $sd['unscanned_serials'] ?? [];
$sdCount = count($sdSerials);
$sdInv = $sd['invoice_number'] ?? '';
$isGood = $sdType === 'all_scanned';
$isWarn = in_array($sdType, ['error', 'not_found', 'invalid']);
$borderCol = $isGood ? '#10b981' : ($isWarn ? '#f59e0b' : '#3b82f6');
@endphp
<div style="background:#111827;border:1px solid {{ $borderCol }};border-radius:0.5rem;padding:0.875rem;animation:fadeIn .25s ease;">
{{-- ── Icon + summary row ── --}}
<div style="display:flex;gap:0.75rem;align-items:flex-start;">
@if($isGood)
<svg style="width:1.25rem;height:1.25rem;color:#10b981;flex-shrink:0;margin-top:0.1rem;" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75 11.25 15 15 9.75M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z" />
</svg>
@elseif($isWarn)
<svg style="width:1.25rem;height:1.25rem;color:#f59e0b;flex-shrink:0;margin-top:0.1rem;" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" d="M12 9v3.75m-9.303 3.376c-.866 1.5.217 3.374 1.948 3.374h14.71c1.73 0 2.813-1.874 1.948-3.374L13.949 3.378c-.866-1.5-3.032-1.5-3.898 0L2.697 16.126ZM12 15.75h.007v.008H12v-.008Z" />
</svg>
@else
<svg style="width:1.25rem;height:1.25rem;color:#3b82f6;flex-shrink:0;margin-top:0.1rem;" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" d="M3.75 4.875c0-.621.504-1.125 1.125-1.125h4.5c.621 0 1.125.504 1.125 1.125v4.5c0 .621-.504 1.125-1.125 1.125h-4.5A1.125 1.125 0 0 1 3.75 9.375v-4.5ZM3.75 14.625c0-.621.504-1.125 1.125-1.125h4.5c.621 0 1.125.504 1.125 1.125v4.5c0 .621-.504 1.125-1.125 1.125h-4.5a1.125 1.125 0 0 1-1.125-1.125v-4.5ZM13.5 4.875c0-.621.504-1.125 1.125-1.125h4.5c.621 0 1.125.504 1.125 1.125v4.5c0 .621-.504 1.125-1.125 1.125h-4.5A1.125 1.125 0 0 1 13.5 9.375v-4.5Z" />
<path stroke-linecap="round" stroke-linejoin="round" d="M6.75 6.75h.75v.75h-.75v-.75ZM6.75 16.5h.75v.75h-.75v-.75ZM16.5 6.75h.75v.75h-.75v-.75ZM13.5 13.5h.75v.75h-.75v-.75ZM13.5 19.5h.75v.75h-.75v-.75ZM19.5 13.5h.75v.75h-.75v-.75ZM19.5 19.5h.75v.75h-.75v-.75ZM16.5 16.5h.75v.75h-.75v-.75Z" />
</svg>
@endif
<div style="flex:1;min-width:0;">
<p style="font-size:0.8125rem;color:#f9fafb;line-height:1.6;margin:0;">
{{ $sd['message'] ?? $invoiceStatusResult }}
</p>
{{-- ── Count pills (only for real invoice data) ── --}}
@if($sdTotal > 0)
<div style="display:flex;gap:0.375rem;flex-wrap:wrap;margin-top:0.625rem;">
<span style="background:#1e3a2f;border:1px solid #10b98155;color:#6ee7b7;font-size:0.68rem;font-weight:700;padding:0.2rem 0.55rem;border-radius:9999px;">
Total: {{ $sdTotal }}
</span>
<span style="background:#1a3350;border:1px solid #3b82f655;color:#93c5fd;font-size:0.68rem;font-weight:700;padding:0.2rem 0.55rem;border-radius:9999px;">
Scanned: {{ $sdScanned }}
</span>
@if($sdNot > 0)
<span style="background:#3b1a1a;border:1px solid #ef444455;color:#fca5a5;font-size:0.68rem;font-weight:700;padding:0.2rem 0.55rem;border-radius:9999px;">
Not scanned: {{ $sdNot }}
</span>
@endif
</div>
@endif
</div>
</div>
{{-- ── Unscanned serial numbers section ── --}}
@if($sdCount > 0)
<div style="margin-top:0.875rem;border-top:1px solid #374151;padding-top:0.75rem;">
<p style="font-size:0.72rem;font-weight:600;color:#9ca3af;margin:0 0 0.5rem;text-transform:uppercase;letter-spacing:.05em;">
Unscanned serial numbers
<span style="background:#3b1a1a;color:#fca5a5;border-radius:9999px;padding:0.1rem 0.45rem;font-size:0.68rem;margin-left:0.25rem;">
{{ $sdCount }}
</span>
</p>
{{-- Serial chips first 10, or all when expanded --}}
@php
$visibleSerials = $showAllUnscanned
? $sdSerials
: array_slice($sdSerials, 0, 10);
$hiddenCount = $sdCount - 10;
@endphp
<div style="display:flex;flex-wrap:wrap;gap:0.35rem;
@if($showAllUnscanned) max-height:200px;overflow-y:auto;padding-right:2px; @endif">
@foreach($visibleSerials as $serial)
<span style="
display:inline-block;
background:#1e293b;
border:1px solid #475569;
color:#e2e8f0;
font-family:monospace;
font-size:0.72rem;
padding:0.2rem 0.5rem;
border-radius:0.3rem;
white-space:nowrap;
">{{ $serial }}</span>
@endforeach
</div>
{{-- Show more / Show less button --}}
@if($sdCount > 10)
<button
wire:click="toggleShowAllUnscanned"
style="
margin-top:0.625rem;
background:transparent;
border:1px solid #3b82f655;
border-radius:0.375rem;
color:#60a5fa;
font-size:0.75rem;
font-weight:600;
padding:0.3rem 0.75rem;
cursor:pointer;
display:flex;
align-items:center;
gap:0.35rem;
transition:background .15s, border-color .15s;
"
onmouseover="this.style.background='#1e3a5f';this.style.borderColor='#3b82f6';"
onmouseout="this.style.background='transparent';this.style.borderColor='#3b82f655';">
@if($showAllUnscanned)
<svg style="width:0.75rem;height:0.75rem;" fill="none" viewBox="0 0 24 24" stroke-width="2.5" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" d="m4.5 15.75 7.5-7.5 7.5 7.5" />
</svg>
Show less
@else
<svg style="width:0.75rem;height:0.75rem;" fill="none" viewBox="0 0 24 24" stroke-width="2.5" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" />
</svg>
Show {{ $hiddenCount }} more…
@endif
</button>
@endif
</div>
@endif
<p style="font-size:0.8125rem;color:#f9fafb;line-height:1.6;margin:0;white-space:pre-line;">{{ $invoiceStatusResult }}</p>
</div>
@endif
@@ -619,4 +732,3 @@
@endif
</button>
</div>