75 lines
3.4 KiB
Python
75 lines
3.4 KiB
Python
|
#!/usr/bin/python3
|
|||
|
# -*- coding: utf-8 -*-
|
|||
|
import os, sqlite3, sys
|
|||
|
|
|||
|
class bdToExсel:
|
|||
|
def __init__(self, bd_name):
|
|||
|
#Подключаемся к БД, создаем таблицу
|
|||
|
bdToExсel.conn = sqlite3.connect(bd_name)
|
|||
|
bdToExсel.cursor = bdToExсel.conn.cursor()
|
|||
|
sql='CREATE TABLE IF NOT EXISTS `tovar` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `tovar_name` TEXT, `barcode` TEXT, `id_tovar` INTEGER, `cena` TEXT, `kolvo` INTEGER, `artikul` TEXT, `user_code` INTEGER)'
|
|||
|
bdToExсel.cursor.execute(sql)
|
|||
|
bdToExсel.conn.commit()
|
|||
|
|
|||
|
def tovarToTovar(self, user_code):
|
|||
|
sql='SELECT `ID`, `NAME`, `MARK` FROM `WARE` WHERE `NAME`<>""'
|
|||
|
bdToExсel.cursor.execute(sql)
|
|||
|
wares=bdToExсel.cursor.fetchall()
|
|||
|
for row in wares:
|
|||
|
id = str(row[0])
|
|||
|
tovar_name = row[1].replace('"', '""')
|
|||
|
artikul = str(row[2])
|
|||
|
artikul = artikul.replace('"', '""')
|
|||
|
info=self.getInfo(id)
|
|||
|
try:
|
|||
|
cena=str(info[0])
|
|||
|
cena=cena.replace('0000', '')
|
|||
|
except:
|
|||
|
# print("Нет цены")
|
|||
|
cena=""
|
|||
|
try:
|
|||
|
kolvo=str(info[1])
|
|||
|
kolvo=kolvo.replace('000000', '')
|
|||
|
except:
|
|||
|
# print("Нет количества")
|
|||
|
kolvo="0"
|
|||
|
try:
|
|||
|
barcode=str(info[2])
|
|||
|
except:
|
|||
|
# print("Нет штрих кода")
|
|||
|
barcode=""
|
|||
|
sql='SELECT COUNT(*) FROM `tovar` WHERE `tovar_name`="' + tovar_name + '" AND `barcode`= "' + barcode + '" AND `user_code`="' + user_code + '" LIMIT 1'
|
|||
|
bdToExсel.cursor.execute(sql)
|
|||
|
countFromTovar = bdToExсel.cursor.fetchall()
|
|||
|
sql='INSERT INTO `tovar` (`tovar_name`, `barcode`, `id_tovar`, `cena`, `kolvo`, `artikul`, `user_code`) VALUES ("' + tovar_name + '", "' + barcode + '", "' + id + '", "' + cena + '", "' + kolvo + '", "' + artikul +'", "' + user_code + '")'
|
|||
|
if (countFromTovar[0][0]==0):
|
|||
|
bdToExсel.cursor.execute(sql)
|
|||
|
bdToExсel.conn.commit()
|
|||
|
|
|||
|
def getInfo(self, id):
|
|||
|
sql2='SELECT ITEM.PRICE, ITEM.INVENTORY, BARCODE.barcode FROM ITEM, BARCODE WHERE ITEM.WARE_ID=' + str(id) + ' AND BARCODE.WAREID=' + str(id)
|
|||
|
bdToExсel.cursor.execute(sql2)
|
|||
|
res = bdToExсel.cursor.fetchall()
|
|||
|
try:
|
|||
|
return [res[0][0], res[0][1], res[0][2]]
|
|||
|
except:
|
|||
|
return ""
|
|||
|
|
|||
|
def FromBDtoCSV(self, user_code, csvFile, convert):
|
|||
|
sql='SELECT * FROM `tovar` WHERE `user_code` = "' + user_code + '"'
|
|||
|
bdToExсel.cursor.execute(sql)
|
|||
|
tovars=bdToExсel.cursor.fetchall()
|
|||
|
with open(csvFile, "w") as file:
|
|||
|
for tovar in tovars:
|
|||
|
file.write(str(tovar[1]) + ';' + str(tovar[2]) + ';' + str(tovar[3]) + ';' + str(tovar[4]) + ';' + str(tovar[5]) + ';' + str(tovar[6]) + '\n')
|
|||
|
if (convert=="1"):
|
|||
|
os.system("iconv -f UTF8 -t CP1251 " + csvFile + " -o result_" + csvFile)
|
|||
|
|
|||
|
if __name__ == '__main__':
|
|||
|
user_code=sys.argv[1]
|
|||
|
bd_name=sys.argv[2]
|
|||
|
csvFile=sys.argv[3]
|
|||
|
convert=sys.argv[4]
|
|||
|
convertBD=bdToExсel(bd_name)
|
|||
|
convertBD.tovarToTovar(user_code)
|
|||
|
convertBD.FromBDtoCSV(user_code, csvFile, convert)
|