File: qof-expenses.c

package info (click to toggle)
gpe-expenses 0.1.9-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,688 kB
  • sloc: sh: 11,563; ansic: 10,306; makefile: 165
file content (870 lines) | stat: -rw-r--r-- 23,563 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
/***************************************************************************
 *            qof-expenses.c
 *
 *  Thu Oct 21 07:59:13 2004-2005
 *  Copyright  2004-2009  Neil Williams  <linux@codehelp.co.uk>
 ****************************************************************************/
/*
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */

#include "config.h"
#include <stdlib.h>
#include <glib.h>
#include <libintl.h>
#include <glib/gprintf.h>
#include <qof.h>
#include <locale.h>
#include "qof-expenses.h"
#define _(String) dgettext (LIBRARY_GETTEXT_PACKAGE, String)

static QofLogModule log_module = GPE_MOD_EXP;

AS_STRING_FUNC  (ExpenseDistance, DISTANCE_LIST)
FROM_STRING_FUNC(ExpenseDistance, DISTANCE_LIST)
AS_STRING_FUNC  (ExpensePayment, PAYMENT_TYPE_LIST)
FROM_STRING_FUNC(ExpensePayment, PAYMENT_TYPE_LIST)
/* leave the macro in place to support types.
 allow translation by mapping the gcc -E output.
*/
AS_STRING_FUNC  (ExpenseType, EXPENSE_TYPE_LIST)
FROM_STRING_FUNC(ExpenseType, EXPENSE_TYPE_LIST)

/** \brief Currency Table data

Table relating the currencies[5] to the actual currency names, mnemonics
 and symbols. In pilot-qof it is indexed by GINT_TO_POINTER(pq_code). */
static GHashTable *qof_currency_table = NULL;

void
qof_currency_foreach(QofCurrencyCB cb, gpointer user_data)
{
	g_hash_table_foreach(qof_currency_table, cb, user_data);
}

/** \brief Populate the currency table with the known currencies.

 Custom currencies are not included.

 All mnemonics are from gnucash and use the ISO4217 namespace

 custom currencies are the same and may end up with the same
 struct: gchar name[16], gchar symbol[4], gchar rate[8].

 Ignore currencies[5] (the five available currencies
 from the main table above) but:

 \todo store currencies[4] from AppInfo which
 are the four custom currencies.
*/

