diff --git a/src/main/kotlin/com/restapi/controllers/ProductCtrl.kt b/src/main/kotlin/com/restapi/controllers/ProductCtrl.kt index 9b55283..2fd8e17 100644 --- a/src/main/kotlin/com/restapi/controllers/ProductCtrl.kt +++ b/src/main/kotlin/com/restapi/controllers/ProductCtrl.kt @@ -19,27 +19,29 @@ object ProductCtrl { } data class PF(val common: CommonFilters, val productFilters: ProductFilters) - data class GetPrice(val productId: Long, val vendor: Any) - fun getPrice(ctx: Context){ + data class GetPrice(val productId: Long, val vendor: Long, val po: Long?) + + fun getPrice(ctx: Context) { val gp = ctx.bodyAsClass() val vendor = Session.database.find(Vendor::class.java, gp.vendor) ?: throw BadRequestResponse("vendor not found for ${gp.vendor}") val product = Session.database.find(Product::class.java, gp.productId) ?: throw BadRequestResponse("product not found for ${gp.productId}") - val poProduct = Session.database.find(PurchaseOrder::class.java) - .where() - .eq("vendor", vendor) - .findList() - .flatMap { - it.products - } - .firstOrNull { - it.productId == product.sysPk - } + val poProduct = if (gp.po != null) { + Session.database.find(PurchaseOrder::class.java, gp.po)?.products?.firstOrNull { it.productId == product.sysPk } + } else { + Session.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") + poProduct ?: throw BadRequestResponse("price not found for this vendor and product and po") ) } + fun getAll(ctx: Context) { val filters = ctx.bodyAsClass() val prods = searchProducts(filters.common, filters.productFilters) diff --git a/src/main/kotlin/com/restapi/domain/models.kt b/src/main/kotlin/com/restapi/domain/models.kt index 4b810c1..0f516ea 100644 --- a/src/main/kotlin/com/restapi/domain/models.kt +++ b/src/main/kotlin/com/restapi/domain/models.kt @@ -24,6 +24,7 @@ data class POProducts( val totalValue: Double = 0.0, val description: String = "", val uom: String = "", + val poId: Long? = null )