00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "config.h"
00023 #include <stdlib.h>
00024 #include <glib.h>
00025 #include <libintl.h>
00026 #include <glib/gprintf.h>
00027 #include <qof.h>
00028 #include <locale.h>
00029 #include "qof-expenses.h"
00030 #define _(String) dgettext (LIBRARY_GETTEXT_PACKAGE, String)
00031
00032 static QofLogModule log_module = GPE_MOD_EXP;
00033
00034 AS_STRING_FUNC (ExpenseDistance, DISTANCE_LIST)
00035 FROM_STRING_FUNC(ExpenseDistance, DISTANCE_LIST)
00036 AS_STRING_FUNC (ExpensePayment, PAYMENT_TYPE_LIST)
00037 FROM_STRING_FUNC(ExpensePayment, PAYMENT_TYPE_LIST)
00038
00039
00040
00041 AS_STRING_FUNC (ExpenseType, EXPENSE_TYPE_LIST)
00042 FROM_STRING_FUNC(ExpenseType, EXPENSE_TYPE_LIST)
00043
00049 static gchar *
00050 qof_main_make_utf8 (gchar * string)
00051 {
00052 gchar *value;
00053
00054 if (!string)
00055 return NULL;
00056 if (g_utf8_validate (string, -1, NULL))
00057 return string;
00058 value = g_locale_to_utf8 (string, -1, NULL, NULL, NULL);
00059 if (!value)
00060 {
00061 PWARN (" unable to convert from locale %s", string);
00062 PINFO ("trying to convert from ISO-8859-15.");
00063 value = g_convert (string, -1, "UTF-8", "ISO-8859-15",
00064 NULL, NULL, NULL);
00065 if (!value)
00066 {
00067 PERR (" conversion failed");
00068 return string;
00069 }
00070 return value;
00071 }
00072 return value;
00073 }
00074
00079 static GHashTable *gpe_currency_table = NULL;
00080
00081 void
00082 gpe_currency_foreach(GpeCurrencyCB cb, gpointer user_data)
00083 {
00084 g_hash_table_foreach(gpe_currency_table, cb, user_data);
00085 }
00086
00103 static void
00104 populate_currencies (void)
00105 {
00106 gpe_currency_table = g_hash_table_new(g_direct_hash, g_direct_equal);
00107 {
00108 GpeCurrency *c = g_new0(GpeCurrency, 1);
00109 c->pq_code = 0;
00110 c->fraction = 100;
00111 c->symbol = "AU$";
00112 c->mnemonic = "AUD";
00113 c->non_utf8 = FALSE;
00114 g_hash_table_insert(gpe_currency_table,
00115 GINT_TO_POINTER(c->pq_code), (gpointer)c);
00116 }
00117 {
00118 GpeCurrency *c = g_new0(GpeCurrency, 1);
00119 c->pq_code = 1;
00120 c->fraction = 100;
00121 c->symbol = "€";
00122 c->mnemonic = "ATS";
00123 c->non_utf8 = TRUE;
00124 g_hash_table_insert(gpe_currency_table,
00125 GINT_TO_POINTER(c->pq_code), (gpointer)c);
00126 }
00127 {
00128 GpeCurrency *c = g_new0(GpeCurrency, 1);
00129 c->pq_code = 2;
00130 c->fraction = 100;
00131 c->symbol = "€";
00132 c->mnemonic = "BEF";
00133 c->non_utf8 = TRUE;
00134 g_hash_table_insert(gpe_currency_table,
00135 GINT_TO_POINTER(c->pq_code), (gpointer)c);
00136 }
00137 {
00138 GpeCurrency *c = g_new0(GpeCurrency, 1);
00139 c->pq_code = 3;
00140 c->fraction = 100;
00141 c->symbol = "R$";
00142 c->mnemonic = "BRL";
00143 c->non_utf8 = FALSE;
00144 g_hash_table_insert(gpe_currency_table,
00145 GINT_TO_POINTER(c->pq_code), (gpointer)c);
00146 }
00147 {
00148 GpeCurrency *c = g_new0(GpeCurrency, 1);
00149 c->pq_code = 4;
00150 c->fraction = 100;
00151 c->symbol = "$CN";
00152 c->mnemonic = "CAD";
00153 c->non_utf8 = FALSE;
00154 g_hash_table_insert(gpe_currency_table,
00155 GINT_TO_POINTER(c->pq_code), (gpointer)c);
00156 }
00157 {
00158 GpeCurrency *c = g_new0(GpeCurrency, 1);
00159 c->pq_code = 5;
00160 c->fraction = 100;
00161 c->symbol = "DKK";
00162 c->mnemonic = "DKK";
00163 c->non_utf8 = FALSE;
00164 g_hash_table_insert(gpe_currency_table,
00165 GINT_TO_POINTER(c->pq_code), (gpointer)c);
00166 }
00167 {
00168 GpeCurrency *c = g_new0(GpeCurrency, 1);
00169 c->pq_code = 6;
00170 c->fraction = 100;
00171 c->symbol = "€";
00172 c->mnemonic = "FIM";
00173 c->non_utf8 = TRUE;
00174 g_hash_table_insert(gpe_currency_table,
00175 GINT_TO_POINTER(c->pq_code), (gpointer)c);
00176 }
00177 {
00178 GpeCurrency *c = g_new0(GpeCurrency, 1);
00179 c->pq_code = 7;
00180 c->fraction = 100;
00181 c->symbol = "€";
00182 c->mnemonic = "FRF";
00183 c->non_utf8 = TRUE;
00184 g_hash_table_insert(gpe_currency_table,
00185 GINT_TO_POINTER(c->pq_code), (gpointer)c);
00186 }
00187 {
00188 GpeCurrency *c = g_new0(GpeCurrency, 1);
00189 c->pq_code = 8;
00190 c->fraction = 100;
00191 c->symbol = "€";
00192 c->mnemonic = "DEM";
00193 c->non_utf8 = TRUE;
00194 g_hash_table_insert(gpe_currency_table,
00195 GINT_TO_POINTER(c->pq_code), (gpointer)c);
00196 }
00197 {
00198 GpeCurrency *c = g_new0(GpeCurrency, 1);
00199 c->pq_code = 9;
00200 c->fraction = 100;
00201 c->symbol = "HK$";
00202 c->mnemonic = "HKD";
00203 c->non_utf8 = FALSE;
00204 g_hash_table_insert(gpe_currency_table,
00205 GINT_TO_POINTER(c->pq_code), (gpointer)c);
00206 }
00207 {
00208 GpeCurrency *c = g_new0(GpeCurrency, 1);
00209 c->pq_code = 10;
00210 c->fraction = 100;
00211 c->symbol = "ISK";
00212 c->mnemonic = "ISK";
00213 c->non_utf8 = FALSE;
00214 g_hash_table_insert(gpe_currency_table,
00215 GINT_TO_POINTER(c->pq_code), (gpointer)c);
00216 }
00217 {
00218 GpeCurrency *c = g_new0(GpeCurrency, 1);
00219 c->pq_code = 11;
00220 c->fraction = 100;
00221 c->symbol = "€";
00222 c->mnemonic = "IEP";
00223 c->non_utf8 = TRUE;
00224 g_hash_table_insert(gpe_currency_table,
00225 GINT_TO_POINTER(c->pq_code), (gpointer)c);
00226 }
00227 {
00228
00229 GpeCurrency *c = g_new0(GpeCurrency, 1);
00230 c->pq_code = 12;
00231 c->fraction = 100;
00232 c->symbol = "EUR";
00233 c->mnemonic = "ITL";
00234 c->non_utf8 = FALSE;
00235 g_hash_table_insert(gpe_currency_table,
00236 GINT_TO_POINTER(c->pq_code), (gpointer)c);
00237 }
00238 {
00239 GpeCurrency *c = g_new0(GpeCurrency, 1);
00240 c->pq_code = 13;
00241 c->fraction = 1;
00242 c->symbol = "¥";
00243 c->mnemonic = "JPY";
00244 c->non_utf8 = TRUE;
00245 g_hash_table_insert(gpe_currency_table,
00246 GINT_TO_POINTER(c->pq_code), (gpointer)c);
00247 }
00248 {
00249 GpeCurrency *c = g_new0(GpeCurrency, 1);
00250 c->pq_code = 14;
00251 c->fraction = 100;
00252 c->symbol = "€";
00253 c->mnemonic = "LUF";
00254 c->non_utf8 = TRUE;
00255 g_hash_table_insert(gpe_currency_table,
00256 GINT_TO_POINTER(c->pq_code), (gpointer)c);
00257 }
00258 {
00259 GpeCurrency *c = g_new0(GpeCurrency, 1);
00260 c->pq_code = 15;
00261 c->fraction = 100;
00262 c->symbol = "MXP";
00263 c->mnemonic = "MXP";
00264 c->non_utf8 = FALSE;
00265 g_hash_table_insert(gpe_currency_table,
00266 GINT_TO_POINTER(c->pq_code), (gpointer)c);
00267 }
00268 {
00269 GpeCurrency *c = g_new0(GpeCurrency, 1);
00270 c->pq_code = 16;
00271 c->fraction = 100;
00272 c->symbol = "€";
00273 c->mnemonic = "ANG";
00274 c->non_utf8 = TRUE;
00275 g_hash_table_insert(gpe_currency_table,
00276 GINT_TO_POINTER(c->pq_code), (gpointer)c);
00277 }
00278 {
00279 GpeCurrency *c = g_new0(GpeCurrency, 1);
00280 c->pq_code = 17;
00281 c->fraction = 100;
00282 c->symbol = "$NZ";
00283 c->mnemonic = "NZD";
00284 c->non_utf8 = FALSE;
00285 g_hash_table_insert(gpe_currency_table,
00286 GINT_TO_POINTER(c->pq_code), (gpointer)c);
00287 }
00288 {
00289 GpeCurrency *c = g_new0(GpeCurrency, 1);
00290 c->pq_code = 18;
00291 c->fraction = 100;
00292 c->symbol = "NOK";
00293 c->mnemonic = "NOK";
00294 c->non_utf8 = FALSE;
00295 g_hash_table_insert(gpe_currency_table,
00296 GINT_TO_POINTER(c->pq_code), (gpointer)c);
00297 }
00298 {
00299 GpeCurrency *c = g_new0(GpeCurrency, 1);
00300 c->pq_code = 19;
00301 c->fraction = 100;
00302 c->symbol = "€";
00303 c->mnemonic = "ESP";
00304 c->non_utf8 = TRUE;
00305 g_hash_table_insert(gpe_currency_table,
00306 GINT_TO_POINTER(c->pq_code), (gpointer)c);
00307 }
00308 {
00309 GpeCurrency *c = g_new0(GpeCurrency, 1);
00310 c->pq_code = 20;
00311 c->fraction = 100;
00312 c->symbol = "SEK";
00313 c->mnemonic = "SEK";
00314 c->non_utf8 = FALSE;
00315 g_hash_table_insert(gpe_currency_table,
00316 GINT_TO_POINTER(c->pq_code), (gpointer)c);
00317 }
00318 {
00319 GpeCurrency *c = g_new0(GpeCurrency, 1);
00320 c->pq_code = 21;
00321 c->fraction = 100;
00322 c->symbol = "CHF";
00323 c->mnemonic = "CHF";
00324 c->non_utf8 = FALSE;
00325 g_hash_table_insert(gpe_currency_table,
00326 GINT_TO_POINTER(c->pq_code), (gpointer)c);
00327 }
00328 {
00329 GpeCurrency *c = g_new0(GpeCurrency, 1);
00330 c->pq_code = 22;
00331 c->fraction = 100;
00332 c->symbol = "£";
00333 c->mnemonic = "GBP";
00334 c->non_utf8 = TRUE;
00335 g_hash_table_insert(gpe_currency_table,
00336 GINT_TO_POINTER(c->pq_code), (gpointer)c);
00337 }
00338 {
00339 GpeCurrency *c = g_new0(GpeCurrency, 1);
00340 c->pq_code = 23;
00341 c->fraction = 100;
00342 c->symbol = "$US";
00343 c->mnemonic = "USD";
00344 c->non_utf8 = FALSE;
00345 g_hash_table_insert(gpe_currency_table,
00346 GINT_TO_POINTER(c->pq_code), (gpointer)c);
00347 }
00348 {
00349 GpeCurrency *c = g_new0(GpeCurrency, 1);
00350 c->pq_code = 24;
00351 c->fraction = 100;
00352 c->symbol = "Rs";
00353 c->mnemonic = "INR";
00354 c->non_utf8 = FALSE;
00355 g_hash_table_insert(gpe_currency_table,
00356 GINT_TO_POINTER(c->pq_code), (gpointer)c);
00357 }
00358 {
00359 GpeCurrency *c = g_new0(GpeCurrency, 1);
00360 c->pq_code = 25;
00361 c->fraction = 1;
00362 c->symbol = "Rp";
00363 c->mnemonic = "IDR";
00364 c->non_utf8 = FALSE;
00365 g_hash_table_insert(gpe_currency_table,
00366 GINT_TO_POINTER(c->pq_code), (gpointer)c);
00367 }
00368 {
00369 GpeCurrency *c = g_new0(GpeCurrency, 1);
00370 c->pq_code = 26;
00371 c->fraction = 100;
00372 c->symbol = "KRW";
00373 c->mnemonic = "KRW";
00374 c->non_utf8 = FALSE;
00375 g_hash_table_insert(gpe_currency_table,
00376 GINT_TO_POINTER(c->pq_code), (gpointer)c);
00377 }
00378 {
00379 GpeCurrency *c = g_new0(GpeCurrency, 1);
00380 c->pq_code = 27;
00381 c->fraction = 100;
00382 c->symbol = "RM";
00383 c->mnemonic = "MYR";
00384 c->non_utf8 = FALSE;
00385 g_hash_table_insert(gpe_currency_table,
00386 GINT_TO_POINTER(c->pq_code), (gpointer)c);
00387 }
00388 {
00389 GpeCurrency *c = g_new0(GpeCurrency, 1);
00390 c->pq_code = 28;
00391 c->fraction = 100;
00392 c->symbol = "RMB";
00393 c->mnemonic = "CNY";
00394 c->non_utf8 = FALSE;
00395 g_hash_table_insert(gpe_currency_table,
00396 GINT_TO_POINTER(c->pq_code), (gpointer)c);
00397 }
00398 {
00399 GpeCurrency *c = g_new0(GpeCurrency, 1);
00400 c->pq_code = 29;
00401 c->fraction = 100;
00402 c->symbol = "P";
00403 c->mnemonic = "PHP";
00404 c->non_utf8 = FALSE;
00405 g_hash_table_insert(gpe_currency_table,
00406 GINT_TO_POINTER(c->pq_code), (gpointer)c);
00407 }
00408 {
00409 GpeCurrency *c = g_new0(GpeCurrency, 1);
00410 c->pq_code = 30;
00411 c->fraction = 100;
00412 c->symbol = "$";
00413 c->mnemonic = "SGD";
00414 c->non_utf8 = FALSE;
00415 g_hash_table_insert(gpe_currency_table,
00416 GINT_TO_POINTER(c->pq_code), (gpointer)c);
00417 }
00418 {
00419 GpeCurrency *c = g_new0(GpeCurrency, 1);
00420 c->pq_code = 31;
00421 c->fraction = 100;
00422 c->symbol = "BHT";
00423 c->mnemonic = "THB";
00424 c->non_utf8 = FALSE;
00425 g_hash_table_insert(gpe_currency_table,
00426 GINT_TO_POINTER(c->pq_code), (gpointer)c);
00427 }
00428 {
00429 GpeCurrency *c = g_new0(GpeCurrency, 1);
00430 c->pq_code = 32;
00431 c->fraction = 100;
00432 c->symbol = "NT$";
00433 c->mnemonic = "TWD";
00434 c->non_utf8 = FALSE;
00435 g_hash_table_insert(gpe_currency_table,
00436 GINT_TO_POINTER(c->pq_code), (gpointer)c);
00437 }
00438 {
00439 GpeCurrency *c = g_new0(GpeCurrency, 1);
00440 c->pq_code = 133;
00441 c->fraction = 100;
00442 c->symbol = "€";
00443 c->mnemonic = "EUR";
00444 c->non_utf8 = TRUE;
00445 g_hash_table_insert(gpe_currency_table,
00446 GINT_TO_POINTER(c->pq_code), (gpointer)c);
00447 }
00448 }
00449
00450 static gboolean
00451 check_name (gpointer G_GNUC_UNUSED key, gpointer value, gpointer data)
00452 {
00453 gchar * mnemonic = (gchar*) data;
00454 GpeCurrency * currency = (GpeCurrency*) value;
00455 if (0 == safe_strcmp (mnemonic, currency->mnemonic))
00456 return TRUE;
00457 return FALSE;
00458 }
00459
00460 GpeCurrency*
00461 gpe_currency_lookup_name (QofInstance * inst, gchar * mnemonic)
00462 {
00463 GpeCurrency *currency;
00464
00465 currency = NULL;
00466 if(!gpe_currency_table) populate_currencies();
00467 currency = (GpeCurrency*) g_hash_table_find
00468 (gpe_currency_table, check_name, mnemonic);
00469 if (!currency) return NULL;
00470 gpe_currency_lookup(inst, currency->pq_code);
00471 return currency;
00472 }
00473
00474 GpeCurrency*
00475 gpe_currency_lookup (QofInstance* inst, gint currency_code)
00476 {
00477 GpeCurrency *currency;
00478
00479 currency = NULL;
00480 if(!gpe_currency_table) populate_currencies();
00481 currency = (GpeCurrency*)g_hash_table_lookup(
00482 gpe_currency_table, GINT_TO_POINTER(currency_code));
00483 if(!currency)
00484 {
00485 PERR (" unsupported currency! %d", currency_code);
00486 return NULL;
00487 }
00488 kvp_frame_set_string(qof_instance_get_slots(inst),
00489 PQ_CURRENCY_MNEMONIC, currency->mnemonic);
00490 kvp_frame_set_gint64(qof_instance_get_slots(inst),
00491 PQ_CURRENCY_FRACTION, currency->fraction);
00492 if(currency->non_utf8)
00493 {
00494 gchar * k_symbol = g_strdup (currency->symbol);
00495 kvp_frame_set_string(qof_instance_get_slots(inst),
00496 PQ_CURRENCY_SYMBOL, qof_main_make_utf8(k_symbol));
00497 }
00498 else
00499 {
00500 kvp_frame_set_string(qof_instance_get_slots(inst),
00501 PQ_CURRENCY_SYMBOL, currency->symbol);
00502 }
00503 return currency;
00504 }
00505
00506 typedef struct ExpenseCustomCurrency {
00507 gchar name[16];
00508 gchar symbol[4];
00509 gchar rate[8];
00510 } ExpenseCustomCurrency_t;
00511
00512 struct Expense {
00513 struct tm date;
00514 ExpenseType type;
00515 ExpensePayment payment;
00516 gint currency;
00517 gchar *amount;
00518 gchar *vendor;
00519 gchar *city;
00520 gchar *attendees;
00521 gchar *note;
00522 };
00523
00524 struct ExpenseAppInfo {
00525
00526 struct ExpenseCustomCurrency currencies[4];
00527 };
00528
00529 struct QofExp_s
00530 {
00531 QofInstance inst;
00532 Expense_t wrap;
00533 ExpenseDistance distance_unit;
00534 gchar *category;
00535 const gchar* print_string;
00536 GpeCurrency *currency;
00537 gdouble temp_amount;
00540 gboolean reset_amount;
00541 };
00542
00543 static QofExp *
00544 expense_create (QofBook *book)
00545 {
00546 Expense_t *qe;
00547 QofExp *obj;
00548 QofCollection *coll;
00549 GList *all;
00550
00551 obj = g_new0(QofExp, 1);
00552 qof_instance_init (&obj->inst, GPE_QOF_EXPENSES, book);
00553 coll = qof_book_get_collection (book, GPE_QOF_EXPENSES);
00554 all = qof_collection_get_data (coll);
00555 all = g_list_prepend (all, obj);
00556 qof_collection_set_data (coll, all);
00557 qe = &obj->wrap;
00558 {
00559 glong nanosecs;
00560 QofTime *qt;
00561 QofDate *qd;
00562
00563 qt = qof_time_get_current ();
00564 nanosecs = qof_time_get_nanosecs (qt);
00565 qd = qof_date_from_qtime (qt);
00566 if (!qof_date_to_struct_tm (qd, &qe->date,
00567 &nanosecs))
00568 DEBUG (" failed to set initial date");
00569 qof_date_free (qd);
00570 qof_time_free (qt);
00571 }
00572 qe->amount = "0";
00573
00574 qe->currency = -1;
00575 if(!gpe_currency_table) populate_currencies();
00576 qof_event_gen ((QofEntity*)obj, QOF_EVENT_CREATE, NULL);
00577 return obj;
00578 }
00579
00580 static const gchar*
00581 qof_exp_paymentAsString(ExpensePayment payment)
00582 {
00583 const gchar *string;
00584
00585 string = "Cash";
00586 string = ExpensePaymentasString(payment);
00587 return string;
00588 }
00589
00590 static const gchar*
00591 qof_exp_typeAsString(ExpenseType type)
00592 {
00593 const gchar* string;
00594
00595 string = "Bus";
00596 string = ExpenseTypeasString(type);
00597 return string;
00598 }
00599
00600 static ExpensePayment
00601 qof_exp_paymentFromString(const gchar* payment_string)
00602 {
00603 return ExpensePaymentfromString(payment_string);
00604 }
00605
00606 static ExpenseType
00607 qof_exp_typeFromString(const gchar* type_string)
00608 {
00609 return ExpenseTypefromString(type_string);
00610 }
00611
00612 static QofTime *
00613 exp_getTime (QofExp * e)
00614 {
00615 Expense_t *qe;
00616 QofDate *qd;
00617 QofTime *qt;
00618
00619 g_return_val_if_fail (e != NULL, NULL);
00620 qe = &e->wrap;
00621 qd = qof_date_from_struct_tm (&qe->date);
00622 qt = qof_date_to_qtime (qd);
00623 qof_date_free (qd);
00624 return qt;
00625 }
00626
00627 static gchar*
00628 exp_getType (QofExp * e)
00629 {
00630 Expense_t *qe;
00631 gchar* string;
00632
00633 g_return_val_if_fail (e != NULL, NULL);
00634 qe = &e->wrap;
00635 string = g_strdup(qof_exp_typeAsString(qe->type));
00636 return string;
00637 }
00638
00639 static gchar*
00640 exp_getPayment (QofExp * e)
00641 {
00642 Expense_t *qe;
00643 gchar* string;
00644
00645 g_return_val_if_fail (e != NULL, NULL);
00646 qe = &e->wrap;
00647 string = g_strdup(qof_exp_paymentAsString(qe->payment));
00648 return string;
00649 }
00650
00651 static gint
00652 exp_getCurrency (QofExp * e)
00653 {
00654 Expense_t *qe;
00655
00656 g_return_val_if_fail (e != NULL, -1);
00657 qe = &e->wrap;
00658 return qe->currency;
00659 }
00660
00661 static QofNumeric
00662 exp_getAmount (QofExp * e)
00663 {
00664 Expense_t *qe;
00665 QofNumeric amount;
00666 gdouble pi_amount;
00667 gchar *numeric_char;
00668
00669 amount = qof_numeric_zero ();
00670 g_return_val_if_fail (e != NULL, amount);
00671 qe = &e->wrap;
00672 if(qe->amount == 0) { return amount; }
00673
00674 pi_amount = strtod (qe->amount, NULL);
00675 if(e->currency)
00676 {
00677 amount = qof_numeric_from_double (pi_amount, e->currency->fraction,
00678 QOF_HOW_DENOM_EXACT | QOF_HOW_RND_ROUND);
00679 }
00680 else
00681 {
00682 amount = qof_numeric_from_double (pi_amount, 100,
00683 QOF_HOW_DENOM_EXACT | QOF_HOW_RND_ROUND);
00684 }
00685 numeric_char = qof_numeric_to_string(amount);
00686 g_free(numeric_char);
00687
00688 if (qof_numeric_check (amount) == QOF_ERROR_OK)
00689 {
00690 return amount;
00691 }
00692 return qof_numeric_zero ();
00693 }
00694
00695 static const gchar *
00696 exp_getVendor (QofExp * e)
00697 {
00698 Expense_t *qe;
00699
00700 g_return_val_if_fail (e != NULL, NULL);
00701 qe = &e->wrap;
00702 return qe->vendor;
00703 }
00704
00705 static const gchar *
00706 exp_getCity (QofExp * e)
00707 {
00708 Expense_t *qe;
00709
00710 g_return_val_if_fail (e != NULL, NULL);
00711 qe = &e->wrap;
00712 return qe->city;
00713 }
00714
00715 static const gchar *
00716 exp_getAttendees (QofExp * e)
00717 {
00718 Expense_t *qe;
00719
00720 g_return_val_if_fail (e != NULL, NULL);
00721 qe = &e->wrap;
00722 return qe->attendees;
00723 }
00724
00725 static const gchar *
00726 exp_getNote (QofExp * e)
00727 {
00728 Expense_t *qe;
00729
00730 g_return_val_if_fail (e != NULL, NULL);
00731 qe = &e->wrap;
00732 return qe->note;
00733 }
00734
00735 static const gchar*
00736 exp_getDistance(QofExp *e)
00737 {
00738 g_return_val_if_fail(e != NULL, NULL);
00739 return ExpenseDistanceasString(e->distance_unit);
00740 }
00741
00742 static const gchar*
00743 exp_getCategory(QofExp *e)
00744 {
00745 g_return_val_if_fail(e != NULL, NULL);
00746 return e->category;
00747 }
00748
00749 static void
00750 exp_setTime (QofExp * e, QofTime *h)
00751 {
00752 Expense_t *qe;
00753 glong nanosecs;
00754 QofDate *qd;
00755
00756 g_return_if_fail (e != NULL);
00757 qe = &e->wrap;
00758 if (!h)
00759 return;
00760 nanosecs = qof_time_get_nanosecs (h);
00761 qd = qof_date_from_qtime (h);
00762 qof_date_to_struct_tm (qd, &qe->date,
00763 &nanosecs);
00764 qof_date_free (qd);
00765 }
00766
00767 static void
00768 exp_setType (QofExp * e, const gchar *type_string)
00769 {
00770 Expense_t *qe;
00771
00772 g_return_if_fail (e != NULL);
00773 qe = &e->wrap;
00774 qe->type = qof_exp_typeFromString(type_string);
00775 }
00776
00777 static void
00778 exp_setPayment (QofExp * e, const gchar *payment_string)
00779 {
00780 Expense_t *qe;
00781
00782 g_return_if_fail (e != NULL);
00783 qe = &e->wrap;
00784 qe->payment = qof_exp_paymentFromString(payment_string);
00785 }
00786
00787 static void
00788 exp_combine_currency_with_amount(QofExp *e)
00789 {
00790 Expense_t *qe;
00791
00792 g_return_if_fail (e != NULL);
00793 qe = &e->wrap;
00794 if(!e->currency || qe->currency < 0)
00795 {
00796
00797 e->currency = gpe_currency_lookup((QofInstance*)e, qe->currency);
00798 }
00799 if(!e->currency)
00800 {
00801 PERR (" Unable to identify currency fraction."
00802 " Using two decimal places.");
00803
00804
00805 qe->amount = g_strdup_printf ("%.2f", e->temp_amount);
00806 return;
00807 }
00808 switch (e->currency->fraction)
00809 {
00810 case 1 :
00811 {
00812 qe->amount = g_strdup_printf ("%.0f", e->temp_amount);
00813 break;
00814 }
00815 case 10 :
00816 {
00817 qe->amount = g_strdup_printf ("%.1f", e->temp_amount);
00818 break;
00819 }
00820 case 100 :
00821 {
00822 qe->amount = g_strdup_printf ("%.2f", e->temp_amount);
00823 break;
00824 }
00825 case 1000 :
00826 {
00827 qe->amount = g_strdup_printf ("%.3f", e->temp_amount);
00828 break;
00829 }
00830 default :
00831 {
00832 PERR (" Invalid currency fraction."
00833 " Using two decimal places as default.");
00834 qe->amount = g_strdup_printf ("%.2f", e->temp_amount);
00835 }
00836 }
00837 }
00838
00839 static void
00840 exp_setCurrency (QofExp * e, gint data)
00841 {
00842 Expense_t *qe;
00843
00844 g_return_if_fail (e != NULL);
00845 qe = &e->wrap;
00846 qe->currency = data;
00847 e->currency = gpe_currency_lookup((QofInstance*)e, data);
00848 if(e->reset_amount)
00849 {
00850 exp_combine_currency_with_amount(e);
00851 }
00852 e->reset_amount = FALSE;
00853 }
00854
00855 static void
00856 exp_setAmount (QofExp * e, QofNumeric h)
00857 {
00858 Expense_t *qe;
00859
00860 g_return_if_fail (e != NULL);
00861 qe = &e->wrap;
00862 e->temp_amount = qof_numeric_to_double (h);
00863 e->reset_amount = TRUE;
00864
00865
00868 if(e->currency) exp_combine_currency_with_amount(e);
00869 }
00870
00871 static void
00872 exp_setVendor (QofExp * e, gchar *h)
00873 {
00874 Expense_t *qe;
00875
00876 g_return_if_fail (e != NULL);
00877 qe = &e->wrap;
00878 qe->vendor = g_strdup (qof_main_make_utf8(h));
00879 }
00880
00881 static void
00882 exp_setCity (QofExp * e, gchar *h)
00883 {
00884 Expense_t *qe;
00885
00886 g_return_if_fail (e != NULL);
00887 qe = &e->wrap;
00888 qe->city = g_strdup (qof_main_make_utf8(h));
00889 }
00890
00891 static void
00892 exp_setAttendees (QofExp * e, gchar *h)
00893 {
00894 Expense_t *qe;
00895
00896 g_return_if_fail (e != NULL);
00897 qe = &e->wrap;
00898 qe->attendees = g_strdup (qof_main_make_utf8(h));
00899 }
00900
00901 static void
00902 exp_setNote (QofExp * e, gchar *h)
00903 {
00904 Expense_t *qe;
00905
00906 g_return_if_fail (e != NULL);
00907 qe = &e->wrap;
00908 qe->note = g_strdup (qof_main_make_utf8(h));
00909 }
00910
00911 static void
00912 exp_setDistance(QofExp *e, const gchar *distance_name)
00913 {
00914
00915 g_return_if_fail(e);
00916 e->distance_unit = ExpenseDistancefromString(distance_name);
00917 }
00918
00919 static void
00920 exp_setCategory(QofExp *e, gchar *n)
00921 {
00922 g_return_if_fail(e != NULL);
00923 e->category = g_strdup(qof_main_make_utf8(n));
00924 }
00925
00926 static const gchar*
00927 expensePrintable (gpointer instance)
00928 {
00929 QofExp *obj;
00930
00931 obj = (QofExp*)instance;
00932 if(!obj) return NULL;
00933 if(exp_getType(obj))
00934 {
00935 return g_strconcat(exp_getType(obj), " ",
00936 exp_getVendor(obj), " ", exp_getCity(obj), NULL);
00937 }
00938 return NULL;
00939 }
00940
00941 static QofObject expenses_object_def = {
00942 .interface_version = QOF_OBJECT_VERSION,
00943 .e_type = GPE_QOF_EXPENSES,
00944 .type_label = QOF_EXPENSES_DESC,
00945 .create = ((gpointer)expense_create),
00946 .book_begin = NULL,
00947 .book_end = NULL,
00948 .is_dirty = qof_collection_is_dirty,
00949 .mark_clean = qof_collection_mark_clean,
00950 .foreach = qof_collection_foreach,
00951 .printable = expensePrintable,
00952 .version_cmp = (gint (*)(gpointer, gpointer)) qof_instance_version_cmp,
00953 };
00954
00955 gboolean
00956 ExpensesRegister (void)
00957 {
00958 static QofParam params[] = {
00959 { EXP_DATE, QOF_TYPE_TIME, (QofAccessFunc) exp_getTime, (QofSetterFunc) exp_setTime, NULL },
00960 { EXP_TYPE, QOF_TYPE_STRING, (QofAccessFunc) exp_getType, (QofSetterFunc) exp_setType, NULL },
00961 { EXP_PAYMENT, QOF_TYPE_STRING, (QofAccessFunc) exp_getPayment, (QofSetterFunc) exp_setPayment, NULL },
00962 { EXP_CURRENCY, QOF_TYPE_INT32, (QofAccessFunc) exp_getCurrency, (QofSetterFunc) exp_setCurrency, NULL },
00963 { EXP_AMOUNT, QOF_TYPE_NUMERIC, (QofAccessFunc) exp_getAmount, (QofSetterFunc) exp_setAmount, NULL },
00964 { EXP_VENDOR, QOF_TYPE_STRING, (QofAccessFunc) exp_getVendor, (QofSetterFunc) exp_setVendor, NULL },
00965 { EXP_CITY, QOF_TYPE_STRING, (QofAccessFunc) exp_getCity, (QofSetterFunc) exp_setCity, NULL },
00966 { EXP_ATTENDEES, QOF_TYPE_STRING, (QofAccessFunc) exp_getAttendees, (QofSetterFunc) exp_setAttendees, NULL },
00967 { EXP_NOTE, QOF_TYPE_STRING, (QofAccessFunc) exp_getNote, (QofSetterFunc) exp_setNote, NULL },
00968 { EXP_DISTANCE, QOF_TYPE_STRING, (QofAccessFunc) exp_getDistance, (QofSetterFunc) exp_setDistance, NULL },
00969 { EXP_CATEGORY, QOF_TYPE_STRING, (QofAccessFunc) exp_getCategory, (QofSetterFunc) exp_setCategory, NULL },
00970 { EXP_KVP, QOF_TYPE_KVP, (QofAccessFunc) qof_instance_get_slots, NULL, NULL },
00971 { QOF_PARAM_BOOK, QOF_ID_BOOK, (QofAccessFunc) qof_instance_get_book, NULL, NULL },
00972 { QOF_PARAM_GUID, QOF_TYPE_GUID, (QofAccessFunc) qof_instance_get_guid, NULL, NULL },
00973 { NULL, NULL, NULL, NULL, NULL },
00974 };
00975
00976 bindtextdomain (LIBRARY_GETTEXT_PACKAGE, LOCALE_DIR);
00977
00978 qof_class_register (GPE_QOF_EXPENSES, NULL, params);
00979 if(!gpe_currency_table) populate_currencies();
00980
00981 return qof_object_register (&expenses_object_def);
00982 }