journal: commodity format directive overrides inferred style (#295)
The commodity directive's format subdirective can now be used to override the inferred style for a commodity, eg to increase or decrease the precision. This doesn't fix the root cause of #295 but is at least a good workaround.
This commit is contained in:
		
							parent
							
								
									2c0ef877eb
								
							
						
					
					
						commit
						9304aa1100
					
				| @ -521,12 +521,14 @@ journalBalanceTransactions j@Journal{jtxns=ts, jcommoditystyles=ss} = | |||||||
|                                     Left e    -> Left e |                                     Left e    -> Left e | ||||||
|       where balance = balanceTransaction (Just ss) |       where balance = balanceTransaction (Just ss) | ||||||
| 
 | 
 | ||||||
| -- | Choose standard display formats for all commodities, and | -- | Choose and apply a consistent display format to the posting | ||||||
| -- adjust all the journal's posting amount styles to use them. | -- amounts in each commodity. Each commodity's format is specified by | ||||||
|  | -- a commodity format directive, or otherwise inferred from posting | ||||||
|  | -- amounts as in hledger < 0.28. | ||||||
| journalApplyCommodityStyles :: Journal -> Journal | journalApplyCommodityStyles :: Journal -> Journal | ||||||
| journalApplyCommodityStyles j@Journal{jtxns=ts, jmarketprices=mps} = j'' | journalApplyCommodityStyles j@Journal{jtxns=ts, jmarketprices=mps} = j'' | ||||||
|     where |     where | ||||||
|       j' = journalChooseCommodityStyles j |       j' = journalInferCommodityStyles j | ||||||
|       j'' = j'{jtxns=map fixtransaction ts, jmarketprices=map fixmarketprice mps} |       j'' = j'{jtxns=map fixtransaction ts, jmarketprices=map fixmarketprice mps} | ||||||
|       fixtransaction t@Transaction{tpostings=ps} = t{tpostings=map fixposting ps} |       fixtransaction t@Transaction{tpostings=ps} = t{tpostings=map fixposting ps} | ||||||
|       fixposting p@Posting{pamount=a} = p{pamount=fixmixedamount a} |       fixposting p@Posting{pamount=a} = p{pamount=fixmixedamount a} | ||||||
| @ -534,19 +536,24 @@ journalApplyCommodityStyles j@Journal{jtxns=ts, jmarketprices=mps} = j'' | |||||||
|       fixmixedamount (Mixed as) = Mixed $ map fixamount as |       fixmixedamount (Mixed as) = Mixed $ map fixamount as | ||||||
|       fixamount a@Amount{acommodity=c} = a{astyle=journalCommodityStyle j' c} |       fixamount a@Amount{acommodity=c} = a{astyle=journalCommodityStyle j' c} | ||||||
| 
 | 
 | ||||||
| -- | Get this journal's standard display style for the given commodity, or the null style. | -- | Get this journal's standard display style for the given | ||||||
|  | -- commodity.  That is the style defined by the last corresponding | ||||||
|  | -- commodity format directive if any, otherwise the style inferred | ||||||
|  | -- from the posting amounts (or in some cases, price amounts) in this | ||||||
|  | -- commodity if any, otherwise the default style. | ||||||
| journalCommodityStyle :: Journal -> CommoditySymbol -> AmountStyle | journalCommodityStyle :: Journal -> CommoditySymbol -> AmountStyle | ||||||
| journalCommodityStyle j c = M.findWithDefault amountstyle c $ jcommoditystyles j | journalCommodityStyle j c = | ||||||
|  |   headDef amountstyle{asprecision=2} $ | ||||||
|  |   catMaybes [ | ||||||
|  |      M.lookup c (jcommodities j) >>= cformat | ||||||
|  |     ,M.lookup c $ jcommoditystyles j | ||||||
|  |     ] | ||||||
| 
 | 
 | ||||||
| -- | Choose a standard display style for each commodity. | -- | Infer a display format for each commodity based on the amounts parsed. | ||||||
| -- "hledger... will use the format of the first posting amount in the | -- "hledger... will use the format of the first posting amount in the | ||||||
| -- commodity, and the highest precision of all posting amounts in the commodity." | -- commodity, and the highest precision of all posting amounts in the commodity." | ||||||
| -- | journalInferCommodityStyles :: Journal -> Journal | ||||||
| -- (In user docs, we may now be calling this "format" for consistency with | journalInferCommodityStyles j = | ||||||
| -- the commodity directive's format keyword; in code, it's mostly "style"). |  | ||||||
| -- |  | ||||||
| journalChooseCommodityStyles :: Journal -> Journal |  | ||||||
| journalChooseCommodityStyles j = |  | ||||||
|   j{jcommoditystyles = |   j{jcommoditystyles = | ||||||
|         commodityStylesFromAmounts $ |         commodityStylesFromAmounts $ | ||||||
|         dbg8 "journalChooseCommmodityStyles using amounts" $ journalAmounts j} |         dbg8 "journalChooseCommmodityStyles using amounts" $ journalAmounts j} | ||||||
|  | |||||||
| @ -1,11 +1,16 @@ | |||||||
| # WIP see doc/mockups/commodity-styles.txt |  | ||||||
| 
 |  | ||||||
| # 1. The four decimal places of the market price should not alter the | # 1. The four decimal places of the market price should not alter the | ||||||
| # canonical style of two decimal places in a's converted-to-B balance | # canonical style of two decimal places in a's converted-to-B balance | ||||||
| # (#295). | # (#295), but it turns out they do with -V. This can be fixed with a | ||||||
|  | # commodity directive. | ||||||
| hledger -f- bal -V -N | hledger -f- bal -V -N | ||||||
| <<< | <<< | ||||||
|  | ; use a commodity directive to ensure B is displayed with two decimal | ||||||
|  | ; places, or the price directive plus -V would make it four | ||||||
|  | commodity B | ||||||
|  |   format 1.00 B | ||||||
|  | 
 | ||||||
| P 2015/1/1 A 1.0001 B | P 2015/1/1 A 1.0001 B | ||||||
|  | 
 | ||||||
| 2015/1/1 | 2015/1/1 | ||||||
|     (a)  1.00 A |     (a)  1.00 A | ||||||
|     (b)  1.00 B |     (b)  1.00 B | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user