static void
populate_currencies (void)
{
	qof_currency_table = g_hash_table_new(g_direct_hash, g_direct_equal);
	{	/* Australia 	0	100		AU$		AUD */
		QofCurrency *c = g_new0(QofCurrency, 1);
		c->pq_code = 0;
		c->fraction = 100;
		c->symbol = "AU$";
		c->mnemonic = "AUD";
		c->non_utf8 = FALSE;
		g_hash_table_insert(qof_currency_table, 
			GINT_TO_POINTER(c->pq_code), (gpointer)c);
	}
	{	/* Austria 	1	100		€		ATS */
		QofCurrency *c = g_new0(QofCurrency, 1);
		c->pq_code = 1;
		c->fraction = 100;
		c->symbol = "€";
		c->mnemonic = "ATS";
		c->non_utf8 = TRUE;
		g_hash_table_insert(qof_currency_table, 
			GINT_TO_POINTER(c->pq_code), (gpointer)c);
	}
	{	/* Belgium  	2	100		€		BEF */
		QofCurrency *c = g_new0(QofCurrency, 1);
		c->pq_code = 2;
		c->fraction = 100;
		c->symbol = "€";
		c->mnemonic = "BEF";
		c->non_utf8 = TRUE;
		g_hash_table_insert(qof_currency_table, 
			GINT_TO_POINTER(c->pq_code), (gpointer)c);
	}
	{	/* Brazil  	3	100		R$		BRL */
		QofCurrency *c = g_new0(QofCurrency, 1);
		c->pq_code = 3;
		c->fraction = 100;
		c->symbol = "R$";
		c->mnemonic = "BRL";
		c->non_utf8 = FALSE;
		g_hash_table_insert(qof_currency_table, 
			GINT_TO_POINTER(c->pq_code), (gpointer)c);
	}
	{	/* Canada	4	100		$CN		CAD */
		QofCurrency *c = g_new0(QofCurrency, 1);
		c->pq_code = 4;
		c->fraction = 100;
		c->symbol = "$CN";
		c->mnemonic = "CAD";
		c->non_utf8 = FALSE;
		g_hash_table_insert(qof_currency_table, 
			GINT_TO_POINTER(c->pq_code), (gpointer)c);
	}
	{	/* Denmark   	5	100		DKK		DKK */
		QofCurrency *c = g_new0(QofCurrency, 1);
		c->pq_code = 5;
		c->fraction = 100;
		c->symbol = "DKK";
		c->mnemonic = "DKK";
		c->non_utf8 = FALSE;
		g_hash_table_insert(qof_currency_table, 
			GINT_TO_POINTER(c->pq_code), (gpointer)c);
	}
	{	/* Finland   	6	100		€		FIM */
		QofCurrency *c = g_new0(QofCurrency, 1);
		c->pq_code = 6;
		c->fraction = 100;
		c->symbol = "€";
		c->mnemonic = "FIM";
		c->non_utf8 = TRUE;
		g_hash_table_insert(qof_currency_table, 
			GINT_TO_POINTER(c->pq_code), (gpointer)c);
	}
	{	/* France    	7	100		€		FRF */
		QofCurrency *c = g_new0(QofCurrency, 1);
		c->pq_code = 7;
		c->fraction = 100;
		c->symbol = "€";
		c->mnemonic = "FRF";
		c->non_utf8 = TRUE;
		g_hash_table_insert(qof_currency_table, 
			GINT_TO_POINTER(c->pq_code), (gpointer)c);
	}
	{	/* Germany   	8	100		€		DEM */
		QofCurrency *c = g_new0(QofCurrency, 1);
		c->pq_code = 8;
		c->fraction = 100;
		c->symbol = "€";
		c->mnemonic = "DEM";
		c->non_utf8 = TRUE;
		g_hash_table_insert(qof_currency_table, 
			GINT_TO_POINTER(c->pq_code), (gpointer)c);
	}
	{	/* Hong Kong	9	100		HK$		HKD */
		QofCurrency *c = g_new0(QofCurrency, 1);
		c->pq_code = 9;
		c->fraction = 100;
		c->symbol = "HK$";
		c->mnemonic = "HKD";
		c->non_utf8 = FALSE;
		g_hash_table_insert(qof_currency_table, 
			GINT_TO_POINTER(c->pq_code), (gpointer)c);
	}
	{	/* Iceland 	10	100		ISK		ISK */
		QofCurrency *c = g_new0(QofCurrency, 1);
		c->pq_code = 10;
		c->fraction = 100;
		c->symbol = "ISK";
		c->mnemonic = "ISK";
		c->non_utf8 = FALSE;
		g_hash_table_insert(qof_currency_table, 
			GINT_TO_POINTER(c->pq_code), (gpointer)c);
	}
	{	/* Ireland   	11	100		€		IEP */
		QofCurrency *c = g_new0(QofCurrency, 1);
		c->pq_code = 11;
		c->fraction = 100;
		c->symbol = "€";
		c->mnemonic = "IEP";
		c->non_utf8 = TRUE;
		g_hash_table_insert(qof_currency_table, 
			GINT_TO_POINTER(c->pq_code), (gpointer)c);
	}
	{	/* Italy   	12	1		EUR		ITL */
		/* The Italian Lira had a fraction == 1*/
		QofCurrency *c = g_new0(QofCurrency, 1);
		c->pq_code = 12;
		c->fraction = 100;
		c->symbol = "EUR";
		c->mnemonic = "ITL";
		c->non_utf8 = FALSE;
		g_hash_table_insert(qof_currency_table, 
			GINT_TO_POINTER(c->pq_code), (gpointer)c);
	}
	{	/* Japan     	13	1	&#165;	¥		JPY */
		QofCurrency *c = g_new0(QofCurrency, 1);
		c->pq_code = 13;
		c->fraction = 1;
		c->symbol = "¥";
		c->mnemonic = "JPY";
		c->non_utf8 = TRUE;
		g_hash_table_insert(qof_currency_table, 
			GINT_TO_POINTER(c->pq_code), (gpointer)c);
	}
	{	/* Luxembourg 	14	100	&#136;	€		LUF */
		QofCurrency *c = g_new0(QofCurrency, 1);
		c->pq_code = 14;
		c->fraction = 100;
		c->symbol = "€";
		c->mnemonic = "LUF";
		c->non_utf8 = TRUE;
		g_hash_table_insert(qof_currency_table, 
			GINT_TO_POINTER(c->pq_code), (gpointer)c);
	}
	{	/* Mexico   	15	100		MXP		MXP */
		QofCurrency *c = g_new0(QofCurrency, 1);
		c->pq_code = 15;
		c->fraction = 100;
		c->symbol = "MXP";
		c->mnemonic = "MXP";
		c->non_utf8 = FALSE;
		g_hash_table_insert(qof_currency_table, 
			GINT_TO_POINTER(c->pq_code), (gpointer)c);
	}
	{	/* Netherlands 	16	100		€		ANG */
		QofCurrency *c = g_new0(QofCurrency, 1);
		c->pq_code = 16;
		c->fraction = 100;
		c->symbol = "€";
		c->mnemonic = "ANG";
		c->non_utf8 = TRUE;
		g_hash_table_insert(qof_currency_table, 
			GINT_TO_POINTER(c->pq_code), (gpointer)c);
	}
	{	/* New Zealand 	17	100		$NZ		NZD */
		QofCurrency *c = g_new0(QofCurrency, 1);
		c->pq_code = 17;
		c->fraction = 100;
		c->symbol = "$NZ";
		c->mnemonic = "NZD";
		c->non_utf8 = FALSE;
		g_hash_table_insert(qof_currency_table, 
			GINT_TO_POINTER(c->pq_code), (gpointer)c);
	}
	{	/* Norway    	18	100		NOK		NOK */
		QofCurrency *c = g_new0(QofCurrency, 1);
		c->pq_code = 18;
		c->fraction = 100;
		c->symbol = "NOK";
		c->mnemonic = "NOK";
		c->non_utf8 = FALSE;
		g_hash_table_insert(qof_currency_table, 
			GINT_TO_POINTER(c->pq_code), (gpointer)c);
	}
	{	/* Spain   	19	100		€		ESP */
		QofCurrency *c = g_new0(QofCurrency, 1);
		c->pq_code = 19;
		c->fraction = 100;
		c->symbol = "€";
		c->mnemonic = "ESP";
		c->non_utf8 = TRUE;
		g_hash_table_insert(qof_currency_table, 
			GINT_TO_POINTER(c->pq_code), (gpointer)c);
	}
	{	/* Sweden 	20	100		SEK		SEK */
		QofCurrency *c = g_new0(QofCurrency, 1);
		c->pq_code = 20;
		c->fraction = 100;
		c->symbol = "SEK";
		c->mnemonic = "SEK";
		c->non_utf8 = FALSE;
		g_hash_table_insert(qof_currency_table, 
			GINT_TO_POINTER(c->pq_code), (gpointer)c);
	}
	{	/* Switzerland 	21	100		CHF		CHF */
		QofCurrency *c = g_new0(QofCurrency, 1);
		c->pq_code = 21;
		c->fraction = 100;
		c->symbol = "CHF";
		c->mnemonic = "CHF";
		c->non_utf8 = FALSE;
		g_hash_table_insert(qof_currency_table, 
			GINT_TO_POINTER(c->pq_code), (gpointer)c);
	}
	{	/* United Kingdom 22	100		£		GBP */
		QofCurrency *c = g_new0(QofCurrency, 1);
		c->pq_code = 22;
		c->fraction = 100;
		c->symbol = "£";
		c->mnemonic = "GBP";
		c->non_utf8 = TRUE;
		g_hash_table_insert(qof_currency_table, 
			GINT_TO_POINTER(c->pq_code), (gpointer)c);
	}
	{	/* United States  23	100		$US		USD */
		QofCurrency *c = g_new0(QofCurrency, 1);
		c->pq_code = 23;
		c->fraction = 100;
		c->symbol = "$US";
		c->mnemonic = "USD";
		c->non_utf8 = FALSE;
		g_hash_table_insert(qof_currency_table, 
			GINT_TO_POINTER(c->pq_code), (gpointer)c);
	}
	{	/* India     	24	100		Rs		INR */
		QofCurrency *c = g_new0(QofCurrency, 1);
		c->pq_code = 24;
		c->fraction = 100;
		c->symbol = "Rs";
		c->mnemonic = "INR";
		c->non_utf8 = FALSE;
		g_hash_table_insert(qof_currency_table, 
			GINT_TO_POINTER(c->pq_code), (gpointer)c);
	}
	{	/* Indonesia 	25	1		Rp		IDR */
		QofCurrency *c = g_new0(QofCurrency, 1);
		c->pq_code = 25;
		c->fraction = 1;
		c->symbol = "Rp";
		c->mnemonic = "IDR";
		c->non_utf8 = FALSE;
		g_hash_table_insert(qof_currency_table, 
			GINT_TO_POINTER(c->pq_code), (gpointer)c);
	}
	{	/* Korea   	26	100		KRW		KRW  (South) */
		QofCurrency *c = g_new0(QofCurrency, 1);
		c->pq_code = 26;
		c->fraction = 100;
		c->symbol = "KRW";
		c->mnemonic = "KRW";
		c->non_utf8 = FALSE;
		g_hash_table_insert(qof_currency_table, 
			GINT_TO_POINTER(c->pq_code), (gpointer)c);
	}
	{	/* Malaysia  	27	100		RM		MYR */
		QofCurrency *c = g_new0(QofCurrency, 1);
		c->pq_code = 27;
		c->fraction = 100;
		c->symbol = "RM";
		c->mnemonic = "MYR";
		c->non_utf8 = FALSE;
		g_hash_table_insert(qof_currency_table, 
			GINT_TO_POINTER(c->pq_code), (gpointer)c);
	}
	{	/* P.R.C. // People's Rep. China 28	100	RMB		CNY */
		QofCurrency *c = g_new0(QofCurrency, 1);
		c->pq_code = 28;
		c->fraction = 100;
		c->symbol = "RMB";
		c->mnemonic = "CNY";
		c->non_utf8 = FALSE;
		g_hash_table_insert(qof_currency_table, 
			GINT_TO_POINTER(c->pq_code), (gpointer)c);
	}
	{	/* Phillipines	29	100		P		PHP */
		QofCurrency *c = g_new0(QofCurrency, 1);
		c->pq_code = 29;
		c->fraction = 100;
		c->symbol = "P";
		c->mnemonic = "PHP";
		c->non_utf8 = FALSE;
		g_hash_table_insert(qof_currency_table, 
			GINT_TO_POINTER(c->pq_code), (gpointer)c);
	}
	{	/* Singapore 	30	100		$		SGD */
		QofCurrency *c = g_new0(QofCurrency, 1);
		c->pq_code = 30;
		c->fraction = 100;
		c->symbol = "$";
		c->mnemonic = "SGD";
		c->non_utf8 = FALSE;
		g_hash_table_insert(qof_currency_table, 
			GINT_TO_POINTER(c->pq_code), (gpointer)c);
	}
	{	/* Thailand  	31	100		BHT		THB */
		QofCurrency *c = g_new0(QofCurrency, 1);
		c->pq_code = 31;
		c->fraction = 100;
		c->symbol = "BHT";
		c->mnemonic = "THB";
		c->non_utf8 = FALSE;
		g_hash_table_insert(qof_currency_table, 
			GINT_TO_POINTER(c->pq_code), (gpointer)c);
	}
	{	/* Taiwan	32	100		NT$		TWD */
		QofCurrency *c = g_new0(QofCurrency, 1);
		c->pq_code = 32;
		c->fraction = 100;
		c->symbol = "NT$";
		c->mnemonic = "TWD";
		c->non_utf8 = FALSE;
		g_hash_table_insert(qof_currency_table, 
			GINT_TO_POINTER(c->pq_code), (gpointer)c);
	}
	{	/* EU (Euro) 	133	100		€		EUR */
		QofCurrency *c = g_new0(QofCurrency, 1);
		c->pq_code = 133;
		c->fraction = 100;
		c->symbol = "€";
		c->mnemonic = "EUR";
		c->non_utf8 = TRUE;
		g_hash_table_insert(qof_currency_table, 
			GINT_TO_POINTER(c->pq_code), (gpointer)c);
	}
}

