qof-expenses.c

Go to the documentation of this file.
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 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 2 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, write to the Free Software
00020  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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     {   /* Australia    0   100     AU$     AUD */
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     {   /* Austria  1   100     €     ATS */
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     {   /* Belgium      2   100     €     BEF */
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     {   /* Brazil   3   100     R$      BRL */
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     {   /* Canada   4   100     $CN     CAD */
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     {   /* Denmark      5   100     DKK     DKK */
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     {   /* Finland      6   100     €     FIM */
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     {   /* France       7   100     €     FRF */
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     {   /* Germany      8   100     €     DEM */
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     {   /* Hong Kong    9   100     HK$     HKD */
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     {   /* Iceland  10  100     ISK     ISK */
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     {   /* Ireland      11  100     €     IEP */
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     {   /* Italy    12  1       EUR     ITL */
00207         /* The Italian Lira had a fraction == 1 */
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     {   /* Japan        13  1   &#165;  ¥      JPY */
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     {   /* Luxembourg   14  100 &#136;  €     LUF */
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     {   /* Mexico       15  100     MXP     MXP */
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     {   /* Netherlands  16  100     €     ANG */
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     {   /* New Zealand  17  100     $NZ     NZD */
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     {   /* Norway       18  100     NOK     NOK */
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     {   /* Spain    19  100     €     ESP */
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     {   /* Sweden   20  100     SEK     SEK */
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     {   /* Switzerland  21  100     CHF     CHF */
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     {   /* United Kingdom 22    100     £      GBP */
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     {   /* United States  23    100     $US     USD */
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     {   /* India        24  100     Rs      INR */
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     {   /* Indonesia    25  1       Rp      IDR */
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     {   /* Korea    26  100     KRW     KRW  (South) */
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     {   /* Malaysia     27  100     RM      MYR */
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     {   /* P.R.C. // People's Rep. China 28 100 RMB     CNY */
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     {   /* Phillipines  29  100     P       PHP */
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     {   /* Singapore    30  100     $       SGD */
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     {   /* Thailand     31  100     BHT     THB */
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     {   /* Taiwan   32  100     NT$     TWD */
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     {   /* EU (Euro)    133 100     €     EUR */
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;             // 0.12
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     /* 0 == AU$ so use an init value */
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";           // following install-expenses convention.
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";            // following install-expenses convention.
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";             // following install-expenses convention.
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     /* floating point as a string converts to gnc_numeric */
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                        /* default: use the most common fraction in the Palm currency list. */
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         /* the gint32 currency is always set AFTER the numeric amount. :-( */
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         /* Amount is stored in the Palm as a string version
00767            of a floating point number. */
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     /* if an amount can ever be set without a currency_code,
00826        this needs to be reviewed. */
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     /* lookup currency_code in currency table. */
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     /* pack_Expense still uses the old prototype
00934        using len instead of pi_buf->used. */
00935     size = pack_Expense (qe, context->pi_buf->data, len);
00936     /* work around the old prototype */
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     /* There is never an entity at this stage */
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     /* There is never an entity at this stage */
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 

Generated on Tue Mar 6 00:08:09 2007 for pilot-qof by  doxygen 1.5.1