add payment, invoce

This commit is contained in:
arsalan 2024-03-06 18:31:03 +05:30
parent d9dcda0724
commit be03724217
6 changed files with 98 additions and 49 deletions

View File

@ -187,6 +187,7 @@ fun main(args: Array<String>) {
PaymentCtrl::getAll, PaymentCtrl::getAll,
Roles(Role.Explicit("ROLE_PAYMENT_CREATE", "ROLE_PAYMENT_VIEW")) Roles(Role.Explicit("ROLE_PAYMENT_CREATE", "ROLE_PAYMENT_VIEW"))
) )
delete("/{id}", PaymentCtrl::delete, Roles(Role.Explicit("ROLE_PAYMENT_CREATE")))
} }
path("/po") { path("/po") {
get("/next", PurchaseOrderCtrl::getNextNum, Roles(Role.Explicit("ROLE_PO_CREATE"))) get("/next", PurchaseOrderCtrl::getNextNum, Roles(Role.Explicit("ROLE_PO_CREATE")))

View File

@ -904,38 +904,59 @@ object OutgoingInventoryCtrl {
ctx.json(seq).status(HttpStatus.OK) ctx.json(seq).status(HttpStatus.OK)
} }
} }
object PaymentCtrl { object PaymentCtrl {
fun create(ctx: Context) { fun create(ctx: Context) {
val pmt = ctx.bodyAsClass(Payment::class.java) val pmt = ctx.bodyAsClass(Payment::class.java)
database.save(pmt)
//update the status of invoices pertaining to payment.vendor //update the status of invoices pertaining to payment.vendor
val invcs = searchInvoices(CommonFilters(sortBy = "date", sortAsc = true), InvoiceFilters(status = InvoiceStatus.PAID_NONE)) val invoices = searchInvoices(
val tot: Double = pmt.amount CommonFilters(sortBy = "date", sortAsc = true),
for(inv in invcs){ InvoiceFilters(status = InvoiceStatus.PAID_NONE)
val deduct = Math.min(pmt.amount, inv.totalAmount) )
inv.totalAmount -= deduct println(invoices)
for (invoice in invoices) {
val deduct = Math.min(pmt.amount, invoice.totalAmount)
invoice.totalAmount -= deduct
pmt.amount -= deduct pmt.amount -= deduct
database.update(inv) if (invoice.totalAmount <= 0.0) {
invoice.status = InvoiceStatus.PAID_FULL
} else {
invoice.status = InvoiceStatus.PAID_SOME
}
database.update(invoice)
pmt.invoicesAffected?.toMutableMap()?.put(invoice.sysPk, deduct)
if (pmt.amount <= 0.0) break if (pmt.amount <= 0.0) break
} }
if (pmt.amount > 0.0){ pmt.excessAmount = pmt.amount
//balance left for this vendor database.save(pmt)
val v = pmt.vendor?.sysPk?.let { database.find(Vendor::class.java, it) }
v?.apply {
outstanding = outstanding?.minus(tot)
database.update(v)
}
}
ctx.json(pmt).status(HttpStatus.CREATED) ctx.json(pmt).status(HttpStatus.CREATED)
} }
fun delete(ctx: Context) {
val id = ctx.pathParam("id").toLong()
val pmt = database.find(Payment::class.java, id)
?: throw NotFoundResponse("No payment found for this id $id")
val invoiceDeductMap = pmt.invoicesAffected
for (entry in invoiceDeductMap?.entries!!.iterator()) {
val inv = database.find(Invoice::class.java, entry.key)
?: throw NotFoundResponse("No invoice found for $entry.key")
inv.totalAmount += entry.value
database.update(inv)
}
database.delete(pmt)
ctx.json(pmt).status(HttpStatus.OK)
}
fun get(ctx: Context) { fun get(ctx: Context) {
val id = ctx.pathParam("id") val id = ctx.pathParam("id")
val pmt = database.find(Payment::class.java, id) val pmt = database.find(Payment::class.java, id)
?: throw NotFoundResponse("No payment found for this id") ?: throw NotFoundResponse("No payment found for this id $id")
ctx.json(pmt).status(HttpStatus.OK) ctx.json(pmt).status(HttpStatus.OK)
} }
data class PMTF(val common: CommonFilters, val paymentFilters: PaymentFilters) data class PMTF(val common: CommonFilters, val paymentFilters: PaymentFilters)
fun getAll(ctx: Context) { fun getAll(ctx: Context) {
val filters = ctx.bodyAsClass<PMTF>() val filters = ctx.bodyAsClass<PMTF>()
val payments = searchPayments(filters.common, filters.paymentFilters) val payments = searchPayments(filters.common, filters.paymentFilters)
@ -949,6 +970,7 @@ object PaymentCtrl {
ctx.json(payments).status(HttpStatus.OK) ctx.json(payments).status(HttpStatus.OK)
} }
} }
fun update(ctx: Context) { fun update(ctx: Context) {
val id = ctx.pathParam("id").toLong() val id = ctx.pathParam("id").toLong()
val pmt = val pmt =
@ -959,19 +981,23 @@ object PaymentCtrl {
ctx.json(pmt).status(HttpStatus.OK) ctx.json(pmt).status(HttpStatus.OK)
} }
} }
object InvoiceCtrl { object InvoiceCtrl {
fun create(ctx: Context) { fun create(ctx: Context) {
val invoice = ctx.bodyAsClass<Invoice>() val invoice = ctx.bodyAsClass<Invoice>()
database.save(invoice) database.save(invoice)
ctx.json(invoice).status(HttpStatus.CREATED) ctx.json(invoice).status(HttpStatus.CREATED)
} }
fun get(ctx: Context) { fun get(ctx: Context) {
val id = ctx.pathParam("id").toLong() val id = ctx.pathParam("id").toLong()
val invoice = database.find(Invoice::class.java, id) val invoice = database.find(Invoice::class.java, id)
?: throw NotFoundResponse("No invoice found with id $id") ?: throw NotFoundResponse("No invoice found with id $id")
ctx.json(invoice).status(HttpStatus.OK) ctx.json(invoice).status(HttpStatus.OK)
} }
data class INVF(val common: CommonFilters, val invoiceFilters: InvoiceFilters) data class INVF(val common: CommonFilters, val invoiceFilters: InvoiceFilters)
fun getAll(ctx: Context) { fun getAll(ctx: Context) {
val filters = ctx.bodyAsClass<INVF>() val filters = ctx.bodyAsClass<INVF>()
val invoices = searchInvoices(filters.common, filters.invoiceFilters) val invoices = searchInvoices(filters.common, filters.invoiceFilters)
@ -985,6 +1011,7 @@ object InvoiceCtrl {
} }
} }
fun update(ctx: Context) { fun update(ctx: Context) {
val id = ctx.pathParam("id").toLong() val id = ctx.pathParam("id").toLong()
val invoice = val invoice =
@ -994,6 +1021,7 @@ object InvoiceCtrl {
invoice.update() invoice.update()
ctx.json(invoice).status(HttpStatus.OK) ctx.json(invoice).status(HttpStatus.OK)
} }
fun getNextNum(ctx: Context) { fun getNextNum(ctx: Context) {
val prefix = "INV/" val prefix = "INV/"
val cnt = database.find(Invoice::class.java) val cnt = database.find(Invoice::class.java)

View File

@ -2,7 +2,6 @@ package com.restapi.controllers
import com.restapi.domain.* import com.restapi.domain.*
import com.restapi.domain.Session.database import com.restapi.domain.Session.database
import org.checkerframework.checker.index.qual.LessThan
import java.time.LocalDate import java.time.LocalDate
//constants //constants

View File

@ -467,6 +467,7 @@ open class OutgoingInventory : BaseTenantModel() {
enum class InvoiceStatus { enum class InvoiceStatus {
PAID_FULL, PAID_SOME, PAID_NONE, ALL PAID_FULL, PAID_SOME, PAID_NONE, ALL
} }
@Entity @Entity
open class Invoice : BaseTenantModel() { open class Invoice : BaseTenantModel() {
fun patchValues(updated: Invoice) { fun patchValues(updated: Invoice) {
@ -478,14 +479,18 @@ open class Invoice : BaseTenantModel() {
this.vendor = updated.vendor this.vendor = updated.vendor
this.status = updated.status this.status = updated.status
} }
var number: String = "" var number: String = ""
var date: LocalDate? = null var date: LocalDate? = null
var totalAmount: Double = 0.0 var totalAmount: Double = 0.0
var poNum: String? = null var poNum: String? = null
@DbJsonB @DbJsonB
var products: List<POProducts>? = null var products: List<POProducts>? = null
@ManyToOne @ManyToOne
var vendor: Vendor? = null var vendor: Vendor? = null
@Enumerated(EnumType.STRING) @Enumerated(EnumType.STRING)
var status: InvoiceStatus? = null var status: InvoiceStatus? = null
} }
@ -499,11 +504,15 @@ open class Payment : BaseTenantModel() {
this.remark = updated.remark this.remark = updated.remark
this.vendor = updated.vendor this.vendor = updated.vendor
} }
var refNumber: String = "" var refNumber: String = ""
var amount: Double = 0.0 var amount: Double = 0.0
var date: LocalDate? = null var date: LocalDate? = null
var remark: String? = null var remark: String? = null
var excessAmount : Double ?= null
@DbJsonB
var invoicesAffected: Map<Long, Double>? = null
@ManyToOne @ManyToOne
var vendor: Vendor? = null var vendor: Vendor? = null
} }

View File

@ -0,0 +1,3 @@
-- apply alter tables
alter table payment add column if not exists excess_amount float;
alter table payment add column if not exists invoices_affected jsonb;

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<migration xmlns="http://ebean-orm.github.io/xml/ns/dbmigration">
<changeSet type="apply">
<addColumn tableName="payment">
<column name="excess_amount" type="double"/>
<column name="invoices_affected" type="jsonb"/>
</addColumn>
</changeSet>
</migration>