static gboolean
check_name (gpointer G_GNUC_UNUSED key, gpointer value, gpointer data)
{
	gchar * mnemonic = (gchar*) data;
	QofCurrency * currency = (QofCurrency*) value;
	if (0 == safe_strcmp (mnemonic, currency->mnemonic))
		return TRUE;
	return FALSE;
}

QofCurrency*
qof_currency_lookup_name (QofInstance * inst, gchar * mnemonic)
{
	QofCurrency *currency;

	currency = NULL;
	if(!qof_currency_table) populate_currencies();
	currency = (QofCurrency*) g_hash_table_find 
		(qof_currency_table, check_name, mnemonic);
	if (!currency) return NULL;
	qof_currency_lookup(inst, currency->pq_code);
	return currency;
}

QofCurrency*
qof_currency_lookup (QofInstance* inst, gint currency_code)
{
	QofCurrency *currency;

	currency = NULL;
	if(!qof_currency_table) populate_currencies();
	currency = (QofCurrency*)g_hash_table_lookup(
		qof_currency_table, GINT_TO_POINTER(currency_code));
	if(!currency)
	{
		PERR (" unsupported currency! %d", currency_code);
		return NULL;
	}
	kvp_frame_set_string(qof_instance_get_slots(inst),
		PQ_CURRENCY_MNEMONIC, currency->mnemonic);
	kvp_frame_set_gint64(qof_instance_get_slots(inst),
		PQ_CURRENCY_FRACTION, currency->fraction);
	if(currency->non_utf8)
	{
		gchar * k_symbol = g_strdup (currency->symbol);
		kvp_frame_set_string(qof_instance_get_slots(inst),
			PQ_CURRENCY_SYMBOL, qof_util_make_utf8(k_symbol));
	}
	else
	{
		kvp_frame_set_string(qof_instance_get_slots(inst),
			PQ_CURRENCY_SYMBOL, currency->symbol);
	}
	return currency;
}

