00001 /** $Revision: 1.45 $ 00002 Last updated: $Date: 2002/09/20 12:21:30 $ 00003 00004 Copyright (C) 2000-2002 Vlad Mereuta <dizzy@users.sourceforge.net> 00005 00006 This program is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 2 of the License, or 00009 any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program; if not, write to the Free Software 00018 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 00019 00020 #ifndef __ACCOUNTS_DB__ 00021 #define __ACCOUNTS_DB__ 00022 00023 00024 #include "Database.h" 00025 00026 /// type of the account database 00027 #define ACCOUNTSDB_TYPE 'DATA' 00028 /// db name 00029 #define ACCOUNTSDB_NAME "FreeCoins-Accounts" 00030 00031 ///this structure contains the overall totals for all the selected accounts 00032 typedef struct { 00033 ///current balance total (all transactions regardless of the cleared state) 00034 Int32 total; 00035 ///the total of cleared transactions 00036 Int32 cleared_total; 00037 ///total available (total+overdrafts) 00038 Int32 available_total; 00039 } totalsStruct; 00040 00041 /// sort order type 00042 typedef enum { soName, soAmount, soDate } sortOrder; 00043 00044 /// account flags for tight packing 00045 typedef struct { 00046 /// indicates whether the account is collapsed or not 00047 unsigned collapsed :1; 00048 /// if true, the program will credit money to this account when spending them and subtract from the balance when paying money into it 00049 unsigned credit_account :1; 00050 ///reserved for future fields 00051 unsigned reserved :14; 00052 } AccountFlags; 00053 00054 ///the packed structure of an account 00055 typedef struct { 00056 /// account balance 00057 Int32 balance; 00058 /** total of cleared trasactions (splits) for this account */ 00059 Int32 cleared_balance; 00060 ///the credit limit for creditcards/overdraft for other accounts 00061 Int32 credit_limit; 00062 /// uid of the parent of this account. if the account is a root one this field should be ignored 00063 UInt32 parent_uid; 00064 ///currency used by this account (UID from currency database) 00065 UInt32 currency; 00066 /// overall balance of the children of this account 00067 Int32 children_total; 00068 /// total cleared balance of the children of this account 00069 Int32 children_cleared_total; 00070 /// total available (inc overdraft) in children accounts 00071 Int32 children_available_total; 00072 ///various account flags, packed 00073 AccountFlags flags; 00074 /// account type (Bank, CC, etc.) 00075 UInt8 type; 00076 /// depth at which this account is situated (0 for root accounts, >0 for the others) 00077 UInt8 depth; 00078 /// total number of children owned by this account. this should facilitate things like sorting 00079 UInt8 children_no; 00080 ///all strings associated with the account 00081 Char description[1]; // actually may be longer than 1 00082 } packedAccountStructure; 00083 00084 ///possible account types 00085 enum AccountType { BANK=0, CREDIT=1, INCOME=2, EXPENSE=3, EQUITY=4 }; 00086 00087 ///unpacked structure of an account 00088 typedef struct { 00089 /// account balance 00090 Int32 balance; 00091 /** total of cleared trasactions (splits) for this account */ 00092 Int32 cleared_balance; 00093 ///the credit limit for creditcards/overdraft for other accounts 00094 Int32 credit_limit; 00095 /// uid of the parent of this account. if the account is a root one this field should be ignored 00096 UInt32 parent_uid; 00097 ///currency used by this account (UID from currency database) 00098 UInt32 currency; 00099 /// overall balance of the children of this account 00100 Int32 children_total; 00101 /// total cleared balance of the children of this account 00102 Int32 children_cleared_total; 00103 /// total available (inc overdraft) in children accounts 00104 Int32 children_available_total; 00105 /// account type (Bank, CC, etc.) 00106 AccountType type; 00107 /// depth at which this account is situated (0 for root accounts, >0 for the others) 00108 UInt8 depth; 00109 /// total number of children owned by this account. this should facilitate things like sorting 00110 UInt8 children_no; 00111 /// indicates whether the account is collapsed or not 00112 UInt8 collapsed; 00113 /// if true, the program will credit money to this account when spending them and subtract from the balance when paying money into it 00114 UInt8 credit_account; 00115 ///account desciption 00116 Char* description; 00117 ///the name of the account - this will show up throughout the program 00118 Char* name; 00119 ///optional note attached to the account 00120 Char* note; 00121 } accountStructure; 00122 00123 /** class to manipulate the accounts database. 00124 The category field is used to split the accounts into visible and invisible. 00125 Onely the visible accounts will be displayed by AccountsTable */ 00126 class AccountsDB : public Database <accountStructure, packedAccountStructure> 00127 { 00128 private: 00129 /** creates a new account 00130 @param name account name 00131 @param parent_uid the uid of the parent for the new account 00132 @param acc_type the type of the account */ 00133 UInt32 createAccount(char *name, UInt32 parent_uid, AccountType acc_type); 00134 00135 public: 00136 ///structure containing the totals 00137 totalsStruct totals; 00138 00139 /** default constructor 00140 opens or creates the database (if created populates it with sample records) */ 00141 AccountsDB() DB_SECTION1; 00142 00143 /** constructor 00144 @param db_open_ref reference to an *already* opened database */ 00145 AccountsDB(DmOpenRef db_open_ref) DB_SECTION1; 00146 00147 /** @param n name of the account to be found 00148 @param categ category of the account 00149 @return unique id of the account or 0 if the account could not be found */ 00150 UInt32 findAccountByName(char* n, UInt16 categ) DB_SECTION1; 00151 00152 /** returns a formated account name in buf 00153 @param acc account to get the name for 00154 @param buf an *allocated* buffer in which the name is to be copied! */ 00155 void getAccountName(UInt32 acc, Char* buf) DB_SECTION1; 00156 00157 /** returns the currency used by an account 00158 @param acc UID of the account to get the currency for 00159 @return UID of the currency used by the account */ 00160 UInt32 getAccountCurrency(UInt32 acc) DB_SECTION1; 00161 00162 /** Build the account's full path name, truncating the beginning if 00163 necessary to fit the given number of pixels. 00164 @param acc The account's UID 00165 @param width number of pixels 00166 @param dest allocated buffer to receive the result. I figure that it 00167 should be able to store width/2 characters in the worst case. */ 00168 void getFullNameTruncLeftToFit(UInt32 acc, Int16 width, Char *dest) DB_SECTION1; 00169 00170 /// unpacks an account record 00171 void unpackRecord (accountStructure* account, packedAccountStructure* packedAccount) DB_SECTION1; 00172 00173 /// packs an account record 00174 void packRecord (accountStructure* account, MemHandle accountDBEntry) DB_SECTION1; 00175 00176 /** Increment the account's balances by specified amounts, notifying the 00177 ancestors about the change as well. 00178 This is a 'lowlevel' function that modifies the account balance 00179 directly. 00180 @param uid uid of the account to be affected 00181 @param clr_amt amount to be added to the cleared balance 00182 @param amount amount to be added to the (total) balance 00183 @param is_parent true if the uid is the parent of an account whose 00184 balance has already changed. In this case, only the children totals 00185 of the given account and its ancestors are updated. */ 00186 void incrementBalance(UInt32 uid, Int32 clr_amt, Int32 amount, Boolean is_parent = false) DB_SECTION1; 00187 00188 /** Updates the balance for an account. This is a 'highlevel' function 00189 that affects the balance by inserting a cleared balance adjustment 00190 transaction. 00191 @param uid unique id 00192 @param newbal the new balance 00193 @param payee optional payee description */ 00194 void updateBalance(UInt32 uid,Int32 newbal,char *payee=NULL) DB_SECTION1; 00195 00196 /** fetch the balance of a given account 00197 @param uid the unique id of the account record 00198 @return the balance in pence */ 00199 Int32 getBalance(UInt32 uid) DB_SECTION1; 00200 00201 /** fetch the cleared balance of a given account 00202 @param uid the unique id of the account record 00203 @return the expected balance in pence */ 00204 Int32 getClearedBalance(UInt32 uid) DB_SECTION1; 00205 00206 /** checks if a parent of the current account is collapsed 00207 @acc account id 00208 @return true if no parrent is collapsed, false otherwise */ 00209 Boolean isDisplayable(UInt16 acc) DB_SECTION1; 00210 00211 /** packs and writes the data from s into the database 00212 @note: after calling this function the record has to be released 00213 @param s pointer to an allocated account which is to be saved 00214 @return the index to the record */ 00215 UInt16 newRecord(accountStructure* s) DB_SECTION1; 00216 00217 /** Deletes an account given by its id 00218 The function scans through all the transactions and removes the ones which 00219 have as source/destinations the account to be deleted and as dest/src an account 00220 in the Income or Expense categories. 00221 00222 All the transaction involving user defined account will be updated so that the 00223 reference to the account to be deleted is replaced with a reference to a Income/Expense 00224 account 00225 00226 @param id the id of the account to be deleted */ 00227 bool deleteRecord(UInt16 id) DB_SECTION1; 00228 00229 /** Compares two records (by the account name) */ 00230 static Int16 compareRecords(packedAccountStructure* rec1,packedAccountStructure* rec2,Int16 other,SortRecordInfoPtr sr1,SortRecordInfoPtr 00231 sr2,MemHandle app_info) DB_SECTION1; 00232 00233 /// recategorize accounts as visible/invisible depending on user's choice 00234 void updateVisibility(void); 00235 }; 00236 00237 /** unfortunatelly this is a bit messy :(. We need to be able to access this db from anywhere in the program (including the compareRecords function). 00238 The best solution I managed to come up with is defining the pointer in .c file, and exporting it in .h (tried defining it as static in .h only but 00239 did not work. Next we need to instatntiate the class (this is done in palmcoins.c) and set the pointer to a valid address */ 00240 extern AccountsDB* accounts_db; 00241 00242 #endif