diff --git a/src/main/kotlin/com/restapi/domain/models.kt b/src/main/kotlin/com/restapi/domain/models.kt index 7df1dc5..8c60201 100644 --- a/src/main/kotlin/com/restapi/domain/models.kt +++ b/src/main/kotlin/com/restapi/domain/models.kt @@ -289,6 +289,10 @@ open class Plant : BaseModel() { this.clear() this.putAll(p.print ?: emptyMap()) } + this.limits?.apply { + this.clear() + this.putAll(p.limits ?: emptyMap()) + } this.save() } diff --git a/src/main/resources/scripts/expense.kts b/src/main/resources/scripts/expense.kts new file mode 100644 index 0000000..c37b836 --- /dev/null +++ b/src/main/resources/scripts/expense.kts @@ -0,0 +1,35 @@ +import com.restapi.domain.DataModel +import com.restapi.domain.EntityModel +import io.ebean.Database +import org.slf4j.Logger +import java.time.LocalDate + +fun execute(d: Map, entityModel: EntityModel?, db: Database, logger: Logger, user: String, tenant: String): Map { + println("execute on $d") + //check if the limits have exceeded in the given month + + //language=PostgreSQL + val q = db.sqlQuery( + """ + select sum(cast(data->>'amount' as float)) as amount from data_model + where entity_name = 'expense' + and cast(data->>'date' as timestamp) between :f and :t + and data->>'plantId' = :p + """.trimIndent() + ) + q.setParameter("f", LocalDate.parse(d["fromDate"] as String)) + q.setParameter("t", LocalDate.parse(d["toDate"] as String)) + q.setParameter("p", d["plantId"] as String) + val sum = q.findOne()?.getLong("amount") ?: 0 + + return mapOf("sum" to sum) +} + +fun preSave(d: DataModel, entityModel: EntityModel?, db: Database, logger: Logger, user: String, tenant: String): Boolean { + logger.warn("PreSave $d") + return true +} + +fun postSave(d: DataModel, entityModel: EntityModel?, db: Database, logger: Logger, user: String, tenant: String) { + println("PostSave $d") +} \ No newline at end of file