File: gsb_form.c

package info (click to toggle)
grisbi 1.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 28,808 kB
  • sloc: ansic: 161,477; sh: 4,559; makefile: 918; xml: 580; perl: 370
file content (3515 lines) | stat: -rw-r--r-- 117,129 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
/* ************************************************************************** */
/*                                                                            */
/*     copyright (c)    2000-2008 Cédric Auger (cedric@grisbi.org)            */
/*          2004-2008 Benjamin Drieu (bdrieu@april.org)                       */
/*                 2009-2018 Pierre Biava (grisbi@pierre.biava.name)          */
/*          http://www.grisbi.org                                             */
/*                                                                            */
/*  This program is free software; you can redistribute it and/or modify      */
/*  it under the terms of the GNU General Public License as published by      */
/*  the Free Software Foundation; either version 2 of the License, or         */
/*  (at your option) any later version.                                       */
/*                                                                            */
/*  This program is distributed in the hope that it will be useful,           */
/*  but WITHOUT ANY WARRANTY; without even the implied warranty of            */
/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             */
/*  GNU General Public License for more details.                              */
/*                                                                            */
/*  You should have received a copy of the GNU General Public License         */
/*  along with this program; if not, write to the Free Software               */
/*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
/*                                                                            */
/* ************************************************************************** */

/**
 * \file gsb_form.c
 * functions working on the transactions and scheduled form
 */


#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "include.h"
#include <gdk/gdkkeysyms.h>
#include <glib/gi18n.h>

/*START_INCLUDE*/
#include "gsb_form.h"
#include "accueil.h"
#include "bet_data.h"
#include "bet_finance_ui.h"
#include "dialog.h"
#include "grisbi_app.h"
#include "grisbi_prefs.h"
#include "gsb_calendar.h"
#include "gsb_calendar_entry.h"
#include "gsb_combo_box.h"
#include "gsb_currency.h"
#include "gsb_data_account.h"
#include "gsb_data_budget.h"
#include "gsb_data_category.h"
#include "gsb_data_currency.h"
#include "gsb_data_form.h"
#include "gsb_data_mix.h"
#include "gsb_data_payee.h"
#include "gsb_data_payment.h"
#include "gsb_data_reconcile.h"
#include "gsb_data_report.h"
#include "gsb_data_scheduled.h"
#include "gsb_data_transaction.h"
#include "gsb_file.h"
#include "gsb_form_scheduler.h"
#include "gsb_form_transaction.h"
#include "gsb_form_widget.h"
#include "gsb_fyear.h"
#include "gsb_locale.h"
#include "gsb_payment_method.h"
#include "gsb_real.h"
#include "gsb_reconcile.h"
#include "gsb_report.h"
#include "gsb_scheduler.h"
#include "gsb_scheduler_list.h"
#include "gsb_transactions_list.h"
#include "gtk_combofix.h"
#include "menu.h"
#include "mouse.h"
#include "navigation.h"
#include "structures.h"
#include "tiers_onglet.h"
#include "traitement_variables.h"
#include "transaction_list.h"
#include "transaction_list_select.h"
#include "utils.h"
#include "utils_buttons.h"
#include "utils_dates.h"
#include "utils_editables.h"
#include "utils_operations.h"
#include "utils_real.h"
#include "utils_str.h"
#include "erreur.h"
/*END_INCLUDE*/

/*START_EXTERN*/
/*END_EXTERN*/

/*START_GLOBAL*/
/** when the automatic complete transaction is done
 * for a split of transaction, we propose to recover too
 * the children with that button */
/*END_GLOBAL*/

/*START_STATIC*/
/** the 3 parts of the form :
 * for scheduled transactions
 * for transactions
 * the buttons valid/cancel */
static GtkWidget *form_scheduled_part;
static GtkWidget *form_transaction_part;
static GtkWidget *form_button_part;

/** need to set the 2 buttons valid/cancel here and cannot */
/* just show/hide the form_button_part because of the split button */
static GtkWidget *form_button_cancel;
static GtkWidget *form_button_recover_split;
static GtkWidget *form_button_valid;

static GtkWidget *transaction_form;
static GDate *save_form_date;
static gchar *save_entry;

/* block the size allocate signal to avoid to move several times */
/* the tree view when open the form */
static gboolean block_size_allocate = FALSE;
 /*END_STATIC*/

/******************************************************************************/
/* Private Methods                                                            */
/******************************************************************************/
/**
 * called by a click on the form
 * propose to configure the form with the right click
 *
 * \param vbox
 * \param ev
 * \param null
 *
 * \return FALSE
 **/
static gboolean gsb_form_button_press (GtkWidget *vbox,
									   GdkEventButton *ev,
									   gpointer null)
{
    GtkWidget *menu;
    GtkWidget *menu_item;

    devel_debug (NULL);

    if (ev->button != RIGHT_BUTTON)
        return FALSE;

    menu = gtk_menu_new ();

    /* propose to configure the form */
    menu_item = gtk_menu_item_new_with_label (_("Configure the form"));
    g_signal_connect_swapped (G_OBJECT(menu_item),
							  "activate",
							  G_CALLBACK (grisbi_prefs_set_page_by_name),
							  "form_num_page");

	gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);

    /* Finish all. */
    gtk_widget_show_all (menu);

#if GTK_CHECK_VERSION (3,22,0)
	gtk_menu_popup_at_pointer (GTK_MENU (menu), NULL);
#else
    gtk_menu_popup (GTK_MENU(menu), NULL, NULL, NULL, NULL, 3, gtk_get_current_event_time());
#endif

    return FALSE;
}

/**
 * check if the string in the param is like "Transfer : account"
 * if yes, return the number of the account
 * if deleted account, return -2
 * if no return -1
 *
 * \param entry_string
 *
 * \return the number of the transfer_account
 * 		-1 if transfer to a non existant account
 * 		-2 if deleted account
 * 		-3 if not a transfer
 **/
static gint gsb_form_check_for_transfer (const gchar *entry_string)
{
    const gchar *account_name;

    if (!entry_string || strncmp (entry_string, _("Transfer : "), strlen (_("Transfer : "))))
	 return -3;

    account_name = memchr (entry_string, ':', strlen (entry_string));

	/* after the : there is a space before the account name */
    account_name = account_name + 2;

    if (strcmp (account_name, _("Deleted account")))
		return gsb_data_account_get_no_account_by_name (account_name);
    else
		return -2;
}

/**
 * get the category in the form, for transaction and scheduled transaction
 * do everything needed by that category (create contra-transaction...)
 *
 * \param transaction_number 	the transaction which work with
 * \param new_transaction 		1 if it's a new_transaction
 * \param is_transaction 		TRUE if it's for a transaction, FALSE for a scheduled transaction
 *
 * \return FALSE
 **/
static gboolean gsb_form_get_categories (gint transaction_number,
										 gint new_transaction,
										 gboolean is_transaction)
{
    GtkWidget *category_combofix;

    devel_debug_int (transaction_number);

    if (!gsb_data_form_check_for_value (TRANSACTION_FORM_CATEGORY))
		return FALSE;

    category_combofix = gsb_form_widget_get_widget (TRANSACTION_FORM_CATEGORY);

    if (gsb_form_widget_check_empty (category_combofix))
    {
		/* there is no category */
		gsb_data_mix_set_category_number (transaction_number, 0, is_transaction);
		gsb_data_mix_set_sub_category_number (transaction_number, 0, is_transaction);

		/* if it's a scheduled transaction, we need to set account_transfer to -1 */
		if (!is_transaction)
			gsb_data_scheduled_set_account_number_transfer (transaction_number, -1);
    }
    else
    {
		gchar *string;
		gint contra_transaction_number;

		/* On protège la chaine pendant toute l'opération */
		string = g_strdup (gtk_combofix_get_text (GTK_COMBOFIX (category_combofix)));

		if (strcmp (string, _("Split of transaction")))
		{
			/* it's not a split of transaction, if it was one, we delete the
			 * transaction's daughters */
			gint account_transfer;

			/* carreful : must set == 1 and not != 0 because if problem to get the result,
			 * that function returns -1 */
			if (!new_transaction
				&&
				gsb_data_mix_get_split_of_transaction (transaction_number, is_transaction) == 1)
			{
				/* we try to modify a split to a non split transaction */
				GSList *children_list;

				/* get the number list of children */
				children_list = gsb_data_mix_get_children (transaction_number, TRUE, is_transaction);

				/* if there is some children, we ask to be sure and delete them */
				if (children_list)
				{
					GSList *save_children_list;

					if (!question_yes_no (_("You are trying to change a split of transaction to another "
											"kind of transaction.\n"
											"There is some children to that transaction, if you continue, "
											"the children will  be deleted.\nAre you sure?"),
										  _("Modifying a transaction"),
										  GTK_RESPONSE_OK))
						return FALSE;

					save_children_list = children_list;
					do
					{
						gint transaction_number_tmp;

						transaction_number_tmp = GPOINTER_TO_INT (save_children_list->data);

						if (is_transaction)
							gsb_transactions_list_delete_transaction (transaction_number_tmp, FALSE);
						else
							gsb_scheduler_list_delete_scheduled_transaction (transaction_number_tmp, FALSE);

						save_children_list = save_children_list->next;
					}
					while (save_children_list);

					g_slist_free (save_children_list);
				}
				gsb_data_mix_set_split_of_transaction (transaction_number, 0, is_transaction);
			}

			/* now, check if it's a transfer or a normal category */
			account_transfer = gsb_form_check_for_transfer (string);
			switch (account_transfer)
			{
				/* if the check returns -3, it's not a transfer, so it's a normal category */
				case -3:
					/* if it's a modification, check if before it was not a transfer and delete
					 * the contra-transaction if necessary */
					if (!new_transaction)
					{
						if (is_transaction)
						{
							contra_transaction_number = gsb_data_transaction_get_contra_transaction_number
								(transaction_number);
							if (contra_transaction_number > 0)
							{
								/* it was a transfer, we delete the contra-transaction */
								gsb_data_transaction_set_contra_transaction_number (contra_transaction_number, 0);
								gsb_transactions_list_delete_transaction (contra_transaction_number, FALSE);
								gsb_data_transaction_set_contra_transaction_number (transaction_number, 0);
							}
						}
						else
							/* if the scheduled transaction was a transfer, it is not anymore, remove that */
							gsb_data_scheduled_set_account_number_transfer (transaction_number, 0);
					}
					gsb_data_category_fill_transaction_by_string (transaction_number, string, is_transaction);
					break;

				case -2:
					/* if the check returns -2, it's a deleted account, so set -1 for transaction number transfer
					 * normally cannot come here if scheduled transaction, but in case,
					 * we set data_mix to protect */
					gsb_data_mix_set_transaction_number_transfer (transaction_number, -1, is_transaction);
					/* we don't set any break here, so with the case -1 the
					 * category will be set to 0 (!!let the case -1 after here) */

					/* FALLTHRU */
				case -1:
					/* if the check returns -1, it's a transfert to non existant account,
					 * so do nothing */
					gsb_data_mix_set_category_number (transaction_number, 0, is_transaction);
					gsb_data_mix_set_sub_category_number (transaction_number, 0, is_transaction);
					break;

					/* all other values are a number of account */
				default :
					/* need to check a lot of things and create the contra-transaction for a transaction,
					 * but nothing to do for a scheduled transaction because no contra-transaction */
					if (is_transaction)
					gsb_form_transaction_validate_transfer (transaction_number,
															new_transaction,
															account_transfer);
					else
					gsb_data_scheduled_set_account_number_transfer (transaction_number, account_transfer);
			}
		}
		else
		{
			/* it's a split of transaction */
			/* if it was a transfer, we delete the contra-transaction */
			contra_transaction_number = gsb_data_transaction_get_contra_transaction_number (transaction_number);
			if (is_transaction && !new_transaction && contra_transaction_number > 0)
			{
				gsb_data_transaction_set_contra_transaction_number (contra_transaction_number, 0);
				gsb_transactions_list_delete_transaction (contra_transaction_number, FALSE);
				gsb_data_transaction_set_contra_transaction_number (transaction_number, 0);
			}

			/* if it's a modification of a transaction and it was not a split,
			 * but it is now, we add a white line as first child */
			if (!new_transaction && !gsb_data_mix_get_split_of_transaction (transaction_number, is_transaction))
			{
				if (!is_transaction)
					gsb_scheduler_list_append_new_scheduled (gsb_data_scheduled_new_white_line (transaction_number),
															 gsb_data_scheduled_get_limit_date (transaction_number));
			}

			gsb_data_mix_set_split_of_transaction (transaction_number, 1, is_transaction);
			gsb_data_mix_set_category_number (transaction_number, 0, is_transaction);
			gsb_data_mix_set_sub_category_number (transaction_number, 0, is_transaction);

			/* normally the following widgets are hidden for a split, but if the user really
			 * want he can annoy us and set an budget for example, and after cry because problems
			 * in reports... so, erase here budget and financial year, if ever they are defined */
			gsb_data_mix_set_budgetary_number (transaction_number, 0, is_transaction);
			gsb_data_mix_set_sub_budgetary_number (transaction_number, 0, is_transaction);
			gsb_data_mix_set_voucher (transaction_number, NULL, is_transaction);
		}
		g_free (string);
    }

    return FALSE;
}

/**
 * initialise le formulaire
 *
 * \param
 *
 * \return FALSE
 **/
static gboolean gsb_form_initialise_transaction_form (void)
{
    gint row, column;
    gint rows_number, columns_number;
    gint account_number;

    devel_debug (NULL);

    /* get the form of the first account */
    account_number = gsb_data_account_first_number ();

    rows_number = gsb_data_form_get_nb_rows ();
    columns_number = gsb_data_form_get_nb_columns ();

    for (row=0 ; row < rows_number ; row++)
	for (column=0 ; column < columns_number ; column++)
	{
	    GtkWidget *widget;
	    gint element;

        element = gsb_data_form_get_value (column, row);
		widget = gsb_form_widget_create (element, account_number);

	    if (!widget)
            continue;

	    gtk_grid_attach (GTK_GRID (form_transaction_part), widget, column, row, 1, 1);
        g_object_set_data (G_OBJECT (widget), "num_row", GINT_TO_POINTER (row));
	}

    return FALSE;
}

/**
 * this function is used to move the tree view when open the form,
 * without that, the selection will be hidden by the form,
 * this moves it to show if necessary the selection
 *
 * \param widget
 * \param allocation
 * \param null
 *
 * \return FALSE
 **/
static gboolean gsb_form_size_allocate (GtkWidget *widget,
										GtkAllocation *allocation,
										gpointer null)
{
    if (grisbi_win_form_expander_is_visible () && !block_size_allocate)
    {
		block_size_allocate = TRUE;
		gsb_transactions_list_set_row_align (-1.0);
    }
    else
		block_size_allocate = FALSE;
    return FALSE;
}

