diff --git a/btc-UI/src/app/components/middle-section/middle-section.component.ts b/btc-UI/src/app/components/middle-section/middle-section.component.ts index 5ba0bf6..3f6831c 100755 --- a/btc-UI/src/app/components/middle-section/middle-section.component.ts +++ b/btc-UI/src/app/components/middle-section/middle-section.component.ts @@ -147,8 +147,6 @@ export class MiddleSectionComponent implements OnInit, OnDestroy { } } - - parseWinLabelsToRows(winLabels: string, fallbackTotal?: number): SelectionData[] { return (winLabels.split('\n') as string[]) .map(line => { @@ -188,11 +186,9 @@ parseWinLabelsToRows(winLabels: string, fallbackTotal?: number): SelectionData[] } - //-------------------------------------------------REPEAT PRINT LOGIC ----------------------------------------------- - - -printRepeat() { - console.log('🖨️ [DEBUG] Printing ticket...'); + //-----------------------REPEAT PRINT LOGIC ---------------------------------- + printRepeat() { + console.log('🖨️ [DEBUG] Printing ticket...'); const userName = localStorage.getItem('userName') || 'Unknown'; @@ -207,15 +203,15 @@ printRepeat() { const millis = String(now.getMilliseconds()).padStart(3, '0'); const barcodeId = `1111${day}${month}${year}${timeStr}${millis}`; - const printableRows = this.filledRows - .filter(row => row.label && row.numbers.length > 0 && row.total > 0) - .map(row => { - const horses = Array.isArray(row.numbers) ? row.numbers.join(',') : row.numbers; - return `${row.label.padEnd(6)} ${horses.padEnd(15)} * ${String(row.value).padEnd(3)} ₹${row.total}`; - }) - .join('\n'); + const printableRows = this.filledRows + .filter(row => row.label && row.numbers.length > 0 && row.total > 0) + .map(row => { + const horses = Array.isArray(row.numbers) ? row.numbers.join(',') : row.numbers; + return `${row.label.padEnd(6)} ${horses.padEnd(15)} * ${String(row.value).padEnd(3)} ₹${row.total}`; + }) + .join('\n'); - const ticketContent = ` + const ticketContent = ` BTC Race Ticket ${printableRows} @@ -223,31 +219,59 @@ Barcode ID : ${barcodeId} Printed by : ${userName} Date : ${now.toLocaleString()}`; - console.log('%c\n' + ticketContent, 'font-family: monospace; color: green'); + console.log('%c\n' + ticketContent, 'font-family: monospace; color: green'); - const payload = { - type: 'repeat', + // ✅ Send to print server + const payload = { + type: 'repeat', printedBy: userName, barcodeId, // send barcode separately - content: ticketContent, + content: ticketContent, timestamp: now.toLocaleString() - }; + }; - fetch('http://localhost:9100/print', { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify(payload) - }) - .then(response => { - if (!response.ok) throw new Error(`Printer error: ${response.status}`); - return response.text(); - }) - .then(result => { - console.log("✅ Repeat ticket print successful:", result); - }) - .catch(error => { - console.error("❌ Repeat ticket print failed:", error); - }); + fetch('http://localhost:9100/print', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(payload) + }) + .then(response => { + if (!response.ok) throw new Error(`Printer error: ${response.status}`); + return response.text(); + }) + .then(result => { + console.log("✅ Repeat ticket print successful:", result); + }) + .catch(error => { + console.error("❌ Repeat ticket print failed:", error); + }); + + // --- Update localStorage for transaction summary like normal print --- + // 1. Increment printClickCount + let clickCount = Number(localStorage.getItem('printClickCount') || '0'); + clickCount += 1; + localStorage.setItem('printClickCount', clickCount.toString()); + + // 2. Add a new ticket entry to localTickets (type: 'ticket', totalAmount) + try { + const existingTicketsStr = localStorage.getItem('localTickets'); + const existingTickets = existingTicketsStr ? JSON.parse(existingTicketsStr) : []; + // Calculate totalAmount for this repeat print + const totalAmount = this.filledRows + .filter(row => row.label && row.numbers.length > 0 && row.total > 0) + .reduce((sum, row) => sum + (row.total || 0), 0); + const ticketEntry = { + type: 'ticket', + printedBy: userName, + totalAmount, + content: ticketContent, + + }; + existingTickets.push(ticketEntry); + localStorage.setItem('localTickets', JSON.stringify(existingTickets)); + } catch (error) { + console.error('❌ Failed to update localTickets for repeat print:', error); + } // Hide Confirm and Print buttons before clearing selections this.showConfirmButton = false; @@ -261,8 +285,6 @@ Date : ${now.toLocaleString()}`; //------------------------------PRINT REPEAT ENDED HERE-----------------------------------------------------S - - erase() { this.selectionService.clearSelections(); this.resetSelections(); diff --git a/btc-UI/src/app/components/touch-pad-menu/touch-pad-menu.component.ts b/btc-UI/src/app/components/touch-pad-menu/touch-pad-menu.component.ts index 59807d5..4a4d7c3 100755 --- a/btc-UI/src/app/components/touch-pad-menu/touch-pad-menu.component.ts +++ b/btc-UI/src/app/components/touch-pad-menu/touch-pad-menu.component.ts @@ -586,43 +586,59 @@ if (this.twoGroupLabels.includes(this.selectedLabel || '')) { } onPadEnter() { - // Disable Enter if maxRowsReached - if (this.maxRowsReached) return; + if (this.maxRowsReached) return; - if (!this.canPrint) { - this.print(); - } - - const value = parseFloat(this.padValue) || 0; - - if (this.selectedLabel === 'WSP') { - const labels = ['WIN', 'SHP', 'PLC']; - const targetLabel = labels[this.wspTicketStage]; - - const updatedSelections = this.selectionService.getSelections().map(sel => { - if (sel.label === targetLabel && JSON.stringify(sel.numbers) === JSON.stringify(this.selectedNumbers)) { - return { - ...sel, - value, - total: value * (sel.numbers?.length || 0) * 10 - }; - } - return sel; - }); - - this.selectionService.setSelections(updatedSelections); - - // Move to next stage - this.wspTicketStage = (this.wspTicketStage + 1) % 3; - this.padValue = ''; - this.updateCanPrint(); - return; - } - - // ✅ Default path: finalize row and reset input + if (!this.canPrint) { this.print(); } + const value = parseFloat(this.padValue) || 0; + + if (this.selectedLabel === 'WSP') { + const labels = ['WIN', 'SHP', 'PLC']; + const targetLabel = labels[this.wspTicketStage]; + const selections = this.selectionService.getSelections(); + + // Find the current WSP row to ensure numbers are synchronized + const currentWSPRow = selections.find(sel => sel.label === targetLabel); + if (currentWSPRow) { + this.selectedNumbers = [...currentWSPRow.numbers]; // Synchronize selectedNumbers + } + + const updatedSelections = selections.map(sel => { + if (sel.label === targetLabel) { + const total = value * (sel.numbers?.length || 0) * 10; + return { + ...sel, + value, + total + }; + } + return sel; + }); + + this.selectionService.setSelections(updatedSelections); + + // Only increment stage if not at the last stage (PLC) + if (this.wspTicketStage < 2) { + this.wspTicketStage++; + // Update selectedNumbers for the next stage + const nextLabel = labels[this.wspTicketStage]; + const nextWSPRow = updatedSelections.find(sel => sel.label === nextLabel); + if (nextWSPRow) { + this.selectedNumbers = [...nextWSPRow.numbers]; + } + } + + this.padValue = ''; + this.updateCanPrint(); + return; + } + + this.print(); +} + + onShashEnter() { if (this.selectedLabel === 'TAN' && this.isBoxed) { return; @@ -841,6 +857,24 @@ enterPadVal(key: string) { // At least one valid row in finalized selections or current row const selections = this.selectionService.getSelections(); const currentRow = this.selectionService.getCurrentRow(); + if (this.selectedLabel === 'WSP') { + // For WSP, require all three rows (WIN, SHP, PLC) to have valid numbers and values >= 1 + const wspLabels = ['WIN', 'SHP', 'PLC']; + const wspSelections = selections.filter(sel => wspLabels.includes(sel.label)); + + // Check if we are at the last stage (PLC) and all rows are valid + const allWSPRowsValid = wspSelections.length === 3 && wspSelections.every(row => + row.label && + row.numbers && + row.numbers.length > 0 && + row.value >= 1 && + row.total > 0 + ); + + return this.wspTicketStage === 2 && allWSPRowsValid; + } + + // For non-WSP, keep existing logic: any valid row enables printing const hasValidRow = selections.some( row => !!row.label && !!row.numbers && row.numbers.length > 0 && row.value > 0 && row.total > 0 ) || (