more clean with non-redundant account info
[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 // transacion exception list
47 var trn_exc = []string{
48         "GEMA",
49         "Deutsche Post",
50         "gesetz IHK",
51         "Gesundheitsbelehrung",
52         "Gewerbezentralregister",
53         "Entgeltabrechnung siehe Anlage",
54         "ENTGELT SPK",
55         "ttenrecht und F",
56         "Unterrichtung Gastst",
57 }
58
59 // account exception list
60 var account_exc = []string{
61         "4970 Nebenkosten des",
62 }
63
64 // account maps
65 type amap struct {
66         pid string // parent id
67         num int // account number
68         taxval int // 7 or 19
69         buy bool // buy or sales
70         tax bool // tax or non-tax(=goods) account
71 }
72
73 // xml
74 type Account struct {
75         XMLName xml.Name `xml:"account"`
76         Name string `xml:"name"`
77         AccountId string `xml:"id"`
78         ParentId string `xml:"parent"`
79 }
80 type Split struct {
81         XMLName xml.Name `xml:"split"`
82         Id string `xml:"id"`
83         Value string `xml:"value"`
84         Quantity string `xml:"quantity"`
85         AccountId string `xml:"account"`
86 }
87 type Transaction struct {
88         XMLName xml.Name `xml:"transaction"`
89         Id string `xml:"id"`
90         Date string `xml:"date-posted>date"`
91         Description string `xml:"description"`
92         Spl []Split `xml:"splits>split"`
93 }
94 type ParsedData struct {
95         XMLName xml.Name `xml:"gnc-v2"`
96         DataCnt []string `xml:"count-data"`
97         Accnt []Account `xml:"book>account"`
98         Trn []Transaction `xml:"book>transaction"`
99 }
100
101 // tax
102 type TaxReport struct {
103         Expenses [2]int
104         InputTax [2]int
105         ExpExc [2]int
106         ITExc [2]int
107         Receipts [2]int
108         SalesTax [2]int
109 }
110
111 // 'global' data
112 var data ParsedData
113 var tax_report TaxReport
114
115 func main() {
116
117         // argv
118         sel_date := ""
119         if len(os.Args) > 1 {
120                 sel_date = os.Args[1]
121         }
122
123         // open xml file
124         file, err := os.Open("c13_skr03.gnucash")
125         if err != nil {
126                 fmt.Println("Error opening file:", err)
127                 return
128         }
129         defer file.Close()
130
131         // read xml file
132         xmldata, err := ioutil.ReadAll(file)
133         if err != nil {
134                 fmt.Println("Error reading file:", err)
135                 return
136         }
137
138         // unmarshal xml data
139         err = xml.Unmarshal(xmldata,&data)
140         if err != nil {
141                 fmt.Println("Error unmarshaling xml data:", err)
142                 return
143         }
144
145         // whooha, this is our data!
146         fmt.Println("Parsed accounts:",len(data.Accnt))
147         fmt.Println("Parsed transactions:",len(data.Trn))
148         fmt.Println("")
149
150         accnt := make(map[string]amap)
151
152         for ac := range data.Accnt {
153                 aid := data.Accnt[ac].AccountId
154                 pid := data.Accnt[ac].ParentId
155                 for iac := range iaa {
156                         // consider account if pid or aid matches
157                         if pid == iaa[iac].id || aid == iaa[iac].id {
158                                 accnt[aid]=amap{
159                                         pid,
160                                         ac,
161                                         iaa[iac].taxval,
162                                         iaa[iac].buy,
163                                         iaa[iac].tax,
164                                 }
165                                 break
166                         }
167                 }
168         }
169
170         // check transactions ...
171         for tc := range data.Trn {
172                 // check balance ...
173                 check_balance(&data.Trn[tc],accnt,sel_date)
174         }
175
176         // tax report
177         fmt.Println("Umsatzsteuervoranmeldung (19% | 7%):")
178         fmt.Println("------------------------------------")
179         fmt.Println("Aufwendungen:",tax_report.Expenses[0],
180                                     tax_report.Expenses[1]);
181         fmt.Println("ohne Ausn.  :",tax_report.Expenses[0]-
182                                     tax_report.ExpExc[0],
183                                     tax_report.Expenses[1]-
184                                     tax_report.ExpExc[1]);
185         fmt.Println("gesch. Vst. :",int((tax_report.Expenses[0]*19)/100.0),
186                                     int((tax_report.Expenses[1]*7)/100.0))
187         fmt.Println("ohne Ausn.  :",int(((tax_report.Expenses[0]-
188                                          tax_report.ExpExc[0])*19)/100.0),
189                                     int(((tax_report.Expenses[1]-
190                                          tax_report.ExpExc[1])*7)/100.0))
191         fmt.Println("Vorsteuer   :",tax_report.InputTax[0],
192                                     tax_report.InputTax[1],
193                     "->",tax_report.InputTax[0]+tax_report.InputTax[1]);
194         fmt.Println("------------------------------------")
195         fmt.Println("Einnahmen   :",-tax_report.Receipts[0],
196                                     -tax_report.Receipts[1]);
197         fmt.Println("gesch. Ust. :",int((-tax_report.Receipts[0]*19)/100.0),
198                                     int((-tax_report.Receipts[1]*7)/100.0));
199         fmt.Println("Umsatzsteuer:",-tax_report.SalesTax[0],
200                                     -tax_report.SalesTax[1]);
201         fmt.Println("------------------------------------")
202
203 }
204
205 func check_balance(ta *Transaction,accnt map[string]amap,sel_date string) bool {
206
207         // check date
208         tdate := strings.Fields(ta.Date)[0]
209         if !strings.Contains(tdate,sel_date) {
210                 return true
211         } else {
212         }
213
214         // [taxval: 19=0 7=1][tax: no=0 yes=1][buy: no=0 yes=1]
215         var sum [2][2][2]int
216
217         for sc := range ta.Spl {
218                 aid := ta.Spl[sc].AccountId
219                 //accnt[aid].tax
220                 for iac := range iaa {
221                         // taxval
222                         tv := int(0)
223                         if iaa[iac].taxval == 7 {
224                                 tv = 1
225                         }
226                         // tax
227                         tax := int(0)
228                         if iaa[iac].tax {
229                                 tax = 1
230                         }
231                         // buy
232                         buy := int(0)
233                         if iaa[iac].buy {
234                                 buy = 1
235                         }
236                         // match! add to sum and break.
237                         match := bool(false)
238                         // check pids if ...
239                         if tax == 0 && buy == 1 {
240                                 _, exists := accnt[aid]
241                                 if exists {
242                                         // pids
243                                         if accnt[aid].pid == iaa[iac].id {
244                                                 match = true
245                                         }
246                                 }
247                         }
248                         // ... however, always check aids
249                         if aid == iaa[iac].id {
250                                 match = true
251                         }
252                         if match {
253                                 inc, _ := strconv.Atoi(strings.TrimSuffix(ta.Spl[sc].Value,"/100"))
254                                 sum[tv][tax][buy] += inc
255                                 break
256                         }
257                 }
258         }
259
260         // check for exceptions
261         exc := false
262         for ec := range trn_exc {
263                 if strings.Contains(ta.Description,trn_exc[ec]) {
264                         exc = true
265                         break
266                 }
267         }
268         //for ac := range accountlist {
269         //      if strings.Contains(data.Accnt[anum].Name,accountlist[ac]){
270         //              return true
271         //      }
272         //}
273
274         // tax report
275         for tv := 0; tv<2; tv++ {
276                 tax_report.Expenses[tv] += sum[tv][0][1]
277                 tax_report.InputTax[tv] += sum[tv][1][1]
278                 tax_report.Receipts[tv] += sum[tv][0][0]
279                 tax_report.SalesTax[tv] += sum[tv][1][0]
280                 if exc {
281                         tax_report.ExpExc[tv] += sum[tv][0][1]
282                         tax_report.ITExc[tv] += sum[tv][1][1]
283                 }
284         }
285
286         // check
287         var expected [2]int
288         check := true
289         for buy := 0; buy < 2; buy++ {
290                 expected[0]=int((sum[0][0][buy]*19)/100.0)
291                 expected[1]=int((sum[1][0][buy]*7)/100.0)
292                 for tv :=0; tv < 2; tv++ {
293                         if expected[tv] < sum[tv][1][buy]-1 ||
294                            expected[tv] > sum[tv][1][buy]+1 {
295                                 var sb, st string
296                                 if buy == 0 {
297                                         sb = "Umsatzsteuer"
298                                 } else {
299                                         sb = "Vorsteuer"
300                                 }
301                                 if tv == 0 {
302                                         st = "19%"
303                                 } else {
304                                         st = " 7%"
305                                 }
306                                 check = false
307                                 fmt.Printf("%s %s: ",sb,st);
308                                 fmt.Printf("Erwarte %d statt %d aus %d! ",
309                                            expected[tv],
310                                            sum[tv][1][buy],sum[tv][0][buy]);
311                                 if(!exc) {
312                                         fmt.Printf("\n");
313                                 } else {
314                                         fmt.Printf("Ausnahme greift!\n");
315                                         fmt.Printf("(%s)\n\n",ta.Description)
316                                 }
317                         }
318                 }
319         }
320         if !check && !exc {
321                 fmt.Println("am",ta.Date)
322                 fmt.Printf("(%s)\n",ta.Description)
323                 fmt.Println("Beteiligte Konten:")
324                 for ic := range ta.Spl {
325                         found := strings.TrimSuffix(ta.Spl[ic].Value,"/100")
326                         aid := ta.Spl[ic].AccountId
327                         _, exists := accnt[aid]
328                         if exists {
329                                 num := accnt[aid].num
330                                 fmt.Printf("  %s => %s\n",data.Accnt[num].Name,
331                                                           found)
332                         } else {
333                                 fmt.Printf("  %s => %s\n",aid,found)
334                         }
335                 }
336                 fmt.Println("")
337         }
338
339         return check
340 }
341
342 func round(v float64) int {
343         if v < 0.0 {
344                 v -= 0.5
345         } else {
346                 v += 0.5
347         }
348         return int(v)
349 }
350