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