add payment, invoice
This commit is contained in:
@@ -909,26 +909,33 @@ object PaymentCtrl {
|
||||
fun create(ctx: Context) {
|
||||
val pmt = ctx.bodyAsClass(Payment::class.java)
|
||||
//update the status of invoices pertaining to payment.vendor
|
||||
|
||||
val vendors: List<Long>? = pmt.vendor?.let { listOf(it.sysPk) }
|
||||
val invoices = searchInvoices(
|
||||
CommonFilters(sortBy = "date", sortAsc = true),
|
||||
InvoiceFilters(status = InvoiceStatus.PAID_NONE)
|
||||
CommonFilters(sortBy = "date", sortAsc = true, vendor = vendors),
|
||||
InvoiceFilters(status = InvoiceStatus.PAID_FULL)
|
||||
)
|
||||
println(invoices)
|
||||
// println(invoices)
|
||||
//pmt.invoicesAffected = mutableMapOf()
|
||||
var totalAmount = pmt.amount
|
||||
var totalDeduct = 0.0
|
||||
for (invoice in invoices) {
|
||||
val deduct = Math.min(pmt.amount, invoice.totalAmount)
|
||||
val deduct = Math.min(totalAmount, invoice.totalAmount)
|
||||
invoice.totalAmount -= deduct
|
||||
pmt.amount -= deduct
|
||||
totalDeduct += deduct
|
||||
totalAmount -= deduct
|
||||
if (invoice.totalAmount <= 0.0) {
|
||||
invoice.status = InvoiceStatus.PAID_FULL
|
||||
} else {
|
||||
invoice.status = InvoiceStatus.PAID_SOME
|
||||
}
|
||||
//println(invoice)
|
||||
database.update(invoice)
|
||||
pmt.invoicesAffected?.toMutableMap()?.put(invoice.sysPk, deduct)
|
||||
if (pmt.amount <= 0.0) break
|
||||
pmt.invoicesAffected?.put(invoice.sysPk, deduct)
|
||||
println(pmt.invoicesAffected)
|
||||
if (totalAmount <= 0.0) break
|
||||
}
|
||||
pmt.excessAmount = pmt.amount
|
||||
pmt.amountDeducted = totalDeduct
|
||||
pmt.excessAmount = pmt.amount - pmt.amountDeducted!!
|
||||
database.save(pmt)
|
||||
ctx.json(pmt).status(HttpStatus.CREATED)
|
||||
}
|
||||
@@ -942,6 +949,7 @@ object PaymentCtrl {
|
||||
val inv = database.find(Invoice::class.java, entry.key)
|
||||
?: throw NotFoundResponse("No invoice found for $entry.key")
|
||||
inv.totalAmount += entry.value
|
||||
inv.status = InvoiceStatus.PAID_SOME
|
||||
database.update(inv)
|
||||
}
|
||||
database.delete(pmt)
|
||||
@@ -960,10 +968,10 @@ object PaymentCtrl {
|
||||
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)
|
||||
exportPayments(payments)
|
||||
val inputStream = FileInputStream("./excel/Payments.xls")
|
||||
ctx.result(inputStream).status(HttpStatus.OK)
|
||||
} else {
|
||||
@@ -1003,7 +1011,7 @@ object InvoiceCtrl {
|
||||
val invoices = searchInvoices(filters.common, filters.invoiceFilters)
|
||||
val excel = ctx.queryParam("excel")
|
||||
if (excel !== null) {
|
||||
// exportPayments(payments)
|
||||
exportInvoices(invoices)
|
||||
val inputStream = FileInputStream("./excel/Invoices.xls")
|
||||
ctx.result(inputStream).status(HttpStatus.OK)
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user