struct QofExp_s
{
	QofInstance inst;
	ExpenseDistance distance_unit;
	struct tm date;
	ExpenseType type;
	ExpensePayment payment;
	gint currency_code;
	gchar *amount;
	gchar *vendor;
	gchar *city;
	gchar *attendees;
	gchar *note;
	gchar *category;
	const gchar* print_string;
	QofCurrency *currency;
	gdouble temp_amount;     /**<  The amount is set before the
		currency code is available so this acts as a buffer until both
		pieces of data are available. */
	gboolean reset_amount;   /**< Denote whether the buffer should be used. */
};

static QofExp *
expense_create (QofBook *book)
{
	QofExp *obj;
	QofCollection *coll;
	GList *all;
	glong nanosecs;
	QofTime *qt;
	QofDate *qd;

	obj = g_new0(QofExp, 1);
	qof_instance_init (&obj->inst, GPE_QOF_EXPENSES, book);
	coll = qof_book_get_collection (book, GPE_QOF_EXPENSES);
	all = qof_collection_get_data (coll);
	all = g_list_prepend (all, obj);
	qof_collection_set_data (coll, all);
	qt = qof_time_get_current ();
	nanosecs = qof_time_get_nanosecs (qt);
	qd = qof_date_from_qtime (qt);
	if (!qof_date_to_struct_tm (qd, &obj->date, &nanosecs))
		DEBUG (" failed to set initial date");
	qof_date_free (qd);
	qof_time_free (qt);
	obj->amount = "0";
	/* 0 == AU$ so use an init value */
	obj->currency_code = -1;
	if(!qof_currency_table) populate_currencies();
	qof_event_gen ((QofEntity*)obj, QOF_EVENT_CREATE, NULL);
	return obj;
}

