diff --git a/src/main/kotlin/com/restapi/controllers/Entities.kt b/src/main/kotlin/com/restapi/controllers/Entities.kt index cf8ad6d..fd9eab9 100644 --- a/src/main/kotlin/com/restapi/controllers/Entities.kt +++ b/src/main/kotlin/com/restapi/controllers/Entities.kt @@ -10,7 +10,6 @@ import com.restapi.domain.Session.currentUser import com.restapi.domain.Session.database import com.restapi.domain.Session.findDataModelByEntityAndUniqId import com.restapi.integ.Scripting -import com.restapi.integ.logger import io.ebean.CallableSql import io.ebean.RawSqlBuilder import io.javalin.http.* @@ -373,9 +372,7 @@ object Entities { } } -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) { @@ -483,11 +480,6 @@ object PurchaseOrderCtrl { } } -data class ProductSearch( - var isSort: String? = null - -) - object ProductCtrl { fun get(ctx: Context) { val id = ctx.pathParam("id") @@ -825,129 +817,6 @@ object RequestForQuote { } } -object IncomingInventoryCtrl { - fun plantsForUser(ctx: Context) { - ctx.json( - Session.currentUserPlants() - ) - } - fun updatePlant(ctx: Context) { - val p = ctx.bodyAsClass() - val plant = database.find(Plant::class.java, ctx.pathParam("id")) - plant.patch(p) - ctx.json( - Session.currentUserPlants() - ) - } - - 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 plantId = ctx.queryParam("plantId") ?: throw BadRequestResponse("plantId not sent") - 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 = (database.find(IncomingInventory::class.java) - .where() - .eq("unloadingPlantId", plantId) - .findCount() + 1) - .toString() - .padStart(6, '0') - val seq = SequenceNumber(inventoryPrefix + 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) { - 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) - } -} - object PaymentCtrl { fun create(ctx: Context) { val pmt = ctx.bodyAsClass(Payment::class.java) @@ -1084,317 +953,3 @@ object InvoiceCtrl { } } -object FleetCtrl { - fun create(ctx: Context) { - val fleet = ctx.bodyAsClass() - database.save(fleet) - ctx.json(fleet).status(HttpStatus.CREATED) - } - - fun get(ctx: Context) { - val id = ctx.pathParam("id").toLong() - val fleet = database.find(Fleet::class.java, id) - ?: throw NotFoundResponse("No fleet found with id $id") - ctx.json(fleet).status(HttpStatus.OK) - } - - data class FLTF(val common: CommonFilters, val fleetFilters: FleetFilters) - - fun getAll(ctx: Context) { - val filters = ctx.bodyAsClass() - val fleets = searchFleets(filters.common, filters.fleetFilters) - val excel = ctx.queryParam("excel") - if (excel !== null) { - exportFleets(fleets) - val inputStream = FileInputStream("./excel/Fleets.xls") - ctx.result(inputStream).status(HttpStatus.OK) - } else { - ctx.json(fleets).status(HttpStatus.OK) - } - - } - - fun update(ctx: Context) { - val id = ctx.pathParam("id").toLong() - val fleet = - database.find(Fleet::class.java, id) ?: throw NotFoundResponse("fleet not found for $id") - val updatedFleet = ctx.bodyAsClass() - fleet.patchValues(updatedFleet) - fleet.update() - ctx.json(fleet).status(HttpStatus.OK) - } - - fun delete(ctx: Context) { - - } -} - -object VehicleCtrl { - fun create(ctx: Context) { - val vehicle = ctx.bodyAsClass() - database.save(vehicle) - ctx.json(vehicle).status(HttpStatus.CREATED) - } - - fun get(ctx: Context) { - val id = ctx.pathParam("id").toLong() - val vehicle = database.find(Vehicle::class.java, id) - ?: throw NotFoundResponse("No vehicle found with id $id") - ctx.json(vehicle).status(HttpStatus.OK) - } - - fun getAll(ctx: Context) { - val vehicles = database.find(Vehicle::class.java).findList() - val excel = ctx.queryParam("excel") - if (excel !== null) { - // exportVehicles(vehicles) - val inputStream = FileInputStream("./excel/Vehicles.xls") - ctx.result(inputStream).status(HttpStatus.OK) - } else { - ctx.json(vehicles).status(HttpStatus.OK) - } - - } - - fun update(ctx: Context) { - val id = ctx.pathParam("id").toLong() - val vehicle = - database.find(Vehicle::class.java, id) ?: throw NotFoundResponse("vehicle not found for $id") - val updatedVehicle = ctx.bodyAsClass() - vehicle.patchValues(updatedVehicle) - vehicle.update() - ctx.json(vehicle).status(HttpStatus.OK) - } -} - -object FleetTypeCtrl { - fun create(ctx: Context) { - val fleetType = ctx.bodyAsClass() - database.save(fleetType) - ctx.json(fleetType).status(HttpStatus.CREATED) - } - - fun get(ctx: Context) { - val id = ctx.pathParam("id").toLong() - val fleetType = database.find(FleetType::class.java, id) - ?: throw NotFoundResponse("No fleetType found with id $id") - ctx.json(fleetType).status(HttpStatus.OK) - } - - fun getAll(ctx: Context) { - val fleetTypes = database.find(FleetType::class.java).findList() - val excel = ctx.queryParam("excel") - if (excel !== null) { - // exportFleetTypes(fleetTypes) - val inputStream = FileInputStream("./excel/FleetTypes.xls") - ctx.result(inputStream).status(HttpStatus.OK) - } else { - ctx.json(fleetTypes).status(HttpStatus.OK) - } - - } - - fun update(ctx: Context) { - val id = ctx.pathParam("id").toLong() - val fleetType = - database.find(FleetType::class.java, id) ?: throw NotFoundResponse("fleetType not found for $id") - val updatedFleetType = ctx.bodyAsClass() - fleetType.patchValues(updatedFleetType) - fleetType.update() - ctx.json(fleetType).status(HttpStatus.OK) - } -} - -object RenewalCtrl { - fun create(ctx: Context) { - val renewal = ctx.bodyAsClass() - database.save(renewal) - ctx.json(renewal).status(HttpStatus.CREATED) - } - - fun get(ctx: Context) { - val id = ctx.pathParam("id").toLong() - val renewal = database.find(Renewal::class.java, id) - ?: throw NotFoundResponse("No renewal found with id $id") - ctx.json(renewal).status(HttpStatus.OK) - } - - //data class RNLF(val common: CommonFilters, val renewalFilters: RenewalFilters) - fun getAll(ctx: Context) { - // val filters = ctx.bodyAsClass() - // val renewals = searchRenewals(filters.common, filters.renewalFilters) - val renewals = database.find(Renewal::class.java).findList() - val excel = ctx.queryParam("excel") - if (excel !== null) { - // exportRenewals(renewals) - val inputStream = FileInputStream("./excel/Renewals.xls") - ctx.result(inputStream).status(HttpStatus.OK) - } else { - ctx.json(renewals).status(HttpStatus.OK) - } - - } - - fun update(ctx: Context) { - val id = ctx.pathParam("id").toLong() - val renewal = - database.find(Renewal::class.java, id) ?: throw NotFoundResponse("renewal not found for $id") - val updatedRenewal = ctx.bodyAsClass() - renewal.patchValues(updatedRenewal) - renewal.update() - ctx.json(renewal).status(HttpStatus.OK) - } - - fun delete(ctx: Context) { - - } -} - -object ReminderCtrl { - fun create(ctx: Context) { - val renewal = ctx.bodyAsClass() - database.save(renewal) - val entry = ReminderLog() - entry.apply { - reminder = renewal - reminderDate = renewal.nextRenewalDate - reminderType = renewal.type - fleet = renewal.fleet - amount = 0.0 - actedUpon = false - } - ReminderLogCtrl.create(entry) - ctx.json(renewal).status(HttpStatus.CREATED) - } - - fun getAllByFleetId(ctx: Context) { - val fleetId = ctx.pathParam("id").toLong() - val history = database.find(Reminder::class.java) - .where() - .eq("fleet_sys_pk", fleetId) - .findList() - ctx.json(history).status(HttpStatus.OK) - } - - fun get(ctx: Context) { - val id = ctx.pathParam("id").toLong() - val renewal = database.find(Reminder::class.java, id) - ?: throw NotFoundResponse("No renewal found with id $id") - ctx.json(renewal).status(HttpStatus.OK) - } - - //data class RNLF(val common: CommonFilters, val renewalFilters: ReminderFilters) - fun getAll(ctx: Context) { - // val filters = ctx.bodyAsClass() - // val renewals = searchReminders(filters.common, filters.renewalFilters) - val renewals = database.find(Reminder::class.java).findList() - val excel = ctx.queryParam("excel") - if (excel !== null) { - // exportReminders(renewals) - val inputStream = FileInputStream("./excel/Reminders.xls") - ctx.result(inputStream).status(HttpStatus.OK) - } else { - ctx.json(renewals).status(HttpStatus.OK) - } - - } - - fun update(ctx: Context) { - val id = ctx.pathParam("id").toLong() - val renewal = - database.find(Reminder::class.java, id) ?: throw NotFoundResponse("renewal not found for $id") - val updatedReminder = ctx.bodyAsClass() - renewal.patchValues(updatedReminder) - renewal.update() - ctx.json(renewal).status(HttpStatus.OK) - } - - fun delete(ctx: Context) { - - } -} - -object ReminderLogCtrl { - fun create(entry: ReminderLog) { - // val renewal = ctx.bodyAsClass() - - database.save(entry) - return - //ctx.json(renewal).status(HttpStatus.CREATED) - } - - data class Done( - val logId: Long, - val reminderId: Long, - val renewalDate: LocalDate, - val nextRenewalDate: LocalDate, - val amount: Double, - val documents: List? - ) - - fun done(ctx: Context) { - val req = ctx.bodyAsClass() - val reminder = - database.find(Reminder::class.java, req.reminderId) ?: throw NotFoundResponse("No Reminder found") - //update reminder - reminder.apply { - lastRenewalDate = req.renewalDate - nextRenewalDate = req.nextRenewalDate - } - reminder.update() - val entry = database.find(ReminderLog::class.java, req.logId) ?: throw NotFoundResponse("Log not found") - entry.apply { - actedUpon = true - documents = req.documents - amount = req.amount - } - entry.update() - ctx.json(entry).status(HttpStatus.OK) - } - - fun getAllByFleetId(ctx: Context) { - val fleetId = ctx.pathParam("id").toLong() - val history = database.find(ReminderLog::class.java) - .where() - .eq("fleet_sys_pk", fleetId) - .findList() - ctx.json(history).status(HttpStatus.OK) - } - - fun get(ctx: Context) { - val id = ctx.pathParam("id").toLong() - val renewal = database.find(ReminderLog::class.java, id) - ?: throw NotFoundResponse("No renewal found with id $id") - ctx.json(renewal).status(HttpStatus.OK) - } - - data class LOGF(val common: CommonFilters, val reminderLogFilters: ReminderLogFilters) - - fun getAll(ctx: Context) { - val filters = ctx.bodyAsClass() - val entries = searchReminderLogs(filters.common, filters.reminderLogFilters) - val excel = ctx.queryParam("excel") - if (excel !== null) { - exportReminderLogs(entries) - val inputStream = FileInputStream("./excel/ReminderLogs.xls") - ctx.result(inputStream).status(HttpStatus.OK) - } else { - ctx.json(entries).status(HttpStatus.OK) - } - - } - - fun update(ctx: Context) { - val id = ctx.pathParam("id").toLong() - val renewal = - database.find(ReminderLog::class.java, id) ?: throw NotFoundResponse("renewal not found for $id") - val updatedReminderLog = ctx.bodyAsClass() - renewal.patchValues(updatedReminderLog) - renewal.update() - ctx.json(renewal).status(HttpStatus.OK) - } - - fun delete(ctx: Context) { - - } -} diff --git a/src/main/kotlin/com/restapi/controllers/Filters.kt b/src/main/kotlin/com/restapi/controllers/Filters.kt index 933ef53..ac474b3 100644 --- a/src/main/kotlin/com/restapi/controllers/Filters.kt +++ b/src/main/kotlin/com/restapi/controllers/Filters.kt @@ -229,14 +229,10 @@ fun searchIncomingInventory( commonFilters: CommonFilters, incomingInventoryFilters: IncomingInventoryFilters ): List { - val q = database.find(IncomingInventory::class.java) - .where() - //.ge("vendorBillAmount", incomingInventoryFilters.vendorBillAmountExceeds) - //.le("vendorBillAmount", incomingInventoryFilters.vendorBillAmountLessThan) - //.ilike("mrn", "%" + incomingInventoryFilters.mrnLike + "%") - //.ilike("vehicle", "%" + incomingInventoryFilters.vehicleLike + "%") + val q = database.find(IncomingInventory::class.java).where() applyFromToHelper(q, commonFilters.from, commonFilters.to, "date") applyVendorHelper(q, commonFilters.vendor) + q.`in`("unloading", Session.currentUserPlants().map { it.plantId }) applySortHelper(q, commonFilters.sortBy, commonFilters.sortAsc) return q.findList() } diff --git a/src/main/kotlin/com/restapi/controllers/Fleets.kt b/src/main/kotlin/com/restapi/controllers/Fleets.kt new file mode 100644 index 0000000..b4804f0 --- /dev/null +++ b/src/main/kotlin/com/restapi/controllers/Fleets.kt @@ -0,0 +1,324 @@ +package com.restapi.controllers + +import com.restapi.domain.* +import io.javalin.http.Context +import io.javalin.http.HttpStatus +import io.javalin.http.NotFoundResponse +import io.javalin.http.bodyAsClass +import java.io.FileInputStream +import java.time.LocalDate + +object FleetCtrl { + fun create(ctx: Context) { + val fleet = ctx.bodyAsClass() + Session.database.save(fleet) + ctx.json(fleet).status(HttpStatus.CREATED) + } + + fun get(ctx: Context) { + val id = ctx.pathParam("id").toLong() + val fleet = Session.database.find(Fleet::class.java, id) + ?: throw NotFoundResponse("No fleet found with id $id") + ctx.json(fleet).status(HttpStatus.OK) + } + + data class FLTF(val common: CommonFilters, val fleetFilters: FleetFilters) + + fun getAll(ctx: Context) { + val filters = ctx.bodyAsClass() + val fleets = searchFleets(filters.common, filters.fleetFilters) + val excel = ctx.queryParam("excel") + if (excel !== null) { + exportFleets(fleets) + val inputStream = FileInputStream("./excel/Fleets.xls") + ctx.result(inputStream).status(HttpStatus.OK) + } else { + ctx.json(fleets).status(HttpStatus.OK) + } + + } + + fun update(ctx: Context) { + val id = ctx.pathParam("id").toLong() + val fleet = + Session.database.find(Fleet::class.java, id) ?: throw NotFoundResponse("fleet not found for $id") + val updatedFleet = ctx.bodyAsClass() + fleet.patchValues(updatedFleet) + fleet.update() + ctx.json(fleet).status(HttpStatus.OK) + } + + fun delete(ctx: Context) { + + } +} + +object VehicleCtrl { + fun create(ctx: Context) { + val vehicle = ctx.bodyAsClass() + Session.database.save(vehicle) + ctx.json(vehicle).status(HttpStatus.CREATED) + } + + fun get(ctx: Context) { + val id = ctx.pathParam("id").toLong() + val vehicle = Session.database.find(Vehicle::class.java, id) + ?: throw NotFoundResponse("No vehicle found with id $id") + ctx.json(vehicle).status(HttpStatus.OK) + } + + fun getAll(ctx: Context) { + val vehicles = Session.database.find(Vehicle::class.java).findList() + val excel = ctx.queryParam("excel") + if (excel !== null) { + // exportVehicles(vehicles) + val inputStream = FileInputStream("./excel/Vehicles.xls") + ctx.result(inputStream).status(HttpStatus.OK) + } else { + ctx.json(vehicles).status(HttpStatus.OK) + } + + } + + fun update(ctx: Context) { + val id = ctx.pathParam("id").toLong() + val vehicle = + Session.database.find(Vehicle::class.java, id) ?: throw NotFoundResponse("vehicle not found for $id") + val updatedVehicle = ctx.bodyAsClass() + vehicle.patchValues(updatedVehicle) + vehicle.update() + ctx.json(vehicle).status(HttpStatus.OK) + } +} + +object FleetTypeCtrl { + fun create(ctx: Context) { + val fleetType = ctx.bodyAsClass() + Session.database.save(fleetType) + ctx.json(fleetType).status(HttpStatus.CREATED) + } + + fun get(ctx: Context) { + val id = ctx.pathParam("id").toLong() + val fleetType = Session.database.find(FleetType::class.java, id) + ?: throw NotFoundResponse("No fleetType found with id $id") + ctx.json(fleetType).status(HttpStatus.OK) + } + + fun getAll(ctx: Context) { + val fleetTypes = Session.database.find(FleetType::class.java).findList() + val excel = ctx.queryParam("excel") + if (excel !== null) { + // exportFleetTypes(fleetTypes) + val inputStream = FileInputStream("./excel/FleetTypes.xls") + ctx.result(inputStream).status(HttpStatus.OK) + } else { + ctx.json(fleetTypes).status(HttpStatus.OK) + } + + } + + fun update(ctx: Context) { + val id = ctx.pathParam("id").toLong() + val fleetType = + Session.database.find(FleetType::class.java, id) ?: throw NotFoundResponse("fleetType not found for $id") + val updatedFleetType = ctx.bodyAsClass() + fleetType.patchValues(updatedFleetType) + fleetType.update() + ctx.json(fleetType).status(HttpStatus.OK) + } +} + +object RenewalCtrl { + fun create(ctx: Context) { + val renewal = ctx.bodyAsClass() + Session.database.save(renewal) + ctx.json(renewal).status(HttpStatus.CREATED) + } + + fun get(ctx: Context) { + val id = ctx.pathParam("id").toLong() + val renewal = Session.database.find(Renewal::class.java, id) + ?: throw NotFoundResponse("No renewal found with id $id") + ctx.json(renewal).status(HttpStatus.OK) + } + + //data class RNLF(val common: CommonFilters, val renewalFilters: RenewalFilters) + fun getAll(ctx: Context) { + // val filters = ctx.bodyAsClass() + // val renewals = searchRenewals(filters.common, filters.renewalFilters) + val renewals = Session.database.find(Renewal::class.java).findList() + val excel = ctx.queryParam("excel") + if (excel !== null) { + // exportRenewals(renewals) + val inputStream = FileInputStream("./excel/Renewals.xls") + ctx.result(inputStream).status(HttpStatus.OK) + } else { + ctx.json(renewals).status(HttpStatus.OK) + } + + } + + fun update(ctx: Context) { + val id = ctx.pathParam("id").toLong() + val renewal = + Session.database.find(Renewal::class.java, id) ?: throw NotFoundResponse("renewal not found for $id") + val updatedRenewal = ctx.bodyAsClass() + renewal.patchValues(updatedRenewal) + renewal.update() + ctx.json(renewal).status(HttpStatus.OK) + } + + fun delete(ctx: Context) { + + } +} + +object ReminderCtrl { + fun create(ctx: Context) { + val renewal = ctx.bodyAsClass() + Session.database.save(renewal) + val entry = ReminderLog() + entry.apply { + reminder = renewal + reminderDate = renewal.nextRenewalDate + reminderType = renewal.type + fleet = renewal.fleet + amount = 0.0 + actedUpon = false + } + ReminderLogCtrl.create(entry) + ctx.json(renewal).status(HttpStatus.CREATED) + } + + fun getAllByFleetId(ctx: Context) { + val fleetId = ctx.pathParam("id").toLong() + val history = Session.database.find(Reminder::class.java) + .where() + .eq("fleet_sys_pk", fleetId) + .findList() + ctx.json(history).status(HttpStatus.OK) + } + + fun get(ctx: Context) { + val id = ctx.pathParam("id").toLong() + val renewal = Session.database.find(Reminder::class.java, id) + ?: throw NotFoundResponse("No renewal found with id $id") + ctx.json(renewal).status(HttpStatus.OK) + } + + //data class RNLF(val common: CommonFilters, val renewalFilters: ReminderFilters) + fun getAll(ctx: Context) { + // val filters = ctx.bodyAsClass() + // val renewals = searchReminders(filters.common, filters.renewalFilters) + val renewals = Session.database.find(Reminder::class.java).findList() + val excel = ctx.queryParam("excel") + if (excel !== null) { + // exportReminders(renewals) + val inputStream = FileInputStream("./excel/Reminders.xls") + ctx.result(inputStream).status(HttpStatus.OK) + } else { + ctx.json(renewals).status(HttpStatus.OK) + } + + } + + fun update(ctx: Context) { + val id = ctx.pathParam("id").toLong() + val renewal = + Session.database.find(Reminder::class.java, id) ?: throw NotFoundResponse("renewal not found for $id") + val updatedReminder = ctx.bodyAsClass() + renewal.patchValues(updatedReminder) + renewal.update() + ctx.json(renewal).status(HttpStatus.OK) + } + + fun delete(ctx: Context) { + + } +} + +object ReminderLogCtrl { + fun create(entry: ReminderLog) { + // val renewal = ctx.bodyAsClass() + + Session.database.save(entry) + return + //ctx.json(renewal).status(HttpStatus.CREATED) + } + + data class Done( + val logId: Long, + val reminderId: Long, + val renewalDate: LocalDate, + val nextRenewalDate: LocalDate, + val amount: Double, + val documents: List? + ) + + fun done(ctx: Context) { + val req = ctx.bodyAsClass() + val reminder = + Session.database.find(Reminder::class.java, req.reminderId) ?: throw NotFoundResponse("No Reminder found") + //update reminder + reminder.apply { + lastRenewalDate = req.renewalDate + nextRenewalDate = req.nextRenewalDate + } + reminder.update() + val entry = Session.database.find(ReminderLog::class.java, req.logId) ?: throw NotFoundResponse("Log not found") + entry.apply { + actedUpon = true + documents = req.documents + amount = req.amount + } + entry.update() + ctx.json(entry).status(HttpStatus.OK) + } + + fun getAllByFleetId(ctx: Context) { + val fleetId = ctx.pathParam("id").toLong() + val history = Session.database.find(ReminderLog::class.java) + .where() + .eq("fleet_sys_pk", fleetId) + .findList() + ctx.json(history).status(HttpStatus.OK) + } + + fun get(ctx: Context) { + val id = ctx.pathParam("id").toLong() + val renewal = Session.database.find(ReminderLog::class.java, id) + ?: throw NotFoundResponse("No renewal found with id $id") + ctx.json(renewal).status(HttpStatus.OK) + } + + data class LOGF(val common: CommonFilters, val reminderLogFilters: ReminderLogFilters) + + fun getAll(ctx: Context) { + val filters = ctx.bodyAsClass() + val entries = searchReminderLogs(filters.common, filters.reminderLogFilters) + val excel = ctx.queryParam("excel") + if (excel !== null) { + exportReminderLogs(entries) + val inputStream = FileInputStream("./excel/ReminderLogs.xls") + ctx.result(inputStream).status(HttpStatus.OK) + } else { + ctx.json(entries).status(HttpStatus.OK) + } + + } + + fun update(ctx: Context) { + val id = ctx.pathParam("id").toLong() + val renewal = + Session.database.find(ReminderLog::class.java, id) ?: throw NotFoundResponse("renewal not found for $id") + val updatedReminderLog = ctx.bodyAsClass() + renewal.patchValues(updatedReminderLog) + renewal.update() + ctx.json(renewal).status(HttpStatus.OK) + } + + fun delete(ctx: Context) { + + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/restapi/controllers/Inventories.kt b/src/main/kotlin/com/restapi/controllers/Inventories.kt new file mode 100644 index 0000000..53b5c8c --- /dev/null +++ b/src/main/kotlin/com/restapi/controllers/Inventories.kt @@ -0,0 +1,131 @@ +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 io.javalin.http.* +import java.io.FileInputStream + +object IncomingInventoryCtrl { + fun plantsForUser(ctx: Context) { + ctx.json( + Session.currentUserPlants() + ) + } + fun updatePlant(ctx: Context) { + val p = ctx.bodyAsClass() + val plant = Session.database.find(Plant::class.java, ctx.pathParam("id")) + plant.patch(p) + ctx.json( + Session.currentUserPlants() + ) + } + + fun create(ctx: Context) { + val ticket = ctx.bodyAsClass() + Session.database.save(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") + 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 = Session.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 plantId = ctx.queryParam("plantId") ?: throw BadRequestResponse("plantId not sent") + val plant = Session.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) + .where() + .eq("unloadingPlantId", plantId) + .findCount() + 1) + .toString() + .padStart(6, '0') + val seq = SequenceNumber(inventoryPrefix + cnt) + ctx.json(seq).status(HttpStatus.OK) + } +} + +object OutgoingInventoryCtrl { + fun create(ctx: Context) { + val ticket = ctx.bodyAsClass() + Session.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") + 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 = Session.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) { + val prefix = "MDN/" + val cnt = Session.database.find(OutgoingInventory::class.java) + .findCount() + .toString() + .padStart(6, '0') + val seq = SequenceNumber(prefix + cnt) + ctx.json(seq).status(HttpStatus.OK) + } +} \ No newline at end of file