Coverage for hledger_lots/prompt_sell.py: 29%

52 statements  

« prev     ^ index     » next       coverage.py v7.2.3, created at 2023-05-04 22:41 -0300

1from dataclasses import dataclass 

2from typing import Optional, Tuple 

3 

4import questionary 

5 

6from . import avg, fifo, prompt 

7from .hl import hledger2txn 

8from .info import LotsInfo 

9 

10 

11@dataclass 

12class SellInfo(prompt.Tradeinfo): 

13 revenue_account: str 

14 

15 

16class PromptSell(prompt.Prompt): 

17 def __init__( 

18 self, 

19 file: Tuple[str, ...], 

20 avg_cost: bool, 

21 check: bool, 

22 no_desc: Optional[str] = None, 

23 ) -> None: 

24 super().__init__(file, avg_cost, check, no_desc) 

25 

26 print(self.initial_info) 

27 self.info = self.get_info() 

28 self.last_purchase = self.get_last_purchase(self.info) 

29 

30 def get_info(self): 

31 commodity = prompt.select_commodities_text(self.commodities) 

32 info = next(info for info in self.infos if info["comm"] == commodity) 

33 return info 

34 

35 def ask_commodity_account(self, info: LotsInfo): 

36 commodity = info["comm"] 

37 accts_txt = self.run_hledger("accounts", "note:Buy", f"cur:{commodity}") 

38 accts = [acct for acct in accts_txt.split("\n") if acct != ""] 

39 

40 answer: str = questionary.select( 

41 "Commodity Account", 

42 choices=accts, 

43 use_shortcuts=True, 

44 ).ask() 

45 return answer 

46 

47 def prompt(self): 

48 commodity = self.info["comm"] 

49 sell_date = self.ask_date(self.last_purchase) 

50 qtty = float(self.ask_sell_qtty(self.info)) 

51 price_str = self.ask_price(self.info) 

52 

53 if price_str == "": 

54 value_str = self.ask_total(qtty, self.info) 

55 value = float(value_str) 

56 price = value / qtty 

57 else: 

58 price = float(price_str) 

59 value = qtty * price 

60 

61 cash_acct = self.ask_cash_account() 

62 

63 if self.avg_cost: 

64 commodity_acct = self.ask_commodity_account(self.info) 

65 else: 

66 commodity_acct = "" 

67 

68 revenue_acct = self.ask_revenue_account() 

69 

70 result = SellInfo( 

71 date=sell_date, 

72 quantity=qtty, 

73 commodity=commodity, 

74 cash_account=cash_acct, 

75 revenue_account=revenue_acct, 

76 commodity_account=commodity_acct, 

77 price=price, 

78 value=value, 

79 ) 

80 

81 return result 

82 

83 def get_hl_txn(self): 

84 sell = self.prompt() 

85 commodity = sell.commodity 

86 adj_txns = hledger2txn(self.file, commodity, self.no_desc) 

87 

88 if self.avg_cost: 

89 txn_print = avg.avg_sell( 

90 txns=adj_txns, 

91 date=sell.date, 

92 qtty=sell.quantity, 

93 cur=sell.commodity, 

94 cash_account=sell.cash_account, 

95 revenue_account=sell.revenue_account, 

96 comm_account=sell.commodity_account, 

97 value=sell.value, 

98 check=self.check, 

99 ) 

100 else: 

101 sell_fifo = fifo.get_sell_lots( 

102 lots=adj_txns, 

103 sell_date=sell.date, 

104 sell_qtty=sell.quantity, 

105 check=self.check, 

106 ) 

107 txn_print = fifo.txn2hl( 

108 txns=sell_fifo, 

109 date=sell.date, 

110 cur=sell.commodity, 

111 cash_account=sell.cash_account, 

112 revenue_account=sell.revenue_account, 

113 value=sell.value, 

114 ) 

115 

116 return txn_print