-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract.py
executable file
·99 lines (85 loc) · 3.61 KB
/
extract.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
import sys
from calculator import Calculator
from util import gui
from util.table import NumericItem, formatFloatToMoney
from PySide2.QtWidgets import QAbstractItemView, QComboBox, QMessageBox, QPushButton, QTableWidget, QTableWidgetItem
from PySide2.QtCore import SIGNAL, QObject
from PySide2 import QtCore
class Extract:
def __init__(self, db):
self.ui = gui.load_ui('./windows/extract.ui')
self.db = db
self.year = self.ui.findChild(QComboBox, 'extract_year')
self.button = self.ui.findChild(QPushButton, 'extract_button')
self.button.clicked.connect(self.checkYearAndUpdateTable)
self.stockTable = self.ui.findChild(QTableWidget, 'stock_table')
self.initTable()
self.updateWindow()
def updateWindow(self):
self.updateAvailableYears()
self.updateTable()
def updateAvailableYears(self):
# Hold year currently selected in window
prevYearSelected = self.year.currentData()
# Update years according to what's in database
self.year.clear()
self.year.addItem('ANO', None)
availableYears = self.db.getYearsWithOrders()
index = 1
for year in availableYears:
self.year.addItem(year, int(year))
if (int(year) == prevYearSelected):
self.year.setCurrentIndex(index)
index = index + 1
def initTable(self):
self.stockTable.setRowCount(0)
self.stockTable.setColumnCount(4)
self.stockTable.setHorizontalHeaderLabels(['Código', 'Qnt.', 'Valor médio (R$)', 'Total (R$)'])
self.stockTable.setSelectionBehavior(QAbstractItemView.SelectRows)
def checkYearAndUpdateTable(self):
if (not self.year.currentData()):
QMessageBox.critical(self.ui, 'ERRO', 'Por favor selecione um ano', QMessageBox.StandardButton.Abort)
return
self.updateTable()
def updateTable(self):
self.stockTable.setSortingEnabled(False)
year = self.year.currentData()
self.fillTable(year)
self.stockTable.setSortingEnabled(True)
def fillTable(self, year):
if (not year):
return
ordersUpToYear = self.db.getOrdersInAscendingDateUpToYear(year)
calculator = Calculator(self.db)
ret = calculator.getYearExtract(ordersUpToYear)
# Now extract from return the
# TODO: Error out if we have negative number of stocks
rows = []
for k, v in ret.items():
amount = v[0]
avgValue = v[1]
if (amount > 0):
rows.append([k, amount, avgValue])
self.stockTable.setRowCount(len(rows))
rowIndex = 0
for row in rows:
# TODO: Extract columns' indexes to variables
for i in range(0, 3):
if i == 0:
item = QTableWidgetItem(row[0])
# Column 1 and 2 are amount and value, respectively, use numerical
elif i == 1:
item = NumericItem(str(row[i]))
item.setData(QtCore.Qt.UserRole, row[i])
elif i == 2:
item = NumericItem(formatFloatToMoney(row[i]))
item.setData(QtCore.Qt.UserRole, row[i])
self.stockTable.setItem(rowIndex, i, item)
# Add total includig taxes
totalValue = row[1]*row[2]
totalItem = NumericItem(formatFloatToMoney(totalValue))
totalItem.setData(QtCore.Qt.UserRole, totalValue)
self.stockTable.setItem(rowIndex, 3, totalItem)
rowIndex = rowIndex + 1
def getUi(self):
return self.ui