diff --git a/src/main/kotlin/com/restapi/controllers/Entities.kt b/src/main/kotlin/com/restapi/controllers/Entities.kt index fd9eab9..d179edf 100644 --- a/src/main/kotlin/com/restapi/controllers/Entities.kt +++ b/src/main/kotlin/com/restapi/controllers/Entities.kt @@ -373,112 +373,6 @@ object Entities { } data class SequenceNumber(val number: String) -object PurchaseOrderCtrl { - - fun getNextNum(ctx: Context) { - val prefix = "PO/" - val cnt = (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 = database.find(PurchaseOrder::class.java, id) ?: throw NotFoundResponse("po not found for $id") - ctx.json(po).status(HttpStatus.OK) - } - - data class PF(val common: CommonFilters, val poFilters: POFilters) - - fun getAll(ctx: Context) { - val filters = ctx.bodyAsClass() - 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() - val prods = po.products - if (prods.isEmpty()) { - ctx.json(mapOf("error" to "empty product list")).status(HttpStatus.BAD_REQUEST) - return - } - database.save(po) - ctx.json(po).status(HttpStatus.CREATED) - } - - fun createBatch(ctx: Context) { - val pos = ctx.bodyAsClass>() - val txn = database.beginTransaction() - try { - txn.isBatchMode = true - for (po in pos) 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 = 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 = 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 = 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 = database.find(PurchaseOrder::class.java, id) ?: throw NotFoundResponse("po not found for $id") - val updatedPo = ctx.bodyAsClass() - po.patchValues(updatedPo) - po.update() - ctx.json(po).status(HttpStatus.OK) - } - - fun delete(ctx: Context) { - val id = ctx.pathParam("id") - val po = database.find(PurchaseOrder::class.java, id) ?: throw NotFoundResponse("no po found with id $id") - database.delete(po) - ctx.status(HttpStatus.OK) - } -} object ProductCtrl { fun get(ctx: Context) { @@ -590,85 +484,6 @@ object ProductCtrl { } -object QuotationCtrl { - fun getNextNum(ctx: Context) { - val prefix = "QUOTE/" - val cnt = 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 = 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() - 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() - //validation - val prods = quote.products - if (prods.isEmpty()) { - ctx.json(mapOf("error" to "empty product list")).status(HttpStatus.BAD_REQUEST) - return - } - database.save(quote) - ctx.json(quote).status(HttpStatus.CREATED) - } - - fun createBatch(ctx: Context) { - val quotes = ctx.bodyAsClass>() - val txn = database.beginTransaction() - try { - txn.isBatchMode = true - for (quote in quotes) 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 = database.find(Quotation::class.java, id) ?: throw NotFoundResponse("no quote found with id $id") - database.delete(quote) - ctx.status(HttpStatus.OK) - } - - fun update(ctx: Context) { - val id = ctx.pathParam("id").toLong() - val quote = database.find(Quotation::class.java, id) ?: throw NotFoundResponse("quote not found for $id") - val updatedQuote = ctx.bodyAsClass() - quote.patchValues(updatedQuote) - quote.update() - ctx.json(quote).status(HttpStatus.OK) - } -} - object DocumentCtrl { fun get(ctx: Context) { val id = ctx.pathParam("id") @@ -793,30 +608,6 @@ object VendorCtrl { } } -object RequestForQuote { - fun create(ctx: Context) { - val rfq = ctx.bodyAsClass() - 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 = 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 PaymentCtrl { fun create(ctx: Context) { val pmt = ctx.bodyAsClass(Payment::class.java) @@ -902,54 +693,3 @@ object PaymentCtrl { } } -object InvoiceCtrl { - fun create(ctx: Context) { - val invoice = ctx.bodyAsClass() - database.save(invoice) - ctx.json(invoice).status(HttpStatus.CREATED) - } - - fun get(ctx: Context) { - val id = ctx.pathParam("id").toLong() - val invoice = 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() - 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 = - database.find(Invoice::class.java, id) ?: throw NotFoundResponse("invoice not found for $id") - val updatedPayment = ctx.bodyAsClass() - invoice.patchValues(updatedPayment) - invoice.update() - ctx.json(invoice).status(HttpStatus.OK) - } - - fun getNextNum(ctx: Context) { - val prefix = "INV/" - val cnt = database.find(Invoice::class.java) - .findCount() - .toString() - .padStart(6, '0') - val seq = SequenceNumber(prefix + cnt) - ctx.json(seq).status(HttpStatus.OK) - } -} - diff --git a/src/main/kotlin/com/restapi/controllers/SaleOrder.kt b/src/main/kotlin/com/restapi/controllers/SaleOrder.kt new file mode 100644 index 0000000..cf71876 --- /dev/null +++ b/src/main/kotlin/com/restapi/controllers/SaleOrder.kt @@ -0,0 +1,269 @@ +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 PF(val common: CommonFilters, val poFilters: POFilters) + + fun getAll(ctx: Context) { + val filters = ctx.bodyAsClass() + 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() + 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>() + 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() + 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() + 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() + //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>() + 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() + quote.patchValues(updatedQuote) + quote.update() + ctx.json(quote).status(HttpStatus.OK) + } +} + +object RequestForQuote { + fun create(ctx: Context) { + val rfq = ctx.bodyAsClass() + 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() + 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() + 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.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) + } +} \ No newline at end of file