]> Dogcows Code - chaz/homebank/blob - src/hb-export.c
Merge branch 'upstream'
[chaz/homebank] / src / hb-export.c
1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2016 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 gchar *sbuf;
47 gint count, i;
48
49 elt = g_string_sized_new(255);
50
51 date = g_date_new ();
52
53 list = g_queue_peek_head_link(acc->txn_queue);
54 while (list != NULL)
55 {
56 Transaction *txn = list->data;
57 Payee *payee;
58 Category *cat;
59 gchar *txt;
60
61 g_date_set_julian (date, txn->date);
62 //#1270876
63 switch(PREFS->dtex_datefmt)
64 {
65 case 0: //"m-d-y"
66 g_string_append_printf (elt, "D%02d/%02d/%04d\n",
67 g_date_get_month(date),
68 g_date_get_day(date),
69 g_date_get_year(date)
70 );
71 break;
72 case 1: //"d-m-y"
73 g_string_append_printf (elt, "D%02d/%02d/%04d\n",
74 g_date_get_day(date),
75 g_date_get_month(date),
76 g_date_get_year(date)
77 );
78 break;
79 case 2: //"y-m-d"
80 g_string_append_printf (elt, "D%04d/%02d/%02d\n",
81 g_date_get_year(date),
82 g_date_get_month(date),
83 g_date_get_day(date)
84 );
85 break;
86 }
87
88 //g_ascii_dtostr (amountbuf, sizeof (amountbuf), txn->amount);
89 g_ascii_formatd (amountbuf, sizeof (amountbuf), "%.2f", txn->amount);
90 g_string_append_printf (elt, "T%s\n", amountbuf);
91
92 sbuf = "";
93 if(txn->status == TXN_STATUS_CLEARED)
94 sbuf = "c";
95 else
96 if(txn->status == TXN_STATUS_RECONCILED)
97 sbuf = "R";
98
99 g_string_append_printf (elt, "C%s\n", sbuf);
100
101 if( txn->paymode == PAYMODE_CHECK)
102 g_string_append_printf (elt, "N%s\n", txn->info);
103
104 //Ppayee
105 payee = da_pay_get(txn->kpay);
106 if(payee)
107 g_string_append_printf (elt, "P%s\n", payee->name);
108
109 // Mmemo
110 g_string_append_printf (elt, "M%s\n", txn->wording);
111
112 // LCategory of transaction
113 // L[Transfer account name]
114 // LCategory of transaction/Class of transaction
115 // L[Transfer account]/Class of transaction
116 if( txn->paymode == PAYMODE_INTXFER && txn->kacc == acc->key)
117 {
118 //#579260
119 Account *dstacc = da_acc_get(txn->kxferacc);
120 if(dstacc)
121 g_string_append_printf (elt, "L[%s]\n", dstacc->name);
122 }
123 else
124 {
125 cat = da_cat_get(txn->kcat);
126 if(cat)
127 {
128 txt = da_cat_get_fullname(cat);
129 g_string_append_printf (elt, "L%s\n", txt);
130 g_free(txt);
131 }
132 }
133
134 // splits
135 count = da_splits_count(txn->splits);
136 for(i=0;i<count;i++)
137 {
138 Split *s = txn->splits[i];
139
140 cat = da_cat_get(s->kcat);
141 if(cat)
142 {
143 txt = da_cat_get_fullname(cat);
144 g_string_append_printf (elt, "S%s\n", txt);
145 g_free(txt);
146 }
147
148 g_string_append_printf (elt, "E%s\n", s->memo);
149
150 g_ascii_formatd (amountbuf, sizeof (amountbuf), "%.2f", s->amount);
151 g_string_append_printf (elt, "$%s\n", amountbuf);
152 }
153
154 g_string_append (elt, "^\n");
155
156
157 list = g_list_next(list);
158 }
159
160 g_io_channel_write_chars(io, elt->str, -1, NULL, NULL);
161
162 g_string_free(elt, TRUE);
163
164 g_date_free(date);
165
166 }
167
168
169
170 static void hb_export_qif_elt_acc(GIOChannel *io, Account *acc)
171 {
172 GString *elt;
173 gchar *type;
174
175 elt = g_string_sized_new(255);
176
177 // account export
178 //#987144 fixed account type
179 switch(acc->type)
180 {
181 case ACC_TYPE_BANK : type = "Bank"; break;
182 case ACC_TYPE_CASH : type = "Cash"; break;
183 case ACC_TYPE_ASSET : type = "Oth A"; break;
184 case ACC_TYPE_CREDITCARD : type = "CCard"; break;
185 case ACC_TYPE_LIABILITY : type = "Oth L"; break;
186 default : type = "Bank"; break;
187 }
188
189 g_string_assign(elt, "!Account\n");
190 g_string_append_printf (elt, "N%s\n", acc->name);
191 g_string_append_printf (elt, "T%s\n", type);
192 g_string_append (elt, "^\n");
193 g_string_append_printf (elt, "!Type:%s\n", type);
194
195 g_io_channel_write_chars(io, elt->str, -1, NULL, NULL);
196
197 g_string_free(elt, TRUE);
198 }
199
200
201 void hb_export_qif_account_single(gchar *filename, Account *acc)
202 {
203 GIOChannel *io;
204
205 io = g_io_channel_new_file(filename, "w", NULL);
206 if(io == NULL)
207 {
208 g_message("file error on: %s", filename);
209 //retval = XML_IO_ERROR;
210 }
211 else
212 {
213 hb_export_qif_elt_acc(io, acc);
214 hb_export_qif_elt_txn(io, acc);
215 g_io_channel_unref (io);
216 }
217 }
218
219
220 void hb_export_qif_account_all(gchar *filename)
221 {
222 GIOChannel *io;
223 GList *lacc, *list;
224
225 io = g_io_channel_new_file(filename, "w", NULL);
226 if(io == NULL)
227 {
228 g_message("file error on: %s", filename);
229 //retval = XML_IO_ERROR;
230 }
231 else
232 {
233 //todo: save accounts in order
234 //todo: save transfer transaction once
235
236 lacc = list = g_hash_table_get_values(GLOBALS->h_acc);
237 while (list != NULL)
238 {
239 Account *item = list->data;
240
241 hb_export_qif_elt_acc(io, item);
242 hb_export_qif_elt_txn(io, item);
243
244 list = g_list_next(list);
245 }
246 g_list_free(lacc);
247
248 g_io_channel_unref (io);
249 }
250
251 }
252
253
254
255
This page took 0.044609 seconds and 4 git commands to generate.