New routeNes

parent d0d28ab3
...@@ -92,19 +92,19 @@ def validate_barcode_data(data: str, standard: str) -> bool: ...@@ -92,19 +92,19 @@ def validate_barcode_data(data: str, standard: str) -> bool:
def generate_barcode_image(data: str, standard: str, width: int = 300, height: int = 100) -> Optional[bytes]: def generate_barcode_image(data: str, standard: str, width: int = 300, height: int = 100) -> Optional[bytes]:
""" """
Generate barcode image as PNG bytes Generate barcode image as PNG bytes
Args: Args:
data: Data to encode in barcode data: Data to encode in barcode
standard: Barcode standard (code128, code39, ean13, etc.) standard: Barcode standard (code128, code39, ean13, etc.)
width: Image width in pixels width: Image width in pixels
height: Image height in pixels height: Image height in pixels
Returns: Returns:
PNG image bytes or None if generation failed PNG image bytes or None if generation failed
""" """
if standard == 'none': if standard == 'none':
return None return None
try: try:
# Special case for UPC-E which is not supported by python-barcode library # Special case for UPC-E which is not supported by python-barcode library
if standard == 'upce': if standard == 'upce':
...@@ -146,7 +146,7 @@ def generate_barcode_image(data: str, standard: str, width: int = 300, height: i ...@@ -146,7 +146,7 @@ def generate_barcode_image(data: str, standard: str, width: int = 300, height: i
except Exception as e: except Exception as e:
logger.error(f"Failed to get barcode class for {barcode_type}: {e}") logger.error(f"Failed to get barcode class for {barcode_type}: {e}")
return None return None
# Create image writer with custom options # Create image writer with custom options
writer = ImageWriter() writer = ImageWriter()
writer.set_options({ writer.set_options({
...@@ -159,21 +159,24 @@ def generate_barcode_image(data: str, standard: str, width: int = 300, height: i ...@@ -159,21 +159,24 @@ def generate_barcode_image(data: str, standard: str, width: int = 300, height: i
'write_text': True, 'write_text': True,
'text': data 'text': data
}) })
# Generate barcode # Create barcode
barcode_instance = barcode_class(data, writer=writer) barcode_instance = barcode_class(data, writer=writer)
# Render to bytes # Get PIL Image using render() method
pil_image = barcode_instance.render()
# Save to bytes
buffer = BytesIO() buffer = BytesIO()
barcode_instance.write(buffer) pil_image.save(buffer, format='PNG')
buffer.seek(0) buffer.seek(0)
image_bytes = buffer.getvalue() image_bytes = buffer.getvalue()
buffer.close() buffer.close()
logger.debug(f"Generated {standard} barcode for data: {data}") logger.debug(f"Generated {standard} barcode for data: {data}")
return image_bytes return image_bytes
except Exception as e: except Exception as e:
logger.error(f"Failed to generate barcode: {e}") logger.error(f"Failed to generate barcode: {e}")
return None return None
...@@ -216,16 +219,12 @@ def calculate_ean13_check_digit(data: str) -> str: ...@@ -216,16 +219,12 @@ def calculate_ean13_check_digit(data: str) -> str:
raise ValueError("EAN-13 check digit calculation requires exactly 12 digits") raise ValueError("EAN-13 check digit calculation requires exactly 12 digits")
# EAN-13 check digit calculation # EAN-13 check digit calculation
# Step 1: Sum digits in odd positions (1st, 3rd, 5th, etc.) multiplied by 3 # Weights alternate starting with 1 for leftmost digit, 3 for next, etc.
odd_sum = sum(int(data[i]) for i in range(0, 12, 2)) * 3 # This ensures the rightmost data digit gets weight 3
weights = [1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3]
# Step 2: Sum digits in even positions (2nd, 4th, 6th, etc.) total = sum(int(data[i]) * weights[i] for i in range(12))
even_sum = sum(int(data[i]) for i in range(1, 12, 2))
# Step 3: Total sum
total = odd_sum + even_sum
# Step 4: Find the smallest number that makes total divisible by 10 # Find the smallest number that makes total divisible by 10
check_digit = (10 - (total % 10)) % 10 check_digit = (10 - (total % 10)) % 10
return data + str(check_digit) return data + str(check_digit)
......
...@@ -5484,16 +5484,11 @@ def generate_bet_barcode(bet_id): ...@@ -5484,16 +5484,11 @@ def generate_bet_barcode(bet_id):
logger.debug(f"Using generated barcode data for bet {bet_uuid}: {barcode_data}") logger.debug(f"Using generated barcode data for bet {bet_uuid}: {barcode_data}")
# Generate barcode image # Generate barcode image
barcode_image = generate_barcode_image(barcode_data, standard, width, height) barcode_image_bytes = generate_barcode_image(barcode_data, standard, width, height)
if barcode_image:
# Convert PIL image to bytes
img_buffer = io.BytesIO()
barcode_image.save(img_buffer, format='PNG')
img_buffer.seek(0)
if barcode_image_bytes:
return Response( return Response(
img_buffer.getvalue(), barcode_image_bytes,
mimetype='image/png', mimetype='image/png',
headers={'Cache-Control': 'public, max-age=3600'} # Cache for 1 hour headers={'Cache-Control': 'public, max-age=3600'} # Cache for 1 hour
) )
......
...@@ -575,25 +575,33 @@ function generateThermalReceipt(betId) { ...@@ -575,25 +575,33 @@ function generateThermalReceipt(betId) {
const receiptContainer = document.getElementById('thermal-receipt'); const receiptContainer = document.getElementById('thermal-receipt');
const receiptHtml = generateReceiptHtml(window.betData); const receiptHtml = generateReceiptHtml(window.betData);
receiptContainer.innerHTML = receiptHtml; receiptContainer.innerHTML = receiptHtml;
// Show the modal // Generate verification codes for preview
const modal = new bootstrap.Modal(document.getElementById('printReceiptModal')); generateVerificationCodes(window.betData.uuid).then(() => {
modal.show(); // Show the modal after verification codes are loaded
const modal = new bootstrap.Modal(document.getElementById('printReceiptModal'));
modal.show();
}).catch(error => {
console.warn('Error generating verification codes for preview, showing modal anyway:', error);
// Show modal even if verification codes fail
const modal = new bootstrap.Modal(document.getElementById('printReceiptModal'));
modal.show();
});
} }
function generateReceiptHtml(betData) { function generateReceiptHtml(betData) {
const currentDateTime = new Date().toLocaleString(); const currentDateTime = new Date().toLocaleString();
let receiptHtml = ` let receiptHtml = `
<div class="thermal-receipt-content"> <div class="thermal-receipt-content">
<!-- Header --> <!-- Header -->
<div class="receipt-header"> <div class="receipt-header">
<div class="receipt-title">BETTING SLIP</div> <div class="receipt-title">BETTING SLIP</div>
</div> </div>
<!-- Separator --> <!-- Separator -->
<div class="receipt-separator">================================</div> <div class="receipt-separator">================================</div>
<!-- Bet Information --> <!-- Bet Information -->
<div class="receipt-info"> <div class="receipt-info">
<div class="receipt-row"> <div class="receipt-row">
...@@ -613,14 +621,14 @@ function generateReceiptHtml(betData) { ...@@ -613,14 +621,14 @@ function generateReceiptHtml(betData) {
<span>${betData.bet_count}</span> <span>${betData.bet_count}</span>
</div> </div>
</div> </div>
<!-- Separator --> <!-- Separator -->
<div class="receipt-separator">================================</div> <div class="receipt-separator">================================</div>
<!-- Bet Details --> <!-- Bet Details -->
<div class="receipt-bets"> <div class="receipt-bets">
`; `;
let totalAmount = 0; let totalAmount = 0;
betData.bet_details.forEach((detail, index) => { betData.bet_details.forEach((detail, index) => {
totalAmount += parseFloat(detail.amount); totalAmount += parseFloat(detail.amount);
...@@ -648,18 +656,18 @@ function generateReceiptHtml(betData) { ...@@ -648,18 +656,18 @@ function generateReceiptHtml(betData) {
</div> </div>
</div> </div>
`; `;
if (index < betData.bet_details.length - 1) { if (index < betData.bet_details.length - 1) {
receiptHtml += `<div class="receipt-separator">- - - - - - - - - - - - - - - - -</div>`; receiptHtml += `<div class="receipt-separator">- - - - - - - - - - - - - - - - -</div>`;
} }
}); });
receiptHtml += ` receiptHtml += `
</div> </div>
<!-- Separator --> <!-- Separator -->
<div class="receipt-separator">================================</div> <div class="receipt-separator">================================</div>
<!-- Total --> <!-- Total -->
<div class="receipt-total"> <div class="receipt-total">
<div class="receipt-total-line"> <div class="receipt-total-line">
...@@ -667,15 +675,15 @@ function generateReceiptHtml(betData) { ...@@ -667,15 +675,15 @@ function generateReceiptHtml(betData) {
<span>${formatCurrency(totalAmount)}</span> <span>${formatCurrency(totalAmount)}</span>
</div> </div>
</div> </div>
<!-- Separator --> <!-- Separator -->
<div class="receipt-separator">================================</div> <div class="receipt-separator">================================</div>
<!-- QR Code and Barcode (conditional based on settings) --> <!-- QR Code and Barcode (conditional based on settings) -->
<div class="receipt-verification" id="receipt-verification-${betData.uuid}"> <div class="receipt-verification" id="receipt-verification-${betData.uuid}">
<!-- QR Code and Barcode will be conditionally added here --> <!-- QR Code and Barcode will be conditionally added here -->
</div> </div>
<!-- Footer --> <!-- Footer -->
<div class="receipt-footer"> <div class="receipt-footer">
<div>Thank you for betting with MBetter!</div> <div>Thank you for betting with MBetter!</div>
...@@ -684,12 +692,7 @@ function generateReceiptHtml(betData) { ...@@ -684,12 +692,7 @@ function generateReceiptHtml(betData) {
</div> </div>
</div> </div>
`; `;
// Generate QR code and barcode after inserting HTML (conditional)
setTimeout(() => {
generateVerificationCodes(betData.uuid);
}, 100);
return receiptHtml; return receiptHtml;
} }
...@@ -704,88 +707,107 @@ function generateQRCode(betUuid) { ...@@ -704,88 +707,107 @@ function generateQRCode(betUuid) {
} }
function generateVerificationCodes(betUuid) { function generateVerificationCodes(betUuid) {
const verificationContainer = document.getElementById(`receipt-verification-${betUuid}`); return new Promise((resolve) => {
if (!verificationContainer) { const verificationContainer = document.getElementById(`receipt-verification-${betUuid}`);
return; if (!verificationContainer) {
} resolve();
return;
}
// Check QR code settings - QR codes should NOT print if disabled let completedOperations = 0;
// Default to NOT showing QR codes if API fails const totalOperations = 2; // QR code check + barcode check
let shouldShowQR = false;
function checkComplete() {
try { completedOperations++;
// Try to get QR settings from API if (completedOperations >= totalOperations) {
fetch('/api/qrcode-settings') resolve();
.then(response => { }
if (!response.ok) { }
console.warn('QR settings API failed with status:', response.status);
return null; // Check QR code settings - QR codes should NOT print if disabled
} // Default to NOT showing QR codes if API fails
return response.json(); let shouldShowQR = false;
})
.then(qrSettings => { try {
if (qrSettings && qrSettings.success && qrSettings.settings) { // Try to get QR settings from API
shouldShowQR = qrSettings.settings.enabled === true && qrSettings.settings.show_on_thermal === true; fetch('/api/qrcode-settings')
console.log('QR settings check result:', shouldShowQR, qrSettings.settings); .then(response => {
} else { if (!response.ok) {
console.warn('Invalid QR settings response:', qrSettings); console.warn('QR settings API failed with status:', response.status);
shouldShowQR = false; // Default to not showing checkComplete();
} return null;
}
return response.json();
})
.then(qrSettings => {
if (qrSettings && qrSettings.success && qrSettings.settings) {
shouldShowQR = qrSettings.settings.enabled === true && qrSettings.settings.show_on_thermal === true;
console.log('QR settings check result:', shouldShowQR, qrSettings.settings);
} else {
console.warn('Invalid QR settings response:', qrSettings);
shouldShowQR = false; // Default to not showing
}
// Add QR code if enabled // Add QR code if enabled
if (shouldShowQR) { if (shouldShowQR) {
console.log('Adding QR code to receipt'); console.log('Adding QR code to receipt');
const qrHtml = ` const qrHtml = `
<div class="receipt-qr"> <div class="receipt-qr">
<div class="qr-code" id="qr-code-${betUuid}"></div> <div class="qr-code" id="qr-code-${betUuid}"></div>
<div class="qr-text">Scan QR for verification</div> <div class="qr-text">Scan QR for verification</div>
</div>
`;
verificationContainer.insertAdjacentHTML('beforeend', qrHtml);
// Generate QR code
const qrContainer = document.getElementById(`qr-code-${betUuid}`);
if (qrContainer) {
const qrImageUrl = `https://api.qrserver.com/v1/create-qr-code/?size=100x100&data=${encodeURIComponent(betUuid)}&format=png`;
qrContainer.innerHTML = `<img src="${qrImageUrl}" alt="QR Code" class="qr-image">`;
}
} else {
console.log('QR code disabled - not adding to receipt');
}
checkComplete();
})
.catch(error => {
console.warn('Failed to check QR code settings, defaulting to disabled:', error);
shouldShowQR = false; // Default to not showing on error
checkComplete();
});
} catch (error) {
console.warn('Error in QR code settings check, defaulting to disabled:', error);
shouldShowQR = false; // Default to not showing on error
checkComplete();
}
// Check barcode settings
fetch(`/api/barcode-data/${betUuid}`)
.then(response => response.json())
.then(data => {
if (data.success && data.enabled && data.barcode_data && data.barcode_data.show_on_thermal && data.barcode_data.image_base64) {
// Add barcode to receipt
const barcodeHtml = `
<div class="receipt-barcode" id="barcode-container-${betUuid}">
<div class="barcode-image" id="barcode-${betUuid}"></div>
<div class="barcode-text">Scan barcode for verification</div>
</div> </div>
`; `;
verificationContainer.insertAdjacentHTML('beforeend', qrHtml); verificationContainer.insertAdjacentHTML('beforeend', barcodeHtml);
// Generate QR code // Display barcode
const qrContainer = document.getElementById(`qr-code-${betUuid}`); const barcodeElement = document.getElementById(`barcode-${betUuid}`);
if (qrContainer) { if (barcodeElement) {
const qrImageUrl = `https://api.qrserver.com/v1/create-qr-code/?size=100x100&data=${encodeURIComponent(betUuid)}&format=png`; barcodeElement.innerHTML = `<img src="data:image/png;base64,${data.barcode_data.image_base64}" alt="Barcode" class="barcode-img" style="width: ${data.barcode_data.width}px; height: ${data.barcode_data.height}px;">`;
qrContainer.innerHTML = `<img src="${qrImageUrl}" alt="QR Code" class="qr-image">`;
} }
} else {
console.log('QR code disabled - not adding to receipt');
} }
checkComplete();
}) })
.catch(error => { .catch(error => {
console.warn('Failed to check QR code settings, defaulting to disabled:', error); console.warn('Failed to check barcode settings:', error);
shouldShowQR = false; // Default to not showing on error checkComplete();
}); });
} catch (error) { });
console.warn('Error in QR code settings check, defaulting to disabled:', error);
shouldShowQR = false; // Default to not showing on error
}
// Check barcode settings
fetch(`/api/barcode-data/${betUuid}`)
.then(response => response.json())
.then(data => {
if (data.success && data.enabled && data.barcode_data && data.barcode_data.show_on_thermal && data.barcode_data.image_base64) {
// Add barcode to receipt
const barcodeHtml = `
<div class="receipt-barcode" id="barcode-container-${betUuid}">
<div class="barcode-image" id="barcode-${betUuid}"></div>
<div class="barcode-text">Scan barcode for verification</div>
</div>
`;
verificationContainer.insertAdjacentHTML('beforeend', barcodeHtml);
// Display barcode
const barcodeElement = document.getElementById(`barcode-${betUuid}`);
if (barcodeElement) {
barcodeElement.innerHTML = `<img src="data:image/png;base64,${data.barcode_data.image_base64}" alt="Barcode" class="barcode-img" style="width: ${data.barcode_data.width}px; height: ${data.barcode_data.height}px;">`;
}
}
})
.catch(error => {
console.warn('Failed to check barcode settings:', error);
});
} }
function printThermalReceipt() { function printThermalReceipt() {
...@@ -1135,7 +1157,25 @@ function directPrintBet(betId) { ...@@ -1135,7 +1157,25 @@ function directPrintBet(betId) {
markBetAsPaidForPrinting(betId).then(() => { markBetAsPaidForPrinting(betId).then(() => {
// Use the global bet data for direct printing // Use the global bet data for direct printing
const receiptHtml = generateReceiptHtml(window.betData); const receiptHtml = generateReceiptHtml(window.betData);
printDirectly(receiptHtml);
// Create temporary container for receipt content
const tempContainer = document.createElement('div');
tempContainer.innerHTML = receiptHtml;
tempContainer.style.display = 'none';
document.body.appendChild(tempContainer);
// Generate verification codes and wait for completion
generateVerificationCodes(window.betData.uuid).then(() => {
// Get the updated HTML content after verification codes are added
const updatedReceiptHtml = tempContainer.innerHTML;
printDirectly(updatedReceiptHtml);
document.body.removeChild(tempContainer);
}).catch(error => {
console.warn('Error generating verification codes, printing without them:', error);
// Print anyway even if verification codes fail
printDirectly(receiptHtml);
document.body.removeChild(tempContainer);
});
}).catch(error => { }).catch(error => {
console.error('Failed to mark bet as paid:', error); console.error('Failed to mark bet as paid:', error);
// Still print even if marking as paid fails // Still print even if marking as paid fails
......
...@@ -635,10 +635,18 @@ function generateThermalReceiptFromList(betId) { ...@@ -635,10 +635,18 @@ function generateThermalReceiptFromList(betId) {
const receiptContainer = document.getElementById('thermal-receipt'); const receiptContainer = document.getElementById('thermal-receipt');
const receiptHtml = generateReceiptHtml(betData); const receiptHtml = generateReceiptHtml(betData);
receiptContainer.innerHTML = receiptHtml; receiptContainer.innerHTML = receiptHtml;
// Show the modal // Generate verification codes for preview
const modal = new bootstrap.Modal(document.getElementById('printReceiptModal')); generateVerificationCodes(betData.uuid).then(() => {
modal.show(); // Show the modal after verification codes are loaded
const modal = new bootstrap.Modal(document.getElementById('printReceiptModal'));
modal.show();
}).catch(error => {
console.warn('Error generating verification codes for preview, showing modal anyway:', error);
// Show modal even if verification codes fail
const modal = new bootstrap.Modal(document.getElementById('printReceiptModal'));
modal.show();
});
} else { } else {
showNotification('Failed to load bet details for printing: ' + (data.error || 'Unknown error'), 'error'); showNotification('Failed to load bet details for printing: ' + (data.error || 'Unknown error'), 'error');
} }
...@@ -670,17 +678,17 @@ function transformBetDataForReceipt(betData) { ...@@ -670,17 +678,17 @@ function transformBetDataForReceipt(betData) {
function generateReceiptHtml(betData) { function generateReceiptHtml(betData) {
const currentDateTime = new Date().toLocaleString(); const currentDateTime = new Date().toLocaleString();
let receiptHtml = ` let receiptHtml = `
<div class="thermal-receipt-content"> <div class="thermal-receipt-content">
<!-- Header --> <!-- Header -->
<div class="receipt-header"> <div class="receipt-header">
<div class="receipt-title">BETTING SLIP</div> <div class="receipt-title">BETTING SLIP</div>
</div> </div>
<!-- Separator --> <!-- Separator -->
<div class="receipt-separator">================================</div> <div class="receipt-separator">================================</div>
<!-- Bet Information --> <!-- Bet Information -->
<div class="receipt-info"> <div class="receipt-info">
<div class="receipt-row"> <div class="receipt-row">
...@@ -692,14 +700,14 @@ function generateReceiptHtml(betData) { ...@@ -692,14 +700,14 @@ function generateReceiptHtml(betData) {
<span>${betData.bet_datetime}</span> <span>${betData.bet_datetime}</span>
</div> </div>
</div> </div>
<!-- Separator --> <!-- Separator -->
<div class="receipt-separator">================================</div> <div class="receipt-separator">================================</div>
<!-- Bet Details --> <!-- Bet Details -->
<div class="receipt-bets"> <div class="receipt-bets">
`; `;
let totalAmount = 0; let totalAmount = 0;
betData.bet_details.forEach((detail, index) => { betData.bet_details.forEach((detail, index) => {
totalAmount += parseFloat(detail.amount); totalAmount += parseFloat(detail.amount);
...@@ -720,18 +728,18 @@ function generateReceiptHtml(betData) { ...@@ -720,18 +728,18 @@ function generateReceiptHtml(betData) {
</div> </div>
</div> </div>
`; `;
if (index < betData.bet_details.length - 1) { if (index < betData.bet_details.length - 1) {
receiptHtml += `<div class="receipt-separator">- - - - - - - - - - - - - - - - -</div>`; receiptHtml += `<div class="receipt-separator">- - - - - - - - - - - - - - - - -</div>`;
} }
}); });
receiptHtml += ` receiptHtml += `
</div> </div>
<!-- Separator --> <!-- Separator -->
<div class="receipt-separator">================================</div> <div class="receipt-separator">================================</div>
<!-- Total --> <!-- Total -->
<div class="receipt-total"> <div class="receipt-total">
<div class="receipt-total-line"> <div class="receipt-total-line">
...@@ -739,15 +747,15 @@ function generateReceiptHtml(betData) { ...@@ -739,15 +747,15 @@ function generateReceiptHtml(betData) {
<span>${formatCurrency(totalAmount)}</span> <span>${formatCurrency(totalAmount)}</span>
</div> </div>
</div> </div>
<!-- Separator --> <!-- Separator -->
<div class="receipt-separator">================================</div> <div class="receipt-separator">================================</div>
<!-- QR Code and Barcode (conditional based on settings) --> <!-- QR Code and Barcode (conditional based on settings) -->
<div class="receipt-verification" id="receipt-verification-${betData.uuid}"> <div class="receipt-verification" id="receipt-verification-${betData.uuid}">
<!-- QR Code and Barcode will be conditionally added here --> <!-- QR Code and Barcode will be conditionally added here -->
</div> </div>
<!-- Footer --> <!-- Footer -->
<div class="receipt-footer"> <div class="receipt-footer">
<div>Thank you for betting with MBetter!</div> <div>Thank you for betting with MBetter!</div>
...@@ -756,12 +764,7 @@ function generateReceiptHtml(betData) { ...@@ -756,12 +764,7 @@ function generateReceiptHtml(betData) {
</div> </div>
</div> </div>
`; `;
// Generate QR code and barcode after inserting HTML (conditional)
setTimeout(() => {
generateVerificationCodes(betData.uuid);
}, 100);
return receiptHtml; return receiptHtml;
} }
...@@ -776,88 +779,107 @@ function generateQRCode(betUuid) { ...@@ -776,88 +779,107 @@ function generateQRCode(betUuid) {
} }
function generateVerificationCodes(betUuid) { function generateVerificationCodes(betUuid) {
const verificationContainer = document.getElementById(`receipt-verification-${betUuid}`); return new Promise((resolve) => {
if (!verificationContainer) { const verificationContainer = document.getElementById(`receipt-verification-${betUuid}`);
return; if (!verificationContainer) {
} resolve();
return;
}
// Check QR code settings - QR codes should NOT print if disabled let completedOperations = 0;
// Default to NOT showing QR codes if API fails const totalOperations = 2; // QR code check + barcode check
let shouldShowQR = false;
function checkComplete() {
try { completedOperations++;
// Try to get QR settings from API if (completedOperations >= totalOperations) {
fetch('/api/qrcode-settings') resolve();
.then(response => { }
if (!response.ok) { }
console.warn('QR settings API failed with status:', response.status);
return null; // Check QR code settings - QR codes should NOT print if disabled
} // Default to NOT showing QR codes if API fails
return response.json(); let shouldShowQR = false;
})
.then(qrSettings => { try {
if (qrSettings && qrSettings.success && qrSettings.settings) { // Try to get QR settings from API
shouldShowQR = qrSettings.settings.enabled === true && qrSettings.settings.show_on_thermal === true; fetch('/api/qrcode-settings')
console.log('QR settings check result:', shouldShowQR, qrSettings.settings); .then(response => {
} else { if (!response.ok) {
console.warn('Invalid QR settings response:', qrSettings); console.warn('QR settings API failed with status:', response.status);
shouldShowQR = false; // Default to not showing checkComplete();
} return null;
}
return response.json();
})
.then(qrSettings => {
if (qrSettings && qrSettings.success && qrSettings.settings) {
shouldShowQR = qrSettings.settings.enabled === true && qrSettings.settings.show_on_thermal === true;
console.log('QR settings check result:', shouldShowQR, qrSettings.settings);
} else {
console.warn('Invalid QR settings response:', qrSettings);
shouldShowQR = false; // Default to not showing
}
// Add QR code if enabled // Add QR code if enabled
if (shouldShowQR) { if (shouldShowQR) {
console.log('Adding QR code to receipt'); console.log('Adding QR code to receipt');
const qrHtml = ` const qrHtml = `
<div class="receipt-qr"> <div class="receipt-qr">
<div class="qr-code" id="qr-code-${betUuid}"></div> <div class="qr-code" id="qr-code-${betUuid}"></div>
<div class="qr-text">Scan QR for verification</div> <div class="qr-text">Scan QR for verification</div>
</div>
`;
verificationContainer.insertAdjacentHTML('beforeend', qrHtml);
// Generate QR code
const qrContainer = document.getElementById(`qr-code-${betUuid}`);
if (qrContainer) {
const qrImageUrl = `https://api.qrserver.com/v1/create-qr-code/?size=100x100&data=${encodeURIComponent(betUuid)}&format=png`;
qrContainer.innerHTML = `<img src="${qrImageUrl}" alt="QR Code" class="qr-image">`;
}
} else {
console.log('QR code disabled - not adding to receipt');
}
checkComplete();
})
.catch(error => {
console.warn('Failed to check QR code settings, defaulting to disabled:', error);
shouldShowQR = false; // Default to not showing on error
checkComplete();
});
} catch (error) {
console.warn('Error in QR code settings check, defaulting to disabled:', error);
shouldShowQR = false; // Default to not showing on error
checkComplete();
}
// Check barcode settings
fetch(`/api/barcode-data/${betUuid}`)
.then(response => response.json())
.then(data => {
if (data.success && data.enabled && data.barcode_data && data.barcode_data.show_on_thermal && data.barcode_data.image_base64) {
// Add barcode to receipt
const barcodeHtml = `
<div class="receipt-barcode" id="barcode-container-${betUuid}">
<div class="barcode-image" id="barcode-${betUuid}"></div>
<div class="barcode-text">Scan barcode for verification</div>
</div> </div>
`; `;
verificationContainer.insertAdjacentHTML('beforeend', qrHtml); verificationContainer.insertAdjacentHTML('beforeend', barcodeHtml);
// Generate QR code // Display barcode
const qrContainer = document.getElementById(`qr-code-${betUuid}`); const barcodeElement = document.getElementById(`barcode-${betUuid}`);
if (qrContainer) { if (barcodeElement) {
const qrImageUrl = `https://api.qrserver.com/v1/create-qr-code/?size=100x100&data=${encodeURIComponent(betUuid)}&format=png`; barcodeElement.innerHTML = `<img src="data:image/png;base64,${data.barcode_data.image_base64}" alt="Barcode" class="barcode-img" style="width: ${data.barcode_data.width}px; height: ${data.barcode_data.height}px;">`;
qrContainer.innerHTML = `<img src="${qrImageUrl}" alt="QR Code" class="qr-image">`;
} }
} else {
console.log('QR code disabled - not adding to receipt');
} }
checkComplete();
}) })
.catch(error => { .catch(error => {
console.warn('Failed to check QR code settings, defaulting to disabled:', error); console.warn('Failed to check barcode settings:', error);
shouldShowQR = false; // Default to not showing on error checkComplete();
}); });
} catch (error) { });
console.warn('Error in QR code settings check, defaulting to disabled:', error);
shouldShowQR = false; // Default to not showing on error
}
// Check barcode settings
fetch(`/api/barcode-data/${betUuid}`)
.then(response => response.json())
.then(data => {
if (data.success && data.enabled && data.barcode_data && data.barcode_data.show_on_thermal && data.barcode_data.image_base64) {
// Add barcode to receipt
const barcodeHtml = `
<div class="receipt-barcode" id="barcode-container-${betUuid}">
<div class="barcode-image" id="barcode-${betUuid}"></div>
<div class="barcode-text">Scan barcode for verification</div>
</div>
`;
verificationContainer.insertAdjacentHTML('beforeend', barcodeHtml);
// Display barcode
const barcodeElement = document.getElementById(`barcode-${betUuid}`);
if (barcodeElement) {
barcodeElement.innerHTML = `<img src="data:image/png;base64,${data.barcode_data.image_base64}" alt="Barcode" class="barcode-img" style="width: ${data.barcode_data.width}px; height: ${data.barcode_data.height}px;">`;
}
}
})
.catch(error => {
console.warn('Failed to check barcode settings:', error);
});
} }
function printThermalReceipt() { function printThermalReceipt() {
...@@ -968,18 +990,25 @@ function directPrintBet(betId) { ...@@ -968,18 +990,25 @@ function directPrintBet(betId) {
if (data.success) { if (data.success) {
const betData = transformBetDataForReceipt(data.bet); const betData = transformBetDataForReceipt(data.bet);
const receiptHtml = generateReceiptHtml(betData); const receiptHtml = generateReceiptHtml(betData);
// Create temporary container for receipt content // Create temporary container for receipt content
const tempContainer = document.createElement('div'); const tempContainer = document.createElement('div');
tempContainer.innerHTML = receiptHtml; tempContainer.innerHTML = receiptHtml;
tempContainer.style.display = 'none'; tempContainer.style.display = 'none';
document.body.appendChild(tempContainer); document.body.appendChild(tempContainer);
// Wait for QR code and barcode to generate, then print directly // Generate verification codes and wait for completion
setTimeout(() => { generateVerificationCodes(betData.uuid).then(() => {
// Get the updated HTML content after verification codes are added
const updatedReceiptHtml = tempContainer.innerHTML;
printDirectly(updatedReceiptHtml);
document.body.removeChild(tempContainer);
}).catch(error => {
console.warn('Error generating verification codes, printing without them:', error);
// Print anyway even if verification codes fail
printDirectly(receiptHtml); printDirectly(receiptHtml);
document.body.removeChild(tempContainer); document.body.removeChild(tempContainer);
}, 600); // Give time for barcode/QR generation });
} else { } else {
showNotification('Failed to load bet details for printing: ' + (data.error || 'Unknown error'), 'error'); showNotification('Failed to load bet details for printing: ' + (data.error || 'Unknown error'), 'error');
} }
......
...@@ -635,9 +635,17 @@ function generateThermalReceipt(betId) { ...@@ -635,9 +635,17 @@ function generateThermalReceipt(betId) {
const receiptHtml = generateReceiptHtml(window.betData); const receiptHtml = generateReceiptHtml(window.betData);
receiptContainer.innerHTML = receiptHtml; receiptContainer.innerHTML = receiptHtml;
// Show the modal // Generate verification codes for preview
const modal = new bootstrap.Modal(document.getElementById('printReceiptModal')); generateVerificationCodes(window.betData.uuid).then(() => {
modal.show(); // Show the modal after verification codes are loaded
const modal = new bootstrap.Modal(document.getElementById('printReceiptModal'));
modal.show();
}).catch(error => {
console.warn('Error generating verification codes for preview, showing modal anyway:', error);
// Show modal even if verification codes fail
const modal = new bootstrap.Modal(document.getElementById('printReceiptModal'));
modal.show();
});
} }
function generateReceiptHtml(betData) { function generateReceiptHtml(betData) {
...@@ -744,114 +752,133 @@ function generateReceiptHtml(betData) { ...@@ -744,114 +752,133 @@ function generateReceiptHtml(betData) {
</div> </div>
`; `;
// Generate QR code and barcode after inserting HTML (conditional)
setTimeout(() => {
generateVerificationCodes(betData.uuid);
}, 100);
return receiptHtml; return receiptHtml;
} }
function generateVerificationCodes(betUuid) { function generateVerificationCodes(betUuid) {
const verificationContainer = document.getElementById(`receipt-verification-${betUuid}`); return new Promise((resolve) => {
if (!verificationContainer) { const verificationContainer = document.getElementById(`receipt-verification-${betUuid}`);
return; if (!verificationContainer) {
} resolve();
return;
}
// Check QR code settings - QR codes should NOT print if disabled let completedOperations = 0;
// Default to NOT showing QR codes if API fails const totalOperations = 2; // QR code check + barcode check
let shouldShowQR = false;
try {
// Try to get QR settings from API
fetch('/api/qrcode-settings')
.then(response => {
if (!response.ok) {
console.warn('QR settings API failed with status:', response.status);
return null;
}
return response.json();
})
.then(qrSettings => {
if (qrSettings && qrSettings.success && qrSettings.settings) {
shouldShowQR = qrSettings.settings.enabled === true && qrSettings.settings.show_on_thermal === true;
console.log('QR settings check result:', shouldShowQR, qrSettings.settings);
} else {
console.warn('Invalid QR settings response:', qrSettings);
shouldShowQR = false; // Default to not showing
}
// Add QR code if enabled function checkComplete() {
if (shouldShowQR) { completedOperations++;
console.log('Adding QR code to receipt'); if (completedOperations >= totalOperations) {
const qrHtml = ` resolve();
<div class="receipt-qr"> }
<div class="qr-code" id="qr-code-${betUuid}"></div> }
<div class="qr-text">Scan QR for verification</div>
</div> // Check QR code settings - QR codes should NOT print if disabled
`; // Default to NOT showing QR codes if API fails
verificationContainer.insertAdjacentHTML('beforeend', qrHtml); let shouldShowQR = false;
// Generate QR code try {
const qrContainer = document.getElementById(`qr-code-${betUuid}`); // Try to get QR settings from API
if (qrContainer) { fetch('/api/qrcode-settings')
const qrImageUrl = `https://api.qrserver.com/v1/create-qr-code/?size=100x100&data=${encodeURIComponent(betUuid)}&format=png`; .then(response => {
qrContainer.innerHTML = `<img src="${qrImageUrl}" alt="QR Code" class="qr-image">`; if (!response.ok) {
console.warn('QR settings API failed with status:', response.status);
checkComplete();
return null;
}
return response.json();
})
.then(qrSettings => {
if (qrSettings && qrSettings.success && qrSettings.settings) {
shouldShowQR = qrSettings.settings.enabled === true && qrSettings.settings.show_on_thermal === true;
console.log('QR settings check result:', shouldShowQR, qrSettings.settings);
} else {
console.warn('Invalid QR settings response:', qrSettings);
shouldShowQR = false; // Default to not showing
} }
} else {
console.log('QR code disabled - not adding to receipt');
}
})
.catch(error => {
console.warn('Failed to check QR code settings, defaulting to disabled:', error);
shouldShowQR = false; // Default to not showing on error
});
} catch (error) {
console.warn('Error in QR code settings check, defaulting to disabled:', error);
shouldShowQR = false; // Default to not showing on error
}
// Check barcode settings - use saved barcode data instead of regenerating // Add QR code if enabled
const betData = window.betData; if (shouldShowQR) {
if (betData && betData.barcode_data && betData.barcode_standard) { console.log('Adding QR code to receipt');
// Use saved barcode data from bet const qrHtml = `
fetch('/api/barcode-settings') <div class="receipt-qr">
.then(response => response.json()) <div class="qr-code" id="qr-code-${betUuid}"></div>
.then(settings => { <div class="qr-text">Scan QR for verification</div>
if (settings.success && settings.settings && settings.settings.show_on_thermal) { </div>
// Generate barcode image using the saved barcode data `;
fetch(`/api/barcode/${betUuid}`) verificationContainer.insertAdjacentHTML('beforeend', qrHtml);
.then(response => response.blob())
.then(blob => { // Generate QR code
const reader = new FileReader(); const qrContainer = document.getElementById(`qr-code-${betUuid}`);
reader.onload = function() { if (qrContainer) {
const base64 = reader.result; const qrImageUrl = `https://api.qrserver.com/v1/create-qr-code/?size=100x100&data=${encodeURIComponent(betUuid)}&format=png`;
// Add barcode to receipt qrContainer.innerHTML = `<img src="${qrImageUrl}" alt="QR Code" class="qr-image">`;
const barcodeHtml = ` }
<div class="receipt-barcode" id="barcode-container-${betUuid}"> } else {
<div class="barcode-image" id="barcode-${betUuid}"></div> console.log('QR code disabled - not adding to receipt');
<div class="barcode-text">Scan barcode for verification</div> }
</div> checkComplete();
`; })
verificationContainer.insertAdjacentHTML('beforeend', barcodeHtml); .catch(error => {
console.warn('Failed to check QR code settings, defaulting to disabled:', error);
// Display barcode shouldShowQR = false; // Default to not showing on error
const barcodeElement = document.getElementById(`barcode-${betUuid}`); checkComplete();
if (barcodeElement) { });
barcodeElement.innerHTML = `<img src="${base64}" alt="Barcode" class="barcode-img" style="width: 200px; height: 100px;">`; } catch (error) {
} console.warn('Error in QR code settings check, defaulting to disabled:', error);
}; shouldShowQR = false; // Default to not showing on error
reader.readAsDataURL(blob); checkComplete();
}) }
.catch(error => {
console.warn('Failed to generate barcode image:', error); // Check barcode settings - use saved barcode data instead of regenerating
}); const betData = window.betData;
} if (betData && betData.barcode_data && betData.barcode_standard) {
}) // Use saved barcode data from bet
.catch(error => { fetch('/api/barcode-settings')
console.warn('Failed to check barcode settings:', error); .then(response => response.json())
}); .then(settings => {
} if (settings.success && settings.settings && settings.settings.show_on_thermal) {
// Generate barcode image using the saved barcode data
fetch(`/api/barcode/${betUuid}`)
.then(response => response.blob())
.then(blob => {
const reader = new FileReader();
reader.onload = function() {
const base64 = reader.result;
// Add barcode to receipt
const barcodeHtml = `
<div class="receipt-barcode" id="barcode-container-${betUuid}">
<div class="barcode-image" id="barcode-${betUuid}"></div>
<div class="barcode-text">Scan barcode for verification</div>
</div>
`;
verificationContainer.insertAdjacentHTML('beforeend', barcodeHtml);
// Display barcode
const barcodeElement = document.getElementById(`barcode-${betUuid}`);
if (barcodeElement) {
barcodeElement.innerHTML = `<img src="${base64}" alt="Barcode" class="barcode-img" style="width: 200px; height: 100px;">`;
}
};
reader.readAsDataURL(blob);
checkComplete();
})
.catch(error => {
console.warn('Failed to generate barcode image:', error);
checkComplete();
});
} else {
checkComplete();
}
})
.catch(error => {
console.warn('Failed to check barcode settings:', error);
checkComplete();
});
} else {
checkComplete();
}
});
} }
function printThermalReceipt() { function printThermalReceipt() {
...@@ -1144,7 +1171,25 @@ function directPrintBet(betId) { ...@@ -1144,7 +1171,25 @@ function directPrintBet(betId) {
markBetAsPaidForPrinting(betId).then(() => { markBetAsPaidForPrinting(betId).then(() => {
// Use the global bet data for direct printing // Use the global bet data for direct printing
const receiptHtml = generateReceiptHtml(window.betData); const receiptHtml = generateReceiptHtml(window.betData);
printDirectly(receiptHtml);
// Create temporary container for receipt content
const tempContainer = document.createElement('div');
tempContainer.innerHTML = receiptHtml;
tempContainer.style.display = 'none';
document.body.appendChild(tempContainer);
// Generate verification codes and wait for completion
generateVerificationCodes(window.betData.uuid).then(() => {
// Get the updated HTML content after verification codes are added
const updatedReceiptHtml = tempContainer.innerHTML;
printDirectly(updatedReceiptHtml);
document.body.removeChild(tempContainer);
}).catch(error => {
console.warn('Error generating verification codes, printing without them:', error);
// Print anyway even if verification codes fail
printDirectly(receiptHtml);
document.body.removeChild(tempContainer);
});
}).catch(error => { }).catch(error => {
console.error('Failed to mark bet as paid:', error); console.error('Failed to mark bet as paid:', error);
// Still print even if marking as paid fails // Still print even if marking as paid fails
......
...@@ -637,10 +637,18 @@ function generateThermalReceiptFromList(betId) { ...@@ -637,10 +637,18 @@ function generateThermalReceiptFromList(betId) {
const receiptContainer = document.getElementById('thermal-receipt'); const receiptContainer = document.getElementById('thermal-receipt');
const receiptHtml = generateReceiptHtml(betData); const receiptHtml = generateReceiptHtml(betData);
receiptContainer.innerHTML = receiptHtml; receiptContainer.innerHTML = receiptHtml;
// Show the modal // Generate verification codes for preview
const modal = new bootstrap.Modal(document.getElementById('printReceiptModal')); generateVerificationCodes(betData.uuid).then(() => {
modal.show(); // Show the modal after verification codes are loaded
const modal = new bootstrap.Modal(document.getElementById('printReceiptModal'));
modal.show();
}).catch(error => {
console.warn('Error generating verification codes for preview, showing modal anyway:', error);
// Show modal even if verification codes fail
const modal = new bootstrap.Modal(document.getElementById('printReceiptModal'));
modal.show();
});
} else { } else {
showNotification('Failed to load bet details for printing: ' + (data.error || 'Unknown error'), 'error'); showNotification('Failed to load bet details for printing: ' + (data.error || 'Unknown error'), 'error');
} }
...@@ -672,17 +680,17 @@ function transformBetDataForReceipt(betData) { ...@@ -672,17 +680,17 @@ function transformBetDataForReceipt(betData) {
function generateReceiptHtml(betData) { function generateReceiptHtml(betData) {
const currentDateTime = new Date().toLocaleString(); const currentDateTime = new Date().toLocaleString();
let receiptHtml = ` let receiptHtml = `
<div class="thermal-receipt-content"> <div class="thermal-receipt-content">
<!-- Header --> <!-- Header -->
<div class="receipt-header"> <div class="receipt-header">
<div class="receipt-title">BETTING SLIP</div> <div class="receipt-title">BETTING SLIP</div>
</div> </div>
<!-- Separator --> <!-- Separator -->
<div class="receipt-separator">================================</div> <div class="receipt-separator">================================</div>
<!-- Bet Information --> <!-- Bet Information -->
<div class="receipt-info"> <div class="receipt-info">
<div class="receipt-row"> <div class="receipt-row">
...@@ -694,14 +702,14 @@ function generateReceiptHtml(betData) { ...@@ -694,14 +702,14 @@ function generateReceiptHtml(betData) {
<span>${betData.bet_datetime}</span> <span>${betData.bet_datetime}</span>
</div> </div>
</div> </div>
<!-- Separator --> <!-- Separator -->
<div class="receipt-separator">================================</div> <div class="receipt-separator">================================</div>
<!-- Bet Details --> <!-- Bet Details -->
<div class="receipt-bets"> <div class="receipt-bets">
`; `;
let totalAmount = 0; let totalAmount = 0;
betData.bet_details.forEach((detail, index) => { betData.bet_details.forEach((detail, index) => {
totalAmount += parseFloat(detail.amount); totalAmount += parseFloat(detail.amount);
...@@ -722,18 +730,18 @@ function generateReceiptHtml(betData) { ...@@ -722,18 +730,18 @@ function generateReceiptHtml(betData) {
</div> </div>
</div> </div>
`; `;
if (index < betData.bet_details.length - 1) { if (index < betData.bet_details.length - 1) {
receiptHtml += `<div class="receipt-separator">- - - - - - - - - - - - - - - - -</div>`; receiptHtml += `<div class="receipt-separator">- - - - - - - - - - - - - - - - -</div>`;
} }
}); });
receiptHtml += ` receiptHtml += `
</div> </div>
<!-- Separator --> <!-- Separator -->
<div class="receipt-separator">================================</div> <div class="receipt-separator">================================</div>
<!-- Total --> <!-- Total -->
<div class="receipt-total"> <div class="receipt-total">
<div class="receipt-total-line"> <div class="receipt-total-line">
...@@ -741,15 +749,15 @@ function generateReceiptHtml(betData) { ...@@ -741,15 +749,15 @@ function generateReceiptHtml(betData) {
<span>${formatCurrency(totalAmount)}</span> <span>${formatCurrency(totalAmount)}</span>
</div> </div>
</div> </div>
<!-- Separator --> <!-- Separator -->
<div class="receipt-separator">================================</div> <div class="receipt-separator">================================</div>
<!-- QR Code and Barcode (conditional based on settings) --> <!-- QR Code and Barcode (conditional based on settings) -->
<div class="receipt-verification" id="receipt-verification-${betData.uuid}"> <div class="receipt-verification" id="receipt-verification-${betData.uuid}">
<!-- QR Code and Barcode will be conditionally added here --> <!-- QR Code and Barcode will be conditionally added here -->
</div> </div>
<!-- Footer --> <!-- Footer -->
<div class="receipt-footer"> <div class="receipt-footer">
<div>Thank you for betting with MBetter!</div> <div>Thank you for betting with MBetter!</div>
...@@ -758,12 +766,7 @@ function generateReceiptHtml(betData) { ...@@ -758,12 +766,7 @@ function generateReceiptHtml(betData) {
</div> </div>
</div> </div>
`; `;
// Generate QR code and barcode after inserting HTML (conditional)
setTimeout(() => {
generateVerificationCodes(betData.uuid);
}, 100);
return receiptHtml; return receiptHtml;
} }
...@@ -778,88 +781,107 @@ function generateQRCode(betUuid) { ...@@ -778,88 +781,107 @@ function generateQRCode(betUuid) {
} }
function generateVerificationCodes(betUuid) { function generateVerificationCodes(betUuid) {
const verificationContainer = document.getElementById(`receipt-verification-${betUuid}`); return new Promise((resolve) => {
if (!verificationContainer) { const verificationContainer = document.getElementById(`receipt-verification-${betUuid}`);
return; if (!verificationContainer) {
} resolve();
return;
}
// Check QR code settings - QR codes should NOT print if disabled let completedOperations = 0;
// Default to NOT showing QR codes if API fails const totalOperations = 2; // QR code check + barcode check
let shouldShowQR = false;
function checkComplete() {
try { completedOperations++;
// Try to get QR settings from API if (completedOperations >= totalOperations) {
fetch('/api/qrcode-settings') resolve();
.then(response => { }
if (!response.ok) { }
console.warn('QR settings API failed with status:', response.status);
return null; // Check QR code settings - QR codes should NOT print if disabled
} // Default to NOT showing QR codes if API fails
return response.json(); let shouldShowQR = false;
})
.then(qrSettings => { try {
if (qrSettings && qrSettings.success && qrSettings.settings) { // Try to get QR settings from API
shouldShowQR = qrSettings.settings.enabled === true && qrSettings.settings.show_on_thermal === true; fetch('/api/qrcode-settings')
console.log('QR settings check result:', shouldShowQR, qrSettings.settings); .then(response => {
} else { if (!response.ok) {
console.warn('Invalid QR settings response:', qrSettings); console.warn('QR settings API failed with status:', response.status);
shouldShowQR = false; // Default to not showing checkComplete();
} return null;
}
return response.json();
})
.then(qrSettings => {
if (qrSettings && qrSettings.success && qrSettings.settings) {
shouldShowQR = qrSettings.settings.enabled === true && qrSettings.settings.show_on_thermal === true;
console.log('QR settings check result:', shouldShowQR, qrSettings.settings);
} else {
console.warn('Invalid QR settings response:', qrSettings);
shouldShowQR = false; // Default to not showing
}
// Add QR code if enabled // Add QR code if enabled
if (shouldShowQR) { if (shouldShowQR) {
console.log('Adding QR code to receipt'); console.log('Adding QR code to receipt');
const qrHtml = ` const qrHtml = `
<div class="receipt-qr"> <div class="receipt-qr">
<div class="qr-code" id="qr-code-${betUuid}"></div> <div class="qr-code" id="qr-code-${betUuid}"></div>
<div class="qr-text">Scan QR for verification</div> <div class="qr-text">Scan QR for verification</div>
</div>
`;
verificationContainer.insertAdjacentHTML('beforeend', qrHtml);
// Generate QR code
const qrContainer = document.getElementById(`qr-code-${betUuid}`);
if (qrContainer) {
const qrImageUrl = `https://api.qrserver.com/v1/create-qr-code/?size=100x100&data=${encodeURIComponent(betUuid)}&format=png`;
qrContainer.innerHTML = `<img src="${qrImageUrl}" alt="QR Code" class="qr-image">`;
}
} else {
console.log('QR code disabled - not adding to receipt');
}
checkComplete();
})
.catch(error => {
console.warn('Failed to check QR code settings, defaulting to disabled:', error);
shouldShowQR = false; // Default to not showing on error
checkComplete();
});
} catch (error) {
console.warn('Error in QR code settings check, defaulting to disabled:', error);
shouldShowQR = false; // Default to not showing on error
checkComplete();
}
// Check barcode settings
fetch(`/api/barcode-data/${betUuid}`)
.then(response => response.json())
.then(data => {
if (data.success && data.enabled && data.barcode_data && data.barcode_data.show_on_thermal && data.barcode_data.image_base64) {
// Add barcode to receipt
const barcodeHtml = `
<div class="receipt-barcode" id="barcode-container-${betUuid}">
<div class="barcode-image" id="barcode-${betUuid}"></div>
<div class="barcode-text">Scan barcode for verification</div>
</div> </div>
`; `;
verificationContainer.insertAdjacentHTML('beforeend', qrHtml); verificationContainer.insertAdjacentHTML('beforeend', barcodeHtml);
// Generate QR code // Display barcode
const qrContainer = document.getElementById(`qr-code-${betUuid}`); const barcodeElement = document.getElementById(`barcode-${betUuid}`);
if (qrContainer) { if (barcodeElement) {
const qrImageUrl = `https://api.qrserver.com/v1/create-qr-code/?size=100x100&data=${encodeURIComponent(betUuid)}&format=png`; barcodeElement.innerHTML = `<img src="data:image/png;base64,${data.barcode_data.image_base64}" alt="Barcode" class="barcode-img" style="width: ${data.barcode_data.width}px; height: ${data.barcode_data.height}px;">`;
qrContainer.innerHTML = `<img src="${qrImageUrl}" alt="QR Code" class="qr-image">`;
} }
} else {
console.log('QR code disabled - not adding to receipt');
} }
checkComplete();
}) })
.catch(error => { .catch(error => {
console.warn('Failed to check QR code settings, defaulting to disabled:', error); console.warn('Failed to check barcode settings:', error);
shouldShowQR = false; // Default to not showing on error checkComplete();
}); });
} catch (error) { });
console.warn('Error in QR code settings check, defaulting to disabled:', error);
shouldShowQR = false; // Default to not showing on error
}
// Check barcode settings
fetch(`/api/barcode-data/${betUuid}`)
.then(response => response.json())
.then(data => {
if (data.success && data.enabled && data.barcode_data && data.barcode_data.show_on_thermal && data.barcode_data.image_base64) {
// Add barcode to receipt
const barcodeHtml = `
<div class="receipt-barcode" id="barcode-container-${betUuid}">
<div class="barcode-image" id="barcode-${betUuid}"></div>
<div class="barcode-text">Scan barcode for verification</div>
</div>
`;
verificationContainer.insertAdjacentHTML('beforeend', barcodeHtml);
// Display barcode
const barcodeElement = document.getElementById(`barcode-${betUuid}`);
if (barcodeElement) {
barcodeElement.innerHTML = `<img src="data:image/png;base64,${data.barcode_data.image_base64}" alt="Barcode" class="barcode-img" style="width: ${data.barcode_data.width}px; height: ${data.barcode_data.height}px;">`;
}
}
})
.catch(error => {
console.warn('Failed to check barcode settings:', error);
});
} }
function printThermalReceipt() { function printThermalReceipt() {
...@@ -974,18 +996,25 @@ function directPrintBet(betId) { ...@@ -974,18 +996,25 @@ function directPrintBet(betId) {
if (data.success) { if (data.success) {
const betData = transformBetDataForReceipt(data.bet); const betData = transformBetDataForReceipt(data.bet);
const receiptHtml = generateReceiptHtml(betData); const receiptHtml = generateReceiptHtml(betData);
// Create temporary container for receipt content // Create temporary container for receipt content
const tempContainer = document.createElement('div'); const tempContainer = document.createElement('div');
tempContainer.innerHTML = receiptHtml; tempContainer.innerHTML = receiptHtml;
tempContainer.style.display = 'none'; tempContainer.style.display = 'none';
document.body.appendChild(tempContainer); document.body.appendChild(tempContainer);
// Wait for QR code and barcode to generate, then print directly // Generate verification codes and wait for completion
setTimeout(() => { generateVerificationCodes(betData.uuid).then(() => {
// Get the updated HTML content after verification codes are added
const updatedReceiptHtml = tempContainer.innerHTML;
printDirectly(updatedReceiptHtml);
document.body.removeChild(tempContainer);
}).catch(error => {
console.warn('Error generating verification codes, printing without them:', error);
// Print anyway even if verification codes fail
printDirectly(receiptHtml); printDirectly(receiptHtml);
document.body.removeChild(tempContainer); document.body.removeChild(tempContainer);
}, 600); // Give time for barcode/QR generation });
} else { } else {
showNotification('Failed to load bet details for printing: ' + (data.error || 'Unknown error'), 'error'); showNotification('Failed to load bet details for printing: ' + (data.error || 'Unknown error'), 'error');
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment