]>
Dogcows Code - chaz/homebank/blob - src/hb-export.c
1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2019 Maxime DOYEN
4 * This file is part of HomeBank.
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.
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.
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/>.
21 #include "hb-export.h"
23 /****************************************************************************/
25 /****************************************************************************/
34 /* our global datas */
35 extern struct HomeBank
*GLOBALS
;
36 extern struct Preferences
*PREFS
;
38 /* = = = = = = = = = = = = = = = = = = = = */
40 static void hb_export_qif_elt_txn(GIOChannel
*io
, Account
*acc
)
45 char amountbuf
[G_ASCII_DTOSTR_BUF_SIZE
];
49 elt
= g_string_sized_new(255);
53 list
= g_queue_peek_head_link(acc
->txn_queue
);
56 Transaction
*txn
= list
->data
;
60 g_date_set_julian (date
, txn
->date
);
62 switch(PREFS
->dtex_datefmt
)
65 g_string_append_printf (elt
, "D%02d/%02d/%04d\n",
66 g_date_get_month(date
),
72 g_string_append_printf (elt
, "D%02d/%02d/%04d\n",
74 g_date_get_month(date
),
79 g_string_append_printf (elt
, "D%04d/%02d/%02d\n",
80 g_date_get_year(date
),
81 g_date_get_month(date
),
87 //g_ascii_dtostr (amountbuf, sizeof (amountbuf), txn->amount);
88 g_ascii_formatd (amountbuf
, sizeof (amountbuf
), "%.2f", txn
->amount
);
89 g_string_append_printf (elt
, "T%s\n", amountbuf
);
92 if(txn
->status
== TXN_STATUS_CLEARED
)
95 if(txn
->status
== TXN_STATUS_RECONCILED
)
98 g_string_append_printf (elt
, "C%s\n", sbuf
);
100 if( txn
->paymode
== PAYMODE_CHECK
)
101 g_string_append_printf (elt
, "N%s\n", txn
->info
);
104 payee
= da_pay_get(txn
->kpay
);
106 g_string_append_printf (elt
, "P%s\n", payee
->name
);
109 g_string_append_printf (elt
, "M%s\n", txn
->memo
);
111 // LCategory of transaction
112 // L[Transfer account name]
113 // LCategory of transaction/Class of transaction
114 // L[Transfer account]/Class of transaction
115 if( txn
->paymode
== PAYMODE_INTXFER
&& txn
->kacc
== acc
->key
)
118 Account
*dstacc
= da_acc_get(txn
->kxferacc
);
120 g_string_append_printf (elt
, "L[%s]\n", dstacc
->name
);
124 cat
= da_cat_get(txn
->kcat
);
127 g_string_append_printf (elt
, "L%s\n", cat
->fullname
);
132 count
= da_splits_length(txn
->splits
);
135 Split
*s
= da_splits_get(txn
->splits
, i
);
137 cat
= da_cat_get(s
->kcat
);
140 g_string_append_printf (elt
, "S%s\n", cat
->fullname
);
143 g_string_append_printf (elt
, "E%s\n", s
->memo
);
145 g_ascii_formatd (amountbuf
, sizeof (amountbuf
), "%.2f", s
->amount
);
146 g_string_append_printf (elt
, "$%s\n", amountbuf
);
149 g_string_append (elt
, "^\n");
152 list
= g_list_next(list
);
155 g_io_channel_write_chars(io
, elt
->str
, -1, NULL
, NULL
);
157 g_string_free(elt
, TRUE
);
165 static void hb_export_qif_elt_acc(GIOChannel
*io
, Account
*acc
)
170 elt
= g_string_sized_new(255);
173 //#987144 fixed account type
176 case ACC_TYPE_BANK
: type
= "Bank"; break;
177 case ACC_TYPE_CASH
: type
= "Cash"; break;
178 case ACC_TYPE_ASSET
: type
= "Oth A"; break;
179 case ACC_TYPE_CREDITCARD
: type
= "CCard"; break;
180 case ACC_TYPE_LIABILITY
: type
= "Oth L"; break;
181 default : type
= "Bank"; break;
184 g_string_assign(elt
, "!Account\n");
185 g_string_append_printf (elt
, "N%s\n", acc
->name
);
186 g_string_append_printf (elt
, "T%s\n", type
);
187 g_string_append (elt
, "^\n");
188 g_string_append_printf (elt
, "!Type:%s\n", type
);
190 g_io_channel_write_chars(io
, elt
->str
, -1, NULL
, NULL
);
192 g_string_free(elt
, TRUE
);
196 void hb_export_qif_account_single(gchar
*filename
, Account
*acc
)
200 io
= g_io_channel_new_file(filename
, "w", NULL
);
203 g_message("file error on: %s", filename
);
204 //retval = XML_IO_ERROR;
208 hb_export_qif_elt_acc(io
, acc
);
209 hb_export_qif_elt_txn(io
, acc
);
210 g_io_channel_unref (io
);
215 void hb_export_qif_account_all(gchar
*filename
)
220 io
= g_io_channel_new_file(filename
, "w", NULL
);
223 g_message("file error on: %s", filename
);
224 //retval = XML_IO_ERROR;
228 //todo: save accounts in order
229 //todo: save transfer transaction once
231 lacc
= list
= g_hash_table_get_values(GLOBALS
->h_acc
);
234 Account
*item
= list
->data
;
236 hb_export_qif_elt_acc(io
, item
);
237 hb_export_qif_elt_txn(io
, item
);
239 list
= g_list_next(list
);
243 g_io_channel_unref (io
);
249 /* = = = = = = = = beta feature version = = = = = = = = */
251 //static GtkPrintSettings *settings = NULL;
252 //static GtkPageSetup *page_setup = NULL;
255 static void papersize(PdfPrintContext
*ppc
)
257 //GList *list, *item;
261 DB( g_print("[papersize]\n") );
263 name
= gtk_paper_size_get_default();
265 DB( g_print("- def paper is %s\n", name
) );
267 ps
= gtk_paper_size_new(name
);
271 /*GtkPageSetup *new_page_setup;
273 if (settings == NULL)
274 settings = gtk_print_settings_new ();
276 new_page_setup = gtk_print_run_page_setup_dialog (NULL,
277 page_setup, settings);
280 g_object_unref (page_setup);
282 page_setup = new_page_setup;
286 gdouble w
, h
, mt
, mb
, ml
, mr
;
287 w
= gtk_paper_size_get_width(ps
, GTK_UNIT_MM
);
288 h
= gtk_paper_size_get_height(ps
, GTK_UNIT_MM
);
289 mt
= gtk_paper_size_get_default_top_margin(ps
, GTK_UNIT_MM
);
290 mr
= gtk_paper_size_get_default_right_margin(ps
, GTK_UNIT_MM
);
291 mb
= gtk_paper_size_get_default_bottom_margin(ps
, GTK_UNIT_MM
);
292 ml
= gtk_paper_size_get_default_left_margin(ps
, GTK_UNIT_MM
);
294 DB( g_print("- name: %s\n", gtk_paper_size_get_display_name(ps
)) );
295 DB( g_print("- w: %f (%f)\n- h: %f (%f)\n", w
, w
/PANGO_SCALE
, h
, h
/PANGO_SCALE
) );
296 DB( g_print("- margin: %f %f %f %f\n", mt
, mr
, mb
, ml
) );
307 gtk_paper_size_free(ps
);
309 /* list all paper size */
311 list = gtk_paper_size_get_paper_sizes (FALSE);
312 item = g_list_first(list);
318 g_print("- name: %s\n", gtk_paper_size_get_display_name(ps));
319 gtk_paper_size_free(ps);
321 item = g_list_next(item);
328 #define FONT "Helvetica 9px"
330 //#define PDF_MARGIN 24
331 #define PDF_COL_MARGIN 8
332 #define PDF_LINE_MARGIN 2
333 #define PDF_FONT_NORMAL 9
334 #define PDF_FONT_TITLE 12
340 #define HEX_R(xcol) (((xcol>>24) & 0xFF)/255)
341 #define HEX_G(xcol) (((xcol>>16) & 0xFF)/255)
342 #define HEX_B(xcol) (((xcol>> 8) & 0xFF)/255)
346 static void hb_pdf_draw_help_rect(cairo_t
*cr
, gint32 xcol
, double x
, double y
, double w
, double h
)
350 cairo_set_line_width(cr
, 1.0);
351 cairo_set_source_rgba(cr
, HEX_R(xcol
), HEX_G(xcol
), HEX_B(xcol
), 1.0); //alpha is unused for now
352 cairo_rectangle (cr
, x
, y
, w
, h
);
362 static void hb_pdf_draw_line(PdfPrintContext
*ppc
, cairo_t
*cr
, gdouble y
, gboolean bold
, gboolean rulehint
)
368 /* Create a PangoLayout, set the font and text */
369 layout
= pango_cairo_create_layout (cr
);
371 //desc = pango_font_description_from_string (FONT);
373 pango_font_description_set_weight(ppc
->desc
, PANGO_WEIGHT_BOLD
);
375 pango_font_description_set_weight(ppc
->desc
, PANGO_WEIGHT_NORMAL
);
377 pango_layout_set_font_description (layout
, ppc
->desc
);
386 cairo_set_source_rgb(cr
, 0.9, 0.9, 0.9);
387 cairo_rectangle (cr
, x
, y
, ppc
->w
- ppc
->ml
- ppc
->mr
, PDF_FONT_NORMAL
);
393 cairo_set_source_rgb(cr
, 0.0, 0.0, 0.0);
394 cairo_move_to(cr
, x
, y
);
396 for(i
=0;i
<PDF_NUMCOL
;i
++)
398 if(ppc
->column_txt
[i
] != NULL
)
402 pango_layout_set_text (layout
, ppc
->column_txt
[i
], -1);
403 pango_layout_get_size (layout
, &width
, &height
);
404 if( i
==1 || i
==2 || i
==3 )
406 pango_layout_set_width(layout
, ppc
->column_width
[i
]*PANGO_SCALE
);
407 pango_layout_set_ellipsize (layout
, PANGO_ELLIPSIZE_END
);
410 //cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
412 if( i
==0 || i
==4 || i
==6 ) // pad right: date/amount/balance
414 //if(*ppc->column_txt[i] != '-')
415 //cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.66); //grey
416 cairo_move_to(cr
, x
+ ppc
->column_width
[i
] - (width
/PANGO_SCALE
) , y
);
419 cairo_move_to(cr
, x
, y
);
421 pango_cairo_show_layout (cr
, layout
);
424 /*cairo_set_line_width(cr, 1.0);
425 cairo_move_to(cr, x, y + .5);
426 cairo_line_to(cr, x + ppc->column_width[i], y+.5);
431 x
+= ppc
->column_width
[i
] + PDF_COL_MARGIN
;
434 g_object_unref (layout
);
439 static void hb_pdf_set_col_title(PdfPrintContext
*ppc
)
441 ppc
->column_txt
[0] = _("Date");
442 ppc
->column_txt
[1] = _("Info");
443 ppc
->column_txt
[2] = _("Payee");
444 ppc
->column_txt
[3] = _("Memo");
445 ppc
->column_txt
[4] = _("Amount");
446 ppc
->column_txt
[5] = "C";
447 ppc
->column_txt
[6] = _("Balance");
451 void hb_export_pdf_listview(GtkTreeView
*treeview
, gchar
*filepath
, gchar
*accname
)
453 cairo_surface_t
*surf
;
456 PangoFontDescription
*desc
;
464 DB( g_print("[gtk-chart] export to pdf\n") );
466 model
= gtk_tree_view_get_model(treeview
);
470 //gchar *filename = "/home/max/Desktop/hb-txn-export.pdf";
471 double width
; //=210 * 2.83;
472 double height
; //=297 * 2.83;
477 surf
= cairo_pdf_surface_create (filepath
, width
, height
);
479 if( cairo_surface_status(surf
) != CAIRO_STATUS_SUCCESS
)
480 //todo: manage error later on
484 cr
= cairo_create (surf
);
485 //cairo_pdf_surface_set_size(surf, width * 2.83, height * 2.83);
487 //g_print("width=%d\n", cairo_image_surface_get_width( surf));
488 double x1
, x2
, y1
, y2
;
489 cairo_clip_extents (cr
, &x1
, &y1
, &x2
, &y2
);
491 DB( g_print("surface w=%f, h=%f\n", x2
- x1
, y2
- y1
) );
492 double pwidth
= x2
- x1
;
495 /* Create a PangoLayout, set the font and text */
496 layout
= pango_cairo_create_layout (cr
);
498 /* get and copy the font from the treeview widget */
499 gtk_style_context_get(gtk_widget_get_style_context(GTK_WIDGET(treeview
)), GTK_STATE_FLAG_NORMAL
, "font", &desc
, NULL
);
500 ppc
.desc
= pango_font_description_copy(desc
);
502 DB( g_print("family: %s\n", pango_font_description_get_family(desc
)) );
503 DB( g_print("size: %d (%d)\n", pango_font_description_get_size (desc
), pango_font_description_get_size (desc
)/PANGO_SCALE
) );
507 /* header is 1 line for date page number at top, then a title in bold, then 2 empty lines */
508 gint header_height
= PDF_FONT_NORMAL
* 2 + PDF_FONT_TITLE
;
509 gint nb_lines
= gtk_tree_model_iter_n_children(model
, NULL
);
511 /* should include here the headertitle line */
513 gint lpp
= floor ((height
-header_height
-ppc
.mt
-ppc
.mb
) / (PDF_FONT_NORMAL
+ PDF_LINE_MARGIN
));
514 gint page
, num_pages
= (nb_lines
- 1) / lpp
+ 1;
516 DB( g_print("\n - should pdf %d lines, lpp=%d, num_pages=%d\n", nb_lines
, lpp
, num_pages
) );
520 gint cur_page_line
= 1;
523 gchar amtbuf
[G_ASCII_DTOSTR_BUF_SIZE
];
524 gchar balbuf
[G_ASCII_DTOSTR_BUF_SIZE
];
526 GDate
*date
= g_date_new ();
528 //cairo_set_font_size(cr, PDF_FONT_NORMAL);
529 pango_font_description_set_absolute_size(ppc
.desc
, PDF_FONT_NORMAL
* PANGO_SCALE
);
530 pango_layout_set_font_description (layout
, ppc
.desc
);
533 hb_pdf_set_col_title(&ppc
);
535 for(col
=0;col
<PDF_NUMCOL
;col
++)
539 ppc
.column_width
[col
] = 0;
540 pango_layout_set_text (layout
, ppc
.column_txt
[col
], -1);
541 pango_layout_get_size (layout
, &tw
, &th
);
542 ppc
.column_width
[col
] = MAX(ppc
.column_width
[col
], tw
/ PANGO_SCALE
);
546 DB( g_print(" - compute width\n") );
548 /* first pass to get max width */
549 valid
= gtk_tree_model_get_iter_first(GTK_TREE_MODEL(model
), &iter
);
555 gtk_tree_model_get (model
, &iter
, LST_DSPOPE_DATAS
, &txn
, -1);
558 g_date_set_julian (date
, txn
->date
);
559 g_date_strftime (dbuffer
, 255-1, "%x", date
);
560 pango_layout_set_text (layout
, dbuffer
, -1);
561 pango_layout_get_size (layout
, &tw
, &th
);
562 ppc
.column_width
[i
] = MAX(ppc
.column_width
[i
], tw
/ PANGO_SCALE
);
565 if(txn
->info
!= NULL
&& strlen(txn
->info
) > 0)
567 pango_layout_set_text (layout
, txn
->info
, -1);
568 pango_layout_get_size (layout
, &tw
, &th
);
569 ppc
.column_width
[i
] = MAX(ppc
.column_width
[i
], tw
/ PANGO_SCALE
);
573 hb_strfnum(amtbuf
, G_ASCII_DTOSTR_BUF_SIZE
-1, txn
->amount
, txn
->kcur
, GLOBALS
->minor
);
574 pango_layout_set_text (layout
, amtbuf
, -1);
575 pango_layout_get_size (layout
, &tw
, &th
);
576 ppc
.column_width
[i
] = MAX(ppc
.column_width
[i
], tw
/ PANGO_SCALE
);
579 pango_layout_set_text (layout
, "R", -1);
580 pango_layout_get_size (layout
, &tw
, &th
);
581 ppc
.column_width
[i
] = MAX(ppc
.column_width
[i
], tw
/ PANGO_SCALE
);
584 hb_strfnum(balbuf
, G_ASCII_DTOSTR_BUF_SIZE
-1, txn
->balance
, txn
->kcur
, GLOBALS
->minor
);
585 pango_layout_set_text (layout
, balbuf
, -1);
586 pango_layout_get_size (layout
, &tw
, &th
);
587 ppc
.column_width
[i
] = MAX(ppc
.column_width
[i
], tw
/ PANGO_SCALE
);
589 valid
= gtk_tree_model_iter_next(GTK_TREE_MODEL(model
), &iter
);
592 /* distribute remaining size */
593 gdouble tmp
= pwidth
- ppc
.ml
- ppc
.mr
- (PDF_COL_MARGIN
*PDF_NUMCOL
);
596 DB( g_print(" page width=%f, remain width=%f\n", pwidth
, tmp
) );
598 tmp
-= ppc
.column_width
[0];
599 tmp
-= ppc
.column_width
[4];
600 tmp
-= ppc
.column_width
[5];
601 tmp
-= ppc
.column_width
[6];
603 /* info=1/4 payee=1/4 memo=2/4 */
604 ppc
.column_width
[1] = tmp
/ 4;;
605 ppc
.column_width
[2] = tmp
/ 4;
606 ppc
.column_width
[3] = 2*tmp
/ 4;
608 DB( g_print(" page width=%f, remain width=%f\n", width
, tmp
) );
611 for(i
=0;i
<PDF_NUMCOL
;i
++)
612 g_print(" col%d=%g ", i
, ppc
.column_width
[i
]);
617 DB( g_print("\n - start printing\n") );
621 valid
= gtk_tree_model_get_iter_first(GTK_TREE_MODEL(model
), &iter
);
627 gtk_tree_model_get (model
, &iter
, LST_DSPOPE_DATAS
, &txn
, -1);
629 //DB( g_print(" - %d, %d, %s\n", x, y, txn->memo) );
630 if(cur_page_line
== 1)
635 hb_pdf_draw_help_rect(cr
, 0xFF0000FF, ppc
.ml
+0.5, ppc
.mt
+0.5, width
-(ppc
.ml
+ppc
.mr
), height
- (ppc
.mt
+ppc
.mb
));
636 hb_pdf_draw_help_rect(cr
, 0xFF00FFFF, ppc
.ml
+0.5, ppc
.mt
+0.5, width
-(ppc
.ml
+ppc
.mr
), header_height
);
639 cairo_set_source_rgb(cr
, 0.0, 0.0, 0.0);
641 // draw account title
642 pango_font_description_set_absolute_size(ppc
.desc
, PDF_FONT_TITLE
* PANGO_SCALE
);
643 pango_layout_set_font_description (layout
, ppc
.desc
);
645 pango_layout_set_text (layout
, accname
, -1);
646 pango_layout_get_pixel_size (layout
, &tw
, &th
);
647 cairo_move_to(cr
, pwidth
/2 - (tw
/2), ppc
.mt
);
648 pango_cairo_show_layout (cr
, layout
);
650 // draw column titles
651 pango_font_description_set_absolute_size(ppc
.desc
, PDF_FONT_NORMAL
* PANGO_SCALE
);
652 pango_layout_set_font_description (layout
, ppc
.desc
);
654 g_sprintf(dbuffer
, "Page %d/%d", page
, num_pages
);
655 pango_layout_set_text (layout
, dbuffer
, -1);
656 pango_layout_get_pixel_size (layout
, &tw
, &th
);
657 cairo_move_to(cr
, pwidth
- ppc
.mr
- tw
, ppc
.mt
);
658 pango_cairo_show_layout (cr
, layout
);
661 y
= ppc
.mt
+ header_height
- (PDF_FONT_NORMAL
+ PDF_LINE_MARGIN
);
662 hb_pdf_set_col_title(&ppc
);
664 hb_pdf_draw_line(&ppc
, cr
, y
, TRUE
, FALSE
);
667 /* print a single line */
669 y
= ppc
.mt
+ header_height
+ (cur_page_line
* (PDF_FONT_NORMAL
+ PDF_LINE_MARGIN
));
674 for(i
=0;i
<PDF_NUMCOL
;i
++)
676 ppc
.column_txt
[i
] = NULL
;
680 g_date_set_julian (date
, txn
->date
);
681 g_date_strftime (dbuffer
, 255-1, "%x", date
);
682 ppc
.column_txt
[i
] = dbuffer
;
685 ppc
.column_txt
[i
] = txn
->info
;
688 Payee
*p
= da_pay_get(txn
->kpay
);
690 ppc
.column_txt
[i
] = p
->name
;
693 /*Category *c = da_cat_get(txn->kcat);
695 ppc.column_txt[i] = da_cat_get_fullname(c);*/
696 ppc
.column_txt
[i
] = txn
->memo
;
699 hb_strfnum(amtbuf
, G_ASCII_DTOSTR_BUF_SIZE
-1, txn
->amount
, txn
->kcur
, GLOBALS
->minor
);
700 ppc
.column_txt
[i
] = amtbuf
;
703 ppc
.column_txt
[i
] = "";
704 if(txn
->status
== TXN_STATUS_CLEARED
)
705 ppc
.column_txt
[i
] = "c";
707 if(txn
->status
== TXN_STATUS_RECONCILED
)
708 ppc
.column_txt
[i
] = "R";
711 hb_strfnum(balbuf
, G_ASCII_DTOSTR_BUF_SIZE
-1, txn
->balance
, txn
->kcur
, GLOBALS
->minor
);
712 ppc
.column_txt
[i
] = balbuf
;
714 hb_pdf_draw_line(&ppc
, cr
, y
, FALSE
, (cur_page_line
% 2));
716 /* free any fullcat name */
717 /*if(ppc.column_txt[3] != NULL)
718 g_free(ppc.column_txt[3]);*/
721 if(cur_page_line
>= lpp
)
723 DB( g_print("\n - next page %d\n", page
) );
732 valid
= gtk_tree_model_iter_next(GTK_TREE_MODEL(model
), &iter
);
737 g_object_unref (layout
);
738 pango_font_description_free (ppc
.desc
);
741 cairo_surface_destroy (surf
);
This page took 0.065839 seconds and 5 git commands to generate.