add fleet

This commit is contained in:
arsalan 2024-03-12 17:56:11 +05:30
parent 67b7c5aa9a
commit 32d4ea28f5
6 changed files with 265 additions and 0 deletions

View File

@ -189,6 +189,36 @@ fun main(args: Array<String>) {
)
delete("/{id}", PaymentCtrl::delete, Roles(Role.Explicit("ROLE_PAYMENT_CREATE")))
}
path("/fleet") {
post("", FleetCtrl::create, Roles(Role.Explicit("ROLE_FLEET_CREATE")))
get(
"/{id}",
FleetCtrl::get,
Roles(Role.Explicit("ROLE_FLEET_VIEW", "ROLE_FLEET_CREATE"))
)
put("/{id}", FleetCtrl::update, Roles(Role.Explicit("ROLE_FLEET_CREATE")))
post(
"/getAll",
FleetCtrl::getAll,
Roles(Role.Explicit("ROLE_FLEET_CREATE", "ROLE_FLEET_VIEW"))
)
delete("/{id}", FleetCtrl::delete, Roles(Role.Explicit("ROLE_FLEET_CREATE")))
}
path("/renewal") {
post("", RenewalCtrl::create, Roles(Role.Explicit("ROLE_FLEET_CREATE")))
get(
"/{id}",
RenewalCtrl::get,
Roles(Role.Explicit("ROLE_FLEET_VIEW", "ROLE_FLEET_CREATE"))
)
put("/{id}", RenewalCtrl::update, Roles(Role.Explicit("ROLE_FLEET_CREATE")))
post(
"/getAll",
RenewalCtrl::getAll,
Roles(Role.Explicit("ROLE_FLEET_CREATE", "ROLE_FLEET_VIEW"))
)
delete("/{id}", RenewalCtrl::delete, Roles(Role.Explicit("ROLE_FLEET_CREATE")))
}
path("/po") {
get("/next", PurchaseOrderCtrl::getNextNum, Roles(Role.Explicit("ROLE_PO_CREATE")))
post("", PurchaseOrderCtrl::create, Roles(Role.Explicit("ROLE_PO_CREATE")))

View File

@ -1040,3 +1040,84 @@ object InvoiceCtrl {
ctx.json(seq).status(HttpStatus.OK)
}
}
object FleetCtrl {
fun create(ctx: Context) {
val fleet = ctx.bodyAsClass<Fleet>()
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<FLTF>()
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>()
fleet.patchValues(updatedFleet)
fleet.update()
ctx.json(fleet).status(HttpStatus.OK)
}
fun delete(ctx: Context) {
}
}
object RenewalCtrl{
fun create(ctx: Context) {
val renewal = ctx.bodyAsClass<Renewal>()
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<FLTF>()
// 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>()
renewal.patchValues(updatedRenewal)
renewal.update()
ctx.json(renewal).status(HttpStatus.OK)
}
fun delete(ctx: Context) {
}
}

View File

@ -97,6 +97,10 @@ data class PaymentFilters(
val amountLessThan: Double = Double.MAX_VALUE
) : CustomFilters
data class FleetFilters(
val fleetNameLike: String = IGNORE
):CustomFilters
fun <T> applyVendorHelper(q: io.ebean.ExpressionList<T>, vids: List<Long>?) {
if (vids.isNullOrEmpty()) return
// q.apply {
@ -266,4 +270,21 @@ fun searchPayments(commonFilters: CommonFilters, paymentFilters: PaymentFilters)
return q.findList()
}
fun searchFleets(filters: CommonFilters, fleetFilters: FleetFilters) : List<Fleet> {
val q = database.find(Fleet::class.java)
.where()
.ilike("name", "%" + fleetFilters.fleetNameLike + "%")
q.raw("(insurance_renewal_date >= ? and insurance_renewal_date <= ?)" +
"or (pollution_renewal_date >= ? and pollution_renewal_date <= ?)" +
"or (fitness_renewal_date >= ? and fitness_renewal_date <= ?)",
filters.from, filters.to, filters.from, filters.to, filters.from, filters.to
)
// applyFromToHelper(q, filters.from, filters.to, "insuranceRenewalDate")
// applyFromToHelper(q, filters.from, filters.to, "pollutionRenewalDate")
// applyFromToHelper(q, filters.from, filters.to, "fitnessRenewalDate")
return q.findList()
}
//if date is null then fromtoheper drops that///

View File

@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import io.ebean.Model
import io.ebean.annotation.*
import io.ebean.annotation.Index
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STRestartNumber
import java.time.LocalDate
import java.time.LocalDateTime
import javax.persistence.*
@ -517,4 +518,69 @@ open class Payment : BaseTenantModel() {
@ManyToOne
var vendor: Vendor? = null
}
data class FleetRenewal(val renewal : String, val date : LocalDate)
@Entity
open class Fleet : BaseTenantModel() {
fun patchValues(updated: Fleet) {
this.name = updated.name
this.type = updated.type
this.regNumber = updated.regNumber
this.model = updated.model
this.make = updated.make
this.driver = updated.driver
this.driverContact = updated.driverContact
this.mileage = updated.mileage
this.cost = updated.cost
this.insuranceRenewalDate = updated.insuranceRenewalDate
this.pollutionRenewalDate = updated.pollutionRenewalDate
this.fitnessRenewalDate = updated.fitnessRenewalDate
this.renewals = updated.renewals
}
var name: String = ""
var type: String = ""
var regNumber: String = ""
var model: String = ""
var make: String = ""
var driver: String = ""
var driverContact: String = ""
var mileage: Double = 0.0
var cost: Double = 0.0
var insuranceRenewalDate: LocalDate? = null
var pollutionRenewalDate: LocalDate? = null
var fitnessRenewalDate: LocalDate? = null
@DbJsonB
var renewals: List<FleetRenewal>?=null
}
@Entity
open class FleetType : BaseTenantModel() {
fun patchValues(updated: FleetType) {
this.name = updated.name
this.personIncharge = updated.personIncharge
}
var name: String = ""
var personIncharge: String = ""
}
@Entity
open class Vehicle : BaseTenantModel() {
fun patchValues(updated: Vehicle) {
this.name = updated.name
}
var name: String = ""
}
@Entity
open class Renewal : BaseTenantModel() {
fun patchValues(updated: Renewal) {
this.name = updated.name
}
var name: String = ""
}

View File

@ -0,0 +1,33 @@
-- apply changes
create table fleet (
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,
mileage float not null,
cost float not null,
insurance_renewal_date date,
pollution_renewal_date date,
fitness_renewal_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,
name varchar(255) not null,
type varchar(255) not null,
reg_number varchar(255) not null,
model varchar(255) not null,
make varchar(255) not null,
driver varchar(255) not null,
driver_contact varchar(255) not null,
created_by varchar(255) not null,
modified_by varchar(255) not null,
constraint ck_fleet_approval_status check ( approval_status in ('PENDING','APPROVED','REJECTED')),
constraint pk_fleet primary key (sys_pk)
);

View File

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<migration xmlns="http://ebean-orm.github.io/xml/ns/dbmigration">
<changeSet type="apply">
<createTable name="fleet" pkName="pk_fleet">
<column name="sys_pk" type="bigint" primaryKey="true"/>
<column name="deleted_on" type="localdatetime"/>
<column name="deleted_by" type="varchar"/>
<column name="current_approval_level" type="integer" defaultValue="0" notnull="true"/>
<column name="required_approval_levels" type="integer" defaultValue="0" notnull="true"/>
<column name="approval_status" type="varchar(8)" defaultValue="'APPROVED'" notnull="true" checkConstraint="check ( approval_status in ('PENDING','APPROVED','REJECTED'))" checkConstraintName="ck_fleet_approval_status"/>
<column name="tags" type="varchar[]" defaultValue="'{}'" notnull="true"/>
<column name="comments" type="jsonb" defaultValue="'[]'" notnull="true"/>
<column name="tenant_id" type="varchar" notnull="true"/>
<column name="name" type="varchar" notnull="true"/>
<column name="type" type="varchar" notnull="true"/>
<column name="reg_number" type="varchar" notnull="true"/>
<column name="model" type="varchar" notnull="true"/>
<column name="make" type="varchar" notnull="true"/>
<column name="driver" type="varchar" notnull="true"/>
<column name="driver_contact" type="varchar" notnull="true"/>
<column name="mileage" type="double" notnull="true"/>
<column name="cost" type="double" notnull="true"/>
<column name="insurance_renewal_date" type="date"/>
<column name="pollution_renewal_date" type="date"/>
<column name="fitness_renewal_date" type="date"/>
<column name="deleted" type="boolean" defaultValue="false" notnull="true"/>
<column name="version" type="integer" defaultValue="1" notnull="true"/>
<column name="created_at" type="localdatetime" defaultValue="'now()'" notnull="true"/>
<column name="modified_at" type="localdatetime" defaultValue="'now()'" notnull="true"/>
<column name="created_by" type="varchar" notnull="true"/>
<column name="modified_by" type="varchar" notnull="true"/>
</createTable>
</changeSet>
</migration>