balance: multicolumn now includes all postings in the displayed periods
This commit is contained in:
		
							parent
							
								
									1fca3e7472
								
							
						
					
					
						commit
						6743f8c2a1
					
				@ -18,6 +18,7 @@ where
 | 
				
			|||||||
import Data.List
 | 
					import Data.List
 | 
				
			||||||
import Data.Maybe
 | 
					import Data.Maybe
 | 
				
			||||||
import Data.Ord
 | 
					import Data.Ord
 | 
				
			||||||
 | 
					import Safe
 | 
				
			||||||
-- import Test.HUnit
 | 
					-- import Test.HUnit
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import Hledger.Data
 | 
					import Hledger.Data
 | 
				
			||||||
@ -64,7 +65,7 @@ type ClippedAccountName = AccountName
 | 
				
			|||||||
-- showing the change of balance, accumulated balance, or historical balance
 | 
					-- showing the change of balance, accumulated balance, or historical balance
 | 
				
			||||||
-- in each of the specified periods.
 | 
					-- in each of the specified periods.
 | 
				
			||||||
multiBalanceReport :: ReportOpts -> Query -> Journal -> MultiBalanceReport
 | 
					multiBalanceReport :: ReportOpts -> Query -> Journal -> MultiBalanceReport
 | 
				
			||||||
multiBalanceReport opts q j = MultiBalanceReport (spans, items, totals)
 | 
					multiBalanceReport opts q j = MultiBalanceReport (displayspans, items, totals)
 | 
				
			||||||
    where
 | 
					    where
 | 
				
			||||||
      -- dbg = const id                                   -- exclude from debug output
 | 
					      -- dbg = const id                                   -- exclude from debug output
 | 
				
			||||||
      dbg s = let p = "multiBalanceReport" in Hledger.Utils.dbg (p++" "++s)  -- add prefix in debug output
 | 
					      dbg s = let p = "multiBalanceReport" in Hledger.Utils.dbg (p++" "++s)  -- add prefix in debug output
 | 
				
			||||||
@ -75,7 +76,12 @@ multiBalanceReport opts q j = MultiBalanceReport (spans, items, totals)
 | 
				
			|||||||
      depthless  = dbg "depthless" . filterQuery (not . queryIsDepth)
 | 
					      depthless  = dbg "depthless" . filterQuery (not . queryIsDepth)
 | 
				
			||||||
      datelessq  = dbg "datelessq"  $ filterQuery (not . queryIsDate) q
 | 
					      datelessq  = dbg "datelessq"  $ filterQuery (not . queryIsDate) q
 | 
				
			||||||
      precedingq = dbg "precedingq" $ And [datelessq, Date $ DateSpan Nothing (spanStart reportspan)]
 | 
					      precedingq = dbg "precedingq" $ And [datelessq, Date $ DateSpan Nothing (spanStart reportspan)]
 | 
				
			||||||
      reportq    = dbg "reportq"    $ depthless q --  $ And [datelessq, Date reportspan] -- laziness at work -- XXX no good, works only in GHCI
 | 
					      requestedspan  = dbg "requestedspan"  $ queryDateSpan (date2_ opts) q                              -- span specified by -b/-e/-p options and query args
 | 
				
			||||||
 | 
					      requestedspan' = dbg "requestedspan'" $ requestedspan `spanDefaultsFrom` journalDateSpan j         -- if open-ended, close it using the journal's end dates
 | 
				
			||||||
 | 
					      intervalspans  = dbg "intervalspans"  $ splitSpan (intervalFromOpts opts) requestedspan'           -- interval spans enclosing it
 | 
				
			||||||
 | 
					      reportspan     = dbg "reportspan"     $ DateSpan (maybe Nothing spanStart $ headMay intervalspans) -- the requested span enlarged to a whole number of intervals
 | 
				
			||||||
 | 
					                                                       (maybe Nothing spanEnd   $ lastMay intervalspans)
 | 
				
			||||||
 | 
					      reportq    = dbg "reportq" $ depthless $ And [datelessq, Date reportspan] -- user's query enlarged to whole intervals and with no depth limit
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      ps :: [Posting] =
 | 
					      ps :: [Posting] =
 | 
				
			||||||
          dbg "ps" $
 | 
					          dbg "ps" $
 | 
				
			||||||