static const gchar*
qof_exp_paymentAsString(ExpensePayment payment)
{
	const gchar *string;

	string = "Cash"; // following install-expenses convention.
	string = ExpensePaymentasString(payment);
	return string;
}

static const gchar*
qof_exp_typeAsString(ExpenseType type)
{
	const gchar* string;

	string = "Bus"; // following install-expenses convention.
	string = ExpenseTypeasString(type);
	return string;
}

static ExpensePayment
qof_exp_paymentFromString(const gchar* payment_string)
{
	return ExpensePaymentfromString(payment_string);
}

static ExpenseType
qof_exp_typeFromString(const gchar* type_string)
{
	return ExpenseTypefromString(type_string);
}

static QofTime *
exp_getTime (QofExp * e)
{
	QofDate *qd;
	QofTime *qt;

	g_return_val_if_fail (e != NULL, NULL);
	qd = qof_date_from_struct_tm (&e->date);
	qt = qof_date_to_qtime (qd);
	qof_date_free (qd);
	return qt;
}

static gchar*
exp_getType (QofExp * e)
{
	gchar* string;

	g_return_val_if_fail (e != NULL, NULL);
	string = g_strdup(qof_exp_typeAsString(e->type));
	return string;
}

static gchar*
exp_getPayment (QofExp * e)
{
	gchar* string;

	g_return_val_if_fail (e != NULL, NULL);
	string = g_strdup(qof_exp_paymentAsString(e->payment));
	return string;
}

