modified logic in chat bot
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Has been cancelled
Gemini PR Review / Gemini PR Review (pull_request) Has been cancelled
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Has been cancelled
Laravel Larastan / larastan (pull_request) Has been cancelled
Laravel Pint / pint (pull_request) Has been cancelled
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Has been cancelled
Gemini PR Review / Gemini PR Review (pull_request) Has been cancelled
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Has been cancelled
Laravel Larastan / larastan (pull_request) Has been cancelled
Laravel Pint / pint (pull_request) Has been cancelled
This commit is contained in:
@@ -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,
|
||||
];
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────
|
||||
|
||||
Reference in New Issue
Block a user