-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.qml
311 lines (286 loc) · 8.9 KB
/
main.qml
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
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12
import QtQuick.Controls.Universal 2.12
import Qt.labs.platform 1.0 as QLP
import "utils.js" as Util
ApplicationWindow {
id: win
visible: true
width: 900
height: 650
minimumHeight: 400
minimumWidth: 600
title: filesEdited?qsTr("Payment File Editor*"):qsTr("Payment File Editor")
property bool appendFlag: false
property real totalPrice: 0
property var currentRaw
property string searchText: ""
property bool filterInvalids: false
property string savePath: ""
property bool anyThingRemovedInSearchMode: false
property bool filterMode: searchText!=="" || filterInvalids
property var openFiles: []
property bool filesEdited: false
onFilterModeChanged: {
if(!filterMode){
removeMarkedElements()
}
}
Component.onCompleted: {
var openedFilePath = fileManager.getOpenedFilePath()
if(openedFilePath!==""){
openFiles.push(openedFilePath)
idelayWork.doit(openFile)
}
}
onClosing: {
if(filesEdited){
close.accepted = false
iareuSureDialog.openAndDo(function(){
Qt.quit()
})
}
}
function removeMarkedElements(){
if(anyThingRemovedInSearchMode){
for(var i = imodel.count - 1 ; i>=0 ;i--){
var element = imodel.get(i)
if(element.removed){
imodel.remove(i)
}
}
anyThingRemovedInSearchMode = false
}
}
function createFile(){
var body = ""
var totalPrice = 0
for(var i=0 ; i< imodel.count;i++){
var element = imodel.get(i)
totalPrice += element.money
body += "\n"+Util.pad(element.acnumber,10)+Util.pad(element.money,15)+Util.pad(0,17)+Util.pad(isidePane.description.text,30," ")
}
var header = ""
header += Util.pad(imodel.count,10)
header+= Util.pad(totalPrice,15)
return header+body
}
function searchElement(){
removeMarkedElements()
ibusyIndicator.running = true
isearchModel.clear()
ilistview.model = isearchModel
searchText = isidePane.searchBox.text
idelayWork.doit(search)
}
function viewInvalids(){
removeMarkedElements()
ibusyIndicator.running = true
isearchModel.clear()
ilistview.model = isearchModel
for(var i=0 ; i< imodel.count;i++){
var element = imodel.get(i)
if(!element.validAccount){
isearchModel.append({"acnumber": element.acnumber,
"money": element.money,
"validAccount": element.validAccount,
"mainindex":i})
}
}
ibusyIndicator.running = false
}
function search(){
for(var i=0 ; i< imodel.count;i++){
var element = imodel.get(i)
if(element.acnumber.indexOf(searchText)!==-1
|| element.money.toString().indexOf(searchText)!==-1){
isearchModel.append({"acnumber": element.acnumber,
"money": element.money,
"validAccount": element.validAccount,
"mainindex":i})
}
}
ibusyIndicator.running = false
}
function openFileDialog(append){
appendFlag = append
iopenFileDialog.open()
}
function openFileAccepted(){
ibusyIndicator.running = true
openFiles = []
for(var i=0;i<iopenFileDialog.files.length;i++){
openFiles.push(iopenFileDialog.files[i].toString().replace("file:///",""))
}
idelayWork.doit(openFile)
}
function openFile(){
if(!appendFlag){
function openFileCloture(){
imodel.clear()
totalPrice = 0
filesEdited = false
fileManager.readFiles(openFiles)
ibusyIndicator.running = false
if(filterMode)searchElement()
}
if(filesEdited){
iareuSureDialog.openAndDo(openFileCloture,function(){
ibusyIndicator.running = false
})
}else{
openFileCloture()
}
}else{
fileManager.readFiles(openFiles)
ibusyIndicator.running = false
if(imodel.count!==0)filesEdited = true
if(filterMode)searchElement()
}
}
function openSaveFileDialog(){
isidePane.goNormalMode()
var date = new Date;
var time = Util.gregorian_to_jalali(date.getFullYear(),date.getMonth()+1,date.getDate())
var y = Util.pad(time[0]%100,2)
var m = Util.pad(time[1],2)
var d = Util.pad(time[2],2)
var name = "FL"+y+m+d
var path = fileManager.openSaveDialog(name);
if(path!==""){
savePath = path
ibusyIndicator.running = true
idelayWork.doit(saveFile)
}
}
function saveFile(){
fileManager.writeToFile(savePath,createFile())
ibusyIndicator.running = false
filesEdited = false
}
function validateACCN(accnumber){
var checksum = accnumber%100;
var number = Math.floor(accnumber/100)
var sum = 0
var numberstr = number+""
if(numberstr.length >10)
return false
while(numberstr.length<8){
numberstr = '0'+numberstr
}
for(var i=0;i<numberstr.length;i++){
var n = numberstr.charAt(i);
sum+= n*(i%2===0?3:7)
}
var add = 2 + Math.floor((sum+4)/97);
var res = (sum+add*2)%99;
return res === checksum;
}
DelayWork{
id: idelayWork
}
Connections{
target: fileManager
onNewElement:{
totalPrice+=money
imodel.append({"acnumber": acNumber,
"money": money,
"validAccount":validateACCN(acNumber)
})
}
onReadError:{
ierrorDialog.openWithModel(readErrors)
}
}
FontLoader{
id: ifontAwsome
source: "qrc:/res/Font Awesome 5 Free-Solid-900.otf"
}
QLP.FileDialog{
id: iopenFileDialog
fileMode: QLP.FileDialog.OpenFiles
nameFilters: ["Payment Files(*.pay)"]
folder: QLP.StandardPaths.writableLocation(QLP.StandardPaths.DocumentsLocation)
onAccepted: {
openFileAccepted()
}
}
RowLayout{
anchors.fill: parent
SidePane{
id: isidePane
}
ToolSeparator{
Layout.fillHeight: true
leftInset: 0
leftPadding: 0
}
AccountsListView{
id: ilistview
BusyIndicator{
id: ibusyIndicator
width: 60
height: width
anchors.centerIn: parent
running: false
}
}
}
ListModel {
id: imodel
}
ListModel{
id: isearchModel
}
AboutDialog{
id: iaboutDialog
}
AreUSureDialog{
id: iareuSureDialog
}
ErrorDialog{
id: ierrorDialog
}
ColumnLayout{
anchors.fill: parent
spacing: 0
FileDropArea{
id: idropOpen
text: "Release to open"
Layout.fillWidth: true
Layout.fillHeight: true
visibleArea: idropOpen.containsDrag || idropAppend.containsDrag
property string name: "value"
onDroped: {
var openedFilesPath = drop.text.split("\n");
appendFlag = false
openFiles = []
for(var i = 0;i<openedFilesPath.length;i++){
(openedFilesPath[i].indexOf(".pay")!==-1)
openFiles.push(openedFilesPath[i].replace("file:///",""))
}
idelayWork.doit(openFile)
}
}
FileDropArea{
id: idropAppend
text: "Release to append"
Layout.fillWidth: true
Layout.fillHeight: true
visible: imodel.count > 0
visibleArea: idropOpen.containsDrag || idropAppend.containsDrag
onDroped: {
var openedFilesPath = drop.text.split("\n");
appendFlag = true
openFiles = []
for(var i = 0;i<openedFilesPath.length;i++){
(openedFilesPath[i].indexOf(".pay")!==-1)
openFiles.push(openedFilesPath[i].replace("file:///",""))
}
idelayWork.doit(openFile)
}
}
}
}