-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchkbk_altvars
1197 lines (1049 loc) · 28.6 KB
/
chkbk_altvars
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
# =============================================================================
# Script: chkbk
# By: SMK
# Date: 04/07/05
# Purpose: main menu for chkbk program
#
# License: GPL
# License file LICENSE.txt
# Email: [email protected]
#
# Copyright (C) 2005 Stephen M. Kaiser
#
# 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
#
# =============================================================================
#################### Begin Variables ########################
VERSION="1.5"
HELP="$HOME/chkbk/HELP.txt"
LICENSE="$HOME/chkbk/LICENSE.txt"
S_DEFAULT="$HOME/chkbk/sav.db.bak"
C_DEFAULT="$HOME/chkbk/chk.db.bak"
SAVDB="$HOME/chkbk/sav.db"
CHKDB="$HOME/chkbk/chk.db"
TMPDB="$HOME/chkbk/new.db"
TMPFILE="$HOME/chkbk/tmpfile"
OUTPUTF="$HOME/chkbk/balance.txt"
loop=y
#################### End Variable Section ###################
#################### Begin Functions ########################
####
## Print Main menu to scren
##
####
mainmenu()
{
while test "$loop" = "y"
do
clear
tput cup 4 29 ; echo "Main Menu"
tput cup 5 23 ; echo "===================="
tput cup 6 25 ; echo "(S) Savings"
tput cup 7 25 ; echo "(C) Checking"
tput cup 8 25 ; echo "(B) Backup"
tput cup 9 25 ; echo "(R) Restore"
tput cup 12 25 ; echo "(Q) Quit"
tput cup 15 20 ; echo "Enter choice: "
tput cup 20 17 ; echo "(H) Help (A) About (L) License"
tput cup 21 14 ; echo "(C) Copyleft 2005 by Stephen M. Kaiser"
### Display Welcome to ChkBk message as an animation
tput cup 2 18 ; echo -n "W " ; sleep 0.01 ; echo -n "e " ; sleep 0.01 ; echo -n "l " ; sleep 0.01 ; echo -n "c " ; sleep 0.01 ; echo -n "o " ; sleep 0.01 ; echo -n "m " ; sleep 0.01 ; echo -n "e " ; sleep 0.01 ; echo -n " t " ; sleep 0.01 ; echo -n "o " ; sleep 0.01 ; echo -n " C " ; sleep 0.01 ; echo -n "h " ; sleep 0.01 ; echo -n "k " ; sleep 0.01 ; echo -n "B " ; sleep 0.01 ; echo -n "k "
tput cup 15 34 ; read choice
#### Begin case
case $choice in
[Ss]) DB="$SAVDB" ; printmenus ;; # change variable so it doesn't bother constants
[Cc]) DB="$CHKDB" ; printmenuc ;; # change variable so it doesn't bother constants
[Bb]) backupmenu ;;
[Rr]) restoremenu ;;
[Hh]) less "$HELP" ;;
[Aa]) tput cup 22 22 ; echo ChkBk Version "$VERSION" ; tput cup 23 19 ; echo "Press <ENTER> to continue" ; read prompt ;;
[Ll]) less "$LICENSE" ;;
[Qq]) clear ; exit ;;
*) invaliderror ;;
esac
#### End case
done
#### End while
}
####
## print main chkmenu
##
####
printmenuc()
{
while test "$loop" = "y"
do
clear
tput cup 4 29 ; echo "Checking"
tput cup 5 23 ; echo "===================="
tput cup 6 25 ; echo "(D) Deposit"
tput cup 7 25 ; echo "(W) Withdrawal"
tput cup 8 25 ; echo "(C) Check Balance"
tput cup 9 25 ; echo "(S) Search Database"
tput cup 12 25 ; echo "(Q) Quit"
tput cup 15 20 ; echo "Enter choice: "
tput cup 15 34 ; read choice2
case $choice2 in
[Dd]) menu ;;
[Ww]) menu ;;
[Cc]) if test -e "$DB"
then
balmenu
else
neednewdb
fi ;;
[Ss]) if test -e "$DB"
then
searchmenu
else
neednewdb
fi ;;
[Hh]) less "$HELP" ;;
[Qq]) mainmenu ;;
*) invaliderror ;;
esac
done
}
####
## print main savings menu
##
####
printmenus()
{
while test "$loop" = "y"
do
clear
tput cup 4 29 ; echo "Savings"
tput cup 5 23 ; echo "===================="
tput cup 6 25 ; echo "(D) Deposit"
tput cup 7 25 ; echo "(W) Withdrawal"
tput cup 8 25 ; echo "(C) Check Balance"
tput cup 9 25 ; echo "(S) Search Database"
tput cup 12 25 ; echo "(Q) Quit"
tput cup 15 20 ; echo "Enter choice: "
tput cup 15 34 ; read choice2
case $choice2 in
[Dd]) menu ;;
[Ww]) menu ;;
[Cc]) if test -e "$DB"
then
balmenu
else
neednewdb
fi ;;
[Ss]) if test -e "$DB"
then
searchmenu
else
neednewdb
fi ;;
[Hh]) less "$HELP" ;;
[Qq]) mainmenu ;;
*) invaliderror ;;
esac
done
}
####
## Print Backup menu
##
####
backupmenu()
{
while test "$loop" = "y"
do
clear
tput cup 4 29 ; echo "Backup?"
tput cup 5 23 ; echo "===================="
tput cup 6 25 ; echo "(S) Savings"
tput cup 7 25 ; echo "(C) Checking"
tput cup 12 25; echo "(Q) Quit"
tput cup 15 20; echo "Enter Choice: "
tput cup 15 34; read choice3
case $choice3 in
# change variables so they don't affect the constants
[Ss]) DB="$SAVDB" ; DB_DEF="$S_DEFAULT" ; pathb ;;
[Cc]) DB="$CHKDB" ; DB_DEF="$C_DEFAULT" ; pathb ;;
[Hh]) less "$HELP" ;;
[Qq]) mainmenu ;;
*) invaliderror ;;
esac
done
}
####
## Print restore menu
##
####
restoremenu()
{
while test "$loop" = "y"
do
clear
tput cup 4 29 ; echo "Restore?"
tput cup 5 23 ; echo "===================="
tput cup 6 25 ; echo "(S) Savings"
tput cup 7 25 ; echo "(C) Checking"
tput cup 12 25; echo "(Q) Quit"
tput cup 15 20; echo "Enter Choice: "
tput cup 15 34; read choice3
case $choice3 in
# change variables so they don't affect the constants
[Ss]) DB="$SAVDB" ; DB_DEF="$S_DEFAULT" ; pathr ;;
[Cc]) DB="$CHKDB" ; DB_DEF="$C_DEFAULT" ; pathr ;;
[Hh]) less "$HELP" ;;
[Qq]) mainmenu ;;
*) invaliderror ;;
esac
done
}
####
## print data entry menu
##
####
menu()
{
clear
menuloop="y"
while test "$menuloop" = "y"
do
tput cup 4 29 ; echo "Enter Info"
tput cup 5 23 ; echo "===================="
tput cup 6 25 ; echo "Check# : "
tput cup 7 25 ; echo "Date : (MM/DD/YY)"
tput cup 8 25 ; echo "Comment : "
tput cup 9 25 ; echo "Amount : "
tput cup 10 25 ; echo "Code : "
tput cup 11 25 ; echo "Fee : "
tput cup 6 35 ; read checknum
if test "$checknum" = "q"
then
case $choice in
[Ss]) printmenus ;;
[Cc]) printmenuc ;;
esac
elif test "$checknum" = "h"
then
less "$HELP" ; menu
else
if test "$checknum" = "" # allow blank check no.
then
date
else # verify that check has between 3 and 4 digits
case "$checknum" in
[0-9][0-9][0-9]) menuloop="n" ; checkchk ; clearerror ; date ;;
[0-9][0-9][0-9][0-9]) menuloop="n" ; checkchk ; clearerror ; date ;;
*) tput cup 6 35 ; echo " "
tput cup 20 25 ; echo "BAD check#. Check numbers can range"
tput cup 21 25 ; echo "from 000-9999" ;;
esac
fi
fi
done
}
####
## make sure of no duplicate check no. entries
##
####
checkchk()
{
if test -e "$DB"
then
while cut -d "^" -f 2-7 "$DB" | grep "^$checknum" > "$TMPFILE"
do
tput cup 20 25 ; echo "This number has already been assigned to: "
tput cup 22 1 ; tr '^' ' ' < "$TMPFILE"
echo ; echo "Press ENTER to continue... "
read prompt
menu
done
fi
}
####
## get date and verify format
##
####
date()
{
dateloop="y"
while test "$dateloop" = "y"
do
tput cup 7 35 ; read date
case "$date" in
[1][0-2]/[0][1-9]/[0-9][0-9]) dateloop="n" ; clearerror ; comment;;
[1][0-2]/[1-2][0-9]/[0-9][0-9]) dateloop="n" ; clearerror ; comment;;
[1][0-2]/[3][0-1]/[0-9][0-9]) dateloop="n" ; clearerror ; comment;;
[0][1-9]/[0][1-9]/[0-9][0-9]) dateloop="n" ; clearerror ; comment ;;
[0][1-9]/[1-2][0-9]/[0-9][0-9]) dateloop="n" ; clearerror ; comment ;;
[0][1-9]/[3][0-1]/[0-9][0-9]) dateloop="n" ; clearerror ; comment;;
[-]) while test "$date" = "-"
do
menu
done ;;
[Hh]) less "$HELP"
tput cup 7 35 ; echo " (MM/DD/YY) " ;;
[Qq]) case $choice in
[Ss]) printmenus ;;
[Cc]) printmenuc ;;
esac ;;
*) tput cup 7 35 ; echo " (MM/DD/YY) "
tput cup 21 25 ; echo "BAD date format"
;;
esac
done
}
####
## get comment field and disallow '^', '/', and '|' characters
##
####
comment()
{
commentloop="y"
while test "$commentloop" = "y"
do
tput cup 8 35 ; read comment
case "$comment" in
*['^']*) clearerror ; tput cup 20 25 ; echo "Sorry but the ^ character is not allowed"
tput cup 21 25 ; echo "due to its use in the database"
tput cup 8 35 ; echo " " ;;
*['|']*) clearerror ; tput cup 20 25 ; echo -e "Sorry but the | character is not allowed for security reasons\n"
tput cup 8 35 ; echo " " ;;
*['/']*) clearerror ; tput cup 20 25 ; echo -e "Sorry but the / character is not allowed for security reasons\n"
tput cup 8 35 ; echo " " ;;
[-]) while test "$comment" = "-"
do
tput cup 8 35 ; echo " "
tput cup 7 35 ; echo " " ; clearerror ; date
done ;;
[Hh]) less "$HELP"
tput cup 8 35 ; echo " " ;;
[Qq]) case $choice in
[Ss]) printmenus ;;
[Cc]) printmenuc ;;
esac ;;
*) commentloop="n" ; clearerror ; amount ;;
esac
done
}
####
## get amt of transaction and verify in dollar format
## allow up to $999999.99 dollars to be entered
####
amount()
{
amountloop="y"
while test "$amountloop" = "y"
do
tput cup 9 35 ; read amount
case "$amount" in
# allow user to enter amount without ending zeros and fill the zeros in
[0-9]) amountloop="n" ; type="a" ; addzeroz ; clearerror ; code ;;
[1-9][0-9]) amountloop="n" ; type="a" ; addzeroz ; clearerror ; code ;;
[1-9][0-9][0-9]) amountloop="n" ; type="a" ; addzeroz ; clearerror ; code ;;
[1-9][0-9][0-9][0-9]) amountloop="n" ; type="a" ; addzeroz ; clearerror ; code ;;
[1-9][0-9][0-9][0-9][0-9]) amountloop="n" ; type="a" ; addzeroz ; clearerror ; code ;;
[1-9][0-9][0-9][0-9][0-9][0-9]) amountloop="n" ; type="a" ; addzeroz ; clearerror ; code ;;
# amount entered with one decimal place- auto add one zero to the end
[0-9].[0-9]) amountloop="n" ; type="a1" ; addzeroz ; clearerror ; code ;;
[1-9][0-9].[0-9]) amountloop="n" ; type="a1" ; addzeroz ; clearerror ; code ;;
[1-9][0-9][0-9].[0-9]) amountloop="n" ; type="a1" ; addzeroz ; clearerror ; code ;;
[1-9][0-9][0-9][0-9].[0-9]) amountloop="n" ; type="a1" ; addzeroz ; clearerror ; code ;;
[1-9][0-9][0-9][0-9][0-9].[0-9]) amountloop="n" ; type="a1" ; addzeroz ; clearerror ; code ;;
[1-9][0-9][0-9][0-9][0-9][0-9].[0-9]) amountloop="n" ; type="a1" ; addzeroz ; clearerror ; code ;;
# amount entered with both decimal places
[0-9].[0-9][0-9]) amountloop="n" ; clearerror ; code ;;
[1-9][0-9].[0-9][0-9]) amountloop="n" ; clearerror ; code ;;
[1-9][0-9][0-9].[0-9][0-9]) amountloop="n" ; clearerror ; code ;;
[1-9][0-9][0-9][0-9].[0-9][0-9]) amountloop="n" ; clearerror ; code ;;
[1-9][0-9][0-9][0-9][0-9].[0-9][0-9]) amountloop="n" ; clearerror ; code ;;
[1-9][0-9][0-9][0-9][0-9][0-9].[0-9][0-9]) amountloop="n" ; clearerror ; code ;;
[-]) while test "$amount" = "-"
do
tput cup 9 35 ; echo " "
tput cup 8 35 ; echo " " ; clearerror ; comment
done ;;
[Hh]) less "$HELP"
tput cup 9 35 ; echo " " ;;
[Qq]) case $choice in
[Ss]) printmenus ;;
[Cc]) printmenuc ;;
esac ;;
*) tput cup 9 35 ; echo " "
tput cup 21 25 ; echo "BAD amount. Please enter a dollar amount" ;;
esac
done
}
####
## add zeros to amount or fee if only the first part of the number is entered
## This is intended to ease use by allowing the user to only have to input
## 11 instead of 11.00 for an $11.00 transaction
####
addzeroz()
{
if test "$type" = "a"
then
amount="$amount".00
tput cup 9 35 ; echo "$amount"
elif test "$type" = "a1"
then
amount="$amount"0
tput cup 9 35 ; echo "$amount"
elif test "$type" = "f"
then
fee="$fee".00
tput cup 11 35 ; echo "$fee"
elif test "$type" = "f1"
then
fee="$fee"0
tput cup 11 35 ; echo "$fee"
else
tput cup 21 25 ; echo "INVALID TYPE! TRY AGAIN..."
if test "$fee" = ""
then
clearerror ; amount
else
clearerror ; fee
fi
fi
}
####
## get any single char code from user for the transaction if one exists
##
####
code()
{
codeloop=y
while test "$codeloop" = "y"
do
tput cup 10 35 ; read code
if test "$code" = ""
then
clearerror ; fee
else
case "$code" in
[Qq]) case $choice in
[Ss]) printmenus ;;
[Cc]) printmenuc ;;
esac ;;
[0-9a-zA-Z]) codeloop="n" ; clearerror ; fee ;;
[-]) while test "$code" = "-"
do
tput cup 10 35 ; echo " "
tput cup 9 35 ; echo " " ; clearerror ; amount
done ;;
[Hh]) less "$HELP"
tput cup 10 35 ; echo " " ;;
*) tput cup 10 35 ; echo " "
tput cup 21 25 ; echo "BAD code. Please enter a single letter or number" ;;
esac
fi
done
}
####
## get fee amt of transaction if one exists
## allow up to $999.99 fee-seems a bit excessive,
## but who knows?
####
fee()
{
feeloop=y
while test "$feeloop" = "y"
do
tput cup 11 35 ; read fee
if test "$fee" = ""
then
clearerror ; printvalues
else
case "$fee" in
# allow user to enter amount without ending zeros and fill the zeros in
[0-9]) feeloop="n" ; type="f" ; addzeroz ; clearerror ; printvalues ;;
[1-9][0-9]) feeloop="n" ; type="f" ; addzeroz ; clearerror ; printvalues ;;
[1-9][0-9][0-9]) feeloop="n" ; type="f" ; addzeroz ; clearerror ; printvalues ;;
# allow user to enter amount with only one decimal point filled in
[0-9].[0-9]) feeloop="n" ; type="f1" ; addzeroz ; clearerror ; printvalues ;;
[1-9][0-9].[0-9]) feeloop="n" ; type="f1" ; addzeroz ; clearerror ; printvalues ;;
[1-9][0-9][0-9].[0-9]) feeloop="n" ; type="f1" ; addzeroz ; clearerror ; printvalues ;;
# fee amount entered with decimal places
[0-9].[0-9][0-9]) feeloop="n" ; clearerror ; printvalues ;;
[1-9][0-9].[0-9][0-9]) feeloop="n" ; clearerror ; printvalues;;
[1-9][0-9][0-9].[0-9][0-9]) feeloop="n" ; clearerror ; printvalues ;;
[-]) while test "$fee" = "-"
do
tput cup 11 35 ; echo " "
tput cup 10 35 ; echo " " ; clearerror ; code
done ;;
[Hh]) less "$HELP"
tput cup 11 35 ; echo " " ;;
[Qq]) case $choice in
[Ss]) printmenus ;;
[Cc]) printmenuc ;;
esac ;;
*) tput cup 11 35 ; echo " "
tput cup 21 25 ; echo "BAD fee. Please enter a dollar amount." ;;
esac
fi
done
}
####
## clear any error output from menu screen
##
####
clearerror()
{
tput cup 20 25 ; echo " "
tput cup 21 25 ; echo " "
}
####
## show the user the values he/she entered and ask
## if they are ok...then send to the appropriate
## calc function based on input in step 1 or
## start allover again
####
printvalues()
{
#echo "You entered:"
#echo "check #"$checknum" : date "$date" : desc "$comment" : amount "$amount" : code "$code" : fee "$fee""
#echo "check #: "$checknum""
#echo "date : "$date""
#echo "comment: "$comment""
#echo "amount : "$amount""
#echo "code : "$code""
#echo "fee : "$fee""
tput cup 4 29 ; echo "You Entered:"
tput cup 5 23 ; echo "===================="
tput cup 6 25 ; echo "Check# : "$checknum""
tput cup 7 25 ; echo "Date : "$date""
tput cup 8 25 ; echo "Comment : "$comment""
tput cup 9 25 ; echo "Amount : "$amount""
tput cup 10 25 ; echo "Code : "$code""
tput cup 11 25 ; echo "Fee : "$fee""
echo
echo -en "\nDo you wish to save these values (y/n)? "
read save
if test "$save" = "y"
then
case "$choice2" in
[Dd]) calcdeposit ;;
[Ww]) calcwithdrawal ;;
esac
else
echo -en "\n\nWould you like to start again (y/n)? "
read nloop
if test "$nloop" = "y"
then
menu
else
mainmenu
fi
fi
}
####
## calculate a deposit transaction
##
####
calcdeposit()
{
if test "$fee" = ""
then fee="0.00" # this is just to make output prettier
fi
deposit="$amount"
withdrawal=""
# get total from previous transaction
oldtotal="$(tail -1 "$DB" 2> /dev/null | cut -d '^' -f 9)"
echo ""$oldtotal"^"$deposit"^" > "$TMPDB"
# calc total(without fee) of this new transaction
newtotal="$(tail -1 "$TMPDB" | cut -d '^' -f 1,2 | awk -F '^' '{ print $1+$2 }')"
echo ""$newtotal"^"$fee"" > "$TMPDB"
# calc total including fee
total="$(cut -d '^' -f 1,2 "$TMPDB" | awk -F '^' '{ print $1-$2 }')"
# show transaction with new balance to user
echo
echo "Previous Balance: $"$oldtotal" + Deposited $"$deposit" - Fee: $"$fee" = New Balance: $"$total""
# make db prettier for output later
if test "$code" = ""
then code="n/a"
fi
# write values to database
echo ""$oldtotal"^"$checknum"^"$date"^"$comment"^"$withdrawal"^"$code"^"$fee"^"$deposit"^"$total"" >> "$DB"
echo -en "\n\nWould you like to make another deposit (y/n)?"
read dloop
if test "$dloop" = "y"
then
menu
else
mainmenu
fi
}
####
## calculate a withdrawal transaction
##
####
calcwithdrawal()
{
if test "$fee" = ""
then fee="0.00" # this is just to make output prettier
fi
deposit=""
withdrawal="$amount"
# get total from previous transaction
oldtotal="$(tail -1 "$DB" 2> /dev/null | cut -d '^' -f 9)"
echo ""$oldtotal"^"$withdrawal"^" > "$TMPDB"
# calc total(without fee) of this new transaction
newtotal="$(tail -1 "$TMPDB" | cut -d '^' -f 1,2 | awk -F '^' '{ print $1-$2 }')"
echo ""$newtotal"^"$fee"" > "$TMPDB"
# calc total including fee
total="$(cut -d '^' -f 1,2 "$TMPDB" | awk -F '^' '{ print $1-$2 }')"
# show transaction with new balance to user
echo
echo "Previous Balance: $"$oldtotal" - Withdrawn: $"$withdrawal" - Fee: $"$fee" = New Balance: $"$total""
# make db prettier for output later
if test "$code" = ""
then code="n/a"
fi
# write vals to db
echo ""$oldtotal"^"$checknum"^"$date"^"$comment"^"$withdrawal"^"$code"^"$fee"^"$deposit"^"$total"" >> "$DB"
echo -en "\n\nWould you like to make another withdrawal (y/n)?"
read wloop
if test "$wloop" = "y"
then
menu
else
mainmenu
fi
}
####
## print menu for checking balance
##
####
balmenu()
{
balloop=y
while test "$balloop" = "y"
do
clear
tput cup 4 29 ; echo "Balance"
tput cup 5 23 ; echo "===================="
tput cup 6 25 ; echo "(C) Current Balance"
tput cup 7 25 ; echo "(V) View Last 10 "
tput cup 8 25 ; echo "(D) Entire Database"
tput cup 9 25 ; echo "(M) Manually Enter"
tput cup 12 25 ; echo "(Q) Quit"
tput cup 15 20 ; echo "Enter choice: "
tput cup 15 34 ; read balchoice
case $balchoice in
[Cc]) balloop=n ; curbal ;;
[Vv]) balloop=n ; tail -10 "$DB" > "$TMPFILE" ; showbal ;;
[Dd]) balloop=n ; cat "$DB" > "$TMPFILE" ; showbal ;;
[Mm]) balloop=n ; getnumbal ;;
[Hh]) less "$HELP" ;;
[Qq]) balloop=n ; case $choice in
[Ss]) printmenus ;;
[Cc]) printmenuc ;;
esac ;;
*) invaliderror ;;
esac
done
}
####
## Decides how many lines to print
##
####
getnumbal()
{
bloop=y
while test "$bloop" = "y"
do
echo -n "How many lines would you like to view? "
read numlines
case "$numlines" in
[1-9]) bloop=n ; tail -"$numlines" "$DB" > "$TMPFILE" ; showbal ;;
[1-9][0-9]) bloop=n ; tail -"$numlines" "$DB" >"$TMPFILE" ; showbal ;;
*) echo -e "\n\nInvalid number of lines...pick a number between 1-99\n"
esac
done
}
####
## Displays current balance only
##
####
curbal()
{
echo -e "\n============================================================================================================" > "$TMPDB"
tail -1 "$DB" | cut -d '^' -f 9 | awk -F ' ' '{ printf("\n Current Balance: $ %-10.2f", $1)
print("\n\n============================================================================================================")
}' >> "$TMPDB"
cat "$TMPDB"
askprint
}
####
## Formats output for reading
##
####
showbal()
{
awk ' BEGIN {
printf("\n%-8s %-10s %-34s %10s %10s %6s %8s %11s \n"),"CHECK#","DATE","COMMENTS","DEBIT","DEPOSIT","CODE","FEE","TOTAL"
print("============================================================================================================")
}' > "$TMPDB"
# print every line of file with separator line after it
for line in "$TMPFILE" ; do
awk -F'^' '{ printf("%-8s %-10s %-34s %10.2f %10.2f %6s %8.2f %11.2f \n\n"), $2,$3,$4,$5,$8,$6,$7,$9
print ("-----------------------------------------------------------------------------------------------------------")
}' $line >> "$TMPDB"
done
# print current balance again at bottom
echo -e "\n============================================================================================================" >> "$TMPDB"
tail -1 "$DB" | cut -d '^' -f 9 | awk -F ' ' '{ printf("\n Current Balance: $ %-10.2f", $1)
print("\n\n============================================================================================================")
}' >> "$TMPDB"
cat "$TMPDB"
askprint
}
####
## user search menu
##
####
searchmenu()
{
searchloop="y"
while test "$searchloop" = "y"
do
clear
tput cup 5 25 ; echo "Enter search: ___________________________"
tput cup 6 25 ; echo '(ex. "01/01/99", "check", "Power bill")'
tput cup 5 39 ; read search
if test "$search" = ""
then
case $choice in
[Ss]) printmenus ;;
[Cc]) printmenuc ;;
esac
elif test "$search" = "q"
then
case $choice in
[Ss]) printmenus ;;
[Cc]) printmenuc ;;
esac
elif test "$search" = "Q"
then
case $choice in
[Ss]) printmenus ;;
[Cc]) printmenuc ;;
esac
else
searchdb
fi
done
}
####
## Search the database for a user defined string
##
####
searchdb()
{
grep -i "$search" "$DB" > "$TMPFILE"
read results < "$TMPFILE"
if test "$results" != ""
then
clear ; showbal
else
clear ; tput cup 5 25 ; echo -en "Sorry, but your search returned no results...\n"
pressenter
fi
}
####
## Ask user if they wanna print and to where
##
####
askprint()
{
ploop=y
echo -en "\n\nDo you wanna print? (y/n) " ; read answer
if test "$answer" = "y"
then
while test "$ploop" = "y"
do
echo -e "\n (F) print to file balance.txt\n (P) print to system default printer" ; read answer
case "$answer" in
[Ff]) ploop=n ; printfile ;;
[Pp]) ploop=n ; printpr ;;
*) echo "INVALID IPNUT" ; pressenter ;;
esac
done
else
if test "$searchloop" = "y"
then
echo -en "\nDo you wanna search again? (y/n) "
read searchloop
fi
fi
}
####
## Print output to file "$OUTPUTF"
##
####
printfile()
{
mv -f "$TMPDB" "$OUTPUTF"
}
####
## Print output to printer
##
####
printpr()
{
lpr -o landscape -o cpi=11 "$TMPDB"
# lp -o cpi=16 "$TMPDB" ;;
# mpage -1 -l -I10 balance.txt |lpr
}
####
## ask where to backup db to
##
####
pathb()
{
echo -en "\n\nEnter the path of where to backup to: ["$DB_DEF"] ";
read path;
if test -d "$path"
then
pathisadir
else
checkpath
fi
}
####
## ask where to restore db from
##
####
pathr()
{
echo -en "\n\nEnter the path of the database to restore: ["$DB_DEF"] ";
read path;
if test -d "$path"
then
pathisadir
else
checkpath
fi
}
####
## Print error message if path entered is a directory not a file
##
####
pathisadir()
{
echo ; echo "Sorry, but the path you entered is a directory."
echo "You must specify a filename for the database."
case $choice in
[Bb]) pathb ;;
[Rr]) pathr ;;
esac
}
####
## check to see if path is valid
##
####
checkpath()
{
### needs to test if file exists
if test "$path" = ""
then
if test -e "$DB_DEF"
then
origexist
else
case $choice in
[Bb]) copyfile ;;
[Rr]) nodb ;;
esac
fi
elif test -e "$path"
then # set default path to path entered for ease of admin later
DB_DEF="$path" ; origexist
elif test "$path" = "q"
then
mainmenu
else # if path entered doesn't exist, write it for backup or error for restore
case $choice in
[Bb]) DB_DEF="$path" ; copyfile ;;
[Rr]) nodb ;;
esac
fi
}
####
## if main database(not backed-up db) exists, prompt to overwrite else just go
## to back it up
####