1 /* ************************************************************************** */
2 /*                                                                            */
3 /*     Copyright (C)	2004-2006 Benjamin Drieu (bdrieu@april.org)	      */
4 /* 			https://www.grisbi.org				      */
5 /*                                                                            */
6 /*  This program is free software; you can redistribute it and/or modify      */
7 /*  it under the terms of the GNU General Public License as published by      */
8 /*  the Free Software Foundation; either version 2 of the License, or         */
9 /*  (at your option) any later version.                                       */
10 /*                                                                            */
11 /*  This program is distributed in the hope that it will be useful,           */
12 /*  but WITHOUT ANY WARRANTY; without even the implied warranty of            */
13 /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             */
14 /*  GNU General Public License for more details.                              */
15 /*                                                                            */
16 /*  You should have received a copy of the GNU General Public License         */
17 /*  along with this program; if not, write to the Free Software               */
18 /*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
19 /*                                                                            */
20 /* ************************************************************************** */
21 
22 
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 
27 #include "include.h"
28 #include <glib/gi18n.h>
29 
30 
31 /*START_INCLUDE*/
32 #include "meta_budgetary.h"
33 #include "grisbi_win.h"
34 #include "gsb_data_budget.h"
35 #include "gsb_data_scheduled.h"
36 #include "gsb_data_transaction.h"
37 #include "gsb_real.h"
38 #include "imputation_budgetaire.h"
39 #include "structures.h"
40 #include "utils_str.h"
41 /*END_INCLUDE*/
42 
43 
44 /*START_STATIC*/
45 static gint budgetary_line_add_div ( void );
46 static gint budgetary_line_add_sub_div ( int div_id );
47 static gboolean budgetary_line_add_transaction_to_div ( gint transaction_number,
48 						 int div_id );
49 static gchar *budgetary_line_div_name ( gint div );
50 static gint budgetary_line_get_without_div_pointer ( void );
51 static GsbReal budgetary_line_sub_div_balance ( gint div, gint sub_div );
52 static gchar *budgetary_line_sub_div_name ( gint div, gint sub_div );
53 static gint budgetary_line_sub_div_nb_transactions ( gint div, gint sub_div );
54 static gint budgetary_lint_get_number_by_name ( const gchar *name, gboolean create );
55 /*END_STATIC*/
56 
57 /*START_EXTERN*/
58 /*END_EXTERN*/
59 
60 /******************************************************************************/
61 /* Private functions                                                          */
62 /******************************************************************************/
63 /**
64  * return budgetary number
65  *
66  * \param	transaction_number
67  *
68  * \return -1 if split or contra transfert transaction other budgetary number
69  **/
budgetary_line_transaction_div_id(gint transaction_number)70 static gint budgetary_line_transaction_div_id (gint transaction_number)
71 {
72 	if (transaction_number)
73 	{
74 		if ( gsb_data_transaction_get_contra_transaction_number (transaction_number) > 0
75 			 ||
76 			 gsb_data_transaction_get_split_of_transaction (transaction_number))
77 			return -1;
78 		else
79 			return gsb_data_transaction_get_budgetary_number (transaction_number);
80 	}
81 	return 0;
82 }
83 
84 static MetatreeInterface _budgetary_interface = {
85     2,
86     METATREE_BUDGET,
87     budgetary_line_tree_currency,
88     N_("Budgetary line"),
89     N_("budgetary line"),
90     N_("sub-budgetary line"),
91     N_("No budgetary line"),
92     N_("No sub-budgetary line"),
93     budgetary_line_get_without_div_pointer,
94     budgetary_lint_get_number_by_name,
95     gsb_data_budget_get_sub_budget_number_by_name,
96     gsb_data_budget_get_nb_transactions,
97     budgetary_line_sub_div_nb_transactions,
98     budgetary_line_div_name,
99     budgetary_line_sub_div_name,
100     gsb_data_budget_get_balance,
101     budgetary_line_sub_div_balance,
102     gsb_data_budget_get_no_budget,
103     gsb_data_budget_get_no_sub_budget,
104     gsb_data_budget_get_budgets_list,
105     gsb_data_budget_get_sub_budget_list,
106     gsb_data_budget_get_type,
107 
108     budgetary_line_transaction_div_id,
109     gsb_data_transaction_get_sub_budgetary_number,
110     gsb_data_transaction_set_budgetary_number,
111     gsb_data_transaction_set_sub_budgetary_number,
112     gsb_data_scheduled_get_budgetary_number,
113     gsb_data_scheduled_get_sub_budgetary_number,
114     gsb_data_scheduled_set_budgetary_number,
115     gsb_data_scheduled_set_sub_budgetary_number,
116 
117     budgetary_line_add_div,
118     budgetary_line_add_sub_div,
119     gsb_data_budget_remove,
120     gsb_data_budget_sub_budget_remove,
121     budgetary_line_add_transaction_to_div,
122     gsb_data_budget_add_transaction_to_budget,
123     gsb_data_budget_remove_transaction_from_budget,
124     gsb_data_budget_remove_transaction_from_budget,
125     budgetary_hold_position_set_path,
126     budgetary_hold_position_set_expand,
127 };
128 
129 static MetatreeInterface *budgetary_interface = &_budgetary_interface;
130 
131 /******************************************************************************/
132 /* Public functions                                                           */
133 /******************************************************************************/
134 /**
135  *
136  *
137  */
budgetary_line_tree_currency(void)138 gint budgetary_line_tree_currency ( void )
139 {
140 	GrisbiWinEtat *w_etat;
141 
142 	w_etat = (GrisbiWinEtat *) grisbi_win_get_w_etat ();
143 
144 	return w_etat->no_devise_totaux_ib;
145 }
146 
147 
148 
149 /**
150  *
151  *
152  */
budgetary_line_get_without_div_pointer(void)153 gint budgetary_line_get_without_div_pointer ( void )
154 {
155     return 0;
156 }
157 
158 
159 /**
160  *
161  *
162  */
budgetary_lint_get_number_by_name(const gchar * name,gboolean create)163 gint budgetary_lint_get_number_by_name ( const gchar *name, gboolean create )
164 {
165     return gsb_data_budget_get_number_by_name (name, create, 0 );
166 }
167 
168 
169 /**
170  *
171  *
172  */
budgetary_line_sub_div_nb_transactions(gint div,gint sub_div)173 gint budgetary_line_sub_div_nb_transactions ( gint div, gint sub_div )
174 {
175     if ( sub_div )
176     {
177 	return gsb_data_budget_get_sub_budget_nb_transactions ( div,
178 								sub_div );
179     }
180     else if ( div )
181     {
182 	return gsb_data_budget_get_nb_direct_transactions (div);
183     }
184     return 0;
185 }
186 
187 
188 
189 /**
190  *
191  *
192  */
budgetary_line_div_name(gint div)193 gchar * budgetary_line_div_name ( gint div )
194 {
195     return gsb_data_budget_get_name ( div,
196 				      0,
197 				      _(budgetary_interface -> no_div_label ));
198 }
199 
200 
201 
202 /**
203  *
204  *
205  */
budgetary_line_sub_div_name(gint div,gint sub_div)206 gchar * budgetary_line_sub_div_name ( gint div, gint sub_div )
207 {
208     return gsb_data_budget_get_sub_budget_name ( div,
209 						 sub_div,
210 						 _(budgetary_interface -> no_sub_div_label ));
211 }
212 
213 
214 
215 
216 
217 /**
218  *
219  *
220  */
budgetary_line_sub_div_balance(gint div,gint sub_div)221 GsbReal budgetary_line_sub_div_balance ( gint div, gint sub_div )
222 {
223     if ( sub_div )
224     {
225 	return gsb_data_budget_get_sub_budget_balance ( div,
226 							sub_div );
227     }
228     else if ( div )
229     {
230 	return gsb_data_budget_get_direct_balance (div);
231     }
232     return null_real;
233 }
234 
235 
236 
237 
238 /**
239  *
240  *
241  */
budgetary_line_add_div(void)242 gint budgetary_line_add_div ( void )
243 {
244     gint new_budget_number;
245     gchar * name;
246     int i = 1;
247 
248     /** Find a unique name for budget */
249     name =  my_strdup (_("New budget"));
250 
251     while ( gsb_data_budget_get_number_by_name ( name, FALSE, 0 ))
252     {
253 	g_free (name);
254 	i++;
255 	name = g_strdup_printf ( _("New budget #%d"), i );
256     }
257 
258     new_budget_number = gsb_data_budget_get_number_by_name ( name, TRUE, 0 );
259 
260     return new_budget_number;
261 }
262 
263 
264 
265 /**
266  *
267  *
268  */
budgetary_line_add_sub_div(int div_id)269 gint budgetary_line_add_sub_div ( int div_id )
270 {
271     gint new_sub_budget_number;
272     gchar * name;
273     int i = 1;
274 
275     if ( !div_id )
276 	return 0;
277 
278     /** Find a unique name for budget */
279     name =  my_strdup (_("New sub-budget"));
280 
281     while ( gsb_data_budget_get_sub_budget_number_by_name ( div_id, name, FALSE ))
282     {
283 	g_free (name);
284 	i++;
285 	name = g_strdup_printf ( _("New sub-budget #%d"), i );
286     }
287 
288     new_sub_budget_number = gsb_data_budget_get_sub_budget_number_by_name ( div_id, name, TRUE );
289 
290     return new_sub_budget_number;
291 }
292 
293 
294 
295 
296 /**
297  *
298  *
299  *
300  */
budgetary_line_add_transaction_to_div(gint transaction_number,int div_id)301 gboolean budgetary_line_add_transaction_to_div ( gint transaction_number,
302 						 int div_id )
303 {
304     gsb_data_budget_add_transaction_to_budget ( transaction_number,
305 						div_id, 0 );
306     return TRUE;
307 }
308 
309 
310 /**
311  *
312  *
313  *
314  */
budgetary_line_get_metatree_interface(void)315 MetatreeInterface *budgetary_line_get_metatree_interface ( void )
316 {
317     return budgetary_interface;
318 }
319 
320 
321 /* Local Variables: */
322 /* c-basic-offset: 4 */
323 /* End: */
324