|
| 1 | +# This Python file uses the following encoding: utf-8 |
| 2 | +from PyQt5.QtPrintSupport import QPrinter, QPrintDialog |
| 3 | +from PyQt5.QtGui import QPainter, QPixmap |
| 4 | +from PyQt5.QtWidgets import QDialog |
| 5 | +from PyQt5.QtCore import QPoint, QSettings |
| 6 | +from src.dao.customerdao import CustomerDAO |
| 7 | +from src.dao.citydao import CityDAO |
| 8 | +from src.dao.statedao import StateDAO |
| 9 | +from src.dao.countrydao import CountryDAO |
| 10 | +from src.dao.productdao import ProductDAO |
| 11 | +from src.dao.saleproductdao import SaleProductDAO |
| 12 | + |
| 13 | + |
| 14 | +class InvoicePrinter: |
| 15 | + def __init__(self): |
| 16 | + self.painter = QPainter() |
| 17 | + self.printer = QPrinter() |
| 18 | + self.settings = QSettings() |
| 19 | + self.margin = int(self.settings.value("printing/margin")) |
| 20 | + self.lineHeight = int(self.settings.value("printing/lineHeight")) |
| 21 | + |
| 22 | + def printInvoice(self, sale): |
| 23 | + self.printDialog = QPrintDialog(self.printer) |
| 24 | + if self.printDialog.exec() == QDialog.Accepted: |
| 25 | + customerDao = CustomerDAO() |
| 26 | + customer = customerDao.select(sale.customerId) |
| 27 | + |
| 28 | + self.painter.begin(self.printer) |
| 29 | + y = self.paintLogo() |
| 30 | + y += 2 * self.lineHeight |
| 31 | + y = self.paintBusiness(self.margin, y) |
| 32 | + y += 2 * self.lineHeight |
| 33 | + y = self.paintCustomer(customer, self.margin, y) |
| 34 | + y += 2 * self.lineHeight |
| 35 | + y = self.paintSaleProducts(sale, self.margin, y) |
| 36 | + y += 2 * self.lineHeight |
| 37 | + self.paintTotal(sale, self.margin, y) |
| 38 | + self.painter.end() |
| 39 | + |
| 40 | + def paintLogo(self): |
| 41 | + logoPos = int(self.settings.value("printing/logoPosition")) |
| 42 | + logo = QPixmap(self.settings.value("printing/logoPath")) |
| 43 | + logoWidth = int(self.settings.value("printing/logoWidth")) |
| 44 | + logoHeight = int(self.settings.value("printing/logoHeight")) |
| 45 | + logoPoint = QPoint(self.margin, logoPos) |
| 46 | + self.painter.drawPixmap(logoPoint, logo.scaled(logoWidth, logoHeight)) |
| 47 | + return logoHeight + logoPos |
| 48 | + |
| 49 | + def paintBusiness(self, x, y): |
| 50 | + businessName = self.settings.value("business/name") |
| 51 | + businessPhone = self.settings.value("business/phone") |
| 52 | + businessAddressLine1 = self.settings.value("business/addressLine1") |
| 53 | + businessAddressLine2 = self.settings.value("business/addressLine2") |
| 54 | + businessEmail = self.settings.value("business/email") |
| 55 | + |
| 56 | + fields = [businessName, |
| 57 | + businessPhone, |
| 58 | + businessAddressLine1, |
| 59 | + businessAddressLine2, |
| 60 | + businessEmail] |
| 61 | + |
| 62 | + for field in fields: |
| 63 | + self.painter.drawText(QPoint(x, y), field) |
| 64 | + y += self.lineHeight |
| 65 | + |
| 66 | + return y |
| 67 | + |
| 68 | + def paintCustomer(self, customer, x, y): |
| 69 | + countryDao = CountryDAO() |
| 70 | + stateDao = StateDAO() |
| 71 | + cityDao = CityDAO() |
| 72 | + |
| 73 | + customerCity = cityDao.select(customer.cityId) |
| 74 | + customerState = stateDao.select(customerCity.stateId) |
| 75 | + customerCountry = countryDao.select(customerState.countryId) |
| 76 | + |
| 77 | + fields = [customer.name, |
| 78 | + customer.addressLine1, |
| 79 | + customer.addressLine2, |
| 80 | + customer.zipcode, |
| 81 | + customer.email, |
| 82 | + customer.phoneNumber, |
| 83 | + customerCity.name, |
| 84 | + customerState.name, |
| 85 | + customerCountry.name] |
| 86 | + |
| 87 | + for field in fields: |
| 88 | + self.painter.drawText(QPoint(x, y), field) |
| 89 | + y += self.lineHeight |
| 90 | + return y |
| 91 | + |
| 92 | + def paintSaleProducts(self, sale, x, y): |
| 93 | + productDao = ProductDAO() |
| 94 | + saleProductDao = SaleProductDAO() |
| 95 | + saleProducts = saleProductDao.select(sale.id) |
| 96 | + |
| 97 | + for saleProduct in saleProducts: |
| 98 | + productName = productDao.select(saleProduct.productId) |
| 99 | + productTotal = saleProduct.quantity * saleProduct.price |
| 100 | + self.painter.drawText(QPoint(x, y), productName.name) |
| 101 | + y += self.lineHeight |
| 102 | + self.painter.drawText(QPoint(x, y), str(saleProduct.quantity) |
| 103 | + + " x " + |
| 104 | + str(saleProduct.price) |
| 105 | + + " = " + |
| 106 | + str(productTotal)) |
| 107 | + y += self.lineHeight |
| 108 | + return y |
| 109 | + |
| 110 | + def paintTotal(self, sale, x, y): |
| 111 | + self.painter.drawText(QPoint(x, y), "Total: $" + "%.2f" % sale.amount) |
| 112 | + y += self.lineHeight |
| 113 | + self.painter.drawText(QPoint(x, y), "Shipping: $" + "%.2f" % sale.shipping) |
| 114 | + y += self.lineHeight |
| 115 | + self.painter.drawText(QPoint(x, y), "Discount: $" + "%.2f" % sale.discount) |
0 commit comments