From 1e7db629aed299c250ea0498c7d26026905ec99c Mon Sep 17 00:00:00 2001 From: gowthaman Date: Tue, 11 Jun 2024 17:02:16 +0530 Subject: [PATCH] keep track of PO --- .../com/restapi/controllers/Inventories.kt | 70 ++++++++++++++----- src/main/kotlin/com/restapi/domain/models.kt | 20 +++--- src/main/resources/logback.xml | 2 +- 3 files changed, 67 insertions(+), 25 deletions(-) diff --git a/src/main/kotlin/com/restapi/controllers/Inventories.kt b/src/main/kotlin/com/restapi/controllers/Inventories.kt index 7e54754..44ac630 100644 --- a/src/main/kotlin/com/restapi/controllers/Inventories.kt +++ b/src/main/kotlin/com/restapi/controllers/Inventories.kt @@ -1,9 +1,8 @@ package com.restapi.controllers -import com.restapi.domain.IncomingInventory -import com.restapi.domain.OutgoingInventory -import com.restapi.domain.Plant -import com.restapi.domain.Session +import com.restapi.domain.* +import com.restapi.domain.Session.database +import com.restapi.integ.logger import io.javalin.http.* import java.io.FileInputStream @@ -16,8 +15,9 @@ object IncomingInventoryCtrl { fun updatePlant(ctx: Context) { val p = ctx.bodyAsClass() - val plant = Session.database.find(Plant::class.java, ctx.pathParam("id")) - plant.patch(p) + database.find(Plant::class.java, ctx.pathParam("id"))?.let { + it.patch(p) + } ctx.json( Session.currentUserPlants() ) @@ -25,23 +25,61 @@ object IncomingInventoryCtrl { fun create(ctx: Context) { val ticket = ctx.bodyAsClass() - Session.database.save(ticket) + database.save(ticket) + keepTrackOfPoQty(ticket) ctx.json(ticket).status(HttpStatus.CREATED) } fun update(ctx: Context) { val id = ctx.pathParam("id").toLong() val ticket = - Session.database.find(IncomingInventory::class.java, id) ?: throw NotFoundResponse("quote not found for $id") + database.find(IncomingInventory::class.java, id) ?: throw NotFoundResponse("quote not found for $id") val updatedTicket = ctx.bodyAsClass() ticket.patchValues(updatedTicket) ticket.update() + keepTrackOfPoQty(ticket) ctx.json(ticket).status(HttpStatus.OK) } + private fun keepTrackOfPoQty(ticket: IncomingInventory) { + ticket.products?.forEach { incomingProduct -> + if (incomingProduct.poId != null) { + database.find(PurchaseOrder::class.java, incomingProduct.poId)?.let { po -> + logger.warn("found po for ${incomingProduct.poId} => ${po.poNum}") + po.products.forEach { poProduct -> + if (poProduct.productId == incomingProduct.productId) { + logger.warn("for ${ticket.mrn} , product ${poProduct.productId} add to po-qty ${incomingProduct.receivedQty}") + //remove existing entry for same mrn and product id + poProduct.receivedVoucher.removeIf { + it.productId == incomingProduct.productId + && it.plantId == ticket.unloadingPlantId + && it.ticket == ticket.mrn + } + poProduct.receivedVoucher.add( + ReceivedVoucher( + ticket = ticket.mrn!!, + productName = poProduct.productName, + productId = poProduct.productId, + qty = incomingProduct.receivedQty, + plantId = ticket.unloadingPlantId!!, + ticketDate = ticket.date!! + ) + ) + } else { + logger.warn("no match for ${poProduct.productId} and ${incomingProduct.productId}") + } + } + po.update() + } + } else { + logger.warn("poId is missing for $incomingProduct") + } + } + } + fun get(ctx: Context) { val id = ctx.pathParam("id").toLong() - val ticket = Session.database.find(IncomingInventory::class.java, id) + val ticket = database.find(IncomingInventory::class.java, id) ?: throw NotFoundResponse("No incoming inventory ticket found with id $id") ctx.json(ticket).status(HttpStatus.OK) } @@ -64,13 +102,13 @@ object IncomingInventoryCtrl { fun getNextNum(ctx: Context) { val prefix = "MRN/" val plantId = ctx.queryParam("plantId") ?: throw BadRequestResponse("plantId not sent") - val plant = Session.database.find(Plant::class.java) + val plant = database.find(Plant::class.java) .where() .eq("plantId", plantId) .findOne() ?: throw BadRequestResponse("plant missing for $plantId") val inventoryPrefix = plant.prefixes?.get("INBOUND") ?: prefix - val cnt = (Session.database.find(IncomingInventory::class.java) + val cnt = (database.find(IncomingInventory::class.java) .where() .eq("unloadingPlantId", plantId) .findCount() + 1) @@ -84,14 +122,14 @@ object IncomingInventoryCtrl { object OutgoingInventoryCtrl { fun create(ctx: Context) { val ticket = ctx.bodyAsClass() - Session.database.save(ticket) + database.save(ticket) ctx.json(ticket).status(HttpStatus.CREATED) } fun update(ctx: Context) { val id = ctx.pathParam("id").toLong() val ticket = - Session.database.find(OutgoingInventory::class.java, id) ?: throw NotFoundResponse("quote not found for $id") + database.find(OutgoingInventory::class.java, id) ?: throw NotFoundResponse("quote not found for $id") val updatedTicket = ctx.bodyAsClass() ticket.patchValues(updatedTicket) ticket.update() @@ -100,7 +138,7 @@ object OutgoingInventoryCtrl { fun get(ctx: Context) { val id = ctx.pathParam("id").toLong() - val ticket = Session.database.find(OutgoingInventory::class.java, id) + val ticket = database.find(OutgoingInventory::class.java, id) ?: throw NotFoundResponse("No incoming inventory ticket found with id $id") ctx.json(ticket).status(HttpStatus.OK) } @@ -123,13 +161,13 @@ object OutgoingInventoryCtrl { fun getNextNum(ctx: Context) { val prefix = "MDN/" val plantId = ctx.queryParam("plantId") ?: throw BadRequestResponse("plantId not sent") - val plant = Session.database.find(Plant::class.java) + val plant = database.find(Plant::class.java) .where() .eq("plantId", plantId) .findOne() ?: throw BadRequestResponse("plant missing for $plantId") val inventoryPrefix = plant.prefixes?.get("OUTBOUND") ?: prefix - val cnt = (Session.database.find(OutgoingInventory::class.java) + val cnt = (database.find(OutgoingInventory::class.java) .where() .eq("unloadingPlantId", plantId) .findCount() + 1) diff --git a/src/main/kotlin/com/restapi/domain/models.kt b/src/main/kotlin/com/restapi/domain/models.kt index 0f516ea..f0712ef 100644 --- a/src/main/kotlin/com/restapi/domain/models.kt +++ b/src/main/kotlin/com/restapi/domain/models.kt @@ -9,15 +9,18 @@ import io.ebean.annotation.* import io.ebean.annotation.Index import java.time.LocalDate import java.time.LocalDateTime -import java.util.* import javax.persistence.* data class Comments(val text: String = "", val by: String = "", val at: LocalDateTime = LocalDateTime.now()) +data class ReceivedVoucher(val ticket: String, val productName: String, val productId: Long, val qty: Double, val plantId: String, val ticketDate: LocalDate) data class POProducts( val productId: Long = 0, val productName: String = "", val unitPrice: Double = 0.0, val quantity: Double = 0.0, + val orderedQty: Double = 0.0, + val receivedQty: Double = 0.0, + val receivedVoucher: MutableList = arrayListOf(), val billQty: Double = 0.0, val gstPct: Double = 0.0, val taxableValue: Double = 0.0, @@ -290,13 +293,14 @@ open class Plant : BaseModel() { var prefixes: MutableMap? = mutableMapOf() } - class RefreshHistory { - var oldAt: String = "" - var oldExpiryAt: String = "" - var newAt: String = "" - var newExpiryAt: String = "" - var createdAt: String = "" - } +class RefreshHistory { + var oldAt: String = "" + var oldExpiryAt: String = "" + var newAt: String = "" + var newExpiryAt: String = "" + var createdAt: String = "" +} + @Entity open class AuthTokenCache : BaseModel() { @Column(columnDefinition = "text") diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml index c338f0b..a1b4376 100644 --- a/src/main/resources/logback.xml +++ b/src/main/resources/logback.xml @@ -21,7 +21,7 @@ - +