/**
 * get the datas in the form and set them in the transaction/scheduled transaction in param
 *
 * \param transaction_number 	the transaction to modify
 * \param is_transaction 		TRUE if it's for a transaction, FALSE for a scheduled transaction
 *
 * \return
 **/
static void gsb_form_take_datas_from_form (gint transaction_number,
										   gboolean is_transaction)
{
    GSList *tmp_list;
    GDate *date;
    GDate *value_date;

    devel_debug_int (transaction_number);

    /* we set a date variable to avoid to parse 2 times, one time for the date,
     * and perhaps a second time with the financial year
     * (cannot take it from the transaction if the fyear field is before the date field...) */
    /* get the date first, because the financial year will use it and it can set before the date in the form */
    date = gsb_calendar_entry_get_date (gsb_form_widget_get_widget (TRANSACTION_FORM_DATE));

    /* as financial year can be taken with value date, need to get the value date now too,
     * if TRANSACTION_FORM_VALUE_DATE doesn't exist, value_date will be NULL */
    value_date = gsb_calendar_entry_get_date (gsb_form_widget_get_widget (TRANSACTION_FORM_VALUE_DATE));

    tmp_list = gsb_form_widget_get_list ();

    while (tmp_list)
    {
		struct_element *element;

		element = tmp_list->data;

		switch (element->element_number)
		{
			case TRANSACTION_FORM_DATE:
			/* set date before */
			gsb_data_mix_set_date (transaction_number, date, is_transaction);

			break;

			case TRANSACTION_FORM_VALUE_DATE:
			if (gsb_form_widget_check_empty (element->element_widget))
				gsb_data_mix_set_value_date (transaction_number, NULL, is_transaction);
			else
				gsb_data_mix_set_value_date (transaction_number,
											 gsb_calendar_entry_get_date (element->element_widget),
											 is_transaction);
			break;

			case TRANSACTION_FORM_EXERCICE:
			if (is_transaction)
			{
				if (conf.affichage_exercice_automatique)
				{
					if (gsb_form_widget_check_empty (gsb_form_widget_get_widget (TRANSACTION_FORM_VALUE_DATE)))
						gsb_data_transaction_set_financial_year_number (transaction_number,
																		gsb_fyear_get_fyear_from_combobox
																		(element->element_widget, date));
					else
						gsb_data_transaction_set_financial_year_number (transaction_number,
																		gsb_fyear_get_fyear_from_combobox
																		(element->element_widget, value_date));
				}
				else
					gsb_data_transaction_set_financial_year_number (transaction_number,
																	gsb_fyear_get_fyear_from_combobox
																	(element->element_widget, date));
			}
			else
				gsb_data_scheduled_set_financial_year_number (transaction_number,
															  gsb_fyear_get_fyear_from_combobox
															  (element->element_widget, NULL));
			break;

			case TRANSACTION_FORM_PARTY:
			if (gsb_form_widget_check_empty (element->element_widget))
				gsb_data_mix_set_party_number (transaction_number, 0, is_transaction);
			else
			{
				const gchar *tmp_name;

				tmp_name = gtk_combofix_get_text (GTK_COMBOFIX (element->element_widget));
				gsb_data_mix_set_party_number (transaction_number,
											   gsb_data_payee_get_number_by_name (tmp_name, TRUE),
											   is_transaction);
			}
			break;

			case TRANSACTION_FORM_DEBIT:
			if (!gsb_form_widget_check_empty (element->element_widget))
			{
				gsb_form_check_auto_separator (element->element_widget);
				gsb_data_mix_set_amount (transaction_number, gsb_real_opposite
										 (utils_real_get_calculate_entry (element->element_widget)),
										 is_transaction);
			}
			break;

			case TRANSACTION_FORM_CREDIT:
				if (!gsb_form_widget_check_empty (element->element_widget))
				{
					gsb_form_check_auto_separator (element->element_widget);
					gsb_data_mix_set_amount (transaction_number,
											 utils_real_get_calculate_entry (element->element_widget),
											 is_transaction);
				}
				break;

			case TRANSACTION_FORM_BUDGET:
				if (gsb_form_widget_check_empty (element->element_widget))
				{
					gsb_data_mix_set_budgetary_number (transaction_number, 0, is_transaction);
					gsb_data_mix_set_sub_budgetary_number (transaction_number, 0, is_transaction);
				}
				else
					gsb_data_budget_set_budget_from_string (transaction_number,
															gtk_combofix_get_text (GTK_COMBOFIX
																				   (element->element_widget)),
															is_transaction);
				break;

			case TRANSACTION_FORM_NOTES:
				if (gsb_form_widget_check_empty (element->element_widget))
					gsb_data_mix_set_notes (transaction_number, NULL, is_transaction);
				else
				{
					gchar* tmp_str;

					tmp_str = my_strdup (gtk_entry_get_text (GTK_ENTRY (element->element_widget)));
					gsb_data_mix_set_notes (transaction_number, tmp_str , is_transaction);
					g_free (tmp_str);
				}
				break;

			case TRANSACTION_FORM_TYPE:
				/* set the type only if visible */
				if (gtk_widget_get_visible (element->element_widget))
				{
					GtkWidget *widget_tmp;
					gint payment_number;

					payment_number = gsb_payment_method_get_selected_number (element->element_widget);

					gsb_data_mix_set_method_of_payment_number (transaction_number, payment_number, is_transaction);

					/* set the number of cheque only if visible */
					widget_tmp = gsb_form_widget_get_widget (TRANSACTION_FORM_CHEQUE);
					if (gtk_widget_get_visible (widget_tmp) && !gsb_form_widget_check_empty (widget_tmp))
					{
						gsb_data_mix_set_method_of_payment_content (transaction_number,
																	gtk_entry_get_text (GTK_ENTRY (widget_tmp)),
																	is_transaction);

					/* get the last number to increase next time */
					if (is_transaction && gsb_data_payment_get_automatic_numbering (payment_number))
						gsb_data_payment_set_last_number (payment_number, gtk_entry_get_text (GTK_ENTRY (widget_tmp)));
					}
					else
						gsb_data_mix_set_method_of_payment_content (transaction_number, NULL, is_transaction);
				}
				else
				{
					gsb_data_mix_set_method_of_payment_number (transaction_number, 0, is_transaction);
					gsb_data_mix_set_method_of_payment_content (transaction_number, NULL, is_transaction);
				}
				break;

			case TRANSACTION_FORM_CONTRA:
				/* here only for scheduled transaction */
				if (!is_transaction && gtk_widget_get_visible (element->element_widget))
					gsb_data_scheduled_set_contra_method_of_payment_number (transaction_number,
																			gsb_payment_method_get_selected_number
																			(element->element_widget));
				break;

			case TRANSACTION_FORM_DEVISE:
				gsb_data_mix_set_currency_number (transaction_number,
												  gsb_currency_get_currency_from_combobox
												  (element->element_widget),
												  is_transaction);
				if (is_transaction)
					gsb_currency_check_for_change (transaction_number);

				break;

			case TRANSACTION_FORM_BANK:
				if (gsb_form_widget_check_empty (element->element_widget))
					gsb_data_mix_set_bank_references (transaction_number, NULL, is_transaction);
				else
				{
					gchar* tmp_str;

					tmp_str = my_strdup (gtk_entry_get_text (GTK_ENTRY(element->element_widget)));
					gsb_data_mix_set_bank_references (transaction_number, tmp_str, is_transaction);
					g_free (tmp_str);
				}
				break;

			case TRANSACTION_FORM_VOUCHER:
				if (gsb_form_widget_check_empty (element->element_widget))
					gsb_data_mix_set_voucher (transaction_number, NULL, is_transaction);
				else
				{
					gchar* tmp_str;

					tmp_str = my_strdup (gtk_entry_get_text (GTK_ENTRY(element->element_widget)));
					gsb_data_mix_set_voucher (transaction_number, tmp_str, is_transaction);
					g_free (tmp_str);
				}
				break;
		}
		tmp_list = tmp_list->next;
    }
}

/**
 * check if the transaction/scheduled transaction in the form is correct
 *
 * \param  transaction_number	can be -2 for a new daughter scheduled transaction, 0 for a new transaction
 * 								or any number corresponding to an older transaction
 * \param is_transaction 		TRUE if it's for a transaction, FALSE for a scheduled transaction
 *
 * \return TRUE or FALSE
 **/
static gboolean gsb_form_validate_form_transaction (gint transaction_number,
													gboolean is_transaction)
{
    GtkWidget *widget;
    GtkWidget *date_widget;
    gchar* tmp_str;
    gint mother_number;
    gint account_number;
    GsbReal number = null_real;

    devel_debug_int (transaction_number);

    account_number = gsb_form_get_account_number ();

    /* check if it's a daughter split */
    mother_number = gsb_data_mix_get_mother_transaction_number (transaction_number, is_transaction);

    /* begin to work with dates */
    date_widget = gsb_form_widget_get_widget (TRANSACTION_FORM_DATE);

    /* check the date exists */
    if (gsb_form_widget_check_empty (date_widget))
    {
        dialogue_error (_("You must enter a date."));
        return (FALSE);
    }

    /* check the date ok */
    if (!gsb_date_check_entry (date_widget))
    {
        tmp_str = g_strdup_printf (_("Invalid date %s"), gtk_entry_get_text (GTK_ENTRY (date_widget)));
        dialogue_error (tmp_str);
        g_free(tmp_str);
        gtk_editable_select_region (GTK_EDITABLE (date_widget), 0, -1);
        gtk_widget_grab_focus (date_widget);

        return (FALSE);
    }
    else
    {
        if (save_form_date)
            g_date_free (save_form_date);
        save_form_date = gsb_date_copy (gsb_calendar_entry_get_date (date_widget));
    }

    /* work with value date */
    widget = gsb_form_widget_get_widget (TRANSACTION_FORM_VALUE_DATE);
    if (widget
		&& !gsb_form_widget_check_empty (widget)
		&& strlen (gtk_entry_get_text (GTK_ENTRY (widget))) > 0)
    {
        if (!gsb_date_check_entry (widget))
        {
            tmp_str = g_strdup_printf (_("Invalid value date %s"), gtk_entry_get_text (GTK_ENTRY (widget)));
            dialogue_error (tmp_str);
            g_free (tmp_str);
            gtk_editable_select_region (GTK_EDITABLE (widget), 0, -1);
            gtk_widget_grab_focus (widget);

            return (FALSE);
        }
        else if (is_transaction
				 && gsb_data_transaction_get_marked_transaction (transaction_number) == OPERATION_RAPPROCHEE
				 && mother_number == 0)
        {
            GDate *value_date;
            const GDate *init_date;
            const GDate *final_date;
            gint reconcile_number;

            value_date = gsb_calendar_entry_get_date (widget);
            reconcile_number = gsb_data_transaction_get_reconcile_number (transaction_number);
            init_date = gsb_data_reconcile_get_init_date (reconcile_number);
            final_date = gsb_data_reconcile_get_final_date (reconcile_number);
            if (g_date_compare (value_date, init_date) < 0
             ||
             g_date_compare (value_date, final_date) > 0)
            {
                tmp_str = g_strdup_printf (_("Beware the date must be between %s and %s"),
										   gsb_format_gdate (init_date),
										   gsb_format_gdate (final_date));
                dialogue_hint (tmp_str, _("Invalid date"));

                g_free(tmp_str);

                gsb_calendar_entry_set_color (widget, FALSE);
                gtk_editable_select_region (GTK_EDITABLE (widget), 0, -1);
                gtk_widget_grab_focus (widget);

                return FALSE;
            }
        }
    }
    else    /* contrôle de la date de l'opération rapprochée si pas de date de valeur */
    {
        if (is_transaction
			&& gsb_data_transaction_get_marked_transaction (transaction_number) == OPERATION_RAPPROCHEE
			&& mother_number == 0)
        {
            const GDate *final_date;
            gint reconcile_number;

            reconcile_number = gsb_data_transaction_get_reconcile_number (transaction_number);
            final_date = gsb_data_reconcile_get_final_date (reconcile_number);
            if (g_date_compare (save_form_date, final_date) > 0)
            {
                tmp_str = g_strdup_printf (_("The date must be less than or equal to %s"),
										   gsb_format_gdate (final_date));
                dialogue_hint (tmp_str, _("Invalid date"));

                g_free(tmp_str);

                gsb_calendar_entry_set_color (date_widget, FALSE);
                gtk_editable_select_region (GTK_EDITABLE (date_widget), 0, -1);
                gtk_widget_grab_focus (date_widget);

                return FALSE;
            }
        }
    }

    /* check if debit or credit is > 0 */
    widget = gsb_form_widget_get_widget (TRANSACTION_FORM_DEBIT);
    if (widget)
    {
        if (gsb_form_widget_check_empty (widget) == FALSE)
            number = gsb_real_opposite (utils_real_get_calculate_entry (widget));

		if (gsb_form_widget_check_empty (widget) == TRUE || number.mantissa == 0)
        {
			gint payment_number;

			widget = gsb_form_widget_get_widget (TRANSACTION_FORM_CREDIT);
            number = utils_real_get_calculate_entry (widget);

            if ((gsb_form_widget_check_empty (widget) == TRUE || number.mantissa == 0)
				&& mother_number)
            {
                dialogue_error (_("You must enter an amount."));
                return (FALSE);
            }
			/* on remet le bon moyen de payement */
			widget = gsb_form_widget_get_widget (TRANSACTION_FORM_TYPE);
			if (widget && gtk_widget_get_sensitive (widget))
			{
				/* change the signe of the method of payment and the contra */
				if (gsb_payment_method_get_combo_sign (widget) == GSB_PAYMENT_DEBIT)
				{
					gsb_payment_method_create_combo_list (widget,
														  GSB_PAYMENT_CREDIT,
														  account_number,
														  0,
														  FALSE);
					payment_number = gsb_data_account_get_default_credit (account_number);
					gsb_payment_method_set_payment_position (widget, payment_number);

					widget = gsb_form_widget_get_widget (TRANSACTION_FORM_CHEQUE);
					if (gsb_data_payment_get_show_entry (payment_number))
					{
						if (widget && gtk_widget_get_visible (widget))
						{
							if (gsb_form_widget_get_old_credit_payment_content ())
							{
								gtk_entry_set_text (GTK_ENTRY (widget),
													gsb_form_widget_get_old_credit_payment_content ());
								gsb_form_widget_set_empty (widget, FALSE);
							}
							else
							{
								gtk_entry_set_text (GTK_ENTRY (widget), "");
								gsb_form_widget_set_empty (widget, TRUE);
							}
						}
					}
					else
					{
						gsb_form_widget_set_empty (widget, TRUE);
						gtk_widget_hide (widget);
					}

					widget = gsb_form_widget_get_widget (TRANSACTION_FORM_CONTRA);
					if (widget && gtk_widget_get_visible (widget))
						gsb_payment_method_create_combo_list (widget,
															  GSB_PAYMENT_DEBIT,
															  account_number,
															  0,
															  TRUE);
				}
			}
        }
    }

    /* check if balance is < 0 and account_kind == GSB_TYPE_CASH */
    if (gsb_data_account_get_kind (account_number) == GSB_TYPE_CASH)
    {
        GsbReal balance;

        balance = gsb_real_add (number, gsb_data_account_get_current_balance (account_number));
        if (balance.mantissa < 0)
        {
            GsbReal previous_debit;

            previous_debit = utils_real_get_from_string (save_entry);
            balance = gsb_real_add (balance, previous_debit);
            g_free (save_entry);
            save_entry = NULL;

            if (balance.mantissa < 0)
            {
                dialogue_error (_("This account cannot be negative.\n\n"
                                  "Please enter another amount or cancel this transaction."));
                return (FALSE);
            }
        }
    }

    /* now work with the categories */
    widget = gsb_form_widget_get_widget (TRANSACTION_FORM_CATEGORY);

    /* check if it's a daughter split that the category is not a split of transaction */
    if (widget
		&& !gsb_form_widget_check_empty (widget)
		&& mother_number
		&& !strcmp (gtk_combofix_get_text (GTK_COMBOFIX (widget)), _("Split of transaction")))
    {
		dialogue_error (_("You cannot set split of transaction in category for a daughter "
						  "of a split of transaction."));
		return (FALSE);
    }

    /* check it's not a transfer on itself and the contra-account exists
     * !!!widget is already set to the category, don't change it before */
    if (widget && !gsb_form_widget_check_empty (widget))
    {
		gint account_transfer;

		account_transfer = gsb_form_check_for_transfer (gtk_combofix_get_text (GTK_COMBOFIX (widget)));

		switch (account_transfer)
		{
			/* if the check returns -3, it's not a transfer, so it's a normal category */
			case -3:
				break;

			/* if the check returns -2, it's a deleted account (refuse for scheduled transaction)*/
			case -2:
				if (is_transaction)
				{
					gsb_data_transaction_set_category_number (transaction_number, 0);
					gsb_data_transaction_set_sub_category_number (transaction_number, 0);
					gsb_data_transaction_set_contra_transaction_number (transaction_number, -1);
				}
				else
				{
					dialogue_error (_("Cannot associate a transfer to a deleted account in a "
									  "scheduled transaction."));
					return FALSE;
				}
				break;

			/* if the check returns -1, it's a non existant account */
			case -1:
				dialogue_error (_("There is no associated account for this transfer or associated "
								  "account is invalid."));

				return (FALSE);
				break;

			/* all other values are a number of account */
			default :
				if (account_transfer == account_number)
				{
					dialogue_error (_("Can't issue a transfer its own account."));
					return (FALSE);
				}
				if (gsb_data_account_get_closed_account (account_transfer))
				{
					dialogue_error (_("Can't issue a transfer on a closed account."));
					return (FALSE);
				}
		}
    }

    /* for the automatic method of payment entry (especially cheques)
     * check if not used before (only for new transaction) */
    widget = gsb_form_widget_get_widget (TRANSACTION_FORM_CHEQUE);
    if (widget && gtk_widget_get_visible (widget))
    {
		GtkWidget *combo_box;
		gint payment;

		combo_box = gsb_form_widget_get_widget (TRANSACTION_FORM_TYPE);
		payment = gsb_payment_method_get_selected_number (combo_box);
		if (gsb_data_payment_get_automatic_numbering (payment))
		{
			/* check if there is something in */
			if (gsb_form_widget_check_empty (widget))
			{

				if (!question_yes_no (_("Selected method of payment has an automatic incremental number\n"
										"but doesn't contain any number.\n"
										"Continue anyway?"),
									  NULL,
									  GTK_RESPONSE_CANCEL))
				{
					return (FALSE);
				}
			}
			else if (mother_number == 0)
			{
				/* check that the number is not used */
				gint tmp_transaction_number;

				tmp_transaction_number = gsb_data_transaction_check_content_payment (payment,
																					 gtk_entry_get_text
																					 (GTK_ENTRY (widget)));

				if (tmp_transaction_number
					&& tmp_transaction_number != transaction_number
					&& !question_yes_no (_("Warning: this cheque number is already used.\nContinue anyway?"),
										 NULL,
										 GTK_RESPONSE_CANCEL))
				{
					return FALSE;
				}
			}
		}
    }

    /* check if the payee is a report, it must be a new transaction */
    widget = gsb_form_widget_get_widget (TRANSACTION_FORM_PARTY);
    if (widget)
    {
		const gchar *payee_name = NULL;

		payee_name = gtk_combofix_get_text (GTK_COMBOFIX (widget));
		if (payee_name && !strncmp (payee_name, _("Report"), strlen (_("Report"))))
		{
			gchar **tab_char;

			/* check if it's a new transaction */
			if (transaction_number > 0)
			{
				dialogue_error (_("A transaction with a multiple payee must be a new one."));
				return (FALSE);
			}
			if (mother_number > 0)
			{
				dialogue_error (_("A transaction with a multiple payee cannot be a split child."));
				return (FALSE);
			}

			/* check if the report exists */
			tab_char = g_strsplit (payee_name, " : ", 2);
			if (!tab_char[1])
			{
				dialogue_error  (_("The word \"Report\" is reserved. Please use another one."));
				g_strfreev (tab_char);
				return FALSE;
			}

			if (!gsb_data_report_get_report_by_name (tab_char[1]))
			{
				dialogue_error (_("Invalid multiple payee."));
				g_strfreev (tab_char);
				return (FALSE);
			}
			g_strfreev (tab_char);
		}
    }

	return (TRUE);
}

