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
|
/* ************************************************************************** */
/* */
/* */
/* Copyright (C) 2000-2008 Cédric Auger (cedric@grisbi.org) */
/* 2003-2008 Benjamin Drieu (bdrieu@april.org) */
/* 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_data_archive.c
* work with the archive store structure, no GUI here
* the archive store are used to show the archive in the list of transactions
* as an archive by itself contains several accounts and no balance for each account
* at the opening of grisbi, we create an intermediate list of structures wich contains
* the link to the archive, but 1 structure per account, with the number of transactions
* and the balance for each.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "include.h"
/*START_INCLUDE*/
#include "gsb_data_archive_store.h"
#include "dialog.h"
#include "gsb_data_account.h"
#include "gsb_data_currency.h"
#include "gsb_data_transaction.h"
#include "gsb_real.h"
#include "transaction_list.h"
/*END_INCLUDE*/
/**
* \struct
* Describe an archive store
*/
typedef struct
{
gint archive_store_number;
/* the corresponding archive (1 archive contains several archive store) */
gint archive_number;
/* account we are working on */
gint account_number;
/* balance of all the transactions of the archive for that account */
gsb_real balance;
/* number of transactions in the archive for that account */
gint nb_transactions;
} struct_store_archive;
/*START_STATIC*/
static void _gsb_data_archive_store_free ( struct_store_archive *archive );
static struct_store_archive *gsb_data_archive_store_find_struct ( gint archive_number,
gint account_number );
static gint gsb_data_archive_store_max_number ( void );
static gint gsb_data_archive_store_new ( void );
/*END_STATIC*/
/*START_EXTERN*/
extern gsb_real null_real;
/*END_EXTERN*/
/** contains the g_slist of struct_store_archive */
static GSList *archive_store_list = NULL;
/** a pointer to the last archive_store used (to increase the speed) */
static struct_store_archive *archive_store_buffer;
/**
* set the archives global variables to NULL,
* usually when we init all the global variables
*
* \param
*
* \return FALSE
* */
gboolean gsb_data_archive_store_init_variables ( void )
{
if ( archive_store_list )
{
GSList* tmp_list = archive_store_list;
while ( tmp_list )
{
struct_store_archive *archive;
gint archive_number;
archive = tmp_list -> data;
archive_number = gsb_data_archive_store_get_archive_number (
archive -> archive_store_number );
transaction_list_remove_archive ( archive_number );
tmp_list = tmp_list -> next;
_gsb_data_archive_store_free ( archive );
}
g_slist_free ( archive_store_list );
}
archive_store_list = NULL;
archive_store_buffer = NULL;
return FALSE;
}
/**
* give the g_slist of archives structure
* usefull when want to check all archives
*
* \param none
*
* \return the g_slist of archives structure
* */
GSList *gsb_data_archive_store_get_archives_list ( void )
{
return archive_store_list;
}
/**
* return the number of the archives given in param
*
* \param archive_ptr a pointer to the struct of the archive
*
* \return the number of the archive, 0 if problem
* */
gint gsb_data_archive_store_get_number ( gpointer archive_ptr )
{
struct_store_archive *archive;
if ( !archive_ptr )
return 0;
archive = archive_ptr;
archive_store_buffer = archive;
return archive -> archive_store_number;
}
/**
* function called at the opening of grisbi
* create all the archive store according to the archives in grisbi
*
* \param
*
* \return
* */
void gsb_data_archive_store_create_list ( void )
{
GSList *tmp_list;
tmp_list = gsb_data_transaction_get_complete_transactions_list ();
while (tmp_list)
{
gint transaction_number;
gint archive_number;
transaction_number = gsb_data_transaction_get_transaction_number (tmp_list -> data);
archive_number = gsb_data_transaction_get_archive_number (transaction_number);
if (archive_number)
{
struct_store_archive *archive_store;
gint floating_point;
gint account_number;
account_number = gsb_data_transaction_get_account_number (transaction_number);
floating_point = gsb_data_currency_get_floating_point (
gsb_data_account_get_currency (account_number) );
archive_store = gsb_data_archive_store_find_struct ( archive_number, account_number);
if (archive_store)
{
/* there is already a struct_store_archive for the same archive and the same account,
* we increase the balance except for operations */
if ( !gsb_data_transaction_get_mother_transaction_number ( transaction_number ) )
archive_store -> balance = gsb_real_add ( archive_store -> balance,
gsb_data_transaction_get_adjusted_amount (
transaction_number,
floating_point));
archive_store -> nb_transactions++;
}
else
{
/* there is no struct_store_archive for that transaction, we make a new one
* with the balance of the transaction as balance */
gint archive_store_number;
archive_store_number = gsb_data_archive_store_new ();
archive_store = gsb_data_archive_store_get_structure (archive_store_number);
archive_store -> archive_number = archive_number;
archive_store -> account_number = account_number;
archive_store -> balance = gsb_data_transaction_get_adjusted_amount (
transaction_number, floating_point);
if ( ! gsb_data_transaction_get_mother_transaction_number ( transaction_number ) )
archive_store -> nb_transactions = 1;
}
}
tmp_list = tmp_list -> next;
}
}
/**
* This internal function is called to free the memory used by a struct_store_archive structure
*/
static void _gsb_data_archive_store_free ( struct_store_archive *archive )
{
if ( ! archive )
return;
g_free ( archive );
if ( archive_store_buffer == archive )
archive_store_buffer = NULL;
}
/**
* remove an archive store
*
* \param archive_store_number the archive store we want to remove
*
* \return TRUE ok
* */
gboolean gsb_data_archive_store_remove ( gint archive_store_number )
{
struct_store_archive *archive;
archive = gsb_data_archive_store_get_structure ( archive_store_number );
if (!archive)
return FALSE;
archive_store_list = g_slist_remove ( archive_store_list,
archive );
_gsb_data_archive_store_free ( archive );
return TRUE;
}
/**
* remove all the archives stores corresponding to the archive
*
* \param archive_number the archive we want remove the corresponding archives stores
*
* \return TRUE ok
* */
gboolean gsb_data_archive_store_remove_by_archive ( gint archive_number )
{
GSList *tmp_list;
tmp_list = archive_store_list;
while (tmp_list)
{
struct_store_archive *archive;
archive = tmp_list -> data;
tmp_list = tmp_list -> next;
if (archive -> archive_number == archive_number)
{
archive_store_list = g_slist_remove ( archive_store_list,
archive );
_gsb_data_archive_store_free ( archive );
}
}
return TRUE;
}
/**
* return the number of the corresponding archive
*
* \param archive_store_number the number of the archive store
*
* \return the of the archive or 0 if fail
* */
gint gsb_data_archive_store_get_archive_number ( gint archive_store_number )
{
struct_store_archive *archive;
archive = gsb_data_archive_store_get_structure ( archive_store_number );
if (!archive)
return 0;
return archive -> archive_number;
}
/**
* return the account_number of the archive store
*
* \param archive_store_number the number of the archive store
*
* \return the of the archive or 0 if fail
* */
gint gsb_data_archive_store_get_account_number ( gint archive_store_number )
{
struct_store_archive *archive;
archive = gsb_data_archive_store_get_structure ( archive_store_number );
if (!archive)
return 0;
return archive -> account_number;
}
/**
* return the balance of the archive store
* ie the balance of the archive for the account in the archive store
*
* \param archive_store_number the number of the archive store
*
* \return the of the archive or 0 if fail
* */
gsb_real gsb_data_archive_store_get_balance ( gint archive_store_number )
{
struct_store_archive *archive;
archive = gsb_data_archive_store_get_structure ( archive_store_number );
if (!archive)
return null_real;
return archive -> balance;
}
/**
* return the number of transactions of the archive store
*
* \param archive_store_number the number of the archive store
*
* \return the of the archive or 0 if fail
* */
gint gsb_data_archive_store_get_transactions_number ( gint archive_store_number )
{
struct_store_archive *archive;
archive = gsb_data_archive_store_get_structure ( archive_store_number );
if (!archive)
return 0;
return archive -> nb_transactions;
}
/**
* find and return the structure of the archive asked
*
* \param archive_store_number number of archive
*
* \return the adr of the struct of the archive (NULL if doesn't exit)
* */
gpointer gsb_data_archive_store_get_structure ( gint archive_store_number )
{
GSList *tmp;
if (!archive_store_number)
return NULL;
/* before checking all the archives, we check the buffer */
if ( archive_store_buffer
&&
archive_store_buffer -> archive_store_number == archive_store_number )
return archive_store_buffer;
tmp = archive_store_list;
while ( tmp )
{
struct_store_archive *archive;
archive = tmp -> data;
if ( archive -> archive_store_number == archive_store_number )
{
archive_store_buffer = archive;
return archive;
}
tmp = tmp -> next;
}
return NULL;
}
/**
* find and return the last number of archive
*
* \param none
*
* \return last number of archive
* */
static gint gsb_data_archive_store_max_number ( void )
{
GSList *tmp;
gint number_tmp = 0;
tmp = archive_store_list;
while ( tmp )
{
struct_store_archive *archive;
archive = tmp -> data;
if ( archive -> archive_store_number > number_tmp )
number_tmp = archive -> archive_store_number;
tmp = tmp -> next;
}
return number_tmp;
}
/**
* find the archive store corresponding to the archive number and account number
* given in param
*
* \param archive_number the wanted archive number
* \param account_number the wanted account number
*
* \return a pointer to the found struct_store_archive or NULL
* */
static struct_store_archive *gsb_data_archive_store_find_struct ( gint archive_number,
gint account_number )
{
GSList *tmp_list;
tmp_list = archive_store_list;
while (tmp_list)
{
struct_store_archive *archive;
archive = tmp_list -> data;
if (archive -> archive_number == archive_number
&&
archive -> account_number == account_number )
return archive;
tmp_list = tmp_list -> next;
}
return NULL;
}
/**
* create a new archive store, give it a number, append it to the list
* and return the number
*
* \param
*
* \return the number of the new archive store
* */
static gint gsb_data_archive_store_new ( void )
{
struct_store_archive *archive;
archive = g_malloc0 ( sizeof ( struct_store_archive ));
if (!archive)
{
dialogue_error_memory ();
return 0;
}
archive -> archive_store_number = gsb_data_archive_store_max_number () + 1;
archive_store_list = g_slist_append ( archive_store_list, archive );
archive_store_buffer = archive;
return archive -> archive_store_number;
}
gsb_real gsb_data_archive_store_get_archives_balance ( gint account_number )
{
GSList *tmp_list;
gsb_real balance = null_real;
tmp_list = gsb_data_archive_store_get_archives_list ( );
while (tmp_list)
{
struct_store_archive *archive_store;
archive_store = tmp_list -> data;
if ( archive_store -> account_number == account_number )
balance = gsb_real_add ( balance, archive_store -> balance );
tmp_list = tmp_list -> next;
}
return balance;
}
/* Local Variables: */
/* c-basic-offset: 4 */
/* End: */
|