feat : almost completed cancel

This commit is contained in:
Sibin Sabu 2025-09-15 15:15:15 +05:30
parent 3486b342d7
commit d851b0f949
2 changed files with 146 additions and 132 deletions

View File

@ -41,7 +41,7 @@ export class SidebarComponent {
this.showCancel = true; this.showCancel = true;
this.cancelWarning = ''; this.cancelWarning = '';
const localTicketsStr = localStorage.getItem('localTicketsnew'); const localTicketsStr = localStorage.getItem('canceltickets');
if (localTicketsStr) { if (localTicketsStr) {
try { try {
const localTickets = JSON.parse(localTicketsStr); const localTickets = JSON.parse(localTicketsStr);
@ -52,7 +52,7 @@ export class SidebarComponent {
} }
} }
} catch (e) { } catch (e) {
console.error('Error reading localTicketsnew:', e); console.error('Error reading canceltickets:', e);
} }
} }
} }
@ -63,17 +63,16 @@ export class SidebarComponent {
} }
//--------------------------------------- UPDATED CANCEL PRINT -------------------------------------------
// //---------------------------------------ADDED THE CANCEL PRINT ------------------------------------------- cancelWarning: string = ''; // For showing inline warnings
// cancelWarning: string = ''; // For showing inline warnings // async printTicketCancel() {
// printTicketCancel() {
// const enteredTicketNo = this.ticketNo?.trim(); // const enteredTicketNo = this.ticketNo?.trim();
// console.log('[STEP 1] Entered Ticket No:', enteredTicketNo); // console.log('[STEP 1] Entered Ticket No:', enteredTicketNo);
// this.cancelWarning = ''; // this.cancelWarning = '';
// const localTicketsStr = localStorage.getItem('localTicketsnew'); // const localTicketsStr = localStorage.getItem('canceltickets');
// if (!localTicketsStr) { // if (!localTicketsStr) {
// console.error('[STEP 2] No localTickets found in localStorage.'); // console.error('[STEP 2] No localTickets found in localStorage.');
// this.cancelWarning = '❌ No ticket history found.'; // this.cancelWarning = '❌ No ticket history found.';
@ -108,20 +107,49 @@ export class SidebarComponent {
// console.log('[STEP 7] Stored Barcode ID:', storedBarcode); // console.log('[STEP 7] Stored Barcode ID:', storedBarcode);
// if (enteredTicketNo === storedBarcode) { // if (enteredTicketNo === storedBarcode) {
// console.log('[✅ MATCH] Ticket No matches barcode. Proceeding to print...'); // console.log('[✅ MATCH] Ticket No matches barcode. Proceeding to cancel...');
// // 🧾 Construct cancel print layout (only totalAmount) // try {
// const cancelTicketData = { // // 🔗 Call cancel API first
// type: 'CANCEL_TICKET', // const cancelReq = {
// ticketId: lastTicket.ticketId || '', // ticketNo: lastTicket.barcodeId,
// barcodeId: lastTicket.barcodeId, // btId: lastTicket.btId || "0485", // <-- put correct btId here
// cancelDate: new Date().toLocaleString(), // usrId: lastTicket.usrId || "341" // <-- put correct usrId here
// totalAmount: lastTicket.totalAmount || 0 // <-- only totalAmount // };
// };
// // 🔍 Console Preview // console.log("📡 Sending cancel request:", cancelReq);
// console.log('🧾 [CANCEL TICKET LAYOUT]');
// console.log(` // const response = await fetch("http://localhost:8085/cancel/ticket", {
// method: "POST",
// headers: { "Content-Type": "application/json" },
// body: JSON.stringify(cancelReq)
// });
// const data = await response.json();
// console.log("📩 Cancel API Response:", data);
// if (!data.ok) {
// console.error("❌ Cancel failed:", data.error);
// this.cancelWarning = `❌ Cancel failed: ${data.error}`;
// return;
// }
// // ✅ Cancel succeeded
// // Clear the input so it wont show until next ticket is issued
// this.ticketNo = ''; // <-- THIS LINE CLEARS THE INPUT
// const cancelTicketData = {
// type: 'CANCEL_TICKET',
// ticketId: lastTicket.ticketId || '',
// barcodeId: lastTicket.barcodeId,
// cancelDate: new Date().toLocaleString(),
// totalAmount: lastTicket.totalAmount || 0
// };
// // 🔍 Console Preview
// console.log('🧾 [CANCEL TICKET LAYOUT]');
// console.log(`
// ======================================== // ========================================
// ⚠️ TICKET CANCELLED ⚠️ // ⚠️ TICKET CANCELLED ⚠️
// ---------------------------------------- // ----------------------------------------
@ -130,26 +158,28 @@ export class SidebarComponent {
// Cancelled At : ${cancelTicketData.cancelDate} // Cancelled At : ${cancelTicketData.cancelDate}
// Original Total: ₹${cancelTicketData.totalAmount} // Original Total: ₹${cancelTicketData.totalAmount}
// ======================================== // ========================================
// `); // `);
// // 🖨️ Send to printer server // // 🖨️ Send to printer server
// fetch('http://localhost:9100/print', { // fetch('http://localhost:9100/print', {
// method: 'POST', // method: 'POST',
// headers: { 'Content-Type': 'application/json' }, // headers: { 'Content-Type': 'application/json' },
// body: JSON.stringify(cancelTicketData) // body: JSON.stringify(cancelTicketData)
// })
// .then(response => {
// if (!response.ok) throw new Error(`Printer error: ${response.status}`);
// return response.text();
// }) // })
// .then(result => { // .then(r => r.text())
// console.log("✅ Cancel ticket print successful:", result); // .then(result => {
// }) // console.log("✅ Cancel ticket print successful:", result);
// .catch(error => { // })
// console.error("❌ Cancel ticket print failed:", error); // .catch(error => {
// }); // console.error("❌ Cancel ticket print failed:", error);
// });
// this.closeCancelPopup(); // this.closeCancelPopup();
// } catch (e) {
// console.error("❌ Cancel API call failed:", e);
// this.cancelWarning = '❌ Cancel API call failed. Please try again.';
// }
// } else { // } else {
// console.warn('[❌ MISMATCH] Entered Ticket No does NOT match last stored barcode.'); // console.warn('[❌ MISMATCH] Entered Ticket No does NOT match last stored barcode.');
@ -160,14 +190,14 @@ export class SidebarComponent {
// //--------------------------------COMPLETED THE CANCEL PRINT --------------------------------------- // //--------------------------------COMPLETED THE CANCEL PRINT ---------------------------------------
//--------------------------------------- UPDATED CANCEL PRINT ------------------------------------------- //--------------------------------------- UPDATED CANCEL PRINT -------------------------------------------
cancelWarning: string = ''; // For showing inline warnings
async printTicketCancel() { async printTicketCancel() {
const enteredTicketNo = this.ticketNo?.trim(); const enteredTicketNo = this.ticketNo?.trim();
console.log('[STEP 1] Entered Ticket No:', enteredTicketNo); console.log('[STEP 1] Entered Ticket No:', enteredTicketNo);
this.cancelWarning = ''; this.cancelWarning = '';
const localTicketsStr = localStorage.getItem('localTicketsnew'); const localTicketsStr = localStorage.getItem('canceltickets');
if (!localTicketsStr) { if (!localTicketsStr) {
console.error('[STEP 2] No localTickets found in localStorage.'); console.error('[STEP 2] No localTickets found in localStorage.');
this.cancelWarning = '❌ No ticket history found.'; this.cancelWarning = '❌ No ticket history found.';
@ -201,46 +231,69 @@ async printTicketCancel() {
const storedBarcode = lastTicket.barcodeId.trim(); const storedBarcode = lastTicket.barcodeId.trim();
console.log('[STEP 7] Stored Barcode ID:', storedBarcode); console.log('[STEP 7] Stored Barcode ID:', storedBarcode);
if (enteredTicketNo === storedBarcode) { if (enteredTicketNo !== storedBarcode) {
console.log('[✅ MATCH] Ticket No matches barcode. Proceeding to cancel...'); console.warn('[❌ MISMATCH] Entered Ticket No does NOT match last stored barcode.');
this.cancelWarning = '❌ Invalid ticket number. Please check and try again.';
return;
}
console.log('[✅ MATCH] Ticket No matches barcode. Proceeding to cancel...');
try {
const cancelReq = {
ticketNo: lastTicket.barcodeId,
btId: lastTicket.btId || "0485",
usrId: lastTicket.usrId || "341"
};
console.log("📡 Sending cancel request:", cancelReq);
const response = await fetch("http://localhost:8085/cancel/ticket", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(cancelReq)
});
const data = await response.json();
console.log("📩 Cancel API Response:", data);
if (!data.ok) {
console.error("❌ Cancel failed:", data.error);
this.cancelWarning = `❌ Cancel failed: ${data.error}`;
return;
}
// ===== SUCCESS =====
// 1) Remove the last ticket from canceltickets so modal won't auto-fill it again
try { try {
// 🔗 Call cancel API first const ctStr = localStorage.getItem('canceltickets');
const cancelReq = { if (ctStr) {
ticketNo: lastTicket.barcodeId, const ct = JSON.parse(ctStr);
btId: lastTicket.btId || "0485", // <-- put correct btId here if (Array.isArray(ct) && ct.length > 0) {
usrId: lastTicket.usrId || "341" // <-- put correct usrId here ct.pop(); // remove last (the one we just cancelled)
}; localStorage.setItem('canceltickets', JSON.stringify(ct));
console.log('[localStorage] removed last entry from canceltickets after cancel');
console.log("📡 Sending cancel request:", cancelReq); }
const response = await fetch("http://localhost:8085/cancel/ticket", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(cancelReq)
});
const data = await response.json();
console.log("📩 Cancel API Response:", data);
if (!data.ok) {
console.error("❌ Cancel failed:", data.error);
this.cancelWarning = `❌ Cancel failed: ${data.error}`;
return;
} }
} catch (e) {
console.warn('Could not update canceltickets after cancel', e);
}
// ✅ Only print if cancel API succeeded // 2) Clear the UI input immediately
const cancelTicketData = { this.ticketNo = '';
type: 'CANCEL_TICKET', this.cancelWarning = '';
ticketId: lastTicket.ticketId || '',
barcodeId: lastTicket.barcodeId,
cancelDate: new Date().toLocaleString(),
totalAmount: lastTicket.totalAmount || 0
};
// 🔍 Console Preview // 3) Prepare print payload and send to printer (non-blocking)
console.log('🧾 [CANCEL TICKET LAYOUT]'); const cancelTicketData = {
console.log(` type: 'CANCEL_TICKET',
ticketId: lastTicket.ticketId || '',
barcodeId: lastTicket.barcodeId,
cancelDate: new Date().toLocaleString(),
totalAmount: lastTicket.totalAmount || 0
};
console.log('🧾 [CANCEL TICKET LAYOUT]');
console.log(`
======================================== ========================================
TICKET CANCELLED TICKET CANCELLED
---------------------------------------- ----------------------------------------
@ -249,32 +302,27 @@ Barcode ID : ${cancelTicketData.barcodeId}
Cancelled At : ${cancelTicketData.cancelDate} Cancelled At : ${cancelTicketData.cancelDate}
Original Total: ${cancelTicketData.totalAmount} Original Total: ${cancelTicketData.totalAmount}
======================================== ========================================
`); `);
// 🖨️ Send to printer server fetch('http://localhost:9100/print', {
fetch('http://localhost:9100/print', { method: 'POST',
method: 'POST', headers: { 'Content-Type': 'application/json' },
headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(cancelTicketData)
body: JSON.stringify(cancelTicketData) })
.then(r => r.text())
.then(result => {
console.log("✅ Cancel ticket print successful:", result);
}) })
.then(r => r.text()) .catch(error => {
.then(result => { console.error("❌ Cancel ticket print failed:", error);
console.log("✅ Cancel ticket print successful:", result); });
})
.catch(error => {
console.error("❌ Cancel ticket print failed:", error);
});
this.closeCancelPopup(); // 4) Close the modal
this.closeCancelPopup();
} catch (e) { } catch (e) {
console.error("❌ Cancel API call failed:", e); console.error("❌ Cancel API call failed:", e);
this.cancelWarning = '❌ Cancel API call failed. Please try again.'; this.cancelWarning = '❌ Cancel API call failed. Please try again.';
}
} else {
console.warn('[❌ MISMATCH] Entered Ticket No does NOT match last stored barcode.');
this.cancelWarning = '❌ Invalid ticket number. Please check and try again.';
} }
} }
@ -554,48 +602,12 @@ ${receiptText}
} }
//----------------------------------------------PRINT WITHDRAW TICKET ENDS HERE ----------------------------- //----------------------------------------------PRINT WITHDRAW TICKET ENDS HERE -----------------------------
raceCardData: any = null; // ✅ Hold fetched data raceCardData: any = null; // ✅ Hold fetched data
// openViewRcPopup() {
// const cachedData = localStorage.getItem('rpinfo');
// if (cachedData) {
// this.raceCardData = JSON.parse(cachedData);
// console.log('📦 Loaded race card from localStorage:', this.raceCardData);
// } else {
// this.raceCardData = { error: 'Race card not available locally' };
// console.warn('⚠️ No race card data found in localStorage.');
// }
// this.showViewRc = true;
// }
// openViewRcPopup() {
// const cachedData = localStorage.getItem('rpinfo');
// if (cachedData) {
// try {
// this.raceCardData = JSON.parse(cachedData); // now it's an array
// console.log('📦 Loaded race card from localStorage:', this.raceCardData);
// } catch (e) {
// console.error('Error parsing rpinfo:', e);
// this.raceCardData = { error: 'Invalid race card data' };
// }
// } else {
// this.raceCardData = { error: 'Race card not available locally' };
// console.warn('⚠️ No race card data found in localStorage.');
// }
// this.showViewRc = true;
// }
openViewRcPopup() { openViewRcPopup() {
const cachedData = localStorage.getItem('rpinfo'); const cachedData = localStorage.getItem('rpinfo');

View File

@ -12,7 +12,7 @@ import { Subscription } from 'rxjs';
import { SelectionService, SelectionData } from '../selection.service/selection.service'; import { SelectionService, SelectionData } from '../selection.service/selection.service';
import { SharedStateService } from '../../service/shared-state.service'; import { SharedStateService } from '../../service/shared-state.service';
import { LabelRestrictionService } from '../selection.service/label-restriction.service'; import { LabelRestrictionService } from '../selection.service/label-restriction.service';
import _ from 'lodash'; import _, { join } from 'lodash';
@Component({ @Component({
selector: 'app-touch-pad-menu', selector: 'app-touch-pad-menu',
@ -1438,6 +1438,7 @@ try {
try { try {
localStorage.setItem('issueT', JSON.stringify(commitJson)); localStorage.setItem('issueT', JSON.stringify(commitJson));
console.log('Saved commitJson into localStorage key: issueT'); console.log('Saved commitJson into localStorage key: issueT');
localStorage.setItem('cancelT', JSON.stringify(commitJson));
} catch (e) { } catch (e) {
console.warn('Failed to save issueT:', e); console.warn('Failed to save issueT:', e);
} }
@ -1524,6 +1525,7 @@ try {
try { try {
localStorage.setItem('localTicketsnew', JSON.stringify([payload])); localStorage.setItem('localTicketsnew', JSON.stringify([payload]));
console.log('📦 [DEBUG] Latest ticket stored in localStorage (previous cleared).'); console.log('📦 [DEBUG] Latest ticket stored in localStorage (previous cleared).');
localStorage.setItem('canceltickets', JSON.stringify([payload]));
} catch (error) { } catch (error) {
console.error('❌ Failed to store ticket locally:', error); console.error('❌ Failed to store ticket locally:', error);
} }