Class: DhanHQ::Models::LedgerEntry

Inherits:
BaseModel
  • Object
show all
Defined in:
lib/DhanHQ/models/ledger_entry.rb

Overview

Model representing a single ledger entry in the Trading Account Ledger Report.

The Ledger Report contains all credit and debit transaction details for a particular time interval. Each entry represents a single transaction with details such as narration, voucher information, debit/credit amounts, and running balance.

Examples:

Fetch ledger entries for a date range

entries = DhanHQ::Models::LedgerEntry.all(
  from_date: "2024-04-01",
  to_date: "2024-04-30"
)
entries.each do |entry|
  puts "#{entry.voucherdate}: #{entry.narration} - #{entry.debit} / #{entry.credit}"
end

Calculate total credits and debits

entries = DhanHQ::Models::LedgerEntry.all(
  from_date: "2024-04-01",
  to_date: "2024-04-30"
)
total_debits = entries.sum { |e| e.debit.to_f }
total_credits = entries.sum { |e| e.credit.to_f }

Constant Summary collapse

HTTP_PATH =

Base path for ledger endpoint.

"/v2/ledger"

Constants included from ResponseHelper

ResponseHelper::STATUS_ERROR_FALLBACK

Instance Attribute Summary

Attributes inherited from BaseModel

#attributes, #errors

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseModel

api, api_type, #assign_attributes, attributes, create, #delete, #destroy, find, #id, #initialize, #new_record?, #optionchain_api?, parse_collection_response, #persisted?, resource_path, #save, #save!, #to_request_params, #update, #valid?, validate_attributes, validation_contract, #validation_contract, where

Methods included from APIHelper

#handle_response

Methods included from AttributeHelper

#camelize_keys, #inspect, #normalize_keys, #snake_case, #titleize_keys

Methods included from ValidationHelper

#valid?, #validate!, #validate_params!

Methods included from RequestHelper

#build_from_response

Constructor Details

This class inherits a constructor from DhanHQ::BaseModel

Class Method Details

.all(from_date:, to_date:) ⇒ Array<LedgerEntry>

Note:

This is a GET request with query parameters. No body required.

Note:

The ledger report represents historical data dumps and entries are returned as-is without additional validation.

Retrieves Trading Account Ledger Report entries for the specified date range.

Fetches all credit and debit transaction details for the given time interval. The ledger entries include transaction descriptions, voucher information, amounts, and running balances.

Examples:

Fetch ledger entries for a month

entries = DhanHQ::Models::LedgerEntry.all(
  from_date: "2024-04-01",
  to_date: "2024-04-30"
)
entries.each do |entry|
  puts "#{entry.voucherdate}: #{entry.narration} - Balance: #{entry.runbal}"
end

Filter entries by transaction type

entries = DhanHQ::Models::LedgerEntry.all(
  from_date: "2024-04-01",
  to_date: "2024-04-30"
)
withdrawals = entries.select { |e| e.voucherdesc == "PAYBNK" }
puts "Total withdrawals: #{withdrawals.size}"

Calculate net balance change

entries = DhanHQ::Models::LedgerEntry.all(
  from_date: "2024-04-01",
  to_date: "2024-04-30"
)
net_change = entries.sum { |e| e.credit.to_f - e.debit.to_f }
puts "Net change: ₹#{net_change}"

Parameters:

  • from_date (String)

    Start date of the ledger report in YYYY-MM-DD format. Example: "2024-04-01"

  • to_date (String)

    End date of the ledger report in YYYY-MM-DD format. Example: "2024-04-30"

Returns:

  • (Array<LedgerEntry>)

    Array of LedgerEntry objects. Returns empty array if no entries exist. Each LedgerEntry object contains (keys normalized to snake_case):

    • :dhan_client_id [String] User-specific identification generated by Dhan
    • :narration [String] Description of the ledger transaction (e.g., "FUNDS WITHDRAWAL", "CLOSING BALANCE")
    • :voucherdate [String] Transaction date in format "Mon DD, YYYY" (e.g., "Jun 22, 2022")
    • :exchange [String] Exchange information for the transaction (e.g., "NSE-CAPITAL", "NSE_CASH")
    • :voucherdesc [String] Nature of transaction (e.g., "PAYBNK", "CLOSING BALANCE", "OPENING BALANCE")
    • :vouchernumber [String] System generated transaction number. May be empty string for balance entries
    • :debit [String] Debit amount as string. Only populated when credit returns "0.00"
    • :credit [String] Credit amount as string. Only populated when debit returns "0.00"
    • :runbal [String] Running balance post transaction as string


97
98
99
100
101
102
103
104
105
# File 'lib/DhanHQ/models/ledger_entry.rb', line 97

def all(from_date:, to_date:)
  response = resource.ledger(from_date: from_date, to_date: to_date)

  return [] unless response.is_a?(Array)

  response.map do |entry|
    new(entry, skip_validation: true)
  end
end

.resourceDhanHQ::Resources::Statements

Provides a shared instance of the Statements resource.

Returns:



41
42
43
# File 'lib/DhanHQ/models/ledger_entry.rb', line 41

def resource
  @resource ||= DhanHQ::Resources::Statements.new
end

Instance Method Details

#to_hHash{Symbol => String}

Converts the LedgerEntry model attributes to a hash representation.

Useful for serialization, logging, or passing ledger entry data to other methods.

Examples:

Convert entry to hash

entry = DhanHQ::Models::LedgerEntry.all(
  from_date: "2024-04-01",
  to_date: "2024-04-30"
).first
entry_hash = entry.to_h
puts entry_hash[:narration]  # => "FUNDS WITHDRAWAL"
puts entry_hash[:runbal]      # => "957.29"

Returns:

  • (Hash{Symbol => String})

    Hash representation of the LedgerEntry model containing:

    • :dhan_client_id [String] User-specific identification
    • :narration [String] Transaction description
    • :voucherdate [String] Transaction date
    • :exchange [String] Exchange information
    • :voucherdesc [String] Nature of transaction
    • :vouchernumber [String] Transaction number
    • :debit [String] Debit amount
    • :credit [String] Credit amount
    • :runbal [String] Running balance


133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/DhanHQ/models/ledger_entry.rb', line 133

def to_h
  {
    dhan_client_id: dhan_client_id,
    narration: narration,
    voucherdate: voucherdate,
    exchange: exchange,
    voucherdesc: voucherdesc,
    vouchernumber: vouchernumber,
    debit: debit,
    credit: credit,
    runbal: runbal
  }
end