@ -84,11 +90,16 @@ multiBalanceReport opts q j = MultiBalanceReport (spans, items, totals)
 | 
				
			|||||||
          filterJournalPostings reportq $        -- remove postings not matched by (adjusted) query
 | 
					          filterJournalPostings reportq $        -- remove postings not matched by (adjusted) query
 | 
				
			||||||
          journalSelectingAmountFromOpts opts j
 | 
					          journalSelectingAmountFromOpts opts j
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      (reportspan, spans) = dbg "report spans" $ reportSpans opts q j ps
 | 
					      displayspans = dbg "displayspans" $ splitSpan (intervalFromOpts opts) displayspan
 | 
				
			||||||
 | 
					        where
 | 
				
			||||||
 | 
					          displayspan
 | 
				
			||||||
 | 
					            | empty_ opts = dbg "displayspan (-E)" $ reportspan                                -- all the requested intervals
 | 
				
			||||||
 | 
					            | otherwise   = dbg "displayspan"      $ requestedspan `spanIntersect` matchedspan -- exclude leading/trailing empty intervals
 | 
				
			||||||
 | 
					          matchedspan = dbg "matchedspan" $ postingsDateSpan' (whichDateFromOpts opts) ps
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      psPerSpan :: [[Posting]] =
 | 
					      psPerSpan :: [[Posting]] =
 | 
				
			||||||
          dbg "psPerSpan" $
 | 
					          dbg "psPerSpan" $
 | 
				
			||||||
          [filter (isPostingInDateSpan' (whichDateFromOpts opts) s) ps | s <- spans]
 | 
					          [filter (isPostingInDateSpan' (whichDateFromOpts opts) s) ps | s <- displayspans]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      postedAcctBalChangesPerSpan :: [[(ClippedAccountName, MixedAmount)]] =
 | 
					      postedAcctBalChangesPerSpan :: [[(ClippedAccountName, MixedAmount)]] =
 | 
				
			||||||
          dbg "postedAcctBalChangesPerSpan" $
 | 
					          dbg "postedAcctBalChangesPerSpan" $
 | 
				
			||||||
 | 
				
			|||||||
@ -19,7 +19,6 @@ module Hledger.Reports.ReportOptions (
 | 
				
			|||||||
  queryFromOpts,
 | 
					  queryFromOpts,
 | 
				
			||||||
  queryFromOptsOnly,
 | 
					  queryFromOptsOnly,
 | 
				
			||||||
  queryOptsFromOpts,
 | 
					  queryOptsFromOpts,
 | 
				
			||||||
  reportSpans,
 | 
					 | 
				
			||||||
  transactionDateFn,
 | 
					  transactionDateFn,
 | 
				
			||||||
  postingDateFn,
 | 
					  postingDateFn,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -290,45 +289,6 @@ tests_queryOptsFromOpts = [
 | 
				
			|||||||
                                                             })
 | 
					                                                             })
 | 
				
			||||||
 ]
 | 
					 ]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
-- | Calculate the overall and (if a reporting interval is specified)
 | 
					 | 
				
			||||||
-- per-interval date spans for a report, based on command-line
 | 
					 | 
				
			||||||
-- options, the search query, and the journal data.
 | 
					 | 
				
			||||||
--
 | 
					 | 
				
			||||||
-- The basic overall report span is:
 | 
					 | 
				
			||||||
-- without -E: the intersection of the requested span and the matched data's span.
 | 
					 | 
				
			||||||
-- with -E:    the full requested span, including leading/trailing intervals that are empty.
 | 
					 | 
				
			||||||
--             If the begin or end date is not specified, those of the journal are used.
 | 
					 | 
				
			||||||
--
 | 
					 | 
				
			||||||
-- The report span will be enlarged if necessary to include a whole
 | 
					 | 
				
			||||||