/**
 * set fixed date if available for the scheduled transaction
 *
 * \param transaction_number
 * \param date
 *
 * \return
 **/
static void gsb_form_set_fixed_date_if_necessary (gint scheduled_number,
												  GDate *date)
 {
	GDateDay day;

	day = g_date_get_day (date);
	if (day == 31)
	{
		gsb_data_scheduled_set_fixed_date (scheduled_number, 31);
		return;
	}

	if (g_date_is_last_of_month (date))
	{
		gint fixed_date;

		fixed_date = gsb_form_scheduler_get_last_day_of_month_dialog (scheduled_number, date);
		gsb_data_scheduled_set_fixed_date (scheduled_number, fixed_date);

		return;
	}

	switch (day)
	{
		case 28:
			gsb_data_scheduled_set_fixed_date (scheduled_number, 28);
			break;
		case 29:
			gsb_data_scheduled_set_fixed_date (scheduled_number, 29);
			break;
		case 30:
			gsb_data_scheduled_set_fixed_date (scheduled_number, 30);
			break;
	}
 }

/******************************************************************************/
/* Public Methods                                                             */
/******************************************************************************/
/**
 * the value transaction_form is static,
 * that function return it
 *
 * \param
 *
 * \return a pointer to the transaction form
 **/
GtkWidget *gsb_form_get_form_widget (void)
{
    return transaction_form;
}

/**
 * return the address of the scheduler part widget
 *
 * \param
 *
 * \return a pointer to a widget (the scheduler part of the form)
 **/
GtkWidget *gsb_form_get_scheduler_part (void)
{
    return form_scheduled_part;
}

/**
 *  Do the grunt job of creating widgets in for the Grisbi form.
 *  Fills them in form_expander, a GtkExpander normally created by
 *  gsb_form_new().  It is reentrant, that is calling it over and over
 *  will reinitialize form content and delete previous content.
 *
 * \param
 *
 * \return
 **/
void gsb_form_create_widgets (void)
{
    GtkWidget *child;
    GtkWidget *event_box;
	GtkWidget *form_expander;
    GtkWidget *hbox_buttons;
    GtkWidget *hbox_buttons_inner;
    GtkWidget *separator;

    devel_debug (NULL);

	form_expander = grisbi_win_get_form_expander ();
	child = gtk_bin_get_child (GTK_BIN (form_expander));
    if (child && GTK_IS_WIDGET(child))
    {
        gsb_form_widget_free_list ();
	    gtk_container_remove (GTK_CONTAINER (form_expander), child);
    }

    /* Create form inside the expander :
     * the form is 3 parts :
     * top : the values specific for the scheduled transactions
     * middle : the values for transactions and scheduled transactions
     * bottom : the buttons valid/cancel */
    transaction_form = gtk_box_new (GTK_ORIENTATION_VERTICAL, MARGIN_BOX);
    gtk_container_add (GTK_CONTAINER (form_expander), transaction_form);

    /* play with that widget to tell to the tree view to scroll to keep the selection visible */
    g_signal_connect_after (G_OBJECT (transaction_form),
							"size-allocate",
							G_CALLBACK (gsb_form_size_allocate),
							NULL);

    /* the scheduled part is a table of SCHEDULED_WIDTH col x SCHEDULED_HEIGHT rows */

    form_scheduled_part = gtk_grid_new ();
    gtk_grid_set_column_spacing (GTK_GRID (form_scheduled_part), 6);
    gtk_box_pack_start (GTK_BOX (transaction_form), form_scheduled_part, FALSE, FALSE, 0);

    gsb_form_scheduler_create (form_scheduled_part);

    /* add a separator between the scheduled and transaction part */
    separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
    gtk_box_pack_start (GTK_BOX (transaction_form), separator, FALSE, FALSE, 0);

    /* the transactions part is a variable table,
     * so set to 1x1 for now, it will change when we show it */
    /* didn't find another way to get the button-press-event on the form_transaction_part,
     * so use an event box */
    event_box = gtk_event_box_new ();
    gtk_event_box_set_above_child (GTK_EVENT_BOX (event_box), FALSE);
    gtk_box_pack_start (GTK_BOX (transaction_form), event_box, FALSE, FALSE, 0);
    g_signal_connect (G_OBJECT (event_box),
					  "button-press-event",
					  G_CALLBACK (gsb_form_button_press),
					  NULL);

    form_transaction_part = gtk_grid_new ();
    gtk_grid_set_column_spacing (GTK_GRID (form_transaction_part), 6);

    gtk_container_add (GTK_CONTAINER (event_box), form_transaction_part);

    gsb_form_initialise_transaction_form ();

    /* the buttons part is a hbox, with the recuperate child split
     * on the left and valid/cancel on the right */
    form_button_part = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
    gtk_box_pack_start (GTK_BOX (transaction_form), form_button_part, FALSE, FALSE, 0);

    /* add a separator between the transaction and button part */
    separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
    gtk_box_pack_start (GTK_BOX (form_button_part), separator, FALSE, FALSE, 0);

    /* Hbox containing buttons */
    hbox_buttons = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
    gtk_box_pack_start (GTK_BOX (form_button_part), hbox_buttons, FALSE, FALSE, 0);

    hbox_buttons_inner = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
    gtk_box_set_homogeneous (GTK_BOX (hbox_buttons_inner), TRUE);
    gtk_box_pack_end (GTK_BOX (hbox_buttons), hbox_buttons_inner, FALSE, FALSE, 0);

    /* create the check button to recover the children of splits */
    form_button_recover_split = gtk_check_button_new_with_label (_("Recover the children"));
    gtk_box_pack_start (GTK_BOX (hbox_buttons_inner), form_button_recover_split, FALSE, FALSE, 0);

    /* create the valid/cancel buttons */
    form_button_valid = utils_buttons_button_new_from_stock ("gtk-ok", _("_Validate"));
    gtk_button_set_relief (GTK_BUTTON (form_button_valid), GTK_RELIEF_NONE);
    g_signal_connect (G_OBJECT (form_button_valid), "clicked",
					  G_CALLBACK (gsb_form_finish_edition), NULL);
    gtk_box_pack_end (GTK_BOX (hbox_buttons_inner), form_button_valid, FALSE, FALSE, 0);

    form_button_cancel = utils_buttons_button_new_from_stock ("gtk-cancel", _("_Cancel"));
    gtk_button_set_relief (GTK_BUTTON (form_button_cancel), GTK_RELIEF_NONE);
    g_signal_connect (G_OBJECT (form_button_cancel), "clicked",
					  G_CALLBACK (gsb_form_escape_form), NULL);
    gtk_box_pack_end (GTK_BOX (hbox_buttons_inner), form_button_cancel, FALSE, FALSE, 0);

    gtk_widget_show_all (transaction_form);
    gtk_widget_hide (form_scheduled_part);

    gtk_widget_set_sensitive (GTK_WIDGET (form_button_valid), FALSE);
    gtk_widget_set_sensitive (GTK_WIDGET (form_button_cancel), FALSE);

    gsb_form_show (FALSE);
}

/**
 * fill the form according to the transaction or scheduled transaction given in param
 *
 * \param transaction_number 	the number of the transaction or scheduled transaction
 * \param is_transaction 		TRUE if transaction, FALSE if scheduled transaction
 * 								that param could be taken from the navigation bar but
 *								i prefer set in param for now
 * \param grab_focus 			if TRUE, after filling the form, the date will grab the focus ;
 *								if FALSE, nothing change with focus
 *
 * \return FALSE
 **/
