26 #include <glib/gprintf.h>
30 #define _(String) dgettext (LIBRARY_GETTEXT_PACKAGE, String)
34 AS_STRING_FUNC (ExpenseDistance, DISTANCE_LIST)
35 FROM_STRING_FUNC(ExpenseDistance, DISTANCE_LIST)
37 FROM_STRING_FUNC(ExpensePayment, PAYMENT_TYPE_LIST)
42 FROM_STRING_FUNC(ExpenseType, EXPENSE_TYPE_LIST)
48 static GHashTable *qof_currency_table = NULL;
51 qof_currency_foreach(QofCurrencyCB cb, gpointer user_data)
53 g_hash_table_foreach(qof_currency_table, cb, user_data);
73 populate_currencies (
void)
75 qof_currency_table = g_hash_table_new(g_direct_hash, g_direct_equal);
83 g_hash_table_insert(qof_currency_table,
84 GINT_TO_POINTER(c->
pq_code), (gpointer)c);
93 g_hash_table_insert(qof_currency_table,
94 GINT_TO_POINTER(c->
pq_code), (gpointer)c);
103 g_hash_table_insert(qof_currency_table,
104 GINT_TO_POINTER(c->
pq_code), (gpointer)c);
113 g_hash_table_insert(qof_currency_table,
114 GINT_TO_POINTER(c->
pq_code), (gpointer)c);
123 g_hash_table_insert(qof_currency_table,
124 GINT_TO_POINTER(c->
pq_code), (gpointer)c);
133 g_hash_table_insert(qof_currency_table,
134 GINT_TO_POINTER(c->
pq_code), (gpointer)c);
143 g_hash_table_insert(qof_currency_table,
144 GINT_TO_POINTER(c->
pq_code), (gpointer)c);
153 g_hash_table_insert(qof_currency_table,
154 GINT_TO_POINTER(c->
pq_code), (gpointer)c);
163 g_hash_table_insert(qof_currency_table,
164 GINT_TO_POINTER(c->
pq_code), (gpointer)c);
173 g_hash_table_insert(qof_currency_table,
174 GINT_TO_POINTER(c->
pq_code), (gpointer)c);
183 g_hash_table_insert(qof_currency_table,
184 GINT_TO_POINTER(c->
pq_code), (gpointer)c);
193 g_hash_table_insert(qof_currency_table,
194 GINT_TO_POINTER(c->
pq_code), (gpointer)c);
204 g_hash_table_insert(qof_currency_table,
205 GINT_TO_POINTER(c->
pq_code), (gpointer)c);
214 g_hash_table_insert(qof_currency_table,
215 GINT_TO_POINTER(c->
pq_code), (gpointer)c);
224 g_hash_table_insert(qof_currency_table,
225 GINT_TO_POINTER(c->
pq_code), (gpointer)c);
234 g_hash_table_insert(qof_currency_table,
235 GINT_TO_POINTER(c->
pq_code), (gpointer)c);
244 g_hash_table_insert(qof_currency_table,
245 GINT_TO_POINTER(c->
pq_code), (gpointer)c);
254 g_hash_table_insert(qof_currency_table,
255 GINT_TO_POINTER(c->
pq_code), (gpointer)c);
264 g_hash_table_insert(qof_currency_table,
265 GINT_TO_POINTER(c->
pq_code), (gpointer)c);
274 g_hash_table_insert(qof_currency_table,
275 GINT_TO_POINTER(c->
pq_code), (gpointer)c);
284 g_hash_table_insert(qof_currency_table,
285 GINT_TO_POINTER(c->
pq_code), (gpointer)c);
294 g_hash_table_insert(qof_currency_table,
295 GINT_TO_POINTER(c->
pq_code), (gpointer)c);
304 g_hash_table_insert(qof_currency_table,
305 GINT_TO_POINTER(c->
pq_code), (gpointer)c);
314 g_hash_table_insert(qof_currency_table,
315 GINT_TO_POINTER(c->
pq_code), (gpointer)c);
324 g_hash_table_insert(qof_currency_table,
325 GINT_TO_POINTER(c->
pq_code), (gpointer)c);
334 g_hash_table_insert(qof_currency_table,
335 GINT_TO_POINTER(c->
pq_code), (gpointer)c);
344 g_hash_table_insert(qof_currency_table,
345 GINT_TO_POINTER(c->
pq_code), (gpointer)c);
354 g_hash_table_insert(qof_currency_table,
355 GINT_TO_POINTER(c->
pq_code), (gpointer)c);
364 g_hash_table_insert(qof_currency_table,
365 GINT_TO_POINTER(c->
pq_code), (gpointer)c);
374 g_hash_table_insert(qof_currency_table,
375 GINT_TO_POINTER(c->
pq_code), (gpointer)c);
384 g_hash_table_insert(qof_currency_table,
385 GINT_TO_POINTER(c->
pq_code), (gpointer)c);
394 g_hash_table_insert(qof_currency_table,
395 GINT_TO_POINTER(c->
pq_code), (gpointer)c);
404 g_hash_table_insert(qof_currency_table,
405 GINT_TO_POINTER(c->
pq_code), (gpointer)c);
414 g_hash_table_insert(qof_currency_table,
415 GINT_TO_POINTER(c->
pq_code), (gpointer)c);
420 check_name (gpointer G_GNUC_UNUSED key, gpointer value, gpointer data)
422 gchar * mnemonic = (gchar*) data;
424 if (0 == safe_strcmp (mnemonic, currency->
mnemonic))
435 if(!qof_currency_table) populate_currencies();
437 (qof_currency_table, check_name, mnemonic);
438 if (!currency)
return NULL;
449 if(!qof_currency_table) populate_currencies();
451 qof_currency_table, GINT_TO_POINTER(currency_code));
454 PERR (
" unsupported currency! %d", currency_code);
457 kvp_frame_set_string(qof_instance_get_slots(inst),
459 kvp_frame_set_gint64(qof_instance_get_slots(inst),
463 gchar * k_symbol = g_strdup (currency->
symbol);
464 kvp_frame_set_string(qof_instance_get_slots(inst),
469 kvp_frame_set_string(qof_instance_get_slots(inst),
478 ExpenseDistance distance_unit;
489 const gchar* print_string;
498 expense_create (QofBook *book)
508 qof_instance_init (&obj->inst, GPE_QOF_EXPENSES, book);
509 coll = qof_book_get_collection (book, GPE_QOF_EXPENSES);
510 all = qof_collection_get_data (coll);
511 all = g_list_prepend (all, obj);
512 qof_collection_set_data (coll, all);
513 qt = qof_time_get_current ();
514 nanosecs = qof_time_get_nanosecs (qt);
515 qd = qof_date_from_qtime (qt);
516 if (!qof_date_to_struct_tm (qd, &obj->date, &nanosecs))
517 DEBUG (
" failed to set initial date");
522 obj->currency_code = -1;
523 if(!qof_currency_table) populate_currencies();
524 qof_event_gen ((
QofEntity*)obj, QOF_EVENT_CREATE, NULL);
534 string = ExpensePaymentasString(payment);
544 string = ExpenseTypeasString(type);
549 qof_exp_paymentFromString(
const gchar* payment_string)
551 return ExpensePaymentfromString(payment_string);
555 qof_exp_typeFromString(
const gchar* type_string)
557 return ExpenseTypefromString(type_string);
566 g_return_val_if_fail (e != NULL, NULL);
567 qd = qof_date_from_struct_tm (&e->date);
568 qt = qof_date_to_qtime (qd);
578 g_return_val_if_fail (e != NULL, NULL);
579 string = g_strdup(qof_exp_typeAsString(e->type));
584 exp_getPayment (
QofExp * e)
588 g_return_val_if_fail (e != NULL, NULL);
589 string = g_strdup(qof_exp_paymentAsString(e->payment));
594 exp_getCurrency (
QofExp * e)
596 g_return_val_if_fail (e != NULL, -1);
597 return e->currency_code;
601 exp_getAmount (
QofExp * e)
607 amount = qof_numeric_zero ();
608 g_return_val_if_fail (e != NULL, amount);
609 if(e->amount == 0) {
return amount; }
611 pi_amount = strtod (e->amount, NULL);
614 amount = qof_numeric_from_double (pi_amount, e->currency->
fraction,
615 QOF_HOW_DENOM_EXACT | QOF_HOW_RND_ROUND);
619 amount = qof_numeric_from_double (pi_amount, 100,
620 QOF_HOW_DENOM_EXACT | QOF_HOW_RND_ROUND);
622 numeric_char = qof_numeric_to_string(amount);
623 g_free(numeric_char);
625 if (qof_numeric_check (amount) == QOF_ERROR_OK)
627 return qof_numeric_zero ();
631 exp_getVendor (
QofExp * e)
633 g_return_val_if_fail (e != NULL, NULL);
640 g_return_val_if_fail (e != NULL, NULL);
645 exp_getAttendees (
QofExp * e)
647 g_return_val_if_fail (e != NULL, NULL);
654 g_return_val_if_fail (e != NULL, NULL);
659 exp_getDistance(
QofExp *e)
661 g_return_val_if_fail(e != NULL, NULL);
662 return ExpenseDistanceasString(e->distance_unit);
666 exp_getCategory(
QofExp *e)
668 g_return_val_if_fail(e != NULL, NULL);
673 exp_setTime (
QofExp * e, QofTime *h)
678 g_return_if_fail (e != NULL);
681 nanosecs = qof_time_get_nanosecs (h);
682 qd = qof_date_from_qtime (h);
683 qof_date_to_struct_tm (qd, &e->date, &nanosecs);
688 exp_setType (
QofExp * e,
const gchar *type_string)
690 g_return_if_fail (e != NULL);
691 e->type = qof_exp_typeFromString(type_string);
695 exp_setPayment (
QofExp * e,
const gchar *payment_string)
697 g_return_if_fail (e != NULL);
698 e->payment = qof_exp_paymentFromString(payment_string);
702 exp_combine_currency_with_amount(
QofExp *e)
704 g_return_if_fail (e != NULL);
705 if(!e->currency || e->currency_code < 0)
712 PERR (
" Unable to identify currency fraction."
713 " Using two decimal places.");
716 e->amount = g_strdup_printf (
"%.2f", e->
temp_amount);
723 e->amount = g_strdup_printf (
"%.0f", e->
temp_amount);
728 e->amount = g_strdup_printf (
"%.1f", e->
temp_amount);
733 e->amount = g_strdup_printf (
"%.2f", e->
temp_amount);
738 e->amount = g_strdup_printf (
"%.3f", e->
temp_amount);
743 PERR (
" Invalid currency fraction."
744 " Using two decimal places as default.");
745 e->amount = g_strdup_printf (
"%.2f", e->
temp_amount);
751 exp_setCurrency (
QofExp * e, gint data)
753 g_return_if_fail (e != NULL);
754 e->currency_code = data;
757 exp_combine_currency_with_amount(e);
764 g_return_if_fail (e != NULL);
771 if(e->currency) exp_combine_currency_with_amount(e);
775 exp_setVendor (
QofExp * e, gchar *h)
777 g_return_if_fail (e != NULL);
778 e->vendor = g_strdup (qof_util_make_utf8(h));
782 exp_setCity (
QofExp * e, gchar *h)
784 g_return_if_fail (e != NULL);
785 e->city = g_strdup (qof_util_make_utf8(h));
789 exp_setAttendees (
QofExp * e, gchar *h)
791 g_return_if_fail (e != NULL);
792 e->attendees = g_strdup (qof_util_make_utf8(h));
796 exp_setNote (
QofExp * e, gchar *h)
798 g_return_if_fail (e != NULL);
799 e->note = g_strdup (qof_util_make_utf8(h));
803 exp_setDistance(
QofExp *e,
const gchar *distance_name)
806 e->distance_unit = ExpenseDistancefromString(distance_name);
810 exp_setCategory(
QofExp *e, gchar *n)
812 g_return_if_fail(e != NULL);
813 e->category = g_strdup(qof_util_make_utf8(n));
817 expensePrintable (gpointer instance)
822 if(!obj)
return NULL;
824 return g_strconcat(exp_getType(obj),
" ",
825 exp_getVendor(obj),
" ", exp_getCity(obj), NULL);
830 .interface_version = QOF_OBJECT_VERSION,
831 .e_type = GPE_QOF_EXPENSES,
832 .type_label = QOF_EXPENSES_DESC,
833 .create = ((gpointer)expense_create),
836 .is_dirty = qof_collection_is_dirty,
837 .mark_clean = qof_collection_mark_clean,
838 .foreach = qof_collection_foreach,
839 .printable = expensePrintable,
840 .version_cmp = (gint (*)(gpointer, gpointer)) qof_instance_version_cmp,
847 { EXP_DATE, QOF_TYPE_TIME, (QofAccessFunc) exp_getTime, (QofSetterFunc) exp_setTime, NULL },
848 { EXP_TYPE, QOF_TYPE_STRING, (QofAccessFunc) exp_getType, (QofSetterFunc) exp_setType, NULL },
849 { EXP_PAYMENT, QOF_TYPE_STRING, (QofAccessFunc) exp_getPayment, (QofSetterFunc) exp_setPayment, NULL },
850 { EXP_CURRENCY, QOF_TYPE_INT32, (QofAccessFunc) exp_getCurrency, (QofSetterFunc) exp_setCurrency, NULL },
851 { EXP_AMOUNT, QOF_TYPE_NUMERIC, (QofAccessFunc) exp_getAmount, (QofSetterFunc) exp_setAmount, NULL },
852 { EXP_VENDOR, QOF_TYPE_STRING, (QofAccessFunc) exp_getVendor, (QofSetterFunc) exp_setVendor, NULL },
853 { EXP_CITY, QOF_TYPE_STRING, (QofAccessFunc) exp_getCity, (QofSetterFunc) exp_setCity, NULL },
854 { EXP_ATTENDEES, QOF_TYPE_STRING, (QofAccessFunc) exp_getAttendees, (QofSetterFunc) exp_setAttendees, NULL },
855 { EXP_NOTE, QOF_TYPE_STRING, (QofAccessFunc) exp_getNote, (QofSetterFunc) exp_setNote, NULL },
856 { EXP_DISTANCE, QOF_TYPE_STRING, (QofAccessFunc) exp_getDistance, (QofSetterFunc) exp_setDistance, NULL },
857 { EXP_CATEGORY, QOF_TYPE_STRING, (QofAccessFunc) exp_getCategory, (QofSetterFunc) exp_setCategory, NULL },
858 { EXP_KVP, QOF_TYPE_KVP, (QofAccessFunc) qof_instance_get_slots, NULL, NULL },
859 { QOF_PARAM_BOOK, QOF_ID_BOOK, (QofAccessFunc) qof_instance_get_book, NULL, NULL },
860 { QOF_PARAM_GUID, QOF_TYPE_GUID, (QofAccessFunc) qof_instance_get_guid, NULL, NULL },
861 { NULL, NULL, NULL, NULL, NULL },
864 bindtextdomain (LIBRARY_GETTEXT_PACKAGE, LOCALE_DIR);
866 qof_class_register (GPE_QOF_EXPENSES, NULL, params);
867 if(!qof_currency_table) populate_currencies();
869 return qof_object_register (&expenses_object_def);