dialog-employee.c

00001 /*
00002  * dialog-employee.c -- Dialog for Employee entry
00003  * Copyright (C) 2001 Derek Atkins
00004  * Author: Derek Atkins <warlord@MIT.EDU>
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License as
00008  * published by the Free Software Foundation; either version 2 of
00009  * the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, contact:
00018  *
00019  * Free Software Foundation           Voice:  +1-617-542-5942
00020  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
00021  * Boston, MA  02110-1301,  USA       gnu@gnu.org
00022  */
00023 
00024 #include "config.h"
00025 
00026 #include <gtk/gtk.h>
00027 #include <glib/gi18n.h>
00028 
00029 #include "dialog-utils.h"
00030 #include "gnc-amount-edit.h"
00031 #include "gnc-currency-edit.h"
00032 #include "gnc-component-manager.h"
00033 #include "gnc-ui.h"
00034 #include "gnc-gui-query.h"
00035 #include "gnc-ui-util.h"
00036 #include "qof.h"
00037 #include "dialog-search.h"
00038 #include "search-param.h"
00039 #include "gnc-account-sel.h"
00040 
00041 #include "gncAddress.h"
00042 #include "gncEmployee.h"
00043 #include "gncEmployeeP.h"
00044 #include "gncOwner.h"
00045 
00046 #include "dialog-employee.h"
00047 #include "dialog-invoice.h"
00048 #include "dialog-payment.h"
00049 
00050 #define DIALOG_NEW_EMPLOYEE_CM_CLASS "dialog-new-employee"
00051 #define DIALOG_EDIT_EMPLOYEE_CM_CLASS "dialog-edit-employee"
00052 
00053 #define GCONF_SECTION_SEARCH "dialogs/business/employee_search"
00054 
00055 void gnc_employee_window_ok_cb (GtkWidget *widget, gpointer data);
00056 void gnc_employee_window_cancel_cb (GtkWidget *widget, gpointer data);
00057 void gnc_employee_window_help_cb (GtkWidget *widget, gpointer data);
00058 void gnc_employee_window_destroy_cb (GtkWidget *widget, gpointer data);
00059 void gnc_employee_name_changed_cb (GtkWidget *widget, gpointer data);
00060 void gnc_employee_ccard_acct_toggled_cb (GtkToggleButton *button, gpointer data);
00061 
00062 typedef enum
00063 {
00064   NEW_EMPLOYEE,
00065   EDIT_EMPLOYEE
00066 } EmployeeDialogType;
00067 
00068 struct _employee_select_window {
00069   GNCBook *     book;
00070   QueryNew *    q;
00071 };
00072 
00073 struct _employee_window {
00074   GtkWidget *   dialog;
00075 
00076   GtkWidget *   id_entry;
00077   GtkWidget *   username_entry;
00078 
00079   GtkWidget *   name_entry;
00080   GtkWidget *   addr1_entry;
00081   GtkWidget *   addr2_entry;
00082   GtkWidget *   addr3_entry;
00083   GtkWidget *   addr4_entry;
00084   GtkWidget *   phone_entry;
00085   GtkWidget *   fax_entry;
00086   GtkWidget *   email_entry;
00087 
00088   GtkWidget *   language_entry;
00089 
00090   GtkWidget *   workday_amount;
00091   GtkWidget *   rate_amount;
00092   GtkWidget *   currency_edit;
00093   GtkWidget *   ccard_acct_check;
00094   GtkWidget *   ccard_acct_sel;
00095 
00096   GtkWidget *   active_check;
00097 
00098   /* ACL? */
00099 
00100   EmployeeDialogType    dialog_type;
00101   GUID          employee_guid;
00102   gint          component_id;
00103   GNCBook *     book;
00104   GncEmployee * created_employee;
00105 };
00106 
00107 static GncEmployee *
00108 ew_get_employee (EmployeeWindow *ew)
00109 {
00110   if (!ew)
00111     return NULL;
00112 
00113   return gncEmployeeLookup (ew->book, &ew->employee_guid);
00114 }
00115 
00116 static void gnc_ui_to_employee (EmployeeWindow *ew, GncEmployee *employee)
00117 {
00118   GncAddress *addr;
00119 
00120   addr = gncEmployeeGetAddr (employee);
00121 
00122   gnc_suspend_gui_refresh ();
00123 
00124   gncEmployeeBeginEdit (employee);
00125 
00126   gncEmployeeSetID (employee, gtk_editable_get_chars
00127                     (GTK_EDITABLE (ew->id_entry), 0, -1));
00128   gncEmployeeSetUsername (employee, gtk_editable_get_chars
00129                       (GTK_EDITABLE (ew->username_entry), 0, -1));
00130 
00131   gncAddressSetName (addr, gtk_editable_get_chars
00132                      (GTK_EDITABLE (ew->name_entry), 0, -1));
00133   gncAddressSetAddr1 (addr, gtk_editable_get_chars
00134                       (GTK_EDITABLE (ew->addr1_entry), 0, -1));
00135   gncAddressSetAddr2 (addr, gtk_editable_get_chars
00136                       (GTK_EDITABLE (ew->addr2_entry), 0, -1));
00137   gncAddressSetAddr3 (addr, gtk_editable_get_chars
00138                       (GTK_EDITABLE (ew->addr3_entry), 0, -1));
00139   gncAddressSetAddr4 (addr, gtk_editable_get_chars
00140                       (GTK_EDITABLE (ew->addr4_entry), 0, -1));
00141   gncAddressSetPhone (addr, gtk_editable_get_chars
00142                       (GTK_EDITABLE (ew->phone_entry), 0, -1));
00143   gncAddressSetFax (addr, gtk_editable_get_chars
00144                     (GTK_EDITABLE (ew->fax_entry), 0, -1));
00145   gncAddressSetEmail (addr, gtk_editable_get_chars
00146                       (GTK_EDITABLE (ew->email_entry), 0, -1));
00147 
00148   gncEmployeeSetActive (employee, gtk_toggle_button_get_active
00149                         (GTK_TOGGLE_BUTTON (ew->active_check)));
00150   gncEmployeeSetLanguage (employee, gtk_editable_get_chars
00151                        (GTK_EDITABLE (ew->language_entry), 0, -1));
00152 
00153   /* Parse and set the workday and rate amounts */
00154   gncEmployeeSetWorkday (employee, gnc_amount_edit_get_amount
00155                        (GNC_AMOUNT_EDIT (ew->workday_amount)));
00156   gncEmployeeSetRate (employee, gnc_amount_edit_get_amount
00157                        (GNC_AMOUNT_EDIT (ew->rate_amount)));
00158   gncEmployeeSetCurrency (employee, gnc_currency_edit_get_currency
00159                           (GNC_CURRENCY_EDIT (ew->currency_edit)));
00160 
00161   /* Fill in the CCard Acct */
00162   gncEmployeeSetCCard (employee,
00163                        (gtk_toggle_button_get_active
00164                         (GTK_TOGGLE_BUTTON (ew->ccard_acct_check)) ?
00165                         gnc_account_sel_get_account
00166                         (GNC_ACCOUNT_SEL (ew->ccard_acct_sel)) : NULL));
00167 
00168   gncEmployeeCommitEdit (employee);
00169   gnc_resume_gui_refresh ();
00170 }
00171 
00172 #if 0
00173 static gboolean check_edit_amount (GtkWidget *dialog, GtkWidget *amount,
00174                                    gnc_numeric *min, gnc_numeric *max,
00175                                    const char * error_message)
00176 {
00177   if (!gnc_amount_edit_evaluate (GNC_AMOUNT_EDIT (amount))) {
00178     if (error_message)
00179       gnc_error_dialog (dialog, error_message);
00180     return TRUE;
00181   }
00182   /* We've got a valid-looking number; check mix/max */
00183   if (min || max) {
00184     gnc_numeric val = gnc_amount_edit_get_amount (GNC_AMOUNT_EDIT (amount));
00185     if ((min && gnc_numeric_compare (*min, val) > 0) ||
00186         (max && gnc_numeric_compare (val, *max) > 0)) {
00187       if (error_message)
00188         gnc_error_dialog (dialog, error_message);
00189       return TRUE;
00190     }
00191   }
00192   return FALSE;
00193 }
00194 #endif
00195 
00196 static gboolean check_entry_nonempty (GtkWidget *dialog, GtkWidget *entry, 
00197                                       const char * error_message)
00198 {
00199   const char *res = gtk_entry_get_text (GTK_ENTRY (entry));
00200   if (safe_strcmp (res, "") == 0) {
00201     if (error_message)
00202       gnc_error_dialog (dialog, "%s", error_message);
00203     return TRUE;
00204   }
00205   return FALSE;
00206 }
00207 
00208 void
00209 gnc_employee_window_ok_cb (GtkWidget *widget, gpointer data)
00210 {
00211   EmployeeWindow *ew = data;
00212   gchar *string;
00213 
00214   /* Check for valid username */
00215   if (check_entry_nonempty (ew->dialog, ew->username_entry,
00216                    _("You must enter a username.")))
00217     return;
00218 
00219   /* Check for valid username */
00220   if (check_entry_nonempty (ew->dialog, ew->name_entry,
00221                    _("You must enter the employee's name.")))
00222     return;
00223 
00224   /* Make sure we have an address */
00225   if (check_entry_nonempty (ew->dialog, ew->addr1_entry, NULL) &&
00226       check_entry_nonempty (ew->dialog, ew->addr2_entry, NULL) &&
00227       check_entry_nonempty (ew->dialog, ew->addr3_entry, NULL) &&
00228       check_entry_nonempty (ew->dialog, ew->addr4_entry, NULL)) {
00229     const char *msg = _("You must enter an address.");
00230     gnc_error_dialog (ew->dialog, "%s", msg);
00231     return;
00232   }
00233 
00234   /* Set the employee id if one has not been chosen */
00235   if (safe_strcmp (gtk_entry_get_text (GTK_ENTRY (ew->id_entry)), "") == 0) {
00236     string = g_strdup_printf ("%.6" G_GINT64_FORMAT,
00237                               gncEmployeeNextID (ew->book));
00238     gtk_entry_set_text (GTK_ENTRY (ew->id_entry), string);
00239     g_free(string);
00240   }
00241 
00242   /* Now save it off */
00243   {
00244     GncEmployee *employee = ew_get_employee (ew);
00245     if (employee) {
00246       gnc_ui_to_employee (ew, employee);
00247     }
00248     ew->created_employee = employee;
00249     ew->employee_guid = *xaccGUIDNULL ();
00250   }
00251 
00252   gnc_close_gui_component (ew->component_id);
00253 }
00254 
00255 void
00256 gnc_employee_window_cancel_cb (GtkWidget *widget, gpointer data)
00257 {
00258   EmployeeWindow *ew = data;
00259 
00260   gnc_close_gui_component (ew->component_id);
00261 }
00262 
00263 void
00264 gnc_employee_window_help_cb (GtkWidget *widget, gpointer data)
00265 {
00266   gnc_gnome_help(HF_HELP, HL_USAGE);
00267 }
00268 
00269 void
00270 gnc_employee_window_destroy_cb (GtkWidget *widget, gpointer data)
00271 {
00272   EmployeeWindow *ew = data;
00273   GncEmployee *employee = ew_get_employee (ew);
00274 
00275   gnc_suspend_gui_refresh ();
00276 
00277   if (ew->dialog_type == NEW_EMPLOYEE && employee != NULL) {
00278     gncEmployeeBeginEdit (employee);
00279     gncEmployeeDestroy (employee);
00280     ew->employee_guid = *xaccGUIDNULL ();
00281   }
00282 
00283   gnc_unregister_gui_component (ew->component_id);
00284   gnc_resume_gui_refresh ();
00285 
00286   g_free (ew);
00287 }
00288 
00289 void
00290 gnc_employee_name_changed_cb (GtkWidget *widget, gpointer data)
00291 {
00292   EmployeeWindow *ew = data;
00293   char *fullname, *title;
00294   const char *name, *id;
00295 
00296   if (!ew)
00297     return;
00298 
00299   name = gtk_entry_get_text (GTK_ENTRY (ew->name_entry));
00300   if (!name || *name == '\0')
00301     name = _("<No name>");
00302 
00303   id = gtk_entry_get_text (GTK_ENTRY (ew->id_entry));
00304 
00305   fullname = g_strconcat (name, " (", id, ")", (char *)NULL);
00306 
00307   if (ew->dialog_type == EDIT_EMPLOYEE)
00308     title = g_strconcat (_("Edit Employee"), " - ", fullname, (char *)NULL);
00309   else
00310     title = g_strconcat (_("New Employee"), " - ", fullname, (char *)NULL);
00311 
00312   gtk_window_set_title (GTK_WINDOW (ew->dialog), title);
00313 
00314   g_free (fullname);
00315   g_free (title);
00316 }
00317 
00318 void
00319 gnc_employee_ccard_acct_toggled_cb (GtkToggleButton *button, gpointer data)
00320 {
00321   EmployeeWindow *ew = data;
00322 
00323   if (!ew)
00324     return;
00325 
00326   if (gtk_toggle_button_get_active (button)) {
00327     gtk_widget_set_sensitive (ew->ccard_acct_sel, TRUE);
00328     gtk_widget_show (ew->ccard_acct_sel);
00329   } else {
00330     gtk_widget_set_sensitive (ew->ccard_acct_sel, TRUE);
00331     gtk_widget_hide (ew->ccard_acct_sel);
00332   }
00333 }
00334 
00335 static void
00336 gnc_employee_window_close_handler (gpointer user_data)
00337 {
00338   EmployeeWindow *ew = user_data;
00339 
00340   gtk_widget_destroy (ew->dialog);
00341 }
00342 
00343 static void
00344 gnc_employee_window_refresh_handler (GHashTable *changes, gpointer user_data)
00345 {
00346   EmployeeWindow *ew = user_data;
00347   const EventInfo *info;
00348   GncEmployee *employee = ew_get_employee (ew);
00349 
00350   /* If there isn't a employee behind us, close down */
00351   if (!employee) {
00352     gnc_close_gui_component (ew->component_id);
00353     return;
00354   }
00355 
00356   /* Next, close if this is a destroy event */
00357   if (changes) {
00358     info = gnc_gui_get_entity_events (changes, &ew->employee_guid);
00359     if (info && (info->event_mask & QOF_EVENT_DESTROY)) {
00360       gnc_close_gui_component (ew->component_id);
00361       return;
00362     }
00363   }
00364 }
00365 
00366 static gboolean
00367 find_handler (gpointer find_data, gpointer user_data)
00368 {
00369   const GUID *employee_guid = find_data;
00370   EmployeeWindow *ew = user_data;
00371 
00372   return(ew && guid_equal(&ew->employee_guid, employee_guid));
00373 }
00374 
00375 static EmployeeWindow *
00376 gnc_employee_new_window (GNCBook *bookp,
00377                          GncEmployee *employee)
00378 {
00379   EmployeeWindow *ew;
00380   GladeXML *xml;
00381   GtkWidget *hbox, *edit;
00382   gnc_commodity *currency;
00383   GNCPrintAmountInfo print_info;
00384   GList *acct_types;
00385   Account *ccard_acct;
00386 
00387   /*
00388    * Find an existing window for this employee.  If found, bring it to
00389    * the front.
00390    */
00391   if (employee) {
00392     GUID employee_guid;
00393     
00394     employee_guid = *gncEmployeeGetGUID (employee);
00395     ew = gnc_find_first_gui_component (DIALOG_EDIT_EMPLOYEE_CM_CLASS,
00396                                        find_handler, &employee_guid);
00397     if (ew) {
00398       gtk_window_present (GTK_WINDOW(ew->dialog));
00399       return(ew);
00400     }
00401   }
00402   
00403   /* Find the default currency */
00404   if (employee)
00405     currency = gncEmployeeGetCurrency (employee);
00406   else
00407     currency = gnc_default_currency ();
00408 
00409   /*
00410    * No existing employee window found.  Build a new one.
00411    */
00412   ew = g_new0 (EmployeeWindow, 1);
00413 
00414   ew->book = bookp;
00415 
00416   /* Find the dialog */
00417   xml = gnc_glade_xml_new ("employee.glade", "Employee Dialog");
00418   ew->dialog = glade_xml_get_widget (xml, "Employee Dialog");
00419 
00420   g_object_set_data (G_OBJECT (ew->dialog), "dialog_info", ew);
00421 
00422   /* Get entry points */
00423   ew->id_entry = glade_xml_get_widget (xml, "id_entry");
00424   ew->username_entry = glade_xml_get_widget (xml, "username_entry");
00425 
00426   ew->name_entry = glade_xml_get_widget (xml, "name_entry");
00427   ew->addr1_entry = glade_xml_get_widget (xml, "addr1_entry");
00428   ew->addr2_entry = glade_xml_get_widget (xml, "addr2_entry");
00429   ew->addr3_entry = glade_xml_get_widget (xml, "addr3_entry");
00430   ew->addr4_entry = glade_xml_get_widget (xml, "addr4_entry");
00431   ew->phone_entry = glade_xml_get_widget (xml, "phone_entry");
00432   ew->fax_entry = glade_xml_get_widget (xml, "fax_entry");
00433   ew->email_entry = glade_xml_get_widget (xml, "email_entry");
00434 
00435   ew->language_entry = glade_xml_get_widget (xml, "language_entry");
00436   ew->active_check = glade_xml_get_widget (xml, "active_check");
00437 
00438   /* Currency */
00439   edit = gnc_currency_edit_new();
00440   gnc_currency_edit_set_currency (GNC_CURRENCY_EDIT(edit), currency);
00441   ew->currency_edit = edit;
00442 
00443   hbox = glade_xml_get_widget (xml, "currency_box");
00444   gtk_box_pack_start (GTK_BOX (hbox), edit, TRUE, TRUE, 0);
00445 
00446   /* WORKDAY: Value */
00447   edit = gnc_amount_edit_new();
00448   gnc_amount_edit_set_evaluate_on_enter (GNC_AMOUNT_EDIT (edit), TRUE);
00449   print_info = gnc_integral_print_info ();
00450   print_info.max_decimal_places = 5;
00451   gnc_amount_edit_set_print_info (GNC_AMOUNT_EDIT (edit), print_info);
00452   gnc_amount_edit_set_fraction (GNC_AMOUNT_EDIT (edit), 100000);
00453   ew->workday_amount = edit;
00454   gtk_widget_show (edit);
00455 
00456   hbox = glade_xml_get_widget (xml, "hours_hbox");
00457   gtk_box_pack_start (GTK_BOX (hbox), edit, TRUE, TRUE, 0);
00458 
00459   /* RATE: Monetary Value */
00460   edit = gnc_amount_edit_new();
00461   print_info = gnc_commodity_print_info (currency, FALSE);
00462   gnc_amount_edit_set_evaluate_on_enter (GNC_AMOUNT_EDIT (edit), TRUE);
00463   gnc_amount_edit_set_print_info (GNC_AMOUNT_EDIT (edit), print_info);
00464   gnc_amount_edit_set_fraction (GNC_AMOUNT_EDIT (edit),
00465                                 gnc_commodity_get_fraction (currency));
00466   ew->rate_amount = edit;
00467   gtk_widget_show (edit);
00468 
00469   hbox = glade_xml_get_widget (xml, "rate_hbox");
00470   gtk_box_pack_start (GTK_BOX (hbox), edit, TRUE, TRUE, 0);
00471 
00472   /* CCard Account Selection */
00473   ew->ccard_acct_check = glade_xml_get_widget (xml, "ccard_check");
00474 
00475   edit = gnc_account_sel_new();
00476   acct_types = g_list_prepend(NULL, (gpointer)ACCT_TYPE_CREDIT);
00477   gnc_account_sel_set_acct_filters (GNC_ACCOUNT_SEL(edit), acct_types);
00478   g_list_free (acct_types);
00479 
00480   ew->ccard_acct_sel = edit;
00481   gtk_widget_show (edit);
00482 
00483   hbox = glade_xml_get_widget (xml, "ccard_acct_hbox");
00484   gtk_box_pack_start (GTK_BOX (hbox), edit, TRUE, TRUE, 0);
00485 
00486   /* Setup signals */
00487   glade_xml_signal_autoconnect_full( xml,
00488                                      gnc_glade_autoconnect_full_func,
00489                                      ew);
00490 
00491   /* Setup initial values */
00492   if (employee != NULL) {
00493     GncAddress *addr;
00494 
00495     ew->dialog_type = EDIT_EMPLOYEE;
00496     ew->employee_guid = *gncEmployeeGetGUID (employee);
00497 
00498     addr = gncEmployeeGetAddr (employee);
00499 
00500     gtk_entry_set_text (GTK_ENTRY (ew->id_entry), gncEmployeeGetID (employee));
00501     gtk_entry_set_text (GTK_ENTRY (ew->username_entry), gncEmployeeGetUsername (employee));
00502 
00503     /* Setup Address */
00504     gtk_entry_set_text (GTK_ENTRY (ew->name_entry), gncAddressGetName (addr));
00505     gtk_entry_set_text (GTK_ENTRY (ew->addr1_entry), gncAddressGetAddr1 (addr));
00506     gtk_entry_set_text (GTK_ENTRY (ew->addr2_entry), gncAddressGetAddr2 (addr));
00507     gtk_entry_set_text (GTK_ENTRY (ew->addr3_entry), gncAddressGetAddr3 (addr));
00508     gtk_entry_set_text (GTK_ENTRY (ew->addr4_entry), gncAddressGetAddr4 (addr));
00509     gtk_entry_set_text (GTK_ENTRY (ew->phone_entry), gncAddressGetPhone (addr));
00510     gtk_entry_set_text (GTK_ENTRY (ew->fax_entry), gncAddressGetFax (addr));
00511     gtk_entry_set_text (GTK_ENTRY (ew->email_entry), gncAddressGetEmail (addr));
00512 
00513     gtk_entry_set_text (GTK_ENTRY (ew->language_entry),
00514                         gncEmployeeGetLanguage (employee));
00515 
00516     /* Set toggle buttons */
00517     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ew->active_check),
00518                                 gncEmployeeGetActive (employee));
00519 
00520     ew->component_id =
00521       gnc_register_gui_component (DIALOG_EDIT_EMPLOYEE_CM_CLASS,
00522                                   gnc_employee_window_refresh_handler,
00523                                   gnc_employee_window_close_handler,
00524                                   ew);
00525   } else {
00526     employee = gncEmployeeCreate (bookp);
00527     ew->employee_guid = *gncEmployeeGetGUID (employee);
00528 
00529     ew->dialog_type = NEW_EMPLOYEE;
00530     ew->component_id =
00531       gnc_register_gui_component (DIALOG_NEW_EMPLOYEE_CM_CLASS,
00532                                   gnc_employee_window_refresh_handler,
00533                                   gnc_employee_window_close_handler,
00534                                   ew);
00535   }
00536 
00537 
00538   /* I know that employee exists here -- either passed in or just created */
00539   /* Set the workday and rate values */
00540   gnc_amount_edit_set_amount (GNC_AMOUNT_EDIT (ew->workday_amount),
00541                               gncEmployeeGetWorkday (employee));
00542   gnc_amount_edit_set_amount (GNC_AMOUNT_EDIT (ew->rate_amount),
00543                               gncEmployeeGetRate (employee));
00544 
00545 
00546   ccard_acct = gncEmployeeGetCCard (employee);
00547   if (ccard_acct == NULL) {
00548     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ew->ccard_acct_check), FALSE);
00549     gtk_widget_set_sensitive (ew->ccard_acct_sel, FALSE);
00550   } else {
00551     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ew->ccard_acct_check), TRUE);
00552     gnc_account_sel_set_account (GNC_ACCOUNT_SEL (ew->ccard_acct_sel), ccard_acct);
00553   }
00554 
00555   /* XXX: Set the ACL */
00556 
00557   gnc_gui_component_watch_entity_type (ew->component_id,
00558                                        GNC_EMPLOYEE_MODULE_NAME,
00559                                        QOF_EVENT_MODIFY | QOF_EVENT_DESTROY);
00560 
00561   gtk_widget_show_all (ew->dialog);
00562 
00563   if (ccard_acct == NULL)
00564     gtk_widget_hide (ew->ccard_acct_sel);
00565 
00566   return ew;
00567 }
00568 
00569 EmployeeWindow *
00570 gnc_ui_employee_new (GNCBook *bookp)
00571 {
00572   EmployeeWindow *ew;
00573 
00574   /* Make sure required options exist */
00575   if (!bookp) return NULL;
00576 
00577   ew = gnc_employee_new_window (bookp, NULL);
00578 
00579   return ew;
00580 }
00581 
00582 EmployeeWindow *
00583 gnc_ui_employee_edit (GncEmployee *employee)
00584 {
00585   EmployeeWindow *ew;
00586 
00587   if (!employee) return NULL;
00588 
00589   ew = gnc_employee_new_window (gncEmployeeGetBook(employee), employee);
00590 
00591   return ew;
00592 }
00593 
00594 /* Functions for employee selection widgets */
00595 
00596 static void
00597 invoice_employee_cb (gpointer *employee_p, gpointer user_data)
00598 {
00599   struct _employee_select_window *sw = user_data;
00600   GncOwner owner;
00601   GncEmployee *employee;
00602 
00603   g_return_if_fail (employee_p && user_data);
00604 
00605   employee = *employee_p;
00606 
00607   if (!employee)
00608     return;
00609 
00610   gncOwnerInitEmployee (&owner, employee);
00611   gnc_invoice_search (NULL, &owner, sw->book);
00612   return;
00613 }
00614 
00615 static void
00616 payment_employee_cb (gpointer *employee_p, gpointer user_data)
00617 {
00618   struct _employee_select_window *sw = user_data;
00619   GncOwner owner;
00620   GncEmployee *employee;
00621 
00622   g_return_if_fail (employee_p && user_data);
00623 
00624   employee = *employee_p;
00625 
00626   if (!employee)
00627     return;
00628 
00629   gncOwnerInitEmployee (&owner, employee);
00630   gnc_ui_payment_new (&owner, sw->book);
00631   return;
00632 }
00633 
00634 static void
00635 edit_employee_cb (gpointer *employee_p, gpointer user_data)
00636 {
00637   GncEmployee *employee;
00638 
00639   g_return_if_fail (employee_p && user_data);
00640 
00641   employee = *employee_p;
00642 
00643   if (!employee)
00644     return;
00645 
00646   gnc_ui_employee_edit (employee);
00647   return;
00648 }
00649 
00650 static gpointer
00651 new_employee_cb (gpointer user_data)
00652 {
00653   struct _employee_select_window *sw = user_data;
00654   EmployeeWindow *ew;
00655   
00656   g_return_val_if_fail (user_data, NULL);
00657 
00658   ew = gnc_ui_employee_new (sw->book);
00659   return ew_get_employee (ew);
00660 }
00661 
00662 static void
00663 free_employee_cb (gpointer user_data)
00664 {
00665   struct _employee_select_window *sw = user_data;
00666 
00667   g_return_if_fail (sw);
00668 
00669   gncQueryDestroy (sw->q);
00670   g_free (sw);
00671 }
00672 
00673 GNCSearchWindow *
00674 gnc_employee_search (GncEmployee *start, GNCBook *book)
00675 {
00676   GNCIdType type = GNC_EMPLOYEE_MODULE_NAME;
00677   struct _employee_select_window *sw;
00678   QueryNew *q, *q2 = NULL;
00679   static GList *params = NULL;
00680   static GList *columns = NULL;
00681   static GNCSearchCallbackButton buttons[] = { 
00682     { N_("View/Edit Employee"), edit_employee_cb},
00683     { N_("Expense Vouchers"), invoice_employee_cb},
00684     { N_("Process Payment"), payment_employee_cb},
00685     { NULL },
00686   };
00687 
00688   g_return_val_if_fail (book, NULL);
00689 
00690   /* Build parameter list in reverse order*/
00691   if (params == NULL) {
00692     params = gnc_search_param_prepend (params, _("Employee ID"), NULL, type,
00693                                        EMPLOYEE_ID, NULL);
00694     params = gnc_search_param_prepend (params, _("Employee Username"), NULL,
00695                                        type, EMPLOYEE_USERNAME, NULL);
00696     params = gnc_search_param_prepend (params, _("Employee Name"), NULL,
00697                                        type, EMPLOYEE_ADDR, ADDRESS_NAME, NULL);
00698   }
00699 
00700   /* Build the column list in reverse order */
00701   if (columns == NULL) {
00702     columns = gnc_search_param_prepend (columns, _("Username"), NULL, type,
00703                                         EMPLOYEE_USERNAME, NULL);
00704     columns = gnc_search_param_prepend (columns, _("ID #"), NULL, type,
00705                                         EMPLOYEE_ID, NULL);
00706     columns = gnc_search_param_prepend (columns, _("Name"), NULL, type,
00707                                         EMPLOYEE_ADDR, ADDRESS_NAME, NULL);
00708   }
00709 
00710   /* Build the queries */
00711   q = gncQueryCreateFor (type);
00712   gncQuerySetBook (q, book);
00713 
00714 #if 0
00715   if (start) {
00716     q2 = gncQueryCopy (q);
00717     gncQueryAddGUIDMatch (q2, g_slist_prepend (NULL, QUERY_PARAM_GUID),
00718                           gncEmployeeGetGUID (start), QUERY_AND);
00719   }
00720 #endif
00721 
00722   /* launch select dialog and return the result */
00723   sw = g_new0 (struct _employee_select_window, 1);
00724   sw->book = book;
00725   sw->q = q;
00726 
00727   return gnc_search_dialog_create (type, _("Find Employee"),
00728                                    params, columns, q, q2,
00729                                    buttons, NULL, new_employee_cb,
00730                                    sw, free_employee_cb,
00731                                    GCONF_SECTION_SEARCH, NULL);
00732 }
00733 
00734 GNCSearchWindow *
00735 gnc_employee_search_select (gpointer start, gpointer book)
00736 {
00737   if (!book) return NULL;
00738 
00739   return gnc_employee_search (start, book);
00740 }
00741 
00742 GNCSearchWindow *
00743 gnc_employee_search_edit (gpointer start, gpointer book)
00744 {
00745   if (start)
00746     gnc_ui_employee_edit (start);
00747 
00748   return NULL;
00749 }

Generated on Tue Oct 14 05:04:26 2008 for GnuCash by  doxygen 1.5.2