gboolean gsb_form_fill_by_transaction (gint transaction_number,
									   gint is_transaction,
									   gboolean grab_focus)
{
    gint mother_number;
    gint account_number;
    gint focus_to;
    gint is_split;
    GSList *tmp_list;

    devel_debug_int (transaction_number);

    if (!transaction_number)
        return FALSE;

    /* get the account */
    account_number = gsb_data_mix_get_account_number (transaction_number, is_transaction);
    mother_number = gsb_data_mix_get_mother_transaction_number (transaction_number, is_transaction);
    is_split = gsb_data_mix_get_split_of_transaction (transaction_number, is_transaction);

    /* if here account number = -1, it's because it's a white line or there were a problem ;
     * in all case, get the current account number */
    if (account_number == -1)
        account_number = gsb_form_get_account_number ();

    /* show and prepare the form */
    gsb_form_show (TRUE);

    if (!is_transaction)
    {
	/* we need to set up the part of scheduler form here because changing the account
	 * button will change the form */
    gsb_form_scheduler_set (transaction_number);
    }

    /* if the transaction is the white line, we set the date and necessary stuff and go away
     * for that use the button_press function, so clicking on a form's field or do enter/double click
     * on white line will do the same */
    if (transaction_number == -1)
    {
        GtkWidget *date_entry;

        /* if we can't touch the focus, it's because we just select a transaction, and
         * for now, select the white line, so keep the form blank */
        if (!grab_focus)
            return FALSE;

        date_entry = gsb_form_widget_get_widget (TRANSACTION_FORM_DATE);

        if (gsb_form_widget_check_empty (date_entry))
            /* for now,  transaction_number_in_form is NULL so can call the next function
             * which will prepare the form for a new transaction */
            gsb_form_button_press_event (date_entry, NULL, GINT_TO_POINTER (TRANSACTION_FORM_DATE));

        gtk_editable_select_region (GTK_EDITABLE (date_entry), 0, -1);
        gtk_widget_grab_focus (GTK_WIDGET (date_entry));

        return FALSE;
    }

    /* set the number of the transaction in the form, can be -2, -3...
     * for white line split
     * that must be defined before gsb_form_change_sensitive_buttons
     * because without that number, others functions cannot get the account
     * number for an execution of scheduled in the home page
     * but after gsb_form_show because it will set that value to 0...*/
    g_object_set_data (G_OBJECT (transaction_form),
					   "transaction_number_in_form",
					   GINT_TO_POINTER (transaction_number));

    gsb_form_change_sensitive_buttons (TRUE);

    /* by default, a function before changed all the form from non sensitive to sensitive,
     * but for split or split child, some widgets need to be unsensitive/hidden */
    gsb_form_set_sensitive (is_split, mother_number);

    /* fill what is necessary in the form */
    tmp_list = gsb_form_widget_get_list ();
    while (tmp_list)
    {
        struct_element *element;

        element = tmp_list->data;
        if (mother_number && transaction_number < 0)
        {
            /* we are on a split white line, we fill only few fields
             * with the element_number of the mother */
            switch (element->element_number)
            {
				case TRANSACTION_FORM_DATE:
				case TRANSACTION_FORM_VALUE_DATE:
				case TRANSACTION_FORM_PARTY:
				case TRANSACTION_FORM_TYPE:
				case TRANSACTION_FORM_CHEQUE:
				case TRANSACTION_FORM_DEVISE:
				case TRANSACTION_FORM_BANK:
					gsb_form_fill_element (element->element_number,
										   account_number,
										   mother_number,
										   is_transaction);
					break;
            }
        }
        else
            /* normal transaction */
            gsb_form_fill_element (element->element_number, account_number, transaction_number, is_transaction);

        tmp_list = tmp_list->next;
    }

    /* for a transaction, need to check if marked R, and do some stuff with that */
    if (is_transaction)
    {
        gint contra_transaction_number;

        /* get the contra transaction */
        contra_transaction_number = gsb_data_transaction_get_contra_transaction_number (transaction_number);

        /* if the transaction is marked R and splitted, cannot change the amounts */
        if (gsb_data_transaction_get_marked_transaction (transaction_number) == OPERATION_RAPPROCHEE
         && mother_number == 0)
        {
            gtk_widget_set_sensitive (gsb_form_widget_get_widget (TRANSACTION_FORM_DEBIT), FALSE);
            gtk_widget_set_sensitive (gsb_form_widget_get_widget (TRANSACTION_FORM_CREDIT), FALSE);
        }

        /* if the contra transaction is marked R, cannot change category and amounts */
        if (contra_transaction_number > 0
         && gsb_data_transaction_get_marked_transaction (contra_transaction_number) == OPERATION_RAPPROCHEE)
        {
            gtk_widget_set_sensitive (gsb_form_widget_get_widget (TRANSACTION_FORM_DEBIT), FALSE);
            gtk_widget_set_sensitive (gsb_form_widget_get_widget (TRANSACTION_FORM_CREDIT), FALSE);

            /* if it's a transfer, we cannot change too the category */
            gtk_widget_set_sensitive (gsb_form_widget_get_widget (TRANSACTION_FORM_CATEGORY), FALSE);
        }
    }

    /* we take focus only if asked */
    if (grab_focus)
    {
        GtkWidget *widget;

        /* the form is full, if it's not a split, we give the focus to the date
         * else, we give the focus to the first free form element */
        if (mother_number)
            focus_to = gsb_form_widget_next_element (account_number, TRANSACTION_FORM_DATE, GTK_DIR_RIGHT);
        else
            focus_to = TRANSACTION_FORM_DATE;

        widget = gsb_form_widget_get_widget (focus_to);
        gtk_editable_select_region (GTK_EDITABLE (widget), 0, -1);
        if (!is_transaction && gtk_widget_is_focus (widget))
            gsb_form_widget_entry_get_focus (widget, NULL, GINT_TO_POINTER (focus_to));
        else
            gtk_widget_grab_focus (widget);
    }

    return FALSE;
}

/**
 * set the form sensitive, depend if it's a split child or normal transaction
 * in fact, the form is always sensitive, so just unsensitive what is needed for split child
 * or split mother
 *
 * \param split TRUE if is split of transaction (mother)
 * \param split_child TRUE if it's a child
 *
 * \return FALSE
 **/
gboolean gsb_form_set_sensitive (gboolean split,
								 gboolean split_child)
{
    GSList *tmp_list;

    /* for now, come here only for split or split child */
    if (!split && !split_child)
		return FALSE;

    tmp_list = gsb_form_widget_get_list ();
    while (tmp_list)
    {
        struct_element *element;

        element = tmp_list->data;

        /* for a split, hide the exercice and the budget */
        if (split)
        {
            switch (element->element_number)
            {
				case TRANSACTION_FORM_EXERCICE:
				case TRANSACTION_FORM_BUDGET:
					gtk_widget_set_sensitive (element->element_widget, FALSE);
					break;
            }
        }

        /* for a child of split, cannot change the date, payee... */
        if (split_child)
        {
            /* mixed widgets for transactions and scheduled transactions */
            switch (element->element_number)
            {
				case TRANSACTION_FORM_DATE:
				case TRANSACTION_FORM_VALUE_DATE:
				case TRANSACTION_FORM_PARTY:
				case TRANSACTION_FORM_DEVISE:
				case TRANSACTION_FORM_CHANGE:
				case TRANSACTION_FORM_TYPE:
				case TRANSACTION_FORM_CHEQUE:
				case TRANSACTION_FORM_BANK:
					gtk_widget_set_sensitive (element->element_widget, FALSE);
					break;
            }
            /* specific widgets for scheduled transactions */
            gtk_widget_hide (gsb_form_get_scheduler_part ());
        }
        tmp_list = tmp_list->next;
    }

    return FALSE;
}

/**
 * fill the element_number in the form according to the given transaction or scheduled transaction
 * this is the best way to fill the form : send here the element number, the transaction or scheduled number
 * and this function will fill the good element with the good value in the form
 *
 * \param element_number 		the element we want to fill
 * \param account_number 		used for currency and method of payment
 * \param transaction_number 	the number of transaction or scheduled transaction to fill the element
 * \param is_transaction 		TRUE if transaction, FALSE if scheduled transaction
 *
 * \return
 **/
void gsb_form_fill_element (gint element_number,
							gint account_number,
							gint transaction_number,
							gboolean is_transaction)
{
    GtkWidget *widget;
    GtkWidget *tmp_widget;
    gchar *char_tmp;
    gint number;
	gint payment_number;

    widget = gsb_form_widget_get_widget (element_number);
    if (!widget)
	return;

    switch (element_number)
    {
		case TRANSACTION_FORM_OP_NB:
			char_tmp = utils_str_itoa (transaction_number);
			gtk_label_set_text (GTK_LABEL (widget), char_tmp);
			g_free (char_tmp);
			break;

		case TRANSACTION_FORM_DATE:
			gsb_form_entry_get_focus (widget);
			gsb_calendar_entry_set_date (widget, gsb_data_mix_get_date (transaction_number, is_transaction));
			break;

		case TRANSACTION_FORM_VALUE_DATE:
			/* value_date only for transactions */
			if (is_transaction
			 && gsb_data_transaction_get_value_date (transaction_number))
			{
				gsb_form_entry_get_focus (widget);
				gsb_calendar_entry_set_date (widget, gsb_data_transaction_get_value_date (transaction_number));
			}
			break;

		case TRANSACTION_FORM_EXERCICE:
			gsb_fyear_set_combobox_history (widget,
											gsb_data_mix_get_financial_year_number
											(transaction_number, is_transaction));
			break;

		case TRANSACTION_FORM_PARTY:
			number = gsb_data_mix_get_party_number (transaction_number, is_transaction);
			if (number)
			{
				const gchar *tmp_name;

				tmp_name = gsb_data_payee_get_name (number, TRUE);
				gsb_form_entry_get_focus (widget);
				gtk_combofix_set_text (GTK_COMBOFIX (widget), tmp_name);
			}
			break;

		case TRANSACTION_FORM_DEBIT:
			if (gsb_data_mix_get_amount (transaction_number, is_transaction).mantissa < 0)
			{
				char_tmp = utils_real_get_string_with_currency (gsb_real_abs (gsb_data_mix_get_amount
																			  (transaction_number, is_transaction)),
																gsb_data_mix_get_currency_number
																(transaction_number, is_transaction),
																FALSE);

				gsb_form_entry_get_focus (widget);
				gtk_entry_set_text (GTK_ENTRY (widget), char_tmp);
				if (gsb_data_account_get_kind (account_number) == GSB_TYPE_CASH)
				{
					save_entry = g_strdup (char_tmp);
				}
				g_free (char_tmp);
			}
			break;

		case TRANSACTION_FORM_CREDIT:
			if (gsb_data_mix_get_amount (transaction_number, is_transaction).mantissa >= 0)
			{
				char_tmp = utils_real_get_string_with_currency (gsb_data_mix_get_amount
																(transaction_number, is_transaction),
																gsb_data_mix_get_currency_number
																(transaction_number, is_transaction),
																FALSE);

				gsb_form_entry_get_focus (widget);
				gtk_entry_set_text (GTK_ENTRY (widget), char_tmp);
				g_free (char_tmp);
			}
			break;

		case TRANSACTION_FORM_CATEGORY:
			if (gsb_data_mix_get_split_of_transaction (transaction_number, is_transaction))
			{
				/* it's a split of transaction */
				gsb_form_entry_get_focus (widget);
				gtk_combofix_set_text (GTK_COMBOFIX (widget), _("Split of transaction"));
			}
			else
			{
				gint contra_transaction_number;

				contra_transaction_number = gsb_data_mix_get_transaction_number_transfer (transaction_number, is_transaction);
				switch (contra_transaction_number)
				{
					case -1:
						/* transfer to deleted account, not possible with scheduled */
						gsb_form_entry_get_focus (widget);
						gtk_combofix_set_text (GTK_COMBOFIX (widget), _("Transfer : Deleted account"));
						break;
					case 0:
						/* normal category */
						char_tmp = gsb_data_category_get_name (gsb_data_mix_get_category_number
															   (transaction_number, is_transaction),
															   gsb_data_mix_get_sub_category_number
															   (transaction_number,is_transaction),
															   NULL);
						if (char_tmp)
						{
							gsb_form_entry_get_focus (widget);
							gtk_combofix_set_text (GTK_COMBOFIX (widget), char_tmp);
							g_free (char_tmp);
						}
						break;
					default:
						/* transfer */
						gsb_form_entry_get_focus (widget);
						char_tmp = g_strconcat (_("Transfer : "),
												gsb_data_account_get_name (gsb_data_mix_get_account_number_transfer
																		   (transaction_number,
																			is_transaction)),
												NULL);
						gtk_combofix_set_text (GTK_COMBOFIX (widget), char_tmp);
						g_free (char_tmp);
				}
			}
			break;

		case TRANSACTION_FORM_BUDGET:
			char_tmp = gsb_data_budget_get_name (gsb_data_mix_get_budgetary_number
												 (transaction_number, is_transaction),
												 gsb_data_mix_get_sub_budgetary_number
												 (transaction_number, is_transaction),
												 NULL);
			if (char_tmp)
			{
				gsb_form_entry_get_focus (widget);
				gtk_combofix_set_text (GTK_COMBOFIX (widget), char_tmp);
				g_free (char_tmp);
			}
			break;

		case TRANSACTION_FORM_NOTES:
			if (gsb_data_mix_get_notes (transaction_number, is_transaction))
			{
				gsb_form_entry_get_focus (widget);
				gtk_entry_set_text (GTK_ENTRY (widget), gsb_data_mix_get_notes (transaction_number, is_transaction));
			}
			break;

		case TRANSACTION_FORM_TYPE:
			/* one small thing here : normally if the transaction is a debit, we set the
			 * negative payment method. but the problem : if we are on a child of split,
			 * that child contains the payment method of the mother, and if the child has not
			 * the same sign of the mother (rare but possible), grisbi will not find the good payment
			 * method for that child because it's not on the good sign... so for a child of split,
			 * we set the payment box of the mother */
			number = gsb_data_mix_get_mother_transaction_number (transaction_number, is_transaction);
			if (!number)
				/* it's not a child split, so set number to transaction_number */
				number = transaction_number;

			/* ok, now number contains either the transaction_number, either the mother transaction number,
			 * we can check the sign with it */
			payment_number = gsb_data_mix_get_method_of_payment_number (transaction_number, is_transaction);
			if (gsb_data_mix_get_amount (number, is_transaction).mantissa < 0)
			{
				gsb_payment_method_create_combo_list (widget, GSB_PAYMENT_DEBIT, account_number, 0, FALSE);
				if (payment_number)
				{
					gsb_payment_method_set_combobox_history (widget, payment_number, FALSE);
				}
			}
			else
			{
				gsb_payment_method_create_combo_list (widget, GSB_PAYMENT_CREDIT, account_number, 0, FALSE);
				if (payment_number)
				{
					gsb_payment_method_set_combobox_history (widget, payment_number, FALSE);
				}
			}
			/* don't show the cheque entry for a child of split */
			tmp_widget = gsb_form_widget_get_widget (TRANSACTION_FORM_CHEQUE);
			if (gtk_widget_get_visible (widget))
			{
				/* we show the cheque entry only for transactions */
				if (payment_number && is_transaction)
				{
					gboolean check_entry = FALSE;

					if (transaction_number == - 1)
						check_entry = TRUE;

					if (gsb_payment_method_set_combobox_history (widget, payment_number, check_entry)
						&& gsb_data_payment_get_show_entry (payment_number)
						&& !gsb_data_mix_get_mother_transaction_number (transaction_number, is_transaction))
					{
						const gchar *tmp_content;

						tmp_content = gsb_data_transaction_get_method_of_payment_content (transaction_number);
						gsb_form_entry_get_focus (tmp_widget);
						if (tmp_content)
							gtk_entry_set_text (GTK_ENTRY (tmp_widget), tmp_content);

						gtk_widget_show (tmp_widget);
					}
				}
				else
					gtk_widget_hide (tmp_widget);
			}
			else
				gtk_widget_hide (tmp_widget);
			break;

		case TRANSACTION_FORM_DEVISE:
			number = gsb_data_mix_get_currency_number (transaction_number, is_transaction);

			gsb_currency_set_combobox_history (widget, number);
			if (transaction_number < 1)
				gsb_currency_init_exchanges ();

			if (is_transaction)
			{
				if (gsb_data_transaction_get_marked_transaction (transaction_number) == OPERATION_RAPPROCHEE)
				{
					gtk_widget_set_sensitive (widget, FALSE);
					gtk_widget_hide (gsb_form_widget_get_widget (TRANSACTION_FORM_CHANGE));
				}
			}
			break;

		case TRANSACTION_FORM_BANK:
			if (gsb_data_mix_get_bank_references (transaction_number, is_transaction))
			{
				gsb_form_entry_get_focus (widget);
				gtk_entry_set_text (GTK_ENTRY (widget),
									gsb_data_mix_get_bank_references (transaction_number, is_transaction));
			}
			break;

		case TRANSACTION_FORM_VOUCHER:
			if (gsb_data_mix_get_voucher (transaction_number, is_transaction))
			{
				gsb_form_entry_get_focus (widget);
				gtk_entry_set_text (GTK_ENTRY (widget),
									gsb_data_mix_get_voucher (transaction_number, is_transaction));
			}
			break;

		case TRANSACTION_FORM_CONTRA:
			if (gsb_data_mix_get_transaction_number_transfer (transaction_number, is_transaction) > 0)
			{
				number = gsb_data_mix_get_account_number_transfer (transaction_number, is_transaction);

				if (gsb_data_mix_get_amount (transaction_number, is_transaction).mantissa < 0)
					gsb_payment_method_create_combo_list (widget, GSB_PAYMENT_CREDIT, number, 0, TRUE);
				else
					gsb_payment_method_create_combo_list (widget, GSB_PAYMENT_DEBIT, number, 0, TRUE);

				if (gtk_widget_get_visible (widget))
				{
					gint contra_number;
					gint method;

					if (is_transaction)
					{
						contra_number = gsb_data_transaction_get_contra_transaction_number (transaction_number);
						method = gsb_data_transaction_get_method_of_payment_number (contra_number);
					}
					else
						method = gsb_data_scheduled_get_contra_method_of_payment_number (transaction_number);

					gsb_payment_method_set_combobox_history (widget, method, FALSE);
				}
			}
			else
				gtk_widget_hide (widget);
		break;

		case TRANSACTION_FORM_MODE:
			if (gsb_data_mix_get_automatic_transaction (transaction_number, is_transaction))
				gtk_label_set_text (GTK_LABEL (widget), _("Auto"));
			else
				gtk_label_set_text (GTK_LABEL (widget), _("Manual"));
			break;
    }
}

