add gitignore
[chaz/homebank] / src / hb-export.c
1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2014 Maxime DOYEN
3 *
4 * This file is part of HomeBank.
5 *
6 * HomeBank is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * HomeBank is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "homebank.h"
21 #include "hb-export.h"
22
23 /****************************************************************************/
24 /* Debug macros */
25 /****************************************************************************/
26 #define MYDEBUG 0
27
28 #if MYDEBUG
29 #define DB(x) (x);
30 #else
31 #define DB(x);
32 #endif
33
34 /* our global datas */
35 extern struct HomeBank *GLOBALS;
36 extern struct Preferences *PREFS;
37
38 /* = = = = = = = = = = = = = = = = = = = = */
39
40 static void hb_export_qif_elt_txn(GIOChannel *io, Account *acc)
41 {
42 GString *elt;
43 GList *list;
44 GDate *date;
45 char amountbuf[G_ASCII_DTOSTR_BUF_SIZE];
46 gint count, i;
47
48 elt = g_string_sized_new(255);
49
50 date = g_date_new ();
51
52 list = g_list_first(GLOBALS->ope_list);
53 while (list != NULL)
54 {
55 Transaction *txn = list->data;
56 Payee *payee;
57 Category *cat;
58 gchar *txt;
59
60 if( txn->kacc == acc->key )
61 {
62 g_date_set_julian (date, txn->date);
63 //#1270876
64 switch(PREFS->dtex_datefmt)
65 {
66 case 0: //"m-d-y"
67 g_string_append_printf (elt, "D%02d/%02d/%04d\n",
68 g_date_get_month(date),
69 g_date_get_day(date),
70 g_date_get_year(date)
71 );
72 break;
73 case 1: //"d-m-y"
74 g_string_append_printf (elt, "D%02d/%02d/%04d\n",
75 g_date_get_day(date),
76 g_date_get_month(date),
77 g_date_get_year(date)
78 );
79 break;
80 case 2: //"y-m-d"
81 g_string_append_printf (elt, "D%04d/%02d/%02d\n",
82 g_date_get_year(date),
83 g_date_get_month(date),
84 g_date_get_day(date)
85 );
86 break;
87 }
88
89 //g_ascii_dtostr (amountbuf, sizeof (amountbuf), txn->amount);
90 g_ascii_formatd (amountbuf, sizeof (amountbuf), "%.2f", txn->amount);
91 g_string_append_printf (elt, "T%s\n", amountbuf);
92
93 g_string_append_printf (elt, "C%s\n", txn->flags & OF_VALID ? "R" : "");
94
95 if( txn->paymode == PAYMODE_CHECK)
96 g_string_append_printf (elt, "N%s\n", txn->info);
97
98 //Ppayee
99 payee = da_pay_get(txn->kpay);
100 if(payee)
101 g_string_append_printf (elt, "P%s\n", payee->name);
102
103 // Mmemo
104 g_string_append_printf (elt, "M%s\n", txn->wording);
105
106 // LCategory of transaction
107 // L[Transfer account name]
108 // LCategory of transaction/Class of transaction
109 // L[Transfer account]/Class of transaction
110 if( txn->paymode == PAYMODE_INTXFER && txn->kacc == acc->key)
111 {
112 //#579260
113 Account *dstacc = da_acc_get(txn->kxferacc);
114 if(dstacc)
115 g_string_append_printf (elt, "L[%s]\n", dstacc->name);
116 }
117 else
118 {
119 cat = da_cat_get(txn->kcat);
120 if(cat)
121 {
122 txt = da_cat_get_fullname(cat);
123 g_string_append_printf (elt, "L%s\n", txt);
124 g_free(txt);
125 }
126 }
127
128 // splits
129 count = da_transaction_splits_count(txn);
130 for(i=0;i<count;i++)
131 {
132 Split *s = txn->splits[i];
133
134 cat = da_cat_get(s->kcat);
135 if(cat)
136 {
137 txt = da_cat_get_fullname(cat);
138 g_string_append_printf (elt, "S%s\n", txt);
139 g_free(txt);
140 }
141
142 g_string_append_printf (elt, "E%s\n", s->memo);
143
144 g_ascii_formatd (amountbuf, sizeof (amountbuf), "%.2f", s->amount);
145 g_string_append_printf (elt, "$%s\n", amountbuf);
146 }
147
148 g_string_append (elt, "^\n");
149 }
150
151 list = g_list_next(list);
152 }
153
154 g_io_channel_write_chars(io, elt->str, -1, NULL, NULL);
155
156 g_string_free(elt, TRUE);
157
158 g_date_free(date);
159
160 }
161
162
163
164 static void hb_export_qif_elt_acc(GIOChannel *io, Account *acc)
165 {
166 GString *elt;
167 gchar *type;
168
169 elt = g_string_sized_new(255);
170
171 // account export
172 //#987144 fixed account type
173 switch(acc->type)
174 {
175 case ACC_TYPE_BANK : type = "Bank"; break;
176 case ACC_TYPE_CASH : type = "Cash"; break;
177 case ACC_TYPE_ASSET : type = "Oth A"; break;
178 case ACC_TYPE_CREDITCARD : type = "CCard"; break;
179 case ACC_TYPE_LIABILITY : type = "Oth L"; break;
180 default : type = "Bank"; break;
181 }
182
183 g_string_assign(elt, "!Account\n");
184 g_string_append_printf (elt, "N%s\n", acc->name);
185 g_string_append_printf (elt, "T%s\n", type);
186 g_string_append (elt, "^\n");
187 g_string_append_printf (elt, "!Type:%s\n", type);
188
189 g_io_channel_write_chars(io, elt->str, -1, NULL, NULL);
190
191 g_string_free(elt, TRUE);
192 }
193
194
195 void hb_export_qif_account_single(gchar *filename, Account *acc)
196 {
197 GIOChannel *io;
198
199 io = g_io_channel_new_file(filename, "w", NULL);
200 if(io == NULL)
201 {
202 g_message("file error on: %s", filename);
203 //retval = XML_IO_ERROR;
204 }
205 else
206 {
207 hb_export_qif_elt_acc(io, acc);
208 hb_export_qif_elt_txn(io, acc);
209 g_io_channel_unref (io);
210 }
211 }
212
213
214 void hb_export_qif_account_all(gchar *filename)
215 {
216 GIOChannel *io;
217 GList *lacc, *list;
218
219 io = g_io_channel_new_file(filename, "w", NULL);
220 if(io == NULL)
221 {
222 g_message("file error on: %s", filename);
223 //retval = XML_IO_ERROR;
224 }
225 else
226 {
227 //todo: save accounts in order
228 //todo: save transfer transaction once
229
230 lacc = list = g_hash_table_get_values(GLOBALS->h_acc);
231 while (list != NULL)
232 {
233 Account *item = list->data;
234
235 hb_export_qif_elt_acc(io, item);
236 hb_export_qif_elt_txn(io, item);
237
238 list = g_list_next(list);
239 }
240 g_list_free(lacc);
241
242 g_io_channel_unref (io);
243 }
244
245 }
246
247
248 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =*/
249
250
This page took 0.049009 seconds and 4 git commands to generate.