Advertisement
Guest User

djnm

a guest
Apr 21st, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 15.76 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import sys
  4. import sqlite3 as db
  5. from PyQt4 import QtCore, QtGui, QtSql, Qt
  6. from PyQt4.QtCore import QStringList, QString
  7. from PyQt4.QtGui  import QApplication, QMainWindow, QSizePolicy
  8. from PyQt4.QtGui  import QTableWidget, QTableWidgetItem
  9. import re
  10. import config
  11.  
  12.  
  13. id_number = config.id_number
  14. db_filename = config.db_filename
  15.  
  16.  
  17. try:
  18.     _fromUtf8 = QtCore.QString.fromUtf8
  19. except AttributeError:
  20.     _fromUtf8 = lambda s: s
  21.  
  22. class MainWindow(QtGui.QWidget):
  23.     def __init__(self, child):
  24.         super(MainWindow, self).__init__()
  25.  
  26.         self.setObjectName(_fromUtf8("self"))
  27.         self.resize(826, 518)
  28.         self.verticalLayout = QtGui.QVBoxLayout(self)
  29.         self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
  30.         self.tableWidget_cards = QtGui.QTableWidget(self)
  31.         font = QtGui.QFont()
  32.         font.setPointSize(12)
  33.         self.tableWidget_cards.setFont(font)
  34.         self.tableWidget_cards.setObjectName(_fromUtf8("tableWidget_cards"))
  35.         self.tableWidget_cards.setColumnCount(0)
  36.         self.tableWidget_cards.setRowCount(0)
  37.         self.verticalLayout.addWidget(self.tableWidget_cards)
  38.         self.horizontalLayout = QtGui.QHBoxLayout()
  39.         self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
  40.         self.Add_Button = QtGui.QPushButton(self)
  41.         font = QtGui.QFont()
  42.         font.setPointSize(12)
  43.         self.Add_Button.setFont(font)
  44.         self.Add_Button.setObjectName(_fromUtf8("Add_Button"))
  45.         self.horizontalLayout.addWidget(self.Add_Button)
  46.         self.Change_Button = QtGui.QPushButton(self)
  47.         font = QtGui.QFont()
  48.         font.setPointSize(12)
  49.         self.Change_Button.setFont(font)
  50.         self.Change_Button.setObjectName(_fromUtf8("Change_Button"))
  51.         self.horizontalLayout.addWidget(self.Change_Button)
  52.         self.Delete_Button = QtGui.QPushButton(self)
  53.         font = QtGui.QFont()
  54.         font.setPointSize(12)
  55.         self.Delete_Button.setFont(font)
  56.         self.Delete_Button.setObjectName(_fromUtf8("Delete_Button"))
  57.         self.horizontalLayout.addWidget(self.Delete_Button)
  58.         self.Close_Button = QtGui.QPushButton(self)
  59.         font = QtGui.QFont()
  60.         font.setPointSize(12)
  61.         self.Close_Button.setFont(font)
  62.         self.Close_Button.setObjectName(_fromUtf8("Close_Button"))
  63.         self.horizontalLayout.addWidget(self.Close_Button)
  64.         self.verticalLayout.addLayout(self.horizontalLayout)
  65.  
  66.         self.retranslateUi()
  67.         QtCore.QObject.connect(self.Close_Button, QtCore.SIGNAL(_fromUtf8("clicked()")), self.close)
  68.         QtCore.QMetaObject.connectSlotsByName(self)
  69.         QtCore.QObject.connect(self.Delete_Button, QtCore.SIGNAL(_fromUtf8("clicked()")), self.delete)
  70.         self.setTabOrder(self.Add_Button, self.Change_Button)
  71.         self.setTabOrder(self.Change_Button, self.Delete_Button)
  72.         self.setTabOrder(self.Delete_Button, self.Close_Button)
  73.         self.setTabOrder(self.Close_Button, self.tableWidget_cards)
  74.         self.Add_Button.clicked.connect(child.show)
  75.         self.data = 'OK'
  76.         self.update()
  77.  
  78.     def retranslateUi(self):
  79.         self.setWindowTitle(QtGui.QApplication.translate("self", "Товары", None, QtGui.QApplication.UnicodeUTF8))
  80.         self.Add_Button.setText(QtGui.QApplication.translate("self", "Добавить", None, QtGui.QApplication.UnicodeUTF8))
  81.         self.Change_Button.setText(QtGui.QApplication.translate("self", "Изменить", None, QtGui.QApplication.UnicodeUTF8))
  82.         self.Delete_Button.setText(QtGui.QApplication.translate("self", "Удалить", None, QtGui.QApplication.UnicodeUTF8))
  83.         self.Close_Button.setText(QtGui.QApplication.translate("self", "Закрыть", None, QtGui.QApplication.UnicodeUTF8))
  84.  
  85.     def printData(self):
  86.         print(self.data)
  87.         self.update()
  88.  
  89.     def update(self):
  90.         print "Update run"
  91.         global db_filename
  92.         self.tableWidget_cards.clear()
  93.         self.tableWidget_cards.setColumnCount(6)
  94.         self.tableWidget_cards.setRowCount(40) #выставить столько полей, сколько в таблице
  95.         self.tableWidget_cards.setHorizontalHeaderLabels(
  96.             [u'Производитель', u'Наименование', u'Ед. измерения', u'Ставка НДС', u'№РУ', u'Дата истечения'])
  97.         conn = db.connect(db_filename)
  98.         c = conn.cursor()
  99.         db_is_new = not os.path.exists(db_filename)
  100.         if db_is_new:
  101.             print 'Need to create schema'
  102.         else:
  103.             print 'Database exists, assume schema does, too.'
  104.         conn.execute(
  105.             '''CREATE TABLE IF NOT EXISTS cards (id INTEGER PRIMARY KEY AUTOINCREMENT, manufacturer TEXT NOT NULL, name  TEXT NOT NULL, unit TEXT, nds TEXT NOT NULL, ru TEXT NOT NULL, date_end TEXT);''')
  106.         print "Table created successfully"
  107.         i = 0
  108.         rowd = 0
  109.         for row in c.execute("SELECT * FROM cards"):
  110.             a = [row[5], row[2], row[4], row[6], row[1], row[3]]
  111.             for i in range(6):
  112.                 name = unicode(a[i])
  113.                 self.tableWidget_cards.setItem(rowd, i, QtGui.QTableWidgetItem(name))
  114.                 print name
  115.                 i += 1
  116.             rowd += 1
  117.             print rowd
  118.         conn.close()
  119.         self.tableWidget_cards.resizeColumnsToContents()
  120.         print "Update end"
  121.  
  122.     def delete(self):
  123.         print "Delete RUN"
  124.         global db_filename
  125.         cell = self.getsamerowcell()
  126.         print cell
  127.         conn = db.connect(db_filename)
  128.         c = conn.cursor()
  129.         c.execute('DELETE FROM cards WHERE name=?', (cell,))
  130.         conn.commit()
  131.         conn.close
  132.         self.update()
  133.         print "Delete END"
  134.  
  135.     def getsamerowcell(self):
  136.         matchcol = 0
  137.         row = self.tableWidget_cards.currentItem().row()
  138.         headercount = self.tableWidget_cards.columnCount()
  139.         for x in range(0,headercount,1):
  140.             headertext = self.tableWidget_cards.horizontalHeaderItem(x).text()
  141.             if u'Наименование' == unicode(headertext):
  142.                 matchcol = x
  143.                 break
  144.         cell = self.tableWidget_cards.item(row,matchcol).text()
  145.         print unicode(cell)
  146.         return unicode(cell)
  147.  
  148.  
  149. class Child(QtGui.QWidget):
  150.     def __init__(self):
  151.         super(Child, self).__init__()
  152.         self.mainWindow = None
  153.  
  154.     def setMainWindow(self, window):
  155.         if self.mainWindow is not None:
  156.             raise Exception('Failed!')
  157.         self.mainWindow = window
  158. #        button = QtGui.QPushButton(u'Пробуй', self)
  159. #        button.setMinimumSize(QtCore.QSize(150, 70))
  160. #        button.clicked.connect(self.mainWindow.printData)
  161.         self.setObjectName(_fromUtf8("self"))
  162.         self.resize(370, 577)
  163.         self.setLocale(QtCore.QLocale(QtCore.QLocale.Russian, QtCore.QLocale.RussianFederation))
  164.         self.verticalLayout = QtGui.QVBoxLayout(self)
  165.         self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
  166.         self.label = QtGui.QLabel(self)
  167.         font = QtGui.QFont()
  168.         font.setPointSize(12)
  169.         self.label.setFont(font)
  170.         self.label.setObjectName(_fromUtf8("label"))
  171.         self.verticalLayout.addWidget(self.label)
  172.         self.manufac_lineEdit = QtGui.QLineEdit(self)
  173.         font = QtGui.QFont()
  174.         font.setPointSize(12)
  175.         self.manufac_lineEdit.setFont(font)
  176.         self.manufac_lineEdit.setObjectName(_fromUtf8("manufac_lineEdit"))
  177.         self.verticalLayout.addWidget(self.manufac_lineEdit)
  178.         self.label_2 = QtGui.QLabel(self)
  179.         font = QtGui.QFont()
  180.         font.setPointSize(12)
  181.         self.label_2.setFont(font)
  182.         self.label_2.setObjectName(_fromUtf8("label_2"))
  183.         self.verticalLayout.addWidget(self.label_2)
  184.         self.name_lineEdit = QtGui.QLineEdit(self)
  185.         font = QtGui.QFont()
  186.         font.setPointSize(12)
  187.         self.name_lineEdit.setFont(font)
  188.         self.name_lineEdit.setObjectName(_fromUtf8("name_lineEdit"))
  189.         self.verticalLayout.addWidget(self.name_lineEdit)
  190.         self.label_3 = QtGui.QLabel(self)
  191.         font = QtGui.QFont()
  192.         font.setPointSize(12)
  193.         self.label_3.setFont(font)
  194.         self.label_3.setObjectName(_fromUtf8("label_3"))
  195.         self.verticalLayout.addWidget(self.label_3)
  196.         self.unit_comboBox = QtGui.QComboBox(self)
  197.         font = QtGui.QFont()
  198.         font.setPointSize(12)
  199.         self.unit_comboBox.setFont(font)
  200.         self.unit_comboBox.setObjectName(_fromUtf8("unit_comboBox"))
  201.         self.unit_comboBox.addItem(_fromUtf8(""))
  202.         self.unit_comboBox.addItem(_fromUtf8(""))
  203.         self.unit_comboBox.addItem(_fromUtf8(""))
  204.         self.unit_comboBox.addItem(_fromUtf8(""))
  205.         self.unit_comboBox.addItem(_fromUtf8(""))
  206.         self.unit_comboBox.addItem(_fromUtf8(""))
  207.         self.verticalLayout.addWidget(self.unit_comboBox)
  208.         self.label_4 = QtGui.QLabel(self)
  209.         font = QtGui.QFont()
  210.         font.setPointSize(12)
  211.         self.label_4.setFont(font)
  212.         self.label_4.setObjectName(_fromUtf8("label_4"))
  213.         self.verticalLayout.addWidget(self.label_4)
  214.         self.NDS_comboBox = QtGui.QComboBox(self)
  215.         font = QtGui.QFont()
  216.         font.setPointSize(12)
  217.         self.NDS_comboBox.setFont(font)
  218.         self.NDS_comboBox.setObjectName(_fromUtf8("NDS_comboBox"))
  219.         self.NDS_comboBox.addItem(_fromUtf8(""))
  220.         self.NDS_comboBox.addItem(_fromUtf8(""))
  221.         self.NDS_comboBox.addItem(_fromUtf8(""))
  222.         self.verticalLayout.addWidget(self.NDS_comboBox)
  223.         self.label_6 = QtGui.QLabel(self)
  224.         font = QtGui.QFont()
  225.         font.setPointSize(12)
  226.         self.label_6.setFont(font)
  227.         self.label_6.setObjectName(_fromUtf8("label_6"))
  228.         self.verticalLayout.addWidget(self.label_6)
  229.         self.RU_lineEdit = QtGui.QLineEdit(self)
  230.         font = QtGui.QFont()
  231.         font.setPointSize(12)
  232.         self.RU_lineEdit.setFont(font)
  233.         self.RU_lineEdit.setObjectName(_fromUtf8("RU_lineEdit"))
  234.         self.verticalLayout.addWidget(self.RU_lineEdit)
  235.         self.label_5 = QtGui.QLabel(self)
  236.         font = QtGui.QFont()
  237.         font.setPointSize(12)
  238.         self.label_5.setFont(font)
  239.         self.label_5.setObjectName(_fromUtf8("label_5"))
  240.         self.verticalLayout.addWidget(self.label_5)
  241.         self.calendarWidget = QtGui.QCalendarWidget(self)
  242.         font = QtGui.QFont()
  243.         font.setPointSize(12)
  244.         self.calendarWidget.setFont(font)
  245.         self.calendarWidget.setObjectName(_fromUtf8("calendarWidget"))
  246.         self.verticalLayout.addWidget(self.calendarWidget)
  247.         self.Save_Button = QtGui.QPushButton(self)
  248.         font = QtGui.QFont()
  249.         font.setPointSize(12)
  250.         self.Save_Button.setFont(font)
  251.         self.Save_Button.setObjectName(_fromUtf8("Change_Button"))
  252.         self.verticalLayout.addWidget(self.Save_Button)
  253.  
  254.         self.retranslateUi()
  255.         QtCore.QMetaObject.connectSlotsByName(self)
  256.  
  257.         QtCore.QObject.connect(self.Save_Button, QtCore.SIGNAL(_fromUtf8("clicked()")), self.save)
  258.  
  259.  
  260.     def retranslateUi(self):
  261.         self.setWindowTitle(QtGui.QApplication.translate("self", "Карточка товара", None, QtGui.QApplication.UnicodeUTF8))
  262.         self.label.setText(QtGui.QApplication.translate("self", "Прозводитель", None, QtGui.QApplication.UnicodeUTF8))
  263.         self.label_2.setText(QtGui.QApplication.translate("self", "Наименование", None, QtGui.QApplication.UnicodeUTF8))
  264.         self.label_3.setText(QtGui.QApplication.translate("self", "Единица измерения", None, QtGui.QApplication.UnicodeUTF8))
  265.         self.unit_comboBox.setItemText(0, QtGui.QApplication.translate("self", "Упаковка", None, QtGui.QApplication.UnicodeUTF8))
  266.         self.unit_comboBox.setItemText(1, QtGui.QApplication.translate("self", "Килограммы", None, QtGui.QApplication.UnicodeUTF8))
  267.         self.unit_comboBox.setItemText(2, QtGui.QApplication.translate("self", "Штуки", None, QtGui.QApplication.UnicodeUTF8))
  268.         self.unit_comboBox.setItemText(3, QtGui.QApplication.translate("self", "Литры", None, QtGui.QApplication.UnicodeUTF8))
  269.         self.unit_comboBox.setItemText(4, QtGui.QApplication.translate("self", "Наборы", None, QtGui.QApplication.UnicodeUTF8))
  270.         self.unit_comboBox.setItemText(5, QtGui.QApplication.translate("self", "Флаконы", None, QtGui.QApplication.UnicodeUTF8))
  271.         self.label_4.setText(QtGui.QApplication.translate("self", "Ставка НДС", None, QtGui.QApplication.UnicodeUTF8))
  272.         self.NDS_comboBox.setItemText(0, QtGui.QApplication.translate("self", "10", None, QtGui.QApplication.UnicodeUTF8))
  273.         self.NDS_comboBox.setItemText(1, QtGui.QApplication.translate("self", "18", None, QtGui.QApplication.UnicodeUTF8))
  274.         self.NDS_comboBox.setItemText(2, QtGui.QApplication.translate("self", "Без НДС", None, QtGui.QApplication.UnicodeUTF8))
  275.         self.label_6.setText(QtGui.QApplication.translate("self", "№ РУ", None, QtGui.QApplication.UnicodeUTF8))
  276.         self.label_5.setText(QtGui.QApplication.translate("self", "Дата окнчания РУ", None, QtGui.QApplication.UnicodeUTF8))
  277.         self.Save_Button.setText(QtGui.QApplication.translate("self", "Сохранить", None, QtGui.QApplication.UnicodeUTF8))
  278.  
  279.  
  280.     def save(self):
  281.         global rowd, db_filename, id_number
  282.         import sqlite3 as db
  283.         print "Start"
  284.         ru = unicode(self.RU_lineEdit.text())
  285.         name = unicode(self.name_lineEdit.text())
  286.         date_ends = self.calendarWidget.selectedDate()
  287.         date_end = date_ends.toString()
  288.         unit = unicode(self.unit_comboBox.currentText())
  289.         nds = unicode(self.NDS_comboBox.currentText())
  290.         manufacturer = unicode(self.manufac_lineEdit.text())
  291.         db_is_new = not os.path.exists(db_filename)
  292.         conn = db.connect(db_filename)
  293.         c = conn.cursor()
  294.         if db_is_new:
  295.             print 'Need to create schema'
  296.         else:
  297.             print 'Database exists, assume schema does, too.'
  298.         conn.execute(
  299.             '''CREATE TABLE IF NOT EXISTS cards (id INTEGER PRIMARY KEY AUTOINCREMENT, manufacturer TEXT NOT NULL, name  TEXT NOT NULL, unit TEXT, nds TEXT NOT NULL, ru TEXT NOT NULL, date_end TEXT);''')
  300.         print "Table created successfully"
  301.         print 'Inserting initial data'
  302.         i = 0
  303.         c.execute("SELECT id FROM cards")
  304.         print "id_number = " + str(id_number)
  305.         rows = c.fetchall()
  306.         for row in rows:
  307.             id_n = str(rows[i])
  308.             id_n = re.search('(\d+)', id_n).group()
  309.             print "id_n_from_rows = " + str(id_n)
  310.             if id_number == 0:
  311.                 with conn:
  312.                     c.execute("Insert into cards (ru, name, date_end, unit, manufacturer, nds) values (?,?,?,?,?,?)",
  313.                               (ru, name, str(date_end), unit, manufacturer, nds))
  314.                 conn.commit
  315.                 break
  316.             elif id_number == int(id_n):
  317.                 print "rows = " + str(rows[i])
  318.                 with conn:
  319.                     c.execute("UPDATE cards SET ru=?, name=?, date_end=?, unit=?, manufacturer=?, nds=? WHERE id=?",(ru, name, str(date_end), unit, manufacturer, id_number, nds))
  320.                 conn.commit
  321.                 id_number = 0
  322.                 break
  323.             i+=1
  324.         conn.close()
  325.         self.manufac_lineEdit.clear()
  326.         self.name_lineEdit.clear()
  327.         self.RU_lineEdit.clear()
  328.         id_number = 0
  329.         print "Wrote"
  330.  
  331.     def closeEvent(self, event):
  332.         self.mainWindow.printData()
  333.  
  334.  
  335. if __name__ == '__main__':
  336.     app = QtGui.QApplication(sys.argv)
  337.     child = Child()
  338.     main = MainWindow(child)
  339.     child.setMainWindow(main)
  340.     main.show()
  341.     sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement