qof-expenses.c

00001 /***************************************************************************
00002  *            qof-expenses.c
00003  *
00004  *  Thu Oct 21 07:59:13 2004-2005
00005  *  Copyright  2004-2005  Neil Williams  <linux@codehelp.co.uk>
00006  ****************************************************************************/
00007 /*
00008     This package 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 
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         {       /* Australia    0       100             AU$             AUD */
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         {       /* Austria      1       100             €             ATS */
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         {       /* Belgium      2       100             €             BEF */
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         {       /* Brazil       3       100             R$              BRL */
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         {       /* Canada       4       100             $CN             CAD */
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         {       /* Denmark      5       100             DKK             DKK */
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         {       /* Finland      6       100             €             FIM */
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         {       /* France       7       100             €             FRF */
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         {       /* Germany      8       100             €             DEM */
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         {       /* Hong Kong    9       100             HK$             HKD */
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         {       /* Iceland      10      100             ISK             ISK */
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         {       /* Ireland      11      100             €             IEP */
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         {       /* Italy        12      1               EUR             ITL */
00228                 /* The Italian Lira had a fraction == 1*/
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         {       /* Japan        13      1       &#165;  ¥              JPY */
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         {       /* Luxembourg   14      100     &#136;  €             LUF */
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         {       /* Mexico       15      100             MXP             MXP */
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         {       /* Netherlands  16      100             €             ANG */
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         {       /* New Zealand  17      100             $NZ             NZD */
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         {       /* Norway       18      100             NOK             NOK */
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         {       /* Spain        19      100             €             ESP */
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         {       /* Sweden       20      100             SEK             SEK */
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         {       /* Switzerland  21      100             CHF             CHF */
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         {       /* United Kingdom 22    100             £              GBP */
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         {       /* United States  23    100             $US             USD */
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         {       /* India        24      100             Rs              INR */
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         {       /* Indonesia    25      1               Rp              IDR */
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         {       /* Korea        26      100             KRW             KRW  (South) */
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         {       /* Malaysia     27      100             RM              MYR */
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         {       /* P.R.C. // People's Rep. China 28     100     RMB             CNY */
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         {       /* Phillipines  29      100             P               PHP */
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         {       /* Singapore    30      100             $               SGD */
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         {       /* Thailand     31      100             BHT             THB */
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         {       /* Taiwan       32      100             NT$             TWD */
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         {       /* EU (Euro)    133     100             €             EUR */
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         gpe_currency_lookup(inst, currency->pq_code);
00470         return currency;
00471 }
00472 
00473 GpeCurrency*
00474 gpe_currency_lookup (QofInstance* inst, gint currency_code)
00475 {
00476         GpeCurrency *currency;
00477 
00478         currency = NULL;
00479         if(!gpe_currency_table) populate_currencies();
00480         currency = (GpeCurrency*)g_hash_table_lookup(
00481                 gpe_currency_table, GINT_TO_POINTER(currency_code));
00482         if(!currency)
00483         {
00484                 PERR (" unsupported currency! %d", currency_code);
00485                 return NULL;
00486         }
00487         kvp_frame_set_string(qof_instance_get_slots(inst),
00488                 PQ_CURRENCY_MNEMONIC, currency->mnemonic);
00489         kvp_frame_set_gint64(qof_instance_get_slots(inst),
00490                 PQ_CURRENCY_FRACTION, currency->fraction);
00491         if(currency->non_utf8)
00492         {
00493                 gchar * k_symbol = g_strdup (currency->symbol);
00494                 kvp_frame_set_string(qof_instance_get_slots(inst),
00495                         PQ_CURRENCY_SYMBOL, qof_main_make_utf8(k_symbol));
00496         }
00497         else
00498         {
00499                 kvp_frame_set_string(qof_instance_get_slots(inst),
00500                         PQ_CURRENCY_SYMBOL, currency->symbol);
00501         }
00502         return currency;
00503 }       
00504 
00505 typedef struct ExpenseCustomCurrency {
00506                 gchar name[16];
00507                 gchar symbol[4];
00508                 gchar rate[8];
00509 } ExpenseCustomCurrency_t;
00510 
00511 struct Expense {
00512                 struct tm date;
00513                 ExpenseType type;
00514                 ExpensePayment payment;
00515                 gint currency;
00516                 gchar *amount;
00517                 gchar *vendor;
00518                 gchar *city;
00519                 gchar *attendees;
00520                 gchar *note;
00521 };
00522 
00523 struct ExpenseAppInfo {
00524 //      struct CategoryAppInfo category;
00525         struct ExpenseCustomCurrency currencies[4];
00526 };
00527 
00528 struct QofExp_s
00529 {
00530         QofInstance inst;
00531         Expense_t wrap;
00532         ExpenseDistance distance_unit;
00533         gchar *category;
00534         const gchar* print_string;
00535         GpeCurrency *currency;
00536         gdouble temp_amount;     
00539         gboolean reset_amount;   
00540 };
00541 
00542 static QofExp *
00543 expense_create (QofBook *book)
00544 {
00545         Expense_t *qe;
00546         QofExp *obj;
00547         QofCollection *coll;
00548         GList *all;
00549 
00550         obj = g_new0(QofExp, 1);
00551         qof_instance_init (&obj->inst, GPE_QOF_EXPENSES, book);
00552         coll = qof_book_get_collection (book, GPE_QOF_EXPENSES);
00553         all = qof_collection_get_data (coll);
00554         all = g_list_prepend (all, obj);
00555         qof_collection_set_data (coll, all);
00556         qe = &obj->wrap;
00557         {
00558                 glong nanosecs;
00559                 QofTime *qt;
00560                 QofDate *qd;
00561 
00562                 qt = qof_time_get_current ();
00563                 nanosecs = qof_time_get_nanosecs (qt);
00564                 qd = qof_date_from_qtime (qt);
00565                 if (!qof_date_to_struct_tm (qd, &qe->date, 
00566                         &nanosecs))
00567                         DEBUG (" failed to set initial date");
00568                 qof_date_free (qd);
00569                 qof_time_free (qt);
00570         }
00571         qe->amount = "0";
00572         /* 0 == AU$ so use an init value */
00573         qe->currency = -1;
00574         if(!gpe_currency_table) populate_currencies();
00575         qof_event_gen ((QofEntity*)obj, QOF_EVENT_CREATE, NULL);
00576         return obj;
00577 }
00578 
00579 static const gchar*
00580 qof_exp_paymentAsString(ExpensePayment payment)
00581 {
00582         const gchar *string;
00583 
00584         string = "Cash"; // following install-expenses convention.
00585         string = ExpensePaymentasString(payment);
00586         return string;
00587 }
00588 
00589 static const gchar*
00590 qof_exp_typeAsString(ExpenseType type)
00591 {
00592         const gchar* string;
00593 
00594         string = "Bus"; // following install-expenses convention.
00595         string = ExpenseTypeasString(type);
00596         return string;
00597 }
00598 
00599 static ExpensePayment
00600 qof_exp_paymentFromString(const gchar* payment_string)
00601 {
00602         return ExpensePaymentfromString(payment_string);
00603 }
00604 
00605 static ExpenseType
00606 qof_exp_typeFromString(const gchar* type_string)
00607 {
00608         return ExpenseTypefromString(type_string);
00609 }
00610 
00611 static QofTime *
00612 exp_getTime (QofExp * e)
00613 {
00614         Expense_t *qe;
00615         QofDate *qd;
00616         QofTime *qt;
00617 
00618         g_return_val_if_fail (e != NULL, NULL);
00619         qe = &e->wrap;
00620         qd = qof_date_from_struct_tm (&qe->date);
00621         qt = qof_date_to_qtime (qd);
00622         qof_date_free (qd);
00623         return qt;
00624 }
00625 
00626 static gchar*
00627 exp_getType (QofExp * e)
00628 {
00629         Expense_t *qe;
00630         gchar* string;
00631 
00632         g_return_val_if_fail (e != NULL, NULL);
00633         qe = &e->wrap;
00634         string = g_strdup(qof_exp_typeAsString(qe->type));
00635         return string;
00636 }
00637 
00638 static gchar*
00639 exp_getPayment (QofExp * e)
00640 {
00641         Expense_t *qe;
00642         gchar* string;
00643 
00644         g_return_val_if_fail (e != NULL, NULL);
00645         qe = &e->wrap;
00646         string = g_strdup(qof_exp_paymentAsString(qe->payment));
00647         return string;
00648 }
00649 
00650 static gint
00651 exp_getCurrency (QofExp * e)
00652 {
00653         Expense_t *qe;
00654 
00655         g_return_val_if_fail (e != NULL, -1);
00656         qe = &e->wrap;
00657         return qe->currency;
00658 }
00659 
00660 static QofNumeric
00661 exp_getAmount (QofExp * e)
00662 {
00663         Expense_t *qe;
00664         QofNumeric amount;
00665         gdouble pi_amount;
00666         gchar *numeric_char;
00667 
00668         amount = qof_numeric_zero ();
00669         g_return_val_if_fail (e != NULL, amount);
00670         qe = &e->wrap;
00671         if(qe->amount == 0) { return amount; }
00672         /* floating point as a string converts to gnc_numeric */
00673         pi_amount = strtod (qe->amount, NULL);
00674         if(e->currency)
00675         {
00676                 amount = qof_numeric_from_double (pi_amount, e->currency->fraction,
00677                         QOF_HOW_DENOM_EXACT | QOF_HOW_RND_ROUND);
00678         }
00679         else /* default: use the most common fraction in the Palm currency list. */
00680         {
00681         amount = qof_numeric_from_double (pi_amount, 100, 
00682                 QOF_HOW_DENOM_EXACT | QOF_HOW_RND_ROUND);
00683         }
00684         numeric_char = qof_numeric_to_string(amount);
00685         g_free(numeric_char);
00686 
00687         if (qof_numeric_check (amount) == QOF_ERROR_OK)
00688         {
00689                 return amount;
00690         }
00691         return qof_numeric_zero ();
00692 }
00693 
00694 static const gchar *
00695 exp_getVendor (QofExp * e)
00696 {
00697         Expense_t *qe;
00698 
00699         g_return_val_if_fail (e != NULL, NULL);
00700         qe = &e->wrap;
00701         return qe->vendor;
00702 }
00703 
00704 static const gchar *
00705 exp_getCity (QofExp * e)
00706 {
00707         Expense_t *qe;
00708 
00709         g_return_val_if_fail (e != NULL, NULL);
00710         qe = &e->wrap;
00711         return qe->city;
00712 }
00713 
00714 static const gchar *
00715 exp_getAttendees (QofExp * e)
00716 {
00717         Expense_t *qe;
00718 
00719         g_return_val_if_fail (e != NULL, NULL);
00720         qe = &e->wrap;
00721         return qe->attendees;
00722 }
00723 
00724 static const gchar *
00725 exp_getNote (QofExp * e)
00726 {
00727         Expense_t *qe;
00728 
00729         g_return_val_if_fail (e != NULL, NULL);
00730         qe = &e->wrap;
00731         return qe->note;
00732 }
00733 
00734 static const gchar*
00735 exp_getDistance(QofExp *e)
00736 {
00737         g_return_val_if_fail(e != NULL, NULL);
00738         return ExpenseDistanceasString(e->distance_unit);
00739 }
00740 
00741 static const gchar* 
00742 exp_getCategory(QofExp *e)
00743 {
00744         g_return_val_if_fail(e != NULL, NULL);
00745         return e->category;
00746 }
00747 
00748 static void
00749 exp_setTime (QofExp * e, QofTime *h)
00750 {
00751         Expense_t *qe;
00752         glong nanosecs;
00753         QofDate *qd;
00754 
00755         g_return_if_fail (e != NULL);
00756         qe = &e->wrap;
00757         if (!h)
00758                 return;
00759         nanosecs = qof_time_get_nanosecs (h);
00760         qd = qof_date_from_qtime (h);
00761         qof_date_to_struct_tm (qd, &qe->date, 
00762                 &nanosecs);
00763         qof_date_free (qd);
00764 }
00765 
00766 static void
00767 exp_setType (QofExp * e, const gchar *type_string)
00768 {
00769         Expense_t *qe;
00770 
00771         g_return_if_fail (e != NULL);
00772         qe = &e->wrap;
00773         qe->type = qof_exp_typeFromString(type_string);
00774 }
00775 
00776 static void
00777 exp_setPayment (QofExp * e, const gchar *payment_string)
00778 {
00779         Expense_t *qe;
00780 
00781         g_return_if_fail (e != NULL);
00782         qe = &e->wrap;
00783         qe->payment = qof_exp_paymentFromString(payment_string);
00784 }
00785 
00786 static void
00787 exp_combine_currency_with_amount(QofExp *e)
00788 {
00789         Expense_t *qe;
00790 
00791         g_return_if_fail (e != NULL);
00792         qe = &e->wrap;
00793         if(!e->currency || qe->currency < 0)
00794         {
00795                 /* the gint32 currency is actually to be set BEFORE the numeric amount. :-( */
00796                 e->currency = gpe_currency_lookup((QofInstance*)e, qe->currency);
00797         }
00798         if(!e->currency)
00799         {
00800                 PERR (" Unable to identify currency fraction."
00801                         " Using two decimal places.");
00802                 /* Amount is stored in the Palm as a string version
00803                 of a floating point number. */
00804                 qe->amount = g_strdup_printf ("%.2f", e->temp_amount);
00805                 return;
00806         }
00807         switch (e->currency->fraction)
00808         {
00809                 case 1 : 
00810                 {
00811                         qe->amount = g_strdup_printf ("%.0f", e->temp_amount);
00812                         break;
00813                 }
00814                 case 10 : 
00815                 {
00816                         qe->amount = g_strdup_printf ("%.1f", e->temp_amount);
00817                         break;
00818                 }
00819                 case 100 : 
00820                 {
00821                         qe->amount = g_strdup_printf ("%.2f", e->temp_amount);
00822                         break;
00823                 }
00824                 case 1000 : 
00825                 {
00826                         qe->amount = g_strdup_printf ("%.3f", e->temp_amount);
00827                         break;
00828                 }
00829                 default :
00830                 {
00831                         PERR (" Invalid currency fraction."
00832                                 " Using two decimal places as default.");
00833                         qe->amount = g_strdup_printf ("%.2f", e->temp_amount);
00834                 }
00835         }
00836 }
00837 
00838 static void
00839 exp_setCurrency (QofExp * e, gint data)
00840 {
00841         Expense_t *qe;
00842 
00843         g_return_if_fail (e != NULL);
00844         qe = &e->wrap;
00845         qe->currency = data;
00846         e->currency = gpe_currency_lookup((QofInstance*)e, data);
00847         if(e->reset_amount)
00848         {
00849                 exp_combine_currency_with_amount(e);
00850         }
00851         e->reset_amount = FALSE;
00852 }
00853 
00854 static void
00855 exp_setAmount (QofExp * e, QofNumeric h)
00856 {
00857         Expense_t *qe;
00858 
00859         g_return_if_fail (e != NULL);
00860         qe = &e->wrap;
00861         e->temp_amount = qof_numeric_to_double (h);
00862         e->reset_amount = TRUE;
00863         /* if an amount can ever be set without a currency_code,
00864                    this needs to be reviewed. */
00867         if(e->currency) exp_combine_currency_with_amount(e);
00868 }
00869 
00870 static void
00871 exp_setVendor (QofExp * e, gchar *h)
00872 {
00873         Expense_t *qe;
00874 
00875         g_return_if_fail (e != NULL);
00876         qe = &e->wrap;
00877         qe->vendor = g_strdup (qof_main_make_utf8(h));
00878 }
00879 
00880 static void
00881 exp_setCity (QofExp * e, gchar *h)
00882 {
00883         Expense_t *qe;
00884 
00885         g_return_if_fail (e != NULL);
00886         qe = &e->wrap;
00887         qe->city = g_strdup (qof_main_make_utf8(h));
00888 }
00889 
00890 static void
00891 exp_setAttendees (QofExp * e, gchar *h)
00892 {
00893         Expense_t *qe;
00894 
00895         g_return_if_fail (e != NULL);
00896         qe = &e->wrap;
00897         qe->attendees = g_strdup (qof_main_make_utf8(h));
00898 }
00899 
00900 static void
00901 exp_setNote (QofExp * e, gchar *h)
00902 {
00903         Expense_t *qe;
00904 
00905         g_return_if_fail (e != NULL);
00906         qe = &e->wrap;
00907         qe->note = g_strdup (qof_main_make_utf8(h));
00908 }
00909 
00910 static void
00911 exp_setDistance(QofExp *e, const gchar *distance_name)
00912 {
00913 
00914         g_return_if_fail(e);
00915         e->distance_unit = ExpenseDistancefromString(distance_name);
00916 }
00917 
00918 static void
00919 exp_setCategory(QofExp *e, gchar *n)
00920 {
00921         g_return_if_fail(e != NULL);
00922         e->category = g_strdup(qof_main_make_utf8(n));
00923 }
00924 
00925 static const gchar*
00926 expensePrintable (gpointer instance)
00927 {
00928         QofExp *obj;
00929         
00930         obj = (QofExp*)instance;
00931         if(!obj) return NULL;
00932         if(exp_getType(obj))
00933         {
00934         return g_strconcat(exp_getType(obj), " ",
00935                 exp_getVendor(obj), " ", exp_getCity(obj), NULL);
00936         }
00937         return NULL;
00938 }
00939 
00940 static QofObject expenses_object_def = {
00941       .interface_version = QOF_OBJECT_VERSION,
00942       .e_type =            GPE_QOF_EXPENSES,
00943       .type_label =        QOF_EXPENSES_DESC,
00944       .create =            ((gpointer)expense_create),
00945       .book_begin =        NULL,
00946       .book_end =          NULL,
00947       .is_dirty =          qof_collection_is_dirty,
00948       .mark_clean =        qof_collection_mark_clean,
00949       .foreach =           qof_collection_foreach,
00950           .printable =         expensePrintable,
00951       .version_cmp =       (gint (*)(gpointer, gpointer)) qof_instance_version_cmp,
00952 };
00953 
00954 gboolean
00955 ExpensesRegister (void)
00956 {
00957         static QofParam params[] = {
00958          { EXP_DATE,       QOF_TYPE_TIME,    (QofAccessFunc) exp_getTime,      (QofSetterFunc) exp_setTime, NULL },
00959          { EXP_TYPE,       QOF_TYPE_STRING,  (QofAccessFunc) exp_getType,      (QofSetterFunc) exp_setType, NULL },
00960          { EXP_PAYMENT,    QOF_TYPE_STRING,  (QofAccessFunc) exp_getPayment,   (QofSetterFunc) exp_setPayment, NULL },
00961          { EXP_CURRENCY,   QOF_TYPE_INT32,   (QofAccessFunc) exp_getCurrency,  (QofSetterFunc) exp_setCurrency, NULL },
00962          { EXP_AMOUNT,     QOF_TYPE_NUMERIC, (QofAccessFunc) exp_getAmount,    (QofSetterFunc) exp_setAmount, NULL },
00963          { EXP_VENDOR,     QOF_TYPE_STRING,  (QofAccessFunc) exp_getVendor,    (QofSetterFunc) exp_setVendor, NULL },
00964          { EXP_CITY,       QOF_TYPE_STRING,  (QofAccessFunc) exp_getCity,      (QofSetterFunc) exp_setCity, NULL },
00965          { EXP_ATTENDEES,  QOF_TYPE_STRING,  (QofAccessFunc) exp_getAttendees, (QofSetterFunc) exp_setAttendees, NULL },
00966          { EXP_NOTE,       QOF_TYPE_STRING,  (QofAccessFunc) exp_getNote,      (QofSetterFunc) exp_setNote, NULL },
00967          { EXP_DISTANCE,   QOF_TYPE_STRING,  (QofAccessFunc) exp_getDistance,  (QofSetterFunc) exp_setDistance, NULL },
00968          { EXP_CATEGORY,   QOF_TYPE_STRING,  (QofAccessFunc) exp_getCategory,  (QofSetterFunc) exp_setCategory, NULL },
00969          { EXP_KVP,        QOF_TYPE_KVP,     (QofAccessFunc) qof_instance_get_slots, NULL, NULL },
00970          { QOF_PARAM_BOOK, QOF_ID_BOOK,      (QofAccessFunc) qof_instance_get_book, NULL, NULL },
00971          { QOF_PARAM_GUID, QOF_TYPE_GUID,    (QofAccessFunc) qof_instance_get_guid, NULL, NULL },
00972          { NULL, NULL, NULL, NULL, NULL },
00973         };
00974 
00975         bindtextdomain (LIBRARY_GETTEXT_PACKAGE, LOCALE_DIR);
00976 
00977         qof_class_register (GPE_QOF_EXPENSES, NULL, params);
00978         if(!gpe_currency_table) populate_currencies();
00979 
00980         return qof_object_register (&expenses_object_def);
00981 }

Generated on Tue Dec 11 05:37:30 2007 for gpe-expenses by  doxygen 1.5.4