Bug 723644.

Make sure that gnc_search_invoice_on_id() returns the correct type
of object.
This commit is contained in:
Mike Evans 2014-02-09 16:21:12 +00:00
parent 5706c0083c
commit 40c236b9e0
3 changed files with 44 additions and 16 deletions

View File

@ -23,7 +23,16 @@
#include "gncIDSearch.h"
static void * search(QofBook * book, const gchar *id, void * object, QofIdType type);
typedef enum
{ UNDEFINED,
CUSTOMER,
VENDOR,
INVOICE,
BILL
}GncSearchType;
static void * search(QofBook * book, const gchar *id, void * object, GncSearchType type);
static QofLogModule log_module = G_LOG_DOMAIN;
/***********************************************************************
* Search the book for a Customer/Invoice/Bill with the same ID.
* If it exists return a valid object, if not then returns NULL.
@ -31,11 +40,13 @@ static void * search(QofBook * book, const gchar *id, void * object, QofIdType t
@param gchar ID of the Customer
@return GncCustomer * Pointer to the customer or NULL of there is no customer
**********************************************************************/
GncCustomer *
gnc_search_customer_on_id (QofBook * book, const gchar *id)
{
GncCustomer *customer = NULL;
QofIdType type = GNC_CUSTOMER_MODULE_NAME;
GncSearchType type = CUSTOMER;
customer = (GncCustomer*)search(book, id, customer, type);
return customer;
}
@ -44,7 +55,7 @@ GncInvoice *
gnc_search_invoice_on_id (QofBook * book, const gchar *id)
{
GncInvoice *invoice = NULL;
QofIdType type = GNC_INVOICE_MODULE_NAME;
GncSearchType type = INVOICE;
invoice = (GncInvoice*)search(book, id, invoice, type);
return invoice;
}
@ -54,7 +65,7 @@ GncInvoice *
gnc_search_bill_on_id (QofBook * book, const gchar *id)
{
GncInvoice *bill = NULL;
QofIdType type = GNC_INVOICE_MODULE_NAME;
GncSearchType type = BILL;
bill = (GncInvoice*)search(book, id, bill, type);
return bill;
}
@ -63,7 +74,7 @@ GncVendor *
gnc_search_vendor_on_id (QofBook * book, const gchar *id)
{
GncVendor *vendor = NULL;
QofIdType type = GNC_VENDOR_MODULE_NAME;
GncSearchType type = VENDOR;
vendor = (GncVendor*)search(book, id, vendor, type);
return vendor;
}
@ -73,33 +84,37 @@ gnc_search_vendor_on_id (QofBook * book, const gchar *id)
* Generic search called after setting up stuff
* DO NOT call directly but type tests should fail anyway
****************************************************************/
static void * search(QofBook * book, const gchar *id, void * object, QofIdType type)
static void * search(QofBook * book, const gchar *id, void * object, GncSearchType type)
{
void *c;
GList *result;
QofQuery *q;
gint len;
QofQueryPredData* string_pred_data;
PINFO("Type = %d", type);
g_return_val_if_fail (type, NULL);
g_return_val_if_fail (id, NULL);
g_return_val_if_fail (book, NULL);
// Build the query
q = qof_query_create_for (type);
q = qof_query_create ();
qof_query_set_book (q, book);
// Search only the id field
string_pred_data = qof_query_string_predicate (QOF_COMPARE_EQUAL, id, QOF_STRING_MATCH_NORMAL, FALSE);
if (strcmp(type, GNC_CUSTOMER_MODULE_NAME))
if (type == CUSTOMER)
{
qof_query_search_for(q,GNC_CUSTOMER_MODULE_NAME);
qof_query_add_term (q, qof_query_build_param_list("CUSTOMER_ID"), string_pred_data, QOF_QUERY_AND);
}
else if (strcmp(type, GNC_INVOICE_MODULE_NAME))
else if (type == INVOICE || type == BILL)
{
qof_query_search_for(q,GNC_INVOICE_MODULE_NAME);
qof_query_add_term (q, qof_query_build_param_list("INVOICE_ID"), string_pred_data, QOF_QUERY_AND);
}
else if (strcmp(type, GNC_VENDOR_MODULE_NAME))
else if (type == VENDOR)
{
qof_query_search_for(q,GNC_VENDOR_MODULE_NAME);
qof_query_add_term (q, qof_query_build_param_list("VENDOR_ID"), string_pred_data, QOF_QUERY_AND);
}
@ -116,18 +131,26 @@ static void * search(QofBook * book, const gchar *id, void * object, QofIdType t
while (result)
{
c = result->data;
if (strcmp(type, GNC_CUSTOMER_MODULE_NAME) && strcmp(id, gncCustomerGetID(c)) == 0)
if (type == CUSTOMER && strcmp(id, gncCustomerGetID(c)) == 0)
{
// correct id found
object = c;
break;
}
else if (strcmp(type, GNC_INVOICE_MODULE_NAME) && strcmp(id, gncInvoiceGetID(c)) == 0)
else if (type == INVOICE && strcmp(id, gncInvoiceGetID(c)) == 0
&& gncInvoiceGetType(c) == GNC_INVOICE_CUST_INVOICE)
{
object = c;
break;
}
else if (strcmp(type, GNC_VENDOR_MODULE_NAME) && strcmp(id, gncVendorGetID(c)) == 0)
else if (type == BILL && strcmp(id, gncInvoiceGetID(c)) == 0
&& gncInvoiceGetType(c) == GNC_INVOICE_VEND_INVOICE)
{
object = c;
break;
}
else if (type == VENDOR && strcmp(id, gncVendorGetID(c)) == 0)
{
object = c;
break;

View File

@ -354,7 +354,7 @@ void gnc_bi_import_gui_open_mode_cb (GtkWidget *widget, gpointer data)
/*****************************************************************
* Set whether we are importing a bi, invoice, Customer or Vendor
* Set whether we are importing a bill, invoice, Customer or Vendor
* ****************************************************************/
void gnc_import_gui_type_cb (GtkWidget *widget, gpointer data)
{

View File

@ -78,6 +78,7 @@
g_free (temp); \
}
static QofLogModule log_module = G_LOG_DOMAIN; //G_LOG_BUSINESS;
bi_import_result
gnc_bi_import_read_file (const gchar * filename, const gchar * parser_regexp,
@ -564,9 +565,12 @@ gnc_bi_import_create_bis (GtkListStore * store, QofBook * book,
invoice = gnc_search_bill_on_id (book, id);
else if (g_ascii_strcasecmp (type, "INVOICE") == 0)
invoice = gnc_search_invoice_on_id (book, id);
PINFO( "Existing %s ID: %s\n", type, gncInvoiceGetID(invoice));
if (!invoice)
// If the search is empty then there is no existing invoice so make a new one
if (invoice == NULL)
{
PINFO( "Creating a new : %s\n", type );
// new invoice
invoice = gncInvoiceCreate (book);
/* Protect against thrashing the DB and trying to write the invoice
@ -686,6 +690,7 @@ gnc_bi_import_create_bis (GtkListStore * store, QofBook * book,
gncEntrySetQuantity (entry, n);
acc = gnc_account_lookup_for_register (gnc_get_current_root_account (),
account);
if (g_ascii_strcasecmp (type, "BILL") == 0)
{
gncEntrySetBillAccount (entry, acc);