-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinventory.red
195 lines (147 loc) · 4.27 KB
/
inventory.red
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
Red [
Title: "inventory.red"
]
do read http://redlang.red/crud-csv.red
CONFIG-DIR: %config
if not exists? config-file: %config/inventory.config.red [
make-dir CONFIG-DIR
write config-file read http://miniapps.red/config/inventory.config.red
print rejoin ["Created " clean-path config-file]
]
do read config-file
devices: select config 'devices
data-file: select devices '.path
if not exists? data-file [
make-dir %db
write data-file read http://miniapps.red/db/inventory.csv
print rejoin ["Created " clean-path data-file]
]
lines: Read/lines data-file
csv-header: first lines
lines: skip lines 1
records: copy []
forall lines [
append/only records split lines/1 ","
]
n: (to-string length? records)
;using red reactive programming
;reactor allows to define the current record
;"is" is called reactive relation.
record: make reactor! [
row: 1
Entry-Date: is [ records/(row)/1]
End-Garantee: is [ records/(row)/2]
type: is [ records/(row)/3]
description: is [ records/(row)/4]
Quantity: is [ records/(row)/5]
Price: is [ records/(row)/6]
Reference: is [ records/(row)/7]
Note: is [ records/(row)/8]
]
header: [
panel [
;text "Description:" field 250x24 data records/1/3
;data-binding using react keyword
text "Description:" description: field 250x24 react [face/data: record/description]
return
text "Type:" type: field 250x24 react [face/data: record/type]
return
]
panel [
text "Entry Date:" entry-date: field react [face/data: record/entry-date]
return
text "End Garantee:" end-garantee: field react [face/data: record/end-garantee]
]
]
body-left: [
text "Quantity / Price"
return
text "quantity:" quantity: field right react [face/data: record/quantity]
return
text "price:" price: field right react [face/data: record/price]
return
]
body-right: [
text "Complements"
return
text "Reference:" reference: field 250x24 react [face/data: record/reference]
return
text "Note:" note: field 250x24 react [face/data: record/note]
return
]
footer: [
across middle
button "first" [
record/row: 1
]
button "previous" [
either (record/row = 1) [
record/row: length? records
][
record/row: record/row - 1
]
]
field-record-number: text 10x15 react [face/data: record/row]
button "next" [
either (record/row < length? records) [
record/row: record/row + 1
][
record/row: 1
]
]
button "last" [
record/row: length? records
]
button "Add" [
append/only records [
{} {} {} {}
{} {} {} {}
]
record/row: length? records
]
button "save" [
record/entry-date: entry-date/text
record/type: type/text
record/End-Garantee: End-Garantee/text
record/description: description/text
record/quantity: quantity/text
record/price: price/text
record/reference: reference/text
record/note: note/text
block-record: copy []
append block-record record/Entry-Date
append block-record record/End-Garantee
append block-record record/type
append block-record record/description
append block-record record/quantity
append block-record record/price
append block-record record/reference
append block-record record/note
record-number: to-integer field-record-number/data
records: update-csv records record-number block-record
lines: copy []
forall records [
line-block: records/1
line: copy ""
forall line-block [
element: line-block/1
line: rejoin [line "," element]
]
remove line
append lines line
]
save-csv/header lines %db/inventory.csv csv-header
print "Record saved"
]
]
Title: "Inventory"
win: layout compose [
title (Title)
panel header
return
panel body-left
panel body-right
return
panel footer
]
view win