]>
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/>.
22 #include "hb-export.h"
23 #include "list-operation.h"
25 /****************************************************************************/
27 /****************************************************************************/
36 /* our global datas */
37 extern struct HomeBank
*GLOBALS
;
38 extern struct Preferences
*PREFS
;
40 /* = = = = = = = = = = = = = = = = = = = = */
42 static void hb_export_qif_elt_txn(GIOChannel
*io
, Account
*acc
)
47 char amountbuf
[G_ASCII_DTOSTR_BUF_SIZE
];
51 elt
= g_string_sized_new(255);
55 list
= g_queue_peek_head_link(acc
->txn_queue
);
58 Transaction
*txn
= list
->data
;
62 g_date_set_julian (date
, txn
->date
);
64 switch(PREFS
->dtex_datefmt
)
67 g_string_append_printf (elt
, "D%02d/%02d/%04d\n",
68 g_date_get_month(date
),
74 g_string_append_printf (elt
, "D%02d/%02d/%04d\n",
76 g_date_get_month(date
),
81 g_string_append_printf (elt
, "D%04d/%02d/%02d\n",
82 g_date_get_year(date
),
83 g_date_get_month(date
),
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
);
94 if(txn
->status
== TXN_STATUS_CLEARED
)
97 if(txn
->status
== TXN_STATUS_RECONCILED
)
100 g_string_append_printf (elt
, "C%s\n", sbuf
);
102 if( txn
->paymode
== PAYMODE_CHECK
)
103 g_string_append_printf (elt
, "N%s\n", txn
->info
);
106 payee
= da_pay_get(txn
->kpay
);
108 g_string_append_printf (elt
, "P%s\n", payee
->name
);
111 g_string_append_printf (elt
, "M%s\n", txn
->memo
);
113 // LCategory of transaction
114 // L[Transfer account name]
115 // LCategory of transaction/Class of transaction
116 // L[Transfer account]/Class of transaction
117 if( txn
->paymode
== PAYMODE_INTXFER
&& txn
->kacc
== acc
->key
)
120 Account
*dstacc
= da_acc_get(txn
->kxferacc
);
122 g_string_append_printf (elt
, "L[%s]\n", dstacc
->name
);
126 cat
= da_cat_get(txn
->kcat
);
129 g_string_append_printf (elt
, "L%s\n", cat
->fullname
);
134 count
= da_splits_length(txn
->splits
);
137 Split
*s
= da_splits_get(txn
->splits
, i
);
139 cat
= da_cat_get(s
->kcat
);
142 g_string_append_printf (elt
, "S%s\n", cat
->fullname
);
145 g_string_append_printf (elt
, "E%s\n", s
->memo
);
147 g_ascii_formatd (amountbuf
, sizeof (amountbuf
), "%.2f", s
->amount
);
148 g_string_append_printf (elt
, "$%s\n", amountbuf
);
151 g_string_append (elt
, "^\n");
154 list
= g_list_next(list
);
157 g_io_channel_write_chars(io
, elt
->str
, -1, NULL
, NULL
);
159 g_string_free(elt
, TRUE
);
167 static void hb_export_qif_elt_acc(GIOChannel
*io
, Account
*acc
)
172 elt
= g_string_sized_new(255);
175 //#987144 fixed account type
178 case ACC_TYPE_BANK
: type
= "Bank"; break;
179 case ACC_TYPE_CASH
: type
= "Cash"; break;
180 case ACC_TYPE_ASSET
: type
= "Oth A"; break;
181 case ACC_TYPE_CREDITCARD
: type
= "CCard"; break;
182 case ACC_TYPE_LIABILITY
: type
= "Oth L"; break;
183 default : type
= "Bank"; break;
186 g_string_assign(elt
, "!Account\n");
187 g_string_append_printf (elt
, "N%s\n", acc
->name
);
188 g_string_append_printf (elt
, "T%s\n", type
);
189 g_string_append (elt
, "^\n");
190 g_string_append_printf (elt
, "!Type:%s\n", type
);
192 g_io_channel_write_chars(io
, elt
->str
, -1, NULL
, NULL
);
194 g_string_free(elt
, TRUE
);
198 void hb_export_qif_account_single(gchar
*filename
, Account
*acc
)
202 io
= g_io_channel_new_file(filename
, "w", NULL
);
205 g_message("file error on: %s", filename
);
206 //retval = XML_IO_ERROR;
210 hb_export_qif_elt_acc(io
, acc
);
211 hb_export_qif_elt_txn(io
, acc
);
212 g_io_channel_unref (io
);
217 void hb_export_qif_account_all(gchar
*filename
)
222 io
= g_io_channel_new_file(filename
, "w", NULL
);
225 g_message("file error on: %s", filename
);
226 //retval = XML_IO_ERROR;
230 //todo: save accounts in order
231 //todo: save transfer transaction once
233 lacc
= list
= g_hash_table_get_values(GLOBALS
->h_acc
);
236 Account
*item
= list
->data
;
238 hb_export_qif_elt_acc(io
, item
);
239 hb_export_qif_elt_txn(io
, item
);
241 list
= g_list_next(list
);
245 g_io_channel_unref (io
);
251 /* = = = = = = = = beta feature version = = = = = = = = */
253 //static GtkPrintSettings *settings = NULL;
254 //static GtkPageSetup *page_setup = NULL;
257 static void papersize(PdfPrintContext
*ppc
)
259 //GList *list, *item;
263 DB( g_print("[papersize]\n") );
265 name
= gtk_paper_size_get_default();
267 DB( g_print("- def paper is %s\n", name
) );
269 ps
= gtk_paper_size_new(name
);
273 /*GtkPageSetup *new_page_setup;
275 if (settings == NULL)
276 settings = gtk_print_settings_new ();
278 new_page_setup = gtk_print_run_page_setup_dialog (NULL,
279 page_setup, settings);
282 g_object_unref (page_setup);
284 page_setup = new_page_setup;
288 gdouble w
, h
, mt
, mb
, ml
, mr
;
289 w
= gtk_paper_size_get_width(ps
, GTK_UNIT_MM
);
290 h
= gtk_paper_size_get_height(ps
, GTK_UNIT_MM
);
291 mt
= gtk_paper_size_get_default_top_margin(ps
, GTK_UNIT_MM
);
292 mr
= gtk_paper_size_get_default_right_margin(ps
, GTK_UNIT_MM
);
293 mb
= gtk_paper_size_get_default_bottom_margin(ps
, GTK_UNIT_MM
);
294 ml
= gtk_paper_size_get_default_left_margin(ps
, GTK_UNIT_MM
);
296 DB( g_print("- name: %s\n", gtk_paper_size_get_display_name(ps
)) );
297 DB( g_print("- w: %f (%f)\n- h: %f (%f)\n", w
, w
/PANGO_SCALE
, h
, h
/PANGO_SCALE
) );
298 DB( g_print("- margin: %f %f %f %f\n", mt
, mr
, mb
, ml
) );
309 gtk_paper_size_free(ps
);
311 /* list all paper size */
313 list = gtk_paper_size_get_paper_sizes (FALSE);
314 item = g_list_first(list);
320 g_print("- name: %s\n", gtk_paper_size_get_display_name(ps));
321 gtk_paper_size_free(ps);
323 item = g_list_next(item);
330 #define FONT "Helvetica 9px"
332 //#define PDF_MARGIN 24
333 #define PDF_COL_MARGIN 8
334 #define PDF_LINE_MARGIN 2
335 #define PDF_FONT_NORMAL 9
336 #define PDF_FONT_TITLE 12
342 #define HEX_R(xcol) (((xcol>>24) & 0xFF)/255)
343 #define HEX_G(xcol) (((xcol>>16) & 0xFF)/255)
344 #define HEX_B(xcol) (((xcol>> 8) & 0xFF)/255)
348 static void hb_pdf_draw_help_rect(cairo_t
*cr
, gint32 xcol
, double x
, double y
, double w
, double h
)
352 cairo_set_line_width(cr
, 1.0);
353 cairo_set_source_rgba(cr
, HEX_R(xcol
), HEX_G(xcol
), HEX_B(xcol
), 1.0); //alpha is unused for now
354 cairo_rectangle (cr
, x
, y
, w
, h
);
364 static void hb_pdf_draw_line(PdfPrintContext
*ppc
, cairo_t
*cr
, gdouble y
, gboolean bold
, gboolean rulehint
)
370 /* Create a PangoLayout, set the font and text */
371 layout
= pango_cairo_create_layout (cr
);
373 //desc = pango_font_description_from_string (FONT);
375 pango_font_description_set_weight(ppc
->desc
, PANGO_WEIGHT_BOLD
);
377 pango_font_description_set_weight(ppc
->desc
, PANGO_WEIGHT_NORMAL
);
379 pango_layout_set_font_description (layout
, ppc
->desc
);
388 cairo_set_source_rgb(cr
, 0.9, 0.9, 0.9);
389 cairo_rectangle (cr
, x
, y
, ppc
->w
- ppc
->ml
- ppc
->mr
, PDF_FONT_NORMAL
);
395 cairo_set_source_rgb(cr
, 0.0, 0.0, 0.0);
396 cairo_move_to(cr
, x
, y
);
398 for(i
=0;i
<PDF_NUMCOL
;i
++)
400 if(ppc
->column_txt
[i
] != NULL
)
404 pango_layout_set_text (layout
, ppc
->column_txt
[i
], -1);
405 pango_layout_get_size (layout
, &width
, &height
);
406 if( i
==1 || i
==2 || i
==3 )
408 pango_layout_set_width(layout
, ppc
->column_width
[i
]*PANGO_SCALE
);
409 pango_layout_set_ellipsize (layout
, PANGO_ELLIPSIZE_END
);
412 //cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
414 if( i
==0 || i
==4 || i
==6 ) // pad right: date/amount/balance
416 //if(*ppc->column_txt[i] != '-')
417 //cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.66); //grey
418 cairo_move_to(cr
, x
+ ppc
->column_width
[i
] - (width
/PANGO_SCALE
) , y
);
421 cairo_move_to(cr
, x
, y
);
423 pango_cairo_show_layout (cr
, layout
);
426 /*cairo_set_line_width(cr, 1.0);
427 cairo_move_to(cr, x, y + .5);
428 cairo_line_to(cr, x + ppc->column_width[i], y+.5);
433 x
+= ppc
->column_width
[i
] + PDF_COL_MARGIN
;
436 g_object_unref (layout
);
441 static void hb_pdf_set_col_title(PdfPrintContext
*ppc
)
443 ppc
->column_txt
[0] = _("Date");
444 ppc
->column_txt
[1] = _("Info");
445 ppc
->column_txt
[2] = _("Payee");
446 ppc
->column_txt
[3] = _("Memo");
447 ppc
->column_txt
[4] = _("Amount");
448 ppc
->column_txt
[5] = "C";
449 ppc
->column_txt
[6] = _("Balance");
453 void hb_export_pdf_listview(GtkTreeView
*treeview
, gchar
*filepath
, gchar
*accname
)
455 cairo_surface_t
*surf
;
458 PangoFontDescription
*desc
;
466 DB( g_print("[gtk-chart] export to pdf\n") );
468 model
= gtk_tree_view_get_model(treeview
);
472 //gchar *filename = "/home/max/Desktop/hb-txn-export.pdf";
473 double width
; //=210 * 2.83;
474 double height
; //=297 * 2.83;
479 surf
= cairo_pdf_surface_create (filepath
, width
, height
);
481 if( cairo_surface_status(surf
) != CAIRO_STATUS_SUCCESS
)
482 //todo: manage error later on
486 cr
= cairo_create (surf
);
487 //cairo_pdf_surface_set_size(surf, width * 2.83, height * 2.83);
489 //g_print("width=%d\n", cairo_image_surface_get_width( surf));
490 double x1
, x2
, y1
, y2
;
491 cairo_clip_extents (cr
, &x1
, &y1
, &x2
, &y2
);
493 DB( g_print("surface w=%f, h=%f\n", x2
- x1
, y2
- y1
) );
494 double pwidth
= x2
- x1
;
497 /* Create a PangoLayout, set the font and text */
498 layout
= pango_cairo_create_layout (cr
);
500 /* get and copy the font from the treeview widget */
501 gtk_style_context_get(gtk_widget_get_style_context(GTK_WIDGET(treeview
)), GTK_STATE_FLAG_NORMAL
, "font", &desc
, NULL
);
502 ppc
.desc
= pango_font_description_copy(desc
);
504 DB( g_print("family: %s\n", pango_font_description_get_family(desc
)) );
505 DB( g_print("size: %d (%d)\n", pango_font_description_get_size (desc
), pango_font_description_get_size (desc
)/PANGO_SCALE
) );
509 /* header is 1 line for date page number at top, then a title in bold, then 2 empty lines */
510 gint header_height
= PDF_FONT_NORMAL
* 2 + PDF_FONT_TITLE
;
511 gint nb_lines
= gtk_tree_model_iter_n_children(model
, NULL
);
513 /* should include here the headertitle line */
515 gint lpp
= floor ((height
-header_height
-ppc
.mt
-ppc
.mb
) / (PDF_FONT_NORMAL
+ PDF_LINE_MARGIN
));
516 gint page
, num_pages
= (nb_lines
- 1) / lpp
+ 1;
518 DB( g_print("\n - should pdf %d lines, lpp=%d, num_pages=%d\n", nb_lines
, lpp
, num_pages
) );
522 gint cur_page_line
= 1;
525 gchar amtbuf
[G_ASCII_DTOSTR_BUF_SIZE
];
526 gchar balbuf
[G_ASCII_DTOSTR_BUF_SIZE
];
528 GDate
*date
= g_date_new ();
530 //cairo_set_font_size(cr, PDF_FONT_NORMAL);
531 pango_font_description_set_absolute_size(ppc
.desc
, PDF_FONT_NORMAL
* PANGO_SCALE
);
532 pango_layout_set_font_description (layout
, ppc
.desc
);
535 hb_pdf_set_col_title(&ppc
);
537 for(col
=0;col
<PDF_NUMCOL
;col
++)
541 ppc
.column_width
[col
] = 0;
542 pango_layout_set_text (layout
, ppc
.column_txt
[col
], -1);
543 pango_layout_get_size (layout
, &tw
, &th
);
544 ppc
.column_width
[col
] = MAX(ppc
.column_width
[col
], tw
/ PANGO_SCALE
);
548 DB( g_print(" - compute width\n") );
550 /* first pass to get max width */
551 valid
= gtk_tree_model_get_iter_first(GTK_TREE_MODEL(model
), &iter
);
557 gtk_tree_model_get (model
, &iter
, MODEL_TXN_POINTER
, &txn
, -1);
560 g_date_set_julian (date
, txn
->date
);
561 g_date_strftime (dbuffer
, 255-1, "%x", date
);
562 pango_layout_set_text (layout
, dbuffer
, -1);
563 pango_layout_get_size (layout
, &tw
, &th
);
564 ppc
.column_width
[i
] = MAX(ppc
.column_width
[i
], tw
/ PANGO_SCALE
);
567 if(txn
->info
!= NULL
&& strlen(txn
->info
) > 0)
569 pango_layout_set_text (layout
, txn
->info
, -1);
570 pango_layout_get_size (layout
, &tw
, &th
);
571 ppc
.column_width
[i
] = MAX(ppc
.column_width
[i
], tw
/ PANGO_SCALE
);
575 hb_strfnum(amtbuf
, G_ASCII_DTOSTR_BUF_SIZE
-1, txn
->amount
, txn
->kcur
, GLOBALS
->minor
);
576 pango_layout_set_text (layout
, amtbuf
, -1);
577 pango_layout_get_size (layout
, &tw
, &th
);
578 ppc
.column_width
[i
] = MAX(ppc
.column_width
[i
], tw
/ PANGO_SCALE
);
581 pango_layout_set_text (layout
, "R", -1);
582 pango_layout_get_size (layout
, &tw
, &th
);
583 ppc
.column_width
[i
] = MAX(ppc
.column_width
[i
], tw
/ PANGO_SCALE
);
586 hb_strfnum(balbuf
, G_ASCII_DTOSTR_BUF_SIZE
-1, txn
->balance
, txn
->kcur
, GLOBALS
->minor
);
587 pango_layout_set_text (layout
, balbuf
, -1);
588 pango_layout_get_size (layout
, &tw
, &th
);
589 ppc
.column_width
[i
] = MAX(ppc
.column_width
[i
], tw
/ PANGO_SCALE
);
591 valid
= gtk_tree_model_iter_next(GTK_TREE_MODEL(model
), &iter
);
594 /* distribute remaining size */
595 gdouble tmp
= pwidth
- ppc
.ml
- ppc
.mr
- (PDF_COL_MARGIN
*PDF_NUMCOL
);
598 DB( g_print(" page width=%f, remain width=%f\n", pwidth
, tmp
) );
600 tmp
-= ppc
.column_width
[0];
601 tmp
-= ppc
.column_width
[4];
602 tmp
-= ppc
.column_width
[5];
603 tmp
-= ppc
.column_width
[6];
605 /* info=1/4 payee=1/4 memo=2/4 */
606 ppc
.column_width
[1] = tmp
/ 4;;
607 ppc
.column_width
[2] = tmp
/ 4;
608 ppc
.column_width
[3] = 2*tmp
/ 4;
610 DB( g_print(" page width=%f, remain width=%f\n", width
, tmp
) );
613 for(i
=0;i
<PDF_NUMCOL
;i
++)
614 g_print(" col%d=%g ", i
, ppc
.column_width
[i
]);
619 DB( g_print("\n - start printing\n") );
623 valid
= gtk_tree_model_get_iter_first(GTK_TREE_MODEL(model
), &iter
);
629 gtk_tree_model_get (model
, &iter
, MODEL_TXN_POINTER
, &txn
, -1);
631 //DB( g_print(" - %d, %d, %s\n", x, y, txn->memo) );
632 if(cur_page_line
== 1)
637 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
));
638 hb_pdf_draw_help_rect(cr
, 0xFF00FFFF, ppc
.ml
+0.5, ppc
.mt
+0.5, width
-(ppc
.ml
+ppc
.mr
), header_height
);
641 cairo_set_source_rgb(cr
, 0.0, 0.0, 0.0);
643 // draw account title
644 pango_font_description_set_absolute_size(ppc
.desc
, PDF_FONT_TITLE
* PANGO_SCALE
);
645 pango_layout_set_font_description (layout
, ppc
.desc
);
647 pango_layout_set_text (layout
, accname
, -1);
648 pango_layout_get_pixel_size (layout
, &tw
, &th
);
649 cairo_move_to(cr
, pwidth
/2 - (tw
/2), ppc
.mt
);
650 pango_cairo_show_layout (cr
, layout
);
652 // draw column titles
653 pango_font_description_set_absolute_size(ppc
.desc
, PDF_FONT_NORMAL
* PANGO_SCALE
);
654 pango_layout_set_font_description (layout
, ppc
.desc
);
656 g_sprintf(dbuffer
, "Page %d/%d", page
, num_pages
);
657 pango_layout_set_text (layout
, dbuffer
, -1);
658 pango_layout_get_pixel_size (layout
, &tw
, &th
);
659 cairo_move_to(cr
, pwidth
- ppc
.mr
- tw
, ppc
.mt
);
660 pango_cairo_show_layout (cr
, layout
);
663 y
= ppc
.mt
+ header_height
- (PDF_FONT_NORMAL
+ PDF_LINE_MARGIN
);
664 hb_pdf_set_col_title(&ppc
);
666 hb_pdf_draw_line(&ppc
, cr
, y
, TRUE
, FALSE
);
669 /* print a single line */
671 y
= ppc
.mt
+ header_height
+ (cur_page_line
* (PDF_FONT_NORMAL
+ PDF_LINE_MARGIN
));
676 for(i
=0;i
<PDF_NUMCOL
;i
++)
678 ppc
.column_txt
[i
] = NULL
;
682 g_date_set_julian (date
, txn
->date
);
683 g_date_strftime (dbuffer
, 255-1, "%x", date
);
684 ppc
.column_txt
[i
] = dbuffer
;
687 ppc
.column_txt
[i
] = txn
->info
;
690 Payee
*p
= da_pay_get(txn
->kpay
);
692 ppc
.column_txt
[i
] = p
->name
;
695 /*Category *c = da_cat_get(txn->kcat);
697 ppc.column_txt[i] = da_cat_get_fullname(c);*/
698 ppc
.column_txt
[i
] = txn
->memo
;
701 hb_strfnum(amtbuf
, G_ASCII_DTOSTR_BUF_SIZE
-1, txn
->amount
, txn
->kcur
, GLOBALS
->minor
);
702 ppc
.column_txt
[i
] = amtbuf
;
705 ppc
.column_txt
[i
] = "";
706 if(txn
->status
== TXN_STATUS_CLEARED
)
707 ppc
.column_txt
[i
] = "c";
709 if(txn
->status
== TXN_STATUS_RECONCILED
)
710 ppc
.column_txt
[i
] = "R";
713 hb_strfnum(balbuf
, G_ASCII_DTOSTR_BUF_SIZE
-1, txn
->balance
, txn
->kcur
, GLOBALS
->minor
);
714 ppc
.column_txt
[i
] = balbuf
;
716 hb_pdf_draw_line(&ppc
, cr
, y
, FALSE
, (cur_page_line
% 2));
718 /* free any fullcat name */
719 /*if(ppc.column_txt[3] != NULL)
720 g_free(ppc.column_txt[3]);*/
723 if(cur_page_line
>= lpp
)
725 DB( g_print("\n - next page %d\n", page
) );
734 valid
= gtk_tree_model_iter_next(GTK_TREE_MODEL(model
), &iter
);
739 g_object_unref (layout
);
740 pango_font_description_free (ppc
.desc
);
743 cairo_surface_destroy (surf
);
This page took 0.062348 seconds and 4 git commands to generate.