/**
 * Positionne le formulaire quand l'expandeur montre le widget enfant
 *
 * \param expander.
 *
 * \return
 **/
void gsb_form_expander_is_extanded (GtkWidget *expander)
{
	GtkWidget *date_entry;

	gsb_form_show (TRUE);
	conf.formulaire_toujours_affiche = TRUE;
	gsb_form_widget_set_focus (TRANSACTION_FORM_DATE);
	date_entry = gsb_form_widget_get_widget (TRANSACTION_FORM_DATE);
	gsb_form_widget_set_empty (date_entry, TRUE);
	gsb_form_button_press_event (date_entry, NULL, GINT_TO_POINTER (TRANSACTION_FORM_DATE));
	gtk_widget_grab_focus (GTK_WIDGET (date_entry));
	gtk_editable_set_position (GTK_EDITABLE (date_entry), -1);
}

/**
 * show the form, detect automaticly what we need to show, even for transactions,
 * scheduled_transactions and the buttons valid/cancel
 *
 * \param show 		TRUE if we want to automatickly turn on the expander,
 *					FALSE if we don't want, so just let it
 *
 * \return FALSE
 **/
gboolean gsb_form_show (gboolean show)
{
	GtkWidget *form_expander;
    gint origin;

    devel_debug_int (show);
	form_expander = grisbi_win_get_form_expander ();

    origin = gsb_form_get_origin ();

    /* show or hide the scheduler part */
    switch (origin)
    {
		case ORIGIN_VALUE_OTHER:
		case ORIGIN_VALUE_HOME:
			return FALSE;
			break;

		case ORIGIN_VALUE_SCHEDULED:
			gsb_form_scheduler_set_frequency (2);
			gtk_widget_show (form_scheduled_part);
			break;

		default:
			gtk_widget_hide (form_scheduled_part);
    }

    gsb_form_clean (origin);
    gtk_widget_show (form_transaction_part);
	grisbi_win_form_expander_show_frame ();

	if (!grisbi_win_form_expander_is_visible () && show)
		gtk_expander_set_expanded (GTK_EXPANDER (form_expander), TRUE);

    return FALSE;
}

/**
 * return the current account number according,
 * if we are on transactions list, return the current account,
 * if we are on scheduling list, return the selected account on button
 * if we are on welcome page, it's only if it's an execution of scheduled
 *
 * \param origin
 *
 * \return the account number or -2 if problem (-1 is reserved to get account from the button)
 **/
gint gsb_form_get_account_number (void)
{
    gint account_number;
    gint origin;

    origin = gsb_form_get_origin ();

    switch (origin)
    {
		case ORIGIN_VALUE_OTHER:
			return -2;
			break;

		case ORIGIN_VALUE_HOME:
			/* we are on the home page, we need to check if the form is showed,
			 * if yes, we get the account number of the scheduled showed in that form */
			if (transaction_form && gtk_widget_get_visible (transaction_form))
			{
				gint scheduled_number;

				scheduled_number = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (transaction_form),
																	   "transaction_number_in_form"));
				if (!scheduled_number)
					return -2;
				account_number = gsb_data_scheduled_get_account_number (scheduled_number);
			}
			else
				return -2;
			break;

		case ORIGIN_VALUE_SCHEDULED:
			account_number = gsb_form_scheduler_get_account ();
			break;

		default:
			account_number = origin;
    }
    return account_number;
}

/**
 * get the page where we are and return a number according that page :
 * ORIGIN_VALUE_OTHER : we are neither on scheduled transactions, neither transactions list, neither welcome page
 * ORIGIN_VALUE_HOME : we are on the welcome page
 * ORIGIN_VALUE_SCHEDULED : we are on the scheduled transactions
 *  0 to x : the account number where we are
 *  that function is called at each beginning to know where we are and what to do...
 *
 *  \param
 *
 *  \return the number where we are
 **/
gint gsb_form_get_origin (void)
{
    gint account_number;

    switch (gsb_gui_navigation_get_current_page())
    {
		case GSB_ACCOUNT_PAGE:
			account_number = gsb_gui_navigation_get_current_account ();

			if (account_number != -1)
				return account_number;
			break;

		case GSB_SCHEDULER_PAGE:
				return ORIGIN_VALUE_SCHEDULED;
			break;

		case GSB_HOME_PAGE:
				return ORIGIN_VALUE_HOME;
			break;
    }
    return ORIGIN_VALUE_OTHER;
}

/**
 * clean the form according to the account_number
 * and set the default values
 *
 * \param account number
 *
 * \return FALSE
 **/
gboolean gsb_form_clean (gint account_number)
{
    GSList *tmp_list;

    devel_debug_int (account_number);

    /* clean the transactions widget */
    tmp_list = gsb_form_widget_get_list ();
    while (tmp_list)
    {
		struct_element *element;

		element = tmp_list->data;

		/* better to protect here if widget != NULL (bad experience...) */
		if (element->element_widget)
		{
			/* some widgets can be set unsensitive because of the children of splits,
			 * so resensitive all to be sure */
			gtk_widget_set_sensitive (element->element_widget, TRUE);

			switch (element->element_number)
			{
				case TRANSACTION_FORM_DATE:
					gsb_form_widget_set_empty (element->element_widget, TRUE);
					gtk_entry_set_text (GTK_ENTRY (element->element_widget), _("Date"));
					break;

				case TRANSACTION_FORM_VALUE_DATE:
					gsb_form_widget_set_empty (element->element_widget, TRUE);
					gtk_entry_set_text (GTK_ENTRY (element->element_widget), _("Value date"));
					break;

				case TRANSACTION_FORM_EXERCICE:
					/* editing a transaction can show some fyear which shouldn't be showed,
					 * so hide them here */
					gsb_fyear_update_fyear_list ();

					/* set the combo_box on 'Automatic' */
					gsb_fyear_set_combobox_history (element->element_widget, 0);
					gtk_widget_set_sensitive (GTK_WIDGET (element->element_widget), FALSE);
					break;

				case TRANSACTION_FORM_PARTY:
						gsb_form_widget_set_empty (element->element_widget, TRUE);
						gtk_combofix_set_text (GTK_COMBOFIX (element->element_widget), _("Payee"));
					break;

				case TRANSACTION_FORM_DEBIT:
					gsb_form_widget_set_empty (element->element_widget, TRUE);
					gtk_entry_set_text (GTK_ENTRY (element->element_widget), _("Debit"));
					break;

				case TRANSACTION_FORM_CREDIT:
					gsb_form_widget_set_empty (element->element_widget, TRUE);
					gtk_entry_set_text (GTK_ENTRY (element->element_widget), _("Credit"));
					break;

				case TRANSACTION_FORM_CATEGORY:
					gsb_form_widget_set_empty (element->element_widget, TRUE);
					gtk_combofix_set_text (GTK_COMBOFIX (element->element_widget), _("Categories : Sub-categories"));
					break;

				case TRANSACTION_FORM_FREE:
					break;

				case TRANSACTION_FORM_BUDGET:
					gsb_form_widget_set_empty (element->element_widget, TRUE);
					gtk_combofix_set_text (GTK_COMBOFIX (element->element_widget), _("Budgetary line"));
					break;

				case TRANSACTION_FORM_NOTES:
					gsb_form_widget_set_empty (element->element_widget, TRUE);
					gtk_entry_set_text (GTK_ENTRY (element->element_widget), _("Notes"));
					break;

				case TRANSACTION_FORM_TYPE:
					{
						gint payment_number;

						payment_number = gsb_data_account_get_default_debit (account_number);
						gsb_payment_method_create_combo_list (element->element_widget,
															  GSB_PAYMENT_DEBIT,
															  account_number,
															  0,
															  FALSE);
						gsb_payment_method_set_payment_position (element->element_widget, payment_number);
						gtk_widget_set_sensitive (GTK_WIDGET (element->element_widget), FALSE);
						gsb_payment_method_show_cheque_entry_if_necessary (payment_number);
						break;
					}

				case TRANSACTION_FORM_CONTRA:
					gtk_widget_hide (element->element_widget);
					break;

				case TRANSACTION_FORM_CHEQUE:
					{
						gint payment_number;

						payment_number = gsb_data_account_get_default_debit (account_number);
						if (!gsb_data_payment_get_automatic_numbering (payment_number))
						{
							gtk_entry_set_text (GTK_ENTRY (element->element_widget), _("Cheque/Transfer number"));
							gsb_form_widget_set_empty (element->element_widget, TRUE);
						}
						break;
					}

				case TRANSACTION_FORM_DEVISE:
					g_signal_handlers_block_by_func (G_OBJECT (element->element_widget),
													 G_CALLBACK (gsb_form_transaction_currency_changed),
													 NULL);
					gsb_currency_set_combobox_history (element->element_widget,
													   gsb_data_account_get_currency (account_number));
					gtk_widget_set_sensitive (GTK_WIDGET (element->element_widget), FALSE);
					g_signal_handlers_unblock_by_func (G_OBJECT (element->element_widget),
													   G_CALLBACK (gsb_form_transaction_currency_changed),
													   NULL);
					break;

				case TRANSACTION_FORM_CHANGE:
					gtk_widget_hide (element->element_widget);
					break;

				case TRANSACTION_FORM_BANK:
					gsb_form_widget_set_empty (element->element_widget, TRUE);
					gtk_entry_set_text (GTK_ENTRY (element->element_widget), _("Bank references"));
					break;

				case TRANSACTION_FORM_VOUCHER:
					gsb_form_widget_set_empty (element->element_widget, TRUE);
					gtk_entry_set_text (GTK_ENTRY (element->element_widget), _("Voucher"));
					break;

				case TRANSACTION_FORM_OP_NB:
					gtk_label_set_text (GTK_LABEL (element->element_widget), NULL);
					break;

				case TRANSACTION_FORM_MODE:
					gtk_label_set_text (GTK_LABEL (element->element_widget), NULL);
					break;
			}
		}
		tmp_list = tmp_list->next;
    }
    g_object_set_data (G_OBJECT (transaction_form), "transaction_number_in_form", NULL);

    /* don't show the recover button */
    gtk_widget_hide (form_button_recover_split);

    /* unsensitive the valid and cancel buttons */
    gtk_widget_set_sensitive (GTK_WIDGET (form_button_valid), FALSE);
    gtk_widget_set_sensitive (GTK_WIDGET (form_button_cancel), FALSE);

    return FALSE;
}

/**
 * Determine element is expandable or not in a GtkTable.
 *
 * \param element_number
 *
 * \return
 */
gint gsb_form_get_element_expandable (gint element_number)
{
    switch (element_number)
    {
		case TRANSACTION_FORM_OP_NB:
		case TRANSACTION_FORM_MODE:
			return GTK_SHRINK;

		default:
			return GTK_EXPAND | GTK_FILL;
    }
}

/**
 * called when an entry get the focus, if the entry is free,
 * set it normal and erase the help content
 *
 * \param entry
 * \param ev
 *
 * \return FALSE
 **/
gboolean gsb_form_entry_get_focus (GtkWidget *entry)
{
    /* the entry can be a combofix or a combo_box or a real entry */
	if (GTK_IS_ENTRY (entry))
	{
        if (gsb_form_widget_check_empty (entry))
        {
            gtk_entry_set_text (GTK_ENTRY (entry), "");
            gsb_form_widget_set_empty (entry, FALSE);
        }
	}
	else
	{
		if (gsb_form_widget_check_empty (entry))
		{
			gtk_combofix_set_text (GTK_COMBOFIX (entry), "");
			gsb_form_widget_set_empty (entry, FALSE);
		}
	}

	/* sensitive the valid and cancel buttons */
    gtk_widget_set_sensitive (GTK_WIDGET (form_button_valid), TRUE);
    gtk_widget_set_sensitive (GTK_WIDGET (form_button_cancel), TRUE);

    return FALSE;
}

/**
 * called when an entry lose the focus
 *
 * \param entry
 * \param ev
 * \param ptr_origin 	a pointer gint which is the number of the element
 *
 * \return FALSE
 **/