-- number of report periods, when a reporting interval is specified.
 | 
					 | 
				
			||||||
--
 | 
					 | 
				
			||||||
reportSpans ::  ReportOpts -> Query -> Journal -> [Posting] -> (DateSpan, [DateSpan])
 | 
					 | 
				
			||||||
reportSpans opts q j matchedps = (reportspan, spans)
 | 
					 | 
				
			||||||
  where
 | 
					 | 
				
			||||||
    (reportspan, spans)
 | 
					 | 
				
			||||||
      | empty_ opts = (dbg "reportspan1" $ enlargedrequestedspan, sps)
 | 
					 | 
				
			||||||
      | otherwise   = (dbg "reportspan2" $ requestedspan `spanIntersect` matchedspan, sps)
 | 
					 | 
				
			||||||
      where
 | 
					 | 
				
			||||||
        sps = dbg "spans" $ splitSpan (intervalFromOpts opts) reportspan
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    -- the requested span specified by the query (-b/-e/-p options and query args);
 | 
					 | 
				
			||||||
    -- might be open-ended
 | 
					 | 
				
			||||||
    requestedspan = dbg "requestedspan" $ queryDateSpan (date2_ opts) q
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    -- the requested span with unspecified dates filled from the journal data
 | 
					 | 
				
			||||||
    finiterequestedspan = dbg "finiterequestedspan" $ requestedspan `orDatesFrom` journalDateSpan j
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    -- enlarge the requested span to a whole number of periods
 | 
					 | 
				
			||||||
    enlargedrequestedspan = dbg "enlargedrequestedspan" $
 | 
					 | 
				
			||||||
                            DateSpan (maybe Nothing spanStart $ headMay requestedspans)
 | 
					 | 
				
			||||||
                                     (maybe Nothing spanEnd   $ lastMay requestedspans)
 | 
					 | 
				
			||||||
      where
 | 
					 | 
				
			||||||
        -- spans of the requested report interval which enclose the requested span
 | 
					 | 
				
			||||||
        requestedspans = dbg "requestedspans" $ splitSpan (intervalFromOpts opts) finiterequestedspan
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    -- earliest and latest dates of matched postings
 | 
					 | 
				
			||||||
    matchedspan = dbg "matchedspan" $ postingsDateSpan' (whichDateFromOpts opts) matchedps
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
tests_Hledger_Reports_ReportOptions :: Test
 | 
					tests_Hledger_Reports_ReportOptions :: Test
 | 
				
			||||||
tests_Hledger_Reports_ReportOptions = TestList $
 | 
					tests_Hledger_Reports_ReportOptions = TestList $
 | 
				
			||||||
 | 
				
			|||||||
@ -257,3 +257,28 @@ Change of balance (flow):
 | 
				
			|||||||
   ||                      2 
 | 
					   ||                      2 
 | 
				
			||||||
 | 
					
 | 
				
			||||||
>>>=0
 | 
					>>>=0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# 18. All matched postings in the displayed intervals should be reported on.
 | 
				
			||||||
 | 
					hledgerdev -f- balance -p 'monthly 2014/1/10-2014/2/20'
 | 
				
			||||||
 | 
					<<<
 | 
				
			||||||
 | 
					2014/1/5
 | 
				
			||||||
 | 
					 (before)  1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					2014/2/1
 | 
				
			||||||
 | 
					 (within)  1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					2014/2/25
 | 
				
			||||||
 | 
					 (after)  1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					>>>
 | 
				
			||||||
 | 
					Change of balance (flow):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        ||  2014/01/01-2014/01/31  2014/02/01-2014/02/28 
 | 
				
			||||||
 | 
					========++===============================================
 | 
				
			||||||
 | 
					 after  ||                      0                      1 
 | 
				
			||||||
 | 
					 before ||                      1                      0 
 | 
				
			||||||
 | 
					 within ||                      0                      1 
 | 
				
			||||||
 | 
					--------++-----------------------------------------------
 | 
				
			||||||
 | 
					        ||                      1                      2 
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					>>>=0
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user