Implemented `dispense acct <name> =`
[tpg/opendispense2.git] / src / cokebank.h
1 /*
2  * OpenDispense 2 
3  * UCC (University [of WA] Computer Club) Electronic Accounting System
4  *
5  * cokebank.h - Coke-Bank common definitions
6  *
7  * This file is licenced under the 3-clause BSD Licence. See the file COPYING
8  * for full details.
9  */
10 /**
11  * \file cokebank.h
12  * \brief Coke Bank API Documentation
13  */
14 #ifndef _COKEBANK_H_
15 #define _COKEBANK_H_
16
17 #include <stdlib.h>
18
19 #define COKEBANK_SALES_ACCT     ">sales"        //!< Sales made into
20 #define COKEBANK_DEBT_ACCT      ">liability"    //!< Credit taken out of
21 #define COKEBANK_FREE_ACCT      ">freeitems"    //!< ODay drink costs taken out of
22
23 /**
24  * \brief Account iterator opaque structure
25  *
26  * Opaque structure for account iterators returned by Bank_Iterator
27  * and used by Bank_IteratorNext and Bank_DelIterator
28  */
29 typedef struct sAcctIterator    tAcctIterator;
30
31 /**
32  * \brief Iterator for a collection of items
33  */
34 typedef struct sItemIterator    tItemIterator;
35
36 /**
37  * \brief Flag values for the \a Flags parameter to Bank_Iterator
38  */
39 enum eBank_ItFlags
40 {
41         BANK_ITFLAG_MINBALANCE  = 0x01, //!< Balance value is Minium Balance
42         BANK_ITFLAG_MAXBALANCE  = 0x02, //!< Balance value is Maximum Balance (higher priority)
43         BANK_ITFLAG_SEENAFTER   = 0x04, //!< Last seen value is lower bound
44         BANK_ITFLAG_SEENBEFORE  = 0x08, //!< Last seen value is upper bound (higher priority)
45         
46         BANK_ITFLAG_SORT_NONE   = 0x000,        //!< No sorting (up to the implementation)
47         BANK_ITFLAG_SORT_NAME   = 0x100,        //!< Sort alphabetically ascending by name
48         BANK_ITFLAG_SORT_BAL    = 0x200,        //!< Sort by balance, ascending
49         BANK_ITFLAG_SORT_UNIXID = 0x300,        //!< Sort by UnixUID (TODO: Needed?)
50         BANK_ITFLAG_SORT_LASTSEEN = 0x400,      //!< Sort by last seen time (ascending)
51         BANK_ITFLAG_SORTMASK    = 0x700,        //!< Sort type mask
52         BANK_ITFLAG_REVSORT     = 0x800 //!< Sort descending instead
53 };
54
55 /**
56  * \brief User flag values
57  *
58  * User flag values used by Bank_GetFlags and Bank_SetFlags
59  */
60 enum eCokebank_Flags {  
61         USER_FLAG_COKE          = 0x01, //!< User is a coke member (can do coke accounting)
62         USER_FLAG_ADMIN         = 0x02, //!< User is a administrator (can create, delete and lock accounts)
63         USER_FLAG_DOORGROUP     = 0x04, //!< User is in the door group (can open the clubroom door)
64         USER_FLAG_INTERNAL      = 0x40, //!< Account is internal (cannot be authenticated, no lower balance limit)
65         USER_FLAG_DISABLED      = 0x80  //!< Account is disabled (no transactions allowed)
66 };
67
68 // --- Cokebank Functions ---
69 /**
70  * \brief Initialise the cokebank
71  * \param Argument      Cokebank argument specified in the dispense config file (typically the database path)
72  * \return Boolean Failure
73  */
74 extern int      Bank_Initialise(const char *Argument);
75
76 /**
77  * \brief Transfer money from one account to another
78  * \param SourceAcct    UID (from \a Bank_GetUserID) to take the money from
79  * \param DestAcct      UID (from \a Bank_GetUserID) give money to
80  * \param Ammount       Amount of money (in cents) to transfer
81  * \param Reason        Reason for the transfer
82  */
83 extern int      Bank_Transfer(int SourceAcct, int DestAcct, int Ammount, const char *Reason);
84 /**
85  * \brief Get flags on an account
86  * \param AcctID        UID to get flags from
87  * \return Flag set as defined in eCokebank_Flags
88  */
89 extern int      Bank_GetFlags(int AcctID);
90 /**
91  * \brief Set an account's flags
92  * \param AcctID        UID to set flags on
93  * \param Mask  Mask of flags changed
94  * \param Value Final value of changed flags
95  */
96 extern int      Bank_SetFlags(int AcctID, int Mask, int Value);
97 /**
98  * \brief Get an account's balance
99  * \param AcctID        Account to query
100  */
101 extern int      Bank_GetBalance(int AcctID);
102 /**
103  * \brief Get the name associated with an account
104  * \return Heap string
105  */
106 extern char     *Bank_GetAcctName(int AcctID);
107 /**
108  * \brief Get an account ID from a passed name
109  * \param Name  Name to search for
110  * \return ID of the account, or -1 if not found
111  */
112 extern int      Bank_GetAcctByName(const char *Name);
113 /**
114  * \brief Create a new account
115  * \param Name  Name for the new account (if NULL, an anoymous account is created)
116  * \return ID of the new account
117  */
118 extern int      Bank_CreateAcct(const char *Name);
119
120 /**
121  * \brief Create an account iterator
122  * \param FlagMask      Mask of account flags to check
123  * \param FlagValues    Wanted values for checked flags
124  * \param Flags Specifies the operation of \a MinMaxBalance and \a Timestamp  (\see eBank_ItFlags)
125  * \param MinMaxBalance Mininum/Maximum balance
126  * \param LastSeen      Latest/Earliest last seen time
127  * \return Pointer to an iterator across the selected data set
128  */
129 extern tAcctIterator    *Bank_Iterator(int FlagMask, int FlagValues,
130         int Flags, int MinMaxBalance, time_t LastSeen);
131
132 /**
133  * \brief Get the current entry in the iterator and move to the next
134  * \param It    Iterator returned by Bank_Iterator
135  * \return Accoun ID, or -1 for end of list
136  */
137 extern int      Bank_IteratorNext(tAcctIterator *It);
138
139 /**
140  * \brief Free an allocated iterator
141  * \param It    Iterator returned by Bank_Iterator
142  */
143 extern void     Bank_DelIterator(tAcctIterator *It);
144
145 /**
146  * \brief Validates a user's authentication
147  * \param Salt  Salt given to the client for hashing the password
148  * \param Username      Username used
149  * \param Password      Password sent by the client
150  * \return User ID
151  */
152 extern int      Bank_GetUserAuth(const char *Salt, const char *Username, const char *Password);
153
154 /**
155  * \brief Get an account ID from a MIFARE card ID
156  * \param CardID        MIFARE card ID
157  * \return Account ID
158  */
159 extern int      Bank_GetAcctByCard(const char *CardID);
160
161 /**
162  * \brief Add a card to an account
163  * \param AcctID        Account ID
164  * \param CardID        MIFARE card ID
165  * \return Boolean failure
166  * \retval 0    Success
167  * \retval 1    Bad account ID
168  * \retval 2    Card in use
169  */
170 extern int      Bank_AddAcctCard(int AcctID, const char *CardID);
171
172 // ---
173 // Server provided helper functions
174 // ---
175 /**
176  * \brief Create a formatted string on the heap
177  * \param Format        Format string
178  * \return Heap string
179  */
180 extern char     *mkstr(const char *Format, ...);
181
182 /**
183  * \brief Dispense log access
184  * \note Try not to over-use, only stuff that matters should go in here
185  */
186 extern void     Log_Info(const char *Format, ...);
187
188 #endif

UCC git Repository :: git.ucc.asn.au