269 lines
9.1 KiB
Kotlin
269 lines
9.1 KiB
Kotlin
package com.restapi.controllers
|
|
|
|
import com.restapi.domain.*
|
|
import io.javalin.http.Context
|
|
import io.javalin.http.HttpStatus
|
|
import io.javalin.http.NotFoundResponse
|
|
import io.javalin.http.bodyAsClass
|
|
import java.io.FileInputStream
|
|
|
|
object PurchaseOrderCtrl {
|
|
|
|
fun getNextNum(ctx: Context) {
|
|
val prefix = "PO/"
|
|
val cnt = (Session.database.find(PurchaseOrder::class.java)
|
|
.findCount() + 1)
|
|
.toString()
|
|
.padStart(6, '0')
|
|
val seq = SequenceNumber(prefix + cnt)
|
|
ctx.json(seq).status(HttpStatus.OK)
|
|
}
|
|
|
|
fun get(ctx: Context) {
|
|
val id = ctx.pathParam("id")
|
|
val po = Session.database.find(PurchaseOrder::class.java, id) ?: throw NotFoundResponse("po not found for $id")
|
|
ctx.json(po).status(HttpStatus.OK)
|
|
}
|
|
|
|
data class PoFilterQuery(val common: CommonFilters, val poFilters: POFilters)
|
|
|
|
fun getAll(ctx: Context) {
|
|
val filters = ctx.bodyAsClass<PoFilterQuery>()
|
|
val pos = searchPos(filters.common, filters.poFilters)
|
|
val excel = ctx.queryParam("excel")
|
|
if (excel != null) {
|
|
exportPos(pos)
|
|
val inputStream = FileInputStream("./excel/Pos.xls")
|
|
ctx.result(inputStream).status(HttpStatus.OK)
|
|
} else {
|
|
ctx.json(pos).status(HttpStatus.OK)
|
|
}
|
|
}
|
|
|
|
fun create(ctx: Context) {
|
|
val po = ctx.bodyAsClass<PurchaseOrder>()
|
|
val prods = po.products
|
|
if (prods.isEmpty()) {
|
|
ctx.json(mapOf("error" to "empty product list")).status(HttpStatus.BAD_REQUEST)
|
|
return
|
|
}
|
|
Session.database.save(po)
|
|
ctx.json(po).status(HttpStatus.CREATED)
|
|
}
|
|
|
|
fun createBatch(ctx: Context) {
|
|
val pos = ctx.bodyAsClass<List<PurchaseOrder>>()
|
|
val txn = Session.database.beginTransaction()
|
|
try {
|
|
txn.isBatchMode = true
|
|
for (po in pos) Session.database.save(po)
|
|
txn.commit()
|
|
ctx.status(HttpStatus.CREATED).result("POS Created")
|
|
} catch (e: Exception) {
|
|
txn.rollback()
|
|
ctx.status(HttpStatus.INTERNAL_SERVER_ERROR).result("Pos Creation failed" + e.message)
|
|
} finally {
|
|
txn.end()
|
|
}
|
|
ctx.result("pos batch created").status(HttpStatus.CREATED)
|
|
}
|
|
|
|
fun approve(ctx: Context) {
|
|
val id = ctx.pathParam("id")
|
|
val po = Session.database.find(PurchaseOrder::class.java, id) ?: throw NotFoundResponse("po not found for $id")
|
|
po.approvalStatus = ApprovalStatus.APPROVED
|
|
po.save()
|
|
ctx.json(po).status(HttpStatus.CREATED)
|
|
//reject all other pos pertaining to the same tx ??
|
|
}
|
|
|
|
fun reject(ctx: Context) {
|
|
val id = ctx.pathParam("id")
|
|
val po = Session.database.find(PurchaseOrder::class.java, id) ?: throw NotFoundResponse("po not found for $id")
|
|
po.apply {
|
|
approvalStatus = ApprovalStatus.REJECTED
|
|
save()
|
|
}
|
|
ctx.json(po).status(HttpStatus.CREATED)
|
|
}
|
|
|
|
fun quoteReference(ctx: Context) {
|
|
//gets the quote reference on which this po is based on
|
|
val id = ctx.pathParam("id")
|
|
val quote = Session.database.find(Quotation::class.java)
|
|
.where()
|
|
.eq("referenceQuotation", id)
|
|
?: throw NotFoundResponse("reference quotation not found for po $id")
|
|
ctx.json(quote)
|
|
}
|
|
|
|
fun update(ctx: Context) {
|
|
val id = ctx.pathParam("id").toLong()
|
|
val po = Session.database.find(PurchaseOrder::class.java, id) ?: throw NotFoundResponse("po not found for $id")
|
|
val updatedPo = ctx.bodyAsClass<PurchaseOrder>()
|
|
po.patchValues(updatedPo)
|
|
po.update()
|
|
ctx.json(po).status(HttpStatus.OK)
|
|
}
|
|
|
|
fun delete(ctx: Context) {
|
|
val id = ctx.pathParam("id")
|
|
val po = Session.database.find(PurchaseOrder::class.java, id) ?: throw NotFoundResponse("no po found with id $id")
|
|
Session.database.delete(po)
|
|
ctx.status(HttpStatus.OK)
|
|
}
|
|
}
|
|
|
|
object QuotationCtrl {
|
|
fun getNextNum(ctx: Context) {
|
|
val prefix = "QUOTE/"
|
|
val cnt = Session.database.find(Quotation::class.java)
|
|
.findCount()
|
|
.toString()
|
|
.padStart(6, '0')
|
|
val seq = SequenceNumber(prefix + cnt)
|
|
ctx.json(seq).status(HttpStatus.OK)
|
|
}
|
|
|
|
fun get(ctx: Context) {
|
|
val id = ctx.pathParam("id")
|
|
val quote = Session.database.find(Quotation::class.java, id) ?: throw NotFoundResponse("quote not found for $id")
|
|
ctx.status(HttpStatus.OK)
|
|
ctx.json(quote)
|
|
}
|
|
|
|
data class QF(val common: CommonFilters, val quoteFilters: QuoteFilters)
|
|
|
|
fun getAll(ctx: Context) {
|
|
val filters = ctx.bodyAsClass<QF>()
|
|
val excel: String? = ctx.queryParam("excel")
|
|
val quotes = searchQuotes(filters.common, filters.quoteFilters)
|
|
if (excel != null) {
|
|
exportQuotations(quotes)
|
|
val inputStream = FileInputStream("./excel/Quotes.xls")
|
|
ctx.result(inputStream).status(HttpStatus.OK)
|
|
} else {
|
|
ctx.json(quotes).status(HttpStatus.OK)
|
|
}
|
|
}
|
|
|
|
fun create(ctx: Context) {
|
|
val quote = ctx.bodyAsClass<Quotation>()
|
|
//validation
|
|
val prods = quote.products
|
|
if (prods.isEmpty()) {
|
|
ctx.json(mapOf("error" to "empty product list")).status(HttpStatus.BAD_REQUEST)
|
|
return
|
|
}
|
|
Session.database.save(quote)
|
|
ctx.json(quote).status(HttpStatus.CREATED)
|
|
}
|
|
|
|
fun createBatch(ctx: Context) {
|
|
val quotes = ctx.bodyAsClass<List<Quotation>>()
|
|
val txn = Session.database.beginTransaction()
|
|
try {
|
|
txn.isBatchMode = true
|
|
for (quote in quotes) Session.database.save(quote)
|
|
txn.commit()
|
|
ctx.status(HttpStatus.CREATED).result("Quotes Created")
|
|
} catch (e: Exception) {
|
|
txn.rollback()
|
|
ctx.status(HttpStatus.INTERNAL_SERVER_ERROR).result("Quotes Creation failed" + e.message)
|
|
} finally {
|
|
txn.end()
|
|
}
|
|
ctx.result("Quotes batch created").status(HttpStatus.CREATED)
|
|
}
|
|
|
|
fun delete(ctx: Context) {
|
|
val id = ctx.pathParam("id")
|
|
val quote = Session.database.find(Quotation::class.java, id) ?: throw NotFoundResponse("no quote found with id $id")
|
|
Session.database.delete(quote)
|
|
ctx.status(HttpStatus.OK)
|
|
}
|
|
|
|
fun update(ctx: Context) {
|
|
val id = ctx.pathParam("id").toLong()
|
|
val quote = Session.database.find(Quotation::class.java, id) ?: throw NotFoundResponse("quote not found for $id")
|
|
val updatedQuote = ctx.bodyAsClass<Quotation>()
|
|
quote.patchValues(updatedQuote)
|
|
quote.update()
|
|
ctx.json(quote).status(HttpStatus.OK)
|
|
}
|
|
}
|
|
|
|
object RequestForQuote {
|
|
fun create(ctx: Context) {
|
|
val rfq = ctx.bodyAsClass<ReqForQuote>()
|
|
Session.database.save(rfq)
|
|
//ctx.result("request for quote created")
|
|
//ctx.json(rfq)
|
|
ctx.status(HttpStatus.CREATED)
|
|
ctx.json("asss")
|
|
}
|
|
|
|
fun get(ctx: Context) {
|
|
val id = ctx.pathParam("id")
|
|
val rfq = Session.database.find(ReqForQuote::class.java, id)
|
|
?: throw NotFoundResponse("request for quote not found for id $id")
|
|
ctx.status(HttpStatus.OK)
|
|
ctx.json(rfq)
|
|
}
|
|
|
|
fun update(ctx: Context) {
|
|
//shuld we compare the new body fields with preexisting ones and prepare a sql query to update those fields??
|
|
|
|
}
|
|
}
|
|
|
|
object InvoiceCtrl {
|
|
fun create(ctx: Context) {
|
|
val invoice = ctx.bodyAsClass<Invoice>()
|
|
Session.database.save(invoice)
|
|
ctx.json(invoice).status(HttpStatus.CREATED)
|
|
}
|
|
|
|
fun get(ctx: Context) {
|
|
val id = ctx.pathParam("id").toLong()
|
|
val invoice = Session.database.find(Invoice::class.java, id)
|
|
?: throw NotFoundResponse("No invoice found with id $id")
|
|
ctx.json(invoice).status(HttpStatus.OK)
|
|
}
|
|
|
|
data class INVF(val common: CommonFilters, val invoiceFilters: InvoiceFilters)
|
|
|
|
fun getAll(ctx: Context) {
|
|
val filters = ctx.bodyAsClass<INVF>()
|
|
val invoices = searchInvoices(filters.common, filters.invoiceFilters)
|
|
val excel = ctx.queryParam("excel")
|
|
if (excel !== null) {
|
|
exportInvoices(invoices)
|
|
val inputStream = FileInputStream("./excel/Invoices.xls")
|
|
ctx.result(inputStream).status(HttpStatus.OK)
|
|
} else {
|
|
ctx.json(invoices).status(HttpStatus.OK)
|
|
}
|
|
|
|
}
|
|
|
|
fun update(ctx: Context) {
|
|
val id = ctx.pathParam("id").toLong()
|
|
val invoice =
|
|
Session.database.find(Invoice::class.java, id) ?: throw NotFoundResponse("invoice not found for $id")
|
|
val updatedPayment = ctx.bodyAsClass<Invoice>()
|
|
invoice.patchValues(updatedPayment)
|
|
invoice.update()
|
|
ctx.json(invoice).status(HttpStatus.OK)
|
|
}
|
|
|
|
fun getNextNum(ctx: Context) {
|
|
val prefix = "INV/"
|
|
val cnt = Session.database.find(Invoice::class.java)
|
|
.findCount()
|
|
.toString()
|
|
.padStart(6, '0')
|
|
val seq = SequenceNumber(prefix + cnt)
|
|
ctx.json(seq).status(HttpStatus.OK)
|
|
}
|
|
} |