reset tax accounts for taxvals set to zero
[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                                         // reset taxvalues of involved accounts
271                                         // (to drop an error)
272                                         if accnt[aid].tax {
273                                                 if accnt[aid].taxval==7 {
274                                                         tv=1
275                                                 }
276                                         } else {
277                                                 continue
278                                         }
279                                 }
280                         } else {
281                                 if accnt[aid].taxval == 0 {
282                                         if accnt[aid].tax {
283                                                 fmt.Println("FATAL!");
284                                         }
285                                         continue
286                                 }
287                                 if accnt[aid].taxval == 7 {
288                                         tv = 1
289                                 }
290                         }
291                         // tax
292                         tax := int(0)
293                         if iaa[iac].tax {
294                                 tax = 1
295                         }
296                         // buy
297                         buy := int(0)
298                         if iaa[iac].buy {
299                                 buy = 1
300                         }
301                         // match! add to sum and break.
302                         match := bool(false)
303                         // check pids if ...
304                         if tax == 0 && buy == 1 {
305                                 _, exists := accnt[aid]
306                                 if exists {
307                                         // pids
308                                         if accnt[aid].pid == iaa[iac].id {
309                                                 match = true
310                                         }
311                                 }
312                         }
313                         // ... however, always check aids
314                         if aid == iaa[iac].id {
315                                 match = true
316                         }
317                         if match {
318                                 inc, _ := strconv.Atoi(strings.TrimSuffix(ta.Spl[sc].Value,"/100"))
319                                 sum[tv][tax][buy] += inc
320                                 break
321                         }
322                 }
323         }
324
325         // check for exceptions
326         exc := false
327         for ec := range trn_exc {
328                 if strings.Contains(ta.Description,trn_exc[ec]) {
329                         exc = true
330                         break
331                 }
332         }
333         //for ac := range accountlist {
334         //      if strings.Contains(data.Accnt[anum].Name,accountlist[ac]){
335         //              return true
336         //      }
337         //}
338
339         // tax report
340         for tv := 0; tv<2; tv++ {
341                 tax_report.Expenses[tv] += sum[tv][0][1]
342                 tax_report.InputTax[tv] += sum[tv][1][1]
343                 tax_report.Receipts[tv] += sum[tv][0][0]
344                 tax_report.SalesTax[tv] += sum[tv][1][0]
345                 if exc {
346                         tax_report.ExpExc[tv] += sum[tv][0][1]
347                         tax_report.ITExc[tv] += sum[tv][1][1]
348                 }
349         }
350
351         // check
352         var expected [2]int
353         check := true
354         for buy := 0; buy < 2; buy++ {
355                 expected[0]=int((sum[0][0][buy]*19)/100.0)
356                 expected[1]=int((sum[1][0][buy]*7)/100.0)
357                 for tv :=0; tv < 2; tv++ {
358                         if expected[tv] < sum[tv][1][buy]-1 ||
359                            expected[tv] > sum[tv][1][buy]+1 {
360                                 var sb, st string
361                                 if buy == 0 {
362                                         sb = "Umsatzsteuer"
363                                 } else {
364                                         sb = "Vorsteuer"
365                                 }
366                                 if tv == 0 {
367                                         st = "19%"
368                                 } else {
369                                         st = " 7%"
370                                 }
371                                 check = false
372                                 fmt.Printf("%s %s: ",sb,st);
373                                 fmt.Printf("Erwarte %d statt %d aus %d! ",
374                                            expected[tv],
375                                            sum[tv][1][buy],sum[tv][0][buy]);
376                                 if(!exc) {
377                                         fmt.Printf("\n");
378                                 } else {
379                                         fmt.Printf("Ausnahme greift!\n");
380                                         fmt.Printf("(%s)\n\n",ta.Description)
381                                 }
382                         }
383                 }
384         }
385         if !check && !exc {
386                 fmt.Println("am",ta.Date)
387                 fmt.Printf("(%s)\n",ta.Description)
388                 fmt.Println("Beteiligte Konten:")
389                 for ic := range ta.Spl {
390                         found := strings.TrimSuffix(ta.Spl[ic].Value,"/100")
391                         aid := ta.Spl[ic].AccountId
392                         _, exists := accnt[aid]
393                         if exists {
394                                 num := accnt[aid].num
395                                 fmt.Printf("  %s => %s\n",data.Accnt[num].Name,
396                                                           found)
397                         } else {
398                                 fmt.Printf("  %s => %s\n",aid,found)
399                         }
400                 }
401                 fmt.Println("")
402         }
403
404         return check
405 }
406
407 func round(v float64) int {
408         if v < 0.0 {
409                 v -= 0.5
410         } else {
411                 v += 0.5
412         }
413         return int(v)
414 }
415