Refactors lightning account naming
Standardizes lightning account name to "Assets:Bitcoin:Lightning" for consistency. Updates the database and code to reflect this change, ensuring that payment processing and account management use the new name. Prevents polling of receivable dialog after settlement.
This commit is contained in:
parent
8a961e1331
commit
49f21da55a
3 changed files with 61 additions and 31 deletions
|
|
@ -217,7 +217,7 @@ async def m006_hierarchical_account_names(db):
|
||||||
# Assets
|
# Assets
|
||||||
"cash": "Assets:Cash",
|
"cash": "Assets:Cash",
|
||||||
"bank": "Assets:Bank",
|
"bank": "Assets:Bank",
|
||||||
"lightning": "Assets:Lightning:Balance",
|
"lightning": "Assets:Bitcoin:Lightning",
|
||||||
"accounts_receivable": "Assets:Receivable",
|
"accounts_receivable": "Assets:Receivable",
|
||||||
|
|
||||||
# Liabilities
|
# Liabilities
|
||||||
|
|
@ -318,3 +318,17 @@ async def m007_balance_assertions(db):
|
||||||
CREATE INDEX idx_balance_assertions_date ON balance_assertions (date);
|
CREATE INDEX idx_balance_assertions_date ON balance_assertions (date);
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def m008_rename_lightning_account(db):
|
||||||
|
"""
|
||||||
|
Rename Lightning account from Assets:Lightning:Balance to Assets:Bitcoin:Lightning
|
||||||
|
for better naming consistency.
|
||||||
|
"""
|
||||||
|
await db.execute(
|
||||||
|
"""
|
||||||
|
UPDATE accounts
|
||||||
|
SET name = 'Assets:Bitcoin:Lightning'
|
||||||
|
WHERE name = 'Assets:Lightning:Balance'
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
|
||||||
|
|
@ -958,10 +958,14 @@ window.app = Vue.createApp({
|
||||||
)
|
)
|
||||||
|
|
||||||
if (response.data && response.data.paid) {
|
if (response.data && response.data.paid) {
|
||||||
|
// Stop polling immediately
|
||||||
|
clearInterval(this.settleReceivableDialog.pollIntervalId)
|
||||||
|
this.settleReceivableDialog.pollIntervalId = null
|
||||||
|
|
||||||
// Record payment in accounting - this creates the journal entry
|
// Record payment in accounting - this creates the journal entry
|
||||||
// that settles the receivable
|
// that settles the receivable
|
||||||
try {
|
try {
|
||||||
await LNbits.api.request(
|
const recordResponse = await LNbits.api.request(
|
||||||
'POST',
|
'POST',
|
||||||
'/castle/api/v1/record-payment',
|
'/castle/api/v1/record-payment',
|
||||||
this.g.user.wallets[0].adminkey,
|
this.g.user.wallets[0].adminkey,
|
||||||
|
|
@ -969,9 +973,7 @@ window.app = Vue.createApp({
|
||||||
payment_hash: paymentHash
|
payment_hash: paymentHash
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
} catch (error) {
|
console.log('Settlement payment recorded:', recordResponse.data)
|
||||||
console.error('Error recording settlement payment:', error)
|
|
||||||
}
|
|
||||||
|
|
||||||
this.$q.notify({
|
this.$q.notify({
|
||||||
type: 'positive',
|
type: 'positive',
|
||||||
|
|
@ -984,6 +986,14 @@ window.app = Vue.createApp({
|
||||||
await this.loadBalance()
|
await this.loadBalance()
|
||||||
await this.loadTransactions()
|
await this.loadTransactions()
|
||||||
await this.loadAllUserBalances()
|
await this.loadAllUserBalances()
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error recording settlement payment:', error)
|
||||||
|
this.$q.notify({
|
||||||
|
type: 'negative',
|
||||||
|
message: 'Payment detected but failed to record: ' + (error.response?.data?.detail || error.message),
|
||||||
|
timeout: 5000
|
||||||
|
})
|
||||||
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
|
|
||||||
40
views_api.py
40
views_api.py
|
|
@ -623,25 +623,31 @@ async def api_record_payment(
|
||||||
"""
|
"""
|
||||||
from lnbits.core.crud.payments import get_standalone_payment
|
from lnbits.core.crud.payments import get_standalone_payment
|
||||||
|
|
||||||
# Get the payment details
|
# Get the payment details (incoming=True to get the invoice, not the payment)
|
||||||
payment = await get_standalone_payment(data.payment_hash)
|
payment = await get_standalone_payment(data.payment_hash, incoming=True)
|
||||||
if not payment:
|
if not payment:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=HTTPStatus.NOT_FOUND, detail="Payment not found"
|
status_code=HTTPStatus.NOT_FOUND, detail="Payment not found"
|
||||||
)
|
)
|
||||||
|
|
||||||
if not payment.paid:
|
if payment.pending:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=HTTPStatus.BAD_REQUEST, detail="Payment not yet paid"
|
status_code=HTTPStatus.BAD_REQUEST, detail="Payment not yet settled"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Get user_id from payment metadata (set during invoice generation)
|
# Get user_id from payment metadata (set during invoice generation)
|
||||||
# This allows admins to record payments on behalf of users
|
target_user_id = None
|
||||||
target_user_id = wallet.wallet.user # Default to wallet user
|
|
||||||
if payment.extra and isinstance(payment.extra, dict):
|
if payment.extra and isinstance(payment.extra, dict):
|
||||||
metadata_user_id = payment.extra.get("user_id")
|
target_user_id = payment.extra.get("user_id")
|
||||||
if metadata_user_id:
|
|
||||||
target_user_id = metadata_user_id
|
if not target_user_id:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=HTTPStatus.BAD_REQUEST,
|
||||||
|
detail="Payment metadata missing user_id. Cannot determine which user to credit.",
|
||||||
|
)
|
||||||
|
|
||||||
|
# Convert amount from millisatoshis to satoshis
|
||||||
|
amount_sats = payment.amount // 1000
|
||||||
|
|
||||||
# Get user's receivable account (what user owes)
|
# Get user's receivable account (what user owes)
|
||||||
user_receivable = await get_or_create_user_account(
|
user_receivable = await get_or_create_user_account(
|
||||||
|
|
@ -649,14 +655,14 @@ async def api_record_payment(
|
||||||
)
|
)
|
||||||
|
|
||||||
# Get lightning account
|
# Get lightning account
|
||||||
lightning_account = await get_account_by_name("Lightning Balance")
|
lightning_account = await get_account_by_name("Assets:Bitcoin:Lightning")
|
||||||
if not lightning_account:
|
if not lightning_account:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=HTTPStatus.NOT_FOUND, detail="Lightning account not found"
|
status_code=HTTPStatus.NOT_FOUND, detail="Lightning account not found"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Create journal entry to record payment
|
# Create journal entry to record payment
|
||||||
# DR Lightning Balance, CR Accounts Receivable (User)
|
# DR Assets:Bitcoin:Lightning, CR Assets:Receivable (User)
|
||||||
# This reduces what the user owes
|
# This reduces what the user owes
|
||||||
|
|
||||||
# Add meta information for audit trail
|
# Add meta information for audit trail
|
||||||
|
|
@ -675,14 +681,14 @@ async def api_record_payment(
|
||||||
lines=[
|
lines=[
|
||||||
CreateEntryLine(
|
CreateEntryLine(
|
||||||
account_id=lightning_account.id,
|
account_id=lightning_account.id,
|
||||||
debit=payment.amount,
|
debit=amount_sats,
|
||||||
credit=0,
|
credit=0,
|
||||||
description="Lightning payment received",
|
description="Lightning payment received",
|
||||||
),
|
),
|
||||||
CreateEntryLine(
|
CreateEntryLine(
|
||||||
account_id=user_receivable.id,
|
account_id=user_receivable.id,
|
||||||
debit=0,
|
debit=0,
|
||||||
credit=payment.amount,
|
credit=amount_sats,
|
||||||
description="Payment applied to balance",
|
description="Payment applied to balance",
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
|
@ -716,14 +722,14 @@ async def api_pay_user(
|
||||||
)
|
)
|
||||||
|
|
||||||
# Get lightning account
|
# Get lightning account
|
||||||
lightning_account = await get_account_by_name("Lightning Balance")
|
lightning_account = await get_account_by_name("Assets:Bitcoin:Lightning")
|
||||||
if not lightning_account:
|
if not lightning_account:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=HTTPStatus.NOT_FOUND, detail="Lightning account not found"
|
status_code=HTTPStatus.NOT_FOUND, detail="Lightning account not found"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Create journal entry
|
# Create journal entry
|
||||||
# DR Accounts Payable (User), CR Lightning Balance
|
# DR Liabilities:Payable (User), CR Assets:Bitcoin:Lightning
|
||||||
entry_data = CreateJournalEntry(
|
entry_data = CreateJournalEntry(
|
||||||
description=f"Payment to user {user_id[:8]}",
|
description=f"Payment to user {user_id[:8]}",
|
||||||
lines=[
|
lines=[
|
||||||
|
|
@ -1044,11 +1050,11 @@ async def api_approve_manual_payment_request(
|
||||||
)
|
)
|
||||||
|
|
||||||
# Get the Lightning asset account
|
# Get the Lightning asset account
|
||||||
lightning_account = await get_account_by_name("Lightning Balance")
|
lightning_account = await get_account_by_name("Assets:Bitcoin:Lightning")
|
||||||
if not lightning_account:
|
if not lightning_account:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=HTTPStatus.NOT_FOUND,
|
status_code=HTTPStatus.NOT_FOUND,
|
||||||
detail="Lightning Balance account not found",
|
detail="Lightning account not found",
|
||||||
)
|
)
|
||||||
|
|
||||||
# Create journal entry: Debit Lightning (asset decreased), Credit Accounts Payable (liability increased)
|
# Create journal entry: Debit Lightning (asset decreased), Credit Accounts Payable (liability increased)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue