00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00031 #include <stdlib.h>
00032 #include <glib.h>
00033 #include <glib/gprintf.h>
00034 #include <qof.h>
00035 #include "pi-expense.h"
00036 #include "qof-main.h"
00037 #include "pilot-qof.h"
00038 #include "qof-expenses.h"
00039
00040 #define QOF_EXPENSES_DESC "Pilot-link QOF expenses"
00041
00044 #define EXPENSE_CREATOR "exps"
00045
00046 static QofLogModule log_module = PQ_MOD_PILOT;
00047
00052 static GHashTable *PQCurrencyTable = NULL;
00053
00055 typedef struct PQCurrency_s
00056 {
00057 gint pq_code;
00058 gint fraction;
00059 const gchar *symbol;
00060 const gchar *mnemonic;
00061 gboolean non_utf8;
00064 } PQCurrency;
00065
00082 static void
00083 populate_currencies (void)
00084 {
00085 PQCurrencyTable = g_hash_table_new (g_direct_hash, g_direct_equal);
00086 {
00087 PQCurrency *c = g_new0 (PQCurrency, 1);
00088 c->pq_code = 0;
00089 c->fraction = 100;
00090 c->symbol = "AU$";
00091 c->mnemonic = "AUD";
00092 c->non_utf8 = FALSE;
00093 g_hash_table_insert (PQCurrencyTable,
00094 GINT_TO_POINTER (c->pq_code), (gpointer) c);
00095 }
00096 {
00097 PQCurrency *c = g_new0 (PQCurrency, 1);
00098 c->pq_code = 1;
00099 c->fraction = 100;
00100 c->symbol = "€";
00101 c->mnemonic = "ATS";
00102 c->non_utf8 = TRUE;
00103 g_hash_table_insert (PQCurrencyTable,
00104 GINT_TO_POINTER (c->pq_code), (gpointer) c);
00105 }
00106 {
00107 PQCurrency *c = g_new0 (PQCurrency, 1);
00108 c->pq_code = 2;
00109 c->fraction = 100;
00110 c->symbol = "€";
00111 c->mnemonic = "BEF";
00112 c->non_utf8 = TRUE;
00113 g_hash_table_insert (PQCurrencyTable,
00114 GINT_TO_POINTER (c->pq_code), (gpointer) c);
00115 }
00116 {
00117 PQCurrency *c = g_new0 (PQCurrency, 1);
00118 c->pq_code = 3;
00119 c->fraction = 100;
00120 c->symbol = "R$";
00121 c->mnemonic = "BRL";
00122 c->non_utf8 = FALSE;
00123 g_hash_table_insert (PQCurrencyTable,
00124 GINT_TO_POINTER (c->pq_code), (gpointer) c);
00125 }
00126 {
00127 PQCurrency *c = g_new0 (PQCurrency, 1);
00128 c->pq_code = 4;
00129 c->fraction = 100;
00130 c->symbol = "$CN";
00131 c->mnemonic = "CAD";
00132 c->non_utf8 = FALSE;
00133 g_hash_table_insert (PQCurrencyTable,
00134 GINT_TO_POINTER (c->pq_code), (gpointer) c);
00135 }
00136 {
00137 PQCurrency *c = g_new0 (PQCurrency, 1);
00138 c->pq_code = 5;
00139 c->fraction = 100;
00140 c->symbol = "DKK";
00141 c->mnemonic = "DKK";
00142 c->non_utf8 = FALSE;
00143 g_hash_table_insert (PQCurrencyTable,
00144 GINT_TO_POINTER (c->pq_code), (gpointer) c);
00145 }
00146 {
00147 PQCurrency *c = g_new0 (PQCurrency, 1);
00148 c->pq_code = 6;
00149 c->fraction = 100;
00150 c->symbol = "€";
00151 c->mnemonic = "FIM";
00152 c->non_utf8 = TRUE;
00153 g_hash_table_insert (PQCurrencyTable,
00154 GINT_TO_POINTER (c->pq_code), (gpointer) c);
00155 }
00156 {
00157 PQCurrency *c = g_new0 (PQCurrency, 1);
00158 c->pq_code = 7;
00159 c->fraction = 100;
00160 c->symbol = "€";
00161 c->mnemonic = "FRF";
00162 c->non_utf8 = TRUE;
00163 g_hash_table_insert (PQCurrencyTable,
00164 GINT_TO_POINTER (c->pq_code), (gpointer) c);
00165 }
00166 {
00167 PQCurrency *c = g_new0 (PQCurrency, 1);
00168 c->pq_code = 8;
00169 c->fraction = 100;
00170 c->symbol = "€";
00171 c->mnemonic = "DEM";
00172 c->non_utf8 = TRUE;
00173 g_hash_table_insert (PQCurrencyTable,
00174 GINT_TO_POINTER (c->pq_code), (gpointer) c);
00175 }
00176 {
00177 PQCurrency *c = g_new0 (PQCurrency, 1);
00178 c->pq_code = 9;
00179 c->fraction = 100;
00180 c->symbol = "HK$";
00181 c->mnemonic = "HKD";
00182 c->non_utf8 = FALSE;
00183 g_hash_table_insert (PQCurrencyTable,
00184 GINT_TO_POINTER (c->pq_code), (gpointer) c);
00185 }
00186 {
00187 PQCurrency *c = g_new0 (PQCurrency, 1);
00188 c->pq_code = 10;
00189 c->fraction = 100;
00190 c->symbol = "ISK";
00191 c->mnemonic = "ISK";
00192 c->non_utf8 = FALSE;
00193 g_hash_table_insert (PQCurrencyTable,
00194 GINT_TO_POINTER (c->pq_code), (gpointer) c);
00195 }
00196 {
00197 PQCurrency *c = g_new0 (PQCurrency, 1);
00198 c->pq_code = 11;
00199 c->fraction = 100;
00200 c->symbol = "€";
00201 c->mnemonic = "IEP";
00202 c->non_utf8 = TRUE;
00203 g_hash_table_insert (PQCurrencyTable,
00204 GINT_TO_POINTER (c->pq_code), (gpointer) c);
00205 }
00206 {
00207
00208 PQCurrency *c = g_new0 (PQCurrency, 1);
00209 c->pq_code = 12;
00210 c->fraction = 100;
00211 c->symbol = "EUR";
00212 c->mnemonic = "ITL";
00213 c->non_utf8 = FALSE;
00214 g_hash_table_insert (PQCurrencyTable,
00215 GINT_TO_POINTER (c->pq_code), (gpointer) c);
00216 }
00217 {
00218 PQCurrency *c = g_new0 (PQCurrency, 1);
00219 c->pq_code = 13;
00220 c->fraction = 1;
00221 c->symbol = "¥";
00222 c->mnemonic = "JPY";
00223 c->non_utf8 = TRUE;
00224 g_hash_table_insert (PQCurrencyTable,
00225 GINT_TO_POINTER (c->pq_code), (gpointer) c);
00226 }
00227 {
00228 PQCurrency *c = g_new0 (PQCurrency, 1);
00229 c->pq_code = 14;
00230 c->fraction = 100;
00231 c->symbol = "€";
00232 c->mnemonic = "LUF";
00233 c->non_utf8 = TRUE;
00234 g_hash_table_insert (PQCurrencyTable,
00235 GINT_TO_POINTER (c->pq_code), (gpointer) c);
00236 }
00237 {
00238 PQCurrency *c = g_new0 (PQCurrency, 1);
00239 c->pq_code = 15;
00240 c->fraction = 100;
00241 c->symbol = "MXP";
00242 c->mnemonic = "MXP";
00243 c->non_utf8 = FALSE;
00244 g_hash_table_insert (PQCurrencyTable,
00245 GINT_TO_POINTER (c->pq_code), (gpointer) c);
00246 }
00247 {
00248 PQCurrency *c = g_new0 (PQCurrency, 1);
00249 c->pq_code = 16;
00250 c->fraction = 100;
00251 c->symbol = "€";
00252 c->mnemonic = "ANG";
00253 c->non_utf8 = TRUE;
00254 g_hash_table_insert (PQCurrencyTable,
00255 GINT_TO_POINTER (c->pq_code), (gpointer) c);
00256 }
00257 {
00258 PQCurrency *c = g_new0 (PQCurrency, 1);
00259 c->pq_code = 17;
00260 c->fraction = 100;
00261 c->symbol = "$NZ";
00262 c->mnemonic = "NZD";
00263 c->non_utf8 = FALSE;
00264 g_hash_table_insert (PQCurrencyTable,
00265 GINT_TO_POINTER (c->pq_code), (gpointer) c);
00266 }
00267 {
00268 PQCurrency *c = g_new0 (PQCurrency, 1);
00269 c->pq_code = 18;
00270 c->fraction = 100;
00271 c->symbol = "NOK";
00272 c->mnemonic = "NOK";
00273 c->non_utf8 = FALSE;
00274 g_hash_table_insert (PQCurrencyTable,
00275 GINT_TO_POINTER (c->pq_code), (gpointer) c);
00276 }
00277 {
00278 PQCurrency *c = g_new0 (PQCurrency, 1);
00279 c->pq_code = 19;
00280 c->fraction = 100;
00281 c->symbol = "€";
00282 c->mnemonic = "ESP";
00283 c->non_utf8 = TRUE;
00284 g_hash_table_insert (PQCurrencyTable,
00285 GINT_TO_POINTER (c->pq_code), (gpointer) c);
00286 }
00287 {
00288 PQCurrency *c = g_new0 (PQCurrency, 1);
00289 c->pq_code = 20;
00290 c->fraction = 100;
00291 c->symbol = "SEK";
00292 c->mnemonic = "SEK";
00293 c->non_utf8 = FALSE;
00294 g_hash_table_insert (PQCurrencyTable,
00295 GINT_TO_POINTER (c->pq_code), (gpointer) c);
00296 }
00297 {
00298 PQCurrency *c = g_new0 (PQCurrency, 1);
00299 c->pq_code = 21;
00300 c->fraction = 100;
00301 c->symbol = "CHF";
00302 c->mnemonic = "CHF";
00303 c->non_utf8 = FALSE;
00304 g_hash_table_insert (PQCurrencyTable,
00305 GINT_TO_POINTER (c->pq_code), (gpointer) c);
00306 }
00307 {
00308 PQCurrency *c = g_new0 (PQCurrency, 1);
00309 c->pq_code = 22;
00310 c->fraction = 100;
00311 c->symbol = "£";
00312 c->mnemonic = "GBP";
00313 c->non_utf8 = TRUE;
00314 g_hash_table_insert (PQCurrencyTable,
00315 GINT_TO_POINTER (c->pq_code), (gpointer) c);
00316 }
00317 {
00318 PQCurrency *c = g_new0 (PQCurrency, 1);
00319 c->pq_code = 23;
00320 c->fraction = 100;
00321 c->symbol = "$US";
00322 c->mnemonic = "USD";
00323 c->non_utf8 = FALSE;
00324 g_hash_table_insert (PQCurrencyTable,
00325 GINT_TO_POINTER (c->pq_code), (gpointer) c);
00326 }
00327 {
00328 PQCurrency *c = g_new0 (PQCurrency, 1);
00329 c->pq_code = 24;
00330 c->fraction = 100;
00331 c->symbol = "Rs";
00332 c->mnemonic = "INR";
00333 c->non_utf8 = FALSE;
00334 g_hash_table_insert (PQCurrencyTable,
00335 GINT_TO_POINTER (c->pq_code), (gpointer) c);
00336 }
00337 {
00338 PQCurrency *c = g_new0 (PQCurrency, 1);
00339 c->pq_code = 25;
00340 c->fraction = 1;
00341 c->symbol = "Rp";
00342 c->mnemonic = "IDR";
00343 c->non_utf8 = FALSE;
00344 g_hash_table_insert (PQCurrencyTable,
00345 GINT_TO_POINTER (c->pq_code), (gpointer) c);
00346 }
00347 {
00348 PQCurrency *c = g_new0 (PQCurrency, 1);
00349 c->pq_code = 26;
00350 c->fraction = 100;
00351 c->symbol = "KRW";
00352 c->mnemonic = "KRW";
00353 c->non_utf8 = FALSE;
00354 g_hash_table_insert (PQCurrencyTable,
00355 GINT_TO_POINTER (c->pq_code), (gpointer) c);
00356 }
00357 {
00358 PQCurrency *c = g_new0 (PQCurrency, 1);
00359 c->pq_code = 27;
00360 c->fraction = 100;
00361 c->symbol = "RM";
00362 c->mnemonic = "MYR";
00363 c->non_utf8 = FALSE;
00364 g_hash_table_insert (PQCurrencyTable,
00365 GINT_TO_POINTER (c->pq_code), (gpointer) c);
00366 }
00367 {
00368 PQCurrency *c = g_new0 (PQCurrency, 1);
00369 c->pq_code = 28;
00370 c->fraction = 100;
00371 c->symbol = "RMB";
00372 c->mnemonic = "CNY";
00373 c->non_utf8 = FALSE;
00374 g_hash_table_insert (PQCurrencyTable,
00375 GINT_TO_POINTER (c->pq_code), (gpointer) c);
00376 }
00377 {
00378 PQCurrency *c = g_new0 (PQCurrency, 1);
00379 c->pq_code = 29;
00380 c->fraction = 100;
00381 c->symbol = "P";
00382 c->mnemonic = "PHP";
00383 c->non_utf8 = FALSE;
00384 g_hash_table_insert (PQCurrencyTable,
00385 GINT_TO_POINTER (c->pq_code), (gpointer) c);
00386 }
00387 {
00388 PQCurrency *c = g_new0 (PQCurrency, 1);
00389 c->pq_code = 30;
00390 c->fraction = 100;
00391 c->symbol = "$";
00392 c->mnemonic = "SGD";
00393 c->non_utf8 = FALSE;
00394 g_hash_table_insert (PQCurrencyTable,
00395 GINT_TO_POINTER (c->pq_code), (gpointer) c);
00396 }
00397 {
00398 PQCurrency *c = g_new0 (PQCurrency, 1);
00399 c->pq_code = 31;
00400 c->fraction = 100;
00401 c->symbol = "BHT";
00402 c->mnemonic = "THB";
00403 c->non_utf8 = FALSE;
00404 g_hash_table_insert (PQCurrencyTable,
00405 GINT_TO_POINTER (c->pq_code), (gpointer) c);
00406 }
00407 {
00408 PQCurrency *c = g_new0 (PQCurrency, 1);
00409 c->pq_code = 32;
00410 c->fraction = 100;
00411 c->symbol = "NT$";
00412 c->mnemonic = "TWD";
00413 c->non_utf8 = FALSE;
00414 g_hash_table_insert (PQCurrencyTable,
00415 GINT_TO_POINTER (c->pq_code), (gpointer) c);
00416 }
00417 {
00418 PQCurrency *c = g_new0 (PQCurrency, 1);
00419 c->pq_code = 133;
00420 c->fraction = 100;
00421 c->symbol = "€";
00422 c->mnemonic = "EUR";
00423 c->non_utf8 = TRUE;
00424 g_hash_table_insert (PQCurrencyTable,
00425 GINT_TO_POINTER (c->pq_code), (gpointer) c);
00426 }
00427 }
00428
00429 static PQCurrency *
00430 pq_currency_lookup (QofInstance * inst, gint currency_code)
00431 {
00432 PQCurrency *currency;
00433
00434 currency = NULL;
00435 if (!PQCurrencyTable)
00436 populate_currencies ();
00437 currency =
00438 (PQCurrency *) g_hash_table_lookup (PQCurrencyTable,
00439 GINT_TO_POINTER (currency_code));
00440 if (!currency)
00441 {
00442 PERR (" unsupported currency! %d", currency_code);
00443 return NULL;
00444 }
00445 kvp_frame_set_string (qof_instance_get_slots (inst),
00446 PQ_CURRENCY_MNEMONIC, currency->mnemonic);
00447 kvp_frame_set_gint64 (qof_instance_get_slots (inst),
00448 PQ_CURRENCY_FRACTION, currency->fraction);
00449 if (currency->non_utf8)
00450 {
00451 kvp_frame_set_string (qof_instance_get_slots (inst),
00452 PQ_CURRENCY_SYMBOL,
00453 qof_main_make_utf8 ((gchar *) currency->symbol));
00454 }
00455 else
00456 {
00457 kvp_frame_set_string (qof_instance_get_slots (inst),
00458 PQ_CURRENCY_SYMBOL, currency->symbol);
00459 }
00460 return currency;
00461 }
00462
00469 typedef struct
00470 {
00471 QofInstance inst;
00472 Expense_t wrap;
00473 enum ExpenseDistance distance_unit;
00474 gchar *category;
00475 const gchar *print_string;
00476 PQCurrency *currency;
00477 gdouble temp_amount;
00480 gboolean reset_amount;
00481 } QofExpense;
00482
00483 static QofExpense *
00484 expense_create (QofBook * book)
00485 {
00486 Expense_t *qe;
00487 QofExpense *obj;
00488 QofCollection *coll;
00489 GList *all;
00490
00491 obj = g_new0 (QofExpense, 1);
00492 qof_instance_init (&obj->inst, PILOT_LINK_QOF_EXPENSES, book);
00493 coll = qof_book_get_collection (book, PILOT_LINK_QOF_EXPENSES);
00494 all = qof_collection_get_data (coll);
00495 all = g_list_prepend (all, obj);
00496 qof_collection_set_data (coll, all);
00497 qe = &obj->wrap;
00498 qe->amount = "0";
00499
00500 qe->currency = -1;
00501 return obj;
00502 }
00503
00504 static const gchar *
00505 qof_exp_distanceAsString (enum ExpenseDistance distance)
00506 {
00507 gchar *string;
00508 gint i;
00509
00510 string = "Miles";
00511 for (i = 0; ExpenseDistanceNames[i] != NULL; i++)
00512 {
00513 if (i == (gint) distance)
00514 string = ExpenseDistanceNames[(gint) distance];
00515 }
00516 return string;
00517 }
00518
00519 static const gchar *
00520 qof_exp_paymentAsString (enum ExpensePayment payment)
00521 {
00522 gchar *string;
00523 gint i;
00524
00525 string = "Cash";
00526 for (i = 0; ExpensePaymentNames[i] != NULL; i++)
00527 {
00528 if (i == (gint) payment)
00529 string = ExpensePaymentNames[(gint) payment];
00530 }
00531 return string;
00532 }
00533
00534 static const gchar *
00535 qof_exp_typeAsString (enum ExpenseType type)
00536 {
00537 gchar *string;
00538 gint i;
00539
00540 string = "Bus";
00541 for (i = 0; ExpenseTypeNames[i] != NULL; i++)
00542 {
00543 if (i == (gint) type)
00544 string = ExpenseTypeNames[(gint) type];
00545 }
00546 return string;
00547 }
00548
00549 static enum ExpensePayment
00550 qof_exp_paymentFromString (const gchar * payment_string)
00551 {
00552 guint i;
00553
00554 for (i = 0; ExpensePaymentNames[i] != NULL; i++)
00555 {
00556 if (0 == safe_strcmp (ExpensePaymentNames[i], payment_string))
00557 {
00558 return (enum ExpensePayment) i;
00559 }
00560 }
00561 return epCash;
00562 }
00563
00564 static enum ExpenseType
00565 qof_exp_typeFromString (const gchar * type_string)
00566 {
00567 guint i;
00568
00569 for (i = 0; ExpenseTypeNames[i] != NULL; i++)
00570 {
00571 if (0 == safe_strcmp (ExpenseTypeNames[i], type_string))
00572 return (enum ExpenseType) i;
00573 }
00574 return etBus;
00575 }
00576
00577 static QofTime*
00578 exp_getTime (QofExpense * e)
00579 {
00580 Expense_t *qe;
00581 QofTime *qt;
00582
00583 g_return_val_if_fail (e != NULL, NULL);
00584 qe = &e->wrap;
00585 qt = qof_time_from_tm (&qe->date, 0);
00586 return qt;
00587 }
00588
00589 static const gchar *
00590 exp_getType (QofExpense * e)
00591 {
00592 Expense_t *qe;
00593
00594 g_return_val_if_fail (e != NULL, NULL);
00595 qe = &e->wrap;
00596 return qof_exp_typeAsString (qe->type);
00597 }
00598
00599 static const gchar *
00600 exp_getPayment (QofExpense * e)
00601 {
00602 Expense_t *qe;
00603
00604 g_return_val_if_fail (e != NULL, NULL);
00605 qe = &e->wrap;
00606 return qof_exp_paymentAsString (qe->payment);
00607 }
00608
00609 static gint
00610 exp_getCurrency (QofExpense * e)
00611 {
00612 Expense_t *qe;
00613
00614 g_return_val_if_fail (e != NULL, -1);
00615 qe = &e->wrap;
00616 return qe->currency;
00617 }
00618
00619 static QofNumeric
00620 exp_getAmount (QofExpense * e)
00621 {
00622 Expense_t *qe;
00623 QofNumeric amount;
00624 gdouble pi_amount;
00625 gchar *numeric_char;
00626
00627 amount = qof_numeric_zero ();
00628 g_return_val_if_fail (e != NULL, amount);
00629 qe = &e->wrap;
00630 if (qe->amount == 0)
00631 return amount;
00632
00633 pi_amount = strtod (qe->amount, NULL);
00634 if (e->currency)
00635 amount = qof_numeric_from_double (pi_amount, e->currency->fraction,
00636 QOF_HOW_DENOM_EXACT | QOF_HOW_RND_ROUND);
00637 else
00638 amount = qof_numeric_from_double (pi_amount, 100,
00639 QOF_HOW_DENOM_EXACT | QOF_HOW_RND_ROUND);
00640 numeric_char = qof_numeric_to_string (amount);
00641 g_free (numeric_char);
00642 if (qof_numeric_check (amount) == QOF_ERROR_OK)
00643 return amount;
00644 return qof_numeric_zero ();
00645 }
00646
00647 static const gchar *
00648 exp_getVendor (QofExpense * e)
00649 {
00650 Expense_t *qe;
00651
00652 g_return_val_if_fail (e != NULL, NULL);
00653 qe = &e->wrap;
00654 return qe->vendor;
00655 }
00656
00657 static const gchar *
00658 exp_getCity (QofExpense * e)
00659 {
00660 Expense_t *qe;
00661
00662 g_return_val_if_fail (e != NULL, NULL);
00663 qe = &e->wrap;
00664 return qe->city;
00665 }
00666
00667 static const gchar *
00668 exp_getAttendees (QofExpense * e)
00669 {
00670 Expense_t *qe;
00671
00672 g_return_val_if_fail (e != NULL, NULL);
00673 qe = &e->wrap;
00674 return qe->attendees;
00675 }
00676
00677 static const gchar *
00678 exp_getNote (QofExpense * e)
00679 {
00680 Expense_t *qe;
00681
00682 g_return_val_if_fail (e != NULL, NULL);
00683 qe = &e->wrap;
00684 return qe->note;
00685 }
00686
00687 static const gchar *
00688 exp_getDistance (QofExpense * e)
00689 {
00690 guint i;
00691 Expense_t *qe;
00692 enum ExpenseDistance type;
00693
00694 qe = &e->wrap;
00695 type = edMiles;
00696 if (qe->type != etMileage)
00697 return NULL;
00698 for (i = 0; ExpenseDistanceNames[i] != NULL; i++)
00699 {
00700 if (i == e->distance_unit)
00701 type = (enum ExpenseDistance) i;
00702 }
00703 return qof_exp_distanceAsString (type);
00704 }
00705
00706 static const gchar *
00707 exp_getCategory (QofExpense * e)
00708 {
00709 g_return_val_if_fail (e != NULL, NULL);
00710 return e->category;
00711 }
00712
00713 static void
00714 exp_setTime (QofExpense * e, QofTime *h)
00715 {
00716 Expense_t *qe;
00717 gboolean result;
00718 QofDate *qd;
00719
00720 g_return_if_fail (e != NULL);
00721 qe = &e->wrap;
00722 qd = qof_date_from_qtime (h);
00723 result = qof_date_to_struct_tm (qd, &qe->date, 0);
00724 if(!result)
00725 PERR (" Date too large for begin.");
00726 qof_date_free (qd);
00727 }
00728
00729 static void
00730 exp_setType (QofExpense * e, const gchar * type_string)
00731 {
00732 Expense_t *qe;
00733
00734 g_return_if_fail (e != NULL);
00735 qe = &e->wrap;
00736 qe->type = qof_exp_typeFromString (type_string);
00737 }
00738
00739 static void
00740 exp_setPayment (QofExpense * e, const gchar * payment_string)
00741 {
00742 Expense_t *qe;
00743
00744 g_return_if_fail (e != NULL);
00745 qe = &e->wrap;
00746 qe->payment = qof_exp_paymentFromString (payment_string);
00747 }
00748
00749 static void
00750 exp_combine_currency_with_amount (QofExpense * e)
00751 {
00752 Expense_t *qe;
00753
00754 g_return_if_fail (e != NULL);
00755 qe = &e->wrap;
00756 if (!e->currency || qe->currency < 0)
00757 {
00758
00759 e->currency = pq_currency_lookup ((QofInstance *) e,
00760 qe->currency);
00761 }
00762 if (!e->currency)
00763 {
00764 PERR (" Unable to identify currency fraction."
00765 " Using two decimal places.");
00766
00767
00768 qe->amount = g_strdup_printf ("%.2f", e->temp_amount);
00769 return;
00770 }
00771 switch (e->currency->fraction)
00772 {
00773 case 1:
00774 {
00775 qe->amount = g_strdup_printf ("%.0f", e->temp_amount);
00776 break;
00777 }
00778 case 10:
00779 {
00780 qe->amount = g_strdup_printf ("%.1f", e->temp_amount);
00781 break;
00782 }
00783 case 100:
00784 {
00785 qe->amount = g_strdup_printf ("%.2f", e->temp_amount);
00786 break;
00787 }
00788 case 1000:
00789 {
00790 qe->amount = g_strdup_printf ("%.3f", e->temp_amount);
00791 break;
00792 }
00793 default:
00794 {
00795 PERR (" Invalid currency fraction."
00796 " Using two decimal places as default.");
00797 qe->amount = g_strdup_printf ("%.2f", e->temp_amount);
00798 }
00799 }
00800 }
00801
00802 static void
00803 exp_setCurrency (QofExpense * e, gint code)
00804 {
00805 Expense_t *qe;
00806
00807 g_return_if_fail (e != NULL);
00808 qe = &e->wrap;
00809 qe->currency = code;
00810 e->currency = pq_currency_lookup ((QofInstance *) e, code);
00811 if (e->reset_amount)
00812 exp_combine_currency_with_amount (e);
00813 e->reset_amount = FALSE;
00814 }
00815
00816 static void
00817 exp_setAmount (QofExpense * 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)
00830 exp_combine_currency_with_amount (e);
00831 }
00832
00833 static void
00834 exp_setVendor (QofExpense * e, gchar * h)
00835 {
00836 Expense_t *qe;
00837
00838 g_return_if_fail (e != NULL);
00839 qe = &e->wrap;
00840 qe->vendor = g_strdup (qof_main_make_utf8 (h));
00841 }
00842
00843 static void
00844 exp_setCity (QofExpense * e, gchar * h)
00845 {
00846 Expense_t *qe;
00847
00848 g_return_if_fail (e != NULL);
00849 qe = &e->wrap;
00850 qe->city = g_strdup (qof_main_make_utf8 (h));
00851 }
00852
00853 static void
00854 exp_setAttendees (QofExpense * e, gchar * h)
00855 {
00856 Expense_t *qe;
00857
00858 g_return_if_fail (e != NULL);
00859 qe = &e->wrap;
00860 qe->attendees = g_strdup (qof_main_make_utf8 (h));
00861 }
00862
00863 static void
00864 exp_setNote (QofExpense * e, gchar * h)
00865 {
00866 Expense_t *qe;
00867
00868 g_return_if_fail (e != NULL);
00869 qe = &e->wrap;
00870 qe->note = g_strdup (qof_main_make_utf8 (h));
00871 }
00872
00873 static void
00874 exp_setDistance (QofExpense * e, const gchar * distance_name)
00875 {
00876 gint i;
00878 for (i = 0; ExpenseDistanceNames[i] != NULL; i++)
00879 {
00880 if (0 == safe_strcmp (ExpenseDistanceNames[i], distance_name))
00881 {
00882 e->distance_unit = i;
00883 }
00884 }
00885 }
00886
00887 static void
00888 exp_setCategory (QofExpense * e, gchar * n)
00889 {
00890 g_return_if_fail (e != NULL);
00891 e->category = g_strdup (qof_main_make_utf8 (n));
00892 }
00893
00894 static gint
00895 exp_unpack (QofEntity * ent, gpointer user_data)
00896 {
00897 Expense_t *qe;
00898 QofExpense *obj;
00899 pi_buffer_t *pi_buf;
00900 PQContext *context;
00901 size_t len;
00902 gint size;
00903
00904 context = (PQContext *) user_data;
00905 g_return_val_if_fail (context != NULL, -1);
00906 g_return_val_if_fail (ent != NULL, -1);
00907 g_return_val_if_fail (PQCurrencyTable != NULL, -1);
00908 obj = (QofExpense *) ent;
00909 qe = &obj->wrap;
00910 pi_buf = (pi_buffer_t *) context->pi_buf;
00911 len = sizeof (pi_buf->data);
00912 size = unpack_Expense (qe, pi_buf->data, pi_buf->allocated);
00913 exp_setCategory (obj, context->names[context->ent_category]);
00914
00915 obj->currency = pq_currency_lookup ((QofInstance *) ent, qe->currency);
00916 return size;
00917 }
00918
00919 static gint
00920 exp_pack (QofEntity * ent, gpointer user_data)
00921 {
00922 PQContext *context;
00923 Expense_t *qe;
00924 QofExpense *obj;
00925 gint size, len;
00926
00927 size = 0;
00928 len = PQ_DEF_BUFSZ;
00929 context = (PQContext *) user_data;
00930 g_return_val_if_fail ((context), -1);
00931 obj = (QofExpense *) ent;
00932 qe = &obj->wrap;
00933
00934
00935 size = pack_Expense (qe, context->pi_buf->data, len);
00936
00937 if (size > 0)
00938 context->pi_buf->used = size;
00939 return size;
00940 }
00941
00942 static gint
00943 exp_pref_unpack (QofEntity * ent, gpointer user_data)
00944 {
00945 struct ExpensePref pref_e;
00946 PQContext *context;
00947
00948
00949 context = (PQContext *) user_data;
00950 g_return_val_if_fail (context != NULL, -1);
00951 ENTER (" ");
00952 unpack_ExpensePref (&pref_e, context->pref_buf, PQ_DEF_BUFSZ);
00953 context->pi_exp_pref.default_currency = pref_e.defaultCurrency;
00954 context->pi_exp_pref.unit_of_distance = pref_e.unitOfDistance;
00955 populate_currencies ();
00956 LEAVE (" ");
00957 return 0;
00958 }
00959
00960 static gint
00961 exp_appinfo_unpack (QofEntity * ent, gpointer user_data)
00962 {
00963 ExpenseAppInfo_t app_e;
00964 PQContext *context;
00965 gint name_count;
00966
00967
00968 context = (PQContext *) user_data;
00969 g_return_val_if_fail (context != NULL, -1);
00970 ENTER (" ");
00971 unpack_ExpenseAppInfo (&app_e, context->app_buf->data, PQ_DEF_BUFSZ);
00972 for (name_count = 0; name_count < 16; name_count++)
00973 {
00974 g_sprintf (context->names[name_count], "%s",
00975 app_e.category.name[name_count]);
00976 }
00977 context->pi_cat = &app_e.category;
00978 LEAVE (" ");
00979 return 0;
00980 }
00981
00982 static gint
00983 qof_exp_free (QofEntity * ent, gpointer user_data)
00984 {
00985 Expense_t *qe;
00986 QofExpense *obj;
00987
00988 g_return_val_if_fail (ent != NULL, -1);
00989 ENTER (" ");
00990 obj = (QofExpense *) ent;
00991 qe = &obj->wrap;
00992 free_Expense (qe);
00993 LEAVE (" ");
00994 return 0;
00995 }
00996
00997 static const gchar *
00998 expensePrintable (gpointer instance)
00999 {
01000 QofExpense *obj;
01001
01002 obj = (QofExpense *) instance;
01003 if (!obj)
01004 return NULL;
01005 if (exp_getType (obj))
01006 return g_strconcat (exp_getType (obj), " ",
01007 exp_getVendor (obj), " ", exp_getCity (obj), NULL);
01008 return NULL;
01009 }
01010
01011 static QofObject expenses_object_def = {
01012 interface_version:QOF_OBJECT_VERSION,
01013 e_type:PILOT_LINK_QOF_EXPENSES,
01014 type_label:QOF_EXPENSES_DESC,
01015 create:(gpointer) expense_create,
01016 book_begin:NULL,
01017 book_end:NULL,
01018 is_dirty:qof_collection_is_dirty,
01019 mark_clean:qof_collection_mark_clean,
01020 foreach:qof_collection_foreach,
01021 printable:expensePrintable,
01022 version_cmp:(gint (*)(gpointer, gpointer)) qof_instance_version_cmp,
01023 };
01024
01025 static PQPack expenses_pack_def = {
01026 e_type:PILOT_LINK_QOF_EXPENSES,
01027 pack_func:exp_pack,
01028 unpack_func:exp_unpack,
01029 free_pack_func:qof_exp_free,
01030 palm_db_name:Expense_DB,
01031 app_info_unpack:exp_appinfo_unpack,
01032 db_pref_unpack:exp_pref_unpack,
01033 pref_creator:EXPENSE_CREATOR,
01034 pref_flag:Expense_Pref,
01035 };
01036
01037 gboolean
01038 ExpensesRegister (void)
01039 {
01040 static QofParam params[] = {
01041 {EXP_DATE, QOF_TYPE_TIME, (QofAccessFunc) exp_getTime,
01042 (QofSetterFunc) exp_setTime},
01043 {EXP_TYPE, QOF_TYPE_STRING, (QofAccessFunc) exp_getType,
01044 (QofSetterFunc) exp_setType},
01045 {EXP_PAYMENT, QOF_TYPE_STRING, (QofAccessFunc) exp_getPayment,
01046 (QofSetterFunc) exp_setPayment},
01047 {EXP_CURRENCY, QOF_TYPE_INT32, (QofAccessFunc) exp_getCurrency,
01048 (QofSetterFunc) exp_setCurrency},
01049 {EXP_AMOUNT, QOF_TYPE_NUMERIC, (QofAccessFunc) exp_getAmount,
01050 (QofSetterFunc) exp_setAmount},
01051 {EXP_VENDOR, QOF_TYPE_STRING, (QofAccessFunc) exp_getVendor,
01052 (QofSetterFunc) exp_setVendor},
01053 {EXP_CITY, QOF_TYPE_STRING, (QofAccessFunc) exp_getCity,
01054 (QofSetterFunc) exp_setCity},
01055 {EXP_ATTENDEES, QOF_TYPE_STRING, (QofAccessFunc) exp_getAttendees,
01056 (QofSetterFunc) exp_setAttendees},
01057 {EXP_NOTE, QOF_TYPE_STRING, (QofAccessFunc) exp_getNote,
01058 (QofSetterFunc) exp_setNote},
01059 {EXP_DISTANCE, QOF_TYPE_STRING, (QofAccessFunc) exp_getDistance,
01060 (QofSetterFunc) exp_setDistance},
01061 {EXP_CATEGORY, QOF_TYPE_STRING, (QofAccessFunc) exp_getCategory,
01062 (QofSetterFunc) exp_setCategory},
01063 {EXP_KVP, QOF_TYPE_KVP, (QofAccessFunc) qof_instance_get_slots,
01064 NULL},
01065 {QOF_PARAM_BOOK, QOF_ID_BOOK,
01066 (QofAccessFunc) qof_instance_get_book, NULL},
01067 {QOF_PARAM_GUID, QOF_TYPE_GUID,
01068 (QofAccessFunc) qof_instance_get_guid, NULL},
01069 {NULL},
01070 };
01071
01072 qof_class_register (PILOT_LINK_QOF_EXPENSES, NULL, params);
01073
01074 pilot_qof_pack_register (&expenses_pack_def);
01075
01076 return qof_object_register (&expenses_object_def);
01077 }
01078