add payment, invoce

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

View File

@@ -904,42 +904,63 @@ object OutgoingInventoryCtrl {
ctx.json(seq).status(HttpStatus.OK)
}
}
object PaymentCtrl {
fun create(ctx :Context){
fun create(ctx: Context) {
val pmt = ctx.bodyAsClass(Payment::class.java)
database.save(pmt)
//update the status of invoices pertaining to payment.vendor
val invcs = searchInvoices(CommonFilters(sortBy = "date", sortAsc = true), InvoiceFilters(status = InvoiceStatus.PAID_NONE))
val tot: Double = pmt.amount
for(inv in invcs){
val deduct = Math.min(pmt.amount, inv.totalAmount)
inv.totalAmount -= deduct
val invoices = searchInvoices(
CommonFilters(sortBy = "date", sortAsc = true),
InvoiceFilters(status = InvoiceStatus.PAID_NONE)
)
println(invoices)
for (invoice in invoices) {
val deduct = Math.min(pmt.amount, invoice.totalAmount)
invoice.totalAmount -= deduct
pmt.amount -= deduct
database.update(inv)
if(pmt.amount <= 0.0) break
}
if (pmt.amount > 0.0){
//balance left for this vendor
val v = pmt.vendor?.sysPk?.let { database.find(Vendor::class.java, it) }
v?.apply {
outstanding = outstanding?.minus(tot)
database.update(v)
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
}
pmt.excessAmount = pmt.amount
database.save(pmt)
ctx.json(pmt).status(HttpStatus.CREATED)
}
fun get(ctx : Context){
val id = ctx.pathParam("id")
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")
?: 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)
}
data class PMTF(val common : CommonFilters, val paymentFilters: PaymentFilters)
fun getAll(ctx : Context){
fun get(ctx: Context) {
val id = ctx.pathParam("id")
val pmt = database.find(Payment::class.java, id)
?: throw NotFoundResponse("No payment found for this id $id")
ctx.json(pmt).status(HttpStatus.OK)
}
data class PMTF(val common: CommonFilters, val paymentFilters: PaymentFilters)
fun getAll(ctx: Context) {
val filters = ctx.bodyAsClass<PMTF>()
val payments = searchPayments(filters.common, filters.paymentFilters)
println(payments)
println(payments)
val excel = ctx.queryParam("excel")
if (excel !== null) {
// exportPayments(payments)
@@ -949,7 +970,8 @@ object PaymentCtrl {
ctx.json(payments).status(HttpStatus.OK)
}
}
fun update(ctx : Context){
fun update(ctx: Context) {
val id = ctx.pathParam("id").toLong()
val pmt =
database.find(Payment::class.java, id) ?: throw NotFoundResponse("payment not found for $id")
@@ -959,20 +981,24 @@ object PaymentCtrl {
ctx.json(pmt).status(HttpStatus.OK)
}
}
object InvoiceCtrl {
fun create(ctx : Context){
val invoice = ctx.bodyAsClass<Invoice>()
database.save(invoice)
ctx.json(invoice).status(HttpStatus.CREATED)
fun create(ctx: Context) {
val invoice = ctx.bodyAsClass<Invoice>()
database.save(invoice)
ctx.json(invoice).status(HttpStatus.CREATED)
}
fun get(ctx : Context){
fun get(ctx: Context) {
val id = ctx.pathParam("id").toLong()
val invoice = database.find(Invoice::class.java, id)
?: throw NotFoundResponse("No invoice found with id $id")
ctx.json(invoice).status(HttpStatus.OK)
}
data class INVF(val common: CommonFilters, val invoiceFilters: InvoiceFilters)
fun getAll(ctx : Context){
fun getAll(ctx: Context) {
val filters = ctx.bodyAsClass<INVF>()
val invoices = searchInvoices(filters.common, filters.invoiceFilters)
val excel = ctx.queryParam("excel")
@@ -985,7 +1011,8 @@ object InvoiceCtrl {
}
}
fun update(ctx : Context){
fun update(ctx: Context) {
val id = ctx.pathParam("id").toLong()
val invoice =
database.find(Invoice::class.java, id) ?: throw NotFoundResponse("invoice not found for $id")
@@ -994,7 +1021,8 @@ object InvoiceCtrl {
invoice.update()
ctx.json(invoice).status(HttpStatus.OK)
}
fun getNextNum(ctx : Context){
fun getNextNum(ctx: Context) {
val prefix = "INV/"
val cnt = database.find(Invoice::class.java)
.findCount()