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