gpe-expenses 0.1.9
|
00001 /*************************************************************************** 00002 * qof-expenses.c 00003 * 00004 * Thu Oct 21 07:59:13 2004-2005 00005 * Copyright 2004-2009 Neil Williams <linux@codehelp.co.uk> 00006 ****************************************************************************/ 00007 /* 00008 This program is free software; you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation; either version 3 of the License, or 00011 (at your option) any later version. 00012 00013 This program is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with this program. If not, see <http://www.gnu.org/licenses/>. 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 /* leave the macro in place to support types. 00039 allow translation by mapping the gcc -E output. 00040 */ 00041 AS_STRING_FUNC (ExpenseType, EXPENSE_TYPE_LIST) 00042 FROM_STRING_FUNC(ExpenseType, EXPENSE_TYPE_LIST) 00043 00048 static GHashTable *qof_currency_table = NULL; 00049 00050 void 00051 qof_currency_foreach(QofCurrencyCB cb, gpointer user_data) 00052 { 00053 g_hash_table_foreach(qof_currency_table, cb, user_data); 00054 } 00055 00072 static void 00073 populate_currencies (void) 00074 { 00075 qof_currency_table = g_hash_table_new(g_direct_hash, g_direct_equal); 00076 { /* Australia 0 100 AU$ AUD */ 00077 QofCurrency *c = g_new0(QofCurrency, 1); 00078 c->pq_code = 0; 00079 c->fraction = 100; 00080 c->symbol = "AU$"; 00081 c->mnemonic = "AUD"; 00082 c->non_utf8 = FALSE; 00083 g_hash_table_insert(qof_currency_table, 00084 GINT_TO_POINTER(c->pq_code), (gpointer)c); 00085 } 00086 { /* Austria 1 100 € ATS */ 00087 QofCurrency *c = g_new0(QofCurrency, 1); 00088 c->pq_code = 1; 00089 c->fraction = 100; 00090 c->symbol = "€"; 00091 c->mnemonic = "ATS"; 00092 c->non_utf8 = TRUE; 00093 g_hash_table_insert(qof_currency_table, 00094 GINT_TO_POINTER(c->pq_code), (gpointer)c); 00095 } 00096 { /* Belgium 2 100 € BEF */ 00097 QofCurrency *c = g_new0(QofCurrency, 1); 00098 c->pq_code = 2; 00099 c->fraction = 100; 00100 c->symbol = "€"; 00101 c->mnemonic = "BEF"; 00102 c->non_utf8 = TRUE; 00103 g_hash_table_insert(qof_currency_table, 00104 GINT_TO_POINTER(c->pq_code), (gpointer)c); 00105 } 00106 { /* Brazil 3 100 R$ BRL */ 00107 QofCurrency *c = g_new0(QofCurrency, 1); 00108 c->pq_code = 3; 00109 c->fraction = 100; 00110 c->symbol = "R$"; 00111 c->mnemonic = "BRL"; 00112 c->non_utf8 = FALSE; 00113 g_hash_table_insert(qof_currency_table, 00114 GINT_TO_POINTER(c->pq_code), (gpointer)c); 00115 } 00116 { /* Canada 4 100 $CN CAD */ 00117 QofCurrency *c = g_new0(QofCurrency, 1); 00118 c->pq_code = 4; 00119 c->fraction = 100; 00120 c->symbol = "$CN"; 00121 c->mnemonic = "CAD"; 00122 c->non_utf8 = FALSE; 00123 g_hash_table_insert(qof_currency_table, 00124 GINT_TO_POINTER(c->pq_code), (gpointer)c); 00125 } 00126 { /* Denmark 5 100 DKK DKK */ 00127 QofCurrency *c = g_new0(QofCurrency, 1); 00128 c->pq_code = 5; 00129 c->fraction = 100; 00130 c->symbol = "DKK"; 00131 c->mnemonic = "DKK"; 00132 c->non_utf8 = FALSE; 00133 g_hash_table_insert(qof_currency_table, 00134 GINT_TO_POINTER(c->pq_code), (gpointer)c); 00135 } 00136 { /* Finland 6 100 € FIM */ 00137 QofCurrency *c = g_new0(QofCurrency, 1); 00138 c->pq_code = 6; 00139 c->fraction = 100; 00140 c->symbol = "€"; 00141 c->mnemonic = "FIM"; 00142 c->non_utf8 = TRUE; 00143 g_hash_table_insert(qof_currency_table, 00144 GINT_TO_POINTER(c->pq_code), (gpointer)c); 00145 } 00146 { /* France 7 100 € FRF */ 00147 QofCurrency *c = g_new0(QofCurrency, 1); 00148 c->pq_code = 7; 00149 c->fraction = 100; 00150 c->symbol = "€"; 00151 c->mnemonic = "FRF"; 00152 c->non_utf8 = TRUE; 00153 g_hash_table_insert(qof_currency_table, 00154 GINT_TO_POINTER(c->pq_code), (gpointer)c); 00155 } 00156 { /* Germany 8 100 € DEM */ 00157 QofCurrency *c = g_new0(QofCurrency, 1); 00158 c->pq_code = 8; 00159 c->fraction = 100; 00160 c->symbol = "€"; 00161 c->mnemonic = "DEM"; 00162 c->non_utf8 = TRUE; 00163 g_hash_table_insert(qof_currency_table, 00164 GINT_TO_POINTER(c->pq_code), (gpointer)c); 00165 } 00166 { /* Hong Kong 9 100 HK$ HKD */ 00167 QofCurrency *c = g_new0(QofCurrency, 1); 00168 c->pq_code = 9; 00169 c->fraction = 100; 00170 c->symbol = "HK$"; 00171 c->mnemonic = "HKD"; 00172 c->non_utf8 = FALSE; 00173 g_hash_table_insert(qof_currency_table, 00174 GINT_TO_POINTER(c->pq_code), (gpointer)c); 00175 } 00176 { /* Iceland 10 100 ISK ISK */ 00177 QofCurrency *c = g_new0(QofCurrency, 1); 00178 c->pq_code = 10; 00179 c->fraction = 100; 00180 c->symbol = "ISK"; 00181 c->mnemonic = "ISK"; 00182 c->non_utf8 = FALSE; 00183 g_hash_table_insert(qof_currency_table, 00184 GINT_TO_POINTER(c->pq_code), (gpointer)c); 00185 } 00186 { /* Ireland 11 100 € IEP */ 00187 QofCurrency *c = g_new0(QofCurrency, 1); 00188 c->pq_code = 11; 00189 c->fraction = 100; 00190 c->symbol = "€"; 00191 c->mnemonic = "IEP"; 00192 c->non_utf8 = TRUE; 00193 g_hash_table_insert(qof_currency_table, 00194 GINT_TO_POINTER(c->pq_code), (gpointer)c); 00195 } 00196 { /* Italy 12 1 EUR ITL */ 00197 /* The Italian Lira had a fraction == 1*/ 00198 QofCurrency *c = g_new0(QofCurrency, 1); 00199 c->pq_code = 12; 00200 c->fraction = 100; 00201 c->symbol = "EUR"; 00202 c->mnemonic = "ITL"; 00203 c->non_utf8 = FALSE; 00204 g_hash_table_insert(qof_currency_table, 00205 GINT_TO_POINTER(c->pq_code), (gpointer)c); 00206 } 00207 { /* Japan 13 1 ¥ ¥ JPY */ 00208 QofCurrency *c = g_new0(QofCurrency, 1); 00209 c->pq_code = 13; 00210 c->fraction = 1; 00211 c->symbol = "¥"; 00212 c->mnemonic = "JPY"; 00213 c->non_utf8 = TRUE; 00214 g_hash_table_insert(qof_currency_table, 00215 GINT_TO_POINTER(c->pq_code), (gpointer)c); 00216 } 00217 { /* Luxembourg 14 100 ˆ € LUF */ 00218 QofCurrency *c = g_new0(QofCurrency, 1); 00219 c->pq_code = 14; 00220 c->fraction = 100; 00221 c->symbol = "€"; 00222 c->mnemonic = "LUF"; 00223 c->non_utf8 = TRUE; 00224 g_hash_table_insert(qof_currency_table, 00225 GINT_TO_POINTER(c->pq_code), (gpointer)c); 00226 } 00227 { /* Mexico 15 100 MXP MXP */ 00228 QofCurrency *c = g_new0(QofCurrency, 1); 00229 c->pq_code = 15; 00230 c->fraction = 100; 00231 c->symbol = "MXP"; 00232 c->mnemonic = "MXP"; 00233 c->non_utf8 = FALSE; 00234 g_hash_table_insert(qof_currency_table, 00235 GINT_TO_POINTER(c->pq_code), (gpointer)c); 00236 } 00237 { /* Netherlands 16 100 € ANG */ 00238 QofCurrency *c = g_new0(QofCurrency, 1); 00239 c->pq_code = 16; 00240 c->fraction = 100; 00241 c->symbol = "€"; 00242 c->mnemonic = "ANG"; 00243 c->non_utf8 = TRUE; 00244 g_hash_table_insert(qof_currency_table, 00245 GINT_TO_POINTER(c->pq_code), (gpointer)c); 00246 } 00247 { /* New Zealand 17 100 $NZ NZD */ 00248 QofCurrency *c = g_new0(QofCurrency, 1); 00249 c->pq_code = 17; 00250 c->fraction = 100; 00251 c->symbol = "$NZ"; 00252 c->mnemonic = "NZD"; 00253 c->non_utf8 = FALSE; 00254 g_hash_table_insert(qof_currency_table, 00255 GINT_TO_POINTER(c->pq_code), (gpointer)c); 00256 } 00257 { /* Norway 18 100 NOK NOK */ 00258 QofCurrency *c = g_new0(QofCurrency, 1); 00259 c->pq_code = 18; 00260 c->fraction = 100; 00261 c->symbol = "NOK"; 00262 c->mnemonic = "NOK"; 00263 c->non_utf8 = FALSE; 00264 g_hash_table_insert(qof_currency_table, 00265 GINT_TO_POINTER(c->pq_code), (gpointer)c); 00266 } 00267 { /* Spain 19 100 € ESP */ 00268 QofCurrency *c = g_new0(QofCurrency, 1); 00269 c->pq_code = 19; 00270 c->fraction = 100; 00271 c->symbol = "€"; 00272 c->mnemonic = "ESP"; 00273 c->non_utf8 = TRUE; 00274 g_hash_table_insert(qof_currency_table, 00275 GINT_TO_POINTER(c->pq_code), (gpointer)c); 00276 } 00277 { /* Sweden 20 100 SEK SEK */ 00278 QofCurrency *c = g_new0(QofCurrency, 1); 00279 c->pq_code = 20; 00280 c->fraction = 100; 00281 c->symbol = "SEK"; 00282 c->mnemonic = "SEK"; 00283 c->non_utf8 = FALSE; 00284 g_hash_table_insert(qof_currency_table, 00285 GINT_TO_POINTER(c->pq_code), (gpointer)c); 00286 } 00287 { /* Switzerland 21 100 CHF CHF */ 00288 QofCurrency *c = g_new0(QofCurrency, 1); 00289 c->pq_code = 21; 00290 c->fraction = 100; 00291 c->symbol = "CHF"; 00292 c->mnemonic = "CHF"; 00293 c->non_utf8 = FALSE; 00294 g_hash_table_insert(qof_currency_table, 00295 GINT_TO_POINTER(c->pq_code), (gpointer)c); 00296 } 00297 { /* United Kingdom 22 100 £ GBP */ 00298 QofCurrency *c = g_new0(QofCurrency, 1); 00299 c->pq_code = 22; 00300 c->fraction = 100; 00301 c->symbol = "£"; 00302 c->mnemonic = "GBP"; 00303 c->non_utf8 = TRUE; 00304 g_hash_table_insert(qof_currency_table, 00305 GINT_TO_POINTER(c->pq_code), (gpointer)c); 00306 } 00307 { /* United States 23 100 $US USD */ 00308 QofCurrency *c = g_new0(QofCurrency, 1); 00309 c->pq_code = 23; 00310 c->fraction = 100; 00311 c->symbol = "$US"; 00312 c->mnemonic = "USD"; 00313 c->non_utf8 = FALSE; 00314 g_hash_table_insert(qof_currency_table, 00315 GINT_TO_POINTER(c->pq_code), (gpointer)c); 00316 } 00317 { /* India 24 100 Rs INR */ 00318 QofCurrency *c = g_new0(QofCurrency, 1); 00319 c->pq_code = 24; 00320 c->fraction = 100; 00321 c->symbol = "Rs"; 00322 c->mnemonic = "INR"; 00323 c->non_utf8 = FALSE; 00324 g_hash_table_insert(qof_currency_table, 00325 GINT_TO_POINTER(c->pq_code), (gpointer)c); 00326 } 00327 { /* Indonesia 25 1 Rp IDR */ 00328 QofCurrency *c = g_new0(QofCurrency, 1); 00329 c->pq_code = 25; 00330 c->fraction = 1; 00331 c->symbol = "Rp"; 00332 c->mnemonic = "IDR"; 00333 c->non_utf8 = FALSE; 00334 g_hash_table_insert(qof_currency_table, 00335 GINT_TO_POINTER(c->pq_code), (gpointer)c); 00336 } 00337 { /* Korea 26 100 KRW KRW (South) */ 00338 QofCurrency *c = g_new0(QofCurrency, 1); 00339 c->pq_code = 26; 00340 c->fraction = 100; 00341 c->symbol = "KRW"; 00342 c->mnemonic = "KRW"; 00343 c->non_utf8 = FALSE; 00344 g_hash_table_insert(qof_currency_table, 00345 GINT_TO_POINTER(c->pq_code), (gpointer)c); 00346 } 00347 { /* Malaysia 27 100 RM MYR */ 00348 QofCurrency *c = g_new0(QofCurrency, 1); 00349 c->pq_code = 27; 00350 c->fraction = 100; 00351 c->symbol = "RM"; 00352 c->mnemonic = "MYR"; 00353 c->non_utf8 = FALSE; 00354 g_hash_table_insert(qof_currency_table, 00355 GINT_TO_POINTER(c->pq_code), (gpointer)c); 00356 } 00357 { /* P.R.C. // People's Rep. China 28 100 RMB CNY */ 00358 QofCurrency *c = g_new0(QofCurrency, 1); 00359 c->pq_code = 28; 00360 c->fraction = 100; 00361 c->symbol = "RMB"; 00362 c->mnemonic = "CNY"; 00363 c->non_utf8 = FALSE; 00364 g_hash_table_insert(qof_currency_table, 00365 GINT_TO_POINTER(c->pq_code), (gpointer)c); 00366 } 00367 { /* Phillipines 29 100 P PHP */ 00368 QofCurrency *c = g_new0(QofCurrency, 1); 00369 c->pq_code = 29; 00370 c->fraction = 100; 00371 c->symbol = "P"; 00372 c->mnemonic = "PHP"; 00373 c->non_utf8 = FALSE; 00374 g_hash_table_insert(qof_currency_table, 00375 GINT_TO_POINTER(c->pq_code), (gpointer)c); 00376 } 00377 { /* Singapore 30 100 $ SGD */ 00378 QofCurrency *c = g_new0(QofCurrency, 1); 00379 c->pq_code = 30; 00380 c->fraction = 100; 00381 c->symbol = "$"; 00382 c->mnemonic = "SGD"; 00383 c->non_utf8 = FALSE; 00384 g_hash_table_insert(qof_currency_table, 00385 GINT_TO_POINTER(c->pq_code), (gpointer)c); 00386 } 00387 { /* Thailand 31 100 BHT THB */ 00388 QofCurrency *c = g_new0(QofCurrency, 1); 00389 c->pq_code = 31; 00390 c->fraction = 100; 00391 c->symbol = "BHT"; 00392 c->mnemonic = "THB"; 00393 c->non_utf8 = FALSE; 00394 g_hash_table_insert(qof_currency_table, 00395 GINT_TO_POINTER(c->pq_code), (gpointer)c); 00396 } 00397 { /* Taiwan 32 100 NT$ TWD */ 00398 QofCurrency *c = g_new0(QofCurrency, 1); 00399 c->pq_code = 32; 00400 c->fraction = 100; 00401 c->symbol = "NT$"; 00402 c->mnemonic = "TWD"; 00403 c->non_utf8 = FALSE; 00404 g_hash_table_insert(qof_currency_table, 00405 GINT_TO_POINTER(c->pq_code), (gpointer)c); 00406 } 00407 { /* EU (Euro) 133 100 € EUR */ 00408 QofCurrency *c = g_new0(QofCurrency, 1); 00409 c->pq_code = 133; 00410 c->fraction = 100; 00411 c->symbol = "€"; 00412 c->mnemonic = "EUR"; 00413 c->non_utf8 = TRUE; 00414 g_hash_table_insert(qof_currency_table, 00415 GINT_TO_POINTER(c->pq_code), (gpointer)c); 00416 } 00417 } 00418 00419 static gboolean 00420 check_name (gpointer G_GNUC_UNUSED key, gpointer value, gpointer data) 00421 { 00422 gchar * mnemonic = (gchar*) data; 00423 QofCurrency * currency = (QofCurrency*) value; 00424 if (0 == safe_strcmp (mnemonic, currency->mnemonic)) 00425 return TRUE; 00426 return FALSE; 00427 } 00428 00429 QofCurrency* 00430 qof_currency_lookup_name (QofInstance * inst, gchar * mnemonic) 00431 { 00432 QofCurrency *currency; 00433 00434 currency = NULL; 00435 if(!qof_currency_table) populate_currencies(); 00436 currency = (QofCurrency*) g_hash_table_find 00437 (qof_currency_table, check_name, mnemonic); 00438 if (!currency) return NULL; 00439 qof_currency_lookup(inst, currency->pq_code); 00440 return currency; 00441 } 00442 00443 QofCurrency* 00444 qof_currency_lookup (QofInstance* inst, gint currency_code) 00445 { 00446 QofCurrency *currency; 00447 00448 currency = NULL; 00449 if(!qof_currency_table) populate_currencies(); 00450 currency = (QofCurrency*)g_hash_table_lookup( 00451 qof_currency_table, GINT_TO_POINTER(currency_code)); 00452 if(!currency) 00453 { 00454 PERR (" unsupported currency! %d", currency_code); 00455 return NULL; 00456 } 00457 kvp_frame_set_string(qof_instance_get_slots(inst), 00458 PQ_CURRENCY_MNEMONIC, currency->mnemonic); 00459 kvp_frame_set_gint64(qof_instance_get_slots(inst), 00460 PQ_CURRENCY_FRACTION, currency->fraction); 00461 if(currency->non_utf8) 00462 { 00463 gchar * k_symbol = g_strdup (currency->symbol); 00464 kvp_frame_set_string(qof_instance_get_slots(inst), 00465 PQ_CURRENCY_SYMBOL, qof_util_make_utf8(k_symbol)); 00466 } 00467 else 00468 { 00469 kvp_frame_set_string(qof_instance_get_slots(inst), 00470 PQ_CURRENCY_SYMBOL, currency->symbol); 00471 } 00472 return currency; 00473 } 00474 00475 struct QofExp_s 00476 { 00477 QofInstance inst; 00478 ExpenseDistance distance_unit; 00479 struct tm date; 00480 ExpenseType type; 00481 ExpensePayment payment; 00482 gint currency_code; 00483 gchar *amount; 00484 gchar *vendor; 00485 gchar *city; 00486 gchar *attendees; 00487 gchar *note; 00488 gchar *category; 00489 const gchar* print_string; 00490 QofCurrency *currency; 00491 gdouble temp_amount; 00494 gboolean reset_amount; 00495 }; 00496 00497 static QofExp * 00498 expense_create (QofBook *book) 00499 { 00500 QofExp *obj; 00501 QofCollection *coll; 00502 GList *all; 00503 glong nanosecs; 00504 QofTime *qt; 00505 QofDate *qd; 00506 00507 obj = g_new0(QofExp, 1); 00508 qof_instance_init (&obj->inst, GPE_QOF_EXPENSES, book); 00509 coll = qof_book_get_collection (book, GPE_QOF_EXPENSES); 00510 all = qof_collection_get_data (coll); 00511 all = g_list_prepend (all, obj); 00512 qof_collection_set_data (coll, all); 00513 qt = qof_time_get_current (); 00514 nanosecs = qof_time_get_nanosecs (qt); 00515 qd = qof_date_from_qtime (qt); 00516 if (!qof_date_to_struct_tm (qd, &obj->date, &nanosecs)) 00517 DEBUG (" failed to set initial date"); 00518 qof_date_free (qd); 00519 qof_time_free (qt); 00520 obj->amount = "0"; 00521 /* 0 == AU$ so use an init value */ 00522 obj->currency_code = -1; 00523 if(!qof_currency_table) populate_currencies(); 00524 qof_event_gen ((QofEntity*)obj, QOF_EVENT_CREATE, NULL); 00525 return obj; 00526 } 00527 00528 static const gchar* 00529 qof_exp_paymentAsString(ExpensePayment payment) 00530 { 00531 const gchar *string; 00532 00533 string = "Cash"; // following install-expenses convention. 00534 string = ExpensePaymentasString(payment); 00535 return string; 00536 } 00537 00538 static const gchar* 00539 qof_exp_typeAsString(ExpenseType type) 00540 { 00541 const gchar* string; 00542 00543 string = "Bus"; // following install-expenses convention. 00544 string = ExpenseTypeasString(type); 00545 return string; 00546 } 00547 00548 static ExpensePayment 00549 qof_exp_paymentFromString(const gchar* payment_string) 00550 { 00551 return ExpensePaymentfromString(payment_string); 00552 } 00553 00554 static ExpenseType 00555 qof_exp_typeFromString(const gchar* type_string) 00556 { 00557 return ExpenseTypefromString(type_string); 00558 } 00559 00560 static QofTime * 00561 exp_getTime (QofExp * e) 00562 { 00563 QofDate *qd; 00564 QofTime *qt; 00565 00566 g_return_val_if_fail (e != NULL, NULL); 00567 qd = qof_date_from_struct_tm (&e->date); 00568 qt = qof_date_to_qtime (qd); 00569 qof_date_free (qd); 00570 return qt; 00571 } 00572 00573 static gchar* 00574 exp_getType (QofExp * e) 00575 { 00576 gchar* string; 00577 00578 g_return_val_if_fail (e != NULL, NULL); 00579 string = g_strdup(qof_exp_typeAsString(e->type)); 00580 return string; 00581 } 00582 00583 static gchar* 00584 exp_getPayment (QofExp * e) 00585 { 00586 gchar* string; 00587 00588 g_return_val_if_fail (e != NULL, NULL); 00589 string = g_strdup(qof_exp_paymentAsString(e->payment)); 00590 return string; 00591 } 00592 00593 static gint 00594 exp_getCurrency (QofExp * e) 00595 { 00596 g_return_val_if_fail (e != NULL, -1); 00597 return e->currency_code; 00598 } 00599 00600 static QofNumeric 00601 exp_getAmount (QofExp * e) 00602 { 00603 QofNumeric amount; 00604 gdouble pi_amount; 00605 gchar *numeric_char; 00606 00607 amount = qof_numeric_zero (); 00608 g_return_val_if_fail (e != NULL, amount); 00609 if(e->amount == 0) { return amount; } 00610 /* floating point as a string converts to gnc_numeric */ 00611 pi_amount = strtod (e->amount, NULL); 00612 if(e->currency) 00613 { 00614 amount = qof_numeric_from_double (pi_amount, e->currency->fraction, 00615 QOF_HOW_DENOM_EXACT | QOF_HOW_RND_ROUND); 00616 } 00617 else /* default: use the most common fraction in the Palm currency list. */ 00618 { 00619 amount = qof_numeric_from_double (pi_amount, 100, 00620 QOF_HOW_DENOM_EXACT | QOF_HOW_RND_ROUND); 00621 } 00622 numeric_char = qof_numeric_to_string(amount); 00623 g_free(numeric_char); 00624 00625 if (qof_numeric_check (amount) == QOF_ERROR_OK) 00626 return amount; 00627 return qof_numeric_zero (); 00628 } 00629 00630 static const gchar * 00631 exp_getVendor (QofExp * e) 00632 { 00633 g_return_val_if_fail (e != NULL, NULL); 00634 return e->vendor; 00635 } 00636 00637 static const gchar * 00638 exp_getCity (QofExp * e) 00639 { 00640 g_return_val_if_fail (e != NULL, NULL); 00641 return e->city; 00642 } 00643 00644 static const gchar * 00645 exp_getAttendees (QofExp * e) 00646 { 00647 g_return_val_if_fail (e != NULL, NULL); 00648 return e->attendees; 00649 } 00650 00651 static const gchar * 00652 exp_getNote (QofExp * e) 00653 { 00654 g_return_val_if_fail (e != NULL, NULL); 00655 return e->note; 00656 } 00657 00658 static const gchar* 00659 exp_getDistance(QofExp *e) 00660 { 00661 g_return_val_if_fail(e != NULL, NULL); 00662 return ExpenseDistanceasString(e->distance_unit); 00663 } 00664 00665 static const gchar* 00666 exp_getCategory(QofExp *e) 00667 { 00668 g_return_val_if_fail(e != NULL, NULL); 00669 return e->category; 00670 } 00671 00672 static void 00673 exp_setTime (QofExp * e, QofTime *h) 00674 { 00675 glong nanosecs; 00676 QofDate *qd; 00677 00678 g_return_if_fail (e != NULL); 00679 if (!h) 00680 return; 00681 nanosecs = qof_time_get_nanosecs (h); 00682 qd = qof_date_from_qtime (h); 00683 qof_date_to_struct_tm (qd, &e->date, &nanosecs); 00684 qof_date_free (qd); 00685 } 00686 00687 static void 00688 exp_setType (QofExp * e, const gchar *type_string) 00689 { 00690 g_return_if_fail (e != NULL); 00691 e->type = qof_exp_typeFromString(type_string); 00692 } 00693 00694 static void 00695 exp_setPayment (QofExp * e, const gchar *payment_string) 00696 { 00697 g_return_if_fail (e != NULL); 00698 e->payment = qof_exp_paymentFromString(payment_string); 00699 } 00700 00701 static void 00702 exp_combine_currency_with_amount(QofExp *e) 00703 { 00704 g_return_if_fail (e != NULL); 00705 if(!e->currency || e->currency_code < 0) 00706 { 00707 /* the gint32 currency is actually to be set BEFORE the numeric amount. :-( */ 00708 e->currency = qof_currency_lookup((QofInstance*)e, e->currency_code); 00709 } 00710 if(!e->currency) 00711 { 00712 PERR (" Unable to identify currency fraction." 00713 " Using two decimal places."); 00714 /* Amount is stored in the Palm as a string version 00715 of a floating point number. */ 00716 e->amount = g_strdup_printf ("%.2f", e->temp_amount); 00717 return; 00718 } 00719 switch (e->currency->fraction) 00720 { 00721 case 1 : 00722 { 00723 e->amount = g_strdup_printf ("%.0f", e->temp_amount); 00724 break; 00725 } 00726 case 10 : 00727 { 00728 e->amount = g_strdup_printf ("%.1f", e->temp_amount); 00729 break; 00730 } 00731 case 100 : 00732 { 00733 e->amount = g_strdup_printf ("%.2f", e->temp_amount); 00734 break; 00735 } 00736 case 1000 : 00737 { 00738 e->amount = g_strdup_printf ("%.3f", e->temp_amount); 00739 break; 00740 } 00741 default : 00742 { 00743 PERR (" Invalid currency fraction." 00744 " Using two decimal places as default."); 00745 e->amount = g_strdup_printf ("%.2f", e->temp_amount); 00746 } 00747 } 00748 } 00749 00750 static void 00751 exp_setCurrency (QofExp * e, gint data) 00752 { 00753 g_return_if_fail (e != NULL); 00754 e->currency_code = data; 00755 e->currency = qof_currency_lookup((QofInstance*)e, data); 00756 if(e->reset_amount) 00757 exp_combine_currency_with_amount(e); 00758 e->reset_amount = FALSE; 00759 } 00760 00761 static void 00762 exp_setAmount (QofExp * e, QofNumeric h) 00763 { 00764 g_return_if_fail (e != NULL); 00765 e->temp_amount = qof_numeric_to_double (h); 00766 e->reset_amount = TRUE; 00767 /* if an amount can ever be set without a currency_code, 00768 this needs to be reviewed. */ 00771 if(e->currency) exp_combine_currency_with_amount(e); 00772 } 00773 00774 static void 00775 exp_setVendor (QofExp * e, gchar *h) 00776 { 00777 g_return_if_fail (e != NULL); 00778 e->vendor = g_strdup (qof_util_make_utf8(h)); 00779 } 00780 00781 static void 00782 exp_setCity (QofExp * e, gchar *h) 00783 { 00784 g_return_if_fail (e != NULL); 00785 e->city = g_strdup (qof_util_make_utf8(h)); 00786 } 00787 00788 static void 00789 exp_setAttendees (QofExp * e, gchar *h) 00790 { 00791 g_return_if_fail (e != NULL); 00792 e->attendees = g_strdup (qof_util_make_utf8(h)); 00793 } 00794 00795 static void 00796 exp_setNote (QofExp * e, gchar *h) 00797 { 00798 g_return_if_fail (e != NULL); 00799 e->note = g_strdup (qof_util_make_utf8(h)); 00800 } 00801 00802 static void 00803 exp_setDistance(QofExp *e, const gchar *distance_name) 00804 { 00805 g_return_if_fail(e); 00806 e->distance_unit = ExpenseDistancefromString(distance_name); 00807 } 00808 00809 static void 00810 exp_setCategory(QofExp *e, gchar *n) 00811 { 00812 g_return_if_fail(e != NULL); 00813 e->category = g_strdup(qof_util_make_utf8(n)); 00814 } 00815 00816 static const gchar* 00817 expensePrintable (gpointer instance) 00818 { 00819 QofExp *obj; 00820 00821 obj = (QofExp*)instance; 00822 if(!obj) return NULL; 00823 if(exp_getType(obj)) 00824 return g_strconcat(exp_getType(obj), " ", 00825 exp_getVendor(obj), " ", exp_getCity(obj), NULL); 00826 return NULL; 00827 } 00828 00829 static QofObject expenses_object_def = { 00830 .interface_version = QOF_OBJECT_VERSION, 00831 .e_type = GPE_QOF_EXPENSES, 00832 .type_label = QOF_EXPENSES_DESC, 00833 .create = ((gpointer)expense_create), 00834 .book_begin = NULL, 00835 .book_end = NULL, 00836 .is_dirty = qof_collection_is_dirty, 00837 .mark_clean = qof_collection_mark_clean, 00838 .foreach = qof_collection_foreach, 00839 .printable = expensePrintable, 00840 .version_cmp = (gint (*)(gpointer, gpointer)) qof_instance_version_cmp, 00841 }; 00842 00843 gboolean 00844 ExpensesRegister (void) 00845 { 00846 static QofParam params[] = { 00847 { EXP_DATE, QOF_TYPE_TIME, (QofAccessFunc) exp_getTime, (QofSetterFunc) exp_setTime, NULL }, 00848 { EXP_TYPE, QOF_TYPE_STRING, (QofAccessFunc) exp_getType, (QofSetterFunc) exp_setType, NULL }, 00849 { EXP_PAYMENT, QOF_TYPE_STRING, (QofAccessFunc) exp_getPayment, (QofSetterFunc) exp_setPayment, NULL }, 00850 { EXP_CURRENCY, QOF_TYPE_INT32, (QofAccessFunc) exp_getCurrency, (QofSetterFunc) exp_setCurrency, NULL }, 00851 { EXP_AMOUNT, QOF_TYPE_NUMERIC, (QofAccessFunc) exp_getAmount, (QofSetterFunc) exp_setAmount, NULL }, 00852 { EXP_VENDOR, QOF_TYPE_STRING, (QofAccessFunc) exp_getVendor, (QofSetterFunc) exp_setVendor, NULL }, 00853 { EXP_CITY, QOF_TYPE_STRING, (QofAccessFunc) exp_getCity, (QofSetterFunc) exp_setCity, NULL }, 00854 { EXP_ATTENDEES, QOF_TYPE_STRING, (QofAccessFunc) exp_getAttendees, (QofSetterFunc) exp_setAttendees, NULL }, 00855 { EXP_NOTE, QOF_TYPE_STRING, (QofAccessFunc) exp_getNote, (QofSetterFunc) exp_setNote, NULL }, 00856 { EXP_DISTANCE, QOF_TYPE_STRING, (QofAccessFunc) exp_getDistance, (QofSetterFunc) exp_setDistance, NULL }, 00857 { EXP_CATEGORY, QOF_TYPE_STRING, (QofAccessFunc) exp_getCategory, (QofSetterFunc) exp_setCategory, NULL }, 00858 { EXP_KVP, QOF_TYPE_KVP, (QofAccessFunc) qof_instance_get_slots, NULL, NULL }, 00859 { QOF_PARAM_BOOK, QOF_ID_BOOK, (QofAccessFunc) qof_instance_get_book, NULL, NULL }, 00860 { QOF_PARAM_GUID, QOF_TYPE_GUID, (QofAccessFunc) qof_instance_get_guid, NULL, NULL }, 00861 { NULL, NULL, NULL, NULL, NULL }, 00862 }; 00863 00864 bindtextdomain (LIBRARY_GETTEXT_PACKAGE, LOCALE_DIR); 00865 00866 qof_class_register (GPE_QOF_EXPENSES, NULL, params); 00867 if(!qof_currency_table) populate_currencies(); 00868 00869 return qof_object_register (&expenses_object_def); 00870 }