static gint
exp_getCurrency (QofExp * e)
{
	g_return_val_if_fail (e != NULL, -1);
	return e->currency_code;
}

static QofNumeric
exp_getAmount (QofExp * e)
{
	QofNumeric amount;
	gdouble pi_amount;
	gchar *numeric_char;

	amount = qof_numeric_zero ();
	g_return_val_if_fail (e != NULL, amount);
	if(e->amount == 0) { return amount; }
	/* floating point as a string converts to gnc_numeric */
	pi_amount = strtod (e->amount, NULL);
	if(e->currency)
	{
		amount = qof_numeric_from_double (pi_amount, e->currency->fraction,
			QOF_HOW_DENOM_EXACT | QOF_HOW_RND_ROUND);
	}
	else /* default: use the most common fraction in the Palm currency list. */
	{
		amount = qof_numeric_from_double (pi_amount, 100, 
			QOF_HOW_DENOM_EXACT | QOF_HOW_RND_ROUND);
	}
	numeric_char = qof_numeric_to_string(amount);
	g_free(numeric_char);

	if (qof_numeric_check (amount) == QOF_ERROR_OK)
		return amount;
	return qof_numeric_zero ();
}

static const gchar *
exp_getVendor (QofExp * e)
{
	g_return_val_if_fail (e != NULL, NULL);
	return e->vendor;
}

static const gchar *
exp_getCity (QofExp * e)
{
	g_return_val_if_fail (e != NULL, NULL);
	return e->city;
}

static const gchar *
exp_getAttendees (QofExp * e)
{
	g_return_val_if_fail (e != NULL, NULL);
	return e->attendees;
}

static const gchar *
exp_getNote (QofExp * e)
{
	g_return_val_if_fail (e != NULL, NULL);
	return e->note;
}

