pilot-qof
0.2.3
|
00001 /*************************************************************************** 00002 * qof-invoice.c 00003 * 00004 * Sat 22 Nov 2008 23:02:03 GMT 00005 * Copyright 2008 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 <stdlib.h> 00023 #include <glib.h> 00024 #include <glib/gprintf.h> 00025 #include <qof.h> 00026 #include "qof-main.h" 00027 #include "qof-datebook.h" 00028 #ifdef HAVE_QOFEXPENSES 00029 #include <qof-expenses.h> 00030 //#define PILOT_LINK_QOF_EXPENSES GPE_QOF_EXPENSES 00031 #else 00032 #define qof_currency_lookup pq_currency_lookup 00033 #define QofCurrency PQCurrency 00034 #endif 00035 #include "pilot-expenses.h" 00036 #include "pilot-expenses-p.h" 00037 #include "qof-address.h" 00038 #include "qof-invoice.h" 00039 00040 #define QOF_INVOICE_DESC "Pilot-link QOF Invoice" 00041 00042 static QofLogModule log_module = INVOICE_MOD; 00043 00044 static GList * date_list = NULL; 00045 static GList * addr_list = NULL; 00046 static GList * exp_list = NULL; 00047 00048 typedef struct 00049 { 00050 QofInstance inst; 00051 gchar *category; 00052 const gchar *print_string; 00053 QofCurrency *currency; 00054 gdouble temp_amount; 00057 gboolean reset_amount; 00058 QofDate * inv_date, * pay_date; 00059 gchar * reference; 00060 gchar * note; 00061 gchar * description; 00062 gchar * city; 00063 gchar * vendor; 00066 QofNumeric amount; 00067 } QofInvoice; 00068 00069 static QofInvoice * 00070 invoice_create (QofBook * book) 00071 { 00072 QofInvoice *obj; 00073 QofCollection *coll; 00074 GList *all; 00075 00076 obj = g_new0 (QofInvoice, 1); 00077 qof_instance_init (&obj->inst, PILOT_INVOICE, book); 00078 coll = qof_book_get_collection (book, PILOT_INVOICE); 00079 all = qof_collection_get_data (coll); 00080 all = g_list_prepend (all, obj); 00081 qof_collection_set_data (coll, all); 00082 obj->amount = qof_numeric_zero(); 00084 obj->inv_date = qof_date_new_dmy(1,1,1970); 00085 obj->pay_date = qof_date_new_dmy(1,1,1970); 00086 obj->currency = g_new0(QofCurrency, 1); 00087 obj->currency->pq_code = -1; 00088 return obj; 00089 } 00090 00091 static QofTime* 00092 inv_getTime (QofInvoice * e) 00093 { 00094 QofTime *qt; 00095 00096 g_return_val_if_fail (e != NULL, NULL); 00097 ENTER (" "); 00101 qt = qof_date_to_qtime (e->inv_date); 00102 LEAVE (" "); 00103 return qt; 00104 } 00105 00106 static QofTime* 00107 pay_getTime (QofInvoice * e) 00108 { 00109 QofTime *qt; 00110 00111 g_return_val_if_fail (e != NULL, NULL); 00115 qt = qof_date_to_qtime (e->pay_date); 00116 return qt; 00117 } 00118 00119 static gint 00120 inv_getCurrency (QofInvoice * i) 00121 { 00122 QofCurrency * c; 00123 00124 g_return_val_if_fail (i != NULL, -1); 00125 if (!i->currency) 00126 return -1; 00127 c = i->currency; 00128 return c->pq_code; 00129 } 00130 00131 static const gchar * 00132 inv_getReference (QofInvoice * e) 00133 { 00134 g_return_val_if_fail (e != NULL, NULL); 00135 return e->reference; 00136 } 00137 00138 static const gchar * 00139 inv_getDescription (QofInvoice * e) 00140 { 00141 g_return_val_if_fail (e != NULL, NULL); 00142 return e->description; 00143 } 00144 00145 static QofNumeric 00146 inv_getAmount (QofInvoice * e) 00147 { 00148 QofNumeric amount; 00149 00150 amount = qof_numeric_zero (); 00151 g_return_val_if_fail (e != NULL, amount); 00152 if (qof_numeric_check (e->amount) == QOF_ERROR_OK) 00153 return e->amount; 00154 return qof_numeric_zero (); 00155 } 00156 00157 static const gchar * 00158 inv_getVendor (QofInvoice * e) 00159 { 00160 g_return_val_if_fail (e != NULL, NULL); 00161 return e->vendor; 00162 } 00163 00164 static const gchar * 00165 inv_getCity (QofInvoice * e) 00166 { 00167 g_return_val_if_fail (e != NULL, NULL); 00168 return e->city; 00169 } 00170 00171 static const gchar * 00172 inv_getNote (QofInvoice * e) 00173 { 00174 g_return_val_if_fail (e != NULL, NULL); 00175 return e->note; 00176 } 00177 00178 static const gchar * 00179 inv_getCategory (QofInvoice * e) 00180 { 00181 g_return_val_if_fail (e != NULL, NULL); 00182 return e->category; 00183 } 00184 00185 static void 00186 inv_setTime (QofInvoice * e, QofTime *h) 00187 { 00188 g_return_if_fail (e != NULL); 00189 e->inv_date = qof_date_from_qtime (h); 00190 } 00191 00192 static void 00193 pay_setTime (QofInvoice * e, QofTime *h) 00194 { 00195 g_return_if_fail (e != NULL); 00196 e->pay_date = qof_date_from_qtime (h); 00197 } 00198 00199 static void 00200 inv_setReference (QofInvoice * e, const gchar * type_string) 00201 { 00202 g_return_if_fail (e != NULL); 00203 e->reference = g_strdup (type_string); 00204 } 00205 00206 static void 00207 inv_setCurrency (QofInvoice * i, gint code) 00208 { 00209 g_return_if_fail (i != NULL); 00210 i->currency = qof_currency_lookup ((QofInstance*) i, code); 00211 } 00212 00213 static void 00214 inv_setDescription (QofInvoice * e, const gchar * desc_string) 00215 { 00216 g_return_if_fail (e != NULL); 00217 e->description = g_strdup (desc_string); 00218 } 00219 00220 static void 00221 inv_setAmount (QofInvoice * e, QofNumeric h) 00222 { 00223 g_return_if_fail (e != NULL); 00224 e->amount = h; 00225 e->temp_amount = qof_numeric_to_double (h); 00226 e->reset_amount = TRUE; 00227 /* if an amount can ever be set without a currency_code, 00228 this needs to be reviewed. */ 00231 /* if (e->currency) 00232 inv_combine_currency_with_amount (e);*/ 00233 } 00234 00235 static void 00236 inv_setVendor (QofInvoice * e, gchar * h) 00237 { 00238 g_return_if_fail (e != NULL); 00239 e->vendor = g_strdup (qof_util_make_utf8 (h)); 00240 } 00241 00242 static void 00243 inv_setCity (QofInvoice * e, gchar * h) 00244 { 00245 g_return_if_fail (e != NULL); 00246 e->city = g_strdup (qof_util_make_utf8 (h)); 00247 } 00248 00249 static void 00250 inv_setNote (QofInvoice * e, gchar * h) 00251 { 00252 g_return_if_fail (e != NULL); 00253 e->note = g_strdup (qof_util_make_utf8 (h)); 00254 } 00255 00256 static void 00257 inv_setCategory (QofInvoice * e, gchar * n) 00258 { 00259 g_return_if_fail (e != NULL); 00260 e->category = g_strdup (qof_util_make_utf8 (n)); 00261 } 00262 00263 static QofCollection * 00264 inv_getDates (QofInvoice * invoice) 00265 { 00266 QofCollection *entry_coll; 00267 GList *list; 00268 QofEntity *entry; 00269 00270 entry_coll = qof_collection_new(PILOT_LINK_QOF_DATEBOOK); 00271 for(list = date_list; list != NULL; list = list->next) 00272 { 00273 entry = QOF_ENTITY(list->data); 00274 qof_collection_add_entity(entry_coll, entry); 00275 } 00276 return entry_coll; 00277 } 00278 00279 static QofCollection * 00280 inv_getAddresses (QofInvoice * invoice) 00281 { 00282 QofCollection *entry_coll; 00283 GList *list; 00284 QofEntity *entry; 00285 00286 entry_coll = qof_collection_new(PILOT_LINK_QOF_ADDRESS); 00287 for(list = addr_list; list != NULL; list = list->next) 00288 { 00289 entry = QOF_ENTITY(list->data); 00290 qof_collection_add_entity(entry_coll, entry); 00291 } 00292 return entry_coll; 00293 } 00294 00295 static QofCollection * 00296 inv_getExpenses (QofInvoice * invoice) 00297 { 00298 QofCollection *entry_coll; 00299 GList *list; 00300 QofEntity *entry; 00301 00302 entry_coll = qof_collection_new(PILOT_LINK_QOF_EXPENSES); 00303 for(list = exp_list; list != NULL; list = list->next) 00304 { 00305 entry = QOF_ENTITY(list->data); 00306 qof_collection_add_entity(entry_coll, entry); 00307 } 00308 return entry_coll; 00309 } 00310 00311 static void 00312 inv_add_date_cb (QofEntity *ent, gpointer user_data) 00313 { 00314 QofInvoice *invoice; 00315 00316 invoice = (QofInvoice*)user_data; 00317 if(!invoice || !ent) { return; } 00318 date_list = g_list_append (date_list, ent); 00319 } 00320 00321 static void 00322 inv_add_addr_cb (QofEntity *ent, gpointer user_data) 00323 { 00324 QofInvoice *invoice; 00325 00326 invoice = (QofInvoice*)user_data; 00327 if(!invoice || !ent) { return; } 00328 addr_list = g_list_append (addr_list, ent); 00329 } 00330 00331 static void 00332 inv_add_exp_cb (QofEntity *ent, gpointer user_data) 00333 { 00334 QofInvoice *invoice; 00335 00336 invoice = (QofInvoice*)user_data; 00337 if(!invoice || !ent) { return; } 00338 exp_list = g_list_append (exp_list, ent); 00339 } 00340 00341 static void 00342 inv_setDates (QofInvoice *invoice, QofCollection *entry_coll) 00343 { 00344 if(!entry_coll) { return; } 00345 if(0 == safe_strcmp(qof_collection_get_type(entry_coll), PILOT_LINK_QOF_DATEBOOK)) 00346 { 00347 qof_collection_foreach(entry_coll, inv_add_date_cb, invoice); 00348 } 00349 } 00350 00351 static void 00352 inv_setAddresses (QofInvoice *invoice, QofCollection *entry_coll) 00353 { 00354 if(!entry_coll) { return; } 00355 if(0 == safe_strcmp(qof_collection_get_type(entry_coll), PILOT_LINK_QOF_ADDRESS)) 00356 { 00357 qof_collection_foreach(entry_coll, inv_add_addr_cb, invoice); 00358 } 00359 } 00360 00361 static void 00362 inv_setExpenses (QofInvoice *invoice, QofCollection *entry_coll) 00363 { 00364 if(!entry_coll) { return; } 00365 if(0 == safe_strcmp(qof_collection_get_type(entry_coll), PILOT_LINK_QOF_EXPENSES)) 00366 { 00367 qof_collection_foreach(entry_coll, inv_add_exp_cb, invoice); 00368 } 00369 } 00370 00371 static const gchar * 00372 invoicePrintable (gpointer instance) 00373 { 00374 QofInvoice *obj; 00375 00376 obj = (QofInvoice *) instance; 00377 if (!obj) 00378 return NULL; 00379 if (inv_getDescription (obj)) 00380 return g_strconcat (inv_getDescription (obj), " ", 00381 inv_getVendor (obj), " ", inv_getCity (obj), NULL); 00382 return NULL; 00383 } 00384 00385 static QofObject expenses_object_def = { 00386 interface_version : QOF_OBJECT_VERSION, 00387 e_type : PILOT_INVOICE, 00388 type_label : QOF_INVOICE_DESC, 00389 create : (gpointer) invoice_create, 00390 book_begin : NULL, 00391 book_end : NULL, 00392 is_dirty : qof_collection_is_dirty, 00393 mark_clean : qof_collection_mark_clean, 00394 foreach : qof_collection_foreach, 00395 printable : invoicePrintable, 00396 version_cmp : (gint (*)(gpointer, gpointer)) qof_instance_version_cmp, 00397 }; 00398 00399 gboolean 00400 InvoiceRegister (void) 00401 { 00402 static QofParam params[] = { 00403 {INV_DATE, QOF_TYPE_TIME, (QofAccessFunc) inv_getTime, 00404 (QofSetterFunc) inv_setTime, NULL}, 00405 {PAY_DATE, QOF_TYPE_TIME, (QofAccessFunc) pay_getTime, 00406 (QofSetterFunc) pay_setTime, NULL}, 00407 {INV_REFERENCE, QOF_TYPE_STRING, (QofAccessFunc) inv_getReference, 00408 (QofSetterFunc) inv_setReference, NULL}, 00409 {INV_CURRENCY, QOF_TYPE_INT32, (QofAccessFunc) inv_getCurrency, 00410 (QofSetterFunc) inv_setCurrency, NULL}, 00411 {INV_AMOUNT, QOF_TYPE_NUMERIC, (QofAccessFunc) inv_getAmount, 00412 (QofSetterFunc) inv_setAmount, NULL}, 00413 {INV_VENDOR, QOF_TYPE_STRING, (QofAccessFunc) inv_getVendor, 00414 (QofSetterFunc) inv_setVendor, NULL}, 00415 {INV_CITY, QOF_TYPE_STRING, (QofAccessFunc) inv_getCity, 00416 (QofSetterFunc) inv_setCity, NULL}, 00417 {INV_DESCRIPTION, QOF_TYPE_STRING, (QofAccessFunc) inv_getDescription, 00418 (QofSetterFunc) inv_setDescription, NULL}, 00419 {INV_NOTE, QOF_TYPE_STRING, (QofAccessFunc) inv_getNote, 00420 (QofSetterFunc) inv_setNote, NULL}, 00421 {INV_CATEGORY, QOF_TYPE_STRING, (QofAccessFunc) inv_getCategory, 00422 (QofSetterFunc) inv_setCategory, NULL}, 00423 {INV_DATES, QOF_TYPE_COLLECT, (QofAccessFunc)inv_getDates, 00424 (QofSetterFunc)inv_setDates, NULL}, 00425 {INV_ADDR, QOF_TYPE_COLLECT, (QofAccessFunc)inv_getAddresses, 00426 (QofSetterFunc)inv_setAddresses, NULL}, 00427 {INV_EXPENSE, QOF_TYPE_COLLECT, (QofAccessFunc)inv_getExpenses, 00428 (QofSetterFunc)inv_setExpenses, NULL}, 00429 {INV_KVP, QOF_TYPE_KVP, (QofAccessFunc) qof_instance_get_slots, 00430 NULL, NULL}, 00431 {QOF_PARAM_BOOK, QOF_ID_BOOK, 00432 (QofAccessFunc) qof_instance_get_book, NULL, NULL}, 00433 {QOF_PARAM_GUID, QOF_TYPE_GUID, 00434 (QofAccessFunc) qof_instance_get_guid, NULL, NULL}, 00435 {NULL, NULL, NULL, NULL, NULL}, 00436 }; 00437 00438 qof_class_register (PILOT_INVOICE, NULL, params); 00439 00440 return qof_object_register (&expenses_object_def); 00441 }