1 /* ***************************************************************************/
2 /*                                                                            */
3 /*     Copyright (C)    2000-2008 Cédric Auger (cedric@grisbi.org)            */
4 /*          2003-2008 Benjamin Drieu (bdrieu@april.org)                       */
5 /*          https://www.grisbi.org/                                            */
6 /*                                                                            */
7 /*  This program is free software; you can redistribute it and/or modify      */
8 /*  it under the terms of the GNU General Public License as published by      */
9 /*  the Free Software Foundation; either version 2 of the License, or         */
10 /*  (at your option) any later version.                                       */
11 /*                                                                            */
12 /*  This program is distributed in the hope that it will be useful,           */
13 /*  but WITHOUT ANY WARRANTY; without even the implied warranty of            */
14 /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             */
15 /*  GNU General Public License for more details.                              */
16 /*                                                                            */
17 /*  You should have received a copy of the GNU General Public License         */
18 /*  along with this program; if not, write to the Free Software               */
19 /*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
20 /*                                                                            */
21 /* ***************************************************************************/
22 
23 /**
24  * \file gsb_scheduled_data.c
25  * work with the scheduled structure, no GUI here
26  */
27 
28 
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32 
33 #include "include.h"
34 #include <glib/gi18n.h>
35 
36 /*START_INCLUDE*/
37 #include "gsb_data_scheduled.h"
38 #include "dialog.h"
39 #include "gsb_currency.h"
40 #include "gsb_data_currency.h"
41 #include "gsb_data_currency_link.h"
42 #include "gsb_file.h"
43 #include "gsb_real.h"
44 #include "utils_dates.h"
45 #include "utils_str.h"
46 #include "erreur.h"
47 /*END_INCLUDE*/
48 
49 typedef struct	_ScheduledStruct	ScheduledStruct;  /* Describe a scheduled transaction */
50 
51 struct _ScheduledStruct
52 {
53     /** @name general stuff */
54     gint 		scheduled_number;
55     gint 		account_number;
56     GsbReal 	scheduled_amount;
57     gint 		party_number;
58     gchar *		notes;
59     gshort 		automatic_scheduled;			/* < 0=manual, 1=automatic (scheduled scheduled) */
60     guint 		financial_year_number;
61 	gint		fixed_date;						/* 0=flottant 1=fixed 2=last_banking_day */
62 
63     /** @name dates of the scheduled */
64     GDate *		date;
65 
66     /** @name currency stuff */
67     gint 		currency_number;
68 
69     /** @name category stuff */
70     gint 		category_number;
71     gint 		sub_category_number;
72     gint 		budgetary_number;
73     gint 		sub_budgetary_number;
74     gint 		account_number_transfer;		/* < -1 for a scheduled neither categ, neither transfer, neither split */
75     gint 		split_of_scheduled;				/* < 1 if it's a split of scheduled */
76     gint 		mother_scheduled_number;		/* < for a split, the mother's scheduled number */
77 
78     /** @name method of payment */
79     gint 		method_of_payment_number;
80     gchar *		method_of_payment_content;
81     gint 		contra_method_of_payment_number;
82 
83     /** @name specific stuff for scheduled transactions */
84     gint 		frequency;						/* <  0=once, 1=week, 2=month, 3=year, 4=perso */
85     gint 		user_interval;					/* <  0=days, 1=monthes, 2=years */
86     gint 		user_entry;
87     GDate *		limit_date;
88 };
89 
90 /*END_STATIC*/
91 
92 /*START_EXTERN*/
93 /*END_EXTERN*/
94 
95 
96 /** the g_slist which contains all the scheduleds structures */
97 static GSList *scheduled_list = NULL;
98 
99 /** the g_slist which contains all the white scheduleds structures
100  * ie : 1 general white line
101  * and 1 white line per split of scheduled */
102 static GSList *white_scheduled_list = NULL;
103 
104 /** 2 pointers to the 2 last scheduled used (to increase the speed) */
105 static ScheduledStruct *scheduled_buffer[2];
106 
107 /** set the current buffer used */
108 static gint current_scheduled_buffer;
109 
110 /******************************************************************************/
111 /* Private Methods                                                            */
112 /******************************************************************************/
113 /**
114  * This internal function is called to free memory used by a ScheduledStruct structure.
115  *
116  * \param 		ScheduledStruct
117  *
118  * \return
119  **/
_gsb_data_scheduled_free(ScheduledStruct * scheduled)120 static void _gsb_data_scheduled_free (ScheduledStruct *scheduled)
121 {
122     if (!scheduled)
123         return;
124 
125     g_free (scheduled->notes);
126     g_free (scheduled->method_of_payment_content);
127 
128     if (scheduled->date)
129         g_date_free (scheduled->date);
130     if (scheduled->limit_date)
131         g_date_free (scheduled->limit_date);
132 
133     g_free (scheduled);
134 
135     scheduled_buffer[0] = NULL;
136     scheduled_buffer[1] = NULL;
137     current_scheduled_buffer = 0;
138 }
139 
140 /**
141  * Delete all scheduled and clear memory used by them
142  *
143  * \param none
144  *
145  * \return
146  **/
gsb_data_scheduled_delete_all_scheduled(void)147 static void gsb_data_scheduled_delete_all_scheduled (void)
148 {
149     if (scheduled_list)
150     {
151         GSList *tmp_list;
152         tmp_list = scheduled_list;
153         while (tmp_list)
154         {
155             ScheduledStruct *scheduled;
156 
157             scheduled = tmp_list->data;
158             tmp_list = tmp_list->next;
159                 _gsb_data_scheduled_free (scheduled);
160         }
161         g_slist_free (scheduled_list);
162         scheduled_list = NULL;
163     }
164 }
165 
166 /**
167  * find the last number of scheduled
168  *
169  * \param none
170  *
171  * \return the number
172  **/
gsb_data_scheduled_get_last_number(void)173 static gint gsb_data_scheduled_get_last_number (void)
174 {
175     gint last_number = 0;
176     GSList *scheduled_list_tmp;
177 
178     scheduled_list_tmp = scheduled_list;
179 
180     while (scheduled_list_tmp)
181     {
182 		ScheduledStruct *scheduled;
183 
184 		scheduled = scheduled_list_tmp->data;
185 		if (scheduled->scheduled_number > last_number)
186 			last_number = scheduled->scheduled_number;
187 
188 		scheduled_list_tmp = scheduled_list_tmp->next;
189     }
190     return last_number;
191 }
192 
193 /**
194  * find the last number of the white lines
195  * all the white lines have a number < 0, and it always exists at least
196  * one line, which number -1 which is the general white line (without mother)
197  * so we never return 0 to avoid -1 for a number of white split
198  *
199  * \param none
200  *
201  * \return the number
202  **/
gsb_data_scheduled_get_last_white_number(void)203 static gint gsb_data_scheduled_get_last_white_number (void)
204 {
205     gint last_number = 0;
206     GSList *scheduled_list_tmp;
207 
208     scheduled_list_tmp = white_scheduled_list;
209 
210     while (scheduled_list_tmp)
211     {
212 		ScheduledStruct *scheduled;
213 
214 		scheduled = scheduled_list_tmp->data;
215 		if (scheduled->scheduled_number < last_number)
216 			last_number = scheduled->scheduled_number;
217 
218 		scheduled_list_tmp = scheduled_list_tmp->next;
219     }
220 
221     /* if the general white line has not been appened already, we
222      * return -1 to keep that number for the general white line
223      * (the number will be decreased after to numbered the new line) */
224 
225     if (!last_number)
226 		last_number = -1;
227 
228     return last_number;
229 }
230 
231 /**
232  * save the pointer in a buffer to increase the speed later
233  *
234  * \param scheduled the pointer to the scheduled
235  *
236  * \return TRUE or FALSE if pb
237  **/
gsb_data_scheduled_save_scheduled_pointer(gpointer scheduled)238 static gboolean gsb_data_scheduled_save_scheduled_pointer (gpointer scheduled)
239 {
240     /* check if the scheduled isn't already saved */
241 
242     if (scheduled == scheduled_buffer[0]
243 	 ||
244 	 scheduled == scheduled_buffer[1])
245 		return TRUE;
246 
247     current_scheduled_buffer = !current_scheduled_buffer;
248     scheduled_buffer[current_scheduled_buffer] = scheduled;
249 
250 	return TRUE;
251 }
252 
253 /**
254  * return the scheduled which the number is in the parameter.
255  * the new scheduled is stored in the buffer
256  *
257  * \param scheduled_number
258  *
259  * \return a pointer to the scheduled, NULL if not found
260  **/
gsb_data_scheduled_get_scheduled_by_no(gint scheduled_number)261 static ScheduledStruct *gsb_data_scheduled_get_scheduled_by_no (gint scheduled_number)
262 {
263     GSList *scheduled_list_tmp;
264 
265     /* check first if the scheduled is in the buffer */
266 
267     if (scheduled_buffer[0]
268 	 &&
269 	 scheduled_buffer[0]->scheduled_number == scheduled_number)
270 		return scheduled_buffer[0];
271 
272     if (scheduled_buffer[1]
273 	 &&
274 	 scheduled_buffer[1]->scheduled_number == scheduled_number)
275 		return scheduled_buffer[1];
276 
277     if (scheduled_number < 0)
278 		scheduled_list_tmp = white_scheduled_list;
279     else
280 		scheduled_list_tmp = scheduled_list;
281 
282     while (scheduled_list_tmp)
283     {
284 		ScheduledStruct *scheduled;
285 
286 		scheduled = scheduled_list_tmp->data;
287 
288 		if (scheduled->scheduled_number == scheduled_number)
289 		{
290 			gsb_data_scheduled_save_scheduled_pointer (scheduled);
291 			return scheduled;
292 		}
293 
294 		scheduled_list_tmp = scheduled_list_tmp->next;
295     }
296 
297     /* here, we didn't find any scheduled with that number */
298 
299     return NULL;
300 }
301 
302 /******************************************************************************/
303 /* Public Methods                                                             */
304 /******************************************************************************/
305 /**
306  * set the scheduleds global variables to NULL, usually when we init all the global variables
307  *
308  * \param none
309  *
310  * \return FALSE
311  **/
gsb_data_scheduled_init_variables(void)312 gboolean gsb_data_scheduled_init_variables (void)
313 {
314     gsb_data_scheduled_delete_all_scheduled ();
315     return FALSE;
316 }
317 
318 
319 /**
320  * return a pointer to the g_slist of scheduleds structure
321  * it's not a copy, so we must not free or change it
322  * if we want to change something, use gsb_data_scheduled_copy_scheduled_list instead
323  *
324  * \param none
325  *
326  * \return the slist of scheduleds structures
327  **/
gsb_data_scheduled_get_scheduled_list(void)328 GSList *gsb_data_scheduled_get_scheduled_list (void)
329 {
330     return scheduled_list;
331 }
332 
333 
334 /**
335  * get the number of the scheduled and save the pointer in the buffer
336  * which will increase the speed later
337  *
338  * \param scheduled a pointer to a scheduled
339  *
340  * \return the number of the scheduled
341  **/
gsb_data_scheduled_get_scheduled_number(gpointer scheduled_pointer)342 gint gsb_data_scheduled_get_scheduled_number (gpointer scheduled_pointer)
343 {
344     ScheduledStruct *scheduled;
345 
346     scheduled = scheduled_pointer;
347 
348     if (!scheduled)
349 		return 0;
350 
351     /* if we want the scheduled number, usually it's to make other stuff after that
352      * so we will save the adr of the scheduled to increase the speed after */
353 
354     gsb_data_scheduled_save_scheduled_pointer (scheduled);
355 
356     return scheduled->scheduled_number;
357 }
358 
359 /**
360  * get the account_number
361  *
362  * \param scheduled_number the number of the scheduled
363  *
364  * \return the account of the scheduled or -1 if problem
365  **/
gsb_data_scheduled_get_account_number(gint scheduled_number)366 gint gsb_data_scheduled_get_account_number (gint scheduled_number)
367 {
368     ScheduledStruct *scheduled;
369 
370     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
371 
372     if (!scheduled)
373 		return -1;
374 
375     return scheduled->account_number;
376 }
377 
378 /**
379  * set the account_number
380  * if the scheduled has some children, they change too
381  *
382  * \param scheduled_number
383  * \param no_account
384  *
385  * \return TRUE if ok
386  **/
gsb_data_scheduled_set_account_number(gint scheduled_number,gint no_account)387 gboolean gsb_data_scheduled_set_account_number (gint scheduled_number,
388 												gint no_account)
389 {
390     ScheduledStruct *scheduled;
391 
392     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
393 
394     if (!scheduled)
395 		return FALSE;
396 
397     scheduled->account_number = no_account;
398 
399     /* if the scheduled is a split, change all the children */
400     if (scheduled->split_of_scheduled)
401     {
402 		GSList *tmp_list;
403 		GSList *save_tmp_list;
404 
405 		tmp_list = gsb_data_scheduled_get_children (scheduled->scheduled_number, FALSE);
406 		save_tmp_list = tmp_list;
407 
408 		while (tmp_list)
409 		{
410 			scheduled = tmp_list->data;
411 			scheduled->account_number = no_account;
412 			tmp_list = tmp_list->next;
413 		}
414 		g_slist_free (save_tmp_list);
415     }
416 
417     return TRUE;
418 }
419 
420 /**
421  * get the GDate of the scheduled
422  *
423  * \param scheduled_number the number of the scheduled
424  *
425  * \return the GDate of the scheduled
426  **/
gsb_data_scheduled_get_date(gint scheduled_number)427 GDate *gsb_data_scheduled_get_date (gint scheduled_number)
428 {
429     ScheduledStruct *scheduled;
430 
431     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
432 
433     if (!scheduled)
434 		return NULL;
435 
436     return scheduled->date;
437 }
438 
439 /**
440  * set the GDate of the scheduled
441  * if the scheduled has some children, they change too
442  *
443  * \param scheduled_number
444  * \param no_account
445  *
446  * \return TRUE if ok
447  **/
gsb_data_scheduled_set_date(gint scheduled_number,const GDate * date)448 gboolean gsb_data_scheduled_set_date (gint scheduled_number,
449 									  const GDate *date)
450 {
451     ScheduledStruct *scheduled;
452 
453     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
454 
455     if (!scheduled)
456 		return FALSE;
457 
458     if (scheduled->date)
459 		g_date_free (scheduled->date);
460     scheduled->date = gsb_date_copy (date);
461 
462     /* if the scheduled is a split, change all the children */
463     if (scheduled->split_of_scheduled)
464     {
465 		GSList *tmp_list;
466 		GSList *save_tmp_list;
467 
468 		tmp_list = gsb_data_scheduled_get_children (scheduled->scheduled_number, FALSE);
469 		save_tmp_list = tmp_list;
470 
471 		while (tmp_list)
472 		{
473 			scheduled = tmp_list->data;
474 
475 			if (scheduled->date)
476 			g_date_free (scheduled->date);
477 			scheduled->date = gsb_date_copy (date);
478 
479 			tmp_list = tmp_list->next;
480 		}
481 		g_slist_free (save_tmp_list);
482     }
483 
484     return TRUE;
485 }
486 
487 /**
488  * get the amount of the scheduled without any currency change
489  * (so just get the given amout)
490  *
491  * \param scheduled_number the number of the scheduled
492  *
493  * \return the amount of the scheduled
494  **/
gsb_data_scheduled_get_amount(gint scheduled_number)495 GsbReal gsb_data_scheduled_get_amount (gint scheduled_number)
496 {
497     ScheduledStruct *scheduled;
498 
499     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
500 
501     if (!scheduled)
502 		return null_real;
503 
504     return scheduled->scheduled_amount;
505 }
506 
507 /**
508  * set the amount of the scheduled
509  *
510  * \param scheduled_number
511  * \param amount
512  *
513  * \return TRUE if ok
514  **/
gsb_data_scheduled_set_amount(gint scheduled_number,GsbReal amount)515 gboolean gsb_data_scheduled_set_amount (gint scheduled_number,
516 										GsbReal amount)
517 {
518     ScheduledStruct *scheduled;
519 
520     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
521 
522     if (!scheduled)
523 		return FALSE;
524 
525     scheduled->scheduled_amount = amount;
526 
527     return TRUE;
528 }
529 
530 /**
531  * get the currency_number
532  *
533  * \param scheduled_number the number of the scheduled
534  *
535  * \return the currency number of the scheduled
536  **/
gsb_data_scheduled_get_currency_number(gint scheduled_number)537 gint gsb_data_scheduled_get_currency_number (gint scheduled_number)
538 {
539     ScheduledStruct *scheduled;
540 
541     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
542 
543     if (!scheduled)
544 		return -1;
545 
546     return scheduled->currency_number;
547 }
548 
549 /**
550  * set the currency_number
551  * if the scheduled has some children, they change too
552  *
553  * \param scheduled_number
554  * \param no_currency
555  *
556  * \return TRUE if ok
557  **/
gsb_data_scheduled_set_currency_number(gint scheduled_number,gint no_currency)558 gboolean gsb_data_scheduled_set_currency_number (gint scheduled_number,
559 												 gint no_currency)
560 {
561     ScheduledStruct *scheduled;
562 
563     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
564 
565     if (!scheduled)
566 		return FALSE;
567 
568     scheduled->currency_number = no_currency;
569 
570     /* if the scheduled is a split, change all the children */
571     if (scheduled->split_of_scheduled)
572     {
573 		GSList *tmp_list;
574 		GSList *save_tmp_list;
575 
576 		tmp_list = gsb_data_scheduled_get_children (scheduled->scheduled_number, FALSE);
577 		save_tmp_list = tmp_list;
578 
579 		while (tmp_list)
580 		{
581 			scheduled = tmp_list->data;
582 			scheduled->currency_number = no_currency;
583 			tmp_list = tmp_list->next;
584 		}
585 		g_slist_free (save_tmp_list);
586     }
587 
588     return TRUE;
589 }
590 
591 /**
592  * get the party_number
593  *
594  * \param scheduled_number the number of the scheduled
595  *
596  * \return the currency number of the scheduled
597  **/
gsb_data_scheduled_get_party_number(gint scheduled_number)598 gint gsb_data_scheduled_get_party_number (gint scheduled_number)
599 {
600     ScheduledStruct *scheduled;
601 
602     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
603 
604     if (!scheduled)
605 		return -1;
606 
607     return scheduled->party_number;
608 }
609 
610 /**
611  * set the party_number
612  * if the scheduled has some children, they change too
613  *
614  * \param scheduled_number
615  * \param value
616  *
617  * \return TRUE if ok
618  **/
gsb_data_scheduled_set_party_number(gint scheduled_number,gint no_party)619 gboolean gsb_data_scheduled_set_party_number (gint scheduled_number,
620 											  gint no_party)
621 {
622     ScheduledStruct *scheduled;
623 
624     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
625 
626     if (!scheduled)
627 		return FALSE;
628 
629     scheduled->party_number = no_party;
630 
631     /* if the scheduled is a split, change all the children */
632     if (scheduled->split_of_scheduled)
633     {
634 		GSList *tmp_list;
635 		GSList *save_tmp_list;
636 
637 		tmp_list = gsb_data_scheduled_get_children (scheduled->scheduled_number, FALSE);
638 		save_tmp_list = tmp_list;
639 
640 		while (tmp_list)
641 		{
642 			scheduled = tmp_list->data;
643 			scheduled->party_number = no_party;
644 			tmp_list = tmp_list->next;
645 		}
646 		g_slist_free (save_tmp_list);
647     }
648 
649     return TRUE;
650 }
651 
652 /**
653  * get the category_number
654  *
655  * \param scheduled_number the number of the scheduled
656  *
657  * \return the category number of the scheduled
658  **/
gsb_data_scheduled_get_category_number(gint scheduled_number)659 gint gsb_data_scheduled_get_category_number (gint scheduled_number)
660 {
661     ScheduledStruct *scheduled;
662 
663     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
664 
665     if (!scheduled)
666 		return 0;
667 
668     return scheduled->category_number;
669 }
670 
671 /**
672  * set the category_number
673  *
674  * \param scheduled_number
675  * \param value
676  *
677  * \return TRUE if ok
678  **/
gsb_data_scheduled_set_category_number(gint scheduled_number,gint no_category)679 gboolean gsb_data_scheduled_set_category_number (gint scheduled_number,
680 												 gint no_category)
681 {
682     ScheduledStruct *scheduled;
683 
684     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
685 
686     if (!scheduled)
687 		return FALSE;
688 
689     scheduled->category_number = no_category;
690 
691     return TRUE;
692 }
693 
694 /**
695  * get the sub_category_number
696  *
697  * \param scheduled_number the number of the scheduled
698  *
699  * \return the sub_category number of the scheduled
700  **/
gsb_data_scheduled_get_sub_category_number(gint scheduled_number)701 gint gsb_data_scheduled_get_sub_category_number (gint scheduled_number)
702 {
703     ScheduledStruct *scheduled;
704 
705     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
706 
707     if (!scheduled)
708 		return -1;
709 
710     return scheduled->sub_category_number;
711 }
712 
713 /**
714  * set the sub_category_number
715  *
716  * \param scheduled_number
717  * \param value
718  *
719  * \return TRUE if ok
720  **/
gsb_data_scheduled_set_sub_category_number(gint scheduled_number,gint no_sub_category)721 gboolean gsb_data_scheduled_set_sub_category_number (gint scheduled_number,
722 													 gint no_sub_category)
723 {
724     ScheduledStruct *scheduled;
725 
726     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
727 
728     if (!scheduled)
729 		return FALSE;
730 
731     scheduled->sub_category_number = no_sub_category;
732 
733     return TRUE;
734 }
735 
736 /**
737  * get if the scheduled is a split_of_scheduled
738  *
739  * \param scheduled_number the number of the scheduled
740  *
741  * \return TRUE if the scheduled is a split of scheduled
742  **/
gsb_data_scheduled_get_split_of_scheduled(gint scheduled_number)743 gint gsb_data_scheduled_get_split_of_scheduled (gint scheduled_number)
744 {
745     ScheduledStruct *scheduled;
746 
747     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
748 
749     if (!scheduled)
750 		return -1;
751 
752     return scheduled->split_of_scheduled;
753 }
754 
755 /**
756  * set if the scheduled is a split_of_scheduled
757  * \param scheduled_number
758  * \param is_split
759  *
760  * \return TRUE if ok
761  **/
gsb_data_scheduled_set_split_of_scheduled(gint scheduled_number,gint is_split)762 gboolean gsb_data_scheduled_set_split_of_scheduled (gint scheduled_number,
763 													gint is_split)
764 {
765     ScheduledStruct *scheduled;
766 
767     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
768 
769     if (!scheduled)
770 		return FALSE;
771 
772     scheduled->split_of_scheduled = is_split;
773 
774     return TRUE;
775 }
776 
777 /**
778  * get the notes
779  *
780  * \param scheduled_number the number of the scheduled
781  *
782  * \return the notes of the scheduled
783  **/
gsb_data_scheduled_get_notes(gint scheduled_number)784 gchar *gsb_data_scheduled_get_notes (gint scheduled_number)
785 {
786     ScheduledStruct *scheduled;
787 
788     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
789 
790     if (!scheduled)
791 		return NULL;
792 
793     return scheduled->notes;
794 }
795 
796 /**
797  * set the notes
798  * the notes parameter will be copy before stored in memory
799  *
800  * \param scheduled_number
801  * \param notes a gchar with the new notes
802  *
803  * \return TRUE if ok
804  **/
gsb_data_scheduled_set_notes(gint scheduled_number,const gchar * notes)805 gboolean gsb_data_scheduled_set_notes (gint scheduled_number,
806 									   const gchar *notes)
807 {
808     ScheduledStruct *scheduled;
809 
810     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
811     if (!scheduled)
812         return FALSE;
813 
814     g_free (scheduled->notes);
815     scheduled->notes = my_strdup (notes);
816 
817     return TRUE;
818 }
819 
820 /**
821  * get the method_of_payment_number
822  *
823  * \param scheduled_number the number of the scheduled
824  *
825  * \return the method_of_payment_number
826  **/
gsb_data_scheduled_get_method_of_payment_number(gint scheduled_number)827 gint gsb_data_scheduled_get_method_of_payment_number (gint scheduled_number)
828 {
829     ScheduledStruct *scheduled;
830 
831     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
832 
833     if (!scheduled)
834 		return -1;
835 
836     return scheduled->method_of_payment_number;
837 }
838 
839 /**
840  * set the method_of_payment_number
841  * if the scheduled has some children, they change too
842  *
843  * \param scheduled_number
844  * \param number
845  *
846  * \return TRUE if ok
847  **/
gsb_data_scheduled_set_method_of_payment_number(gint scheduled_number,gint number)848 gboolean gsb_data_scheduled_set_method_of_payment_number (gint scheduled_number,
849 														  gint number)
850 {
851     ScheduledStruct *scheduled;
852 
853     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
854 
855     if (!scheduled)
856 		return FALSE;
857 
858     scheduled->method_of_payment_number = number;
859 
860     /* if the scheduled is a split, change all the children */
861     if (scheduled->split_of_scheduled)
862     {
863 		GSList *tmp_list;
864 		GSList *save_tmp_list;
865 
866 		tmp_list = gsb_data_scheduled_get_children (scheduled->scheduled_number, FALSE);
867 		save_tmp_list = tmp_list;
868 
869 		while (tmp_list)
870 		{
871 			scheduled = tmp_list->data;
872 			scheduled->method_of_payment_number = number;
873 			tmp_list = tmp_list->next;
874 		}
875 		g_slist_free (save_tmp_list);
876     }
877 
878     return TRUE;
879 }
880 
881 /**
882  * get the method_of_payment_content
883  *
884  * \param scheduled_number the number of the scheduled
885  *
886  * \return the method_of_payment_content of the scheduled
887  **/
gsb_data_scheduled_get_method_of_payment_content(gint scheduled_number)888 gchar *gsb_data_scheduled_get_method_of_payment_content (gint scheduled_number)
889 {
890     ScheduledStruct *scheduled;
891 
892     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
893 
894     if (!scheduled)
895 		return NULL;
896 
897     return scheduled->method_of_payment_content;
898 }
899 
900 /**
901  * set the method_of_payment_content
902  * dupplicate the parameter before storing it in the scheduled
903  *
904  * \param scheduled_number
905  * \param method_of_payment_content a gchar with the new method_of_payment_content
906  *
907  * \return TRUE if ok
908  **/
gsb_data_scheduled_set_method_of_payment_content(gint scheduled_number,const gchar * method_of_payment_content)909 gboolean gsb_data_scheduled_set_method_of_payment_content (gint scheduled_number,
910 														   const gchar *method_of_payment_content)
911 {
912     ScheduledStruct *scheduled;
913 
914     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
915     if (!scheduled)
916         return FALSE;
917 
918     g_free (scheduled->method_of_payment_content);
919     scheduled->method_of_payment_content = my_strdup (method_of_payment_content);
920 
921     return TRUE;
922 }
923 
924 /**
925  * get the automatic_scheduled
926  *
927  * \param scheduled_number the number of the scheduled
928  *
929  * \return 1 if the scheduled was taken automaticly
930  **/
gsb_data_scheduled_get_automatic_scheduled(gint scheduled_number)931 gint gsb_data_scheduled_get_automatic_scheduled (gint scheduled_number)
932 {
933     ScheduledStruct *scheduled;
934 
935     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
936 
937     if (!scheduled)
938 		return -1;
939 
940     return scheduled->automatic_scheduled;
941 }
942 
943 /**
944  * set the automatic_scheduled
945  *
946  * \param scheduled_number
947  * \param  automatic_scheduled
948  *
949  * \return TRUE if ok
950  **/
gsb_data_scheduled_set_automatic_scheduled(gint scheduled_number,gint automatic_scheduled)951 gboolean gsb_data_scheduled_set_automatic_scheduled (gint scheduled_number,
952 													 gint automatic_scheduled)
953 {
954     ScheduledStruct *scheduled;
955 
956     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
957 
958     if (!scheduled)
959 		return FALSE;
960 
961     scheduled->automatic_scheduled = automatic_scheduled;
962 
963     return TRUE;
964 }
965 
966 /**
967  * get the financial_year_number
968  *
969  * \param scheduled_number the number of the scheduled
970  *
971  * \return the financial_year_number
972  **/
gsb_data_scheduled_get_financial_year_number(gint scheduled_number)973 gint gsb_data_scheduled_get_financial_year_number (gint scheduled_number)
974 {
975     ScheduledStruct *scheduled;
976 
977     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
978 
979 	if (!scheduled)
980 		return -1;
981 	else if (scheduled->split_of_scheduled)
982 		return 0;
983 	else
984 		return scheduled->financial_year_number;
985 }
986 
987 /**
988  * set the financial_year_number
989  *
990  * \param scheduled_number
991  * \param  financial_year_number
992  *
993  * \return TRUE if ok
994  **/
gsb_data_scheduled_set_financial_year_number(gint scheduled_number,gint financial_year_number)995 gboolean gsb_data_scheduled_set_financial_year_number (gint scheduled_number,
996 													   gint financial_year_number)
997 {
998     ScheduledStruct *scheduled;
999 
1000     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
1001 
1002     if (!scheduled)
1003 		return FALSE;
1004 
1005     scheduled->financial_year_number = financial_year_number;
1006 
1007     return TRUE;
1008 }
1009 
1010 /**
1011  * get the budgetary_number
1012  *
1013  * \param scheduled_number the number of the scheduled
1014  *
1015  * \return the budgetary_number of the scheduled
1016  **/
gsb_data_scheduled_get_budgetary_number(gint scheduled_number)1017 gint gsb_data_scheduled_get_budgetary_number (gint scheduled_number)
1018 {
1019     ScheduledStruct *scheduled;
1020 
1021     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
1022 
1023     if (!scheduled)
1024 		return -1;
1025 
1026     return scheduled->budgetary_number;
1027 }
1028 
1029 /**
1030  * set the budgetary_number
1031  *
1032  * \param scheduled_number
1033  * \param budgetary_number
1034  *
1035  * \return TRUE if ok
1036  **/
gsb_data_scheduled_set_budgetary_number(gint scheduled_number,gint budgetary_number)1037 gboolean gsb_data_scheduled_set_budgetary_number (gint scheduled_number,
1038 												  gint budgetary_number)
1039 {
1040     ScheduledStruct *scheduled;
1041 
1042     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
1043 
1044     if (!scheduled)
1045 		return FALSE;
1046 
1047     scheduled->budgetary_number = budgetary_number;
1048 
1049     return TRUE;
1050 }
1051 
1052 /**
1053  * get the  sub_budgetary_number
1054  *
1055  * \param scheduled_number the number of the scheduled
1056  *
1057  * \return the sub_budgetary_number number of the scheduled
1058  **/
gsb_data_scheduled_get_sub_budgetary_number(gint scheduled_number)1059 gint gsb_data_scheduled_get_sub_budgetary_number (gint scheduled_number)
1060 {
1061     ScheduledStruct *scheduled;
1062 
1063     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
1064 
1065     if (!scheduled)
1066 		return -1;
1067 
1068     return scheduled->sub_budgetary_number;
1069 }
1070 
1071 /**
1072  * set the sub_budgetary_number
1073  *
1074  * \param scheduled_number
1075  * \param sub_budgetary_number
1076  *
1077  * \return TRUE if ok
1078  **/
gsb_data_scheduled_set_sub_budgetary_number(gint scheduled_number,gint sub_budgetary_number)1079 gboolean gsb_data_scheduled_set_sub_budgetary_number (gint scheduled_number,
1080 													  gint sub_budgetary_number)
1081 {
1082     ScheduledStruct *scheduled;
1083 
1084     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
1085 
1086     if (!scheduled)
1087 		return FALSE;
1088 
1089     scheduled->sub_budgetary_number = sub_budgetary_number;
1090 
1091     return TRUE;
1092 }
1093 
1094 /**
1095  * this function check if the scheduled transaction is a transfer or
1096  * not, to avoid many lines in the source code
1097  *
1098  * \param scheduled_number
1099  *
1100  * \return TRUE : is a transfer, FALSE : is not a transfer
1101  **/
gsb_data_scheduled_is_transfer(gint scheduled_number)1102 gboolean gsb_data_scheduled_is_transfer (gint scheduled_number)
1103 {
1104     ScheduledStruct *scheduled;
1105 
1106     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
1107 
1108     if (!scheduled)
1109 		return FALSE;
1110 
1111     /* it's more complex than for a transaction, because no transaction number to transfer,
1112      * we have only account_number_transfer, and it can be 0...
1113      * first, need to check if it's -1 => neither transfer, neither categ */
1114 
1115     if (scheduled->account_number_transfer == -1)
1116 		return FALSE;
1117 
1118     /* next, if account_number_transfer is > 0, it's a transfer */
1119     if (scheduled->account_number_transfer > 0)
1120 		return TRUE;
1121 
1122     /* ok, now we have an account_number_transfer at 0, so it can be
1123      * a normal scheduled transactions (with categs), or split */
1124     if (scheduled->category_number || scheduled->split_of_scheduled)
1125 		return FALSE;
1126 
1127     return TRUE;
1128 }
1129 
1130 /**
1131  * get the  account_number_transfer
1132  * \param scheduled_number the number of the scheduled
1133  *
1134  * \return the account_number_transfer number of the scheduled
1135  **/
gsb_data_scheduled_get_account_number_transfer(gint scheduled_number)1136 gint gsb_data_scheduled_get_account_number_transfer (gint scheduled_number)
1137 {
1138     ScheduledStruct *scheduled;
1139 
1140     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
1141 
1142     if (!scheduled)
1143 		return -1;
1144 
1145     return scheduled->account_number_transfer;
1146 }
1147 
1148 /**
1149  * set the account_number_transfer
1150  *
1151  * \param scheduled_number
1152  * \param account_number_transfer
1153  *
1154  * \return TRUE if ok
1155  **/
gsb_data_scheduled_set_account_number_transfer(gint scheduled_number,gint account_number_transfer)1156 gboolean gsb_data_scheduled_set_account_number_transfer (gint scheduled_number,
1157 														 gint account_number_transfer)
1158 {
1159     ScheduledStruct *scheduled;
1160 
1161     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
1162 
1163     if (!scheduled)
1164 		return FALSE;
1165 
1166     scheduled->account_number_transfer = account_number_transfer;
1167 
1168     return TRUE;
1169 }
1170 
1171 /**
1172  * get the  mother_scheduled_number
1173  *
1174  * \param scheduled_number the number of the scheduled
1175  *
1176  * \return the mother_scheduled_number of the scheduled or 0 if the scheduled doen't exist
1177  **/
gsb_data_scheduled_get_mother_scheduled_number(gint scheduled_number)1178 gint gsb_data_scheduled_get_mother_scheduled_number (gint scheduled_number)
1179 {
1180     ScheduledStruct *scheduled;
1181 
1182     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
1183 
1184     if (!scheduled)
1185 		return 0;
1186 
1187     return scheduled->mother_scheduled_number;
1188 }
1189 
1190 /**
1191  * set the mother_scheduled_number
1192  *
1193  * \param scheduled_number
1194  * \param mother_scheduled_number
1195  *
1196  * \return TRUE if ok
1197  **/
gsb_data_scheduled_set_mother_scheduled_number(gint scheduled_number,gint mother_scheduled_number)1198 gboolean gsb_data_scheduled_set_mother_scheduled_number (gint scheduled_number,
1199 														 gint mother_scheduled_number)
1200 {
1201     ScheduledStruct *scheduled;
1202 
1203     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
1204 
1205     if (!scheduled)
1206 		return FALSE;
1207 
1208     scheduled->mother_scheduled_number = mother_scheduled_number;
1209 
1210     return TRUE;
1211 }
1212 
1213 /**
1214  * get the contra_method_of_payment_number
1215  *
1216  * \param scheduled_number the number of the scheduled
1217  *
1218  * \return the contra_method_of_payment_number
1219  **/
gsb_data_scheduled_get_contra_method_of_payment_number(gint scheduled_number)1220 gint gsb_data_scheduled_get_contra_method_of_payment_number (gint scheduled_number)
1221 {
1222     ScheduledStruct *scheduled;
1223 
1224     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
1225 
1226     if (!scheduled)
1227 		return -1;
1228 
1229     return scheduled->contra_method_of_payment_number;
1230 }
1231 
1232 /**
1233  * set the contra_method_of_payment_number
1234  *
1235  * \param scheduled_number
1236  * \param
1237  *
1238  * \return TRUE if ok
1239  **/
gsb_data_scheduled_set_contra_method_of_payment_number(gint scheduled_number,gint number)1240 gboolean gsb_data_scheduled_set_contra_method_of_payment_number (gint scheduled_number,
1241 																 gint number)
1242 {
1243     ScheduledStruct *scheduled;
1244 
1245     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
1246 
1247     if (!scheduled)
1248 		return FALSE;
1249 
1250     scheduled->contra_method_of_payment_number = number;
1251 
1252     return TRUE;
1253 }
1254 
1255 /**
1256  * get the frequency
1257  *
1258  * \param scheduled_number the number of the scheduled
1259  *
1260  * \return the frequency
1261  **/
gsb_data_scheduled_get_frequency(gint scheduled_number)1262 gint gsb_data_scheduled_get_frequency (gint scheduled_number)
1263 {
1264     ScheduledStruct *scheduled;
1265 
1266     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
1267 
1268     if (!scheduled)
1269 		return -1;
1270 
1271     return scheduled->frequency;
1272 }
1273 
1274 /**
1275  * set the frequency
1276  *
1277  * \param scheduled_number
1278  * \param
1279  *
1280  * \return TRUE if ok
1281  **/
gsb_data_scheduled_set_frequency(gint scheduled_number,gint number)1282 gboolean gsb_data_scheduled_set_frequency (gint scheduled_number,
1283 										   gint number)
1284 {
1285     ScheduledStruct *scheduled;
1286 
1287     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
1288 
1289     if (!scheduled)
1290 		return FALSE;
1291 
1292     scheduled->frequency = number;
1293 
1294     return TRUE;
1295 }
1296 
1297 /**
1298  * get the user_interval
1299  *
1300  * \param scheduled_number the number of the scheduled
1301  *
1302  * \return the user_interval
1303  **/
gsb_data_scheduled_get_user_interval(gint scheduled_number)1304 gint gsb_data_scheduled_get_user_interval (gint scheduled_number)
1305 {
1306     ScheduledStruct *scheduled;
1307 
1308     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
1309 
1310     if (!scheduled)
1311 		return -1;
1312 
1313     return scheduled->user_interval;
1314 }
1315 
1316 /**
1317  * set the user_interval
1318  *
1319  * \param scheduled_number
1320  * \param
1321  *
1322  * \return TRUE if ok
1323  **/
gsb_data_scheduled_set_user_interval(gint scheduled_number,gint number)1324 gboolean gsb_data_scheduled_set_user_interval (gint scheduled_number,
1325 											   gint number)
1326 {
1327     ScheduledStruct *scheduled;
1328 
1329     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
1330 
1331     if (!scheduled)
1332 		return FALSE;
1333 
1334     scheduled->user_interval = number;
1335 
1336     return TRUE;
1337 }
1338 
1339 /**
1340  * get the user_entry
1341  *
1342  * \param scheduled_number the number of the scheduled
1343  *
1344  * \return the user_entry
1345  **/
gsb_data_scheduled_get_user_entry(gint scheduled_number)1346 gint gsb_data_scheduled_get_user_entry (gint scheduled_number)
1347 {
1348     ScheduledStruct *scheduled;
1349 
1350     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
1351 
1352     if (!scheduled)
1353 		return -1;
1354 
1355     return scheduled->user_entry;
1356 }
1357 
1358 /**
1359  * set the user_entry
1360  *
1361  * \param scheduled_number
1362  * \param
1363  *
1364  * \return TRUE if ok
1365  **/
gsb_data_scheduled_set_user_entry(gint scheduled_number,gint number)1366 gboolean gsb_data_scheduled_set_user_entry (gint scheduled_number,
1367 											gint number)
1368 {
1369     ScheduledStruct *scheduled;
1370 
1371     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
1372 
1373     if (!scheduled)
1374 		return FALSE;
1375 
1376     scheduled->user_entry = number;
1377 
1378     return TRUE;
1379 }
1380 
1381 /**
1382  * get the limit_GDate of the scheduled
1383  *
1384  * \param scheduled_number the number of the scheduled
1385  *
1386  * \return the limit_GDate of the scheduled
1387  **/
gsb_data_scheduled_get_limit_date(gint scheduled_number)1388 GDate *gsb_data_scheduled_get_limit_date (gint scheduled_number)
1389 {
1390     ScheduledStruct *scheduled;
1391 
1392     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
1393 
1394     if (!scheduled)
1395 		return NULL;
1396 
1397     return scheduled->limit_date;
1398 }
1399 
1400 /**
1401  * set the limit_GDate of the scheduled
1402  *
1403  * \param scheduled_number
1404  * \param no_account
1405  *
1406  * \return TRUE if ok
1407  **/
gsb_data_scheduled_set_limit_date(gint scheduled_number,const GDate * date)1408 gboolean gsb_data_scheduled_set_limit_date (gint scheduled_number,
1409 											const GDate *date)
1410 {
1411     ScheduledStruct *scheduled;
1412 
1413     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
1414 
1415     if (!scheduled)
1416 		return FALSE;
1417 
1418     if (scheduled->limit_date)
1419 	g_date_free (scheduled->limit_date);
1420 
1421     scheduled->limit_date = gsb_date_copy (date);
1422 
1423     return TRUE;
1424 }
1425 
1426 /**
1427  * create a new scheduled and append it to the list in the right account
1428  * set the scheduled number given in param (if no number, give the last number + 1)
1429  * set the number of the account, the number of the scheduled and the currency number
1430  * which is by default the currency of the account
1431  *
1432  * \param scheduled_number the number of the scheduled
1433  *
1434  * \return the number of the new scheduled, 0 if problem
1435  **/
gsb_data_scheduled_new_scheduled_with_number(gint scheduled_number)1436 gint gsb_data_scheduled_new_scheduled_with_number (gint scheduled_number)
1437 {
1438     ScheduledStruct *scheduled;
1439 
1440     scheduled = g_malloc0 (sizeof (ScheduledStruct));
1441 
1442     if (!scheduled)
1443     {
1444         dialogue_error_memory ();
1445         return FALSE;
1446     }
1447 
1448     if (!scheduled_number)
1449         scheduled_number = gsb_data_scheduled_get_last_number () + 1;
1450 
1451     scheduled->scheduled_number = scheduled_number;
1452 
1453     scheduled_list = g_slist_append (scheduled_list, scheduled);
1454 
1455     gsb_data_scheduled_save_scheduled_pointer (scheduled);
1456 
1457     return scheduled->scheduled_number;
1458 }
1459 
1460 /**
1461  * create a new scheduled with gsb_data_scheduled_new_scheduled_with_number
1462  * but set automatickly the last number
1463  *
1464  * \param
1465  *
1466  * \return the number of the new scheduled
1467  **/
gsb_data_scheduled_new_scheduled(void)1468 gint gsb_data_scheduled_new_scheduled (void)
1469 {
1470     return gsb_data_scheduled_new_scheduled_with_number (gsb_data_scheduled_get_last_number () + 1);
1471 }
1472 
1473 /**
1474  * create a new white line
1475  * if there is a mother scheduled, it's a split and we increment in the negatives values
1476  * the number of that line
1477  * without mother scheduled, it's the general white line, the number is -1
1478  *
1479  * if it's a child split, the account is set as for its mother,
1480  * if it's the last white line, the account is set to -1
1481  * that scheduled is appended to the white scheduleds list
1482  *
1483  * \param mother_scheduled_number the number of the mother's scheduled if it's a split child ; 0 if not
1484  *
1485  * \return the number of the white line
1486  *
1487  **/
gsb_data_scheduled_new_white_line(gint mother_scheduled_number)1488 gint gsb_data_scheduled_new_white_line (gint mother_scheduled_number)
1489 {
1490     ScheduledStruct *scheduled;
1491 
1492     scheduled = g_malloc0 (sizeof (ScheduledStruct));
1493 
1494     if (!scheduled)
1495     {
1496 		dialogue_error (_("Cannot allocate memory, bad things will happen soon"));
1497 		/* to avoid more error, return the number of the general white line... */
1498 		return -1;
1499     }
1500 
1501     /* we fill some things for the child split to help to sort the list */
1502 
1503     if (mother_scheduled_number)
1504     {
1505 		scheduled->scheduled_number = gsb_data_scheduled_get_last_white_number () - 1;
1506 		scheduled->mother_scheduled_number = mother_scheduled_number;
1507 		scheduled->account_number = gsb_data_scheduled_get_account_number (mother_scheduled_number);
1508     }
1509     else
1510     {
1511 		scheduled->scheduled_number = -1;
1512 		scheduled->account_number = -1;
1513     }
1514 
1515     white_scheduled_list = g_slist_append (white_scheduled_list, scheduled);
1516 
1517     gsb_data_scheduled_save_scheduled_pointer (scheduled);
1518 
1519     return scheduled->scheduled_number;
1520 }
1521 
1522 /**
1523  * remove the scheduled from the scheduled's list
1524  * free the scheduled, if there's some children,
1525  * remove also the children
1526  *
1527  * \param scheduled_number
1528  *
1529  * \return TRUE if ok
1530  **/
gsb_data_scheduled_remove_scheduled(gint scheduled_number)1531 gboolean gsb_data_scheduled_remove_scheduled (gint scheduled_number)
1532 {
1533     ScheduledStruct *scheduled;
1534 
1535     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
1536 
1537     if (!scheduled)
1538         return FALSE;
1539 
1540     if (scheduled->split_of_scheduled)
1541     {
1542         GSList *list_tmp;
1543 
1544         list_tmp = gsb_data_scheduled_get_children (scheduled_number, FALSE);
1545 
1546         while (list_tmp)
1547         {
1548             ScheduledStruct *scheduled_child;
1549 
1550             scheduled_child = list_tmp->data;
1551 
1552             if (scheduled_child)
1553             {
1554                 scheduled_list = g_slist_remove (scheduled_list, scheduled_child);
1555                 _gsb_data_scheduled_free (scheduled_child);
1556             }
1557             list_tmp = list_tmp->next;
1558         }
1559     }
1560 
1561     scheduled_list = g_slist_remove (scheduled_list, scheduled);
1562 
1563     _gsb_data_scheduled_free (scheduled);
1564 
1565     gsb_file_set_modified (TRUE);
1566 
1567     return TRUE;
1568 }
1569 
1570 /**
1571  * find the children of the split given in param and
1572  * return their numbers or their adress in a GSList
1573  * the list sould be freed
1574  *
1575  * \param scheduled_number a split of scheduled transaction
1576  * \param return_number TRUE if we want a list of number, FALSE if we want a list of struct adr
1577  *
1578  * \return a GSList of the numbers/adress of the children, NULL if no child
1579  **/
gsb_data_scheduled_get_children(gint scheduled_number,gboolean return_number)1580 GSList *gsb_data_scheduled_get_children (gint scheduled_number,
1581 										 gboolean return_number)
1582 {
1583     ScheduledStruct *scheduled;
1584     GSList *children_list = NULL;
1585     GSList *tmp_list;
1586 
1587     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
1588 
1589     if (!scheduled || !scheduled->split_of_scheduled)
1590 		return NULL;
1591 
1592     /* get the normal children */
1593     tmp_list = scheduled_list;
1594     while (tmp_list)
1595     {
1596 		ScheduledStruct *tmp_scheduled;
1597 
1598 		tmp_scheduled = tmp_list->data;
1599 
1600 		if (tmp_scheduled->mother_scheduled_number == scheduled_number)
1601 		{
1602 			if (return_number)
1603 				children_list = g_slist_append (children_list,
1604 												GINT_TO_POINTER (tmp_scheduled->scheduled_number));
1605 			else
1606 				children_list = g_slist_append (children_list, tmp_scheduled);
1607 		}
1608 		tmp_list = tmp_list->next;
1609     }
1610 
1611     /* get the white line too */
1612     tmp_list = white_scheduled_list;
1613     while (tmp_list)
1614     {
1615 		ScheduledStruct *tmp_scheduled;
1616 
1617 		tmp_scheduled = tmp_list->data;
1618 
1619 		if (tmp_scheduled->mother_scheduled_number == scheduled_number)
1620 		{
1621 			if (return_number)
1622 				children_list = g_slist_append (children_list,
1623 												GINT_TO_POINTER (tmp_scheduled->scheduled_number));
1624 			else
1625 				children_list = g_slist_append (children_list, tmp_scheduled);
1626 		}
1627 		tmp_list = tmp_list->next;
1628     }
1629 
1630     return children_list;
1631 }
1632 
1633 /**
1634  * find the white line corresponding to the scheduled transaction
1635  * given in param and return the number
1636  * if the scheduled is not a split, return -1, the general white line
1637  *
1638  * \param scheduled_number a split scheduled number
1639  *
1640  * \return the number of the white line of the split or -1
1641  **/
gsb_data_scheduled_get_white_line(gint scheduled_number)1642 gint gsb_data_scheduled_get_white_line (gint scheduled_number)
1643 {
1644     ScheduledStruct *scheduled;
1645     GSList *tmp_list;
1646 
1647     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
1648 
1649     if (!scheduled || !scheduled->split_of_scheduled)
1650        return -1;
1651 
1652     tmp_list = white_scheduled_list;
1653 
1654     while (tmp_list)
1655     {
1656 		ScheduledStruct *tmp_scheduled;
1657 
1658 		tmp_scheduled = tmp_list->data;
1659 
1660 		if (tmp_scheduled->mother_scheduled_number == scheduled_number)
1661 			return tmp_scheduled->scheduled_number;
1662 
1663 		tmp_list = tmp_list->next;
1664     }
1665     return -1;
1666 }
1667 
1668 /**
1669  * get floating point of the currency of the scheduled given
1670  *
1671  * \param scheduled_number the number of the scheduled
1672  *
1673  * \return the floating_point of currency number of the scheduled
1674  **/
gsb_data_scheduled_get_currency_floating_point(gint scheduled_number)1675 gint gsb_data_scheduled_get_currency_floating_point (gint scheduled_number)
1676 {
1677     ScheduledStruct *scheduled;
1678     gint floating_point;
1679 
1680     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
1681 
1682     if (!scheduled)
1683         return -1;
1684     else
1685     {
1686         floating_point = gsb_data_currency_get_floating_point (scheduled->currency_number);
1687         return floating_point;
1688     }
1689 }
1690 
1691 /**
1692  * get the amount of the scheduled, modified to be ok with the currency
1693  * given in param
1694  *
1695  * \param scheduled_number 		the number of the scheduled
1696  * \param return_currency_number 	the currency we want to adjust the transaction's amount
1697  * \param return_exponent 		the exponent we want to have for the returned number, or -1 for the exponent of the returned currency
1698  *
1699  * \return the amount of the transaction
1700  **/
gsb_data_scheduled_get_adjusted_amount_for_currency(gint scheduled_number,gint return_currency_number,gint return_exponent)1701 GsbReal gsb_data_scheduled_get_adjusted_amount_for_currency (gint scheduled_number,
1702 															 gint return_currency_number,
1703 															 gint return_exponent)
1704 {
1705     ScheduledStruct *scheduled;
1706     GsbReal amount = null_real;
1707     gint link_number;
1708 
1709     if (return_exponent == -1)
1710         return_exponent = gsb_data_currency_get_floating_point (return_currency_number);
1711 
1712     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
1713 
1714     if (!(scheduled && return_currency_number))
1715         return gsb_real_adjust_exponent  (null_real, return_exponent);
1716 
1717     /* if the transaction currency is the same of the account's one,
1718      * we just return the transaction's amount */
1719     if (scheduled->currency_number == return_currency_number)
1720         return gsb_real_adjust_exponent  (scheduled->scheduled_amount, return_exponent);
1721 
1722     /* now we can adjust the amount */
1723     if ((link_number = gsb_data_currency_link_search (scheduled->currency_number,
1724 													  return_currency_number)))
1725     {
1726 	/* there is a hard link between the transaction currency and the return currency */
1727         if (gsb_data_currency_link_get_first_currency (link_number) == scheduled->currency_number)
1728             amount = gsb_real_mul (scheduled->scheduled_amount,
1729 								   gsb_data_currency_link_get_change_rate (link_number));
1730         else
1731             amount = gsb_real_div (scheduled->scheduled_amount,
1732 								   gsb_data_currency_link_get_change_rate (link_number));
1733 
1734     }
1735     else if (return_currency_number > 0 && scheduled->currency_number > 0)
1736     {
1737         gchar *tmp_str;
1738 
1739         tmp_str = g_strdup (_("Error: is missing one or more links between currencies.\n"
1740 							  "You need to fix it and start over."));
1741         dialogue_error (tmp_str);
1742 
1743         g_free (tmp_str);
1744     }
1745 
1746     return gsb_real_adjust_exponent  (amount, return_exponent);
1747 }
1748 
1749 /**
1750  * copy the content of a scheduled transaction into the second one
1751  * the 2 scheduled transactions must exist before
1752  * only the scheduled_number will be modified in the target transaction
1753  *
1754  * \param source_scheduled_number the transaction we want to copy
1755  * \param target_scheduled_number the transaction we want to fill with the content of the first one
1756  *
1757  * \return TRUE if ok, FALSE else
1758  **/
gsb_data_scheduled_copy_scheduled(gint source_scheduled_number,gint target_scheduled_number)1759 gboolean gsb_data_scheduled_copy_scheduled (gint source_scheduled_number,
1760 											gint target_scheduled_number)
1761 {
1762     ScheduledStruct *source_scheduled;
1763     ScheduledStruct *target_scheduled;
1764 
1765     source_scheduled = gsb_data_scheduled_get_scheduled_by_no (source_scheduled_number);
1766     target_scheduled = gsb_data_scheduled_get_scheduled_by_no (target_scheduled_number);
1767 
1768     if (!source_scheduled || !target_scheduled)
1769         return FALSE;
1770 
1771     memcpy (target_scheduled, source_scheduled, sizeof (ScheduledStruct));
1772 
1773     target_scheduled->scheduled_number = target_scheduled_number;
1774 
1775     /* make a new copy of all the pointers */
1776     if (target_scheduled->notes)
1777         target_scheduled->notes = my_strdup (source_scheduled->notes);
1778 
1779     if (target_scheduled->date)
1780         target_scheduled->date = gsb_date_copy (source_scheduled->date);
1781 
1782     if (target_scheduled->limit_date)
1783         target_scheduled->limit_date = gsb_date_copy (source_scheduled->limit_date);
1784 
1785     if (target_scheduled->method_of_payment_content)
1786         target_scheduled->method_of_payment_content = my_strdup (source_scheduled->method_of_payment_content);
1787     return TRUE;
1788 }
1789 
1790 /**
1791  *
1792  *
1793  * \param	mother_scheduled_number
1794  *
1795  * \return TRUE si la variance de l'opération ventilée == 0
1796  **/
gsb_data_scheduled_get_variance(gint mother_scheduled_number)1797 gboolean gsb_data_scheduled_get_variance (gint mother_scheduled_number)
1798 {
1799     GSList *tmp_list;
1800     GsbReal total_split = null_real;
1801     GsbReal variance;
1802 
1803     tmp_list = scheduled_list;
1804     while (tmp_list)
1805     {
1806         ScheduledStruct *tmp_scheduled;
1807 
1808         tmp_scheduled = tmp_list->data;
1809 
1810         if (tmp_scheduled->mother_scheduled_number == mother_scheduled_number)
1811         {
1812             total_split = gsb_real_add (total_split,
1813 										gsb_data_scheduled_get_amount (tmp_scheduled->scheduled_number));
1814         }
1815         tmp_list = tmp_list->next;
1816     }
1817 
1818     variance = gsb_real_sub (gsb_data_scheduled_get_amount (mother_scheduled_number), total_split);
1819 
1820     if (variance.mantissa == 0)
1821         return TRUE;
1822     else
1823         return FALSE;
1824 }
1825 
1826 /**
1827  *
1828  *
1829  * \param	scheduled_number
1830  *
1831  * \return	0=flottant 1=fixed 2=last_banking_day
1832  **/
gsb_data_scheduled_get_fixed_date(gint scheduled_number)1833 gint gsb_data_scheduled_get_fixed_date (gint scheduled_number)
1834 {
1835     ScheduledStruct *scheduled;
1836 
1837     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
1838 
1839     if (!scheduled)
1840 		return 0;
1841 
1842     return scheduled->fixed_date;
1843 }
1844 
1845 /**
1846  *
1847  *
1848  * \param	scheduled number
1849  * \param	fixed date
1850  *
1851  * \return TRUE if set or FALSE is not set
1852  **/
gsb_data_scheduled_set_fixed_date(gint scheduled_number,gint fixed_date)1853 gboolean gsb_data_scheduled_set_fixed_date (gint scheduled_number,
1854 											gint fixed_date)
1855 {
1856     ScheduledStruct *scheduled;
1857 
1858     scheduled = gsb_data_scheduled_get_scheduled_by_no (scheduled_number);
1859 
1860     if (!scheduled)
1861 		return FALSE;
1862 
1863     scheduled->fixed_date = fixed_date;
1864 
1865     return TRUE;
1866 }
1867 
1868 /**
1869  *
1870  *
1871  * \param
1872  *
1873  * \return
1874  **/
1875 /* Local Variables: */
1876 /* c-basic-offset: 4 */
1877 /* End: */
1878