-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckbook.js
More file actions
116 lines (109 loc) · 3.35 KB
/
checkbook.js
File metadata and controls
116 lines (109 loc) · 3.35 KB
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
// accounts table:
//
// id
// name
// balance
// type
// notes
function Checkbook(dbName, success, failure) {
dbName = (dbName) ? dbName : "Cheque";
var self = this;
this.accounts = new ChequeHash();
this.storage = new Storage(dbName);
this.storage.createTable("accounts", {name: "string", balance: "number", type: "string", notes: "text"}, function() {
self.storage.read("accounts", null, {order: "name"}, function(rows) {
for(var i = 0, j = rows.length; i < j; i++) {
var data = rows[i];
data.checkbook = self;
var acct = new Account(data);
self.accounts.set(acct.name, acct);
}
if(success)
success(self);
}, function() {
if(failure)
failure();
});
var s = self.storage;
s.createTable("categories", {name: "string", type: "string"}, function() {
s.count("categories", null, function(rowCount) {
if(rowCount === 0) {
var defaultCategories = [
{name: "Card Swiped", type: "debit"},
{name: "Check", type: "debit"},
{name: "E-Purchase", type: "debit"},
{name: "Withdrawal", type: "debit"},
{name: "Deposit", type: "credit"},
{name: "Refund", type: "credit"},
{name: "Correction - Debit", type: "debit"},
{name: "Correction - Credit", type: "credit"},
{name: "Transfer", type: ""}
];
for(var i = 0, j = defaultCategories.length; i < j; i++) {
var category = defaultCategories[i];
s.write("categories", category);
}
}
});
});
});
}
Checkbook.prototype = {
accountsByName: function() {
return this.accounts.getValues().sort(function(a, b) {
if(a.name > b.name)
return 1;
else if(a.name < b.name)
return -1;
return 0;
});
},
addOrAccessAccount: function(options, callback) {
var self = this;
var name = options.name;
if(this.accounts.has(name)) {
if(callback) {
var acct = this.getAccount(name);
callback(acct);
}
}
else {
this.storage.count("accounts", {name: options.name}, function(rowCount) {
if(rowCount === 0) {
self.storage.write("accounts", options, function(insertId) {
options.checkbook = self;
options.id = insertId;
var acct = new Account(options);
self.accounts.set(name, acct);
acct.save(callback);
});
}
});
}
},
removeAccount: function(name, success) {
// wipe out all entries for this account number first
var acct = this.getAccount(name);
this.storage.erase("entries", {account_id: acct.id});
this.storage.erase("accounts", {id: acct.id}, success);
this.accounts.remove(name);
},
removeAccountById: function(id, success) {
// wipe out all entries for this account number first
var acct = this.getAccountById(id);
this.storage.erase("entries", {account_id: id});
this.storage.erase("accounts", {id: id}, success);
this.accounts.remove(acct.name);
},
getAccount: function(name) {
return this.accounts.get(name);
},
getAccountById: function(id) {
var foundAccount = null;
this.accounts.each(function(acct) {
if(acct.id === id)
foundAccount = acct;
});
return foundAccount;
}
};