gboolean gsb_form_entry_lose_focus (GtkWidget *entry,
                        GdkEventFocus *ev,
                        gint *ptr_origin)
{
    gchar *string;
    gint element_number;
    GtkWidget *widget;
    GtkWidget *tmp_widget;
    gint account_number;
    gint transaction_number;
    gint payment_number;
    devel_debug (NULL);

	/* still not found, if change the content of the form, something come in entry
     * which is nothing, so protect here */
    if (!GTK_IS_WIDGET (entry) || !GTK_IS_ENTRY (entry))
		return FALSE;

	/* remove the selection */
    gtk_editable_select_region (GTK_EDITABLE (entry), 0, 0);
    element_number = GPOINTER_TO_INT (ptr_origin);
    account_number = gsb_form_get_account_number ();
    transaction_number = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (gsb_form_get_form_widget ()),
															 "transaction_number_in_form"));

	/* sometimes the combofix popus stays showed, so remove here */
    if (element_number == TRANSACTION_FORM_PARTY
     || element_number == TRANSACTION_FORM_CATEGORY
     || element_number == TRANSACTION_FORM_BUDGET)
    {
        widget = gsb_form_widget_get_widget (element_number);
		if (GTK_IS_COMBOFIX (widget))
        	gtk_combofix_hide_popup (GTK_COMBOFIX (widget));
    }

    /* string will be filled only if the field is empty */
    string = NULL;
    switch (element_number)
    {
		case TRANSACTION_FORM_DATE:
		case TRANSACTION_FORM_VALUE_DATE:
			if (!strlen (gtk_entry_get_text (GTK_ENTRY (entry))))
			{
				if (save_form_date)
					string = gsb_format_gdate (save_form_date);
				else
					string = gsb_date_today ();
				gtk_entry_set_text (GTK_ENTRY (entry), string);
				g_free (string);
				string = NULL;
			}
			break;

		case TRANSACTION_FORM_PARTY :
			/* we complete the transaction */
			if (!gsb_form_transaction_complete_form_by_payee (gtk_entry_get_text (GTK_ENTRY (entry))))
				string = gsb_form_widget_get_name (TRANSACTION_FORM_PARTY);
			break;

		case TRANSACTION_FORM_DEBIT :
			if (gsb_form_widget_amount_entry_validate (element_number) == FALSE)
				return TRUE;

			/* we change the payment method to adapt it for the debit */
			if (strlen (gtk_entry_get_text (GTK_ENTRY (entry))))
			{
				/* empty the credit */
				widget = gsb_form_widget_get_widget (TRANSACTION_FORM_CREDIT);
				if (!gsb_form_widget_check_empty (widget))
				{
					gtk_entry_set_text (GTK_ENTRY (widget),
										gsb_form_widget_get_name (TRANSACTION_FORM_CREDIT));
					gsb_form_widget_set_empty (widget, TRUE);
				}

				widget = gsb_form_widget_get_widget (TRANSACTION_FORM_TYPE);

				/* change the method of payment if necessary
				 * (if grey, it's a child of split so do nothing) */
				if (widget && gtk_widget_get_sensitive (widget))
				{
					/* change the signe of the method of payment and the contra */
					if (gsb_payment_method_get_combo_sign (widget) == GSB_PAYMENT_CREDIT)
					{
						gsb_payment_method_create_combo_list (widget,
															  GSB_PAYMENT_DEBIT,
															  account_number,
															  0,
															  FALSE);
						/* if there is no payment method, the last function hide it, but we have
						 * to hide the cheque element too */
						if (!gtk_widget_get_visible (widget))
							gtk_widget_hide (gsb_form_widget_get_widget (TRANSACTION_FORM_CHEQUE));

						widget = gsb_form_widget_get_widget (TRANSACTION_FORM_CONTRA);
						if (widget && gtk_widget_get_visible (widget))
							gsb_payment_method_create_combo_list (gsb_form_widget_get_widget
																  (TRANSACTION_FORM_CONTRA),
																  GSB_PAYMENT_CREDIT,
																  account_number,
																  0,
																  TRUE);
					}
				}
				gsb_form_check_auto_separator (entry);
			}
			else
			{
				/* si pas de nouveau débit on essaie de remettre l'ancien crédit */
				if ((string = gsb_form_widget_get_old_credit ()))
				{
					tmp_widget = gsb_form_widget_get_widget (TRANSACTION_FORM_CREDIT);
					gtk_entry_set_text (GTK_ENTRY (tmp_widget), string);
					gsb_form_widget_set_empty (tmp_widget, FALSE);
					g_free (string);

					widget = gsb_form_widget_get_widget (TRANSACTION_FORM_TYPE);
					if (widget && gtk_widget_get_sensitive (widget))
					{
						/* change the signe of the method of payment and the contra */
						if (gsb_payment_method_get_combo_sign (widget) == GSB_PAYMENT_DEBIT)
						{
							if (transaction_number == -1)
								payment_number =  gsb_form_widget_get_old_credit_payment_number ();
							else
								payment_number = gsb_data_transaction_get_method_of_payment_number
													(transaction_number);

							gsb_payment_method_create_combo_list (widget,
																  GSB_PAYMENT_CREDIT,
																  account_number,
																  0,
																  FALSE);
							gsb_payment_method_set_payment_position (widget, payment_number);

							widget = gsb_form_widget_get_widget (TRANSACTION_FORM_CHEQUE);
							if (widget && gtk_widget_get_visible (widget))
							{
								if (gsb_form_widget_get_old_credit_payment_content ())
								{
									gtk_entry_set_text (GTK_ENTRY (widget),
														gsb_form_widget_get_old_credit_payment_content ());
									gsb_form_widget_set_empty (widget, FALSE);
								}
							}

							widget = gsb_form_widget_get_widget (TRANSACTION_FORM_CONTRA);
							if (widget && gtk_widget_get_visible (widget))
								gsb_payment_method_create_combo_list (gsb_form_widget_get_widget
																	  (TRANSACTION_FORM_CONTRA),
																	  GSB_PAYMENT_DEBIT,
																	  account_number,
																	  0,
																	  TRUE);
						}
					}
				}
				string = gsb_form_widget_get_name (TRANSACTION_FORM_DEBIT);
			}
			break;

		case TRANSACTION_FORM_CREDIT :
			if (gsb_form_widget_amount_entry_validate (element_number) == FALSE)
				return TRUE;
			/* we change the payment method to adapt it for the debit */
			if (strlen (gtk_entry_get_text (GTK_ENTRY (entry))))
			{
				/* empty the debit */
				widget = gsb_form_widget_get_widget (TRANSACTION_FORM_DEBIT);
				if (!gsb_form_widget_check_empty (widget))
				{
					gtk_entry_set_text (GTK_ENTRY (widget),
										gsb_form_widget_get_name (TRANSACTION_FORM_DEBIT));
					gsb_form_widget_set_empty (widget, TRUE);
				}
				widget = gsb_form_widget_get_widget (TRANSACTION_FORM_TYPE);

				/* change the method of payment if necessary
				 * (if grey, it's a child of split so do nothing) */
				if (widget && gtk_widget_get_sensitive (widget))
				{
					/* change the signe of the method of payment and the contra */
					if (gsb_payment_method_get_combo_sign (widget) == GSB_PAYMENT_DEBIT)
					{
						gsb_payment_method_create_combo_list (widget, GSB_PAYMENT_CREDIT, account_number, 0, FALSE);
						/* if there is no payment method, the last function hide it, but we have
						 * to hide the cheque element too */
						if (!gtk_widget_get_visible (widget))
							gtk_widget_hide (gsb_form_widget_get_widget (TRANSACTION_FORM_CHEQUE));

						widget = gsb_form_widget_get_widget (TRANSACTION_FORM_CONTRA);
						if (widget && gtk_widget_get_visible (widget))
							gsb_payment_method_create_combo_list (widget, GSB_PAYMENT_DEBIT, account_number, 0, TRUE);
					}
				}
				gsb_form_check_auto_separator (entry);
			}
			else
			{
				/* si pas de nouveau credit on essaie de remettre l'ancien débit */
				if ((string = gsb_form_widget_get_old_debit ()))
				{
					tmp_widget = gsb_form_widget_get_widget (TRANSACTION_FORM_DEBIT);

					gtk_entry_set_text (GTK_ENTRY (tmp_widget), string);
					gsb_form_widget_set_empty (tmp_widget, FALSE);
					g_free (string);

					widget = gsb_form_widget_get_widget (TRANSACTION_FORM_TYPE);
					if (widget
						 &&
						 gtk_widget_get_sensitive (widget))
					{
						/* change the signe of the method of payment and the contra */
						if (gsb_payment_method_get_combo_sign (widget) == GSB_PAYMENT_CREDIT)
						{
						if (transaction_number == -1)
							payment_number =  gsb_form_widget_get_old_debit_payment_number ();
						else
							payment_number = gsb_data_transaction_get_method_of_payment_number (
												transaction_number);

						gsb_payment_method_create_combo_list (widget, GSB_PAYMENT_DEBIT, account_number, 0, FALSE);
						gsb_payment_method_set_payment_position (widget, payment_number);

						widget = gsb_form_widget_get_widget (TRANSACTION_FORM_CHEQUE);
						if (widget && gtk_widget_get_visible (widget))
						{
							if (gsb_form_widget_get_old_debit_payment_content ())
							{
								gtk_entry_set_text (GTK_ENTRY (widget),
													gsb_form_widget_get_old_debit_payment_content ());
								gsb_form_widget_set_empty (widget, FALSE);
							}
						}

						widget = gsb_form_widget_get_widget (TRANSACTION_FORM_CONTRA);
						if (widget && gtk_widget_get_visible (widget))
							gsb_payment_method_create_combo_list (gsb_form_widget_get_widget
																  (TRANSACTION_FORM_CONTRA),
																  GSB_PAYMENT_CREDIT,
																  account_number,
																  0,
																  TRUE);
						}
					}
				}
				string = gsb_form_widget_get_name (TRANSACTION_FORM_CREDIT);
			}
			break;

		case TRANSACTION_FORM_CATEGORY :
			if (strlen (gtk_entry_get_text (GTK_ENTRY (entry))))
			{
				/* if it's a transfer, set the content of the contra combo */
				if (gsb_data_form_check_for_value (TRANSACTION_FORM_CONTRA))
				{
					/* if it's a transfer, set the contra_method of payment menu */
					gint contra_account_number;

					contra_account_number = gsb_form_check_for_transfer (gtk_entry_get_text (GTK_ENTRY (entry)));
					if (contra_account_number >= 0 && contra_account_number != account_number)
					{
						if (gsb_form_widget_check_empty (gsb_form_widget_get_widget (TRANSACTION_FORM_CREDIT)))
							/* there is something in debit */
							gsb_payment_method_create_combo_list (gsb_form_widget_get_widget
																  (TRANSACTION_FORM_CONTRA),
																  GSB_PAYMENT_CREDIT,
																  contra_account_number,
																  0,
																  TRUE);
						else
							/* there is something in credit */
							gsb_payment_method_create_combo_list (gsb_form_widget_get_widget
																  (TRANSACTION_FORM_CONTRA),
																  GSB_PAYMENT_DEBIT,
																  contra_account_number,
																  0,
																  TRUE);
					}
					else
						gtk_widget_hide (gsb_form_widget_get_widget (TRANSACTION_FORM_CONTRA));
				}
				if (strcmp (gtk_entry_get_text (GTK_ENTRY (entry)), _("Split of transaction"))
				 && gtk_widget_get_visible (form_button_recover_split))
				{
					gtk_widget_hide (form_button_recover_split);
				}
			}
			else
				string = gsb_form_widget_get_name (TRANSACTION_FORM_CATEGORY);
			break;

		case TRANSACTION_FORM_CHEQUE :
		case TRANSACTION_FORM_BUDGET :
		case TRANSACTION_FORM_VOUCHER :
		case TRANSACTION_FORM_NOTES :
		case TRANSACTION_FORM_BANK :
			if (!strlen (gtk_entry_get_text (GTK_ENTRY (entry))))
				string = _(gsb_form_widget_get_name (element_number));
			break;

		default :
			break;
    }

    /* if string is not NULL, the entry is empty so set the empty field to TRUE */
    if (string)
    {
		switch (element_number)
		{
			case TRANSACTION_FORM_PARTY :
			case TRANSACTION_FORM_CATEGORY :
			case TRANSACTION_FORM_BUDGET :
				/* need to work with the combofix to avoid some signals if we work
				 * directly on the entry */
				widget = gsb_form_widget_get_widget (element_number);
				gtk_combofix_set_text (GTK_COMBOFIX (widget), _(string));
				break;

			default:
				gtk_entry_set_text (GTK_ENTRY (entry), string);
			break;
		}
		gsb_form_widget_set_empty (entry, TRUE);
    }

    return FALSE;
}

/**
 * called when leave an amount entry, il the automatic separator
 * is on, set the separator in the entry if there is none
 *
 * \param entry
 *
 * \return
 **/
void gsb_form_check_auto_separator (GtkWidget *entry)
{
    gint account_number;
    gchar *string;
    gint floating_point;
    gchar *tmp = NULL;
    gchar *mon_decimal_point;
    gunichar decimal_point;
    gsize i;

	if (!etat.automatic_separator || !entry)
		return;

    /* we need a my_strdup to permit to do the g_free later
     * because if strlen < floating point we need to
     * malloc another string */
    string = my_strdup (gtk_entry_get_text (GTK_ENTRY(entry)));
    if (!string || strlen (string) == 0)
		return;

    account_number = gsb_form_get_account_number ();
    floating_point = gsb_data_currency_get_floating_point (gsb_data_account_get_currency (account_number));

    mon_decimal_point = gsb_locale_get_mon_decimal_point ();
    decimal_point = g_utf8_get_char_validated (mon_decimal_point, -1);

    if (g_utf8_strchr (string, -1, decimal_point))
    {
        g_free (string);
        g_free (mon_decimal_point);
        return;
    }

    /* if string is < the floating_point, increase it to have
     * 1 character more (to set the 0 before the .) */
    if ((gint) strlen(string) <= floating_point)
    {
        gchar *concat_tmp;

        tmp = g_malloc (floating_point - strlen(string) + 2);
        for (i=0 ; i<(floating_point - strlen(string) + 1) ; i++)
            tmp[i] = '0';
        tmp[floating_point - strlen(string) + 1] = 0;
        concat_tmp = g_strconcat (tmp, string, NULL);
        g_free (tmp);
        g_free (string);
        string = concat_tmp;
    }

    tmp = g_malloc ((strlen(string)+2) * sizeof (gchar));

    memcpy (tmp, string, strlen(string) - floating_point);

    i = strlen(string) - floating_point;
    tmp[i] = decimal_point;
    i++;
    memcpy (tmp + i, string + i - 1, floating_point);
    i = i + floating_point;
    tmp[i] = 0;
    gtk_entry_set_text (GTK_ENTRY (entry), tmp);
    g_free (tmp);
    g_free (string);
    g_free (mon_decimal_point);
}

/**
 * called when we press the button in an entry field in
 * the form
 * if "transaction_number_in_form" as data of transaction_form is NULL,
 * do the necessary to begin a new empty transaction
 *
 * \param entry 		which receive the signal
 * \param ev 			can be NULL
 * \param ptr_origin 	a pointer to int on the element_number
 *
 * \return FALSE
 **/
