Skip to content

Commit

Permalink
added date and ticket number to printed receipt
Browse files Browse the repository at this point in the history
  • Loading branch information
forntoh committed Jun 23, 2020
1 parent aaf6ec9 commit 999016a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class OrderController : Controller() {
var orderItems: ObservableList<OrderItemModel> 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)
Expand All @@ -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
)
}

Expand Down
6 changes: 2 additions & 4 deletions src/main/kotlin/com/example/demo/data/model/ActeDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.*

Expand All @@ -17,13 +17,11 @@ import tornadofx.*
* @version 1.0
* @created 12-Jun-2020 11:17:28 AM
*/
object ActesTbl : IdTable<Int>() {
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<Int>) : IntEntity(id) {
Expand Down
25 changes: 14 additions & 11 deletions src/main/kotlin/com/example/demo/utils/PrinterService.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -29,6 +29,8 @@ class PrinterService : Printable {

private var items: List<Item> = 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
Expand All @@ -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 {
Expand Down Expand Up @@ -76,9 +77,11 @@ class PrinterService : Printable {
return Printable.PAGE_EXISTS
}

fun printReceipt(items: List<Item>, clientName: String) {
fun printReceipt(items: List<Item>, 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()
Expand Down Expand Up @@ -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)
Expand Down
21 changes: 15 additions & 6 deletions src/main/kotlin/com/example/demo/utils/Receipt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 999016a

Please sign in to comment.