108 lines
4.5 KiB
Python
108 lines
4.5 KiB
Python
__author__ = 'tanshu'
|
|
from PyQt5 import uic, QtCore
|
|
|
|
|
|
file = '/home/tanshu/Programming/summer/summer/QtDesignerFiles/ProductDetail.ui'
|
|
base, form = uic.loadUiType(file)
|
|
|
|
|
|
class ProductDetail(base, form):
|
|
def __init__(self, router, product, product_groups, taxes, parent=None):
|
|
super(ProductDetail, self).__init__()
|
|
self.setupUi(self)
|
|
self.router = router
|
|
self.product = product
|
|
self.txtName.setText(product['Name'])
|
|
self.txtUnits.setText(product['Units'])
|
|
self.product_group_model = ProductGroupModel(product_groups)
|
|
self.cmbProductGroup.setModel(self.product_group_model)
|
|
pg_index = [i for (i, d) in enumerate(product_groups) if
|
|
d['ProductGroupID'] == product['ProductGroup']['ProductGroupID']]
|
|
if len(pg_index) != 0:
|
|
self.cmbProductGroup.setCurrentIndex(pg_index[0])
|
|
|
|
self.dsbPrice.setMinimum(0)
|
|
self.dsbPrice.setValue(product['Price'])
|
|
self.chkHasHappyHour.setCheckState(QtCore.Qt.Checked if product['HasHappyHour'] else QtCore.Qt.Unchecked)
|
|
|
|
self.service_tax_model = TaxModel(taxes)
|
|
self.cmbServiceTax.setModel(self.service_tax_model)
|
|
st_index = [i for (i, d) in enumerate(taxes) if d['TaxID'] == product['ServiceTax']['TaxID']]
|
|
if len(st_index) != 0:
|
|
self.cmbServiceTax.setCurrentIndex(st_index[0])
|
|
|
|
self.vat_model = TaxModel(taxes)
|
|
self.cmbVat.setModel(self.vat_model)
|
|
vat_index = [i for (i, d) in enumerate(taxes) if d['TaxID'] == product['Vat']['TaxID']]
|
|
if len(vat_index) != 0:
|
|
self.cmbVat.setCurrentIndex(vat_index[0])
|
|
|
|
self.dsbServiceCharge.setMinimum(0)
|
|
self.dsbServiceCharge.setValue(product['ServiceCharge'])
|
|
self.chkScTaxable.setCheckState(QtCore.Qt.Checked if product['ScTaxable'] else QtCore.Qt.Unchecked)
|
|
self.chkIsActive.setCheckState(QtCore.Qt.Checked if product['IsActive'] else QtCore.Qt.Unchecked)
|
|
self.sbSortOrder.setMinimum(0)
|
|
self.sbSortOrder.setValue(product['SortOrder'])
|
|
self.accepted.connect(self.save_product)
|
|
self.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
|
|
|
|
def save_product(self):
|
|
self.product['Name'] = self.txtName.text()
|
|
self.product['Units'] = self.txtUnits.text()
|
|
self.product['ProductGroup'] = self.cmbProductGroup.currentData()
|
|
self.product['Price'] = self.dsbPrice.value()
|
|
self.product['HasHappyHour'] = self.chkHasHappyHour.isChecked()
|
|
self.product['ServiceTax'] = self.cmbServiceTax.currentData()
|
|
self.product['Vat'] = self.cmbVat.currentData()
|
|
self.product['ServiceCharge'] = self.dsbServiceCharge.value() / 100
|
|
self.product['ScTaxable'] = self.chkScTaxable.isChecked()
|
|
self.product['IsActive'] = self.chkIsActive.isChecked()
|
|
self.product['SortOrder'] = self.sbSortOrder.value()
|
|
|
|
self.router.save_product(self.product)
|
|
|
|
|
|
class ProductGroupModel(QtCore.QAbstractListModel):
|
|
def __init__(self, product_groups=None, parent=None):
|
|
QtCore.QAbstractListModel.__init__(self, parent)
|
|
self.product_groups = product_groups if product_groups is not None else []
|
|
|
|
def rowCount(self, parent):
|
|
return len(self.product_groups)
|
|
|
|
def data(self, index, role):
|
|
if role == QtCore.Qt.DisplayRole:
|
|
item = self.product_groups[index.row()]
|
|
return item['Name']
|
|
if role == QtCore.Qt.UserRole:
|
|
return self.product_groups[index.row()]
|
|
|
|
def headerData(self, section, orientation, role):
|
|
if role == QtCore.Qt.DisplayRole:
|
|
if orientation == QtCore.Qt.Horizontal:
|
|
return 'Product Groups'
|
|
else:
|
|
return section + 1
|
|
|
|
|
|
class TaxModel(QtCore.QAbstractListModel):
|
|
def __init__(self, taxes=None, parent=None):
|
|
QtCore.QAbstractListModel.__init__(self, parent)
|
|
self.taxes = taxes if taxes is not None else []
|
|
|
|
def rowCount(self, parent):
|
|
return len(self.taxes)
|
|
|
|
def data(self, index, role):
|
|
if role == QtCore.Qt.DisplayRole:
|
|
item = self.taxes[index.row()]
|
|
return item['Name'] + ' ' + '({0:,.2f}%)'.format(item['Rate'] * 100)
|
|
if role == QtCore.Qt.UserRole:
|
|
return self.taxes[index.row()]
|
|
|
|
def headerData(self, section, orientation, role):
|
|
if role == QtCore.Qt.DisplayRole:
|
|
if orientation == QtCore.Qt.Horizontal:
|
|
return 'Taxes'
|
|
else:
|
|
return section + 1 |