From 999016a902d631cb4a23a8f89301abb7e9dd444a Mon Sep 17 00:00:00 2001 From: forntoh Date: Tue, 23 Jun 2020 11:01:10 +0100 Subject: [PATCH] added date and ticket number to printed receipt --- .../demo/controller/OrderController.kt | 9 ++++--- .../com/example/demo/data/model/ActeDao.kt | 6 ++--- .../com/example/demo/utils/PrinterService.kt | 25 +++++++++++-------- .../kotlin/com/example/demo/utils/Receipt.kt | 21 +++++++++++----- 4 files changed, 37 insertions(+), 24 deletions(-) diff --git a/src/main/kotlin/com/example/demo/controller/OrderController.kt b/src/main/kotlin/com/example/demo/controller/OrderController.kt index 8c70b5c..0840b20 100644 --- a/src/main/kotlin/com/example/demo/controller/OrderController.kt +++ b/src/main/kotlin/com/example/demo/controller/OrderController.kt @@ -18,7 +18,7 @@ class OrderController : Controller() { var orderItems: ObservableList by singleAssign() private var printerService: PrinterService by singleAssign() - fun createOrder(patientEntryModel: PatientEntryModel) = execute { + private fun createOrder(patientEntryModel: PatientEntryModel) = execute { val o = OrderTbl.new(UUID.randomUUID()) { timeStamp = LocalDate.now().toDateTime(LocalTime.now()) patient = EntityID(patientEntryModel.id.value.toInt(), PatientsTbl) @@ -30,13 +30,16 @@ class OrderController : Controller() { it[Quantity] = item.qtyTemp.value.toInt() } } + Pair(o.id.value.toString(), o.timeStamp) } fun printOrder(patientEntryModel: PatientEntryModel) { - createOrder(patientEntryModel) + val pair = createOrder(patientEntryModel) printerService.printReceipt( orderItems.map { Item(it.label.value, it.qtyTemp.value, it.price.value.toDouble()) }, - patientEntryModel.name.value + patientEntryModel.name.value, + pair.first, + pair.second ) } diff --git a/src/main/kotlin/com/example/demo/data/model/ActeDao.kt b/src/main/kotlin/com/example/demo/data/model/ActeDao.kt index bb272bd..8c5d7b0 100644 --- a/src/main/kotlin/com/example/demo/data/model/ActeDao.kt +++ b/src/main/kotlin/com/example/demo/data/model/ActeDao.kt @@ -8,7 +8,7 @@ import javafx.beans.property.SimpleStringProperty import org.jetbrains.exposed.dao.IntEntity import org.jetbrains.exposed.dao.IntEntityClass import org.jetbrains.exposed.dao.id.EntityID -import org.jetbrains.exposed.dao.id.IdTable +import org.jetbrains.exposed.dao.id.IntIdTable import org.jetbrains.exposed.sql.ResultRow import tornadofx.* @@ -17,13 +17,11 @@ import tornadofx.* * @version 1.0 * @created 12-Jun-2020 11:17:28 AM */ -object ActesTbl : IdTable() { - override val id = integer("ActeId").autoIncrement().entityId() +object ActesTbl : IntIdTable(columnName = "ActeId") { val Name = text("Name") val AppliedAmount = decimal("AppliedAmount", scale = 0, precision = 9) val OfficialAmount = decimal("OfficialAmount", scale = 0, precision = 9) val SynthesisSection = reference("SynthesisSectionId", SynthesisSectionsTbl, fkName = "FK_Acte_Belongs") - override val primaryKey = PrimaryKey(columns = *arrayOf(id)) } class ActeTbl(id: EntityID) : IntEntity(id) { diff --git a/src/main/kotlin/com/example/demo/utils/PrinterService.kt b/src/main/kotlin/com/example/demo/utils/PrinterService.kt index 1862e6d..5480bc9 100644 --- a/src/main/kotlin/com/example/demo/utils/PrinterService.kt +++ b/src/main/kotlin/com/example/demo/utils/PrinterService.kt @@ -1,12 +1,12 @@ package com.example.demo.utils +import org.joda.time.DateTime +import org.joda.time.format.DateTimeFormat import java.awt.Graphics import java.awt.Graphics2D import java.awt.print.PageFormat import java.awt.print.Printable import java.awt.print.PrinterJob -import java.time.LocalDateTime -import java.time.format.DateTimeFormatter import java.util.* import javax.print.* import javax.print.attribute.HashPrintRequestAttributeSet @@ -29,6 +29,8 @@ class PrinterService : Printable { private var items: List = emptyList() private var clientName = "" + private lateinit var orderTime: DateTime + private var ticketNumber = "" override fun print(graphics: Graphics, pageFormat: PageFormat, pageIndex: Int): Int { if (pageIndex > 0) return Printable.NO_SUCH_PAGE @@ -39,16 +41,15 @@ class PrinterService : Printable { val headerMainCenter = Section(0.1f).addRows(Row("", RowType.IMAGE)) val headerMainRight = Section(0.45f).addRows(Row("MINISTRY OF PUBLIC HEALTH"), Row("CENTER REGIONAL DELEGATION"), Row("P.O. BOX: 1113 EFOULAN - YAOUNDE"), Row("TEL: +237 22 31 26 98")) - val now = LocalDateTime.now() - val dateFormatter = DateTimeFormatter.ofPattern("dd/MM/yy - HH:mm:ss", Locale.FRANCE) - val sub = Section(1f).addRows(Row("TICKET NO: 000094 OF ${dateFormatter.format(now)}")) + val dateFormatter = DateTimeFormat.forPattern("dd/MM/yy - HH:mm:ss") + val sub = Section(1f).addRows(Row("TICKET NO: $ticketNumber OF ${orderTime.toString(dateFormatter)}")) val info = Section(1f, Alignment.LEFT).addRows( Row("CLIENT: $clientName") ) - val bodyOne = Section(0.71f, Alignment.LEFT).addRows(Row("ITEM")) - val bodyTwo = Section(0.05f, Alignment.CENTER).addRows(Row("QTY")) + val bodyOne = Section(0.70f, Alignment.LEFT).addRows(Row("ITEM")) + val bodyTwo = Section(0.06f, Alignment.CENTER).addRows(Row("QTY")) val bodyThree = Section(0.12f, Alignment.RIGHT).addRows(Row("PRICE")) val bodyFour = Section(0.12f, Alignment.RIGHT).addRows(Row("AMOUNT")) items.forEach { @@ -76,9 +77,11 @@ class PrinterService : Printable { return Printable.PAGE_EXISTS } - fun printReceipt(items: List, clientName: String) { + fun printReceipt(items: List, clientName: String, ticketNumber: String, orderTime: DateTime) { this.items = items this.clientName = clientName + this.ticketNumber = ticketNumber + this.orderTime = orderTime //val flavor: DocFlavor = DocFlavor.BYTE_ARRAY.AUTOSENSE //val pRas: PrintRequestAttributeSet = HashPrintRequestAttributeSet() @@ -112,9 +115,9 @@ class PrinterService : Printable { private fun getPageFormat(job: PrinterJob, bodyHeight: Int): PageFormat { val pf = job.defaultPage() - val height = 29.7.cm.toDouble()//(headerHeight + bodyHeight + footerHeight).cm.toDouble() - val width = 21.cm.toDouble()//7.6.cm.toDouble() - val margin = 1.5.cm.toDouble()//0.3.cm.toDouble() + val height = pf.height + val width = pf.width + val margin = 1.5.cm.toDouble() val paper = with(pf.paper) { setSize(width, height) diff --git a/src/main/kotlin/com/example/demo/utils/Receipt.kt b/src/main/kotlin/com/example/demo/utils/Receipt.kt index 0940cb8..76584b5 100644 --- a/src/main/kotlin/com/example/demo/utils/Receipt.kt +++ b/src/main/kotlin/com/example/demo/utils/Receipt.kt @@ -43,6 +43,10 @@ class Receipt( // Calculate the end x position of the section // val sectionEnd = sectionStart + (section.weight * pageWidth) + // + // Calculate the section width + // + val sectionWidth = sectionEnd - sectionStart if (container.isTable) { graphics2D.drawRect(sectionEnd.toInt(), (yPos - fontMetrics.height).toInt(), sectionEnd.toInt(), tableHeight) @@ -63,26 +67,31 @@ class Receipt( graphics2D.font = Font(Font.SANS_SERIF, Font.BOLD, 10) graphics2D.drawLine(sectionStart.toInt(), (rowY - fontMetrics.height).toInt(), sectionEnd.toInt(), (rowY - fontMetrics.height).toInt()) } -// + // // Calculate the text width // - val textWidth = fontMetrics.stringWidth(row.label) + var textWidth = fontMetrics.stringWidth(row.label) // // Check alignment // val rowX = when (section.alignment) { Alignment.RIGHT -> if (container.isTable) sectionEnd - textWidth - 6 else sectionEnd - textWidth - Alignment.CENTER -> ((sectionEnd - sectionStart) / 2f + sectionStart) - (textWidth / 2f) + Alignment.CENTER -> (sectionWidth / 2f + sectionStart) - (textWidth / 2f) else -> if (container.isTable) sectionStart + 6 else sectionStart } // // draw the text // if (row.type == RowType.TEXT) { - graphics2D.drawString(row.label, rowX, rowY) + var text = row.label + while (textWidth >= sectionWidth - 2.cm && sectionWidth > 3.cm) { + text = text.dropLast(4) + textWidth = fontMetrics.stringWidth(text) + } + graphics2D.drawString(text, rowX, rowY) } else { - val icon = ImageIcon(javaClass.classLoader.getResource("header.png")?.path?.replace("/", "\\\\")) - val width = (sectionEnd - sectionStart).toInt() + val icon = ImageIcon(javaClass.classLoader.getResource("header.png")?.path) + val width = sectionWidth.toInt() val height = (lineHeight * container.maxRowCount()).toInt() val imgX = (sectionEnd - width).toInt() graphics2D.drawImage(icon.image, imgX, (yPos - (lineHeight / 2)).toInt(), width, height, null)