static const gchar*
exp_getDistance(QofExp *e)
{
	g_return_val_if_fail(e != NULL, NULL);
	return ExpenseDistanceasString(e->distance_unit);
}

static const gchar* 
exp_getCategory(QofExp *e)
{
	g_return_val_if_fail(e != NULL, NULL);
	return e->category;
}

static void
exp_setTime (QofExp * e, QofTime *h)
{
	glong nanosecs;
	QofDate *qd;

	g_return_if_fail (e != NULL);
	if (!h)
		return;
	nanosecs = qof_time_get_nanosecs (h);
	qd = qof_date_from_qtime (h);
	qof_date_to_struct_tm (qd, &e->date, &nanosecs);
	qof_date_free (qd);
}

static void
exp_setType (QofExp * e, const gchar *type_string)
{
	g_return_if_fail (e != NULL);
	e->type = qof_exp_typeFromString(type_string);
}

static void
exp_setPayment (QofExp * e, const gchar *payment_string)
{
	g_return_if_fail (e != NULL);
	e->payment = qof_exp_paymentFromString(payment_string);
}

static void
exp_combine_currency_with_amount(QofExp *e)
{
	g_return_if_fail (e != NULL);
	if(!e->currency || e->currency_code < 0)
	{
		/* the gint32 currency is actually to be set BEFORE the numeric amount. :-( */
		e->currency = qof_currency_lookup((QofInstance*)e, e->currency_code);
	}
	if(!e->currency)
	{
		PERR (" Unable to identify currency fraction."
			" Using two decimal places.");
		/* Amount is stored in the Palm as a string version
		of a floating point number. */
		e->amount = g_strdup_printf ("%.2f", e->temp_amount);
		return;
	}
	switch (e->currency->fraction)
	{
		case 1 : 
		{
			e->amount = g_strdup_printf ("%.0f", e->temp_amount);
			break;
		}
		case 10 : 
		{
			e->amount = g_strdup_printf ("%.1f", e->temp_amount);
			break;
		}
		case 100 : 
		{
			e->amount = g_strdup_printf ("%.2f", e->temp_amount);
			break;
		}
		case 1000 : 
		{
			e->amount = g_strdup_printf ("%.3f", e->temp_amount);
			break;
		}
		default :
		{
			PERR (" Invalid currency fraction."
				" Using two decimal places as default.");
			e->amount = g_strdup_printf ("%.2f", e->temp_amount);
		}
	}
}

static void
exp_setCurrency (QofExp * e, gint data)
{
	g_return_if_fail (e != NULL);
	e->currency_code = data;
	e->currency = qof_currency_lookup((QofInstance*)e, data);
	if(e->reset_amount)
		exp_combine_currency_with_amount(e);
	e->reset_amount = FALSE;
}

static void
exp_setAmount (QofExp * e, QofNumeric h)
{
	g_return_if_fail (e != NULL);
	e->temp_amount = qof_numeric_to_double (h);
	e->reset_amount = TRUE;
	/* if an amount can ever be set without a currency_code,
		   this needs to be reviewed. */
	/** \todo FIXME: INSERT handler can set one without the
	other. Need to use the pref.default_currency? */
	if(e->currency) exp_combine_currency_with_amount(e);
}

static void
exp_setVendor (QofExp * e, gchar *h)
{
	g_return_if_fail (e != NULL);
	e->vendor = g_strdup (qof_util_make_utf8(h));
}

static void
exp_setCity (QofExp * e, gchar *h)
{
	g_return_if_fail (e != NULL);
	e->city = g_strdup (qof_util_make_utf8(h));
}

static void
exp_setAttendees (QofExp * e, gchar *h)
{
	g_return_if_fail (e != NULL);
	e->attendees = g_strdup (qof_util_make_utf8(h));
}

static void
exp_setNote (QofExp * e, gchar *h)
{
	g_return_if_fail (e != NULL);
	e->note = g_strdup (qof_util_make_utf8(h));
}

static void
exp_setDistance(QofExp *e, const gchar *distance_name)
{
	g_return_if_fail(e);
	e->distance_unit = ExpenseDistancefromString(distance_name);
}

