From f00629cf45ec0b8673cd8c74cfcf8459ffd32c19 Mon Sep 17 00:00:00 2001 From: Sibin Sabu Date: Thu, 18 Sep 2025 12:46:51 +0530 Subject: [PATCH] feat : almost done full sidebar functinalities --- .../components/sidebar/sidebar.component.ts | 435 ++++++++++-------- 1 file changed, 240 insertions(+), 195 deletions(-) diff --git a/btc-UI/src/app/components/sidebar/sidebar.component.ts b/btc-UI/src/app/components/sidebar/sidebar.component.ts index b61b66d..ce03464 100755 --- a/btc-UI/src/app/components/sidebar/sidebar.component.ts +++ b/btc-UI/src/app/components/sidebar/sidebar.component.ts @@ -124,7 +124,7 @@ async printTicketCancel() { const cancelReq = { ticketNo: lastTicket.barcodeId, btId: lastTicket.btId || "0485", - usrId: lastTicket.usrId || "341" + usrId: lastTicket.usrId || "066" }; console.log("๐Ÿ“ก Sending cancel request:", cancelReq); @@ -229,122 +229,7 @@ Original Total: โ‚น${cancelTicketData.totalAmount} payoutWarning = ''; // show GUI message when payout response is not as expected -// async printPayoutTicket() { -// console.log("๐Ÿ–จ๏ธ Print payout clicked"); -// const ticketNo = this.payoutTicketNo?.toString().trim(); -// if (!ticketNo) { -// console.warn("โš ๏ธ No payout ticket number set."); -// return; -// } - -// const userName = localStorage.getItem('userName') || 'Unknown'; - -// // Prepare request payload (same as your curl) -// const apiPayload = { -// ticketNo: ticketNo, -// voucherNo: "", -// memAcctNum: "", -// memAcctCardCode: "", -// moneyTyp: "C", -// btId: "0485", // replace with dynamic btId if needed -// usrId: "341" // replace with dynamic usrId if needed -// }; - -// console.log("๐Ÿ“ก Sending payout API request:", apiPayload); - -// try { -// const apiUrl = "http://localhost:8086/payout/ticket?debug=1"; - -// const resp = await fetch(apiUrl, { -// method: "POST", -// headers: { "Content-Type": "application/json" }, -// body: JSON.stringify(apiPayload) -// }); - -// if (!resp.ok) { -// console.error("โŒ Payout API HTTP error:", resp.status, resp.statusText); -// return; -// } - -// const json = await resp.json(); -// console.log("๐Ÿ“ฉ Payout API response JSON:", json); - -// // โœ… Save only the response (no request) to localStorage -// try { -// localStorage.setItem("payout", JSON.stringify(json)); -// console.log("[localStorage] saved payout response only"); -// } catch (e) { -// console.warn("โš ๏ธ Failed to save payout to localStorage:", e); -// } - -// if (!json.ok) { -// console.error("โŒ Payout API returned ok: false -", json.error || json); -// return; -// } - - -// // โœ… Load the payout response from localStorage -// let payoutResp: any = null; -// try { -// const stored = localStorage.getItem("payout"); -// if (stored) payoutResp = JSON.parse(stored); -// } catch (e) { -// console.warn("โš ๏ธ Failed to parse stored payout:", e); -// } - -// // Default dividend fallback -// let dividend = "0"; -// if (payoutResp && payoutResp.data && typeof payoutResp.data.cashAmount !== "undefined") { -// dividend = String(payoutResp.data.cashAmount); -// } -// // Success: proceed to build print payload -// const backendData = json.data || {}; -// const printData = { -// type: 'payout', -// ticketNumber: ticketNo, -// printedBy: userName, -// dividend: dividend, -// backend: backendData -// }; - -// const layoutPreview = ` -// ------------------------------- -// ๐ŸŽ‰ PAYOUT TICKET ๐ŸŽ‰ -// ------------------------------- -// Ticket No : ${printData.ticketNumber} -// Dividend : ${printData.dividend} -// Printed By : ${printData.printedBy} -// Backend Amt : ${backendData.cashAmount ?? 'N/A'} -// User Name : ${backendData.userName ?? 'N/A'} -// Date : ${new Date().toLocaleString()} -// ------------------------------- -// ๐Ÿ’ธ Thank you for playing ๐Ÿ’ธ -// ------------------------------- -// `; -// console.log("[๐Ÿงพ PAYOUT LAYOUT PREVIEW]"); -// console.log(layoutPreview); - -// try { -// const printResp = await fetch('http://localhost:9100/print', { -// method: 'POST', -// headers: { 'Content-Type': 'application/json' }, -// body: JSON.stringify(printData) -// }); - -// if (!printResp.ok) throw new Error(`Printer HTTP ${printResp.status}`); -// const printResult = await printResp.text(); -// console.log("โœ… Payout print successful:", printResult); -// } catch (printErr) { -// console.error("โŒ Payout print failed:", printErr); -// } - -// this.closePayoutPopup(); - -// } catch (err) { -// console.error("โŒ Payout API call failed:", err); -// } -// } async printPayoutTicket() { console.log("๐Ÿ–จ๏ธ Print payout clicked"); this.payoutWarning = ''; @@ -492,10 +377,11 @@ Date : ${new Date().toLocaleString()} //------------------------------------PRINT DEPOSIT TICKET STARTS HERE------------------------------------------- +//------------------------------------PRINT DEPOSIT TICKET STARTS HERE------------------------------------------- +async printDeposit() { + console.log("๐Ÿ–จ๏ธ Print deposit clicked"); + this.depositWarning = ''; -printDeposit() { - const userName = localStorage.getItem('userName') || 'Unknown'; - this.depositWarning = ''; // โœ… Validation โ€” stop if any of the required fields are missing if ( !this.depositOperatorId?.toString().trim() || @@ -503,12 +389,70 @@ printDeposit() { !this.depositAmount?.toString().trim() ) { console.warn("โŒ Cannot print deposit โ€” missing required fields."); - this.depositWarning = 'โŒ Please fill Operator ID, Shroff ID, and Amount before printing.'; - return; // Stop here โ€” no print + this.depositWarning = 'โŒ Please fill Operator ID, Shroff ID, and Amount before printing.'; + return; } - // Denomination voucher section - const content = ` + const userName = localStorage.getItem('userName') || 'Unknown'; + const btid = localStorage.getItem('btid') || "0648"; // fallback if not in localStorage + const btMake = "I"; + + // Build the API payload to mirror your curl + const apiPayload = { + opCardCd: String(this.depositOperatorId).trim(), + suCardCd: String(this.depositShroffId).trim(), + amount: Number(this.depositAmount), + btId: btid, + btMake: btMake + }; + + console.log("๐Ÿ“ก Sending deposit API request:", apiPayload); + + try { + const apiUrl = "http://localhost:8088/deposit"; + const resp = await fetch(apiUrl, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(apiPayload) + }); + + if (!resp.ok) { + console.error("โŒ Deposit API HTTP error:", resp.status, resp.statusText); + this.depositWarning = `โŒ Deposit API HTTP ${resp.status}`; + return; + } + + const json = await resp.json(); + console.log("๐Ÿ“ฉ Deposit API response JSON:", json); + + // Save only the response + try { + localStorage.setItem("deposit", JSON.stringify(json)); + console.log("[localStorage] saved deposit response only"); + } catch (e) { + console.warn("โš ๏ธ Failed to save deposit to localStorage:", e); + } + + if (json.ok === false) { + console.error("โŒ Deposit API returned ok: false -", json.error || json); + this.depositWarning = `โŒ Deposit failed: ${json.error ?? 'server error'}`; + return; + } + + // Accept either nested or flat amount + const nestedAmt = (json.data && typeof json.data.amount !== 'undefined') ? json.data.amount : undefined; + const flatAmt = (typeof json.amount !== 'undefined') ? json.amount : undefined; + + if (typeof nestedAmt === 'undefined' && typeof flatAmt === 'undefined') { + console.warn("Deposit response missing amount in both expected places."); + this.depositWarning = 'โš ๏ธ Deposit incomplete โ€” server not responding correctly. Printing request amount instead.'; + } + + const backendAmount = (typeof nestedAmt !== 'undefined') ? nestedAmt : (typeof flatAmt !== 'undefined' ? flatAmt : null); + const printedAmount = backendAmount !== null ? backendAmount : Number(this.depositAmount); + + // Denomination voucher section + const content = ` ๐Ÿงพ Denomination Voucher 2000 x _____________ = _____________ 500 x _____________ = _____________ @@ -523,18 +467,19 @@ Total = ____________________________ ===================================== `; - // Full preview โ€” matches backend print style - const now = new Date(); - const date = now.toISOString().split('T')[0]; - const time = now.toTimeString().split(' ')[0]; + // Full preview โ€” matches backend print style + const now = new Date(); + const date = now.toISOString().split('T')[0]; + const time = now.toTimeString().split(' ')[0]; - const previewReceipt = ` + const previewReceipt = ` Bangalore Turf Club DEPOSIT RECEIPT ------------------------------- Operator : ${this.depositOperatorId} Shroff : ${this.depositShroffId} -Amount : โ‚น${this.depositAmount} +Requested : โ‚น${this.depositAmount} +Settled : โ‚น${printedAmount} Printed By: ${userName} Date : ${date} Time : ${time} @@ -542,36 +487,44 @@ Time : ${time} ${content} `; - console.log("๐Ÿ–จ๏ธ DEPOSIT PRINT LAYOUT:\n" + previewReceipt); + console.log("๐Ÿ–จ๏ธ DEPOSIT PRINT LAYOUT:\n" + previewReceipt); - // โœ… Send to print server - const payload = { - type: 'deposit', - printedBy: userName, - operatorId: this.depositOperatorId, - shroffId: this.depositShroffId, - amount: this.depositAmount, - content: content - }; + // โœ… Send to print server + const backendData = json.data ? json.data : { ...json }; + const payload = { + type: 'deposit', + printedBy: userName, + operatorId: this.depositOperatorId, + shroffId: this.depositShroffId, + amountRequested: Number(this.depositAmount), + amountSettled: printedAmount, + content: content, + backend: backendData + }; - 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("โœ… Deposit print successful:", result); - }) - .catch(error => { - console.error("โŒ Deposit print failed:", error); - }); + try { + const printResp = await fetch('http://localhost:9100/print', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(payload) + }); - this.closeDepositPopup(); + if (!printResp.ok) throw new Error(`Printer error: ${printResp.status}`); + const result = await printResp.text(); + console.log("โœ… Deposit print successful:", result); + } catch (printErr) { + console.error("โŒ Deposit print failed:", printErr); + this.depositWarning = 'โŒ Print failed. Check printer server.'; + } + + this.closeDepositPopup(); + + } catch (err) { + console.error("โŒ Deposit API call failed:", err); + this.depositWarning = 'โŒ Deposit API call failed. Please try again.'; + } } +//--------------------------------------PRINT DEPOSIT TICKET ENDS HERE ------------------------------------------------------------- //--------------------------------------PRINT DEPOSIT TICKETS ENDS HERE ------------------------------------------------------------- @@ -590,24 +543,105 @@ ${content} //---------------------------------------PRINT WITHDRAW STARTS HERE -------------------------------------------------------- - -printWithdraw() { - const userName = localStorage.getItem('userName') || 'Unknown'; +// component class properties (place near other field declarations) +userName = ''; +btid: string | null = null; - // Clear previous warning +withdrawBtId = ""; +withdrawBtMake = "I" ; + +// ngOnInit - ensure you already have this in your component life-cycle +ngOnInit() { + this.userName = localStorage.getItem('userName') || ''; + this.btid = localStorage.getItem('btid'); // <- pulled from localStorage like your navbar + // any other init code... +} + +//---------------------------------------PRINT WITHDRAW STARTS HERE -------------------------------------------------------- +async printWithdraw() { + console.log("๐Ÿ–จ๏ธ Print withdraw clicked"); this.withdrawWarning = ''; - // โœ… Validation + // Basic validation if ( !this.withdrawOperatorId?.toString().trim() || !this.withdrawShroffId?.toString().trim() || !this.withdrawAmount?.toString().trim() ) { + console.warn("โš ๏ธ Missing operator/shroff/amount."); this.withdrawWarning = 'โŒ Please fill Operator ID, Shroff ID, and Amount before printing.'; - return; // Stop here โ€” no print + return; } - const receiptText = ` + const userName = localStorage.getItem('userName') || this.userName || 'Unknown'; + + // Resolve btId: prefer global btid from localStorage (this.btid), fallback to withdrawBtId, then fallback literal "0648" + const resolvedBtId = (this.btid && this.btid.toString().trim()) ? this.btid.toString().trim() + : (this.withdrawBtId && this.withdrawBtId.toString().trim()) ? this.withdrawBtId.toString().trim() + : "0648"; + + // btMake is always "I" + const resolvedBtMake = "I"; + + // Build the API payload to mirror your curl + const apiPayload = { + opCardCd: String(this.withdrawOperatorId).trim(), + suCardCd: String(this.withdrawShroffId).trim(), + amount: Number(this.withdrawAmount), + btId: resolvedBtId, + btMake: resolvedBtMake + }; + + console.log("๐Ÿ“ก Sending withdraw API request:", apiPayload); + + try { + const apiUrl = "http://localhost:8088/withdraw"; + const resp = await fetch(apiUrl, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(apiPayload) + }); + + if (!resp.ok) { + console.error("โŒ Withdraw API HTTP error:", resp.status, resp.statusText); + this.withdrawWarning = `โŒ Withdraw API HTTP ${resp.status}`; + return; + } + + const json = await resp.json(); + console.log("๐Ÿ“ฉ Withdraw API response JSON:", json); + + // Persist only the response (similar to payout) + try { + localStorage.setItem("withdraw", JSON.stringify(json)); + console.log("[localStorage] saved withdraw response only"); + } catch (e) { + console.warn("โš ๏ธ Failed to save withdraw to localStorage:", e); + } + + // If backend uses ok:false semantics + if (json.ok === false) { + console.error("โŒ Withdraw API returned ok: false -", json.error || json); + this.withdrawWarning = `โŒ Withdraw failed: ${json.error ?? 'server error'}`; + return; + } + + // Accept either nested or flat numeric amount/cash fields if present + const nestedAmt = (json.data && typeof json.data.amount !== 'undefined') ? json.data.amount : undefined; + const flatAmt = (typeof json.amount !== 'undefined') ? json.amount : undefined; + + // If there's no amount at all, we still allow printing but warn the user + if (typeof nestedAmt === 'undefined' && typeof flatAmt === 'undefined') { + console.warn("Withdraw response missing amount/cash in both expected places."); + this.withdrawWarning = 'โš ๏ธ Withdraw incomplete โ€” server not responding correctly. Printing request amount instead.'; + } + + // Prefer backend provided amount if available, otherwise use user's input + const backendAmount = (typeof nestedAmt !== 'undefined') ? nestedAmt : (typeof flatAmt !== 'undefined' ? flatAmt : null); + const printedAmount = backendAmount !== null ? backendAmount : Number(this.withdrawAmount); + + // Prepare the same receipt text you used (denominations etc.) + const receiptText = ` ๐Ÿงพ Denomination Voucher 2000 x _____________ = _____________ 500 x _____________ = _____________ @@ -622,18 +656,18 @@ Total = ____________________________ ===================================== `; + const now = new Date(); + const date = now.toISOString().split('T')[0]; + const time = now.toTimeString().split(' ')[0]; - const now = new Date(); - const date = now.toISOString().split('T')[0]; - const time = now.toTimeString().split(' ')[0]; - - const previewReceipt = ` + const previewReceipt = ` Bangalore Turf Club -DEPOSIT RECEIPT +WITHDRAW RECEIPT ------------------------------- Operator : ${this.withdrawOperatorId} Shroff : ${this.withdrawShroffId} -Amount : โ‚น${this.withdrawAmount} +Requested : โ‚น${this.withdrawAmount} +Settled : โ‚น${printedAmount} Printed By: ${userName} Date : ${date} Time : ${time} @@ -641,36 +675,47 @@ Time : ${time} ${receiptText} `; - console.log(previewReceipt); + console.log(previewReceipt); - const payload = { - type: 'withdraw', - printedBy: userName, - operatorId: this.withdrawOperatorId, - shroffId: this.withdrawShroffId, - amount: this.withdrawAmount, - content: receiptText - }; + // Prepare payload for printer (include backend data for reference) + const backendData = json.data ? json.data : { ...json }; + const printData = { + type: 'withdraw', + ticketNumber: null, + printedBy: userName, + operatorId: this.withdrawOperatorId, + shroffId: this.withdrawShroffId, + amountRequested: Number(this.withdrawAmount), + amountSettled: printedAmount, + content: receiptText, + backend: backendData + }; - 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("โœ… Withdraw print successful:", result); - }) - .catch(error => { - console.error("โŒ Withdraw print failed:", error); - }); + // Send print job + try { + const printResp = await fetch('http://localhost:9100/print', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(printData) + }); - this.closeWithdrawPopup(); + if (!printResp.ok) throw new Error(`Printer HTTP ${printResp.status}`); + const printResult = await printResp.text(); + console.log("โœ… Withdraw print successful:", printResult); + } catch (printErr) { + console.error("โŒ Withdraw print failed:", printErr); + this.withdrawWarning = 'โŒ Print failed. Check printer server.'; + } + + // close withdraw modal + this.closeWithdrawPopup(); + + } catch (err) { + console.error("โŒ Withdraw API call failed:", err); + this.withdrawWarning = 'โŒ Withdraw API call failed. Please try again.'; + } } - +//----------------------------------------------PRINT WITHDRAW TICKET ENDS HERE ----------------------------- //----------------------------------------------PRINT WITHDRAW TICKET ENDS HERE -----------------------------