gboolean gsb_form_button_press_event (GtkWidget *entry,
									  GdkEventButton *ev,
									  gint *ptr_origin)
{
    GtkWidget *widget;

	/* we do the first part only if we click on the form directly, without double click or
     * entry in the transaction list,
     * in that case, transaction_number_in_form is 0 and set to -1, as a white line */
    if (!g_object_get_data (G_OBJECT (transaction_form), "transaction_number_in_form"))
    {
		GtkWidget *date_entry;

		/* set the new transaction number */
		g_object_set_data (G_OBJECT (transaction_form), "transaction_number_in_form", GINT_TO_POINTER (-1));

		/* set the current date into the date entry */
		date_entry = gsb_form_widget_get_widget (TRANSACTION_FORM_DATE);
		if (gsb_form_widget_check_empty (date_entry))
		{
			if (save_form_date)
				gtk_entry_set_text (GTK_ENTRY (date_entry), gsb_format_gdate (save_form_date));
			else
				gtk_entry_set_text (GTK_ENTRY (date_entry), gsb_date_today ());
			gsb_form_widget_set_empty (date_entry, FALSE);
		}

		/* set the form sensitive */
		gsb_form_change_sensitive_buttons (TRUE);

		/* if we are on scheduled transactions, show the scheduled part of form */
		if (gsb_form_get_origin () == ORIGIN_VALUE_SCHEDULED)
			gsb_form_scheduler_sensitive_buttons (TRUE);

		/* set the number of cheque for the method of payment if necessary */
		widget = gsb_form_widget_get_widget (TRANSACTION_FORM_TYPE);

		if (widget && gtk_widget_get_visible (widget))
		{
			gint payment_number;
			gchar* tmp_str;

			payment_number = gsb_payment_method_get_selected_number (widget);
			if (gsb_data_payment_get_automatic_numbering (payment_number))
			{
				widget = gsb_form_widget_get_widget (TRANSACTION_FORM_CHEQUE);

				gsb_form_entry_get_focus (widget);

				if (!strlen (gtk_entry_get_text (GTK_ENTRY (widget))))
				{
					tmp_str = gsb_data_payment_incremente_last_number (payment_number, 1);
					gtk_entry_set_text (GTK_ENTRY (widget), tmp_str);
					g_free (tmp_str);
				}
			}
		}
    }

    return FALSE;
}

/**
 * sensitive or unsensitive all the buttons on the form
 *
 * \param sensitive TRUE or FALSE
 *
 * \return FALSE
 **/
gboolean gsb_form_change_sensitive_buttons (gboolean sensitive)
{
    if (gsb_data_form_check_for_value (TRANSACTION_FORM_TYPE))
		gtk_widget_set_sensitive (GTK_WIDGET (gsb_form_widget_get_widget (TRANSACTION_FORM_TYPE)),
								  sensitive);

    if (gsb_data_form_check_for_value (TRANSACTION_FORM_DEVISE))
		gtk_widget_set_sensitive (GTK_WIDGET (gsb_form_widget_get_widget (TRANSACTION_FORM_DEVISE)),
								  sensitive);

    if (gsb_data_form_check_for_value (TRANSACTION_FORM_EXERCICE))
		gtk_widget_set_sensitive (GTK_WIDGET (gsb_form_widget_get_widget (TRANSACTION_FORM_EXERCICE)),
								  sensitive);
    return FALSE;
}

/**
 * called when press a key on an element of the form
 *
 * \param widget 		which receive the signal
 * \param ev
 * \param ptr_origin 	a pointer number of the element
 *
 * \return FALSE
 **/
gboolean gsb_form_key_press_event (GtkWidget *widget,
								   GdkEventKey *ev,
								   gint *ptr_origin)
{
    GtkWidget *widget_prov;
    gint account_number;
    gint element_number;
    gint element_suivant;

	element_number = GPOINTER_TO_INT (ptr_origin);
    account_number = gsb_form_get_account_number ();

    /* if conf.form_enter_key = 1, entry finish the transaction, else does as tab */
    if (!conf.form_enter_key && (ev->keyval == GDK_KEY_Return || ev->keyval == GDK_KEY_KP_Enter))
		ev->keyval = GDK_KEY_Tab ;

    if (!g_object_get_data (G_OBJECT (transaction_form), "transaction_number_in_form"))
	/* set the new transaction number */
        g_object_set_data (G_OBJECT (transaction_form), "transaction_number_in_form", GINT_TO_POINTER (-1));

    switch (ev->keyval)
    {
		case GDK_KEY_1:
		case GDK_KEY_2:
		case GDK_KEY_3:
		case GDK_KEY_4:
		case GDK_KEY_5:
		case GDK_KEY_6:
		case GDK_KEY_7:
		case GDK_KEY_8:
		case GDK_KEY_9:
		case GDK_KEY_0:
			switch (element_number)
			{
				case TRANSACTION_FORM_DEBIT:
					widget_prov = gsb_form_widget_get_widget (TRANSACTION_FORM_CREDIT);
					if (!gsb_form_widget_check_empty (widget_prov))
					{
						gtk_entry_set_text (GTK_ENTRY (widget_prov),
											gsb_form_widget_get_name (TRANSACTION_FORM_CREDIT));
						gsb_form_widget_set_empty (widget_prov, TRUE);
					}
					break;
				case TRANSACTION_FORM_CREDIT:
					widget_prov = gsb_form_widget_get_widget (TRANSACTION_FORM_DEBIT);
					if (!gsb_form_widget_check_empty (widget_prov))
					{
						gtk_entry_set_text (GTK_ENTRY (widget_prov),
											gsb_form_widget_get_name (TRANSACTION_FORM_DEBIT));
						gsb_form_widget_set_empty (widget_prov, TRUE);
					}
					break;
			}
			break;

		case GDK_KEY_Escape :
			gsb_form_escape_form ();
			break;

		case GDK_KEY_Up:
			element_suivant = gsb_form_widget_next_element (account_number, element_number, GTK_DIR_UP);
			gsb_form_widget_set_focus (element_suivant);
			return TRUE;
			break;

		case GDK_KEY_Down:
			element_suivant = gsb_form_widget_next_element (account_number, element_number, GTK_DIR_DOWN);
			gsb_form_widget_set_focus (element_suivant);
			return TRUE;
			break;

		case GDK_KEY_ISO_Left_Tab:
			if (element_number == TRANSACTION_FORM_CREDIT
				|| element_number == TRANSACTION_FORM_DEBIT)
			{
				if (gsb_form_widget_amount_entry_validate (element_number) == FALSE)
					return TRUE;
			}

			element_suivant = gsb_form_widget_next_element (account_number, element_number, GTK_DIR_LEFT);

			if (element_number == TRANSACTION_FORM_VALUE_DATE)
			{
				widget_prov = gsb_form_widget_get_widget (TRANSACTION_FORM_VALUE_DATE);

				if (strlen (gtk_entry_get_text (GTK_ENTRY (widget_prov))) == 0)
				{
					gsb_form_widget_set_empty (widget_prov, TRUE);
					gtk_entry_set_text (GTK_ENTRY (widget_prov), _("Value date"));
				}
				gsb_form_widget_set_focus (element_suivant);
			}

			gsb_form_widget_set_focus (element_suivant);
			return TRUE;
			break;

		case GDK_KEY_Tab :
			if (element_number == TRANSACTION_FORM_CREDIT
				|| element_number == TRANSACTION_FORM_DEBIT)
			{
				if (gsb_form_widget_amount_entry_validate (element_number) == FALSE)
					return TRUE;
			}

			element_suivant = gsb_form_widget_next_element (account_number, element_number, GTK_DIR_RIGHT);

			/* if element = value_date fix the bug 578 */
			if (element_number == TRANSACTION_FORM_VALUE_DATE)
			{
				widget_prov = gsb_form_widget_get_widget (TRANSACTION_FORM_VALUE_DATE);

				if (strlen (gtk_entry_get_text (GTK_ENTRY (widget_prov))) == 0)
				{
					gsb_form_widget_set_empty (widget_prov, TRUE);
					gtk_entry_set_text (GTK_ENTRY (widget_prov), _("Value date"));
				}
				gsb_form_widget_set_focus (element_suivant);
			}

			if (element_suivant == -2)
				gsb_form_finish_edition();
			else
				gsb_form_widget_set_focus (element_suivant);

			return TRUE;
			break;

		case GDK_KEY_KP_Enter :
		case GDK_KEY_Return :

			/* need to check here if we are performing a scheduled transaction in home page
			 * or another transaction, because if we are in home page, cannot finish like that,
			 * we need to finish with the dialog which contains the form */
			if (gsb_gui_navigation_get_current_page () == GSB_HOME_PAGE)
			{
				GtkWidget *tmp_widget;

				/* ok, we are on the home page. the best way is to send the response ok
				 * to the dialog */
				tmp_widget = widget;
				do
				{
					tmp_widget = gtk_widget_get_parent (GTK_WIDGET (tmp_widget));
				}
				while (GTK_IS_WIDGET (tmp_widget) && !GTK_IS_DIALOG (tmp_widget));
				gtk_dialog_response (GTK_DIALOG (tmp_widget), GTK_RESPONSE_OK);

				return TRUE;
			}
			else
			{
				if (element_number == TRANSACTION_FORM_CREDIT
					|| element_number == TRANSACTION_FORM_DEBIT)
				{
					if (gsb_form_widget_amount_entry_validate (element_number) == FALSE)
						return TRUE;
				}

				widget_prov = gsb_form_widget_get_widget (TRANSACTION_FORM_PARTY);

				if (GTK_IS_COMBOFIX (widget_prov))
					gsb_form_transaction_complete_form_by_payee (gtk_combofix_get_text (GTK_COMBOFIX (widget_prov)));

				gsb_form_finish_edition ();
				return TRUE;
			}
			break;

		case GDK_KEY_KP_Add:
		case GDK_KEY_plus:
		case GDK_KEY_equal:		/* This should make all our US users happy */
			/* increase the check of 1 */
			if (element_number == TRANSACTION_FORM_CHEQUE)
			{
				increment_decrement_champ (widget, 1);
				return TRUE;
			}
			break;

		case GDK_KEY_KP_Subtract:
		case GDK_KEY_minus:
			/* decrease the check of 1 */
			if (element_number == TRANSACTION_FORM_CHEQUE)
			{
				increment_decrement_champ (widget, -1);
				return TRUE;
			}
			break;
    }

    if (element_number == TRANSACTION_FORM_TYPE)
    {
        gint payment_number;

        switch (ev->keyval)
        {
			case GDK_KEY_c:
			case GDK_KEY_C:
				payment_number = gsb_data_payment_get_number_by_name (_("Credit card"), account_number);
				if (payment_number)
					gsb_payment_method_set_combobox_history (widget, payment_number, TRUE);

				return TRUE;
				break;

			case GDK_KEY_d:
			case GDK_KEY_D:
				payment_number = gsb_data_payment_get_number_by_name (_("Direct deposit"), account_number);
				if (payment_number)
					gsb_payment_method_set_combobox_history (widget, payment_number, TRUE);

				return TRUE;
				break;

			case GDK_KEY_h:
			case GDK_KEY_H:
				payment_number = gsb_data_payment_get_number_by_name (_("Check"), account_number);
				if (payment_number)
					gsb_payment_method_set_combobox_history (widget, payment_number, TRUE);

				return TRUE;
				break;

			case GDK_KEY_l:
			case GDK_KEY_L:
				payment_number = gsb_data_payment_get_number_by_name (_("Cash withdrawal"), account_number);
				if (payment_number)
					gsb_payment_method_set_combobox_history (widget, payment_number, TRUE);

				return TRUE;
				break;

			case GDK_KEY_p:
			case GDK_KEY_P:
				payment_number = gsb_data_payment_get_number_by_name (_("Direct debit"), account_number);
				if (payment_number)
					gsb_payment_method_set_combobox_history (widget, payment_number, TRUE);

				return TRUE;
				break;

			case GDK_KEY_t:
			case GDK_KEY_T:
			case GDK_KEY_v:
			case GDK_KEY_V:
				payment_number = gsb_data_payment_get_number_by_name (_("Transfer"), account_number);
				if (payment_number)
					gsb_payment_method_set_combobox_history (widget, payment_number, TRUE);

				return TRUE;
				break;
        }
    }

    return FALSE;
}

/**
 * called when the user finishes the edition of the form
 * this function works for transaction and scheduled transactions,
 * it add/modify the transaction in the form
 *
 * \param none
 *
 * \return FALSE
 **/
