1 /* ************************************************************************** */
2 /*                                                                            */
3 /*     copyright (c)    2000-2008 Cédric Auger (cedric@grisbi.org)            */
4 /*          2004-2008 Benjamin Drieu (bdrieu@april.org)                       */
5 /*                 2009-2020 Pierre Biava (grisbi@pierre.biava.name)          */
6 /*          https://www.grisbi.org/                                           */
7 /*                                                                            */
8 /*  This program is free software; you can redistribute it and/or modify      */
9 /*  it under the terms of the GNU General Public License as published by      */
10 /*  the Free Software Foundation; either version 2 of the License, or         */
11 /*  (at your option) any later version.                                       */
12 /*                                                                            */
13 /*  This program is distributed in the hope that it will be useful,           */
14 /*  but WITHOUT ANY WARRANTY; without even the implied warranty of            */
15 /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             */
16 /*  GNU General Public License for more details.                              */
17 /*                                                                            */
18 /*  You should have received a copy of the GNU General Public License         */
19 /*  along with this program; if not, write to the Free Software               */
20 /*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
21 /*                                                                            */
22 /* ************************************************************************** */
23 
24 /**
25  * \file gsb_form.c
26  * functions working on the transactions and scheduled form
27  */
28 
29 
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33 
34 #include "include.h"
35 #include <gdk/gdkkeysyms.h>
36 #include <glib/gi18n.h>
37 
38 /*START_INCLUDE*/
39 #include "gsb_form.h"
40 #include "accueil.h"
41 #include "bet_data.h"
42 #include "bet_finance_ui.h"
43 #include "dialog.h"
44 #include "grisbi_app.h"
45 #include "grisbi_prefs.h"
46 #include "gsb_calendar.h"
47 #include "gsb_calendar_entry.h"
48 #include "gsb_combo_box.h"
49 #include "gsb_currency.h"
50 #include "gsb_data_account.h"
51 #include "gsb_data_budget.h"
52 #include "gsb_data_category.h"
53 #include "gsb_data_currency.h"
54 #include "gsb_data_form.h"
55 #include "gsb_data_mix.h"
56 #include "gsb_data_payee.h"
57 #include "gsb_data_payment.h"
58 #include "gsb_data_reconcile.h"
59 #include "gsb_data_report.h"
60 #include "gsb_data_scheduled.h"
61 #include "gsb_data_transaction.h"
62 #include "gsb_file.h"
63 #include "gsb_form_scheduler.h"
64 #include "gsb_form_transaction.h"
65 #include "gsb_form_widget.h"
66 #include "gsb_fyear.h"
67 #include "gsb_locale.h"
68 #include "gsb_payment_method.h"
69 #include "gsb_real.h"
70 #include "gsb_reconcile.h"
71 #include "gsb_report.h"
72 #include "gsb_scheduler.h"
73 #include "gsb_scheduler_list.h"
74 #include "gsb_transactions_list.h"
75 #include "gtk_combofix.h"
76 #include "menu.h"
77 #include "mouse.h"
78 #include "navigation.h"
79 #include "structures.h"
80 #include "tiers_onglet.h"
81 #include "traitement_variables.h"
82 #include "transaction_list.h"
83 #include "transaction_list_select.h"
84 #include "utils.h"
85 #include "utils_buttons.h"
86 #include "utils_dates.h"
87 #include "utils_editables.h"
88 #include "utils_operations.h"
89 #include "utils_real.h"
90 #include "utils_str.h"
91 #include "erreur.h"
92 /*END_INCLUDE*/
93 
94 /*START_EXTERN*/
95 /*END_EXTERN*/
96 
97 /*START_GLOBAL*/
98 /*END_GLOBAL*/
99 
100 /*START_STATIC*/
101 /** the 3 parts of the form :
102  * for scheduled transactions
103  * for transactions
104  * the buttons valid/cancel */
105 static GtkWidget *form_scheduled_part;
106 static GtkWidget *form_transaction_part;
107 static GtkWidget *form_button_part;
108 
109 /** need to set the 2 buttons valid/cancel here and cannot */
110 /* just show/hide the form_button_part because of the split button */
111 static GtkWidget *form_button_cancel;
112 /** when the automatic complete transaction is done
113  * for a split of transaction, we propose to recover too
114  * the children with that button */
115 
116 static GtkWidget *form_button_recover_split;
117 static GtkWidget *form_button_valid;
118 
119 static GtkWidget *transaction_form;
120 static GDate *save_form_date;
121 static gchar *save_entry;
122 
123 /* block the size allocate signal to avoid to move several times */
124 /* the tree view when open the form */
125 static gboolean block_size_allocate = FALSE;
126  /*END_STATIC*/
127 
128 /******************************************************************************/
129 /* Private Methods                                                            */
130 /******************************************************************************/
131 /**
132  * called by a click on the form
133  * propose to configure the form with the right click
134  *
135  * \param vbox
136  * \param ev
137  * \param null
138  *
139  * \return FALSE
140  **/
gsb_form_button_press(GtkWidget * vbox,GdkEventButton * ev,gpointer null)141 static gboolean gsb_form_button_press (GtkWidget *vbox,
142 									   GdkEventButton *ev,
143 									   gpointer null)
144 {
145     GtkWidget *menu;
146     GtkWidget *menu_item;
147 
148     devel_debug (NULL);
149 
150     if (ev->button != RIGHT_BUTTON)
151         return FALSE;
152 
153     menu = gtk_menu_new ();
154 
155     /* propose to configure the form */
156     menu_item = gtk_menu_item_new_with_label (_("Configure the form"));
157     g_signal_connect_swapped (G_OBJECT(menu_item),
158 							  "activate",
159 							  G_CALLBACK (grisbi_prefs_set_page_by_name),
160 							  (gchar*)"form_num_page");
161 
162 	gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
163 
164     /* Finish all. */
165     gtk_widget_show_all (menu);
166 
167 #if GTK_CHECK_VERSION (3,22,0)
168 	gtk_menu_popup_at_pointer (GTK_MENU (menu), NULL);
169 #else
170     gtk_menu_popup (GTK_MENU(menu), NULL, NULL, NULL, NULL, 3, gtk_get_current_event_time());
171 #endif
172 
173     return FALSE;
174 }
175 
176 /**
177  * check if the string in the param is like "Transfer : account"
178  * if yes, return the number of the account
179  * if deleted account, return -2
180  * if no return -1
181  *
182  * \param entry_string
183  *
184  * \return the number of the transfer_account
185  * 		-1 if transfer to a non existant account
186  * 		-2 if deleted account
187  * 		-3 if not a transfer
188  **/
gsb_form_check_for_transfer(const gchar * entry_string)189 static gint gsb_form_check_for_transfer (const gchar *entry_string)
190 {
191     const gchar *account_name;
192 
193     if (!entry_string || strncmp (entry_string, _("Transfer : "), strlen (_("Transfer : "))))
194 	 return -3;
195 
196     account_name = memchr (entry_string, ':', strlen (entry_string));
197 
198 	/* after the : there is a space before the account name */
199     account_name = account_name + 2;
200 
201     if (strcmp (account_name, _("Deleted account")))
202 		return gsb_data_account_get_no_account_by_name (account_name);
203     else
204 		return -2;
205 }
206 
207 /**
208  * get the category in the form, for transaction and scheduled transaction
209  * do everything needed by that category (create contra-transaction...)
210  *
211  * \param transaction_number 	the transaction which work with
212  * \param new_transaction 		1 if it's a new_transaction
213  * \param is_transaction 		TRUE if it's for a transaction, FALSE for a scheduled transaction
214  *
215  * \return FALSE
216  **/
gsb_form_get_categories(gint transaction_number,gint new_transaction,gboolean is_transaction)217 static gboolean gsb_form_get_categories (gint transaction_number,
218 										 gint new_transaction,
219 										 gboolean is_transaction)
220 {
221     GtkWidget *category_combofix;
222 
223     devel_debug_int (transaction_number);
224 
225     if (!gsb_data_form_check_for_value (TRANSACTION_FORM_CATEGORY))
226 		return FALSE;
227 
228     category_combofix = gsb_form_widget_get_widget (TRANSACTION_FORM_CATEGORY);
229 
230     if (gsb_form_widget_check_empty (category_combofix))
231     {
232 		/* there is no category */
233 		gsb_data_mix_set_category_number (transaction_number, 0, is_transaction);
234 		gsb_data_mix_set_sub_category_number (transaction_number, 0, is_transaction);
235 
236 		/* if it's a scheduled transaction, we need to set account_transfer to -1 */
237 		if (!is_transaction)
238 			gsb_data_scheduled_set_account_number_transfer (transaction_number, -1);
239     }
240     else
241     {
242 		gchar *string;
243 		gint contra_transaction_number;
244 
245 		/* On protège la chaine pendant toute l'opération */
246 		string = g_strdup (gtk_combofix_get_text (GTK_COMBOFIX (category_combofix)));
247 
248 		if (strcmp (string, _("Split of transaction")))
249 		{
250 			/* it's not a split of transaction, if it was one, we delete the
251 			 * transaction's daughters */
252 			gint account_transfer;
253 
254 			/* carreful : must set == 1 and not != 0 because if problem to get the result,
255 			 * that function returns -1 */
256 			if (!new_transaction
257 				&&
258 				gsb_data_mix_get_split_of_transaction (transaction_number, is_transaction) == 1)
259 			{
260 				/* we try to modify a split to a non split transaction */
261 				GSList *children_list;
262 
263 				/* get the number list of children */
264 				children_list = gsb_data_mix_get_children (transaction_number, TRUE, is_transaction);
265 
266 				/* if there is some children, we ask to be sure and delete them */
267 				if (children_list)
268 				{
269 					GSList *save_children_list;
270 
271 					if (!dialogue_yes_no (_("You are trying to change a split of transaction to another "
272 											"kind of transaction.\n"
273 											"There is some children to that transaction, if you continue, "
274 											"the children will  be deleted.\nAre you sure?"),
275 										  _("Modifying a transaction"),
276 										  GTK_RESPONSE_OK))
277 						return FALSE;
278 
279 					save_children_list = children_list;
280 					do
281 					{
282 						gint transaction_number_tmp;
283 
284 						transaction_number_tmp = GPOINTER_TO_INT (save_children_list->data);
285 
286 						if (is_transaction)
287 							gsb_transactions_list_delete_transaction (transaction_number_tmp, FALSE);
288 						else
289 							gsb_scheduler_list_delete_scheduled_transaction (transaction_number_tmp, FALSE);
290 
291 						save_children_list = save_children_list->next;
292 					}
293 					while (save_children_list);
294 
295 					g_slist_free (save_children_list);
296 				}
297 				gsb_data_mix_set_split_of_transaction (transaction_number, 0, is_transaction);
298 			}
299 
300 			/* now, check if it's a transfer or a normal category */
301 			account_transfer = gsb_form_check_for_transfer (string);
302 			switch (account_transfer)
303 			{
304 				/* if the check returns -3, it's not a transfer, so it's a normal category */
305 				case -3:
306 					/* if it's a modification, check if before it was not a transfer and delete
307 					 * the contra-transaction if necessary */
308 					if (!new_transaction)
309 					{
310 						if (is_transaction)
311 						{
312 							contra_transaction_number = gsb_data_transaction_get_contra_transaction_number
313 								(transaction_number);
314 							if (contra_transaction_number > 0)
315 							{
316 								/* it was a transfer, we delete the contra-transaction */
317 								gsb_data_transaction_set_contra_transaction_number (contra_transaction_number, 0);
318 								gsb_transactions_list_delete_transaction (contra_transaction_number, FALSE);
319 								gsb_data_transaction_set_contra_transaction_number (transaction_number, 0);
320 							}
321 						}
322 						else
323 							/* if the scheduled transaction was a transfer, it is not anymore, remove that */
324 							gsb_data_scheduled_set_account_number_transfer (transaction_number, 0);
325 					}
326 					gsb_data_category_fill_transaction_by_string (transaction_number, string, is_transaction);
327 					break;
328 
329 				case -2:
330 					/* if the check returns -2, it's a deleted account, so set -1 for transaction number transfer
331 					 * normally cannot come here if scheduled transaction, but in case,
332 					 * we set data_mix to protect */
333 					gsb_data_mix_set_transaction_number_transfer (transaction_number, -1, is_transaction);
334 					/* we don't set any break here, so with the case -1 the
335 					 * category will be set to 0 (!!let the case -1 after here) */
336 
337 					/* FALLTHRU */
338 				case -1:
339 					/* if the check returns -1, it's a transfert to non existant account,
340 					 * so do nothing */
341 					gsb_data_mix_set_category_number (transaction_number, 0, is_transaction);
342 					gsb_data_mix_set_sub_category_number (transaction_number, 0, is_transaction);
343 					break;
344 
345 					/* all other values are a number of account */
346 				default :
347 					/* need to check a lot of things and create the contra-transaction for a transaction,
348 					 * but nothing to do for a scheduled transaction because no contra-transaction */
349 					if (is_transaction)
350 					gsb_form_transaction_validate_transfer (transaction_number,
351 															new_transaction,
352 															account_transfer);
353 					else
354 					gsb_data_scheduled_set_account_number_transfer (transaction_number, account_transfer);
355 			}
356 		}
357 		else
358 		{
359 			/* it's a split of transaction */
360 			/* if it was a transfer, we delete the contra-transaction */
361 			contra_transaction_number = gsb_data_transaction_get_contra_transaction_number (transaction_number);
362 			if (is_transaction && !new_transaction && contra_transaction_number > 0)
363 			{
364 				gsb_data_transaction_set_contra_transaction_number (contra_transaction_number, 0);
365 				gsb_transactions_list_delete_transaction (contra_transaction_number, FALSE);
366 				gsb_data_transaction_set_contra_transaction_number (transaction_number, 0);
367 			}
368 
369 			/* if it's a modification of a transaction and it was not a split,
370 			 * but it is now, we add a white line as first child */
371 			if (!new_transaction && !gsb_data_mix_get_split_of_transaction (transaction_number, is_transaction))
372 			{
373 				if (!is_transaction)
374 					gsb_scheduler_list_append_new_scheduled (gsb_data_scheduled_new_white_line (transaction_number),
375 															 gsb_data_scheduled_get_limit_date (transaction_number));
376 			}
377 
378 			gsb_data_mix_set_split_of_transaction (transaction_number, 1, is_transaction);
379 			gsb_data_mix_set_category_number (transaction_number, 0, is_transaction);
380 			gsb_data_mix_set_sub_category_number (transaction_number, 0, is_transaction);
381 
382 			/* normally the following widgets are hidden for a split, but if the user really
383 			 * want he can annoy us and set an budget for example, and after cry because problems
384 			 * in reports... so, erase here budget and financial year, if ever they are defined */
385 			gsb_data_mix_set_budgetary_number (transaction_number, 0, is_transaction);
386 			gsb_data_mix_set_sub_budgetary_number (transaction_number, 0, is_transaction);
387 			gsb_data_mix_set_voucher (transaction_number, NULL, is_transaction);
388 		}
389 		g_free (string);
390     }
391 
392     return FALSE;
393 }
394 
395 /**
396  * initialise le formulaire
397  *
398  * \param
399  *
400  * \return FALSE
401  **/
gsb_form_initialise_transaction_form(void)402 static gboolean gsb_form_initialise_transaction_form (void)
403 {
404     gint row, column;
405     gint rows_number, columns_number;
406     gint account_number;
407 
408     devel_debug (NULL);
409 
410     /* get the form of the first account */
411     account_number = gsb_data_account_first_number ();
412 
413     rows_number = gsb_data_form_get_nb_rows ();
414     columns_number = gsb_data_form_get_nb_columns ();
415 
416     for (row=0 ; row < rows_number ; row++)
417 	for (column=0 ; column < columns_number ; column++)
418 	{
419 	    GtkWidget *widget;
420 	    gint element;
421 
422         element = gsb_data_form_get_value (column, row);
423 		widget = gsb_form_widget_create (element, account_number);
424 
425 	    if (!widget)
426             continue;
427 
428 	    gtk_grid_attach (GTK_GRID (form_transaction_part), widget, column, row, 1, 1);
429         g_object_set_data (G_OBJECT (widget), "num_row", GINT_TO_POINTER (row));
430 	}
431 
432     return FALSE;
433 }
434 
435 /**
436  * this function is used to move the tree view when open the form,
437  * without that, the selection will be hidden by the form,
438  * this moves it to show if necessary the selection
439  *
440  * \param widget
441  * \param allocation
442  * \param null
443  *
444  * \return FALSE
445  **/
gsb_form_size_allocate(GtkWidget * widget,GtkAllocation * allocation,gpointer null)446 static void gsb_form_size_allocate (GtkWidget *widget,
447 									GtkAllocation *allocation,
448 									gpointer null)
449 {
450     if (grisbi_win_form_expander_is_expanded () && !block_size_allocate)
451     {
452 		block_size_allocate = TRUE;
453 		gsb_transactions_list_set_row_align (-1.0);
454     }
455     else
456 		block_size_allocate = FALSE;
457 }
458 
459 /**
460  * get the datas in the form and set them in the transaction/scheduled transaction in param
461  *
462  * \param transaction_number 	the transaction to modify
463  * \param is_transaction 		TRUE if it's for a transaction, FALSE for a scheduled transaction
464  *
465  * \return
466  **/
gsb_form_take_datas_from_form(gint transaction_number,gboolean is_transaction)467 static void gsb_form_take_datas_from_form (gint transaction_number,
468 										   gboolean is_transaction)
469 {
470     GSList *tmp_list;
471     GDate *date;
472     GDate *value_date;
473 
474     devel_debug_int (transaction_number);
475 
476     /* we set a date variable to avoid to parse 2 times, one time for the date,
477      * and perhaps a second time with the financial year
478      * (cannot take it from the transaction if the fyear field is before the date field...) */
479     /* get the date first, because the financial year will use it and it can set before the date in the form */
480     date = gsb_calendar_entry_get_date (gsb_form_widget_get_widget (TRANSACTION_FORM_DATE));
481 
482     /* as financial year can be taken with value date, need to get the value date now too,
483      * if TRANSACTION_FORM_VALUE_DATE doesn't exist, value_date will be NULL */
484     value_date = gsb_calendar_entry_get_date (gsb_form_widget_get_widget (TRANSACTION_FORM_VALUE_DATE));
485 
486     tmp_list = gsb_form_widget_get_list ();
487 
488     while (tmp_list)
489     {
490 		struct_element *element;
491 
492 		element = tmp_list->data;
493 
494 		switch (element->element_number)
495 		{
496 			case TRANSACTION_FORM_DATE:
497 			/* set date before */
498 			gsb_data_mix_set_date (transaction_number, date, is_transaction);
499 
500 			break;
501 
502 			case TRANSACTION_FORM_VALUE_DATE:
503 			if (gsb_form_widget_check_empty (element->element_widget))
504 				gsb_data_mix_set_value_date (transaction_number, NULL, is_transaction);
505 			else
506 				gsb_data_mix_set_value_date (transaction_number,
507 											 gsb_calendar_entry_get_date (element->element_widget),
508 											 is_transaction);
509 			break;
510 
511 			case TRANSACTION_FORM_EXERCICE:
512 			if (is_transaction)
513 			{
514 				GrisbiAppConf *a_conf;
515 
516 				a_conf = (GrisbiAppConf *) grisbi_app_get_a_conf ();
517 				if (a_conf->affichage_exercice_automatique)
518 				{
519 					if (gsb_form_widget_check_empty (gsb_form_widget_get_widget (TRANSACTION_FORM_VALUE_DATE)))
520 						gsb_data_transaction_set_financial_year_number (transaction_number,
521 																		gsb_fyear_get_fyear_from_combobox
522 																		(element->element_widget, date));
523 					else
524 						gsb_data_transaction_set_financial_year_number (transaction_number,
525 																		gsb_fyear_get_fyear_from_combobox
526 																		(element->element_widget, value_date));
527 				}
528 				else
529 					gsb_data_transaction_set_financial_year_number (transaction_number,
530 																	gsb_fyear_get_fyear_from_combobox
531 																	(element->element_widget, date));
532 			}
533 			else
534 				gsb_data_scheduled_set_financial_year_number (transaction_number,
535 															  gsb_fyear_get_fyear_from_combobox
536 															  (element->element_widget, NULL));
537 			break;
538 
539 			case TRANSACTION_FORM_PARTY:
540 			if (gsb_form_widget_check_empty (element->element_widget))
541 				gsb_data_mix_set_party_number (transaction_number, 0, is_transaction);
542 			else
543 			{
544 				const gchar *tmp_name;
545 
546 				tmp_name = gtk_combofix_get_text (GTK_COMBOFIX (element->element_widget));
547 				gsb_data_mix_set_party_number (transaction_number,
548 											   gsb_data_payee_get_number_by_name (tmp_name, TRUE),
549 											   is_transaction);
550 			}
551 			break;
552 
553 			case TRANSACTION_FORM_DEBIT:
554 			if (!gsb_form_widget_check_empty (element->element_widget))
555 			{
556 				gsb_form_check_auto_separator (element->element_widget);
557 				gsb_data_mix_set_amount (transaction_number, gsb_real_opposite
558 										 (utils_real_get_calculate_entry (element->element_widget)),
559 										 is_transaction);
560 			}
561 			break;
562 
563 			case TRANSACTION_FORM_CREDIT:
564 				if (!gsb_form_widget_check_empty (element->element_widget))
565 				{
566 					gsb_form_check_auto_separator (element->element_widget);
567 					gsb_data_mix_set_amount (transaction_number,
568 											 utils_real_get_calculate_entry (element->element_widget),
569 											 is_transaction);
570 				}
571 				break;
572 
573 			case TRANSACTION_FORM_BUDGET:
574 				if (gsb_form_widget_check_empty (element->element_widget))
575 				{
576 					gsb_data_mix_set_budgetary_number (transaction_number, 0, is_transaction);
577 					gsb_data_mix_set_sub_budgetary_number (transaction_number, 0, is_transaction);
578 				}
579 				else
580 					gsb_data_budget_set_budget_from_string (transaction_number,
581 															gtk_combofix_get_text (GTK_COMBOFIX
582 																				   (element->element_widget)),
583 															is_transaction);
584 				break;
585 
586 			case TRANSACTION_FORM_NOTES:
587 				if (gsb_form_widget_check_empty (element->element_widget))
588 					gsb_data_mix_set_notes (transaction_number, NULL, is_transaction);
589 				else
590 				{
591 					gchar* tmp_str;
592 
593 					tmp_str = my_strdup (gtk_entry_get_text (GTK_ENTRY (element->element_widget)));
594 					gsb_data_mix_set_notes (transaction_number, tmp_str , is_transaction);
595 					g_free (tmp_str);
596 				}
597 				break;
598 
599 			case TRANSACTION_FORM_TYPE:
600 				/* set the type only if visible */
601 				if (gtk_widget_get_visible (element->element_widget))
602 				{
603 					GtkWidget *widget_tmp;
604 					gint payment_number;
605 
606 					payment_number = gsb_payment_method_get_selected_number (element->element_widget);
607 
608 					gsb_data_mix_set_method_of_payment_number (transaction_number, payment_number, is_transaction);
609 
610 					/* set the number of cheque only if visible */
611 					widget_tmp = gsb_form_widget_get_widget (TRANSACTION_FORM_CHEQUE);
612 					if (gtk_widget_get_visible (widget_tmp) && !gsb_form_widget_check_empty (widget_tmp))
613 					{
614 						gsb_data_mix_set_method_of_payment_content (transaction_number,
615 																	gtk_entry_get_text (GTK_ENTRY (widget_tmp)),
616 																	is_transaction);
617 
618 					/* get the last number to increase next time */
619 					if (is_transaction && gsb_data_payment_get_automatic_numbering (payment_number))
620 						gsb_data_payment_set_last_number (payment_number, gtk_entry_get_text (GTK_ENTRY (widget_tmp)));
621 					}
622 					else
623 						gsb_data_mix_set_method_of_payment_content (transaction_number, NULL, is_transaction);
624 				}
625 				else
626 				{
627 					gsb_data_mix_set_method_of_payment_number (transaction_number, 0, is_transaction);
628 					gsb_data_mix_set_method_of_payment_content (transaction_number, NULL, is_transaction);
629 				}
630 				break;
631 
632 			case TRANSACTION_FORM_CONTRA:
633 				/* here only for scheduled transaction */
634 				if (!is_transaction && gtk_widget_get_visible (element->element_widget))
635 					gsb_data_scheduled_set_contra_method_of_payment_number (transaction_number,
636 																			gsb_payment_method_get_selected_number
637 																			(element->element_widget));
638 				break;
639 
640 			case TRANSACTION_FORM_DEVISE:
641 				gsb_data_mix_set_currency_number (transaction_number,
642 												  gsb_currency_get_currency_from_combobox
643 												  (element->element_widget),
644 												  is_transaction);
645 				if (is_transaction)
646 					gsb_currency_check_for_change (transaction_number);
647 
648 				break;
649 
650 			case TRANSACTION_FORM_BANK:
651 				if (gsb_form_widget_check_empty (element->element_widget))
652 					gsb_data_mix_set_bank_references (transaction_number, NULL, is_transaction);
653 				else
654 				{
655 					gchar* tmp_str;
656 
657 					tmp_str = my_strdup (gtk_entry_get_text (GTK_ENTRY(element->element_widget)));
658 					gsb_data_mix_set_bank_references (transaction_number, tmp_str, is_transaction);
659 					g_free (tmp_str);
660 				}
661 				break;
662 
663 			case TRANSACTION_FORM_VOUCHER:
664 				if (gsb_form_widget_check_empty (element->element_widget))
665 					gsb_data_mix_set_voucher (transaction_number, NULL, is_transaction);
666 				else
667 				{
668 					gchar* tmp_str;
669 
670 					tmp_str = my_strdup (gtk_entry_get_text (GTK_ENTRY(element->element_widget)));
671 					gsb_data_mix_set_voucher (transaction_number, tmp_str, is_transaction);
672 					g_free (tmp_str);
673 				}
674 				break;
675 		}
676 		tmp_list = tmp_list->next;
677     }
678 }
679 
680 /**
681  * check if the transaction/scheduled transaction in the form is correct
682  *
683  * \param  transaction_number	can be -2 for a new daughter scheduled transaction, 0 for a new transaction
684  * 								or any number corresponding to an older transaction
685  * \param is_transaction 		TRUE if it's for a transaction, FALSE for a scheduled transaction
686  *
687  * \return TRUE or FALSE
688  **/
gsb_form_validate_form_transaction(gint transaction_number,gboolean is_transaction)689 static gboolean gsb_form_validate_form_transaction (gint transaction_number,
690 													gboolean is_transaction)
691 {
692     GtkWidget *widget;
693     GtkWidget *date_widget;
694     gchar* tmp_str;
695     gint mother_number;
696     gint account_number;
697     GsbReal number = null_real;
698 
699     devel_debug_int (transaction_number);
700 
701     account_number = gsb_form_get_account_number ();
702 
703     /* check if it's a daughter split */
704     mother_number = gsb_data_mix_get_mother_transaction_number (transaction_number, is_transaction);
705 
706     /* begin to work with dates */
707     date_widget = gsb_form_widget_get_widget (TRANSACTION_FORM_DATE);
708 
709     /* check the date exists */
710     if (gsb_form_widget_check_empty (date_widget))
711     {
712         dialogue_error (_("You must enter a date."));
713         return (FALSE);
714     }
715 
716     /* check the date ok */
717     if (!gsb_date_check_entry (date_widget))
718     {
719         tmp_str = g_strdup_printf (_("Invalid date %s"), gtk_entry_get_text (GTK_ENTRY (date_widget)));
720         dialogue_error (tmp_str);
721         g_free(tmp_str);
722         gtk_editable_select_region (GTK_EDITABLE (date_widget), 0, -1);
723         gtk_widget_grab_focus (date_widget);
724 
725         return (FALSE);
726     }
727     else
728     {
729         if (save_form_date)
730             g_date_free (save_form_date);
731         save_form_date = gsb_date_copy (gsb_calendar_entry_get_date (date_widget));
732     }
733 
734     /* work with value date */
735     widget = gsb_form_widget_get_widget (TRANSACTION_FORM_VALUE_DATE);
736     if (widget
737 		&& !gsb_form_widget_check_empty (widget)
738 		&& strlen (gtk_entry_get_text (GTK_ENTRY (widget))) > 0)
739     {
740         if (!gsb_date_check_entry (widget))
741         {
742             tmp_str = g_strdup_printf (_("Invalid value date %s"), gtk_entry_get_text (GTK_ENTRY (widget)));
743             dialogue_error (tmp_str);
744             g_free (tmp_str);
745             gtk_editable_select_region (GTK_EDITABLE (widget), 0, -1);
746             gtk_widget_grab_focus (widget);
747 
748             return (FALSE);
749         }
750         else if (is_transaction
751 				 && gsb_data_transaction_get_marked_transaction (transaction_number) == OPERATION_RAPPROCHEE
752 				 && mother_number == 0)
753         {
754             GDate *value_date;
755             const GDate *init_date;
756             const GDate *final_date;
757             gint reconcile_number;
758 
759             value_date = gsb_calendar_entry_get_date (widget);
760             reconcile_number = gsb_data_transaction_get_reconcile_number (transaction_number);
761             init_date = gsb_data_reconcile_get_init_date (reconcile_number);
762             final_date = gsb_data_reconcile_get_final_date (reconcile_number);
763             if (g_date_compare (value_date, init_date) < 0
764              ||
765              g_date_compare (value_date, final_date) > 0)
766             {
767                 tmp_str = g_strdup_printf (_("Beware the date must be between %s and %s"),
768 										   gsb_format_gdate (init_date),
769 										   gsb_format_gdate (final_date));
770                 dialogue_hint (tmp_str, _("Invalid date"));
771 
772                 g_free(tmp_str);
773 
774                 gsb_calendar_entry_set_color (widget, FALSE);
775                 gtk_editable_select_region (GTK_EDITABLE (widget), 0, -1);
776                 gtk_widget_grab_focus (widget);
777 
778                 return FALSE;
779             }
780         }
781     }
782     else    /* contrôle de la date de l'opération rapprochée si pas de date de valeur */
783     {
784         if (is_transaction
785 			&& gsb_data_transaction_get_marked_transaction (transaction_number) == OPERATION_RAPPROCHEE
786 			&& mother_number == 0)
787         {
788             const GDate *final_date;
789             gint reconcile_number;
790 
791             reconcile_number = gsb_data_transaction_get_reconcile_number (transaction_number);
792             final_date = gsb_data_reconcile_get_final_date (reconcile_number);
793             if (g_date_compare (save_form_date, final_date) > 0)
794             {
795                 tmp_str = g_strdup_printf (_("The date must be less than or equal to %s"),
796 										   gsb_format_gdate (final_date));
797                 dialogue_hint (tmp_str, _("Invalid date"));
798 
799                 g_free(tmp_str);
800 
801                 gsb_calendar_entry_set_color (date_widget, FALSE);
802                 gtk_editable_select_region (GTK_EDITABLE (date_widget), 0, -1);
803                 gtk_widget_grab_focus (date_widget);
804 
805                 return FALSE;
806             }
807         }
808     }
809 
810     /* check if debit or credit is > 0 */
811     widget = gsb_form_widget_get_widget (TRANSACTION_FORM_DEBIT);
812     if (widget)
813     {
814         if (gsb_form_widget_check_empty (widget) == FALSE)
815             number = gsb_real_opposite (utils_real_get_calculate_entry (widget));
816 
817 		if (gsb_form_widget_check_empty (widget) == TRUE || number.mantissa == 0)
818         {
819 			gint payment_number;
820 
821 			widget = gsb_form_widget_get_widget (TRANSACTION_FORM_CREDIT);
822             number = utils_real_get_calculate_entry (widget);
823 
824             if ((gsb_form_widget_check_empty (widget) == TRUE || number.mantissa == 0)
825 				&& mother_number)
826             {
827                 dialogue_error (_("You must enter an amount."));
828                 return (FALSE);
829             }
830 			/* on remet le bon moyen de payement */
831 			widget = gsb_form_widget_get_widget (TRANSACTION_FORM_TYPE);
832 			if (widget && gtk_widget_get_sensitive (widget))
833 			{
834 				/* change the signe of the method of payment and the contra */
835 				if (gsb_payment_method_get_combo_sign (widget) == GSB_PAYMENT_DEBIT)
836 				{
837 					gsb_payment_method_create_combo_list (widget,
838 														  GSB_PAYMENT_CREDIT,
839 														  account_number,
840 														  0,
841 														  FALSE);
842 					payment_number = gsb_data_account_get_default_credit (account_number);
843 					gsb_payment_method_set_payment_position (widget, payment_number);
844 
845 					widget = gsb_form_widget_get_widget (TRANSACTION_FORM_CHEQUE);
846 					if (gsb_data_payment_get_show_entry (payment_number))
847 					{
848 						if (widget && gtk_widget_get_visible (widget))
849 						{
850 							if (gsb_form_widget_get_old_credit_payment_content ())
851 							{
852 								gtk_entry_set_text (GTK_ENTRY (widget),
853 													gsb_form_widget_get_old_credit_payment_content ());
854 								gsb_form_widget_set_empty (widget, FALSE);
855 							}
856 							else
857 							{
858 								gtk_entry_set_text (GTK_ENTRY (widget), "");
859 								gsb_form_widget_set_empty (widget, TRUE);
860 							}
861 						}
862 					}
863 					else
864 					{
865 						gsb_form_widget_set_empty (widget, TRUE);
866 						gtk_widget_hide (widget);
867 					}
868 
869 					widget = gsb_form_widget_get_widget (TRANSACTION_FORM_CONTRA);
870 					if (widget && gtk_widget_get_visible (widget))
871 						gsb_payment_method_create_combo_list (widget,
872 															  GSB_PAYMENT_DEBIT,
873 															  account_number,
874 															  0,
875 															  TRUE);
876 				}
877 			}
878         }
879     }
880 
881     /* check if balance is < 0 and account_kind == GSB_TYPE_CASH */
882     if (gsb_data_account_get_kind (account_number) == GSB_TYPE_CASH)
883     {
884         GsbReal balance;
885 
886         balance = gsb_real_add (number, gsb_data_account_get_current_balance (account_number));
887         if (balance.mantissa < 0)
888         {
889             GsbReal previous_debit;
890 
891             previous_debit = utils_real_get_from_string (save_entry);
892             balance = gsb_real_add (balance, previous_debit);
893             g_free (save_entry);
894             save_entry = NULL;
895 
896             if (balance.mantissa < 0)
897             {
898                 dialogue_error (_("This account cannot be negative.\n\n"
899                                   "Please enter another amount or cancel this transaction."));
900                 return (FALSE);
901             }
902         }
903     }
904 
905     /* now work with the categories */
906     widget = gsb_form_widget_get_widget (TRANSACTION_FORM_CATEGORY);
907 
908     /* check if it's a daughter split that the category is not a split of transaction */
909     if (widget
910 		&& !gsb_form_widget_check_empty (widget)
911 		&& mother_number
912 		&& !strcmp (gtk_combofix_get_text (GTK_COMBOFIX (widget)), _("Split of transaction")))
913     {
914 		dialogue_error (_("You cannot set split of transaction in category for a daughter "
915 						  "of a split of transaction."));
916 		return (FALSE);
917     }
918 
919     /* check it's not a transfer on itself and the contra-account exists
920      * !!!widget is already set to the category, don't change it before */
921     if (widget && !gsb_form_widget_check_empty (widget))
922     {
923 		gint account_transfer;
924 
925 		account_transfer = gsb_form_check_for_transfer (gtk_combofix_get_text (GTK_COMBOFIX (widget)));
926 
927 		switch (account_transfer)
928 		{
929 			/* if the check returns -3, it's not a transfer, so it's a normal category */
930 			case -3:
931 				break;
932 
933 			/* if the check returns -2, it's a deleted account (refuse for scheduled transaction)*/
934 			case -2:
935 				if (is_transaction)
936 				{
937 					gsb_data_transaction_set_category_number (transaction_number, 0);
938 					gsb_data_transaction_set_sub_category_number (transaction_number, 0);
939 					gsb_data_transaction_set_contra_transaction_number (transaction_number, -1);
940 				}
941 				else
942 				{
943 					dialogue_error (_("Cannot associate a transfer to a deleted account in a "
944 									  "scheduled transaction."));
945 					return FALSE;
946 				}
947 				break;
948 
949 			/* if the check returns -1, it's a non existant account */
950 			case -1:
951 				dialogue_error (_("There is no associated account for this transfer or associated "
952 								  "account is invalid."));
953 
954 				return (FALSE);
955 				break;
956 
957 			/* all other values are a number of account */
958 			default :
959 				if (account_transfer == account_number)
960 				{
961 					dialogue_error (_("Can't issue a transfer its own account."));
962 					return (FALSE);
963 				}
964 				if (gsb_data_account_get_closed_account (account_transfer))
965 				{
966 					dialogue_error (_("Can't issue a transfer on a closed account."));
967 					return (FALSE);
968 				}
969 		}
970     }
971 
972     /* for the automatic method of payment entry (especially cheques)
973      * check if not used before (only for new transaction) */
974     widget = gsb_form_widget_get_widget (TRANSACTION_FORM_CHEQUE);
975     if (widget && gtk_widget_get_visible (widget))
976     {
977 		GtkWidget *combo_box;
978 		gint payment;
979 
980 		combo_box = gsb_form_widget_get_widget (TRANSACTION_FORM_TYPE);
981 		payment = gsb_payment_method_get_selected_number (combo_box);
982 		if (gsb_data_payment_get_automatic_numbering (payment))
983 		{
984 			/* check if there is something in */
985 			if (gsb_form_widget_check_empty (widget))
986 			{
987 
988 				if (!dialogue_yes_no (_("Selected method of payment has an automatic incremental number\n"
989 										"but doesn't contain any number.\n"
990 										"Continue anyway?"),
991 									  NULL,
992 									  GTK_RESPONSE_CANCEL))
993 				{
994 					return (FALSE);
995 				}
996 			}
997 			else if (mother_number == 0)
998 			{
999 				/* check that the number is not used */
1000 				gint tmp_transaction_number;
1001 
1002 				tmp_transaction_number = gsb_data_transaction_check_content_payment (payment,
1003 																					 gtk_entry_get_text
1004 																					 (GTK_ENTRY (widget)));
1005 
1006 				if (tmp_transaction_number
1007 					&& tmp_transaction_number != transaction_number
1008 					&& !dialogue_yes_no (_("Warning: this cheque number is already used.\nContinue anyway?"),
1009 										 NULL,
1010 										 GTK_RESPONSE_CANCEL))
1011 				{
1012 					return FALSE;
1013 				}
1014 			}
1015 		}
1016     }
1017 
1018     /* check if the payee is a report, it must be a new transaction */
1019     widget = gsb_form_widget_get_widget (TRANSACTION_FORM_PARTY);
1020     if (widget)
1021     {
1022 		const gchar *payee_name = NULL;
1023 
1024 		payee_name = gtk_combofix_get_text (GTK_COMBOFIX (widget));
1025 		if (payee_name && !strncmp (payee_name, _("Report"), strlen (_("Report"))))
1026 		{
1027 			gchar **tab_char;
1028 
1029 			/* check if it's a new transaction */
1030 			if (transaction_number > 0)
1031 			{
1032 				dialogue_error (_("A transaction with a multiple payee must be a new one."));
1033 				return (FALSE);
1034 			}
1035 			if (mother_number > 0)
1036 			{
1037 				dialogue_error (_("A transaction with a multiple payee cannot be a split child."));
1038 				return (FALSE);
1039 			}
1040 
1041 			/* check if the report exists */
1042 			tab_char = g_strsplit (payee_name, " : ", 2);
1043 			if (!tab_char[1])
1044 			{
1045 				dialogue_error  (_("The word \"Report\" is reserved. Please use another one."));
1046 				g_strfreev (tab_char);
1047 				return FALSE;
1048 			}
1049 
1050 			if (!gsb_data_report_get_report_by_name (tab_char[1]))
1051 			{
1052 				dialogue_error (_("Invalid multiple payee."));
1053 				g_strfreev (tab_char);
1054 				return (FALSE);
1055 			}
1056 			g_strfreev (tab_char);
1057 		}
1058     }
1059 
1060 	return (TRUE);
1061 }
1062 
1063 /**
1064  * set fixed date if available for the scheduled transaction
1065  *
1066  * \param transaction_number
1067  * \param date
1068  *
1069  * \return
1070  **/
gsb_form_set_fixed_date_if_necessary(gint scheduled_number,GDate * date)1071 static void gsb_form_set_fixed_date_if_necessary (gint scheduled_number,
1072 												  GDate *date)
1073  {
1074 	GDateDay day;
1075 
1076 	day = g_date_get_day (date);
1077 	if (day == 31)
1078 	{
1079 		gsb_data_scheduled_set_fixed_date (scheduled_number, 31);
1080 		return;
1081 	}
1082 
1083 	if (g_date_is_last_of_month (date))
1084 	{
1085 		gint fixed_date;
1086 
1087 		fixed_date = gsb_form_scheduler_get_last_day_of_month_dialog (scheduled_number, date);
1088 		gsb_data_scheduled_set_fixed_date (scheduled_number, fixed_date);
1089 
1090 		return;
1091 	}
1092 
1093 	switch (day)
1094 	{
1095 		case 28:
1096 			gsb_data_scheduled_set_fixed_date (scheduled_number, 28);
1097 			break;
1098 		case 29:
1099 			gsb_data_scheduled_set_fixed_date (scheduled_number, 29);
1100 			break;
1101 		case 30:
1102 			gsb_data_scheduled_set_fixed_date (scheduled_number, 30);
1103 			break;
1104 	}
1105  }
1106 
1107 /******************************************************************************/
1108 /* Public Methods                                                             */
1109 /******************************************************************************/
1110 /**
1111  * the value transaction_form is static,
1112  * that function return it
1113  *
1114  * \param
1115  *
1116  * \return a pointer to the transaction form
1117  **/
gsb_form_get_form_widget(void)1118 GtkWidget *gsb_form_get_form_widget (void)
1119 {
1120     return transaction_form;
1121 }
1122 
1123 /**
1124  * return the address of the scheduler part widget
1125  *
1126  * \param
1127  *
1128  * \return a pointer to a widget (the scheduler part of the form)
1129  **/
gsb_form_get_scheduler_part(void)1130 GtkWidget *gsb_form_get_scheduler_part (void)
1131 {
1132     return form_scheduled_part;
1133 }
1134 
1135 /**
1136  *  Do the grunt job of creating widgets in for the Grisbi form.
1137  *  Fills them in form_expander, a GtkExpander normally created by
1138  *  gsb_form_new().  It is reentrant, that is calling it over and over
1139  *  will reinitialize form content and delete previous content.
1140  *
1141  * \param
1142  *
1143  * \return
1144  **/
gsb_form_create_widgets(void)1145 void gsb_form_create_widgets (void)
1146 {
1147     GtkWidget *child;
1148     GtkWidget *event_box;
1149 	GtkWidget *form_expander;
1150     GtkWidget *hbox_buttons;
1151     GtkWidget *hbox_buttons_inner;
1152     GtkWidget *separator;
1153 
1154     devel_debug (NULL);
1155 
1156 	form_expander = grisbi_win_get_form_expander ();
1157 	child = gtk_bin_get_child (GTK_BIN (form_expander));
1158     if (child && GTK_IS_WIDGET(child))
1159     {
1160         gsb_form_widget_free_list ();
1161 	    gtk_container_remove (GTK_CONTAINER (form_expander), child);
1162     }
1163 
1164     /* Create form inside the expander :
1165      * the form is 3 parts :
1166      * top : the values specific for the scheduled transactions
1167      * middle : the values for transactions and scheduled transactions
1168      * bottom : the buttons valid/cancel */
1169     transaction_form = gtk_box_new (GTK_ORIENTATION_VERTICAL, MARGIN_BOX);
1170     gtk_container_add (GTK_CONTAINER (form_expander), transaction_form);
1171 
1172     /* play with that widget to tell to the tree view to scroll to keep the selection visible */
1173     g_signal_connect_after (G_OBJECT (transaction_form),
1174 							"size-allocate",
1175 							G_CALLBACK (gsb_form_size_allocate),
1176 							NULL);
1177 
1178     /* the scheduled part is a table of SCHEDULED_WIDTH col x SCHEDULED_HEIGHT rows */
1179     form_scheduled_part = gtk_grid_new ();
1180     gtk_grid_set_column_spacing (GTK_GRID (form_scheduled_part), MARGIN_BOX);
1181     gtk_box_pack_start (GTK_BOX (transaction_form), form_scheduled_part, FALSE, FALSE, 0);
1182 
1183     gsb_form_scheduler_create (form_scheduled_part);
1184 
1185     /* add a separator between the scheduled and transaction part */
1186     separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
1187     gtk_box_pack_start (GTK_BOX (transaction_form), separator, FALSE, FALSE, 0);
1188 
1189     /* the transactions part is a variable table,
1190      * so set to 1x1 for now, it will change when we show it */
1191     /* didn't find another way to get the button-press-event on the form_transaction_part,
1192      * so use an event box */
1193     event_box = gtk_event_box_new ();
1194     gtk_event_box_set_above_child (GTK_EVENT_BOX (event_box), FALSE);
1195     gtk_box_pack_start (GTK_BOX (transaction_form), event_box, FALSE, FALSE, 0);
1196     g_signal_connect (G_OBJECT (event_box),
1197 					  "button-press-event",
1198 					  G_CALLBACK (gsb_form_button_press),
1199 					  NULL);
1200 
1201     form_transaction_part = gtk_grid_new ();
1202     gtk_grid_set_column_spacing (GTK_GRID (form_transaction_part), MARGIN_BOX);
1203 
1204     gtk_container_add (GTK_CONTAINER (event_box), form_transaction_part);
1205 
1206     gsb_form_initialise_transaction_form ();
1207 
1208     /* the buttons part is a hbox, with the recuperate child split
1209      * on the left and valid/cancel on the right */
1210     form_button_part = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
1211     gtk_box_pack_start (GTK_BOX (transaction_form), form_button_part, FALSE, FALSE, 0);
1212 
1213     /* add a separator between the transaction and button part */
1214     separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
1215     gtk_box_pack_start (GTK_BOX (form_button_part), separator, FALSE, FALSE, 0);
1216 
1217     /* Hbox containing buttons */
1218     hbox_buttons = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
1219     gtk_box_pack_start (GTK_BOX (form_button_part), hbox_buttons, FALSE, FALSE, 0);
1220 
1221     hbox_buttons_inner = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
1222     gtk_box_set_homogeneous (GTK_BOX (hbox_buttons_inner), TRUE);
1223     gtk_box_pack_end (GTK_BOX (hbox_buttons), hbox_buttons_inner, FALSE, FALSE, 0);
1224 
1225     /* create the check button to recover the children of splits */
1226     form_button_recover_split = gtk_check_button_new_with_label (_("Recover the children"));
1227     gtk_box_pack_start (GTK_BOX (hbox_buttons_inner), form_button_recover_split, FALSE, FALSE, 0);
1228 
1229     /* create the valid/cancel buttons */
1230     form_button_valid = utils_buttons_button_new_from_stock ("gtk-ok", _("V_alidate"));
1231     gtk_button_set_relief (GTK_BUTTON (form_button_valid), GTK_RELIEF_NONE);
1232     g_signal_connect (G_OBJECT (form_button_valid), "clicked",
1233 					  G_CALLBACK (gsb_form_finish_edition), NULL);
1234     gtk_box_pack_end (GTK_BOX (hbox_buttons_inner), form_button_valid, FALSE, FALSE, 0);
1235 
1236     form_button_cancel = utils_buttons_button_new_from_stock ("gtk-cancel", _("_Cancel"));
1237     gtk_button_set_relief (GTK_BUTTON (form_button_cancel), GTK_RELIEF_NONE);
1238     g_signal_connect (G_OBJECT (form_button_cancel), "clicked",
1239 					  G_CALLBACK (gsb_form_escape_form), NULL);
1240     gtk_box_pack_end (GTK_BOX (hbox_buttons_inner), form_button_cancel, FALSE, FALSE, 0);
1241 
1242     gtk_widget_show_all (transaction_form);
1243     gtk_widget_hide (form_scheduled_part);
1244 
1245     gtk_widget_set_sensitive (GTK_WIDGET (form_button_valid), FALSE);
1246     gtk_widget_set_sensitive (GTK_WIDGET (form_button_cancel), FALSE);
1247 
1248     gsb_form_show (FALSE);
1249 }
1250 
1251 /**
1252  * fill the form according to the transaction or scheduled transaction given in param
1253  *
1254  * \param transaction_number 	the number of the transaction or scheduled transaction
1255  * \param is_transaction 		TRUE if transaction, FALSE if scheduled transaction
1256  * 								that param could be taken from the navigation bar but
1257  *								i prefer set in param for now
1258  * \param grab_focus 			if TRUE, after filling the form, the date will grab the focus ;
1259  *								if FALSE, nothing change with focus
1260  *
1261  * \return FALSE
1262  **/
gsb_form_fill_by_transaction(gint transaction_number,gint is_transaction,gboolean grab_focus)1263 gboolean gsb_form_fill_by_transaction (gint transaction_number,
1264 									   gint is_transaction,
1265 									   gboolean grab_focus)
1266 {
1267     gint mother_number;
1268     gint account_number;
1269     gint focus_to;
1270     gint is_split;
1271     GSList *tmp_list;
1272 
1273     devel_debug_int (transaction_number);
1274 
1275     if (!transaction_number)
1276         return FALSE;
1277 
1278     /* get the account */
1279     account_number = gsb_data_mix_get_account_number (transaction_number, is_transaction);
1280     mother_number = gsb_data_mix_get_mother_transaction_number (transaction_number, is_transaction);
1281     is_split = gsb_data_mix_get_split_of_transaction (transaction_number, is_transaction);
1282 
1283     /* if here account number = -1, it's because it's a white line or there were a problem ;
1284      * in all case, get the current account number */
1285     if (account_number == -1)
1286         account_number = gsb_form_get_account_number ();
1287 
1288     /* show and prepare the form */
1289     gsb_form_show (TRUE);
1290 
1291     if (!is_transaction)
1292     {
1293 	/* we need to set up the part of scheduler form here because changing the account
1294 	 * button will change the form */
1295     gsb_form_scheduler_set (transaction_number);
1296     }
1297 
1298     /* if the transaction is the white line, we set the date and necessary stuff and go away
1299      * for that use the button_press function, so clicking on a form's field or do enter/double click
1300      * on white line will do the same */
1301     if (transaction_number == -1)
1302     {
1303         GtkWidget *date_entry;
1304 
1305         /* if we can't touch the focus, it's because we just select a transaction, and
1306          * for now, select the white line, so keep the form blank */
1307         if (!grab_focus)
1308             return FALSE;
1309 
1310         date_entry = gsb_form_widget_get_widget (TRANSACTION_FORM_DATE);
1311 
1312         if (gsb_form_widget_check_empty (date_entry))
1313             /* for now,  transaction_number_in_form is NULL so can call the next function
1314              * which will prepare the form for a new transaction */
1315             gsb_form_button_press_event (date_entry, NULL, GINT_TO_POINTER (TRANSACTION_FORM_DATE));
1316 
1317         gtk_editable_select_region (GTK_EDITABLE (date_entry), 0, -1);
1318         gtk_widget_grab_focus (GTK_WIDGET (date_entry));
1319 
1320         return FALSE;
1321     }
1322 
1323     /* set the number of the transaction in the form, can be -2, -3...
1324      * for white line split
1325      * that must be defined before gsb_form_change_sensitive_buttons
1326      * because without that number, others functions cannot get the account
1327      * number for an execution of scheduled in the home page
1328      * but after gsb_form_show because it will set that value to 0...*/
1329     g_object_set_data (G_OBJECT (transaction_form),
1330 					   "transaction_number_in_form",
1331 					   GINT_TO_POINTER (transaction_number));
1332 
1333     gsb_form_change_sensitive_buttons (TRUE);
1334 
1335     /* by default, a function before changed all the form from non sensitive to sensitive,
1336      * but for split or split child, some widgets need to be unsensitive/hidden */
1337     gsb_form_set_sensitive (is_split, mother_number);
1338 
1339     /* fill what is necessary in the form */
1340     tmp_list = gsb_form_widget_get_list ();
1341     while (tmp_list)
1342     {
1343         struct_element *element;
1344 
1345         element = tmp_list->data;
1346         if (mother_number && transaction_number < 0)
1347         {
1348             /* we are on a split white line, we fill only few fields
1349              * with the element_number of the mother */
1350             switch (element->element_number)
1351             {
1352 				case TRANSACTION_FORM_DATE:
1353 				case TRANSACTION_FORM_VALUE_DATE:
1354 				case TRANSACTION_FORM_PARTY:
1355 				case TRANSACTION_FORM_TYPE:
1356 				case TRANSACTION_FORM_CHEQUE:
1357 				case TRANSACTION_FORM_DEVISE:
1358 				case TRANSACTION_FORM_BANK:
1359 					gsb_form_fill_element (element->element_number,
1360 										   account_number,
1361 										   mother_number,
1362 										   is_transaction);
1363 					break;
1364             }
1365         }
1366         else
1367             /* normal transaction */
1368             gsb_form_fill_element (element->element_number, account_number, transaction_number, is_transaction);
1369 
1370         tmp_list = tmp_list->next;
1371     }
1372 
1373     /* for a transaction, need to check if marked R, and do some stuff with that */
1374     if (is_transaction)
1375     {
1376         gint contra_transaction_number;
1377 
1378         /* get the contra transaction */
1379         contra_transaction_number = gsb_data_transaction_get_contra_transaction_number (transaction_number);
1380 
1381         /* if the transaction is marked R and splitted, cannot change the amounts */
1382         if (gsb_data_transaction_get_marked_transaction (transaction_number) == OPERATION_RAPPROCHEE
1383          && mother_number == 0)
1384         {
1385             gtk_widget_set_sensitive (gsb_form_widget_get_widget (TRANSACTION_FORM_DEBIT), FALSE);
1386             gtk_widget_set_sensitive (gsb_form_widget_get_widget (TRANSACTION_FORM_CREDIT), FALSE);
1387         }
1388 
1389         /* if the contra transaction is marked R, cannot change category and amounts */
1390         if (contra_transaction_number > 0
1391          && gsb_data_transaction_get_marked_transaction (contra_transaction_number) == OPERATION_RAPPROCHEE)
1392         {
1393             gtk_widget_set_sensitive (gsb_form_widget_get_widget (TRANSACTION_FORM_DEBIT), FALSE);
1394             gtk_widget_set_sensitive (gsb_form_widget_get_widget (TRANSACTION_FORM_CREDIT), FALSE);
1395 
1396             /* if it's a transfer, we cannot change too the category */
1397             gtk_widget_set_sensitive (gsb_form_widget_get_widget (TRANSACTION_FORM_CATEGORY), FALSE);
1398         }
1399     }
1400 
1401     /* we take focus only if asked */
1402     if (grab_focus)
1403     {
1404         GtkWidget *widget;
1405 
1406         /* the form is full, if it's not a split, we give the focus to the date
1407          * else, we give the focus to the first free form element */
1408         if (mother_number)
1409             focus_to = gsb_form_widget_next_element (account_number, TRANSACTION_FORM_DATE, GTK_DIR_RIGHT);
1410         else
1411             focus_to = TRANSACTION_FORM_DATE;
1412 
1413         widget = gsb_form_widget_get_widget (focus_to);
1414         gtk_editable_select_region (GTK_EDITABLE (widget), 0, -1);
1415         if (!is_transaction && gtk_widget_is_focus (widget))
1416             gsb_form_widget_entry_get_focus (widget, NULL, GINT_TO_POINTER (focus_to));
1417         else
1418             gtk_widget_grab_focus (widget);
1419     }
1420 
1421     return FALSE;
1422 }
1423 
1424 /**
1425  * set the form sensitive, depend if it's a split child or normal transaction
1426  * in fact, the form is always sensitive, so just unsensitive what is needed for split child
1427  * or split mother
1428  *
1429  * \param split TRUE if is split of transaction (mother)
1430  * \param split_child TRUE if it's a child
1431  *
1432  * \return FALSE
1433  **/
gsb_form_set_sensitive(gboolean split,gboolean split_child)1434 gboolean gsb_form_set_sensitive (gboolean split,
1435 								 gboolean split_child)
1436 {
1437     GSList *tmp_list;
1438 
1439     /* for now, come here only for split or split child */
1440     if (!split && !split_child)
1441 		return FALSE;
1442 
1443     tmp_list = gsb_form_widget_get_list ();
1444     while (tmp_list)
1445     {
1446         struct_element *element;
1447 
1448         element = tmp_list->data;
1449 
1450         /* for a split, hide the exercice and the budget */
1451         if (split)
1452         {
1453             switch (element->element_number)
1454             {
1455 				case TRANSACTION_FORM_EXERCICE:
1456 				case TRANSACTION_FORM_BUDGET:
1457 					gtk_widget_set_sensitive (element->element_widget, FALSE);
1458 					break;
1459             }
1460         }
1461 
1462         /* for a child of split, cannot change the date, payee... */
1463         if (split_child)
1464         {
1465             /* mixed widgets for transactions and scheduled transactions */
1466             switch (element->element_number)
1467             {
1468 				case TRANSACTION_FORM_DATE:
1469 				case TRANSACTION_FORM_VALUE_DATE:
1470 				case TRANSACTION_FORM_PARTY:
1471 				case TRANSACTION_FORM_DEVISE:
1472 				case TRANSACTION_FORM_CHANGE:
1473 				case TRANSACTION_FORM_TYPE:
1474 				case TRANSACTION_FORM_CHEQUE:
1475 				case TRANSACTION_FORM_BANK:
1476 					gtk_widget_set_sensitive (element->element_widget, FALSE);
1477 					break;
1478             }
1479             /* specific widgets for scheduled transactions */
1480             gtk_widget_hide (gsb_form_get_scheduler_part ());
1481         }
1482         tmp_list = tmp_list->next;
1483     }
1484 
1485     return FALSE;
1486 }
1487 
1488 /**
1489  * fill the element_number in the form according to the given transaction or scheduled transaction
1490  * this is the best way to fill the form : send here the element number, the transaction or scheduled number
1491  * and this function will fill the good element with the good value in the form
1492  *
1493  * \param element_number 		the element we want to fill
1494  * \param account_number 		used for currency and method of payment
1495  * \param transaction_number 	the number of transaction or scheduled transaction to fill the element
1496  * \param is_transaction 		TRUE if transaction, FALSE if scheduled transaction
1497  *
1498  * \return
1499  **/
gsb_form_fill_element(gint element_number,gint account_number,gint transaction_number,gboolean is_transaction)1500 void gsb_form_fill_element (gint element_number,
1501 							gint account_number,
1502 							gint transaction_number,
1503 							gboolean is_transaction)
1504 {
1505     GtkWidget *widget;
1506     GtkWidget *tmp_widget;
1507     gchar *char_tmp;
1508     gint number;
1509 	gint payment_number;
1510 
1511     widget = gsb_form_widget_get_widget (element_number);
1512     if (!widget)
1513 	return;
1514 
1515     switch (element_number)
1516     {
1517 		case TRANSACTION_FORM_OP_NB:
1518 			char_tmp = utils_str_itoa (transaction_number);
1519 			gtk_label_set_text (GTK_LABEL (widget), char_tmp);
1520 			g_free (char_tmp);
1521 			break;
1522 
1523 		case TRANSACTION_FORM_DATE:
1524 			gsb_form_entry_get_focus (widget);
1525 			gsb_calendar_entry_set_date (widget, gsb_data_mix_get_date (transaction_number, is_transaction));
1526 			break;
1527 
1528 		case TRANSACTION_FORM_VALUE_DATE:
1529 			/* value_date only for transactions */
1530 			if (is_transaction
1531 			 && gsb_data_transaction_get_value_date (transaction_number))
1532 			{
1533 				gsb_form_entry_get_focus (widget);
1534 				gsb_calendar_entry_set_date (widget, gsb_data_transaction_get_value_date (transaction_number));
1535 			}
1536 			break;
1537 
1538 		case TRANSACTION_FORM_EXERCICE:
1539 			gsb_fyear_set_combobox_history (widget,
1540 											gsb_data_mix_get_financial_year_number
1541 											(transaction_number, is_transaction));
1542 			break;
1543 
1544 		case TRANSACTION_FORM_PARTY:
1545 			number = gsb_data_mix_get_party_number (transaction_number, is_transaction);
1546 			if (number)
1547 			{
1548 				const gchar *tmp_name;
1549 
1550 				tmp_name = gsb_data_payee_get_name (number, TRUE);
1551 				gsb_form_entry_get_focus (widget);
1552 				gtk_combofix_set_text (GTK_COMBOFIX (widget), tmp_name);
1553 			}
1554 			break;
1555 
1556 		case TRANSACTION_FORM_DEBIT:
1557 			if (gsb_data_mix_get_amount (transaction_number, is_transaction).mantissa < 0)
1558 			{
1559 				char_tmp = utils_real_get_string_with_currency (gsb_real_abs (gsb_data_mix_get_amount
1560 																			  (transaction_number, is_transaction)),
1561 																gsb_data_mix_get_currency_number
1562 																(transaction_number, is_transaction),
1563 																FALSE);
1564 
1565 				gsb_form_entry_get_focus (widget);
1566 				gtk_entry_set_text (GTK_ENTRY (widget), char_tmp);
1567 				if (gsb_data_account_get_kind (account_number) == GSB_TYPE_CASH)
1568 				{
1569 					save_entry = g_strdup (char_tmp);
1570 				}
1571 				g_free (char_tmp);
1572 			}
1573 			break;
1574 
1575 		case TRANSACTION_FORM_CREDIT:
1576 			if (gsb_data_mix_get_amount (transaction_number, is_transaction).mantissa >= 0)
1577 			{
1578 				char_tmp = utils_real_get_string_with_currency (gsb_data_mix_get_amount
1579 																(transaction_number, is_transaction),
1580 																gsb_data_mix_get_currency_number
1581 																(transaction_number, is_transaction),
1582 																FALSE);
1583 
1584 				gsb_form_entry_get_focus (widget);
1585 				gtk_entry_set_text (GTK_ENTRY (widget), char_tmp);
1586 				g_free (char_tmp);
1587 			}
1588 			break;
1589 
1590 		case TRANSACTION_FORM_CATEGORY:
1591 			if (gsb_data_mix_get_split_of_transaction (transaction_number, is_transaction))
1592 			{
1593 				/* it's a split of transaction */
1594 				gsb_form_entry_get_focus (widget);
1595 				gtk_combofix_set_text (GTK_COMBOFIX (widget), _("Split of transaction"));
1596 			}
1597 			else
1598 			{
1599 				gint contra_transaction_number;
1600 
1601 				contra_transaction_number = gsb_data_mix_get_transaction_number_transfer (transaction_number, is_transaction);
1602 				switch (contra_transaction_number)
1603 				{
1604 					case -1:
1605 						/* transfer to deleted account, not possible with scheduled */
1606 						gsb_form_entry_get_focus (widget);
1607 						gtk_combofix_set_text (GTK_COMBOFIX (widget), _("Transfer : Deleted account"));
1608 						break;
1609 					case 0:
1610 						/* normal category */
1611 						char_tmp = gsb_data_category_get_name (gsb_data_mix_get_category_number
1612 															   (transaction_number, is_transaction),
1613 															   gsb_data_mix_get_sub_category_number
1614 															   (transaction_number,is_transaction),
1615 															   NULL);
1616 						if (char_tmp)
1617 						{
1618 							gsb_form_entry_get_focus (widget);
1619 							gtk_combofix_set_text (GTK_COMBOFIX (widget), char_tmp);
1620 							g_free (char_tmp);
1621 						}
1622 						break;
1623 					default:
1624 						/* transfer */
1625 						gsb_form_entry_get_focus (widget);
1626 						char_tmp = g_strconcat (_("Transfer : "),
1627 												gsb_data_account_get_name (gsb_data_mix_get_account_number_transfer
1628 																		   (transaction_number,
1629 																			is_transaction)),
1630 												NULL);
1631 						gtk_combofix_set_text (GTK_COMBOFIX (widget), char_tmp);
1632 						g_free (char_tmp);
1633 				}
1634 			}
1635 			break;
1636 
1637 		case TRANSACTION_FORM_BUDGET:
1638 			char_tmp = gsb_data_budget_get_name (gsb_data_mix_get_budgetary_number
1639 												 (transaction_number, is_transaction),
1640 												 gsb_data_mix_get_sub_budgetary_number
1641 												 (transaction_number, is_transaction),
1642 												 NULL);
1643 			if (char_tmp)
1644 			{
1645 				gsb_form_entry_get_focus (widget);
1646 				gtk_combofix_set_text (GTK_COMBOFIX (widget), char_tmp);
1647 				g_free (char_tmp);
1648 			}
1649 			break;
1650 
1651 		case TRANSACTION_FORM_NOTES:
1652 			if (gsb_data_mix_get_notes (transaction_number, is_transaction))
1653 			{
1654 				gsb_form_entry_get_focus (widget);
1655 				gtk_entry_set_text (GTK_ENTRY (widget), gsb_data_mix_get_notes (transaction_number, is_transaction));
1656 			}
1657 			break;
1658 
1659 		case TRANSACTION_FORM_TYPE:
1660 			/* one small thing here : normally if the transaction is a debit, we set the
1661 			 * negative payment method. but the problem : if we are on a child of split,
1662 			 * that child contains the payment method of the mother, and if the child has not
1663 			 * the same sign of the mother (rare but possible), grisbi will not find the good payment
1664 			 * method for that child because it's not on the good sign... so for a child of split,
1665 			 * we set the payment box of the mother */
1666 			number = gsb_data_mix_get_mother_transaction_number (transaction_number, is_transaction);
1667 			if (!number)
1668 				/* it's not a child split, so set number to transaction_number */
1669 				number = transaction_number;
1670 
1671 			/* ok, now number contains either the transaction_number, either the mother transaction number,
1672 			 * we can check the sign with it */
1673 			payment_number = gsb_data_mix_get_method_of_payment_number (transaction_number, is_transaction);
1674 			if (gsb_data_mix_get_amount (number, is_transaction).mantissa < 0)
1675 			{
1676 				gsb_payment_method_create_combo_list (widget, GSB_PAYMENT_DEBIT, account_number, 0, FALSE);
1677 				if (payment_number)
1678 				{
1679 					gsb_payment_method_set_combobox_history (widget, payment_number, FALSE);
1680 				}
1681 			}
1682 			else
1683 			{
1684 				gsb_payment_method_create_combo_list (widget, GSB_PAYMENT_CREDIT, account_number, 0, FALSE);
1685 				if (payment_number)
1686 				{
1687 					gsb_payment_method_set_combobox_history (widget, payment_number, FALSE);
1688 				}
1689 			}
1690 			/* don't show the cheque entry for a child of split */
1691 			tmp_widget = gsb_form_widget_get_widget (TRANSACTION_FORM_CHEQUE);
1692 			if (gtk_widget_get_visible (widget))
1693 			{
1694 				/* we show the cheque entry only for transactions */
1695 				if (payment_number && is_transaction)
1696 				{
1697 					gboolean check_entry = FALSE;
1698 
1699 					if (transaction_number == - 1)
1700 						check_entry = TRUE;
1701 
1702 					if (gsb_payment_method_set_combobox_history (widget, payment_number, check_entry)
1703 						&& gsb_data_payment_get_show_entry (payment_number)
1704 						&& !gsb_data_mix_get_mother_transaction_number (transaction_number, is_transaction))
1705 					{
1706 						const gchar *tmp_content;
1707 
1708 						tmp_content = gsb_data_transaction_get_method_of_payment_content (transaction_number);
1709 						gsb_form_entry_get_focus (tmp_widget);
1710 						if (tmp_content)
1711 							gtk_entry_set_text (GTK_ENTRY (tmp_widget), tmp_content);
1712 
1713 						gtk_widget_show (tmp_widget);
1714 					}
1715 				}
1716 				else
1717 					gtk_widget_hide (tmp_widget);
1718 			}
1719 			else
1720 				gtk_widget_hide (tmp_widget);
1721 			break;
1722 
1723 		case TRANSACTION_FORM_DEVISE:
1724 			number = gsb_data_mix_get_currency_number (transaction_number, is_transaction);
1725 
1726 			gsb_currency_set_combobox_history (widget, number);
1727 			if (transaction_number < 1)
1728 				gsb_currency_init_exchanges ();
1729 
1730 			if (is_transaction)
1731 			{
1732 				if (gsb_data_transaction_get_marked_transaction (transaction_number) == OPERATION_RAPPROCHEE)
1733 				{
1734 					gtk_widget_set_sensitive (widget, FALSE);
1735 					gtk_widget_hide (gsb_form_widget_get_widget (TRANSACTION_FORM_CHANGE));
1736 				}
1737 			}
1738 			break;
1739 
1740 		case TRANSACTION_FORM_BANK:
1741 			if (gsb_data_mix_get_bank_references (transaction_number, is_transaction))
1742 			{
1743 				gsb_form_entry_get_focus (widget);
1744 				gtk_entry_set_text (GTK_ENTRY (widget),
1745 									gsb_data_mix_get_bank_references (transaction_number, is_transaction));
1746 			}
1747 			break;
1748 
1749 		case TRANSACTION_FORM_VOUCHER:
1750 			if (gsb_data_mix_get_voucher (transaction_number, is_transaction))
1751 			{
1752 				gsb_form_entry_get_focus (widget);
1753 				gtk_entry_set_text (GTK_ENTRY (widget),
1754 									gsb_data_mix_get_voucher (transaction_number, is_transaction));
1755 			}
1756 			break;
1757 
1758 		case TRANSACTION_FORM_CONTRA:
1759 			if (gsb_data_mix_get_transaction_number_transfer (transaction_number, is_transaction) > 0)
1760 			{
1761 				number = gsb_data_mix_get_account_number_transfer (transaction_number, is_transaction);
1762 
1763 				if (gsb_data_mix_get_amount (transaction_number, is_transaction).mantissa < 0)
1764 					gsb_payment_method_create_combo_list (widget, GSB_PAYMENT_CREDIT, number, 0, TRUE);
1765 				else
1766 					gsb_payment_method_create_combo_list (widget, GSB_PAYMENT_DEBIT, number, 0, TRUE);
1767 
1768 				if (gtk_widget_get_visible (widget))
1769 				{
1770 					gint contra_number;
1771 					gint method;
1772 
1773 					if (is_transaction)
1774 					{
1775 						contra_number = gsb_data_transaction_get_contra_transaction_number (transaction_number);
1776 						method = gsb_data_transaction_get_method_of_payment_number (contra_number);
1777 					}
1778 					else
1779 						method = gsb_data_scheduled_get_contra_method_of_payment_number (transaction_number);
1780 
1781 					gsb_payment_method_set_combobox_history (widget, method, FALSE);
1782 				}
1783 			}
1784 			else
1785 				gtk_widget_hide (widget);
1786 		break;
1787 
1788 		case TRANSACTION_FORM_MODE:
1789 			if (!is_transaction)
1790 			{
1791 				if (gsb_data_mix_get_split_of_transaction (transaction_number, is_transaction))
1792 				{
1793 					if (gsb_data_mix_get_automatic_transaction (transaction_number, is_transaction))
1794 						gtk_label_set_text (GTK_LABEL (widget), _("Auto"));
1795 					else
1796 						gtk_label_set_text (GTK_LABEL (widget), _("Manual"));
1797 				}
1798 			}
1799 			break;
1800     }
1801 }
1802 
1803 /**
1804  * Positionne le formulaire quand l'expandeur montre le widget enfant
1805  *
1806  * \param expander.
1807  *
1808  * \return
1809  **/
gsb_form_expander_is_extanded(GtkWidget * expander)1810 void gsb_form_expander_is_extanded (GtkWidget *expander)
1811 {
1812 	GtkWidget *date_entry;
1813 	GrisbiAppConf *a_conf;
1814 
1815 	a_conf = (GrisbiAppConf *) grisbi_app_get_a_conf ();
1816 
1817 	gsb_form_show (TRUE);
1818 	a_conf->formulaire_toujours_affiche = TRUE;
1819 	gsb_form_widget_set_focus (TRANSACTION_FORM_DATE);
1820 	date_entry = gsb_form_widget_get_widget (TRANSACTION_FORM_DATE);
1821 	gsb_form_widget_set_empty (date_entry, TRUE);
1822 	gsb_form_button_press_event (date_entry, NULL, GINT_TO_POINTER (TRANSACTION_FORM_DATE));
1823 	gtk_widget_grab_focus (GTK_WIDGET (date_entry));
1824 	gtk_editable_set_position (GTK_EDITABLE (date_entry), -1);
1825 }
1826 
1827 /**
1828  * show the form, detect automaticly what we need to show, even for transactions,
1829  * scheduled_transactions and the buttons valid/cancel
1830  *
1831  * \param show 		TRUE if we want to automatickly turn on the expander,
1832  *					FALSE if we don't want, so just let it
1833  *
1834  * \return FALSE
1835  **/
gsb_form_show(gboolean show)1836 gboolean gsb_form_show (gboolean show)
1837 {
1838 	GtkWidget *form_expander;
1839     gint origin;
1840 
1841     devel_debug_int (show);
1842 	form_expander = grisbi_win_get_form_expander ();
1843 
1844     origin = gsb_form_get_origin ();
1845 
1846     /* show or hide the scheduler part */
1847     switch (origin)
1848     {
1849 		case ORIGIN_VALUE_OTHER:
1850 		case ORIGIN_VALUE_HOME:
1851 			return FALSE;
1852 			break;
1853 
1854 		case ORIGIN_VALUE_SCHEDULED:
1855 			gsb_form_scheduler_set_frequency (2);
1856 			gtk_widget_show (form_scheduled_part);
1857 			break;
1858 
1859 		default:
1860 			gtk_widget_hide (form_scheduled_part);
1861     }
1862 
1863     gsb_form_clean (origin);
1864     gtk_widget_show (form_transaction_part);
1865 	grisbi_win_form_expander_show_frame ();
1866 
1867 	if (!grisbi_win_form_expander_is_expanded () && show)
1868 		gtk_expander_set_expanded (GTK_EXPANDER (form_expander), TRUE);
1869 
1870     return FALSE;
1871 }
1872 
1873 /**
1874  * return the current account number according,
1875  * if we are on transactions list, return the current account,
1876  * if we are on scheduling list, return the selected account on button
1877  * if we are on welcome page, it's only if it's an execution of scheduled
1878  *
1879  * \param origin
1880  *
1881  * \return the account number or -2 if problem (-1 is reserved to get account from the button)
1882  **/
gsb_form_get_account_number(void)1883 gint gsb_form_get_account_number (void)
1884 {
1885     gint account_number;
1886     gint origin;
1887 
1888     origin = gsb_form_get_origin ();
1889 
1890     switch (origin)
1891     {
1892 		case ORIGIN_VALUE_OTHER:
1893 			return -2;
1894 			break;
1895 
1896 		case ORIGIN_VALUE_HOME:
1897 			/* we are on the home page, we need to check if the form is showed,
1898 			 * if yes, we get the account number of the scheduled showed in that form */
1899 			if (transaction_form && gtk_widget_get_visible (transaction_form))
1900 			{
1901 				gint scheduled_number;
1902 
1903 				scheduled_number = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (transaction_form),
1904 																	   "transaction_number_in_form"));
1905 				if (!scheduled_number)
1906 					return -2;
1907 				account_number = gsb_data_scheduled_get_account_number (scheduled_number);
1908 			}
1909 			else
1910 				return -2;
1911 			break;
1912 
1913 		case ORIGIN_VALUE_SCHEDULED:
1914 			account_number = gsb_form_scheduler_get_account ();
1915 			break;
1916 
1917 		default:
1918 			account_number = origin;
1919     }
1920     return account_number;
1921 }
1922 
1923 /**
1924  * get the page where we are and return a number according that page :
1925  * ORIGIN_VALUE_OTHER : we are neither on scheduled transactions, neither transactions list, neither welcome page
1926  * ORIGIN_VALUE_HOME : we are on the welcome page
1927  * ORIGIN_VALUE_SCHEDULED : we are on the scheduled transactions
1928  *  0 to x : the account number where we are
1929  *  that function is called at each beginning to know where we are and what to do...
1930  *
1931  *  \param
1932  *
1933  *  \return the number where we are
1934  **/
gsb_form_get_origin(void)1935 gint gsb_form_get_origin (void)
1936 {
1937     gint account_number;
1938 
1939     switch (gsb_gui_navigation_get_current_page())
1940     {
1941 		case GSB_ACCOUNT_PAGE:
1942 			account_number = gsb_gui_navigation_get_current_account ();
1943 
1944 			if (account_number != -1)
1945 				return account_number;
1946 			break;
1947 
1948 		case GSB_SCHEDULER_PAGE:
1949 				return ORIGIN_VALUE_SCHEDULED;
1950 			break;
1951 
1952 		case GSB_HOME_PAGE:
1953 				return ORIGIN_VALUE_HOME;
1954 			break;
1955     }
1956     return ORIGIN_VALUE_OTHER;
1957 }
1958 
1959 /**
1960  * clean the form according to the account_number
1961  * and set the default values
1962  *
1963  * \param account number
1964  *
1965  * \return FALSE
1966  **/
gsb_form_clean(gint account_number)1967 gboolean gsb_form_clean (gint account_number)
1968 {
1969     GSList *tmp_list;
1970 
1971     devel_debug_int (account_number);
1972 
1973     /* clean the transactions widget */
1974     tmp_list = gsb_form_widget_get_list ();
1975     while (tmp_list)
1976     {
1977 		struct_element *element;
1978 
1979 		element = tmp_list->data;
1980 
1981 		/* better to protect here if widget != NULL (bad experience...) */
1982 		if (element->element_widget)
1983 		{
1984 			/* some widgets can be set unsensitive because of the children of splits,
1985 			 * so resensitive all to be sure */
1986 			gtk_widget_set_sensitive (element->element_widget, TRUE);
1987 
1988 			switch (element->element_number)
1989 			{
1990 				case TRANSACTION_FORM_DATE:
1991 					gsb_form_widget_set_empty (element->element_widget, TRUE);
1992 					gtk_entry_set_text (GTK_ENTRY (element->element_widget), _("Date"));
1993 					gtk_widget_set_size_request (element->element_widget, FORM_COURT_WIDTH, -1);
1994 					break;
1995 
1996 				case TRANSACTION_FORM_VALUE_DATE:
1997 					gsb_form_widget_set_empty (element->element_widget, TRUE);
1998 					gtk_entry_set_text (GTK_ENTRY (element->element_widget), _("Value date"));
1999 					gtk_widget_set_size_request (element->element_widget, FORM_COURT_WIDTH, -1);
2000 					break;
2001 
2002 				case TRANSACTION_FORM_EXERCICE:
2003 					/* editing a transaction can show some fyear which shouldn't be showed,
2004 					 * so hide them here */
2005 					gsb_fyear_update_fyear_list ();
2006 
2007 					/* set the combo_box on 'Automatic' */
2008 					gsb_fyear_set_combobox_history (element->element_widget, 0);
2009 					gtk_widget_set_sensitive (GTK_WIDGET (element->element_widget), FALSE);
2010 					gtk_widget_set_size_request (element->element_widget, FORM_COURT_WIDTH, -1);
2011 					break;
2012 
2013 				case TRANSACTION_FORM_PARTY:
2014 					gsb_form_widget_set_empty (element->element_widget, TRUE);
2015 					gtk_combofix_set_text (GTK_COMBOFIX (element->element_widget), _("Payee"));
2016 					gtk_widget_set_size_request (element->element_widget, FORM_LONG_WIDTH, -1);
2017 					break;
2018 
2019 				case TRANSACTION_FORM_DEBIT:
2020 					gsb_form_widget_set_empty (element->element_widget, TRUE);
2021 					gtk_entry_set_text (GTK_ENTRY (element->element_widget), _("Debit"));
2022 					gtk_widget_set_size_request (element->element_widget, FORM_COURT_WIDTH, -1);
2023 					break;
2024 
2025 				case TRANSACTION_FORM_CREDIT:
2026 					gsb_form_widget_set_empty (element->element_widget, TRUE);
2027 					gtk_entry_set_text (GTK_ENTRY (element->element_widget), _("Credit"));
2028 					gtk_widget_set_size_request (element->element_widget, FORM_COURT_WIDTH, -1);
2029 					break;
2030 
2031 				case TRANSACTION_FORM_CATEGORY:
2032 					gsb_form_widget_set_empty (element->element_widget, TRUE);
2033 					gtk_combofix_set_text (GTK_COMBOFIX (element->element_widget), _("Categories : Sub-categories"));
2034 					gtk_widget_set_size_request (element->element_widget, FORM_LONG_WIDTH, -1);
2035 					break;
2036 
2037 				case TRANSACTION_FORM_FREE:
2038 					break;
2039 
2040 				case TRANSACTION_FORM_BUDGET:
2041 					gsb_form_widget_set_empty (element->element_widget, TRUE);
2042 					gtk_combofix_set_text (GTK_COMBOFIX (element->element_widget), _("Budgetary line"));
2043 					gtk_widget_set_size_request (element->element_widget, FORM_LONG_WIDTH, -1);
2044 					break;
2045 
2046 				case TRANSACTION_FORM_NOTES:
2047 					gsb_form_widget_set_empty (element->element_widget, TRUE);
2048 					gtk_entry_set_text (GTK_ENTRY (element->element_widget), _("Notes"));
2049 					gtk_widget_set_size_request (element->element_widget, FORM_LONG_WIDTH, -1);
2050 					break;
2051 
2052 				case TRANSACTION_FORM_TYPE:
2053 					{
2054 						gint payment_number;
2055 
2056 						payment_number = gsb_data_account_get_default_debit (account_number);
2057 						gsb_payment_method_create_combo_list (element->element_widget,
2058 															  GSB_PAYMENT_DEBIT,
2059 															  account_number,
2060 															  0,
2061 															  FALSE);
2062 						gsb_payment_method_set_payment_position (element->element_widget, payment_number);
2063 						gtk_widget_set_sensitive (GTK_WIDGET (element->element_widget), FALSE);
2064 						gsb_payment_method_show_cheque_entry_if_necessary (payment_number);
2065 						gtk_widget_set_size_request (element->element_widget, FORM_COURT_WIDTH, -1);
2066 						break;
2067 					}
2068 
2069 				case TRANSACTION_FORM_CONTRA:
2070 					gtk_widget_hide (element->element_widget);
2071 				gtk_widget_set_size_request (element->element_widget, FORM_COURT_WIDTH, -1);
2072 					break;
2073 
2074 				case TRANSACTION_FORM_CHEQUE:
2075 					{
2076 						gint payment_number;
2077 
2078 						payment_number = gsb_data_account_get_default_debit (account_number);
2079 						if (!gsb_data_payment_get_automatic_numbering (payment_number))
2080 						{
2081 							gtk_entry_set_text (GTK_ENTRY (element->element_widget), _("Cheque/Transfer number"));
2082 							gsb_form_widget_set_empty (element->element_widget, TRUE);
2083 						}
2084 						gtk_widget_set_size_request (element->element_widget, FORM_COURT_WIDTH, -1);
2085 						break;
2086 					}
2087 
2088 				case TRANSACTION_FORM_DEVISE:
2089 					g_signal_handlers_block_by_func (G_OBJECT (element->element_widget),
2090 													 G_CALLBACK (gsb_form_transaction_currency_changed),
2091 													 NULL);
2092 					gsb_currency_set_combobox_history (element->element_widget,
2093 													   gsb_data_account_get_currency (account_number));
2094 					gtk_widget_set_sensitive (GTK_WIDGET (element->element_widget), FALSE);
2095 					g_signal_handlers_unblock_by_func (G_OBJECT (element->element_widget),
2096 													   G_CALLBACK (gsb_form_transaction_currency_changed),
2097 													   NULL);
2098 			gtk_widget_set_size_request (element->element_widget, FORM_COURT_WIDTH, -1);
2099 					break;
2100 
2101 				case TRANSACTION_FORM_CHANGE:
2102 					gtk_widget_hide (element->element_widget);
2103 			gtk_widget_set_size_request (element->element_widget, FORM_COURT_WIDTH, -1);
2104 					break;
2105 
2106 				case TRANSACTION_FORM_BANK:
2107 					gsb_form_widget_set_empty (element->element_widget, TRUE);
2108 					gtk_entry_set_text (GTK_ENTRY (element->element_widget), _("Bank references"));
2109 			gtk_widget_set_size_request (element->element_widget, FORM_COURT_WIDTH, -1);
2110 					break;
2111 
2112 				case TRANSACTION_FORM_VOUCHER:
2113 					gsb_form_widget_set_empty (element->element_widget, TRUE);
2114 					gtk_entry_set_text (GTK_ENTRY (element->element_widget), _("Voucher"));
2115 			gtk_widget_set_size_request (element->element_widget, FORM_COURT_WIDTH, -1);
2116 					break;
2117 
2118 				case TRANSACTION_FORM_OP_NB:
2119 					gtk_label_set_text (GTK_LABEL (element->element_widget), NULL);
2120 			gtk_widget_set_size_request (element->element_widget, FORM_COURT_WIDTH, -1);
2121 					break;
2122 
2123 				case TRANSACTION_FORM_MODE:
2124 					gtk_label_set_text (GTK_LABEL (element->element_widget), NULL);
2125 			gtk_widget_set_size_request (element->element_widget, FORM_COURT_WIDTH, -1);
2126 					break;
2127 			}
2128 		}
2129 		tmp_list = tmp_list->next;
2130     }
2131     g_object_set_data (G_OBJECT (transaction_form), "transaction_number_in_form", NULL);
2132 
2133     /* don't show the recover button */
2134     gtk_widget_hide (form_button_recover_split);
2135 
2136     /* unsensitive the valid and cancel buttons */
2137     gtk_widget_set_sensitive (GTK_WIDGET (form_button_valid), FALSE);
2138     gtk_widget_set_sensitive (GTK_WIDGET (form_button_cancel), FALSE);
2139 
2140     return FALSE;
2141 }
2142 
2143 /**
2144  * Determine element is expandable or not in a GtkTable.
2145  *
2146  * \param element_number
2147  *
2148  * \return
2149  */
gsb_form_get_element_expandable(gint element_number)2150 gint gsb_form_get_element_expandable (gint element_number)
2151 {
2152     switch (element_number)
2153     {
2154 		case TRANSACTION_FORM_OP_NB:
2155 		case TRANSACTION_FORM_MODE:
2156 			return GTK_SHRINK;
2157 
2158 		default:
2159 			return GTK_EXPAND | GTK_FILL;
2160     }
2161 }
2162 
2163 /**
2164  * called when an entry get the focus, if the entry is free,
2165  * set it normal and erase the help content
2166  *
2167  * \param entry
2168  * \param ev
2169  *
2170  * \return FALSE
2171  **/
gsb_form_entry_get_focus(GtkWidget * entry)2172 gboolean gsb_form_entry_get_focus (GtkWidget *entry)
2173 {
2174     /* the entry can be a combofix or a combo_box or a real entry */
2175 	if (GTK_IS_ENTRY (entry))
2176 	{
2177         if (gsb_form_widget_check_empty (entry))
2178         {
2179             gtk_entry_set_text (GTK_ENTRY (entry), "");
2180             gsb_form_widget_set_empty (entry, FALSE);
2181         }
2182 	}
2183 	else
2184 	{
2185 		if (gsb_form_widget_check_empty (entry))
2186 		{
2187 			gtk_combofix_set_text (GTK_COMBOFIX (entry), "");
2188 			gsb_form_widget_set_empty (entry, FALSE);
2189 		}
2190 	}
2191 
2192 	/* sensitive the valid and cancel buttons */
2193     gtk_widget_set_sensitive (GTK_WIDGET (form_button_valid), TRUE);
2194     gtk_widget_set_sensitive (GTK_WIDGET (form_button_cancel), TRUE);
2195 
2196     return FALSE;
2197 }
2198 
2199 /**
2200  * called when an entry lose the focus
2201  *
2202  * \param entry
2203  * \param ev
2204  * \param ptr_origin 	a pointer gint which is the number of the element
2205  *
2206  * \return FALSE
2207  **/
gsb_form_entry_lose_focus(GtkWidget * entry,GdkEventFocus * ev,gint * ptr_origin)2208 gboolean gsb_form_entry_lose_focus (GtkWidget *entry,
2209                         GdkEventFocus *ev,
2210                         gint *ptr_origin)
2211 {
2212     const gchar *string;
2213     gint element_number;
2214     GtkWidget *widget;
2215     GtkWidget *tmp_widget;
2216     gint account_number;
2217     gint transaction_number;
2218     gint payment_number;
2219 
2220     //~ devel_debug (NULL);
2221 
2222 	/* still not found, if change the content of the form, something come in entry
2223      * which is nothing, so protect here */
2224     if (!GTK_IS_WIDGET (entry) || !GTK_IS_ENTRY (entry))
2225 		return FALSE;
2226 
2227 	/* remove the selection */
2228     gtk_editable_select_region (GTK_EDITABLE (entry), 0, 0);
2229     element_number = GPOINTER_TO_INT (ptr_origin);
2230     account_number = gsb_form_get_account_number ();
2231     transaction_number = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (gsb_form_get_form_widget ()),
2232 															 "transaction_number_in_form"));
2233 
2234 	/* sometimes the combofix popus stays showed, so remove here */
2235     if (element_number == TRANSACTION_FORM_PARTY
2236      || element_number == TRANSACTION_FORM_CATEGORY
2237      || element_number == TRANSACTION_FORM_BUDGET)
2238     {
2239         widget = gsb_form_widget_get_widget (element_number);
2240 		if (GTK_IS_COMBOFIX (widget))
2241         	gtk_combofix_hide_popup (GTK_COMBOFIX (widget));
2242     }
2243 
2244     /* string will be filled only if the field is empty */
2245     string = NULL;
2246     switch (element_number)
2247     {
2248 		case TRANSACTION_FORM_DATE:
2249 		case TRANSACTION_FORM_VALUE_DATE:
2250 			if (!strlen (gtk_entry_get_text (GTK_ENTRY (entry))))
2251 			{
2252 				gchar *vstring;
2253 				if (save_form_date)
2254 					vstring = gsb_format_gdate (save_form_date);
2255 				else
2256 					vstring = gsb_date_today ();
2257 				gtk_entry_set_text (GTK_ENTRY (entry), vstring);
2258 				g_free (vstring);
2259 				string = NULL;
2260 			}
2261 			break;
2262 
2263 		case TRANSACTION_FORM_PARTY :
2264 			/* we complete the transaction */
2265 			if (!gsb_form_transaction_complete_form_by_payee (gtk_entry_get_text (GTK_ENTRY (entry))))
2266 				string = gsb_form_widget_get_name (TRANSACTION_FORM_PARTY);
2267 			break;
2268 
2269 		case TRANSACTION_FORM_DEBIT :
2270 			if (gsb_form_widget_amount_entry_validate (element_number) == FALSE)
2271 				return TRUE;
2272 
2273 			/* we change the payment method to adapt it for the debit */
2274 			if (strlen (gtk_entry_get_text (GTK_ENTRY (entry))))
2275 			{
2276 				/* empty the credit */
2277 				widget = gsb_form_widget_get_widget (TRANSACTION_FORM_CREDIT);
2278 				if (!gsb_form_widget_check_empty (widget))
2279 				{
2280 					gtk_entry_set_text (GTK_ENTRY (widget),
2281 										gsb_form_widget_get_name (TRANSACTION_FORM_CREDIT));
2282 					gsb_form_widget_set_empty (widget, TRUE);
2283 				}
2284 
2285 				widget = gsb_form_widget_get_widget (TRANSACTION_FORM_TYPE);
2286 
2287 				/* change the method of payment if necessary
2288 				 * (if grey, it's a child of split so do nothing) */
2289 				if (widget && gtk_widget_get_sensitive (widget))
2290 				{
2291 					/* change the signe of the method of payment and the contra */
2292 					if (gsb_payment_method_get_combo_sign (widget) == GSB_PAYMENT_CREDIT)
2293 					{
2294 						gsb_payment_method_create_combo_list (widget,
2295 															  GSB_PAYMENT_DEBIT,
2296 															  account_number,
2297 															  0,
2298 															  FALSE);
2299 						/* if there is no payment method, the last function hide it, but we have
2300 						 * to hide the cheque element too */
2301 						if (!gtk_widget_get_visible (widget))
2302 							gtk_widget_hide (gsb_form_widget_get_widget (TRANSACTION_FORM_CHEQUE));
2303 
2304 						widget = gsb_form_widget_get_widget (TRANSACTION_FORM_CONTRA);
2305 						if (widget && gtk_widget_get_visible (widget))
2306 							gsb_payment_method_create_combo_list (gsb_form_widget_get_widget
2307 																  (TRANSACTION_FORM_CONTRA),
2308 																  GSB_PAYMENT_CREDIT,
2309 																  account_number,
2310 																  0,
2311 																  TRUE);
2312 					}
2313 				}
2314 				gsb_form_check_auto_separator (entry);
2315 			}
2316 			else
2317 			{
2318 				/* si pas de nouveau débit on essaie de remettre l'ancien crédit */
2319 				gchar *vstring = gsb_form_widget_get_old_credit ();
2320 				if (vstring)
2321 				{
2322 					tmp_widget = gsb_form_widget_get_widget (TRANSACTION_FORM_CREDIT);
2323 					gtk_entry_set_text (GTK_ENTRY (tmp_widget), vstring);
2324 					gsb_form_widget_set_empty (tmp_widget, FALSE);
2325 					g_free (vstring);
2326 
2327 					widget = gsb_form_widget_get_widget (TRANSACTION_FORM_TYPE);
2328 					if (widget && gtk_widget_get_sensitive (widget))
2329 					{
2330 						/* change the signe of the method of payment and the contra */
2331 						if (gsb_payment_method_get_combo_sign (widget) == GSB_PAYMENT_DEBIT)
2332 						{
2333 							if (transaction_number == -1)
2334 								payment_number =  gsb_form_widget_get_old_credit_payment_number ();
2335 							else
2336 								payment_number = gsb_data_transaction_get_method_of_payment_number
2337 													(transaction_number);
2338 
2339 							gsb_payment_method_create_combo_list (widget,
2340 																  GSB_PAYMENT_CREDIT,
2341 																  account_number,
2342 																  0,
2343 																  FALSE);
2344 							gsb_payment_method_set_payment_position (widget, payment_number);
2345 
2346 							widget = gsb_form_widget_get_widget (TRANSACTION_FORM_CHEQUE);
2347 							if (widget && gtk_widget_get_visible (widget))
2348 							{
2349 								if (gsb_form_widget_get_old_credit_payment_content ())
2350 								{
2351 									gtk_entry_set_text (GTK_ENTRY (widget),
2352 														gsb_form_widget_get_old_credit_payment_content ());
2353 									gsb_form_widget_set_empty (widget, FALSE);
2354 								}
2355 							}
2356 
2357 							widget = gsb_form_widget_get_widget (TRANSACTION_FORM_CONTRA);
2358 							if (widget && gtk_widget_get_visible (widget))
2359 								gsb_payment_method_create_combo_list (gsb_form_widget_get_widget
2360 																	  (TRANSACTION_FORM_CONTRA),
2361 																	  GSB_PAYMENT_DEBIT,
2362 																	  account_number,
2363 																	  0,
2364 																	  TRUE);
2365 						}
2366 					}
2367 				}
2368 				string = gsb_form_widget_get_name (TRANSACTION_FORM_DEBIT);
2369 			}
2370 			break;
2371 
2372 		case TRANSACTION_FORM_CREDIT :
2373 			if (gsb_form_widget_amount_entry_validate (element_number) == FALSE)
2374 				return TRUE;
2375 			/* we change the payment method to adapt it for the debit */
2376 			if (strlen (gtk_entry_get_text (GTK_ENTRY (entry))))
2377 			{
2378 				/* empty the debit */
2379 				widget = gsb_form_widget_get_widget (TRANSACTION_FORM_DEBIT);
2380 				if (!gsb_form_widget_check_empty (widget))
2381 				{
2382 					gtk_entry_set_text (GTK_ENTRY (widget),
2383 										gsb_form_widget_get_name (TRANSACTION_FORM_DEBIT));
2384 					gsb_form_widget_set_empty (widget, TRUE);
2385 				}
2386 				widget = gsb_form_widget_get_widget (TRANSACTION_FORM_TYPE);
2387 
2388 				/* change the method of payment if necessary
2389 				 * (if grey, it's a child of split so do nothing) */
2390 				if (widget && gtk_widget_get_sensitive (widget))
2391 				{
2392 					/* change the signe of the method of payment and the contra */
2393 					if (gsb_payment_method_get_combo_sign (widget) == GSB_PAYMENT_DEBIT)
2394 					{
2395 						gsb_payment_method_create_combo_list (widget, GSB_PAYMENT_CREDIT, account_number, 0, FALSE);
2396 						/* if there is no payment method, the last function hide it, but we have
2397 						 * to hide the cheque element too */
2398 						if (!gtk_widget_get_visible (widget))
2399 							gtk_widget_hide (gsb_form_widget_get_widget (TRANSACTION_FORM_CHEQUE));
2400 
2401 						widget = gsb_form_widget_get_widget (TRANSACTION_FORM_CONTRA);
2402 						if (widget && gtk_widget_get_visible (widget))
2403 							gsb_payment_method_create_combo_list (widget, GSB_PAYMENT_DEBIT, account_number, 0, TRUE);
2404 					}
2405 				}
2406 				gsb_form_check_auto_separator (entry);
2407 			}
2408 			else
2409 			{
2410 				/* si pas de nouveau credit on essaie de remettre l'ancien débit */
2411 				gchar *vstring = gsb_form_widget_get_old_debit ();
2412 				if (vstring)
2413 				{
2414 					tmp_widget = gsb_form_widget_get_widget (TRANSACTION_FORM_DEBIT);
2415 
2416 					gtk_entry_set_text (GTK_ENTRY (tmp_widget), vstring);
2417 					gsb_form_widget_set_empty (tmp_widget, FALSE);
2418 					g_free (vstring);
2419 
2420 					widget = gsb_form_widget_get_widget (TRANSACTION_FORM_TYPE);
2421 					if (widget
2422 						 &&
2423 						 gtk_widget_get_sensitive (widget))
2424 					{
2425 						/* change the signe of the method of payment and the contra */
2426 						if (gsb_payment_method_get_combo_sign (widget) == GSB_PAYMENT_CREDIT)
2427 						{
2428 						if (transaction_number == -1)
2429 							payment_number =  gsb_form_widget_get_old_debit_payment_number ();
2430 						else
2431 							payment_number = gsb_data_transaction_get_method_of_payment_number (
2432 												transaction_number);
2433 
2434 						gsb_payment_method_create_combo_list (widget, GSB_PAYMENT_DEBIT, account_number, 0, FALSE);
2435 						gsb_payment_method_set_payment_position (widget, payment_number);
2436 
2437 						widget = gsb_form_widget_get_widget (TRANSACTION_FORM_CHEQUE);
2438 						if (widget && gtk_widget_get_visible (widget))
2439 						{
2440 							if (gsb_form_widget_get_old_debit_payment_content ())
2441 							{
2442 								gtk_entry_set_text (GTK_ENTRY (widget),
2443 													gsb_form_widget_get_old_debit_payment_content ());
2444 								gsb_form_widget_set_empty (widget, FALSE);
2445 							}
2446 						}
2447 
2448 						widget = gsb_form_widget_get_widget (TRANSACTION_FORM_CONTRA);
2449 						if (widget && gtk_widget_get_visible (widget))
2450 							gsb_payment_method_create_combo_list (gsb_form_widget_get_widget
2451 																  (TRANSACTION_FORM_CONTRA),
2452 																  GSB_PAYMENT_CREDIT,
2453 																  account_number,
2454 																  0,
2455 																  TRUE);
2456 						}
2457 					}
2458 				}
2459 				string = gsb_form_widget_get_name (TRANSACTION_FORM_CREDIT);
2460 			}
2461 			break;
2462 
2463 		case TRANSACTION_FORM_CATEGORY :
2464 			if (strlen (gtk_entry_get_text (GTK_ENTRY (entry))))
2465 			{
2466 				/* if it's a transfer, set the content of the contra combo */
2467 				if (gsb_data_form_check_for_value (TRANSACTION_FORM_CONTRA))
2468 				{
2469 					/* if it's a transfer, set the contra_method of payment menu */
2470 					gint contra_account_number;
2471 
2472 					contra_account_number = gsb_form_check_for_transfer (gtk_entry_get_text (GTK_ENTRY (entry)));
2473 					if (contra_account_number >= 0 && contra_account_number != account_number)
2474 					{
2475 						if (gsb_form_widget_check_empty (gsb_form_widget_get_widget (TRANSACTION_FORM_CREDIT)))
2476 							/* there is something in debit */
2477 							gsb_payment_method_create_combo_list (gsb_form_widget_get_widget
2478 																  (TRANSACTION_FORM_CONTRA),
2479 																  GSB_PAYMENT_CREDIT,
2480 																  contra_account_number,
2481 																  0,
2482 																  TRUE);
2483 						else
2484 							/* there is something in credit */
2485 							gsb_payment_method_create_combo_list (gsb_form_widget_get_widget
2486 																  (TRANSACTION_FORM_CONTRA),
2487 																  GSB_PAYMENT_DEBIT,
2488 																  contra_account_number,
2489 																  0,
2490 																  TRUE);
2491 					}
2492 					else
2493 						gtk_widget_hide (gsb_form_widget_get_widget (TRANSACTION_FORM_CONTRA));
2494 				}
2495 				if (strcmp (gtk_entry_get_text (GTK_ENTRY (entry)), _("Split of transaction"))
2496 				 && gtk_widget_get_visible (form_button_recover_split))
2497 				{
2498 					gtk_widget_hide (form_button_recover_split);
2499 				}
2500 			}
2501 			else
2502 				string = gsb_form_widget_get_name (TRANSACTION_FORM_CATEGORY);
2503 			break;
2504 
2505 		case TRANSACTION_FORM_CHEQUE :
2506 		case TRANSACTION_FORM_BUDGET :
2507 		case TRANSACTION_FORM_VOUCHER :
2508 		case TRANSACTION_FORM_NOTES :
2509 		case TRANSACTION_FORM_BANK :
2510 			if (!strlen (gtk_entry_get_text (GTK_ENTRY (entry))))
2511 				string = _(gsb_form_widget_get_name (element_number));
2512 			break;
2513 
2514 		default :
2515 			break;
2516     }
2517 
2518     /* if string is not NULL, the entry is empty so set the empty field to TRUE */
2519     if (string)
2520     {
2521 		switch (element_number)
2522 		{
2523 			case TRANSACTION_FORM_PARTY :
2524 			case TRANSACTION_FORM_CATEGORY :
2525 			case TRANSACTION_FORM_BUDGET :
2526 				/* need to work with the combofix to avoid some signals if we work
2527 				 * directly on the entry */
2528 				widget = gsb_form_widget_get_widget (element_number);
2529 				gtk_combofix_set_text (GTK_COMBOFIX (widget), _(string));
2530 				break;
2531 
2532 			default:
2533 				gtk_entry_set_text (GTK_ENTRY (entry), string);
2534 			break;
2535 		}
2536 		gsb_form_widget_set_empty (entry, TRUE);
2537     }
2538 
2539     return FALSE;
2540 }
2541 
2542 /**
2543  * called when leave an amount entry, il the automatic separator
2544  * is on, set the separator in the entry if there is none
2545  *
2546  * \param entry
2547  *
2548  * \return
2549  **/
gsb_form_check_auto_separator(GtkWidget * entry)2550 void gsb_form_check_auto_separator (GtkWidget *entry)
2551 {
2552     gint account_number;
2553     gchar *string;
2554     gint floating_point;
2555     gchar *tmp = NULL;
2556     gchar *mon_decimal_point;
2557     gunichar decimal_point;
2558     gsize i;
2559 
2560 	if (!etat.automatic_separator || !entry)
2561 		return;
2562 
2563     /* we need a my_strdup to permit to do the g_free later
2564      * because if strlen < floating point we need to
2565      * malloc another string */
2566     string = my_strdup (gtk_entry_get_text (GTK_ENTRY(entry)));
2567     if (!string || strlen (string) == 0)
2568 		return;
2569 
2570     account_number = gsb_form_get_account_number ();
2571     floating_point = gsb_data_currency_get_floating_point (gsb_data_account_get_currency (account_number));
2572 
2573     mon_decimal_point = gsb_locale_get_mon_decimal_point ();
2574     decimal_point = g_utf8_get_char_validated (mon_decimal_point, -1);
2575 
2576     if (g_utf8_strchr (string, -1, decimal_point))
2577     {
2578         g_free (string);
2579         g_free (mon_decimal_point);
2580         return;
2581     }
2582 
2583     /* if string is < the floating_point, increase it to have
2584      * 1 character more (to set the 0 before the .) */
2585     if ((gint) strlen(string) <= floating_point)
2586     {
2587         gchar *concat_tmp;
2588 
2589         tmp = g_malloc (floating_point - strlen(string) + 2);
2590         for (i=0 ; i<(floating_point - strlen(string) + 1) ; i++)
2591             tmp[i] = '0';
2592         tmp[floating_point - strlen(string) + 1] = 0;
2593         concat_tmp = g_strconcat (tmp, string, NULL);
2594         g_free (tmp);
2595         g_free (string);
2596         string = concat_tmp;
2597     }
2598 
2599     tmp = g_malloc ((strlen(string)+2) * sizeof (gchar));
2600 
2601     memcpy (tmp, string, strlen(string) - floating_point);
2602 
2603     i = strlen(string) - floating_point;
2604     tmp[i] = decimal_point;
2605     i++;
2606     memcpy (tmp + i, string + i - 1, floating_point);
2607     i = i + floating_point;
2608     tmp[i] = 0;
2609     gtk_entry_set_text (GTK_ENTRY (entry), tmp);
2610     g_free (tmp);
2611     g_free (string);
2612     g_free (mon_decimal_point);
2613 }
2614 
2615 /**
2616  * called when we press the button in an entry field in
2617  * the form
2618  * if "transaction_number_in_form" as data of transaction_form is NULL,
2619  * do the necessary to begin a new empty transaction
2620  *
2621  * \param entry 		which receive the signal
2622  * \param ev 			can be NULL
2623  * \param ptr_origin 	a pointer to int on the element_number
2624  *
2625  * \return FALSE
2626  **/
gsb_form_button_press_event(GtkWidget * entry,GdkEventButton * ev,gint * ptr_origin)2627 gboolean gsb_form_button_press_event (GtkWidget *entry,
2628 									  GdkEventButton *ev,
2629 									  gint *ptr_origin)
2630 {
2631     GtkWidget *widget;
2632 
2633 	/* we do the first part only if we click on the form directly, without double click or
2634      * entry in the transaction list,
2635      * in that case, transaction_number_in_form is 0 and set to -1, as a white line */
2636     if (!g_object_get_data (G_OBJECT (transaction_form), "transaction_number_in_form"))
2637     {
2638 		GtkWidget *date_entry;
2639 
2640 		/* set the new transaction number */
2641 		g_object_set_data (G_OBJECT (transaction_form), "transaction_number_in_form", GINT_TO_POINTER (-1));
2642 
2643 		/* set the current date into the date entry */
2644 		date_entry = gsb_form_widget_get_widget (TRANSACTION_FORM_DATE);
2645 		if (gsb_form_widget_check_empty (date_entry))
2646 		{
2647 			if (save_form_date)
2648 				gtk_entry_set_text (GTK_ENTRY (date_entry), gsb_format_gdate (save_form_date));
2649 			else
2650 				gtk_entry_set_text (GTK_ENTRY (date_entry), gsb_date_today ());
2651 			gsb_form_widget_set_empty (date_entry, FALSE);
2652 		}
2653 
2654 		/* set the form sensitive */
2655 		gsb_form_change_sensitive_buttons (TRUE);
2656 
2657 		/* if we are on scheduled transactions, show the scheduled part of form */
2658 		if (gsb_form_get_origin () == ORIGIN_VALUE_SCHEDULED)
2659 			gsb_form_scheduler_sensitive_buttons (TRUE);
2660 
2661 		/* set the number of cheque for the method of payment if necessary */
2662 		widget = gsb_form_widget_get_widget (TRANSACTION_FORM_TYPE);
2663 
2664 		if (widget && gtk_widget_get_visible (widget))
2665 		{
2666 			gint payment_number;
2667 			gchar* tmp_str;
2668 
2669 			payment_number = gsb_payment_method_get_selected_number (widget);
2670 			if (gsb_data_payment_get_automatic_numbering (payment_number))
2671 			{
2672 				widget = gsb_form_widget_get_widget (TRANSACTION_FORM_CHEQUE);
2673 
2674 				gsb_form_entry_get_focus (widget);
2675 
2676 				if (!strlen (gtk_entry_get_text (GTK_ENTRY (widget))))
2677 				{
2678 					tmp_str = gsb_data_payment_incremente_last_number (payment_number, 1);
2679 					gtk_entry_set_text (GTK_ENTRY (widget), tmp_str);
2680 					g_free (tmp_str);
2681 				}
2682 			}
2683 		}
2684     }
2685 
2686     return FALSE;
2687 }
2688 
2689 /**
2690  * sensitive or unsensitive all the buttons on the form
2691  *
2692  * \param sensitive TRUE or FALSE
2693  *
2694  * \return FALSE
2695  **/
gsb_form_change_sensitive_buttons(gboolean sensitive)2696 gboolean gsb_form_change_sensitive_buttons (gboolean sensitive)
2697 {
2698     if (gsb_data_form_check_for_value (TRANSACTION_FORM_TYPE))
2699 		gtk_widget_set_sensitive (GTK_WIDGET (gsb_form_widget_get_widget (TRANSACTION_FORM_TYPE)),
2700 								  sensitive);
2701 
2702     if (gsb_data_form_check_for_value (TRANSACTION_FORM_DEVISE))
2703 		gtk_widget_set_sensitive (GTK_WIDGET (gsb_form_widget_get_widget (TRANSACTION_FORM_DEVISE)),
2704 								  sensitive);
2705 
2706     if (gsb_data_form_check_for_value (TRANSACTION_FORM_EXERCICE))
2707 		gtk_widget_set_sensitive (GTK_WIDGET (gsb_form_widget_get_widget (TRANSACTION_FORM_EXERCICE)),
2708 								  sensitive);
2709     return FALSE;
2710 }
2711 
2712 /**
2713  * called when press a key on an element of the form
2714  *
2715  * \param widget 		which receive the signal
2716  * \param ev
2717  * \param ptr_origin 	a pointer number of the element
2718  *
2719  * \return FALSE
2720  **/
gsb_form_key_press_event(GtkWidget * widget,GdkEventKey * ev,gint * ptr_origin)2721 gboolean gsb_form_key_press_event (GtkWidget *widget,
2722 								   GdkEventKey *ev,
2723 								   gint *ptr_origin)
2724 {
2725     GtkWidget *widget_prov;
2726     gint account_number;
2727     gint element_number;
2728     gint element_suivant;
2729 	GrisbiAppConf *a_conf;
2730 
2731 	a_conf = (GrisbiAppConf *) grisbi_app_get_a_conf ();
2732 
2733 	element_number = GPOINTER_TO_INT (ptr_origin);
2734     account_number = gsb_form_get_account_number ();
2735 
2736     /* if a_conf->form_enter_key = 1, entry finish the transaction, else does as tab */
2737     if (!a_conf->form_enter_key && (ev->keyval == GDK_KEY_Return || ev->keyval == GDK_KEY_KP_Enter))
2738 		ev->keyval = GDK_KEY_Tab ;
2739 
2740     if (!g_object_get_data (G_OBJECT (transaction_form), "transaction_number_in_form"))
2741 	/* set the new transaction number */
2742         g_object_set_data (G_OBJECT (transaction_form), "transaction_number_in_form", GINT_TO_POINTER (-1));
2743 
2744     switch (ev->keyval)
2745     {
2746 		case GDK_KEY_1:
2747 		case GDK_KEY_2:
2748 		case GDK_KEY_3:
2749 		case GDK_KEY_4:
2750 		case GDK_KEY_5:
2751 		case GDK_KEY_6:
2752 		case GDK_KEY_7:
2753 		case GDK_KEY_8:
2754 		case GDK_KEY_9:
2755 		case GDK_KEY_0:
2756 			switch (element_number)
2757 			{
2758 				case TRANSACTION_FORM_DEBIT:
2759 					widget_prov = gsb_form_widget_get_widget (TRANSACTION_FORM_CREDIT);
2760 					if (!gsb_form_widget_check_empty (widget_prov))
2761 					{
2762 						gtk_entry_set_text (GTK_ENTRY (widget_prov),
2763 											gsb_form_widget_get_name (TRANSACTION_FORM_CREDIT));
2764 						gsb_form_widget_set_empty (widget_prov, TRUE);
2765 					}
2766 					break;
2767 				case TRANSACTION_FORM_CREDIT:
2768 					widget_prov = gsb_form_widget_get_widget (TRANSACTION_FORM_DEBIT);
2769 					if (!gsb_form_widget_check_empty (widget_prov))
2770 					{
2771 						gtk_entry_set_text (GTK_ENTRY (widget_prov),
2772 											gsb_form_widget_get_name (TRANSACTION_FORM_DEBIT));
2773 						gsb_form_widget_set_empty (widget_prov, TRUE);
2774 					}
2775 					break;
2776 			}
2777 			break;
2778 
2779 		case GDK_KEY_Escape :
2780 			gsb_form_escape_form ();
2781 			break;
2782 
2783 		case GDK_KEY_Up:
2784 			element_suivant = gsb_form_widget_next_element (account_number, element_number, GTK_DIR_UP);
2785 			gsb_form_widget_set_focus (element_suivant);
2786 			return TRUE;
2787 			break;
2788 
2789 		case GDK_KEY_Down:
2790 			element_suivant = gsb_form_widget_next_element (account_number, element_number, GTK_DIR_DOWN);
2791 			gsb_form_widget_set_focus (element_suivant);
2792 			return TRUE;
2793 			break;
2794 
2795 		case GDK_KEY_ISO_Left_Tab:
2796 			if (element_number == TRANSACTION_FORM_CREDIT
2797 				|| element_number == TRANSACTION_FORM_DEBIT)
2798 			{
2799 				if (gsb_form_widget_amount_entry_validate (element_number) == FALSE)
2800 					return TRUE;
2801 			}
2802 
2803 			element_suivant = gsb_form_widget_next_element (account_number, element_number, GTK_DIR_LEFT);
2804 
2805 			if (element_number == TRANSACTION_FORM_VALUE_DATE)
2806 			{
2807 				widget_prov = gsb_form_widget_get_widget (TRANSACTION_FORM_VALUE_DATE);
2808 
2809 				if (strlen (gtk_entry_get_text (GTK_ENTRY (widget_prov))) == 0)
2810 				{
2811 					gsb_form_widget_set_empty (widget_prov, TRUE);
2812 					gtk_entry_set_text (GTK_ENTRY (widget_prov), _("Value date"));
2813 				}
2814 				gsb_form_widget_set_focus (element_suivant);
2815 			}
2816 
2817 			gsb_form_widget_set_focus (element_suivant);
2818 			return TRUE;
2819 			break;
2820 
2821 		case GDK_KEY_Tab :
2822 			if (element_number == TRANSACTION_FORM_CREDIT
2823 				|| element_number == TRANSACTION_FORM_DEBIT)
2824 			{
2825 				if (gsb_form_widget_amount_entry_validate (element_number) == FALSE)
2826 					return TRUE;
2827 			}
2828 
2829 			element_suivant = gsb_form_widget_next_element (account_number, element_number, GTK_DIR_RIGHT);
2830 
2831 			/* if element = value_date fix the bug 578 */
2832 			if (element_number == TRANSACTION_FORM_VALUE_DATE)
2833 			{
2834 				widget_prov = gsb_form_widget_get_widget (TRANSACTION_FORM_VALUE_DATE);
2835 
2836 				if (strlen (gtk_entry_get_text (GTK_ENTRY (widget_prov))) == 0)
2837 				{
2838 					gsb_form_widget_set_empty (widget_prov, TRUE);
2839 					gtk_entry_set_text (GTK_ENTRY (widget_prov), _("Value date"));
2840 				}
2841 				gsb_form_widget_set_focus (element_suivant);
2842 			}
2843 
2844 			if (element_suivant == -2)
2845 				gsb_form_finish_edition();
2846 			else
2847 				gsb_form_widget_set_focus (element_suivant);
2848 
2849 			return TRUE;
2850 			break;
2851 
2852 		case GDK_KEY_KP_Enter :
2853 		case GDK_KEY_Return :
2854 
2855 			/* need to check here if we are performing a scheduled transaction in home page
2856 			 * or another transaction, because if we are in home page, cannot finish like that,
2857 			 * we need to finish with the dialog which contains the form */
2858 			if (gsb_gui_navigation_get_current_page () == GSB_HOME_PAGE)
2859 			{
2860 				GtkWidget *tmp_widget;
2861 
2862 				/* ok, we are on the home page. the best way is to send the response ok
2863 				 * to the dialog */
2864 				tmp_widget = widget;
2865 				do
2866 				{
2867 					tmp_widget = gtk_widget_get_parent (GTK_WIDGET (tmp_widget));
2868 				}
2869 				while (GTK_IS_WIDGET (tmp_widget) && !GTK_IS_DIALOG (tmp_widget));
2870 				gtk_dialog_response (GTK_DIALOG (tmp_widget), GTK_RESPONSE_OK);
2871 
2872 				return TRUE;
2873 			}
2874 			else
2875 			{
2876 				if (element_number == TRANSACTION_FORM_CREDIT
2877 					|| element_number == TRANSACTION_FORM_DEBIT)
2878 				{
2879 					if (gsb_form_widget_amount_entry_validate (element_number) == FALSE)
2880 						return TRUE;
2881 				}
2882 
2883 				widget_prov = gsb_form_widget_get_widget (TRANSACTION_FORM_PARTY);
2884 
2885 				if (GTK_IS_COMBOFIX (widget_prov))
2886 					gsb_form_transaction_complete_form_by_payee (gtk_combofix_get_text (GTK_COMBOFIX (widget_prov)));
2887 
2888 				gsb_form_finish_edition ();
2889 				return TRUE;
2890 			}
2891 			break;
2892 
2893 		case GDK_KEY_KP_Add:
2894 		case GDK_KEY_plus:
2895 		case GDK_KEY_equal:		/* This should make all our US users happy */
2896 			/* increase the check of 1 */
2897 			if (element_number == TRANSACTION_FORM_CHEQUE)
2898 			{
2899 				increment_decrement_champ (widget, 1);
2900 				return TRUE;
2901 			}
2902 			break;
2903 
2904 		case GDK_KEY_KP_Subtract:
2905 		case GDK_KEY_minus:
2906 			/* decrease the check of 1 */
2907 			if (element_number == TRANSACTION_FORM_CHEQUE)
2908 			{
2909 				increment_decrement_champ (widget, -1);
2910 				return TRUE;
2911 			}
2912 			break;
2913     }
2914 
2915     if (element_number == TRANSACTION_FORM_TYPE)
2916     {
2917         gint payment_number;
2918 
2919         switch (ev->keyval)
2920         {
2921 			case GDK_KEY_c:
2922 			case GDK_KEY_C:
2923 				payment_number = gsb_data_payment_get_number_by_name (_("Credit card"), account_number);
2924 				if (payment_number)
2925 					gsb_payment_method_set_combobox_history (widget, payment_number, TRUE);
2926 
2927 				return TRUE;
2928 				break;
2929 
2930 			case GDK_KEY_d:
2931 			case GDK_KEY_D:
2932 				payment_number = gsb_data_payment_get_number_by_name (_("Direct deposit"), account_number);
2933 				if (payment_number)
2934 					gsb_payment_method_set_combobox_history (widget, payment_number, TRUE);
2935 
2936 				return TRUE;
2937 				break;
2938 
2939 			case GDK_KEY_h:
2940 			case GDK_KEY_H:
2941 				payment_number = gsb_data_payment_get_number_by_name (_("Check"), account_number);
2942 				if (payment_number)
2943 					gsb_payment_method_set_combobox_history (widget, payment_number, TRUE);
2944 
2945 				return TRUE;
2946 				break;
2947 
2948 			case GDK_KEY_l:
2949 			case GDK_KEY_L:
2950 				payment_number = gsb_data_payment_get_number_by_name (_("Cash withdrawal"), account_number);
2951 				if (payment_number)
2952 					gsb_payment_method_set_combobox_history (widget, payment_number, TRUE);
2953 
2954 				return TRUE;
2955 				break;
2956 
2957 			case GDK_KEY_p:
2958 			case GDK_KEY_P:
2959 				payment_number = gsb_data_payment_get_number_by_name (_("Direct debit"), account_number);
2960 				if (payment_number)
2961 					gsb_payment_method_set_combobox_history (widget, payment_number, TRUE);
2962 
2963 				return TRUE;
2964 				break;
2965 
2966 			case GDK_KEY_t:
2967 			case GDK_KEY_T:
2968 			case GDK_KEY_v:
2969 			case GDK_KEY_V:
2970 				payment_number = gsb_data_payment_get_number_by_name (_("Transfer"), account_number);
2971 				if (payment_number)
2972 					gsb_payment_method_set_combobox_history (widget, payment_number, TRUE);
2973 
2974 				return TRUE;
2975 				break;
2976         }
2977     }
2978 
2979     return FALSE;
2980 }
2981 
2982 /**
2983  * called when the user finishes the edition of the form
2984  * this function works for transaction and scheduled transactions,
2985  * it add/modify the transaction in the form
2986  *
2987  * \param none
2988  *
2989  * \return FALSE
2990  **/
gsb_form_finish_edition(void)2991 gboolean gsb_form_finish_edition (void)
2992 {
2993     GSList *list_tmp;
2994     GSList *payee_list;
2995     gint account_number;
2996     gint mother_number;
2997     gint new_transaction;
2998     gint nbre_passage = 0;
2999     gint saved_scheduled_number = 0;
3000     gint source_transaction_number = -1;
3001     gint transaction_number;
3002     gboolean execute_scheduled = FALSE;
3003     gboolean is_transaction;
3004 
3005     devel_debug (NULL);
3006 
3007     /* get the number of the transaction, stored in the form (< 0 if new) */
3008     transaction_number = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (transaction_form),
3009 															 "transaction_number_in_form"));
3010 
3011     /* set a debug here, if transaction_number is 0, should look for where it comes */
3012     if (!transaction_number)
3013         notice_debug ("Coming in gsb_form_finish_edition with a 0 number of transaction. "
3014 					  "This is a bug,\n"
3015 					  "please try to do it again and report the bug.");
3016 
3017     account_number = gsb_form_get_account_number ();
3018 
3019     /* check if we finish a transaction or a scheduled transaction
3020      * we have to decide if it's a transaction or a scheduled transaction,
3021      * and if it's a scheduled, if we execute it (and create transaction) or work on it*/
3022     if (gsb_form_get_origin () == ORIGIN_VALUE_SCHEDULED
3023 		|| gsb_form_get_origin () == ORIGIN_VALUE_HOME)
3024     {
3025         if (g_object_get_data (G_OBJECT (transaction_form), "execute_scheduled"))
3026         {
3027            /* we want to execute the scheduled transaction */
3028            is_transaction = TRUE;
3029            execute_scheduled = TRUE;
3030 
3031            /* we need to keep the number of scheduled, to check later if there is
3032             * some children and modifie the scheduled transaction */
3033            saved_scheduled_number = transaction_number;
3034            /* as it's a new transaction, do the same as a white line */
3035            transaction_number = -1;
3036         }
3037         else
3038             is_transaction = FALSE;
3039     }
3040     else
3041         is_transaction = TRUE;
3042 
3043     /* a new transaction has a number < 0
3044      * -1 for the general white line
3045      *  -2, -3, ... for the white lines of scheduled transactions
3046      *  or it's an execution of scheduled transaction */
3047     if (transaction_number < 0)
3048         new_transaction = 1;
3049     else
3050         new_transaction = 0;
3051 
3052     /* check if the datas are ok */
3053     if (!gsb_form_validate_form_transaction (transaction_number, is_transaction))
3054         return FALSE;
3055 
3056     /* if the party is a report, we make as transactions as the number of parties in the
3057      * report. So we create a list with the party's numbers or -1 if it's a normal
3058      * party */
3059     payee_list = gsb_form_transaction_get_parties_list_from_report ();
3060 
3061     /* now we go throw the list */
3062     list_tmp = payee_list;
3063     while (list_tmp)
3064     {
3065         if (GPOINTER_TO_INT (list_tmp->data) == -1)
3066             /* it's a normal party, we set the list_tmp to NULL */
3067             list_tmp = NULL;
3068         else
3069         {
3070             /* it's a report, so each time we come here we set the parti's combofix to the
3071              * party of the report */
3072 
3073             if (!list_tmp->data)
3074             {
3075                 dialogue_error (_("No payee selected for this report."));
3076                 return FALSE;
3077             }
3078             else
3079             {
3080                 GtkWidget *widget;
3081                 gint payment_number;
3082 
3083                 if (nbre_passage == 0)
3084                 {
3085                     widget = gsb_form_widget_get_widget (TRANSACTION_FORM_PARTY);
3086                     gtk_combofix_set_text (GTK_COMBOFIX (widget),
3087 										   gsb_data_payee_get_name (GPOINTER_TO_INT (list_tmp->data),
3088 																	TRUE));
3089                     gsb_form_widget_set_empty (widget, FALSE);
3090                 }
3091                 else
3092                 {
3093                     transaction_number = gsb_data_transaction_new_transaction (account_number);
3094 
3095                     gsb_data_transaction_copy_transaction (source_transaction_number,
3096 														   transaction_number, TRUE);
3097                     gsb_data_transaction_set_party_number (transaction_number,
3098 														   GPOINTER_TO_INT (list_tmp->data));
3099 
3100                     /* if it's not the first party and the method of payment has to change its number (cheque),
3101                      * we increase the number. as we are in a party's list, it's always a new transactio,
3102                      * so we know that it's not the first if transaction_number is not 0 */
3103 
3104                     payment_number = gsb_data_transaction_get_method_of_payment_number (source_transaction_number);
3105 
3106                     if (gsb_data_payment_get_automatic_numbering (payment_number))
3107                     {
3108                         gchar *tmp_str;
3109 
3110                         tmp_str = gsb_data_payment_incremente_last_number (payment_number, nbre_passage);
3111                         gsb_data_transaction_set_method_of_payment_content (transaction_number, tmp_str);
3112                         g_free (tmp_str) ;
3113                     }
3114                     gsb_transactions_list_append_new_transaction (transaction_number, TRUE);
3115                 }
3116                 nbre_passage++;
3117             }
3118 
3119             list_tmp = list_tmp->next;
3120             if (list_tmp == NULL)
3121                 break;
3122             else if (nbre_passage > 1)
3123                 continue;
3124         }
3125 
3126         /* now we create the transaction if necessary and set the mother in case of child of split */
3127         if (new_transaction)
3128         {
3129             /* it's a new transaction, we create it, and set the mother if necessary */
3130             gint mother_transaction = 0;
3131 
3132             /* if we are on a white child (ie number < -1, -1 is only for the general white line),
3133              * get the mother of transaction */
3134             if (transaction_number < -1)
3135                 mother_transaction = gsb_data_mix_get_mother_transaction_number (transaction_number,
3136 																				 is_transaction);
3137             transaction_number = gsb_data_mix_new_transaction (account_number, is_transaction);
3138             if (source_transaction_number == -1)
3139                 source_transaction_number = transaction_number;
3140 
3141             gsb_data_mix_set_mother_transaction_number (transaction_number, mother_transaction, is_transaction);
3142 
3143             /* si l'opération mère est rapprochée on marque aussi la fille */
3144             if (mother_transaction
3145              &&
3146              gsb_data_transaction_get_marked_transaction (mother_transaction) == OPERATION_RAPPROCHEE)
3147             {
3148                 gsb_data_transaction_set_marked_transaction (transaction_number, OPERATION_RAPPROCHEE);
3149                 gsb_data_transaction_set_reconcile_number (transaction_number,
3150 														   gsb_data_transaction_get_reconcile_number
3151 														   (mother_transaction));
3152             }
3153         }
3154 
3155         /* take the datas in the form, except the category */
3156         gsb_form_take_datas_from_form (transaction_number, is_transaction);
3157 
3158         /* si l'opération est une opération planifiée exécutée on met en forme TRANSACTION_FORM_CHEQUE */
3159         if (execute_scheduled)
3160         {
3161             gint payment_number;
3162 
3163             payment_number = gsb_data_transaction_get_method_of_payment_number (transaction_number);
3164 
3165             if (gsb_data_payment_get_automatic_numbering (payment_number))
3166             {
3167                 gchar *tmp_str;
3168 
3169                 tmp_str = gsb_data_payment_incremente_last_number (payment_number, 1);
3170                 gsb_data_transaction_set_method_of_payment_content (transaction_number, tmp_str);
3171                 g_free (tmp_str) ;
3172             }
3173         }
3174 
3175         /* perhaps the currency button is not shown
3176          * in that case, we give the account currency to that transaction */
3177         if (new_transaction && !gsb_form_widget_get_widget (TRANSACTION_FORM_DEVISE))
3178             gsb_data_mix_set_currency_number (transaction_number,
3179 											  gsb_data_account_get_currency (account_number),
3180 											  is_transaction);
3181 
3182         /* get the category and do the stuff with that (contra-transaction...) */
3183         gsb_form_get_categories (transaction_number, new_transaction, is_transaction);
3184 
3185         /* for the rest we need to split for transactions/scheduled */
3186         if (is_transaction)
3187         {
3188             /* it's a transaction or an execution of scheduled transaction */
3189             if (new_transaction)
3190             {
3191 				gint split_transaction_number;
3192 
3193 				gsb_transactions_list_append_new_transaction (transaction_number, TRUE);
3194 
3195 				/* if it's a real new transaction and if it's a split, we ask if the user wants
3196 				 * to recover previous children */
3197 				if (gsb_data_transaction_get_split_of_transaction (transaction_number)
3198 					&& !execute_scheduled
3199 					&& gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (form_button_recover_split))
3200 					&& (split_transaction_number = gsb_form_transactions_look_for_last_party
3201 						(gsb_data_transaction_get_party_number (transaction_number),
3202 						 transaction_number,
3203 														 gsb_data_transaction_get_account_number(transaction_number))))
3204 					gsb_form_transaction_recover_splits_of_transaction (transaction_number,
3205 																		split_transaction_number);
3206             }
3207             else
3208             {
3209             /* update a transaction */
3210             gsb_transactions_list_update_transaction (transaction_number);
3211 
3212             /* we are on a modification of transaction, but if the modified transaction is a split
3213              * and has no children (ie only white line), we assume the user wants now fill the children, so we will do the
3214              * same as for a new transaction : open the expander and select the white line */
3215             if (transaction_list_get_n_children (transaction_number) == 1)
3216             {
3217                 new_transaction = TRUE;
3218                 gsb_transactions_list_switch_expander (transaction_number);
3219             }
3220             }
3221         }
3222         else
3223         {
3224             /* it's a scheduled transaction */
3225             gsb_form_scheduler_get_scheduler_part (transaction_number);
3226 
3227             if (new_transaction)
3228             {
3229 				gsb_scheduler_list_append_new_scheduled (transaction_number,
3230 														 gsb_scheduler_list_get_end_date_scheduled_showed ());
3231 				/* recover if necessary previous children */
3232 				if (gsb_data_scheduled_get_split_of_scheduled (transaction_number)
3233 					&&
3234 					gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (form_button_recover_split)))
3235 				{
3236 					gint tmp_account_number;
3237 					gint party_number;
3238 					gint split_transaction_number = 0;
3239 
3240 					tmp_account_number = gsb_data_scheduled_get_account_number(transaction_number);
3241 					party_number = gsb_data_scheduled_get_party_number (transaction_number);
3242 					split_transaction_number = gsb_form_transactions_look_for_last_party (party_number,
3243 																						  0,
3244 																						  tmp_account_number);
3245 					if (split_transaction_number)
3246 						gsb_form_scheduler_recover_splits_of_transaction (transaction_number,
3247 																		  split_transaction_number);
3248 				}
3249             }
3250             else
3251 				gsb_scheduler_list_update_transaction_in_list (transaction_number);
3252 
3253             /* needed for the two in case of we change the date */
3254             gsb_scheduler_list_set_background_color (gsb_scheduler_list_get_tree_view ());
3255             gsb_calendar_update ();
3256         }
3257     }
3258     g_slist_free (payee_list);
3259 
3260     /* if it's a reconciliation and we modify a transaction, check
3261      * the amount of marked transactions */
3262     if (is_transaction && run.equilibrage)
3263     {
3264         if (new_transaction)
3265             /* we are reconciling and it's a new transaction, so need to show the checkbox */
3266             transaction_list_show_toggle_mark (TRUE);
3267         else
3268         {
3269             gsb_reconcile_update_amounts (NULL, NULL);
3270         }
3271     }
3272 
3273     /* if we executed a scheduled transation, need to increase the date of the scheduled
3274      * and execute the children if it's a split */
3275     if (execute_scheduled)
3276     {
3277         gint increase_result;
3278 
3279         /* first, check if it's a scheduled split and execute the childrent */
3280         if (gsb_data_scheduled_get_split_of_scheduled (saved_scheduled_number))
3281 		{
3282 			gint transfer_account;
3283 
3284 			transfer_account = gsb_data_scheduled_get_account_number_transfer (saved_scheduled_number+1);
3285 			if (gsb_data_account_get_bet_init_sch_with_loan (transfer_account))
3286 			{
3287 				GDate *date;
3288 
3289 				date = gsb_date_copy (gsb_data_scheduled_get_date (saved_scheduled_number));
3290 				g_date_add_months (date, 1);
3291 				bet_finance_get_loan_amount_at_date (saved_scheduled_number, transfer_account, date, TRUE);
3292 			}
3293             gsb_scheduler_execute_children_of_scheduled_transaction (saved_scheduled_number, transaction_number);
3294 		}
3295 
3296         /* now we can increase the scheduled transaction */
3297         increase_result = gsb_scheduler_increase_scheduled (saved_scheduled_number);
3298 
3299         /* the next step is to update the list, but do it only if we are on the scheduled
3300          * list, else we needn't because the update will be done when going to that list */
3301         if (gsb_gui_navigation_get_current_page () == GSB_SCHEDULER_PAGE)
3302         {
3303             if (increase_result)
3304                 gsb_scheduler_list_update_transaction_in_list (saved_scheduled_number);
3305 
3306             gsb_scheduler_list_set_background_color (gsb_scheduler_list_get_tree_view ());
3307         }
3308     }
3309 
3310     /* if it was a new transaction, do the stuff to do another new transaction */
3311     if (new_transaction && !execute_scheduled)
3312     {
3313         /* we are on a new transaction, if that transaction is a split,
3314          * we give the focus to the new white line created for that and
3315          * edit it, for that we need to open the transaction to select the
3316          * white line, and set it as current transaction */
3317         if (gsb_data_mix_get_split_of_transaction (transaction_number, is_transaction))
3318         {
3319             /* it's a split */
3320             gint white_line_number;
3321 
3322             white_line_number = gsb_data_mix_get_white_line (transaction_number, is_transaction);
3323             if (is_transaction)
3324                 transaction_list_select (white_line_number);
3325             else
3326                 gsb_scheduler_list_select (white_line_number);
3327         }
3328 
3329         /* it was a new transaction, we save the last date entry */
3330         gsb_date_set_last_date (gtk_entry_get_text (GTK_ENTRY (gsb_form_widget_get_widget (TRANSACTION_FORM_DATE))));
3331 
3332         /* we need to use edit_transaction to make a new child split if necessary */
3333         if (is_transaction)
3334             gsb_transactions_list_edit_transaction (gsb_data_account_get_current_transaction_number
3335 													(account_number));
3336         else
3337             gsb_scheduler_list_edit_transaction (gsb_scheduler_list_get_current_scheduled_number ());
3338     }
3339     else
3340 	{
3341         gsb_form_escape_form ();
3342 	}
3343 
3344 	/* c'est une opération planifiée et l'option fixer le jour de fin de mois est positionnée */
3345 	if (!is_transaction && etat.scheduler_set_fixed_date)
3346 	{
3347 		GDateDay day;
3348 
3349 		day = g_date_get_day (save_form_date);
3350 		if (day > 27)
3351 			gsb_form_set_fixed_date_if_necessary (transaction_number, save_form_date);
3352 	}
3353 
3354     /* on sort de la saisie des opérations filles si variance == 0 */
3355     if ((mother_number = gsb_data_mix_get_mother_transaction_number (transaction_number, is_transaction)))
3356     {
3357         if (is_transaction)
3358 		{
3359 			gint archive_number = 0;
3360 
3361 			/* on verifie ici si l'operation mere est archivée dans ce cas on archive aussi les opérations filles */
3362 			archive_number = gsb_data_transaction_get_archive_number (mother_number);
3363 			if (archive_number)
3364 				gsb_data_transaction_set_archive_number (transaction_number, archive_number);
3365 
3366 			if (transaction_list_get_variance (gsb_data_account_get_current_transaction_number (account_number)))
3367 			{
3368 				GrisbiAppConf *a_conf;
3369 
3370 				a_conf = (GrisbiAppConf *) grisbi_app_get_a_conf ();
3371 				gtk_tree_view_collapse_all (GTK_TREE_VIEW (gsb_transactions_list_get_tree_view ()));
3372 				if (a_conf->form_validate_split)
3373 					transaction_list_select (mother_number);
3374 				else
3375 					transaction_list_select (-1);
3376 			}
3377 		}
3378         else if (!is_transaction && !execute_scheduled && gsb_data_scheduled_get_variance (mother_number))
3379         {
3380             gtk_tree_view_collapse_all (GTK_TREE_VIEW (gsb_scheduler_list_get_tree_view ()));
3381             gsb_scheduler_list_select (mother_number);
3382         }
3383     }
3384 
3385     /* if it was a modification of transaction, we need to update the sort and colors
3386      * (done automatically for new transaction) */
3387     if (is_transaction && !new_transaction)
3388         gsb_transactions_list_update_tree_view (account_number, TRUE);
3389 
3390     /* show the warnings */
3391     if (is_transaction)
3392     {
3393         affiche_dialogue_soldes_minimaux ();
3394         update_transaction_in_trees (transaction_number);
3395         if (gsb_data_transaction_get_marked_transaction (transaction_number) == OPERATION_POINTEE)
3396         {
3397             gsb_gui_navigation_update_statement_label (account_number);
3398             run.mise_a_jour_liste_comptes_accueil = TRUE;
3399         }
3400     }
3401 
3402     /* as we modify or create a transaction, we invalidate the current report */
3403     gsb_report_set_current (0);
3404 
3405     /* force the update module budget */
3406     gsb_data_account_set_bet_maj (account_number, BET_MAJ_ALL);
3407 
3408     gsb_file_set_modified (TRUE);
3409 
3410     /* If the origin of the operation is a model, so we select the new transaction */
3411     if (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (transaction_form), "transaction_selected_in_form")) == -1)
3412     {
3413         transaction_list_select (transaction_number);
3414         return FALSE;
3415     }
3416 
3417     if (execute_scheduled)
3418     {
3419         GtkTreeSelection *selection;
3420         GtkTreePath *path;
3421 
3422 		gsb_scheduler_list_edit_transaction (gsb_scheduler_list_get_current_scheduled_number ());
3423         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (gsb_scheduler_list_get_tree_view ()));
3424         path = gtk_tree_path_new_from_string ("0");
3425         gtk_tree_selection_select_path (selection, path);
3426 
3427         gtk_tree_path_free (path);
3428     }
3429 
3430     /* give the focus to the date widget */
3431 	if (is_transaction)
3432 	{
3433 		if (mother_number == 0)
3434 			gsb_form_widget_set_focus (TRANSACTION_FORM_DATE);
3435 	}
3436 	else
3437 	{
3438 		gsb_scheduler_check_scheduled_transactions_time_limit ();
3439 		gsb_scheduler_list_edit_transaction (gsb_scheduler_list_get_current_scheduled_number ());
3440 	}
3441 
3442     return FALSE;
3443 }
3444 
3445 
3446 /**
3447  * called when cancel a form
3448  *
3449  * \param
3450  *
3451  * \return FALSE
3452  **/
gsb_form_escape_form(void)3453 gboolean gsb_form_escape_form (void)
3454 {
3455 	GrisbiAppConf *a_conf;
3456 
3457     devel_debug (NULL);
3458 	a_conf = (GrisbiAppConf *) grisbi_app_get_a_conf ();
3459 
3460     /* in all case clean the scheduler part of the form */
3461     gsb_form_scheduler_clean ();
3462 
3463     if (a_conf->formulaire_toujours_affiche)
3464     {
3465         gsb_form_clean (gsb_form_get_account_number ());
3466     }
3467     else
3468     {
3469 		GtkWidget *form_expander;
3470 
3471 		form_expander = grisbi_win_get_form_expander ();
3472         gtk_expander_set_expanded (GTK_EXPANDER (form_expander), FALSE);
3473     }
3474 
3475     switch (gsb_form_get_origin ())
3476     {
3477 		case ORIGIN_VALUE_OTHER:
3478 			notice_debug ("Should not come here... (gsb_form_escape_form)");
3479 			gtk_widget_grab_focus (gsb_gui_navigation_get_tree_view ());
3480 			break;
3481 
3482 		case ORIGIN_VALUE_HOME:
3483 			gtk_widget_grab_focus (GTK_WIDGET (grisbi_app_get_active_window (NULL)));
3484 			break;
3485 
3486 		case ORIGIN_VALUE_SCHEDULED:
3487 			gtk_widget_grab_focus (gsb_scheduler_list_get_tree_view ());
3488 			break;
3489 
3490 		default:
3491 			gtk_widget_grab_focus (gsb_transactions_list_get_tree_view());
3492     }
3493 
3494     return FALSE;
3495 }
3496 
3497 /**
3498  * return the widget of the element_number given in param in the list
3499  *
3500  * \param element_number
3501  * \param GSList *list
3502   *
3503  * \return a GtkWidget * or NULL
3504  **/
gsb_form_get_element_widget_from_list(gint element_number,GSList * list)3505 GtkWidget *gsb_form_get_element_widget_from_list (gint element_number,
3506 												  GSList *list)
3507 {
3508     GSList *list_tmp;
3509 
3510     list_tmp = list;
3511 
3512     while (list_tmp)
3513     {
3514         struct_element *element;
3515 
3516         element = list_tmp->data;
3517         if (element->element_number == element_number)
3518             return element->element_widget;
3519 
3520         list_tmp = list_tmp->next;
3521     }
3522 
3523     return NULL;
3524 }
3525 
3526 /**
3527  *
3528  *
3529  * \param
3530  *
3531  * \return
3532  **/
gsb_form_get_form_transaction_part(void)3533 GtkWidget *gsb_form_get_form_transaction_part (void)
3534 {
3535 	return form_transaction_part;
3536 }
3537 
3538 /**
3539  *
3540  *
3541  * \param
3542  *
3543  * \return
3544  **/
gsb_form_sensitive_cancel_valid_buttons(gboolean sensitive)3545 void gsb_form_sensitive_cancel_valid_buttons (gboolean sensitive)
3546 {
3547     gtk_widget_set_sensitive (GTK_WIDGET (form_button_valid), TRUE);
3548     gtk_widget_set_sensitive (GTK_WIDGET (form_button_cancel), TRUE);
3549 }
3550 
3551 /**
3552  *
3553  *
3554  * \param
3555  *
3556  * \return
3557  **/
gsb_form_get_recover_split_button(void)3558 GtkWidget *gsb_form_get_recover_split_button (void)
3559 {
3560 	return form_button_recover_split;
3561 }
3562 
3563 /**
3564  *
3565  *
3566  * \param
3567  *
3568  * \return
3569  **/
3570 /* Local Variables: */
3571 /* c-basic-offset: 4 */
3572 /* End: */
3573