static void
exp_setCategory(QofExp *e, gchar *n)
{
	g_return_if_fail(e != NULL);
	e->category = g_strdup(qof_util_make_utf8(n));
}

static const gchar*
expensePrintable (gpointer instance)
{
	QofExp *obj;

	obj = (QofExp*)instance;
	if(!obj) return NULL;
	if(exp_getType(obj))
		return g_strconcat(exp_getType(obj), " ", 
			exp_getVendor(obj), " ", exp_getCity(obj), NULL);
	return NULL;
}

static QofObject expenses_object_def = {
	.interface_version = QOF_OBJECT_VERSION,
	.e_type =            GPE_QOF_EXPENSES,
	.type_label =        QOF_EXPENSES_DESC,
	.create =            ((gpointer)expense_create),
	.book_begin =        NULL,
	.book_end =          NULL,
	.is_dirty =          qof_collection_is_dirty,
	.mark_clean =        qof_collection_mark_clean,
	.foreach =           qof_collection_foreach,
	.printable =         expensePrintable,
	.version_cmp =       (gint (*)(gpointer, gpointer)) qof_instance_version_cmp,
};

gboolean
ExpensesRegister (void)
{
	static QofParam params[] = {
	 { EXP_DATE,       QOF_TYPE_TIME,    (QofAccessFunc) exp_getTime,      (QofSetterFunc) exp_setTime, NULL },
	 { EXP_TYPE,       QOF_TYPE_STRING,  (QofAccessFunc) exp_getType,      (QofSetterFunc) exp_setType, NULL },
	 { EXP_PAYMENT,    QOF_TYPE_STRING,  (QofAccessFunc) exp_getPayment,   (QofSetterFunc) exp_setPayment, NULL },
	 { EXP_CURRENCY,   QOF_TYPE_INT32,   (QofAccessFunc) exp_getCurrency,  (QofSetterFunc) exp_setCurrency, NULL },
	 { EXP_AMOUNT,     QOF_TYPE_NUMERIC, (QofAccessFunc) exp_getAmount,    (QofSetterFunc) exp_setAmount, NULL },
	 { EXP_VENDOR,     QOF_TYPE_STRING,  (QofAccessFunc) exp_getVendor,    (QofSetterFunc) exp_setVendor, NULL },
	 { EXP_CITY,       QOF_TYPE_STRING,  (QofAccessFunc) exp_getCity,      (QofSetterFunc) exp_setCity, NULL },
	 { EXP_ATTENDEES,  QOF_TYPE_STRING,  (QofAccessFunc) exp_getAttendees, (QofSetterFunc) exp_setAttendees, NULL },
	 { EXP_NOTE,       QOF_TYPE_STRING,  (QofAccessFunc) exp_getNote,      (QofSetterFunc) exp_setNote, NULL },
	 { EXP_DISTANCE,   QOF_TYPE_STRING,  (QofAccessFunc) exp_getDistance,  (QofSetterFunc) exp_setDistance, NULL },
	 { EXP_CATEGORY,   QOF_TYPE_STRING,  (QofAccessFunc) exp_getCategory,  (QofSetterFunc) exp_setCategory, NULL },
	 { EXP_KVP,        QOF_TYPE_KVP,     (QofAccessFunc) qof_instance_get_slots, NULL, NULL },
	 { QOF_PARAM_BOOK, QOF_ID_BOOK,      (QofAccessFunc) qof_instance_get_book, NULL, NULL },
	 { QOF_PARAM_GUID, QOF_TYPE_GUID,    (QofAccessFunc) qof_instance_get_guid, NULL, NULL },
	 { NULL, NULL, NULL, NULL, NULL },
	};

	bindtextdomain (LIBRARY_GETTEXT_PACKAGE, LOCALE_DIR);

	qof_class_register (GPE_QOF_EXPENSES, NULL, params);
	if(!qof_currency_table) populate_currencies();

	return qof_object_register (&expenses_object_def);
}