diff --git a/.gitignore b/.gitignore index 64eadf0..acb5b73 100644 --- a/.gitignore +++ b/.gitignore @@ -43,4 +43,11 @@ bin/ application.yaml initial-data.sql app.yaml -*.env.json \ No newline at end of file +*.env.json + +### API Logs ### +api.log +api.2024* + +### Excel FIles ### +./excel \ No newline at end of file diff --git a/api.http b/api.http index 88f5c2d..1e9aa29 100644 --- a/api.http +++ b/api.http @@ -380,3 +380,8 @@ Authorization: {{auth-token}} GET http://localhost:9001/api/vendor/quote/next Content-Type: application/json Authorization: {{auth-token}} + +### GET NEXT INCOMING SEW NUMBER +GET http://localhost:9001/api/vendor/incoming/next +Content-Type: application/json +Authorization: {{auth-token}} diff --git a/app_conf.yaml b/app_conf.yaml new file mode 100644 index 0000000..72c1c97 --- /dev/null +++ b/app_conf.yaml @@ -0,0 +1,61 @@ +#DO Not edit this file, copy to your HOME Directory and then rename it to app.yaml and then edit it +app: + db: + pass: password + url: jdbc:postgresql://10.10.10.211/arsalan_rmc_module_app + user: arsalan + name: arsalan_rmc_module_app + run_migration: 'true' + cors: + enabled: 'true' + hosts: www.readymixerp.com,app.readymixerp.com + port: '9001' + cache: + redis_uri: redis://10.10.10.112:6379/ + iam: + url: https://auth.readymixerp.com + realm: rmc-dev + client_redirect_uri: http://localhost:9001/auth/code + client: rmc + scripts: + path: /Users/gowthaman.b/IdeaProjects/rmc_modules_api/src/main/resources/scripts + security: + private_key: |- + -----BEGIN PRIVATE KEY----- + MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQD4ba8OhlyB9MUx + MFmT8c9sOKqXOG6ZM6qLxr5SnY1au47rYsHXt8Hb+tTUxpkf+5bndaZYGVgQ1mtw + eEVj3qZq9A+FnxxwZ9uIIdbOUPvsZLzNYnK3bFFRsmWMf30gDs6ZdS63b2tnXXE0 + aoItwVguElUKVXKUymS0VlBB0ZLCYTlD6Q4sDm7HGXJZj5kVNVbBCBe42yLx8Y1F + TyNL8nPSS+SAdLUaPJ/6j+741+exAIOc0rSBJOMF0XNOwBZ85EuOiPa0e1sUz0qj + VfmZyeRFxkSWJBnqum1jrVFp55bMb37DYtZa0aFIcPdDfY42frDlPLcI4zzRwCn3 + ggHL/nL9AgMBAAECggEAJ85sxu8zl8/l4EsG3M8EdVWidrGgUyRgDElFCiLcWVta + prqVJIt5YNYRJU5J5Jc2fRGxHPEOrJVW84IUsvskVQM/ZiHyd3ZvdaGKdFZYpO5t + VvGSlR53l0IhGxal24L+isCn7X+ec5pu6b8JQJX4RbBConHCTDdz/yDMzQcXiiqj + ezzSovZ1Xy/2dn7sOTFtEZi47d3AhBnjh8Xqk3Dc9UChooxuIU6WEbqWFxEkbzJS + sXIv9xADVDqFqjHGv6Tk1W+y7y8M2EHosJfhO0LmWFL7nUdw0WSJV+UqQxHrUSJs + SnYHkKRTYl2ljpjkuECp/YqUqdlNq/5T5jBE6cyopwKBgQD6+XGZfssdAqfnaEK2 + nHdUAdklUFAFQpSmwIUwTZEHDC/CD+ErVjfbEfZ2mFOvWAIMwLmiLFuDT7E5sHaT + K4A2DQ8KyU3iJkH4nhxdYepLc7MSYElkn6fHNrXcJ9vPACmrtoa9rVW/LpAjsRq6 + fDxLo12/+EmFvpZ0oEAIQXk6ZwKBgQD9ZzBgPapI8m7cjcdojqq0dJ2M5Sw7Bx5n + VFOC3H4Cx0xWTdwwZ9CZQ4v/XiiHiUGzwhfkNJ2x3DdpUCkPDD8o3cFXPRW4GsjD + kv/D0kL/JJAesG9XB9yMTMBoe2yGMudDVc7SYgUI2YXhmHYkpcjSzM0DftLL2Z47 + GY1h385Q+wKBgGTRxe/Kfp+lzHtqZ7ph+pG1uFyD+dFTINIn7pkr38G8BIdpx6OY + HBIWEjMsGBoNOa2T0j6yoQSMA/7Pw6J1TCjqcAt+OJpLkh7krTJaPjuXO+163qDc + fhLKCJ5rKKLsRtEjHtedhR+q/d5IrBsUA0jDVMrkW+ytVlV9dpuaaa+rAoGASON8 + m8JBD/iEAPbbK+0VlxCQHO3ymgwDJ8+usc6AhIYVJCIDOv0xmFRAmbTYzZuihXVH + 8AFedsGUQrunA8gPBs86hMByVeGGbBMFdKsvUDqRJfK0JAGD4+tT0PnnjnZn5Qty + kTtWnWQMSYbUPNhe+pukQOQi+DXheLhx3XxF2S8CgYEAopLjGIR/xeuV7QkGJfCO + d2wEJROBPd3pbDlR4fLDO8RCw3irFYQgQd9WgGJY5KgyfjKLHkK7DMQKn+/yfUp5 + UKeesBDXATpyQQrpfoKIhhZKtYphOwvIzugtLsd5sza++sjC9RwkRmYr0rzlHdUl + vtr3fru0Bzven2MeiQnqmCM= + -----END PRIVATE KEY----- + public_key: |- + -----BEGIN PUBLIC KEY----- + MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA+G2vDoZcgfTFMTBZk/HP + bDiqlzhumTOqi8a+Up2NWruO62LB17fB2/rU1MaZH/uW53WmWBlYENZrcHhFY96m + avQPhZ8ccGfbiCHWzlD77GS8zWJyt2xRUbJljH99IA7OmXUut29rZ11xNGqCLcFY + LhJVClVylMpktFZQQdGSwmE5Q+kOLA5uxxlyWY+ZFTVWwQgXuNsi8fGNRU8jS/Jz + 0kvkgHS1Gjyf+o/u+NfnsQCDnNK0gSTjBdFzTsAWfORLjoj2tHtbFM9Ko1X5mcnk + RcZEliQZ6rptY61RaeeWzG9+w2LWWtGhSHD3Q32ONn6w5Ty3COM80cAp94IBy/5y + /QIDAQAB + -----END PUBLIC KEY----- \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index bdbc8a3..da7e75b 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,8 +1,11 @@ +import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar + plugins { kotlin("jvm") version "1.9.22" kotlin("kapt") version "1.9.22" id("idea") id("io.ebean") version "13.23.2" + id("com.github.johnrengelman.shadow") version "8.1.1" application } @@ -53,6 +56,18 @@ kotlin { jvmToolchain(17) } +tasks { + named("shadowJar") { + archiveBaseName.set("rest-api") + mergeServiceFiles() + manifest { + attributes(mapOf("Main-Class" to "com.restapi.MainKt")) + } + isZip64 = true + } +} + + application { mainClass.set("com.restapi.MainKt") } \ No newline at end of file diff --git a/excel/IncomingInventory.xls b/excel/IncomingInventory.xls new file mode 100644 index 0000000..a232475 Binary files /dev/null and b/excel/IncomingInventory.xls differ diff --git a/excel/OutgoingInventory.xls b/excel/OutgoingInventory.xls new file mode 100644 index 0000000..1a61a7c Binary files /dev/null and b/excel/OutgoingInventory.xls differ diff --git a/excel/Pos.xls b/excel/Pos.xls new file mode 100644 index 0000000..63ee154 Binary files /dev/null and b/excel/Pos.xls differ diff --git a/excel/Products.xls b/excel/Products.xls new file mode 100644 index 0000000..dd86121 Binary files /dev/null and b/excel/Products.xls differ diff --git a/excel/Quotes.xls b/excel/Quotes.xls new file mode 100644 index 0000000..54a01e6 Binary files /dev/null and b/excel/Quotes.xls differ diff --git a/excel/VendorList.xls b/excel/VendorList.xls new file mode 100644 index 0000000..254a07c Binary files /dev/null and b/excel/VendorList.xls differ diff --git a/src/main/kotlin/com/restapi/AppAccessManager.kt b/src/main/kotlin/com/restapi/AppAccessManager.kt index ad84c1d..de65e6b 100644 --- a/src/main/kotlin/com/restapi/AppAccessManager.kt +++ b/src/main/kotlin/com/restapi/AppAccessManager.kt @@ -42,7 +42,7 @@ class AppAccessManager : AccessManager { Role.DbOps -> listOf("ROLE_DB_OPS") Role.Entity -> loadEntityActionRole(entity, action) is Role.Standard -> role.action.toList().map { "ROLE_${entity}_${it}" } - is Role.Explicit -> role.roles + is Role.Explicit -> role.roles.toList() + listOf("ROLE_ADMIN") }.map(String::uppercase) } diff --git a/src/main/kotlin/com/restapi/Main.kt b/src/main/kotlin/com/restapi/Main.kt index 3e253bf..0710df2 100644 --- a/src/main/kotlin/com/restapi/Main.kt +++ b/src/main/kotlin/com/restapi/Main.kt @@ -110,56 +110,127 @@ fun main(args: Array) { it.json(mapOf("status" to true)) } - path("/vendor"){ - path("/"){ - post("", VendorCtrl::create, Roles(Role.Explicit(listOf("ROLE_VENDOR_CREATE", "ROLE_ADMIN")))) - post("/batch", VendorCtrl::createBatch, Roles(Role.Explicit(listOf("ROLE_VENDOR_CREATE", "ROLE_ADMIN")))) - get("/{id}", VendorCtrl::get, Roles(Role.Explicit(listOf("ROLE_VENDOR_VIEW", "ROLE_VENDOR_CREATE", "ROLE_ADMIN")))) - post("/getAll", VendorCtrl::getAll, Roles(Role.Explicit(listOf("ROLE_VENDOR_VIEW", "ROLE_VENDOR_CREATE")))) - get("quotes/{id}", VendorCtrl::getQuotes, Roles(Role.Explicit(listOf("ROLE_ADMIN", "ROLE_QUOTE_VIEW", "ROLE_QUOTE_CREATE", "ROLE_VENDOR_VIEW")))) - get("pos/{id}", VendorCtrl::getPos, Roles(Role.Explicit(listOf("ROLE_ADMIN", "ROLE_PO_VIEW", "ROLE_PO_CREATE`")))) - put("/rate/{id}/{rating}", VendorCtrl::rate, Roles(Role.Explicit(listOf("ROLE_VENDOR_CREATE", "ROLE_ADMIN")))) + path("/vendor") { + path("/") { + post("", VendorCtrl::create, Roles(Role.Explicit("ROLE_VENDOR_CREATE"))) + post("/batch", VendorCtrl::createBatch, Roles(Role.Explicit("ROLE_VENDOR_CREATE"))) + get("/{id}", VendorCtrl::get, Roles(Role.Explicit("ROLE_VENDOR_VIEW", "ROLE_VENDOR_CREATE"))) + post( + "/getAll", + VendorCtrl::getAll, + Roles(Role.Explicit("ROLE_VENDOR_VIEW", "ROLE_VENDOR_CREATE")) + ) + get( + "quotes/{id}", + VendorCtrl::getQuotes, + Roles(Role.Explicit("ROLE_QUOTE_VIEW", "ROLE_QUOTE_CREATE", "ROLE_VENDOR_VIEW")) + ) + get("pos/{id}", VendorCtrl::getPos, Roles(Role.Explicit("ROLE_PO_VIEW", "ROLE_PO_CREATE`"))) + put("/rate/{id}/{rating}", VendorCtrl::rate, Roles(Role.Explicit("ROLE_VENDOR_CREATE"))) + put("/{id}", VendorCtrl::update, Roles(Role.Explicit("ROLE_VENDOR_CREATE"))) } - path("/po"){ - get("/next", PurchaseOrderCtrl::getNextNum, Roles(Role.Explicit(listOf("ROLE_PO_CREATE", "ROLE_ADMIN")))) - post("", PurchaseOrderCtrl::create, Roles(Role.Explicit(listOf("ROLE_PO_CREATE", "ROLE_ADMIN")))) - post("/batch", PurchaseOrderCtrl::createBatch, Roles(Role.Explicit(listOf("ROLE_PO_CREATE", "ROLE_ADMIN")))) - post("/getAll", PurchaseOrderCtrl::getAll, Roles(Role.Explicit(listOf("ROLE_PO_CREATE", "ROLE_PO_VIEW", "ROLE_VENDOR_CREATE", "ROLE_ADMIN")))) - get("/{id}", PurchaseOrderCtrl::get, Roles(Role.Explicit(listOf("ROLE_PO_CREATE", "ROLE_PO_VIEW", "ROLE_QUOTE_CREATE")))) - put("/approve/{id}", PurchaseOrderCtrl::approve, Roles(Role.Explicit(listOf("ROLE_ADMIN")))) - put("/reject/{id}", PurchaseOrderCtrl::reject, Roles(Role.Explicit(listOf("ROLE_ADMIN")))) - get("/refQuote/{id}", PurchaseOrderCtrl::quoteReference, Roles(Role.Explicit(listOf("ROLE_PO_CREATE", "ROLE_ADMIN")))) + path("/incoming") { + post("", IncomingInventoryCtrl::create, Roles(Role.Explicit("ROLE_INVENTORY_CREATE"))) + get("/next", IncomingInventoryCtrl::getNextNum, Roles(Role.Explicit("ROLE_INVENTORY_CREATE"))) + get( + "/{id}", + IncomingInventoryCtrl::get, + Roles(Role.Explicit("ROLE_INVENTORY_VIEW", "ROLE_INVENTORY_CREATE")) + ) + put("/{id}", IncomingInventoryCtrl::update, Roles(Role.Explicit("ROLE_INVENTORY_CREATE"))) + post( + "/getAll", + IncomingInventoryCtrl::getAll, + Roles(Role.Explicit("ROLE_INVENTORY_CREATE", "ROLE_INVENTORY_VIEW")) + ) } - path("/quote"){ - get("/next", QuotationCtrl::getNextNum, Roles(Role.Explicit(listOf("ROLE_QUOTE_CREATE", "ROLE_ADMIN")))) - post("", QuotationCtrl::create, Roles(Role.Explicit(listOf("ROLE_QUOTE_CREATE", "ROLE_ADMIN")))) - post("/batch", QuotationCtrl::createBatch, Roles(Role.Explicit(listOf("ROLE_QUOTE_CREATE", "ROLE_ADMIN")))) - post("/getAll", QuotationCtrl::getAll, Roles(Role.Explicit(listOf("ROLE_QUOTE_CREATE", "ROLE_ADMIN", "ROLE_QUOTE_VIEW")))) - get("/{id}", QuotationCtrl::get, Roles(Role.Explicit(listOf("ROLE_QUOTE_VIEW", "ROLE_ADMIN", "ROLE_QUOTE_CREATE")))) - delete("/{id}", QuotationCtrl::delete, Roles(Role.Explicit(listOf("ROLE_QUOTE_CREATE", "ROLE_ADMIN")))) + path("/outgoing") { + post("", OutgoingInventoryCtrl::create, Roles(Role.Explicit("ROLE_INVENTORY_CREATE"))) + get("/next", OutgoingInventoryCtrl::getNextNum, Roles(Role.Explicit("ROLE_INVENTORY_CREATE"))) + get( + "/{id}", + OutgoingInventoryCtrl::get, + Roles(Role.Explicit("ROLE_INVENTORY_VIEW", "ROLE_INVENTORY_CREATE")) + ) + put("/{id}", OutgoingInventoryCtrl::update, Roles(Role.Explicit("ROLE_INVENTORY_CREATE"))) + post( + "/getAll", + OutgoingInventoryCtrl::getAll, + Roles(Role.Explicit("ROLE_INVENTORY_CREATE", "ROLE_INVENTORY_VIEW")) + ) } - path("/product"){ - post("", ProductCtrl::create, Roles(Role.Explicit(listOf("ROLE_PRODUCT_CREATE", "ROLE_ADMIN")))) - //get("/{hsnCode}", ProductCtrl::get, Roles(Role.Explicit(listOf("ROLE_PRODUCT_VIEW", "ROLE_ADMIN")))) - put("/{id}", ProductCtrl::update, Roles(Role.Explicit(listOf("ROLE_PRODUCT_CREATE", "ROLE_ADMIN")))) - //patch("/{id}", ProductCtrl::patch, Roles(Role.Explicit(listOf("ROLE_PRODUCT_UPDATE", "ROLE_ADMIN")))) - delete("/{id}", ProductCtrl::delete, Roles(Role.Explicit(listOf("ROLE_PRODUCT_CREATE", "ROLE_ADMIN")))) - get("", ProductCtrl::getAll, Roles(Role.Explicit(listOf("ROLE_PRODUCT_VIEW", "ROLE_ADMIN")))) - get("/{id}", ProductCtrl::get, Roles(Role.Explicit(listOf("ROLE_PRODUCT_VIEW", "ROLE_ADMIN")))) - post("/getAll", ProductCtrl::getAll, Roles(Role.Explicit(listOf("ROLE_PRODUCT_VIEW", "ROLE_ADMIN")))) + path("/po") { + get("/next", PurchaseOrderCtrl::getNextNum, Roles(Role.Explicit("ROLE_PO_CREATE"))) + post("", PurchaseOrderCtrl::create, Roles(Role.Explicit("ROLE_PO_CREATE"))) + post("/batch", PurchaseOrderCtrl::createBatch, Roles(Role.Explicit("ROLE_PO_CREATE"))) + post( + "/getAll", + PurchaseOrderCtrl::getAll, + Roles(Role.Explicit("ROLE_PO_CREATE", "ROLE_PO_VIEW", "ROLE_VENDOR_CREATE")) + ) + get( + "/{id}", + PurchaseOrderCtrl::get, + Roles(Role.Explicit("ROLE_PO_CREATE", "ROLE_PO_VIEW", "ROLE_QUOTE_CREATE")) + ) + put("/{id}", PurchaseOrderCtrl::update, Roles(Role.Explicit("ROLE_PO_CREATE"))) + put("/approve/{id}", PurchaseOrderCtrl::approve, Roles(Role.Explicit())) + put("/reject/{id}", PurchaseOrderCtrl::reject, Roles(Role.Explicit())) + get("/refQuote/{id}", PurchaseOrderCtrl::quoteReference, Roles(Role.Explicit("ROLE_PO_CREATE"))) } - path("/doc"){ - post("", Document::create, Roles(Role.Explicit(listOf("ROLE_DOC_CREATE", "ROLE_ADMIN")))) + path("/quote") { + get("/next", QuotationCtrl::getNextNum, Roles(Role.Explicit("ROLE_QUOTE_CREATE"))) + post("", QuotationCtrl::create, Roles(Role.Explicit("ROLE_QUOTE_CREATE"))) + post("/batch", QuotationCtrl::createBatch, Roles(Role.Explicit("ROLE_QUOTE_CREATE"))) + post( + "/getAll", + QuotationCtrl::getAll, + Roles(Role.Explicit("ROLE_QUOTE_CREATE", "ROLE_QUOTE_VIEW")) + ) + get("/{id}", QuotationCtrl::get, Roles(Role.Explicit("ROLE_QUOTE_VIEW", "ROLE_QUOTE_CREATE"))) + put("/{id}", QuotationCtrl::update, Roles(Role.Explicit("ROLE_QUOTE_CREATE"))) + delete("/{id}", QuotationCtrl::delete, Roles(Role.Explicit("ROLE_QUOTE_CREATE"))) + } + path("/product") { + post("", ProductCtrl::create, Roles(Role.Explicit("ROLE_PRODUCT_CREATE"))) + put("/{id}", ProductCtrl::update, Roles(Role.Explicit("ROLE_PRODUCT_CREATE"))) + delete("/{id}", ProductCtrl::delete, Roles(Role.Explicit("ROLE_PRODUCT_CREATE"))) + patch("/{id}", ProductCtrl::patch, Roles(Role.Explicit("ROLE_PRODUCT_CREATE"))) + post("/getAll", ProductCtrl::getAll, Roles(Role.Explicit("ROLE_PRODUCT_VIEW"))) + get("/{id}", ProductCtrl::get, Roles(Role.Explicit("ROLE_PRODUCT_VIEW"))) + } + path("/doc") { + post("", DocumentCtrl::create, Roles(Role.Explicit("ROLE_DOC_CREATE"))) //why type and refid are clubbed ?? - get("/{type}/{refId}", Document::getWithRefId, Roles(Role.Explicit(listOf("ROLE_DOC_VIEW", "ROLE_ADMIN", "ROLE_PRODUCT_CREATE")))) - get("/{id}", Document::get, Roles(Role.Explicit(listOf("ROLE_DOC_VIEW", "ROLE_ADMIN", "ROLE_PRODUCT_CREATE")))) - get("/print/{id}", Document::print, Roles(Role.Explicit(listOf("ROLE_DOC_CREATE", "ROLE_DOC_VIEW")))) - delete("/{id}", Document::delete, Roles(Role.Explicit(listOf("ROLE_DOC_CREATE")))) + get( + "/{type}/{refId}", + DocumentCtrl::getWithRefId, + Roles(Role.Explicit("ROLE_DOC_VIEW", "ROLE_PRODUCT_CREATE")) + ) + get("/{id}", DocumentCtrl::get, Roles(Role.Explicit("ROLE_DOC_VIEW", "ROLE_PRODUCT_CREATE"))) + get( + "/print/{id}", + DocumentCtrl::print, + Roles(Role.Explicit("ROLE_DOC_CREATE", "ROLE_DOC_VIEW")) + ) + delete("/{id}", DocumentCtrl::delete, Roles(Role.Explicit("ROLE_DOC_CREATE"))) } - path("/reqForQuote"){ - post("", RequestForQuote::create, Roles(Role.Explicit(listOf("ROLE_QUOTE_CREATE", "ROLE_PO_CREATE", "ROLE_RFQ_CREATE", "ROLE_ADMIN")))) - get("/{id}", RequestForQuote::get, Roles(Role.Explicit(listOf("ROLE_RFQ_CREATE", "ROLE_RFQ_VIEW", "ROLE_QUOTE_VIEW", "ROLE_PO_VIEW", "ROLE_ADMIN")))) - put("/{id}", RequestForQuote::update, Roles(Role.Explicit(listOf("ROLE_QUOTE_CREATE", "ROLE_PO_CREATE", "ROLE_RFQ_CREATE", "ROLE_ADMIN")))) + path("/reqForQuote") { + post( + "", + RequestForQuote::create, + Roles(Role.Explicit("ROLE_QUOTE_CREATE", "ROLE_PO_CREATE", "ROLE_RFQ_CREATE")) + ) + get( + "/{id}", + RequestForQuote::get, + Roles(Role.Explicit("ROLE_RFQ_CREATE", "ROLE_RFQ_VIEW", "ROLE_QUOTE_VIEW", "ROLE_PO_VIEW")) + ) + put( + "/{id}", + RequestForQuote::update, + Roles(Role.Explicit("ROLE_QUOTE_CREATE", "ROLE_PO_CREATE", "ROLE_RFQ_CREATE")) + ) } } post("/script/database/{name}", Entities::executeStoredProcedure, Roles(adminRole, Role.DbOps)) diff --git a/src/main/kotlin/com/restapi/Test.kt b/src/main/kotlin/com/restapi/Test.kt new file mode 100644 index 0000000..a1aef92 --- /dev/null +++ b/src/main/kotlin/com/restapi/Test.kt @@ -0,0 +1,74 @@ +package com.restapi + +import com.fasterxml.jackson.core.JsonParser +import com.fasterxml.jackson.databind.DeserializationContext +import com.fasterxml.jackson.databind.JsonDeserializer +import com.fasterxml.jackson.databind.JsonNode +import com.fasterxml.jackson.databind.annotation.JsonDeserialize +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import com.fasterxml.jackson.module.kotlin.readValue +import com.restapi.controllers.QueryParam +import com.restapi.controllers.RawQuery + +class FDeserializer : JsonDeserializer() { + override fun deserialize(p: JsonParser, p1: DeserializationContext?): F { + //custom logic to do thia + val node = p.readValueAsTree() + + if (node.isObject) { + if (node.has("name")) { + return F.P(name = node.get("name").textValue()) + } else if (node.has("num")) { + return F.Q(num = node.get("num").textValue()) + } else if (node.has("tag")) { + return F.D(tag = node.get("tag").textValue()) + } + + } else { + //incorrect + } + throw IllegalArgumentException() + } + +} +val om = jacksonObjectMapper() + +@JsonDeserialize(using = FDeserializer::class) +sealed interface F { + data class P(val name: String) : F + data class Q(val num: String) : F + data class D(val tag: String) : F +} + +val j = """ + {"name":"a"} +""".trimIndent() + +val j2 = """ + {"num":"a"} +""".trimIndent() + +val j3 = """ + {"tag":"a"} +""".trimIndent() + +val j4 = """ + { + "sql":"aaaa", + "params": { + "a":"b", + "c": { + "type":"STRING", + "value":"aaaa" + } + + } + } +""".trimIndent() + +fun main() { + println(om.readValue(j)) + println(om.readValue(j2)) + println(om.readValue(j3)) + println(om.readValue(j4)) +} \ No newline at end of file diff --git a/src/main/kotlin/com/restapi/config/Auth.kt b/src/main/kotlin/com/restapi/config/Auth.kt index f42e6fc..a48201b 100644 --- a/src/main/kotlin/com/restapi/config/Auth.kt +++ b/src/main/kotlin/com/restapi/config/Auth.kt @@ -235,7 +235,7 @@ enum class Action { sealed class Role { open class Standard(vararg val action: Action) : Role() data object Entity : Role() - data class Explicit(val roles: List) : Role() + open class Explicit(vararg val roles: String) : Role() data object DbOps : Role() } diff --git a/src/main/kotlin/com/restapi/controllers/Entities.kt b/src/main/kotlin/com/restapi/controllers/Entities.kt index 715297a..b22c399 100644 --- a/src/main/kotlin/com/restapi/controllers/Entities.kt +++ b/src/main/kotlin/com/restapi/controllers/Entities.kt @@ -6,18 +6,15 @@ import com.fasterxml.jackson.databind.JsonDeserializer import com.fasterxml.jackson.databind.JsonNode import com.fasterxml.jackson.databind.annotation.JsonDeserialize import com.restapi.domain.* -import com.restapi.domain.Product -import com.restapi.domain.PurchaseOrder -import com.restapi.domain.Quotation import com.restapi.domain.Session.currentUser import com.restapi.domain.Session.database import com.restapi.domain.Session.findDataModelByEntityAndUniqId -import com.restapi.domain.Vendor import com.restapi.integ.Scripting 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 @@ -373,12 +370,13 @@ object Entities { false } } -data class Filters(val common :CommonFilters, val custom :CustomFilters) -data class SequenceNumber(val number:String) -data class BatchPos(val pos :List) + +data class Filters(val common: CommonFilters, val custom: CustomFilters) +data class SequenceNumber(val number: String) +data class BatchPos(val pos: List) object PurchaseOrderCtrl { - fun getNextNum(ctx: Context){ + fun getNextNum(ctx: Context) { val prefix = "PO/" val cnt = database.find(PurchaseOrder::class.java) .findCount() @@ -387,39 +385,57 @@ object PurchaseOrderCtrl { val seq = SequenceNumber(prefix + cnt) ctx.json(seq).status(HttpStatus.OK) } - fun get(ctx :Context){ + + 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){ + + fun getAll(ctx: Context) { val filters = ctx.bodyAsClass() val pos = searchPos(filters.common, filters.poFilters) - ctx.json(pos).status(HttpStatus.OK) + 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){ + + 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 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){ + + 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 @@ -427,14 +443,18 @@ object PurchaseOrderCtrl { ctx.json(po).status(HttpStatus.CREATED) //reject all other pos pertaining to the same tx ?? } - fun reject(ctx :Context){ + + 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.approvalStatus = ApprovalStatus.REJECTED - po.save() + po.apply { + approvalStatus = ApprovalStatus.REJECTED + save() + } ctx.json(po).status(HttpStatus.CREATED) } - fun quoteReference(ctx :Context){ + + 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) @@ -443,6 +463,22 @@ object PurchaseOrderCtrl { ?: 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) + } } data class ProductSearch( @@ -451,25 +487,33 @@ data class ProductSearch( ) object ProductCtrl { - fun get(ctx :Context){ + 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") - println("Product found") - println(product) ctx.json(product).status(HttpStatus.OK) } - fun getAll(ctx: Context){ - val productList = Session.database.find(Product::class.java) - .findList() - .sortedBy { it.hsnCode } - ctx.json(productList) + data class PF(val common: CommonFilters, val productFilters: ProductFilters) + + fun getAll(ctx: Context) { + val filters = ctx.bodyAsClass() + 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){ + + fun create(ctx: Context) { val product = ctx.bodyAsClass() database.save(product) ctx.json(product).status(HttpStatus.CREATED) @@ -477,21 +521,63 @@ object ProductCtrl { fun delete(ctx: Context) { val id = ctx.pathParam("id") - val product = database.delete(Product::class.java, 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>() + 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") - + 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.patchValues(updatedProduct) + product.update() + ctx.json(product).status(HttpStatus.OK) } + + + @JvmStatic + fun main(args: Array) { + + 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 QuotationCtrl { - fun getNextNum(ctx: Context){ + fun getNextNum(ctx: Context) { val prefix = "QUOTE/" val cnt = database.find(Quotation::class.java) .findCount() @@ -500,33 +586,50 @@ object QuotationCtrl { val seq = SequenceNumber(prefix + cnt) ctx.json(seq).status(HttpStatus.OK) } - fun get(ctx :Context){ + + fun get(ctx: Context) { val id = ctx.pathParam("id") - val quote = database.find(Quotation::class.java, id) ?: throw NotFoundResponse("quote not found for $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){ + + fun getAll(ctx: Context) { val filters = ctx.bodyAsClass() + val excel: String? = ctx.queryParam("excel") val quotes = searchQuotes(filters.common, filters.quoteFilters) - ctx.json(quotes).status(HttpStatus.OK) + 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){ + 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){ + + fun createBatch(ctx: Context) { val quotes = ctx.bodyAsClass>() - val txn = database.beginTransaction() + val txn = database.beginTransaction() try { txn.isBatchMode = true - for(quote in quotes) database.save(quote) + for (quote in quotes) database.save(quote) txn.commit() ctx.status(HttpStatus.CREATED).result("Quotes Created") - } catch(e :Exception){ + } catch (e: Exception) { txn.rollback() ctx.status(HttpStatus.INTERNAL_SERVER_ERROR).result("Quotes Creation failed" + e.message) } finally { @@ -534,134 +637,149 @@ object QuotationCtrl { } ctx.result("Quotes batch created").status(HttpStatus.CREATED) } - fun delete(ctx :Context){ + + fun delete(ctx: Context) { val id = ctx.pathParam("id") - val quote = database.find(Quotation::class.java, id) ?: throw NotFoundResponse("quote not found for id $id") - quote.delete() + val quote = database.find(Quotation::class.java, id) ?: throw NotFoundResponse("no quote found with id $id") + database.delete(quote) ctx.status(HttpStatus.OK) - ctx.result("quote with $id deleted") } - fun generatePO(ctx :Context){ - //user should be redirected to a po form submission with prefilled values - //create a PO object with values from the quote and then send it as body to vendor/po/create ?? - } - fun reqForQuote(ctx :Context){ - val reqForQuoteNum = ctx.pathParam(("rfqNum")) - val rfq = database.find(RequestForQuote::class.java) - .where() - .eq("reqForQuoteNum", reqForQuoteNum) - ?: throw NotFoundResponse("request for quote not found for this quotation") - ctx.status(HttpStatus.OK) - ctx.json(rfq) + + 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 Document { - fun get(ctx :Context){ - val id = ctx.pathParam("id") + +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() + + fun create(ctx: Context) { + val doc = ctx.bodyAsClass() database.save(doc) ctx.status(HttpStatus.CREATED) ctx.json(doc) } - fun print(ctx :Context){ + + fun print(ctx: Context) { //would be handled in the frontend ?? } - fun delete(ctx :Context){ + + fun delete(ctx: Context) { val id = ctx.pathParam("id") - val doc = database.find(Document::class.java, id) ?: throw NotFoundResponse("no doc found with id $id") - //doc.delete() + val doc = database.find(Document::class.java, id) ?: throw NotFoundResponse("no document found with id $id") + database.delete(doc) ctx.status(HttpStatus.OK) - ctx.result("document deleted with id $id") } - fun getWithRefId(ctx :Context){ + + 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("refId", refId) + .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 { - fun get(ctx :Context){ - val id = ctx.pathParam("id") + 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){ + + data class VF(val common: CommonFilters, val vendorFilters: VendorFilters) + + fun getAll(ctx: Context) { val filters = ctx.bodyAsClass() - println(filters.common) - println(filters.vendorFilters) - val pos = searchVendors(filters.common, filters.vendorFilters) - ctx.status(HttpStatus.OK) - ctx.json(pos) - } - fun createBatch(ctx: Context){ - val vendors = ctx.bodyAsClass>() - val txn = database.beginTransaction() - try { - txn.isBatchMode = true - for(v in vendors) database.save(v) - txn.commit() - ctx.status(HttpStatus.CREATED).result("Vendors Created") - } catch(e :Exception){ - txn.rollback() - ctx.status(HttpStatus.INTERNAL_SERVER_ERROR).result("Vendor Creation failed" + e.message) - } finally { - txn.end() + logger.info("filters = {}", filters) + 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 create(ctx :Context){ + + fun createBatch(ctx: Context) { + val vendors = ctx.bodyAsClass>() + database.saveAll(vendors) + } + + fun create(ctx: Context) { val vendor = ctx.bodyAsClass() database.save(vendor) ctx.status(HttpStatus.CREATED) ctx.json(vendor) } - fun update(ctx :Context){ + 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.patchValues(updatedVendor) + vendor.update() + ctx.json(vendor).status(HttpStatus.OK) } - fun delete(ctx :Context){ - } - fun getQuotes(ctx :Context){ + 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", id) + .eq("vendor", database.find(Vendor::class.java, id) ?: throw NotFoundResponse("vendor not found for $id")) .findList() - ctx.status(HttpStatus.OK) - ctx.json(quotes) + ctx.json(quotes).status(HttpStatus.OK) } - fun getPos(ctx :Context){ - val id = ctx.pathParam("id") + + fun getPos(ctx: Context) { + val id = ctx.pathParam("id").toLong() val pos = database.find(PurchaseOrder::class.java) .where() - .eq("vendor", id) + .eq("vendor", database.find(Vendor::class.java, id) ?: throw NotFoundResponse("vendor not found for $id")) .findList() - ctx.status(HttpStatus.OK) - ctx.json(pos) + ctx.json(pos).status(HttpStatus.OK) } - fun rate(ctx :Context){ + + fun rate(ctx: Context) { val id = ctx.pathParam("id") - val rating = ctx.pathParam("rating").toDouble() - val vendor = database.find(Vendor::class.java, id) ?: throw NotFoundResponse("vendor not found for id $id") - //could place some rating validation checks - vendor.rating = rating - vendor.save() - ctx.status(HttpStatus.OK) - ctx.result("rating changed") + 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 RequestForQuote { fun create(ctx: Context) { val rfq = ctx.bodyAsClass() @@ -684,4 +802,105 @@ object RequestForQuote { //shuld we compare the new body fields with preexisting ones and prepare a sql query to update those fields?? } -} \ No newline at end of file +} + +object IncomingInventoryCtrl { + fun create(ctx: Context) { + val ticket = ctx.bodyAsClass() + database.save(ticket) + ctx.json(ticket).status(HttpStatus.CREATED) + } + + fun update(ctx: Context) { + val id = ctx.pathParam("id").toLong() + val ticket = + database.find(IncomingInventory::class.java, id) ?: throw NotFoundResponse("quote not found for $id") + val updatedTicket = ctx.bodyAsClass() + ticket.patchValues(updatedTicket) + ticket.update() + ctx.json(ticket).status(HttpStatus.OK) + } + + fun get(ctx: Context) { + val id = ctx.pathParam("id").toLong() + val ticket = database.find(IncomingInventory::class.java, id) + ?: throw NotFoundResponse("No incoming inventory ticket found with id $id") + ctx.json(ticket).status(HttpStatus.OK) + } + + data class IIF(val common: CommonFilters, val incomingInventoryFilters: IncomingInventoryFilters) + + fun getAll(ctx: Context) { + val filters = ctx.bodyAsClass() + val tickets = searchIncomingInventory(filters.common, filters.incomingInventoryFilters) + val excel = ctx.queryParam("excel") + if (excel !== null) { + exportIncomingInventory(tickets) + val inputStream = FileInputStream("./excel/IncomingInventory.xls") + ctx.result(inputStream).status(HttpStatus.OK) + } else { + ctx.json(tickets).status(HttpStatus.OK) + } + } + + fun getNextNum(ctx: Context) { + val prefix = "MRN/" + val cnt = database.find(IncomingInventory::class.java) + .findCount() + .toString() + .padStart(6, '0') + val seq = SequenceNumber(prefix + cnt) + ctx.json(seq).status(HttpStatus.OK) + } +} + +object OutgoingInventoryCtrl { + fun create(ctx: Context) { + val ticket = ctx.bodyAsClass() + database.save(ticket) + ctx.json(ticket).status(HttpStatus.CREATED) + } + + fun update(ctx: Context) { + val id = ctx.pathParam("id").toLong() + val ticket = + database.find(OutgoingInventory::class.java, id) ?: throw NotFoundResponse("quote not found for $id") + val updatedTicket = ctx.bodyAsClass() + ticket.patchValues(updatedTicket) + ticket.update() + ctx.json(ticket).status(HttpStatus.OK) + } + + fun get(ctx: Context) { + val id = ctx.pathParam("id").toLong() + val ticket = database.find(OutgoingInventory::class.java, id) + ?: throw NotFoundResponse("No incoming inventory ticket found with id $id") + ctx.json(ticket).status(HttpStatus.OK) + } + + data class OIF(val common: CommonFilters, val outgoingInventoryFilters: OutgoingInventoryFilters) + + fun getAll(ctx: Context) { + val filters = ctx.bodyAsClass() + val tickets = searchOutgoingInventory(filters.common, filters.outgoingInventoryFilters) + val excel = ctx.queryParam("excel") + if (excel !== null) { + exportOutgoingInventory(tickets) + val inputStream = FileInputStream("./excel/OutgoingInventory.xls") + ctx.result(inputStream).status(HttpStatus.OK) + } else { + ctx.json(tickets).status(HttpStatus.OK) + } + } + + fun getNextNum(ctx: Context) { + println("inside next num") + val prefix = "MDN/" + val cnt = database.find(OutgoingInventory::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/Excel.kt b/src/main/kotlin/com/restapi/controllers/Excel.kt index 6bccc55..b518e40 100644 --- a/src/main/kotlin/com/restapi/controllers/Excel.kt +++ b/src/main/kotlin/com/restapi/controllers/Excel.kt @@ -1,7 +1,5 @@ package com.restapi.controllers -import com.fasterxml.jackson.databind.DeserializationFeature -import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule -import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper + import com.google.gson.Gson import com.restapi.domain.* import com.restapi.domain.Document @@ -14,16 +12,21 @@ import com.restapi.domain.Session.database import com.restapi.domain.Vendor import org.apache.poi.hssf.usermodel.DVConstraint import org.apache.poi.hssf.usermodel.HSSFDataValidation -import org.apache.poi.ss.usermodel.* +import org.apache.poi.ss.usermodel.Cell +import org.apache.poi.ss.usermodel.CellType +import org.apache.poi.ss.usermodel.DateUtil +import org.apache.poi.ss.usermodel.Workbook +import org.apache.poi.ss.usermodel.WorkbookFactory import org.apache.poi.ss.util.CellRangeAddressList -import org.apache.poi.xssf.usermodel.XSSFWorkbook -import java.io.* +import java.io.File +import java.io.FileInputStream +import java.io.FileOutputStream import java.text.SimpleDateFormat import java.time.LocalDate import java.time.ZoneId import java.util.* -fun createHeaderRow(cols :List, sh :HSSFSheet, wb: Workbook) { +fun createHeaderRow(cols: List, sh: HSSFSheet, wb: Workbook) { val boldFont = wb.createFont() boldFont.bold = true val style = wb.createCellStyle() @@ -31,13 +34,14 @@ fun createHeaderRow(cols :List, sh :HSSFSheet, wb: Workbook) { style.locked = true sh.createRow(0).apply { - cols.forEachIndexed{index, value -> + cols.forEachIndexed { index, value -> val cell = createCell(index) cell.setCellValue(value) cell.setCellStyle(style) } } } + fun String.parseDate(format: String): Date? { val locale = Locale.getDefault() return try { @@ -46,227 +50,389 @@ fun String.parseDate(format: String): Date? { null } } -fun dateFromCellHelper(cell: Cell): LocalDate?{ - val date = when(cell.cellType){ + +fun dateFromCellHelper(cell: Cell): LocalDate? { + val date = when (cell.cellType) { CellType.STRING -> cell.stringCellValue.parseDate("yyyy-MM-dd") CellType.NUMERIC -> { if (DateUtil.isCellDateFormatted(cell)) { cell.getDateCellValue() - } else{ + } else { null } } + else -> null } return date?.toInstant()?.atZone(ZoneId.systemDefault())?.toLocalDate() } + fun stringFromCellHelper(cell: Cell): String { - val string = when(cell.cellType){ + val string = when (cell.cellType) { CellType.NUMERIC -> cell.numericCellValue.toString() CellType.STRING -> cell.stringCellValue else -> "" } return string } + fun doubleFromCellHelper(cell: Cell): Double { - val double = when(cell.cellType){ + val double = when (cell.cellType) { CellType.NUMERIC -> cell.numericCellValue CellType.STRING -> cell.stringCellValue.toDoubleOrNull() else -> 0.0 } - return double?:0.0 + return double ?: 0.0 } -fun longIntFromCellHelper(cell : Cell) :Long { - val long = when(cell.cellType){ +fun longIntFromCellHelper(cell: Cell): Long { + val long = when (cell.cellType) { CellType.NUMERIC -> cell.numericCellValue.toLong() CellType.STRING -> cell.stringCellValue.toLong() else -> 0 } return long } + enum class FileType { QUOTES, POS, VENDORS, PRODS, DOCS } + enum class EnumFor { UOM, DocType } -fun saveExcelFileLocally(fileName :String, wb: Workbook){ - val out = FileOutputStream(fileName) +fun saveExcelFileLocally(fileName: String, wb: Workbook) { + val path = "./excel/" + val out = FileOutputStream(path + fileName) wb.use { it.write(out) } out.close() } -fun TemplateExcelFile(fileType: FileType){ - when(fileType){ + +fun TemplateExcelFile(fileType: FileType) { + when (fileType) { FileType.QUOTES -> { - val headers : List = listOf("Quotation Number", "Date", "Open Till", "Product Id", "Product Name", "Product Unit Price", "Quantity", "Vendor Name", "Vendor Address", "RFQ Number", "Total Amount", "Terms and Conditions") + val headers: List = listOf( + "Quotation Number", + "Date", + "Open Till", + "Product Id", + "Product Name", + "Product Unit Price", + "Quantity", + "Vendor Name", + "Vendor Address", + "RFQ Number", + "Total Amount", + "Terms and Conditions" + ) val wb = HSSFWorkbook() val sh = wb.createSheet() createHeaderRow(headers, sh, wb) saveExcelFileLocally("Quotes_Template.xls", wb) } + FileType.POS -> { - val headers : List = listOf("Number", "Date", "Open Till", "Reference Quotation Number", "Vendor Name", "Vendor Address", "Product Id", "Product Name", "Unit Price", "Quantity", "Total Amount", "Terms and Conditions") + val headers: List = listOf( + "Number", + "Date", + "Open Till", + "Reference Quotation Number", + "Vendor Name", + "Vendor Address", + "Product Id", + "Product Name", + "Unit Price", + "Quantity", + "Total Amount", + "Terms and Conditions" + ) val wb = HSSFWorkbook() val sh = wb.createSheet() createHeaderRow(headers, sh, wb) saveExcelFileLocally("Purchase_Order_Template.xls", wb) } + FileType.VENDORS -> { - val headers : List = listOf("Name", "MSME", "GST Number", "Address", "Rating", "Contact Name", "Contact Email", "Contact Mobile") + val headers: List = listOf( + "Name", + "MSME", + "GST Number", + "Address", + "Rating", + "Contact Name", + "Contact Email", + "Contact Mobile" + ) val wb = HSSFWorkbook() val sh = wb.createSheet() createHeaderRow(headers, sh, wb) saveExcelFileLocally("Vendors_Template.xls", wb) } + FileType.PRODS -> { - val headers : List = listOf("Id", "Name", "Description", "HSN Code", "UOM") + val headers: List = listOf("Id", "Name", "Description", "HSN Code", "UOM") val wb = HSSFWorkbook() val sh = wb.createSheet() createHeaderRow(headers, sh, wb) val r0 = CellRangeAddressList(0, 1000, 4, 4) - val dv0 = HSSFDataValidation(r0, DVConstraint.createExplicitListConstraint(arrayOf("LTR", "MTR", "NOS", "ALL"))).apply { + val dv0 = HSSFDataValidation( + r0, + DVConstraint.createExplicitListConstraint(arrayOf("LTR", "MTR", "NOS", "ALL")) + ).apply { suppressDropDownArrow = true } sh.addValidationData(dv0) saveExcelFileLocally("Products_Template.xls", wb) } + FileType.DOCS -> { } } } -fun ExportQuotations(quotes :List) { + +fun exportQuotations(quotes: List) { val wb = HSSFWorkbook() val sh = wb.createSheet() - val headers : List = listOf("Quotation Number", "Date", "Open Till", "Product Id", "Product Name", "Product Unit Price", "Quantity", "Vendor Name", "Vendor Address", "RFQ Number", "Total AMount", "Terms and Conditions") + val headers: List = listOf( + "Quotation Number", + "Date", + "Open Till", + "Product Id", + "Product Name", + "Product Unit Price", + "Quantity", + "Vendor Name", + "Vendor Address", + "RFQ Number", + "Total AMount", + "Terms and Conditions" + ) createHeaderRow(headers, sh, wb) - val totalCols = headers.size var rowCnt = 1 - for(quote in quotes){ + for (quote in quotes) { val prodCnt = quote.products.size - for (j in 0..prodCnt - 1){ + for (j in 0..){ +fun exportVendors(vendors: List) { val wb = HSSFWorkbook() val sh = wb.createSheet() - val headers : List = listOf("Name", "MSME", "GST Number", "Address", "Rating", "Contact Name", "Contact Email", "Contact Mobile") + val headers: List = + listOf("No.", "Name", "MSME", "GST Number", "Address", "Rating", "Contact Name", "Contact Email", "Contact Mobile") createHeaderRow(headers, sh, wb) val totalCols = headers.size var rowCnt = 1 - for (vendor in vendors){ + for (vendor in vendors) { val contactCnt = vendor.contacts.size - for (j in 0..contactCnt - 1){ + for (j in 0..){ +fun exportProds(prods: List) { val wb = HSSFWorkbook() val sh = wb.createSheet() - val headers : List = listOf("Id", "Name", "Description", "HSN Code", "UOM") + val headers: List = listOf("Id", "Name", "Description", "HSN Code", "UOM") createHeaderRow(headers, sh, wb) - val totalCols = headers.size var rowCnt = 1 - for (prod in prods){ + for (prod in prods) { val row = sh.createRow(rowCnt++) var i = 0 - row.createCell(i++).setCellValue(prod.id.toString()) - row.createCell(i++).setCellValue(prod.name) - row.createCell(i++).setCellValue(prod.description) - row.createCell(i++).setCellValue(prod.hsnCode) - row.createCell(i++).setCellValue(prod.uom?.name) + row.createCell(i++).setCellValue(prod.id.toString()) + row.createCell(i++).setCellValue(prod.name) + row.createCell(i++).setCellValue(prod.description) + row.createCell(i++).setCellValue(prod.hsnCode) + row.createCell(i++).setCellValue(prod.uom?.name) } + saveExcelFileLocally("Products.xls", wb) } -fun ExportPos(pos :List){ + +fun exportPos(pos: List) { val wb = HSSFWorkbook() val sh = wb.createSheet() - val headers : List = listOf("Number", "Date", "Open Till", "Reference Quotation Number", "Vendor Name", "Vendor Address", "Product Id", "Product Name", "Unit Price", "Quantity", "Total Amount", "Terms and Conditions") + val headers: List = listOf( + "Number", + "Date", + "Open Till", + "Reference Quotation Number", + "Vendor Name", + "Vendor Address", + "Product Id", + "Product Name", + "Unit Price", + "Quantity", + "Total Amount", + "Terms and Conditions" + ) createHeaderRow(headers, sh, wb) - val totalCols = headers.size var rowCnt = 1 - for(po in pos){ + for (po in pos) { val prodCnt = po.products.size - for (j in 0..prodCnt - 1){ + for (j in 0..) { + val wb = HSSFWorkbook() + val sh = wb.createSheet() + + val headers: List = listOf( + "MRN", + "Date", + "Vendor Name", + "Vendor Bill Number", + "Vendor Bill Amount", + "Product Id", + "Product Name", + "Unit Price", + "Quantity", + ) + createHeaderRow(headers, sh, wb) + + var rowCnt = 1 + for (ticket in tickets) { + val prodCnt = ticket.products?.size + + for (j in 0..) { + val wb = HSSFWorkbook() + val sh = wb.createSheet() + + val headers: List = listOf( + "MDN", + "Date", + "Out Mode", + "Purpose", + "Product Id", + "Product Name", + "Unit Price", + "Quantity", + ) + createHeaderRow(headers, sh, wb) + + var rowCnt = 1 + for (ticket in tickets) { + val prodCnt = ticket.products?.size + + for (j in 0.. { //Quote Number, ProductName, Product Quantity, Total Amount, RFQ Number, Quote Date, Valid Till, TNC[], Documents[] - val quotesMap : MutableMap = mutableMapOf() - val quotesList : List = mutableListOf() + val quotesMap: MutableMap = mutableMapOf() + val quotesList: List = mutableListOf() sh.rowIterator().forEach { row -> - if(row == null){ + if (row == null) { //reached eof return@forEach } @@ -286,7 +452,7 @@ fun ImportFromExcel(fileType: FileType, filePath : String) { if (quotesMap.containsKey(quoteNumber)) { //duplicated row quotesMap.get(quoteNumber)?.products?.add(prod) - }else { + } else { val v = Vendor() v.apply { name = vendorName @@ -307,17 +473,18 @@ fun ImportFromExcel(fileType: FileType, filePath : String) { } } //docs, tncs - // println("$quotesMap") + // println("$quotesMap") // quotesMap.forEach { (k, v) -> // println("$v") // } } + FileType.POS -> { //poNum, poDate, validTill, refQuoteNum, prodName, prodQuantity, totalAmount, products, vendorName, vendorGst, vendorAddress, tnc[]. docs[] - val PoMap : MutableMap = mutableMapOf() + val PoMap: MutableMap = mutableMapOf() sh.rowIterator().forEach { row -> - if(row == null) return@forEach + if (row == null) return@forEach val poNum = stringFromCellHelper(row.getCell(0)) val poDate = dateFromCellHelper(row.getCell(1)) val refQuoteNum = stringFromCellHelper(row.getCell(2)) @@ -330,11 +497,11 @@ fun ImportFromExcel(fileType: FileType, filePath : String) { val totalPoAmount = doubleFromCellHelper(row.getCell(9)) //tncs, docs - val prod = POProducts("", prodName, 0.0, prodQuantity,"") - if(PoMap.containsKey(poNum)){ + val prod = POProducts("", prodName, 0.0, prodQuantity, "") + if (PoMap.containsKey(poNum)) { //repeated row PoMap.get(poNum)?.products?.add(prod) - }else{ + } else { val vendor = Vendor() vendor.name = vendorName vendor.address = vendorAddress @@ -348,15 +515,16 @@ fun ImportFromExcel(fileType: FileType, filePath : String) { } } } + FileType.VENDORS -> { sh.rowIterator().forEach { row -> //name, msme, gstNum, addresss, rating, contacts - if(row == null) return@forEach + if (row == null) return@forEach val name = stringFromCellHelper(row.getCell(0)) val msme = stringFromCellHelper(row.getCell(1)) val gstNum = stringFromCellHelper(row.getCell(2)) val address = stringFromCellHelper(row.getCell(3)) - val rating = doubleFromCellHelper(row.getCell(4)) + val rating = doubleFromCellHelper(row.getCell(4)) //vendor object val vendor = Vendor() @@ -367,9 +535,10 @@ fun ImportFromExcel(fileType: FileType, filePath : String) { vendor.rating = rating } } + FileType.PRODS -> { sh.rowIterator().forEach { row -> - if(row == null) return@forEach + if (row == null) return@forEach //id, name, description, hsnCode, uom val prodId = longIntFromCellHelper(row.getCell(0)) val prodName = stringFromCellHelper(row.getCell(1)) @@ -383,14 +552,15 @@ fun ImportFromExcel(fileType: FileType, filePath : String) { prod.name = prodName prod.description = prodDesc prod.hsnCode = prodHsnCode - prod.uom = when(prodUom) { + prod.uom = when (prodUom) { "nos" -> UOM.NOS "ltr" -> UOM.LTR "mtr" -> UOM.MTR - else -> UOM.LTR + else -> UOM.ALL } } } + FileType.DOCS -> { sh.rowIterator().forEach { row -> //Document Name, Document Type, RefID, url @@ -403,179 +573,15 @@ fun ImportFromExcel(fileType: FileType, filePath : String) { //new doc object val doc = Document() doc.name = docName - doc.typeOfDoc = when(docType) { + doc.typeOfDoc = when (docType) { "quote" -> DocType.QUOTE "po" -> DocType.PO "invoice" -> DocType.INVOICE else -> DocType.ALL } - doc.refId = refId + doc.refIdOfDoc = refId.toLong() doc.url = url } } } -} - -data class validateExcel( - val name: String, - val description: String, - val hsnCode: String, - val ok: Boolean, - val err: String, -) - -val app_common_om = jacksonObjectMapper().apply { - registerModule(JavaTimeModule()) - configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) -} - -fun ExcelRead(): String{ - val inputStream = FileInputStream("C:\\Users\\vinay\\IdeaProjects\\readymixerp_modules_api_git\\Untitled 1.xlsx") - val workbook = WorkbookFactory.create(inputStream) - val workSheet = workbook.getSheetAt(0) - var h = true - //Header check - if(workSheet.getRow(0).getCell(0).stringCellValue.equals("Name")) { - if (workSheet.getRow(0).getCell(1).stringCellValue.equals("Description")) { - if (workSheet.getRow(0).getCell(2).stringCellValue.equals("HSN")) { - if (workSheet.getRow(0).getCell(3).stringCellValue.equals("UOM")) { - h = false - }else return "Header UOM mismatch" - }else return "Header-HSN mismatch" - }else return "Header-Desc mismatch" - }else return "Header-Name mismatch" - - val resp = arrayListOf() - - if(h==false) { - workSheet.rowIterator().forEach { row -> - - if (row == null) return@forEach - - if (h) { - - val pName = row.getCell(0).run { - when { - this == null -> "" - this.cellType == CellType.STRING -> this.stringCellValue - else -> "" - } - } - - val pDesc = row.getCell(1).run { - when { - this == null -> "" - this.cellType == CellType.STRING -> this.stringCellValue - else -> "" - } - } - val pHsn = row.getCell(2).run { - when { - this == null -> "" - this.cellType == CellType.STRING -> this.stringCellValue - else -> "" - } - } - - if (pName.isEmpty() && pDesc.isEmpty() && pHsn.isEmpty()) { - return@forEach - } - if (pName.isEmpty()) { - resp.add( - validateExcel( - name = pName, - description = pDesc, - hsnCode = pHsn, - ok = false, - err = "Product name is required" - ) - ) - return@forEach - } - if (pDesc.isEmpty()) { - resp.add( - validateExcel( - name = pName, - description = pDesc, - hsnCode = pHsn, - ok = false, - err = "Product description is required" - ) - ) - return@forEach - } - if (pHsn.isEmpty()) { - resp.add( - validateExcel( - name = pName, - description = pDesc, - hsnCode = pHsn, - ok = false, - err = "Product HSN is required" - ) - ) - return@forEach - } - } - h = true - } - } - return app_common_om.writeValueAsString(resp) -} - - -fun CreateExcel(productList: List): InputStream { - val wb = XSSFWorkbook() - val sh = wb.createSheet() - val rows: Row = sh.createRow(0) - rows.createCell(0).setCellValue("Name") - rows.createCell(1).setCellValue("Description") - rows.createCell(2).setCellValue("HSN") - rows.createCell(3).setCellValue("UOM") - - var rowNum = 1 - for (product in productList) { - val row: Row = sh.createRow(rowNum++) - - row.createCell(0).setCellValue(product.name) - row.createCell(1).setCellValue(product.description) - row.createCell(2).setCellValue(product.hsnCode) - - val uomCell: Cell = row.createCell(3) - uomCell.setCellValue(product.uom?.name ?: "") - } - - val baos = ByteArrayOutputStream() - wb.write(baos) - wb.close() - - return ByteArrayInputStream(baos.toByteArray()) - -} - -fun excelToDb(): List { - val inputStream = FileInputStream("C:\\Users\\vinay\\IdeaProjects\\readymixerp_modules_api_git\\Untitled 1.xlsx") - val workbook = WorkbookFactory.create(inputStream) - val workSheet = workbook.getSheetAt(0) - - for (row in workSheet) { - val cell1Value = row.getCell(0).stringCellValue - val cell2Value = row.getCell(1).stringCellValue - val cell3Value = row.getCell(2).stringCellValue - val cell4Value = row?.getCell(3)?.stringCellValue - - val prod = Product() - prod.name = cell1Value - prod.description = cell2Value - prod.hsnCode = cell3Value - prod.uom = when(cell4Value) { - "nos" -> UOM.NOS - "ltr" -> UOM.LTR - "mtr" -> UOM.MTR - else -> UOM.ALL - } - database.saveAll(prod) - } - val productList = Session.database.find(Product::class.java).findList() - return productList } \ No newline at end of file diff --git a/src/main/kotlin/com/restapi/controllers/Filters.kt b/src/main/kotlin/com/restapi/controllers/Filters.kt index 32b7d07..0852d24 100644 --- a/src/main/kotlin/com/restapi/controllers/Filters.kt +++ b/src/main/kotlin/com/restapi/controllers/Filters.kt @@ -1,70 +1,89 @@ package com.restapi.controllers import com.restapi.domain.* -import com.restapi.domain.Quotation import com.restapi.domain.Session.database import java.time.LocalDate //constants const val IGNORE = "%" -val baseDate :LocalDate = LocalDate.of(1500, 1,1, ) -val maxDate :LocalDate = LocalDate.of(3000, 1 ,1) +val baseDate: LocalDate = LocalDate.of(1500, 1, 1) +val maxDate: LocalDate = LocalDate.of(3000, 1, 1) 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? = null, - val sortAsc :Boolean = true, - val sortBy :String = IGNORE +data class CommonFilters( + val from: LocalDate = baseDate, + val to: LocalDate = maxDate, + val vendor: List? = null, + val sortAsc: Boolean = true, + val sortBy: String = IGNORE ) -interface CustomFilters{} -data class POFilters ( - val poNumLike :String = IGNORE, - val totalAmountExceeds :Long = Long.MIN_VALUE, - val totalAmountLessThan :Long = Long.MAX_VALUE, + +interface CustomFilters {} + +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, + val refQuotation: String = IGNORE, ) : CustomFilters -enum class UOMFilter { - ALL //fixme: later -} -data class ProductFilters ( - val nameLike :String = IGNORE, - val hsnLike :String = IGNORE, - val uom :UOMFilter = UOMFilter.ALL, +data class ProductFilters( + val nameLike: String = IGNORE, + val hsnLike: String = IGNORE, + val uom: UOM = UOM.ALL, ) : CustomFilters -data class DocumentFilters ( - val nameLike :String = IGNORE, - val typeOfDoc :DocType = DocType.ALL, - val docDateFrom :LocalDate = baseDate, - val docDataTo :LocalDate = maxDate, -) :CustomFilters -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 = maxDate, - val validAfter :LocalDate = baseDate, - val totalAmountExceeds :Long = Long.MIN_VALUE, - val totalAmountLessThan :Long = Long.MAX_VALUE, -) :CustomFilters -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, -) :CustomFilters -fun applyVendorHelper(q :io.ebean.ExpressionList, vids :List?) { + +data class DocumentFilters( + val nameLike: String = IGNORE, + val typeOfDoc: DocType = DocType.ALL, + val docDateFrom: LocalDate = baseDate, + val docDataTo: LocalDate = maxDate, +) : CustomFilters + +data class RFQFilters( + val validBefore: LocalDate = maxDate, + val validAfter: LocalDate = baseDate, + val reqForQuoteNumLike: String = IGNORE, +) : CustomFilters + +data class QuoteFilters( + val quoteNumLike: String = IGNORE, + val validBefore: LocalDate = maxDate, + val validAfter: LocalDate = baseDate, + val totalAmountExceeds: Long = Long.MIN_VALUE, + val totalAmountLessThan: Long = Long.MAX_VALUE, +) : CustomFilters + +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, +) : CustomFilters + +data class IncomingInventoryFilters( + val mrnLike: String = IGNORE, + val vehicleLike: String = IGNORE, + val vendorBillAmountExceeds: Double = Double.MIN_VALUE, + val vendorBillAmountLessThan: Double = Double.MAX_VALUE +) : CustomFilters + +data class OutgoingInventoryFilters( + val mdnLike: String = IGNORE, + val purposeLike: String = IGNORE, + val personLike: String = IGNORE, + val vehicleLike: String = IGNORE, + val outMode: OutMode = OutMode.ALL +) : CustomFilters + +fun applyVendorHelper(q: io.ebean.ExpressionList, vids: List?) { if (vids.isNullOrEmpty()) return // q.apply { // q.`in`("vendor", vids) @@ -73,35 +92,40 @@ fun applyVendorHelper(q :io.ebean.ExpressionList, vids :List?) { // println(vids[0]) q.eq("vendor_sys_pk", vids[0]) } -fun applySortHelper(q :io.ebean.ExpressionList, sortBy :String, asc :Boolean) { - if(sortBy == IGNORE) return; + +fun applySortHelper(q: io.ebean.ExpressionList, sortBy: String, asc: Boolean) { + if (sortBy == IGNORE) return; val order = if (asc) "ASC" else "DESC" q.orderBy("$sortBy $order") } -fun applyFromToHelper(q :io.ebean.ExpressionList, fromDate: LocalDate, toDate: LocalDate, colName :String) { + +fun applyFromToHelper(q: io.ebean.ExpressionList, fromDate: LocalDate, toDate: LocalDate, colName: String) { q.ge(colName, fromDate) .le(colName, toDate) } -fun applyCommonFilters(q :io.ebean.ExpressionList, commonFilters: CommonFilters) { + +fun applyCommonFilters(q: io.ebean.ExpressionList, commonFilters: CommonFilters) { applyVendorHelper(q, commonFilters.vendor) applySortHelper(q, commonFilters.sortBy, commonFilters.sortAsc) } -fun searchQuotes(commonFilters: CommonFilters, quoteFilters: QuoteFilters) : List { + +fun searchQuotes(commonFilters: CommonFilters, quoteFilters: QuoteFilters): List { val q = database.find(Quotation::class.java) .where() - .ge("quoteDate", commonFilters.fromDate) - .le("quoteDate", commonFilters.toDate) - .ge("validTill",quoteFilters.validAfter) + .ge("quoteDate", commonFilters.from) + .le("quoteDate", commonFilters.to) + .ge("validTill", quoteFilters.validAfter) .le("validTill", quoteFilters.validBefore) .ge("totalAmount", quoteFilters.totalAmountExceeds) .le("totalAmount", quoteFilters.totalAmountLessThan) - .ilike("quoteNum", "%" + quoteFilters.quoteNumLike + "%") - applyFromToHelper(q, commonFilters.fromDate, commonFilters.toDate, "quoteDate") + .ilike("quoteNum", "%" + quoteFilters.quoteNumLike + "%") + applyFromToHelper(q, commonFilters.from, commonFilters.to, "quoteDate") applyVendorHelper(q, commonFilters.vendor) applySortHelper(q, commonFilters.sortBy, commonFilters.sortAsc) return q.findList() } -fun searchVendors(commonFilters: CommonFilters, vendorFilters: VendorFilters) : List { + +fun searchVendors(commonFilters: CommonFilters, vendorFilters: VendorFilters): List { val q = database.find(Vendor::class.java) .where() .ge("rating", vendorFilters.ratingExceeds) @@ -113,11 +137,25 @@ fun searchVendors(commonFilters: CommonFilters, vendorFilters: VendorFilters) : applySortHelper(q, commonFilters.sortBy, commonFilters.sortAsc) return q.findList() } -fun searchDocs(commonFilters: CommonFilters, documentFilters: DocumentFilters) : List { + +fun searchProducts(commonFilters: CommonFilters, productFilters: ProductFilters): List { + val q = database.find(Product::class.java) + .where() + .ilike("name", "%" + productFilters.nameLike + "%") + .ilike("hsnCode", "%" + productFilters.hsnLike + "%") + + if (productFilters.uom != UOM.ALL) { + q.eq("uom", productFilters.uom) + } + applySortHelper(q, commonFilters.sortBy, commonFilters.sortAsc) + return q.findList() +} + +fun searchDocs(commonFilters: CommonFilters, documentFilters: DocumentFilters): List { val q = database.find(Document::class.java) .where() .apply { - if(documentFilters.typeOfDoc != DocType.ALL){ + if (documentFilters.typeOfDoc != DocType.ALL) { this.eq("docType", documentFilters.typeOfDoc) } } @@ -126,7 +164,8 @@ fun searchDocs(commonFilters: CommonFilters, documentFilters: DocumentFilters) : applyVendorHelper(q, commonFilters.vendor) return q.findList() } -fun searchPos(commonFilters: CommonFilters, poFilters: POFilters?) : List { + +fun searchPos(commonFilters: CommonFilters, poFilters: POFilters?): List { val poFilters = poFilters ?: POFilters() val q = database.find(PurchaseOrder::class.java) .where() @@ -136,12 +175,13 @@ fun searchPos(commonFilters: CommonFilters, poFilters: POFilters?) : List { + +fun searchRFQ(commonFilters: CommonFilters, rfqFilters: RFQFilters): List { val q = database.find(ReqForQuote::class.java) .where() .ge("validTill", rfqFilters.validAfter) @@ -152,11 +192,37 @@ fun searchRFQ(commonFilters: CommonFilters, rfqFilters: RFQFilters) : List { - val p = database.find(Product::class.java) +fun searchIncomingInventory( + commonFilters: CommonFilters, + incomingInventoryFilters: IncomingInventoryFilters +): List { + val q = database.find(IncomingInventory::class.java) .where() - .ilike("hsnCode", productFilters.hsnLike) - .ilike("Pname", productFilters.nameLike) - applySortHelper(p, commonFilters.sortBy, commonFilters.sortAsc) - return p.findList() + .ge("vendorBillAmount", incomingInventoryFilters.vendorBillAmountExceeds) + .le("vendorBillAmount", incomingInventoryFilters.vendorBillAmountLessThan) + .ilike("mrn", "%" + incomingInventoryFilters.mrnLike + "%") + .ilike("vehicle", "%" + incomingInventoryFilters.vehicleLike + "%") + applyFromToHelper(q, commonFilters.from, commonFilters.to, "date") + applyVendorHelper(q, commonFilters.vendor) + applySortHelper(q, commonFilters.sortBy, commonFilters.sortAsc) + return q.findList() +} + +fun searchOutgoingInventory( + commonFilters: CommonFilters, + outgoingInventoryFilters: OutgoingInventoryFilters +): List { + val q = database.find(OutgoingInventory::class.java) + .where() + .ilike("mdn", "%" + outgoingInventoryFilters.mdnLike + "%") + .ilike("purpose", "%" + outgoingInventoryFilters.purposeLike + "%") + // .ilike("person", "%" + outgoingInventoryFilters.personLike + "%") + //.ilike("vehicle", "%" + outgoingInventoryFilters.vehicleLike + "%") + if (outgoingInventoryFilters.outMode != OutMode.ALL) { + q.eq("outMode", outgoingInventoryFilters.outMode) + } + applyFromToHelper(q, commonFilters.from, commonFilters.to, "date") + applySortHelper(q, commonFilters.sortBy, commonFilters.sortAsc) + + return q.findList() } \ No newline at end of file diff --git a/src/main/kotlin/com/restapi/domain/migration.kt b/src/main/kotlin/com/restapi/domain/migration.kt index f3806de..b3362e6 100644 --- a/src/main/kotlin/com/restapi/domain/migration.kt +++ b/src/main/kotlin/com/restapi/domain/migration.kt @@ -8,7 +8,7 @@ object DBMigration { private fun create(){ val dbMigration: DbMigration = DbMigration.create() dbMigration.setPlatform(Platform.POSTGRES) - + //dbMigration.setGeneratePendingDrop("1.8") dbMigration.generateMigration() } diff --git a/src/main/kotlin/com/restapi/domain/models.kt b/src/main/kotlin/com/restapi/domain/models.kt index eef9914..83860d8 100644 --- a/src/main/kotlin/com/restapi/domain/models.kt +++ b/src/main/kotlin/com/restapi/domain/models.kt @@ -9,11 +9,16 @@ 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 POProducts(val productId: String = "", val productName: String = "", val unitPrice :Double = 0.0, val quantity: Double = 0.0, val description :String = "") +data class POProducts( + val productId: String = "", + val productName: String = "", + val unitPrice: Double = 0.0, + val quantity: Double = 0.0, + val description: String = "" +) enum class ApprovalStatus { @@ -219,7 +224,6 @@ open class DataModel : BaseTenantModel() { } - @Entity @Index(unique = true, name = "unique_session_id", columnNames = ["session_id"]) open class AnonSession : BaseTenantModel() { @@ -245,29 +249,54 @@ class SafeStringDeserializer : JsonDeserializer() { } data class ContactPerson(val name: String = "", val email: String = "", val mobile: String = "") + @Entity -open class Vendor :BaseTenantModel() { - var name :String = "" - var msme :String = "" - var gstNumber :String = "" - var address :String = "" - var rating :Double = 0.0 +open class Vendor : BaseTenantModel() { + fun patchValues(updatedVendor: Vendor) { + this.name = updatedVendor.name + this.msme = updatedVendor.msme + this.gstNumber = updatedVendor.gstNumber + this.address = updatedVendor.address + this.rating = updatedVendor.rating + this.contacts = updatedVendor.contacts + } + + var name: String = "" + var msme: String = "" + var gstNumber: String = "" + var address: String = "" + var rating: Double = 0.0 + @DbJsonB - var contacts :List = mutableListOf() + var contacts: List = mutableListOf() } + @Entity -open class PurchaseOrder :BaseTenantModel() { +open class PurchaseOrder : BaseTenantModel() { + fun patchValues(updatedPo: PurchaseOrder) { + this.poDate = updatedPo.poDate + this.validTill = updatedPo.validTill + this.tnc = updatedPo.tnc + this.products = updatedPo.products + this.vendor = updatedPo.vendor + this.documents = updatedPo.documents + this.totalAmount = updatedPo.totalAmount + } + @DbJsonB - var products :MutableList = mutableListOf() + var products: MutableList = mutableListOf() + @ManyToOne - var vendor :Vendor? = null - var referenceQuotation :String? = "" - var totalAmount :Double = 0.0 + var vendor: Vendor? = null + var referenceQuotation: String? = "" + var totalAmount: Double = 0.0 var poNum: String = "" var poDate: LocalDate? = null var validTill: LocalDate? = null + @DbArray var tnc: List? = arrayListOf() + @DbArray var documents: List? = arrayListOf() } @@ -276,65 +305,156 @@ enum class UOM { NOS, LTR, MTR, ALL } -@Entity -open class Product :BaseTenantModel() { - var id: Long? = null - var name :String = "" - var description :String = "" - var hsnCode :String = "" - @Enumerated(EnumType.STRING) - var uom: UOM? = null +enum class TypeOfProduct { + RAW_MATERIAL } @Entity -open class Quotation :BaseTenantModel() { +open class Product : BaseTenantModel() { + fun patchValues(updatedProduct: Product) { + this.name = updatedProduct.name + this.description = updatedProduct.description + this.hsnCode = updatedProduct.hsnCode + this.uom = updatedProduct.uom + } + + var id: Long? = null + var name: String = "" + var description: String = "" + var hsnCode: String = "" + + @Enumerated(EnumType.STRING) + var uom: UOM? = null + + @Enumerated(EnumType.STRING) + var type: TypeOfProduct? = null + + +} + +@Entity +open class Quotation : BaseTenantModel() { + fun patchValues(updatedQuote: Quotation) { + this.quoteDate = updatedQuote.quoteDate + this.vendorQuoteNum = updatedQuote.vendorQuoteNum + this.validTill = updatedQuote.validTill + this.reqForQuoteNum = updatedQuote.reqForQuoteNum + this.tnc = updatedQuote.tnc + this.products = updatedQuote.products + this.vendor = updatedQuote.vendor + this.documents = updatedQuote.documents + this.totalAmount = updatedQuote.totalAmount + } + @DbJsonB - var products :MutableList = mutableListOf() + var products: MutableList = mutableListOf() + @ManyToOne - var vendor :Vendor? = null - var totalAmount :Double = 0.0 + var vendor: Vendor? = null + var totalAmount: Double = 0.0 var reqForQuoteNum: String? = "" var quoteNum: String = "" var vendorQuoteNum: String? = "" var quoteDate: LocalDate? = null var validTill: LocalDate? = null + @DbArray var tnc: List? = arrayListOf() @DbArray var documents: List? = arrayListOf() + + var taxesIncluded: Boolean ?= null } -enum class DocType{ +enum class DocType { PO, QUOTE, INVOICE, ALL } + @Entity -open class Document :BaseTenantModel() { - var name :String = "" +open class Document : BaseTenantModel() { + var name: String = "" + @Enumerated(EnumType.STRING) - var typeOfDoc :DocType? = null + var typeOfDoc: DocType? = null + //could be quoteNum, PoNum, InvoiceNum - var refId: String? = null - var description :String = "" - var url :String = "" - var docDate :LocalDate? = null - var vendor :Vendor? = null + var refIdOfDoc: Long? = null + var description: String = "" + var url: String = "" + var docDate: LocalDate? = null } -enum class RFQStatus{ +enum class RFQStatus { DELIVERED, PO, QUOTE, CANCELLED } + @Entity -open class ReqForQuote :BaseTenantModel() { +open class ReqForQuote : BaseTenantModel() { @DbArray - var potentialVendors :List? = null + var potentialVendors: List? = null + @Enumerated(EnumType.STRING) - var status :RFQStatus? = null -// @DbArray -// var docs :List? = null + var status: RFQStatus? = null + @DbJsonB - var products :List? = null + var products: List? = null var reqForQuoteNum: String? = null var openTill: LocalDate? = null +} + +@Entity +open class IncomingInventory : BaseTenantModel() { + fun patchValues(updated: IncomingInventory) { + this.date = updated.date + this.vendorBillNum = updated.vendorBillNum + this.vendorBillAmount = updated.vendorBillAmount + this.vehicle = updated.vehicle + this.products = updated.products + this.vendor = updated.vendor + } + + var mrn: String? = null + var date: LocalDate? = null + var vendorBillNum: String? = null + var vendorBillAmount: Double = 0.0 + var vehicle: String = "" + + @DbJsonB + var products: List? = null + + @ManyToOne + var vendor: Vendor? = null +} + + +enum class OutMode { + PERSON, VEHICLE, ALL +} + +@Entity +open class OutgoingInventory : BaseTenantModel() { + fun patchValues(updated: OutgoingInventory) { + this.date = updated.date + this.products = updated.products + this.purpose = updated.purpose + this.outMode = updated.outMode + this.person = updated.person + this.vehicle = updated.vehicle + + } + + var mdn: String? = null + var date: LocalDate? = null + + @DbJsonB + var products: List? = null + var purpose: String? = null + + @Enumerated(EnumType.STRING) + var outMode: OutMode? = null + + var person: String? = null + var vehicle: String? = null } \ No newline at end of file diff --git a/src/main/resources/dbmigration/1.10__dropsFor_1.7.sql b/src/main/resources/dbmigration/1.10__dropsFor_1.7.sql new file mode 100644 index 0000000..7bb2646 --- /dev/null +++ b/src/main/resources/dbmigration/1.10__dropsFor_1.7.sql @@ -0,0 +1,2 @@ +-- apply alter tables +alter table incoming_inventory drop column vendor_bil_num; diff --git a/src/main/resources/dbmigration/1.11__dropsFor_1.8.sql b/src/main/resources/dbmigration/1.11__dropsFor_1.8.sql new file mode 100644 index 0000000..f1e840c --- /dev/null +++ b/src/main/resources/dbmigration/1.11__dropsFor_1.8.sql @@ -0,0 +1,3 @@ +-- apply alter tables +alter table incoming_inventory drop column mdn; +alter table outgoing_inventory drop column mrn; diff --git a/src/main/resources/dbmigration/1.5.sql b/src/main/resources/dbmigration/1.5.sql new file mode 100644 index 0000000..d022737 --- /dev/null +++ b/src/main/resources/dbmigration/1.5.sql @@ -0,0 +1,2 @@ +-- apply alter tables +alter table document add column if not exists ref_id_of_doc bigint; diff --git a/src/main/resources/dbmigration/1.6.sql b/src/main/resources/dbmigration/1.6.sql new file mode 100644 index 0000000..a2a1b46 --- /dev/null +++ b/src/main/resources/dbmigration/1.6.sql @@ -0,0 +1,64 @@ +-- apply changes +create table incoming_inventory ( + sys_pk bigint generated by default as identity not null, + deleted_on timestamp, + current_approval_level integer default 0 not null, + required_approval_levels integer default 0 not null, + date date, + vendor_bill_amount float not null, + vendor_sys_pk bigint, + deleted boolean default false not null, + version integer default 1 not null, + created_at timestamp default 'now()' not null, + modified_at timestamp default 'now()' not null, + deleted_by varchar(255), + approval_status varchar(8) default 'APPROVED' not null, + tags varchar[] default '{}' not null, + comments jsonb default '[]' not null, + tenant_id varchar(255) not null, + mdn varchar(255) not null, + vendor_bil_num varchar(255) not null, + vehicle varchar(255) not null, + products jsonb, + created_by varchar(255) not null, + modified_by varchar(255) not null, + constraint ck_incoming_inventory_approval_status check ( approval_status in ('PENDING','APPROVED','REJECTED')), + constraint pk_incoming_inventory primary key (sys_pk) +); + +create table outgoing_inventory ( + sys_pk bigint generated by default as identity not null, + deleted_on timestamp, + current_approval_level integer default 0 not null, + required_approval_levels integer default 0 not null, + date date, + deleted boolean default false not null, + version integer default 1 not null, + created_at timestamp default 'now()' not null, + modified_at timestamp default 'now()' not null, + deleted_by varchar(255), + approval_status varchar(8) default 'APPROVED' not null, + tags varchar[] default '{}' not null, + comments jsonb default '[]' not null, + tenant_id varchar(255) not null, + mrn varchar(255) not null, + purpose varchar(255), + out_mode varchar(7), + person varchar(255), + vehicle varchar(255), + created_by varchar(255) not null, + modified_by varchar(255) not null, + constraint ck_outgoing_inventory_approval_status check ( approval_status in ('PENDING','APPROVED','REJECTED')), + constraint ck_outgoing_inventory_out_mode check ( out_mode in ('PERSON','VEHICLE','ALL')), + constraint pk_outgoing_inventory primary key (sys_pk) +); + +-- apply alter tables +alter table product add column if not exists type varchar(12); +alter table quotation add column if not exists taxes_included boolean; +-- apply post alter +alter table product add constraint ck_product_type check ( type in ('RAW_MATERIAL')); +-- foreign keys and indices +create index ix_incoming_inventory_vendor_sys_pk on incoming_inventory (vendor_sys_pk); +alter table incoming_inventory add constraint fk_incoming_inventory_vendor_sys_pk foreign key (vendor_sys_pk) references vendor (sys_pk) on delete restrict on update restrict; + diff --git a/src/main/resources/dbmigration/1.7.sql b/src/main/resources/dbmigration/1.7.sql new file mode 100644 index 0000000..7c3df62 --- /dev/null +++ b/src/main/resources/dbmigration/1.7.sql @@ -0,0 +1,2 @@ +-- apply alter tables +alter table incoming_inventory add column if not exists vendor_bill_num varchar(255); diff --git a/src/main/resources/dbmigration/1.8.sql b/src/main/resources/dbmigration/1.8.sql new file mode 100644 index 0000000..1028a42 --- /dev/null +++ b/src/main/resources/dbmigration/1.8.sql @@ -0,0 +1,4 @@ +-- apply alter tables +alter table incoming_inventory add column if not exists mrn varchar(255); +alter table outgoing_inventory add column if not exists mdn varchar(255); +alter table outgoing_inventory add column if not exists products jsonb; diff --git a/src/main/resources/dbmigration/1.9__dropsFor_1.5.sql b/src/main/resources/dbmigration/1.9__dropsFor_1.5.sql new file mode 100644 index 0000000..bb4df97 --- /dev/null +++ b/src/main/resources/dbmigration/1.9__dropsFor_1.5.sql @@ -0,0 +1,2 @@ +-- apply alter tables +alter table document drop column ref_id; diff --git a/src/main/resources/dbmigration/model/1.10__dropsFor_1.7.model.xml b/src/main/resources/dbmigration/model/1.10__dropsFor_1.7.model.xml new file mode 100644 index 0000000..9f340d4 --- /dev/null +++ b/src/main/resources/dbmigration/model/1.10__dropsFor_1.7.model.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/resources/dbmigration/model/1.11__dropsFor_1.8.model.xml b/src/main/resources/dbmigration/model/1.11__dropsFor_1.8.model.xml new file mode 100644 index 0000000..3641709 --- /dev/null +++ b/src/main/resources/dbmigration/model/1.11__dropsFor_1.8.model.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/src/main/resources/dbmigration/model/1.5.model.xml b/src/main/resources/dbmigration/model/1.5.model.xml new file mode 100644 index 0000000..695974a --- /dev/null +++ b/src/main/resources/dbmigration/model/1.5.model.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/dbmigration/model/1.6.model.xml b/src/main/resources/dbmigration/model/1.6.model.xml new file mode 100644 index 0000000..bf11a71 --- /dev/null +++ b/src/main/resources/dbmigration/model/1.6.model.xml @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/dbmigration/model/1.7.model.xml b/src/main/resources/dbmigration/model/1.7.model.xml new file mode 100644 index 0000000..d6a6659 --- /dev/null +++ b/src/main/resources/dbmigration/model/1.7.model.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/dbmigration/model/1.8.model.xml b/src/main/resources/dbmigration/model/1.8.model.xml new file mode 100644 index 0000000..205ce77 --- /dev/null +++ b/src/main/resources/dbmigration/model/1.8.model.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/dbmigration/model/1.9__dropsFor_1.5.model.xml b/src/main/resources/dbmigration/model/1.9__dropsFor_1.5.model.xml new file mode 100644 index 0000000..40c91db --- /dev/null +++ b/src/main/resources/dbmigration/model/1.9__dropsFor_1.5.model.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml index 1cbfb22..5ba8f96 100644 --- a/src/main/resources/logback.xml +++ b/src/main/resources/logback.xml @@ -1,4 +1,19 @@ + + api.log + + api.%d{yyyy-MM-dd}.%i.log.gz + 30 + + 100MB + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n @@ -14,5 +29,6 @@ + \ No newline at end of file