-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
302 lines (249 loc) · 9.21 KB
/
model.py
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
#!/usr/bin/python
# -*- coding: windows-1251 -*-
import os, os.path, fdb, codecs
def Float(s):
return float(s.replace(",", "."))
class NameWithID():
def __init__(self):
self.ID = 0
self.Name = u"<Íå âûáðàíî>"
def getID(self):
return self.ID
def getName(self):
return self.Name
def setAll(self, raw):
self.ID = raw[0]
self.Name = raw[1]
class PartFolder(NameWithID):
def __init__(self):
NameWithID.__init__(self)
self.Name = u"<Íå âûáðàíà>"
def setFolder(self, raw):
self.setAll(raw)
class Postav(NameWithID):
def __init__(self):
NameWithID.__init__(self)
self.Name = u"<Íå âûáðàí>"
def setPostav(self, raw):
self.setAll(raw)
class Store(NameWithID):
def __init__(self):
NameWithID.__init__(self)
self.Name = u"<Íå âûáðàí>"
def setStore(self, raw):
self.setAll(raw)
class appData():
def __init__(self):
self.__pathDB = os.path.abspath(os.path.join(os.getcwd(), "..", "Base", "AEnter.gdb"))
self.__pathCSV = ""
self.__Postav = Postav()
self.__Sklad = Store()
self.__PartFolder = PartFolder()
self.__Author = None
self.weID = 1
self.DocType = 1
self.__db = fdb.connect(dsn="localhost:"+self.__pathDB, user='SYSDBA', password='masterkey', charset='WIN1251')
def getPathDB(self):
return self.__pathDB
def getPathCSV(self):
return self.__pathCSV
def setPathCSV(self, path):
self.__pathCSV = path
def getPostav(self):
return self.__Postav
def setPostav(self, postData):
self.__Postav.setPostav(postData)
def getStore(self):
return self.__Sklad
def setStore(self, data):
self.__Sklad.setStore(data)
def getPartFolder(self):
return self.__PartFolder
def setPartFolder(self, pfData):
self.__PartFolder.setFolder(pfData)
def getAuthor(self):
return self.__Author
def setSklad(self, authorID):
self.__Author = authorID
def getAllPartFolders(self):
cur = self.__db.cursor()
cur.execute("select id,name,parent from parttree order by treelevel, parent, codegroup")
result = cur.fetchall()
return result
def getAllPostav(self):
cur = self.__db.cursor()
cur.execute("select id, nameshort, 0 from contragent") # TODO äîáàâèòü ôèëüòð òîëüêî íà ïîñòàâùèêîâ
result = cur.fetchall()
return result
def executeSQL(self, SQL):
cur = self.__db.cursor()
cur.execute(SQL)
return cur.fetchall()
def insertSQL(self, tableName, entry):
questions = "?, " * len(entry)
keys = ", ".join(entry.keys())
values = [tuple(entry.values()),]
SQL = "insert into "+tableName+" ("
SQL += keys
SQL += ") values ("+questions[:-2]+")"
cur = self.__db.cursor()
cur.executemany(SQL, values)
self.__db.commit()
def getNextID(self, sequence):
SQL = "select next value for "+sequence+" from RDB$DATABASE"
return self.executeSQL(SQL)[0][0]
def getNextNum(self, table):
SQL = "select max(number) from "+table+" where iddoctype=1"
return self.executeSQL(SQL)[0][0] + 1
def getPriceCoeffitients(self):
SQL = "select coeffr, coeffmo, coeffo, coeffs from options where idcontragent = "+str(self.weID)
result = self.executeSQL(SQL)
return result[0]
def createPart(self, rawEntry):
coeffr, coeffmo, coeffo, coeffs = self.getPriceCoeffitients()
entry = {
"ID": self.getNextID("PART_ID"),
"IDPARTTREE": self.getPartFolder().getID(),
"NAME": rawEntry[4],
"NAMESHORT": rawEntry[4][:40],
"FIRM": self.getPostav().getID(),
"NUMBER": rawEntry[1],
"COEFFR": coeffr,
"COEFFMO": coeffmo,
"COEFFO": coeffo,
"COEFFS": coeffs,
"PRICER": Float(rawEntry[3]) * coeffr,
"PRICEMO": Float(rawEntry[3]) * coeffmo,
"PRICEO": Float(rawEntry[3]) * coeffo,
"PRICES": Float(rawEntry[3]) * coeffs,
# TODO MAGIC VALUES
"IDCOUNTRY": 3,
"UNITDIMENSION": 1,
"SETHANDS": "F",
"NDS": 0,
"NFS": 0,
"INDEXNAME": "-",
"WEIGTH": 0,
"VOLUME": 0,
"MATERIAL": 2,
"WORKPERIOD": 0,
"QUANTITYPERCAR": 0,
"AMOUNTINPACKAGE": 0,
"KINDOFPACKING": 1,
"AMOUNTINCONTAINER": 0,
"KINDOFCONTAINER": 5,
"MINSTOCK": 0,
"MAXSTOCK": 0,
"INSURANCESTOCK": 0,
"DELIVERYDATE": 0,
"MINQUANTITYORDE": 0,
"OPTIMUMQUANTITYORDE": 0,
"BACKLOGDEMAND": 0,
"GROUPOFDEMAND": -1,
"BARCODETYPE": -1,
"BARCODE": 0,
"CURRENCY": "ÐÓÁ",
"UNIT": "øò.",
#"CODEUIN": 35000021,
#"CODEGROUP": 21,
"IDMARK": 0,
"PRICESOURCE": 0,
}
self.insertSQL("part", entry)
return entry["ID"]
def getPartIDFromArticul(self, rawEntry):
articul = rawEntry[1]
#print(articul)
result = self.executeSQL("select id from part where NUMBER='"+articul+"'")
if result == []:
result = self.createPart(rawEntry)
else:
result = result[0][0]
return result
def getEntryForDocLine(self, rawEntry):
result = {
"ID": self.getNextID("DOCPARTS_ID"),
"IDCONTRAGENT": self.weID,
"IDPART": self.getPartIDFromArticul(rawEntry),
"IDDOCHEAD": self.docheader_ID,
"SUMMCURRENCY": Float(rawEntry[3]),
"COUNTS": Float(rawEntry[2]),
"TYPECURRENCY": 1,
"CURRENCYSHORT": "ÐÓÁ", # TODO MAGIC VALUES
"DIRECTION": 0,
"SDISCOUNT": 0,
"SNDS": 0,
"SNFS": 0,
"SKLAD": "A", # TODO MAGIC VALUE
"INDEXNAME": "A01", # TODO MAGIC VALUE
}
return result
def read_file(self):
f = codecs.open(self.__pathCSV, 'r', 'cp1251')
lines = f.readlines()
f.close()
self.Number = self.getNextNum("docnumber")
self.contragentmoney_ID = self.getNextID("CONTRAGENTMONEY_ID")
self.direxchange_ID = self.getNextID("DIREXCHANGE_ID")
self.docheader_ID = self.getNextID("DOCHEADER_ID")
self.docnumber_ID = self.getNextID("DOCNUMBER_ID")
self.doclines = []
totalSum = float(0)
for line in lines[1:]:
rawEntry = line.split(";")
self.doclines.append(self.getEntryForDocLine(rawEntry))
value = Float(rawEntry[2])
price = Float(rawEntry[3])
totalSum += value * price
self.money = totalSum
def update_contragentmoney(self):
entry = {
"ID": self.contragentmoney_ID,
"IDCONTRAGENTIN": self.weID,
"IDCONTRAGENTOUT": self.getPostav().getID(),
"IDCONTRAGENT": self.getPostav().getID(),
"NAMECONTRAGENT": self.getPostav().getName(),
"IDDOC": self.docheader_ID,
"MONEY": self.money,
"DIRECTION": 1,
"TYPEDOC": -1,
}
self.insertSQL("contragentmoney", entry)
def update_docheader(self):
entry = {
"ID": self.docheader_ID,
"IDCONTRAGENTOUT": self.getPostav().getID(),
"IDCONTRAGENTIN": self.weID,
"NUMBER": self.Number,
"IDDOCTYPE": self.DocType,
"SUMM": self.money,
"BALANCE": self.money,
"SUMMPART": self.money,
"DIRECTION": 1, # TODO MAGIC VALUE
"IDFOULDER": 3, # TODO MAGIC VALUE
"STATUSDOC": "F",
}
self.insertSQL("docheader", entry)
def update_docnumber(self):
entry = {
"ID": self.docnumber_ID,
"IDOPTIONS": 1,
"NUMBER": self.Number,
"IDDOCTYPE": 1,
}
self.insertSQL("docnumber", entry)
def update_docparts(self):
for docline in self.doclines:
self.insertSQL("docparts", docline)
def AllOk(self):
return True # TODO
def LoadIncome(self):
#pass
self.read_file()
self.update_contragentmoney()
self.update_docheader()
self.update_docnumber()
self.update_docparts()
return self.Number
def LoadOutcome(self):
print("TODO")