add filters
This commit is contained in:
parent
20f6abf3b7
commit
cfdb792aa5
138
src/main/kotlin/com/restapi/controllers/Filters.kt
Normal file
138
src/main/kotlin/com/restapi/controllers/Filters.kt
Normal file
@ -0,0 +1,138 @@
|
||||
package com.restapi.controllers
|
||||
|
||||
import com.restapi.domain.DocType
|
||||
import com.restapi.domain.PurchaseOrder
|
||||
import com.restapi.domain.Quotation
|
||||
import com.restapi.domain.ReqForQuote
|
||||
import com.restapi.domain.UOM
|
||||
import java.time.LocalDate
|
||||
import com.restapi.domain.Session.database
|
||||
|
||||
//constants
|
||||
const val IGNORE = "%"
|
||||
val baseDate :LocalDate = LocalDate.MIN
|
||||
val maxDate :LocalDate = LocalDate.MAX
|
||||
const val RATING_MAX = 10.0
|
||||
const val RATING_MIN = 0.0
|
||||
//common filters would be used by most of the handlers
|
||||
//require a list of vendor ids to be passed
|
||||
data class CommonFilters (
|
||||
val fromDate :LocalDate = baseDate,
|
||||
val toDate :LocalDate = maxDate,
|
||||
val vendor :List<Long>? = null,
|
||||
val sortAsc :Boolean = true,
|
||||
val sortBy :String = IGNORE
|
||||
)
|
||||
data class POFilters (
|
||||
val poNumLike :String = IGNORE,
|
||||
val totalAmountExceeds :Long = Long.MIN_VALUE,
|
||||
val totalAmountLessThan :Long = Long.MAX_VALUE,
|
||||
val validAfter: LocalDate = baseDate,
|
||||
val validBefore: LocalDate = maxDate,
|
||||
val refQuotation :String = IGNORE,
|
||||
)
|
||||
data class ProductFilters (
|
||||
val nameLike :String = IGNORE,
|
||||
val hsnLike :String = IGNORE,
|
||||
val uom :UOM = UOM.ALL,
|
||||
)
|
||||
data class DocumentFilters (
|
||||
val nameLike :String = IGNORE,
|
||||
val typeOfDoc :DocType = DocType.ALL,
|
||||
)
|
||||
data class RFQFilters (
|
||||
val validBefore :LocalDate = maxDate,
|
||||
val validAfter :LocalDate = baseDate,
|
||||
val reqForQuoteNumLike :String = IGNORE,
|
||||
)
|
||||
data class QuoteFilters (
|
||||
val quoteNumLike :String = IGNORE,
|
||||
val validBefore :LocalDate = baseDate,
|
||||
val validAfter :LocalDate = maxDate,
|
||||
val totalAmountExceeds :Long = Long.MIN_VALUE,
|
||||
val totalAmountLessThan :Long = Long.MAX_VALUE,
|
||||
)
|
||||
data class VendorFilters (
|
||||
val nameLike :String = IGNORE,
|
||||
val msmeLike :String = IGNORE,
|
||||
val gstNumLike :String = IGNORE,
|
||||
val addressLike :String = IGNORE,
|
||||
val ratingExceeds :Double = RATING_MIN,
|
||||
val ratingLessThan :Double = RATING_MAX,
|
||||
)
|
||||
fun<T> applyVendorHelper(q :io.ebean.ExpressionList<T>, vids :List<Long>?) {
|
||||
if (vids.isNullOrEmpty()) return
|
||||
q.apply {
|
||||
q.`in`("vendor", vids)
|
||||
}
|
||||
}
|
||||
fun<T> applySortHelper(q :io.ebean.ExpressionList<T>, sortBy :String, asc :Boolean) {
|
||||
if(sortBy == IGNORE) return;
|
||||
val order = if (asc) "ASC" else "DESC"
|
||||
q.orderBy("$sortBy $order")
|
||||
}
|
||||
fun<T> applyCommonFilters(q :io.ebean.ExpressionList<T>, commonFilters: CommonFilters) {
|
||||
applyVendorHelper<T>(q, commonFilters.vendor)
|
||||
applySortHelper<T>(q, commonFilters.sortBy, commonFilters.sortAsc)
|
||||
}
|
||||
fun searchQuotes(commonFilters: CommonFilters, quoteFilters: QuoteFilters) : List<Quotation> {
|
||||
val q = database.find(Quotation::class.java)
|
||||
.where()
|
||||
.between("quoteDate", commonFilters.fromDate, commonFilters.toDate)
|
||||
.ilike("quoteNum", quoteFilters.quoteNumLike )
|
||||
.ge("validTill",quoteFilters.validAfter)
|
||||
.le("validTill", quoteFilters.validBefore)
|
||||
.le("totalAmount", quoteFilters.totalAmountLessThan)
|
||||
.ge("totalAmount", quoteFilters.totalAmountExceeds)
|
||||
.apply {
|
||||
if(!commonFilters.vendor?.isEmpty()!!){
|
||||
commonFilters.vendor.let { this.`in`("vendor", it) }
|
||||
}
|
||||
}
|
||||
applySortHelper(q, commonFilters.sortBy, commonFilters.sortAsc)
|
||||
return q.findList()
|
||||
}
|
||||
fun searchVendors(commonFilters: CommonFilters, vendorFilters: VendorFilters) : List<Vendor> {
|
||||
val q = database.find(Vendor::class.java)
|
||||
.where()
|
||||
.ge("rating", vendorFilters.ratingExceeds)
|
||||
.le("rating", vendorFilters.ratingLessThan)
|
||||
.ilike("name", vendorFilters.nameLike)
|
||||
.ilike("msme", vendorFilters.msmeLike)
|
||||
.ilike("gstNum", vendorFilters.gstNumLike)
|
||||
.ilike("address", vendorFilters.addressLike)
|
||||
applySortHelper(q, commonFilters.sortBy, commonFilters.sortAsc)
|
||||
return q.findList()
|
||||
}
|
||||
fun searchDocs(commonFilters: CommonFilters, documentFilters: DocumentFilters) : List<Document> {
|
||||
val q = database.find(Document::class.java)
|
||||
.where()
|
||||
.apply {
|
||||
if(documentFilters.typeOfDoc != DocType.ALL){
|
||||
this.eq("docType", documentFilters.typeOfDoc)
|
||||
}
|
||||
}
|
||||
.ilike("name", documentFilters.nameLike )
|
||||
applySortHelper(q, commonFilters.sortBy, commonFilters.sortAsc)
|
||||
return q.findList()
|
||||
}
|
||||
fun searchPos(commonFilters: CommonFilters, poFilters: POFilters) : List<PurchaseOrder> {
|
||||
val q = database.find(PurchaseOrder::class.java)
|
||||
.where()
|
||||
.between("totalAmount", poFilters.totalAmountExceeds, poFilters.totalAmountLessThan)
|
||||
.between("validTill", poFilters.validAfter, poFilters.validBefore)
|
||||
.ilike("poNum", poFilters.poNumLike )
|
||||
.ilike("referenceQuotation", poFilters.refQuotation )
|
||||
applyVendorHelper(q, commonFilters.vendor)
|
||||
applySortHelper(q, commonFilters.sortBy, commonFilters.sortAsc)
|
||||
return q.findList()
|
||||
}
|
||||
fun searchRFQ(commonFilters: CommonFilters, rfqFilters: RFQFilters) : List<ReqForQuote> {
|
||||
val q = database.find(ReqForQuote::class.java)
|
||||
.where()
|
||||
.between("validTill", rfqFilters.validAfter, rfqFilters.validBefore)
|
||||
.ilike("reqForQuoteNum", rfqFilters.reqForQuoteNumLike)
|
||||
applyVendorHelper(q, commonFilters.vendor)
|
||||
applySortHelper(q, commonFilters.sortBy, commonFilters.sortAsc)
|
||||
return q.findList()
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user