move around classes
This commit is contained in:
@@ -14,7 +14,6 @@ import io.ebean.CallableSql
|
||||
import io.ebean.RawSqlBuilder
|
||||
import io.javalin.http.*
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.io.FileInputStream
|
||||
import java.sql.Types
|
||||
import java.time.LocalDate
|
||||
import java.time.LocalDateTime
|
||||
@@ -374,322 +373,3 @@ object Entities {
|
||||
|
||||
data class SequenceNumber(val number: String)
|
||||
|
||||
object ProductCtrl {
|
||||
fun get(ctx: Context) {
|
||||
val id = ctx.pathParam("id")
|
||||
val product = database.find(Product::class.java)
|
||||
.where()
|
||||
.eq("sys_pk", id.toLong())
|
||||
.findOne()
|
||||
?: throw NotFoundResponse("Product not found for $id")
|
||||
ctx.json(product).status(HttpStatus.OK)
|
||||
}
|
||||
|
||||
data class PF(val common: CommonFilters, val productFilters: ProductFilters)
|
||||
data class GetPrice(val productId: Long, val vendor: Any)
|
||||
fun getPrice(ctx: Context){
|
||||
val gp = ctx.bodyAsClass<GetPrice>()
|
||||
val vendor = database.find(Vendor::class.java, gp.vendor) ?: throw BadRequestResponse("vendor not found for ${gp.vendor}")
|
||||
val product = database.find(Product::class.java, gp.productId) ?: throw BadRequestResponse("product not found for ${gp.productId}")
|
||||
val poProduct = database.find(PurchaseOrder::class.java)
|
||||
.where()
|
||||
.eq("vendor", vendor)
|
||||
.findList()
|
||||
.flatMap {
|
||||
it.products
|
||||
}
|
||||
.firstOrNull {
|
||||
it.productId == product.sysPk
|
||||
}
|
||||
|
||||
ctx.json(
|
||||
poProduct ?: throw BadRequestResponse("price not found for this vendor and product")
|
||||
)
|
||||
|
||||
}
|
||||
fun getAll(ctx: Context) {
|
||||
val filters = ctx.bodyAsClass<PF>()
|
||||
val prods = searchProducts(filters.common, filters.productFilters)
|
||||
val excel = ctx.queryParam("excel")
|
||||
if (excel != null) {
|
||||
exportProds(prods)
|
||||
val inputStream = FileInputStream("./excel/Products.xls")
|
||||
ctx.result(inputStream).status(HttpStatus.OK)
|
||||
|
||||
} else {
|
||||
ctx.json(prods).status(HttpStatus.OK)
|
||||
}
|
||||
}
|
||||
|
||||
fun create(ctx: Context) {
|
||||
val product = ctx.bodyAsClass<Product>()
|
||||
database.save(product)
|
||||
ctx.json(product).status(HttpStatus.CREATED)
|
||||
}
|
||||
|
||||
fun delete(ctx: Context) {
|
||||
val id = ctx.pathParam("id")
|
||||
val prod = database.find(Product::class.java, id) ?: throw NotFoundResponse("no product found with id $id")
|
||||
database.delete(prod)
|
||||
ctx.status(HttpStatus.OK)
|
||||
}
|
||||
|
||||
fun patch(ctx: Context) {
|
||||
val id = ctx.pathParam("id")
|
||||
val patchValues = ctx.bodyAsClass<Map<String, Any>>()
|
||||
database.beginTransaction().use {
|
||||
patchValues.entries.forEach { en ->
|
||||
val key = en.key
|
||||
val value = en.value
|
||||
database.sqlUpdate("update products set $key = ? where id = ?").apply {
|
||||
setParameter(1, value)
|
||||
setParameter(2, id)
|
||||
execute()
|
||||
}
|
||||
}
|
||||
it.commit()
|
||||
}
|
||||
}
|
||||
|
||||
fun update(ctx: Context) {
|
||||
val id = ctx.pathParam("id").toLong()
|
||||
val product = database.find(Product::class.java, id) ?: throw NotFoundResponse("product not found for $id")
|
||||
val updatedProduct = ctx.bodyAsClass<Product>()
|
||||
product.patchValues(updatedProduct)
|
||||
product.update()
|
||||
ctx.json(product).status(HttpStatus.OK)
|
||||
}
|
||||
|
||||
|
||||
@JvmStatic
|
||||
fun main(args: Array<String>) {
|
||||
|
||||
val patchValues = mapOf("name" to 1)
|
||||
val id = 1;
|
||||
database.beginTransaction().use {
|
||||
patchValues.entries.forEach { en ->
|
||||
val key = en.key
|
||||
val value = en.value
|
||||
|
||||
database.sqlUpdate("update product set $key = ? where sys_pk = ?").apply {
|
||||
setParameter(1, value)
|
||||
setParameter(2, id)
|
||||
|
||||
execute()
|
||||
}
|
||||
}
|
||||
it.commit()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
object DocumentCtrl {
|
||||
fun get(ctx: Context) {
|
||||
val id = ctx.pathParam("id")
|
||||
val doc = database.find(Document::class.java, id) ?: throw NotFoundResponse("no doc found with id $id")
|
||||
ctx.status(HttpStatus.OK)
|
||||
ctx.json(doc)
|
||||
}
|
||||
|
||||
fun create(ctx: Context) {
|
||||
val doc = ctx.bodyAsClass<Document>()
|
||||
database.save(doc)
|
||||
ctx.status(HttpStatus.CREATED)
|
||||
ctx.json(doc)
|
||||
}
|
||||
|
||||
fun print(ctx: Context) {
|
||||
//would be handled in the frontend ??
|
||||
}
|
||||
|
||||
fun delete(ctx: Context) {
|
||||
val id = ctx.pathParam("id")
|
||||
val doc = database.find(Document::class.java, id) ?: throw NotFoundResponse("no document found with id $id")
|
||||
database.delete(doc)
|
||||
ctx.status(HttpStatus.OK)
|
||||
}
|
||||
|
||||
fun getWithRefId(ctx: Context) {
|
||||
//fetches a particular doc (po, quote) with ref id
|
||||
val refId = ctx.pathParam("refId")
|
||||
val doc = database.find(Document::class.java)
|
||||
.where()
|
||||
.eq("typeOfDoc", DocType.valueOf(ctx.pathParam("type")))
|
||||
.eq("refIdOfDoc", refId)
|
||||
?: throw NotFoundResponse("no doc found for refId $refId")
|
||||
ctx.status(HttpStatus.OK)
|
||||
ctx.json(doc)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
object VendorCtrl {
|
||||
val logger = LoggerFactory.getLogger("Vendor")
|
||||
fun get(ctx: Context) {
|
||||
val id = ctx.pathParam("id").toLong()
|
||||
val vendor = database.find(Vendor::class.java, id) ?: throw NotFoundResponse("no vendor found with id $id")
|
||||
ctx.status(HttpStatus.OK)
|
||||
ctx.json(vendor)
|
||||
}
|
||||
|
||||
data class VF(val common: CommonFilters, val vendorFilters: VendorFilters)
|
||||
|
||||
fun getAll(ctx: Context) {
|
||||
val filters = ctx.bodyAsClass<VF>()
|
||||
val excel: String? = ctx.queryParam("excel")
|
||||
val vendors = searchVendors(filters.common, filters.vendorFilters)
|
||||
if (excel !== null) {
|
||||
exportVendors(vendors)
|
||||
val inputStream = FileInputStream("./excel/VendorList.xls")
|
||||
ctx.result(inputStream).status(HttpStatus.OK)
|
||||
} else {
|
||||
ctx.json(vendors).status(HttpStatus.OK)
|
||||
}
|
||||
}
|
||||
|
||||
fun createBatch(ctx: Context) {
|
||||
val vendors = ctx.bodyAsClass<List<Vendor>>()
|
||||
database.saveAll(vendors)
|
||||
}
|
||||
|
||||
fun create(ctx: Context) {
|
||||
val vendor = ctx.bodyAsClass<Vendor>()
|
||||
database.save(vendor)
|
||||
ctx.status(HttpStatus.CREATED)
|
||||
ctx.json(vendor)
|
||||
}
|
||||
|
||||
fun update(ctx: Context) {
|
||||
val id = ctx.pathParam("id").toLong()
|
||||
val vendor = database.find(Vendor::class.java, id) ?: throw NotFoundResponse("vendor not found for $id")
|
||||
val updatedVendor = ctx.bodyAsClass<Vendor>()
|
||||
vendor.patchValues(updatedVendor)
|
||||
vendor.update()
|
||||
ctx.json(vendor).status(HttpStatus.OK)
|
||||
}
|
||||
|
||||
fun delete(ctx: Context) {
|
||||
val id = ctx.pathParam("id")
|
||||
val vendor = database.find(Vendor::class.java, id) ?: throw NotFoundResponse("no vendor found with id $id")
|
||||
database.delete(vendor)
|
||||
ctx.status(HttpStatus.OK)
|
||||
}
|
||||
|
||||
fun getQuotes(ctx: Context) {
|
||||
val id = ctx.pathParam("id").toLong()
|
||||
val quotes = database.find(Quotation::class.java)
|
||||
.where()
|
||||
.eq("vendor", database.find(Vendor::class.java, id) ?: throw NotFoundResponse("vendor not found for $id"))
|
||||
.findList()
|
||||
ctx.json(quotes).status(HttpStatus.OK)
|
||||
}
|
||||
|
||||
fun getPos(ctx: Context) {
|
||||
val id = ctx.pathParam("id").toLong()
|
||||
val pos = database.find(PurchaseOrder::class.java)
|
||||
.where()
|
||||
.eq("vendor", database.find(Vendor::class.java, id) ?: throw NotFoundResponse("vendor not found for $id"))
|
||||
.findList()
|
||||
ctx.json(pos).status(HttpStatus.OK)
|
||||
}
|
||||
|
||||
fun rate(ctx: Context) {
|
||||
val id = ctx.pathParam("id")
|
||||
val rating1 = ctx.pathParam("rating").toDouble()
|
||||
|
||||
database.find(Vendor::class.java, id)?.let {
|
||||
it.rating = rating1
|
||||
it.save()
|
||||
} ?: throw NotFoundResponse("vendor not found for id $id")
|
||||
|
||||
|
||||
ctx.result("rating changed").status(HttpStatus.OK)
|
||||
}
|
||||
}
|
||||
|
||||
object PaymentCtrl {
|
||||
fun create(ctx: Context) {
|
||||
val pmt = ctx.bodyAsClass(Payment::class.java)
|
||||
//update the status of invoices pertaining to payment.vendor
|
||||
val vendors: List<Long>? = pmt.vendor?.let { listOf(it.sysPk) }
|
||||
val invoices = searchInvoices(
|
||||
CommonFilters(sortBy = "date", sortAsc = true, vendor = vendors),
|
||||
InvoiceFilters(status = InvoiceStatus.PAID_FULL)
|
||||
)
|
||||
// println(invoices)
|
||||
//pmt.invoicesAffected = mutableMapOf()
|
||||
var totalAmount = pmt.amount
|
||||
var totalDeduct = 0.0
|
||||
for (invoice in invoices) {
|
||||
val deduct = Math.min(totalAmount, invoice.totalAmount)
|
||||
invoice.totalAmount -= deduct
|
||||
totalDeduct += deduct
|
||||
totalAmount -= deduct
|
||||
if (invoice.totalAmount <= 0.0) {
|
||||
invoice.status = InvoiceStatus.PAID_FULL
|
||||
} else {
|
||||
invoice.status = InvoiceStatus.PAID_SOME
|
||||
}
|
||||
//println(invoice)
|
||||
database.update(invoice)
|
||||
pmt.invoicesAffected?.put(invoice.sysPk, deduct)
|
||||
println(pmt.invoicesAffected)
|
||||
if (totalAmount <= 0.0) break
|
||||
}
|
||||
pmt.amountDeducted = totalDeduct
|
||||
pmt.excessAmount = pmt.amount - pmt.amountDeducted!!
|
||||
database.save(pmt)
|
||||
ctx.json(pmt).status(HttpStatus.CREATED)
|
||||
}
|
||||
|
||||
fun delete(ctx: Context) {
|
||||
val id = ctx.pathParam("id").toLong()
|
||||
val pmt = database.find(Payment::class.java, id)
|
||||
?: throw NotFoundResponse("No payment found for this id $id")
|
||||
val invoiceDeductMap = pmt.invoicesAffected
|
||||
for (entry in invoiceDeductMap?.entries!!.iterator()) {
|
||||
val inv = database.find(Invoice::class.java, entry.key)
|
||||
?: throw NotFoundResponse("No invoice found for $entry.key")
|
||||
inv.totalAmount += entry.value
|
||||
inv.status = InvoiceStatus.PAID_SOME
|
||||
database.update(inv)
|
||||
}
|
||||
database.delete(pmt)
|
||||
ctx.json(pmt).status(HttpStatus.OK)
|
||||
}
|
||||
|
||||
fun get(ctx: Context) {
|
||||
val id = ctx.pathParam("id")
|
||||
val pmt = database.find(Payment::class.java, id)
|
||||
?: throw NotFoundResponse("No payment found for this id $id")
|
||||
ctx.json(pmt).status(HttpStatus.OK)
|
||||
}
|
||||
|
||||
data class PMTF(val common: CommonFilters, val paymentFilters: PaymentFilters)
|
||||
|
||||
fun getAll(ctx: Context) {
|
||||
val filters = ctx.bodyAsClass<PMTF>()
|
||||
val payments = searchPayments(filters.common, filters.paymentFilters)
|
||||
// println(payments)
|
||||
val excel = ctx.queryParam("excel")
|
||||
if (excel !== null) {
|
||||
exportPayments(payments)
|
||||
val inputStream = FileInputStream("./excel/Payments.xls")
|
||||
ctx.result(inputStream).status(HttpStatus.OK)
|
||||
} else {
|
||||
ctx.json(payments).status(HttpStatus.OK)
|
||||
}
|
||||
}
|
||||
|
||||
fun update(ctx: Context) {
|
||||
val id = ctx.pathParam("id").toLong()
|
||||
val pmt =
|
||||
database.find(Payment::class.java, id) ?: throw NotFoundResponse("payment not found for $id")
|
||||
val updatedPayment = ctx.bodyAsClass<Payment>()
|
||||
pmt.patchValues(updatedPayment)
|
||||
pmt.update()
|
||||
ctx.json(pmt).status(HttpStatus.OK)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user