2c3646df6d50f90b55bde08c6be848f19f620d1c
[outofuni/gocash.git] / gocash.go
1 package main
2
3 import (
4         "encoding/xml"
5         "fmt"
6         "io/ioutil"
7         "os"
8         "strings"
9         "strconv"
10 )
11
12 //
13 // hardcoded account ids we have to look at
14 //
15 // --- buy
16 // wareneingang 19% and 7%
17 const pid_buy_n = string("8e3b7c42e3173ed85f3d4736e82afb4d")
18 const pid_buy_s = string("0cfd2ceb45fff89b9d1b7ce3af66cdf3")
19 const pid_misc  = string("e3acc2865dbf931e41cf2b90240de5c2")
20 const pid_rep   = string("b1d04ad157cac569f4299d4ddf94ed6f")
21 const pid_room  = string("4394ed4ffa7266f8f8731080926a7a61")
22 const pid_cap   = string("4196ee026d1bdb785df2c975fca91ae0")
23 const aid_werbe = string("cb67d346eac01c2b66e2394df4e8d6e8")
24 // abziehbare vst 19% and 7%
25 const aid_vst_n = string("7c449e13125d6b93043f963628106db2")
26 const aid_vst_s = string("006643c1c0a91f2b40614c75a49c6295")
27 // --- sales
28 // receipts
29 const aid_rec_n = string("f3e905732b729ba096a50dab60559ce7")
30 const aid_rec_s = string("66c1b04bd897766cb2be538094e1db6a")
31 const aid_tip   = string("1d20024badc11a99a8e1cf3a9a64a501")
32 const aid_dep   = string("9772f4e231f6f5e3100132cc53eb3447")
33 // ust
34 const aid_ust_n = string("e4bd6ff52408be8076f24aeb105893d9")
35 const aid_ust_s = string("38bf40d16529f2a1e611c073c6c1dc9c")
36
37 type inv_accnts struct {
38         id string
39         taxval int
40         tax bool
41         buy bool
42 }
43
44 // make these account data the only one, above p/aids redundant!
45 // we have all the information in here!
46
47 var iaa []inv_accnts = []inv_accnts{
48 // wareneingang 19% and 7% (note: pids!)
49  { "8e3b7c42e3173ed85f3d4736e82afb4d",19,false,true },
50  { "0cfd2ceb45fff89b9d1b7ce3af66cdf3", 7,false,true },
51  { "e3acc2865dbf931e41cf2b90240de5c2",19,false,true },
52  { "b1d04ad157cac569f4299d4ddf94ed6f",19,false,true },
53  { "4394ed4ffa7266f8f8731080926a7a61",19,false,true },
54  { "4196ee026d1bdb785df2c975fca91ae0",19,false,true },
55 // aids ...
56  { "cb67d346eac01c2b66e2394df4e8d6e8",19,false,true },
57 // abziehbare vst 19% and 7%
58  { "7c449e13125d6b93043f963628106db2",19,true,true },
59  { "006643c1c0a91f2b40614c75a49c6295", 7,true,true },
60 // --- sales
61 // receipts
62  { "f3e905732b729ba096a50dab60559ce7",19,false,false },
63  { "66c1b04bd897766cb2be538094e1db6a", 7,false,false },
64  { "1d20024badc11a99a8e1cf3a9a64a501",19,false,false },
65  { "9772f4e231f6f5e3100132cc53eb3447",19,false,false },
66 // ust
67  { "e4bd6ff52408be8076f24aeb105893d9",19,true,false },
68  { "38bf40d16529f2a1e611c073c6c1dc9c", 7,true,false },
69 }
70
71 // transacion exception list
72 var trn_exc = []string{
73         "GEMA",
74         "Deutsche Post",
75         "gesetz IHK",
76         "Gesundheitsbelehrung",
77         "Gewerbezentralregister",
78         "Entgeltabrechnung siehe Anlage",
79         "ENTGELT SPK",
80         "ttenrecht und F",
81         "Unterrichtung Gastst",
82 }
83
84 // account exception list
85 var account_exc = []string{
86         "4970 Nebenkosten des",
87 }
88
89 // account maps
90 type amap struct {
91         pid string // parent id
92         num int // account number
93         taxval int // 7 or 19
94         buy bool // buy or sales
95         tax bool // tax or non-tax(=goods) account
96         rid []string // required transaction account(s)
97 }
98
99 // xml
100 type Account struct {
101         XMLName xml.Name `xml:"account"`
102         Name string `xml:"name"`
103         AccountId string `xml:"id"`
104         ParentId string `xml:"parent"`
105 }
106 type Split struct {
107         XMLName xml.Name `xml:"split"`
108         Id string `xml:"id"`
109         Value string `xml:"value"`
110         Quantity string `xml:"quantity"`
111         AccountId string `xml:"account"`
112 }
113 type Transaction struct {
114         XMLName xml.Name `xml:"transaction"`
115         Id string `xml:"id"`
116         Date string `xml:"date-posted>date"`
117         Description string `xml:"description"`
118         Spl []Split `xml:"splits>split"`
119 }
120 type ParsedData struct {
121         XMLName xml.Name `xml:"gnc-v2"`
122         DataCnt []string `xml:"count-data"`
123         Accnt []Account `xml:"book>account"`
124         Trn []Transaction `xml:"book>transaction"`
125 }
126
127 // 'global' data
128 var data ParsedData
129
130 func main() {
131
132         // argv
133         sel_date := ""
134         if len(os.Args) > 1 {
135                 sel_date = os.Args[1]
136         }
137
138         // open xml file
139         file, err := os.Open("c13_skr03.gnucash")
140         if err != nil {
141                 fmt.Println("Error opening file:", err)
142                 return
143         }
144         defer file.Close()
145
146         // read xml file
147         xmldata, err := ioutil.ReadAll(file)
148         if err != nil {
149                 fmt.Println("Error reading file:", err)
150                 return
151         }
152
153         // unmarshal xml data
154         err = xml.Unmarshal(xmldata,&data)
155         if err != nil {
156                 fmt.Println("Error unmarshaling xml data:", err)
157                 return
158         }
159
160         // whooha, this is our data!
161         fmt.Println("Parsed accounts:",len(data.Accnt))
162         fmt.Println("Parsed transactions:",len(data.Trn))
163         fmt.Println("")
164
165         accnt := make(map[string]amap)
166
167         for ac := range data.Accnt {
168                 aid := data.Accnt[ac].AccountId
169                 pid := data.Accnt[ac].ParentId
170                 // general map
171                 rid := make ([]string,10,10)
172                 accnt[aid]=amap{
173                         pid,ac,0,false,false,rid,
174                 }
175                 rid[0]="NONE"
176                 tmp := accnt[aid]
177                 switch {
178                 // ---- buy
179                 //   -- goods
180                 case pid == pid_buy_n || pid == pid_misc || pid == pid_rep || pid == pid_room || pid == pid_cap || aid == aid_werbe:
181                         tmp.taxval=19
182                         tmp.buy=true
183                         rid[0]=aid_vst_n
184                         accnt[aid]=tmp
185                 case pid == pid_buy_s:
186                         tmp.taxval=7
187                         tmp.buy=true
188                         rid[0]=aid_vst_s
189                         accnt[aid]=tmp
190                 //   -- tax
191                 case aid == aid_vst_n:
192                         tmp.taxval=19
193                         tmp.buy=true
194                         tmp.tax=true
195                         rid=[]string{pid_buy_n,pid_misc,pid_rep,pid_room,pid_cap,aid_werbe,}
196                         accnt[aid]=tmp
197                 case aid == aid_vst_s:
198                         tmp.taxval=7
199                         tmp.buy=true
200                         tmp.tax=true
201                         rid[0]=pid_buy_s
202                         accnt[aid]=tmp
203                 // ---- sales ----
204                 //   -- receipts
205                 case aid == aid_rec_n || aid == aid_tip || aid == aid_dep:
206                         tmp.taxval=19
207                         rid[0]=aid_ust_n
208                         accnt[aid]=tmp
209                 case aid == aid_rec_s:
210                         tmp.taxval=7
211                         rid[0]=aid_ust_s
212                         accnt[aid]=tmp
213                 //   -- tax
214                 case aid == aid_ust_n:
215                         tmp.taxval=19
216                         tmp.tax=true
217                         rid=[]string{aid_rec_n,aid_tip,aid_dep,}
218                         accnt[aid]=tmp
219                 case aid == aid_ust_s:
220                         tmp.taxval=7
221                         tmp.tax=true
222                         rid[0]=aid_rec_s
223                         accnt[aid]=tmp
224                 }
225         }
226
227         // check transactions ...
228         for tc := range data.Trn {
229                 // check balance ...
230                 check_balance(&data.Trn[tc],accnt,sel_date)
231         }
232
233 }
234
235 func check_balance(ta *Transaction,accnt map[string]amap,sel_date string) bool {
236
237         // check date
238         tdate := strings.Fields(ta.Date)[0]
239         if !strings.Contains(tdate,sel_date) {
240                 return true
241         } else {
242         }
243
244         // [taxval: 19=0 7=1][tax: no=0 yes=1][buy: no=0 yes=1]
245         var sum [2][2][2]int
246
247         for sc := range ta.Spl {
248                 aid := ta.Spl[sc].AccountId
249                 //accnt[aid].tax
250                 for iac := range iaa {
251                         // taxval
252                         tv := int(0)
253                         if iaa[iac].taxval == 7 {
254                                 tv = 1
255                         }
256                         // tax
257                         tax := int(0)
258                         if iaa[iac].tax {
259                                 tax = 1
260                         }
261                         // buy
262                         buy := int(0)
263                         if iaa[iac].buy {
264                                 buy = 1
265                         }
266                         // match! add to sum and break.
267                         match := bool(false)
268                         // check pids if ...
269                         if tax == 0 && buy == 1 {
270                                 _, exists := accnt[aid]
271                                 if exists {
272                                         // pids
273                                         if accnt[aid].pid == iaa[iac].id {
274                                                 match = true
275                                         }
276                                 }
277                         }
278                         // ... however, always check aids
279                         if aid == iaa[iac].id {
280                                 match = true
281                         }
282                         if match {
283                                 inc, _ := strconv.Atoi(strings.TrimSuffix(ta.Spl[sc].Value,"/100"))
284                                 sum[tv][tax][buy] += inc
285                                 break
286                         }
287                 }
288         }
289
290         // check for exceptions
291         exc := false
292         for ec := range trn_exc {
293                 if ta.Description == trn_exc[ec] {
294                         exc = true
295                         break
296                 }
297         }
298         //for ac := range accountlist {
299         //      if strings.Contains(data.Accnt[anum].Name,accountlist[ac]){
300         //              return true
301         //      }
302         //}
303
304         // check
305         var expected [2]int
306         check := true
307         for buy := 0; buy < 2; buy++ {
308                 expected[0]=int((sum[0][0][buy]*19)/100.0)
309                 expected[1]=int((sum[1][0][buy]*7)/100.0)
310                 for tv :=0; tv < 2; tv++ {
311                         if expected[tv] < sum[tv][1][buy]-1 ||
312                            expected[tv] > sum[tv][1][buy]+1 {
313                                 var sb, st string
314                                 if buy == 0 {
315                                         sb = "Umsatzsteuer"
316                                 } else {
317                                         sb = "Vorsteuer"
318                                 }
319                                 if tv == 0 {
320                                         st = "19%"
321                                 } else {
322                                         st = " 7%"
323                                 }
324                                 check = false
325                                 fmt.Printf("%s %s: ",sb,st);
326                                 fmt.Printf("Erwarte %d statt %d aus %d! ",
327                                            expected[tv],
328                                            sum[tv][1][buy],sum[tv][0][buy]);
329                                 if(!exc) {
330                                         fmt.Printf("\n");
331                                 } else {
332                                         fmt.Printf("Ausnahme greift!\n");
333                                         fmt.Printf("(%s)\n\n",ta.Description)
334                                 }
335                         }
336                 }
337         }
338         if !check && !exc {
339                 fmt.Println("am",ta.Date)
340                 fmt.Printf("(%s)\n",ta.Description)
341                 fmt.Println("Beteiligte Konten:")
342                 for ic := range ta.Spl {
343                         found := strings.TrimSuffix(ta.Spl[ic].Value,"/100")
344                         aid := ta.Spl[ic].AccountId
345                         _, exists := accnt[aid]
346                         if exists {
347                                 num := accnt[aid].num
348                                 fmt.Printf("  %s => %s\n",data.Accnt[num].Name,
349                                                           found)
350                         } else {
351                                 fmt.Printf("  %s => %s\n",aid,found)
352                         }
353                 }
354                 fmt.Println("")
355         }
356
357         return check
358 }
359
360 func round(v float64) int {
361         if v < 0.0 {
362                 v -= 0.5
363         } else {
364                 v += 0.5
365         }
366         return int(v)
367 }
368