File: vaccounts.ml

package info (click to toggle)
misery 0.2-1.1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 380 kB
  • ctags: 298
  • sloc: ml: 1,295; xml: 180; makefile: 94
file content (62 lines) | stat: -rw-r--r-- 2,127 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
open Account
open Unix

let read_headers_from_channel short_name channel =
  Misc.verbose ("Reading headers from virtual account file for " ^ short_name);
  let name =
  begin
    try
      input_line channel
    with End_of_file ->
      Misc.fail (
        "Couldn't read first line (account name) from virtual account with short name `"
        ^ short_name ^ "'")
  end
  in
    (name, Variables.read_variables channel)

let load_vaccount_file short_name channel =
  let name, vars = read_headers_from_channel short_name channel in
  let acct = Account.create_virtual short_name name vars in
  let _ = Accountdbase.add_account short_name acct in
  let rec f () =
    let context = "whilst reading virtual account file `" ^ short_name ^ "'" in
    try
      let line = input_line channel in
      let name, negate =
        if String.length line > 0 && String.get line 0 = '-'
        then (String.sub line 1 ((String.length line) - 1)), true
        else line, false
      in
      let acct = Accountdbase.lookup_account name context in
      let total = Sumofmoney.default_to_normal (Account.total acct) in
      let tm = Unix.localtime (Unix.time ()) in
      let total_str = if negate then "Negated total" else "Total" in
      let txn = { year = tm.tm_year + 1900;
                  month = tm.tm_mon + 1;
		  day = tm.tm_mday;
		  amount = if not negate then total
                           else Sumofmoney.negate total;
		  creditor = "phantom";
		  description = total_str ^ " of account " ^
		                (Account.full_name acct);
                  automatically_added = true;
                  linked = false;
		  do_not_symmetrise = true }
      in
        Accountdbase.add_txn short_name txn context;
	f ()
    with End_of_file -> ()
  in
    f ()

let process () =
  Misc.verbose "Processing virtual accounts.";
  Misc.iter_files "vaccounts" load_vaccount_file;
  Misc.verbose "Processing of virtual accounts done."

let process_late () =
  Misc.verbose "Processing late virtual accounts.";
  Misc.iter_files "vaccounts_late" load_vaccount_file;
  Misc.verbose "Processing of late virtual accounts done."