gboolean gsb_form_finish_edition (void)
{
    GSList *list_tmp;
    GSList *payee_list;
    gint account_number;
    gint mother_number;
    gint new_transaction;
    gint nbre_passage = 0;
    gint saved_scheduled_number = 0;
    gint source_transaction_number = -1;
    gint transaction_number;
    gboolean execute_scheduled = FALSE;
    gboolean is_transaction;

    devel_debug (NULL);

    /* get the number of the transaction, stored in the form (< 0 if new) */
    transaction_number = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (transaction_form),
															 "transaction_number_in_form"));

    /* set a debug here, if transaction_number is 0, should look for where it comes */
    if (!transaction_number)
        notice_debug ("Coming in gsb_form_finish_edition with a 0 number of transaction. "
					  "This is a bug,\n"
					  "please try to do it again and report the bug.");

    account_number = gsb_form_get_account_number ();

    /* check if we finish a transaction or a scheduled transaction
     * we have to decide if it's a transaction or a scheduled transaction,
     * and if it's a scheduled, if we execute it (and create transaction) or work on it*/
    if (gsb_form_get_origin () == ORIGIN_VALUE_SCHEDULED
		|| gsb_form_get_origin () == ORIGIN_VALUE_HOME)
    {
        if (g_object_get_data (G_OBJECT (transaction_form), "execute_scheduled"))
        {
           /* we want to execute the scheduled transaction */
           is_transaction = TRUE;
           execute_scheduled = TRUE;

           /* we need to keep the number of scheduled, to check later if there is
            * some children and modifie the scheduled transaction */
           saved_scheduled_number = transaction_number;
           /* as it's a new transaction, do the same as a white line */
           transaction_number = -1;
        }
        else
            is_transaction = FALSE;
    }
    else
        is_transaction = TRUE;

    /* a new transaction has a number < 0
     * -1 for the general white line
     *  -2, -3, ... for the white lines of scheduled transactions
     *  or it's an execution of scheduled transaction */
    if (transaction_number < 0)
        new_transaction = 1;
    else
        new_transaction = 0;

    /* check if the datas are ok */
    if (!gsb_form_validate_form_transaction (transaction_number, is_transaction))
        return FALSE;

    /* if the party is a report, we make as transactions as the number of parties in the
     * report. So we create a list with the party's numbers or -1 if it's a normal
     * party */
    payee_list = gsb_form_transaction_get_parties_list_from_report ();

    /* now we go throw the list */
    list_tmp = payee_list;
    while (list_tmp)
    {
        if (GPOINTER_TO_INT (list_tmp->data) == -1)
            /* it's a normal party, we set the list_tmp to NULL */
            list_tmp = NULL;
        else
        {
            /* it's a report, so each time we come here we set the parti's combofix to the
             * party of the report */

            if (!list_tmp->data)
            {
                dialogue_error (_("No payee selected for this report."));
                return FALSE;
            }
            else
            {
                GtkWidget *widget;
                gint payment_number;

                if (nbre_passage == 0)
                {
                    widget = gsb_form_widget_get_widget (TRANSACTION_FORM_PARTY);
                    gtk_combofix_set_text (GTK_COMBOFIX (widget),
										   gsb_data_payee_get_name (GPOINTER_TO_INT (list_tmp->data),
																	TRUE));
                    gsb_form_widget_set_empty (widget, FALSE);
                }
                else
                {
                    transaction_number = gsb_data_transaction_new_transaction (account_number);

                    gsb_data_transaction_copy_transaction (source_transaction_number,
														   transaction_number, TRUE);
                    gsb_data_transaction_set_party_number (transaction_number,
														   GPOINTER_TO_INT (list_tmp->data));

                    /* if it's not the first party and the method of payment has to change its number (cheque),
                     * we increase the number. as we are in a party's list, it's always a new transactio,
                     * so we know that it's not the first if transaction_number is not 0 */

                    payment_number = gsb_data_transaction_get_method_of_payment_number (source_transaction_number);

                    if (gsb_data_payment_get_automatic_numbering (payment_number))
                    {
                        gchar *tmp_str;

                        tmp_str = gsb_data_payment_incremente_last_number (payment_number, nbre_passage);
                        gsb_data_transaction_set_method_of_payment_content (transaction_number, tmp_str);
                        g_free (tmp_str) ;
                    }
                    gsb_transactions_list_append_new_transaction (transaction_number, TRUE);
                }
                nbre_passage++;
            }

            list_tmp = list_tmp->next;
            if (list_tmp == NULL)
                break;
            else if (nbre_passage > 1)
                continue;
        }

        /* now we create the transaction if necessary and set the mother in case of child of split */
        if (new_transaction)
        {
            /* it's a new transaction, we create it, and set the mother if necessary */
            gint mother_transaction = 0;

            /* if we are on a white child (ie number < -1, -1 is only for the general white line),
             * get the mother of transaction */
            if (transaction_number < -1)
                mother_transaction = gsb_data_mix_get_mother_transaction_number (transaction_number,
																				 is_transaction);
            transaction_number = gsb_data_mix_new_transaction (account_number, is_transaction);
            if (source_transaction_number == -1)
                source_transaction_number = transaction_number;

            gsb_data_mix_set_mother_transaction_number (transaction_number, mother_transaction, is_transaction);

            /* si l'opération mère est rapprochée on marque aussi la fille */
            if (mother_transaction
             &&
             gsb_data_transaction_get_marked_transaction (mother_transaction) == OPERATION_RAPPROCHEE)
            {
                gsb_data_transaction_set_marked_transaction (transaction_number, OPERATION_RAPPROCHEE);
                gsb_data_transaction_set_reconcile_number (transaction_number,
														   gsb_data_transaction_get_reconcile_number
														   (mother_transaction));
            }
        }

        /* take the datas in the form, except the category */
        gsb_form_take_datas_from_form (transaction_number, is_transaction);

        /* si l'opération est une opération planifiée exécutée on met en forme TRANSACTION_FORM_CHEQUE */
        if (execute_scheduled)
        {
            gint payment_number;

            payment_number = gsb_data_transaction_get_method_of_payment_number (transaction_number);

            if (gsb_data_payment_get_automatic_numbering (payment_number))
            {
                gchar *tmp_str;

                tmp_str = gsb_data_payment_incremente_last_number (payment_number, 1);
                gsb_data_transaction_set_method_of_payment_content (transaction_number, tmp_str);
                g_free (tmp_str) ;
            }
        }

        /* perhaps the currency button is not shown
         * in that case, we give the account currency to that transaction */
        if (new_transaction && !gsb_form_widget_get_widget (TRANSACTION_FORM_DEVISE))
            gsb_data_mix_set_currency_number (transaction_number,
											  gsb_data_account_get_currency (account_number),
											  is_transaction);

        /* get the category and do the stuff with that (contra-transaction...) */
        gsb_form_get_categories (transaction_number, new_transaction, is_transaction);

        /* for the rest we need to split for transactions/scheduled */
        if (is_transaction)
        {
            /* it's a transaction or an execution of scheduled transaction */
            if (new_transaction)
            {
				gint split_transaction_number;

				gsb_transactions_list_append_new_transaction (transaction_number, TRUE);

				/* if it's a real new transaction and if it's a split, we ask if the user wants
				 * to recover previous children */
				if (gsb_data_transaction_get_split_of_transaction (transaction_number)
					&& !execute_scheduled
					&& gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (form_button_recover_split))
					&& (split_transaction_number = gsb_form_transactions_look_for_last_party
						(gsb_data_transaction_get_party_number (transaction_number),
						 transaction_number,
														 gsb_data_transaction_get_account_number(transaction_number))))
					gsb_form_transaction_recover_splits_of_transaction (transaction_number,
																		split_transaction_number);
            }
            else
            {
            /* update a transaction */
            gsb_transactions_list_update_transaction (transaction_number);

            /* we are on a modification of transaction, but if the modified transaction is a split
             * and has no children (ie only white line), we assume the user wants now fill the children, so we will do the
             * same as for a new transaction : open the expander and select the white line */
            if (transaction_list_get_n_children (transaction_number) == 1)
            {
                new_transaction = TRUE;
                gsb_transactions_list_switch_expander (transaction_number);
            }
            }
        }
        else
        {
            /* it's a scheduled transaction */
            gsb_form_scheduler_get_scheduler_part (transaction_number);

            if (new_transaction)
            {
				gsb_scheduler_list_append_new_scheduled (transaction_number,
														 gsb_scheduler_list_get_end_date_scheduled_showed ());
				/* recover if necessary previous children */
				if (gsb_data_scheduled_get_split_of_scheduled (transaction_number)
					&&
					gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (form_button_recover_split)))
				{
					gint tmp_account_number;
					gint party_number;
					gint split_transaction_number = 0;

					tmp_account_number = gsb_data_scheduled_get_account_number(transaction_number);
					party_number = gsb_data_scheduled_get_party_number (transaction_number);
					split_transaction_number = gsb_form_transactions_look_for_last_party (party_number,
																						  0,
																						  tmp_account_number);
					if (split_transaction_number)
						gsb_form_scheduler_recover_splits_of_transaction (transaction_number,
																		  split_transaction_number);
				}
            }
            else
				gsb_scheduler_list_update_transaction_in_list (transaction_number);

            /* needed for the two in case of we change the date */
            gsb_scheduler_list_set_background_color (gsb_scheduler_list_get_tree_view ());
            gsb_calendar_update ();
        }
    }
    g_slist_free (payee_list);

    /* if it's a reconciliation and we modify a transaction, check
     * the amount of marked transactions */
    if (is_transaction && run.equilibrage)
    {
        if (new_transaction)
            /* we are reconciling and it's a new transaction, so need to show the checkbox */
            transaction_list_show_toggle_mark (TRUE);
        else
        {
            gsb_reconcile_update_amounts (NULL, NULL);
        }
    }

    /* if we executed a scheduled transation, need to increase the date of the scheduled
     * and execute the children if it's a split */
    if (execute_scheduled)
    {
        gint increase_result;

        /* first, check if it's a scheduled split and execute the childrent */
        if (gsb_data_scheduled_get_split_of_scheduled (saved_scheduled_number))
		{
			gint transfer_account;

			transfer_account = gsb_data_scheduled_get_account_number_transfer (saved_scheduled_number+1);
			if (gsb_data_account_get_bet_init_sch_with_loan (transfer_account))
			{
				GDate *date;

				date = gsb_date_copy (gsb_data_scheduled_get_date (saved_scheduled_number));
				g_date_add_months (date, 1);
				bet_finance_get_loan_amount_at_date (saved_scheduled_number, transfer_account, date, TRUE);
			}
            gsb_scheduler_execute_children_of_scheduled_transaction (saved_scheduled_number, transaction_number);
		}

        /* now we can increase the scheduled transaction */
        increase_result = gsb_scheduler_increase_scheduled (saved_scheduled_number);

        /* the next step is to update the list, but do it only if we are on the scheduled
         * list, else we needn't because the update will be done when going to that list */
        if (gsb_gui_navigation_get_current_page () == GSB_SCHEDULER_PAGE)
        {
            if (increase_result)
                gsb_scheduler_list_update_transaction_in_list (saved_scheduled_number);

            gsb_scheduler_list_set_background_color (gsb_scheduler_list_get_tree_view ());
        }
    }

    /* if it was a new transaction, do the stuff to do another new transaction */
    if (new_transaction && !execute_scheduled)
    {
        /* we are on a new transaction, if that transaction is a split,
         * we give the focus to the new white line created for that and
         * edit it, for that we need to open the transaction to select the
         * white line, and set it as current transaction */
        if (gsb_data_mix_get_split_of_transaction (transaction_number, is_transaction))
        {
            /* it's a split */
            gint white_line_number;

            white_line_number = gsb_data_mix_get_white_line (transaction_number, is_transaction);
            if (is_transaction)
                transaction_list_select (white_line_number);
            else
                gsb_scheduler_list_select (white_line_number);
        }

        /* it was a new transaction, we save the last date entry */
        gsb_date_set_last_date (gtk_entry_get_text (GTK_ENTRY (gsb_form_widget_get_widget (TRANSACTION_FORM_DATE))));

        /* we need to use edit_transaction to make a new child split if necessary */
        if (is_transaction)
            gsb_transactions_list_edit_transaction (gsb_data_account_get_current_transaction_number
													(account_number));
        else
            gsb_scheduler_list_edit_transaction (gsb_scheduler_list_get_current_scheduled_number ());
    }
    else
	{
        gsb_form_escape_form ();
	}

	/* c'est une opération planifiée et l'option fixer le jour de fin de mois est positionnée */
	if (!is_transaction && etat.scheduler_set_fixed_date)
	{
		GDateDay day;

		day = g_date_get_day (save_form_date);
		if (day > 27)
			gsb_form_set_fixed_date_if_necessary (transaction_number, save_form_date);
	}

    /* on sort de la saisie des opérations filles si variance == 0 */
    if ((mother_number = gsb_data_mix_get_mother_transaction_number (transaction_number, is_transaction)))
    {
        if (is_transaction
			&& transaction_list_get_variance (gsb_data_account_get_current_transaction_number (account_number)))
        {
            gtk_tree_view_collapse_all (GTK_TREE_VIEW (gsb_transactions_list_get_tree_view ()));
            transaction_list_select (-1);
        }
        else if (!is_transaction && !execute_scheduled && gsb_data_scheduled_get_variance (mother_number))
        {
            gtk_tree_view_collapse_all (GTK_TREE_VIEW (gsb_scheduler_list_get_tree_view ()));
            gsb_scheduler_list_select (mother_number);
        }
    }

    /* if it was a modification of transaction, we need to update the sort and colors
     * (done automatically for new transaction) */
    if (is_transaction && !new_transaction)
        gsb_transactions_list_update_tree_view (account_number, TRUE);

    /* show the warnings */
    if (is_transaction)
    {
        affiche_dialogue_soldes_minimaux ();
        update_transaction_in_trees (transaction_number);
        if (gsb_data_transaction_get_marked_transaction (transaction_number) == OPERATION_POINTEE)
        {
            gsb_gui_navigation_update_statement_label (account_number);
            run.mise_a_jour_liste_comptes_accueil = TRUE;
        }
    }

    /* as we modify or create a transaction, we invalidate the current report */
    gsb_report_set_current (0);

    /* force the update module budget */
    gsb_data_account_set_bet_maj (account_number, BET_MAJ_ALL);

    gsb_file_set_modified (TRUE);

    /* If the origin of the operation is a model, so we select the new transaction */
    if (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (transaction_form), "transaction_selected_in_form")) == -1)
    {
        transaction_list_select (transaction_number);
        return FALSE;
    }

    if (execute_scheduled)
    {
        GtkTreeSelection *selection;
        GtkTreePath *path;

		gsb_scheduler_list_edit_transaction (gsb_scheduler_list_get_current_scheduled_number ());
        selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (gsb_scheduler_list_get_tree_view ()));
        path = gtk_tree_path_new_from_string ("0");
        gtk_tree_selection_select_path (selection, path);

        gtk_tree_path_free (path);
    }

    /* give the focus to the date widget */
	if (is_transaction)
	{
		if (mother_number == 0)
			gsb_form_widget_set_focus (TRANSACTION_FORM_DATE);
	}
	else
		gsb_scheduler_list_edit_transaction (gsb_scheduler_list_get_current_scheduled_number ());

    return FALSE;
}


/**
 * called when cancel a form
 *
 * \param
 *
 * \return FALSE
 **/
gboolean gsb_form_escape_form (void)
{
    devel_debug (NULL);

    /* in all case clean the scheduler part of the form */
    gsb_form_scheduler_clean ();

    if (conf.formulaire_toujours_affiche)
    {
        gsb_form_clean (gsb_form_get_account_number ());
    }
    else
    {
		GtkWidget *form_expander;

		form_expander = grisbi_win_get_form_expander ();
        gtk_expander_set_expanded (GTK_EXPANDER (form_expander), FALSE);
    }

    switch (gsb_form_get_origin ())
    {
		case ORIGIN_VALUE_OTHER:
			notice_debug ("Should not come here... (gsb_form_escape_form)");
			gtk_widget_grab_focus (gsb_gui_navigation_get_tree_view ());
			break;

		case ORIGIN_VALUE_HOME:
			gtk_widget_grab_focus (GTK_WIDGET (grisbi_app_get_active_window (NULL)));
			break;

		case ORIGIN_VALUE_SCHEDULED:
			gtk_widget_grab_focus (gsb_scheduler_list_get_tree_view ());
			break;

		default:
			gtk_widget_grab_focus (gsb_transactions_list_get_tree_view());
    }

    return FALSE;
}

/**
 * return the widget of the element_number given in param in the list
 *
 * \param element_number
 * \param GSList *list
  *
 * \return a GtkWidget * or NULL
 **/
GtkWidget *gsb_form_get_element_widget_from_list (gint element_number,
												  GSList *list)
{
    GSList *list_tmp;

    list_tmp = list;

    while (list_tmp)
    {
        struct_element *element;

        element = list_tmp->data;
        if (element->element_number == element_number)
            return element->element_widget;

        list_tmp = list_tmp->next;
    }

    return NULL;
}

/**
 *
 *
 * \param
 *
 * \return
 **/
GtkWidget *gsb_form_get_form_transaction_part (void)
{
	return form_transaction_part;
}

/**
 *
 *
 * \param
 *
 * \return
 **/
void gsb_form_sensitive_cancel_valid_buttons (gboolean sensitive)
{
    gtk_widget_set_sensitive (GTK_WIDGET (form_button_valid), TRUE);
    gtk_widget_set_sensitive (GTK_WIDGET (form_button_cancel), TRUE);
}

/**
 *
 *
 * \param
 *
 * \return
 **/
GtkWidget *gsb_form_get_recover_split_button (void)
{
	return form_button_recover_split;
}

/**
 *
 *
 * \param
 *
 * \return
 **/
/* Local Variables: */
/* c-basic-offset: 4 */
/* End: */