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