add payment, invoice

This commit is contained in:
arsalan 2024-03-07 18:01:23 +05:30
parent be03724217
commit 67b7c5aa9a
10 changed files with 118 additions and 20 deletions

BIN
excel/Invoices.xls Normal file

Binary file not shown.

BIN
excel/Payments.xls Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -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 {

View File

@ -101,6 +101,7 @@ enum class FileType {
enum class EnumFor {
UOM, DocType
}
fun saveExcelFileLocally(fileName: String, wb: Workbook) {
val path = "./excel/"
val out = FileOutputStream(path + fileName)
@ -245,7 +246,17 @@ fun exportVendors(vendors: List<Vendor>) {
val sh = wb.createSheet()
val headers: List<String> =
listOf("No.", "Name", "MSME", "GST Number", "Address", "Rating", "Contact Name", "Contact Email", "Contact Mobile")
listOf(
"No.",
"Name",
"MSME",
"GST Number",
"Address",
"Rating",
"Contact Name",
"Contact Email",
"Contact Mobile"
)
createHeaderRow(headers, sh, wb)
val totalCols = headers.size
@ -338,7 +349,7 @@ fun exportPos(pos: List<PurchaseOrder>) {
saveExcelFileLocally("Pos.xls", wb)
}
fun exportIncomingInventory(tickets : List<IncomingInventory>) {
fun exportIncomingInventory(tickets: List<IncomingInventory>) {
val wb = HSSFWorkbook()
val sh = wb.createSheet()
@ -379,7 +390,7 @@ fun exportIncomingInventory(tickets : List<IncomingInventory>) {
saveExcelFileLocally("IncomingInventory.xls", wb)
}
fun exportOutgoingInventory(tickets : List<OutgoingInventory>) {
fun exportOutgoingInventory(tickets: List<OutgoingInventory>) {
val wb = HSSFWorkbook()
val sh = wb.createSheet()
@ -417,6 +428,73 @@ fun exportOutgoingInventory(tickets : List<OutgoingInventory>) {
}
saveExcelFileLocally("OutgoingInventory.xls", wb)
}
fun exportPayments(payments: List<Payment>) {
val wb = HSSFWorkbook()
val sh = wb.createSheet()
val headers: List<String> = listOf("Reference Number", "Vendor", "Amount Paid", "Amount Deducted", "Excess Amount")
createHeaderRow(headers, sh, wb)
var rowCnt = 1
for (pmt in payments) {
val row = sh.createRow(rowCnt++)
var i = 0
row.createCell(i++).setCellValue(pmt.refNumber)
row.createCell(i++).setCellValue(pmt.vendor?.name)
row.createCell(i++).setCellValue(pmt.amount)
pmt.amountDeducted?.let { row.createCell(i++).setCellValue(it) }
pmt.excessAmount?.let { row.createCell(i++).setCellValue(it) }
}
saveExcelFileLocally("Payments.xls", wb)
}
fun exportInvoices(invoices: List<Invoice>) {
val wb = HSSFWorkbook()
val sh = wb.createSheet()
val headers: List<String> = listOf(
"Number",
"Date",
"Reference PO",
"Status",
"Vendor Name",
"Vendor Address",
"Product Id",
"Product Name",
"Unit Price",
"Quantity",
"Total Amount"
)
createHeaderRow(headers, sh, wb)
var rowCnt = 1
for (invoice in invoices) {
val prodCnt = invoice.products?.size
for (j in 0..<prodCnt!!) {
val row = sh.createRow(rowCnt++)
var i = 0
row.createCell(i++).setCellValue(invoice.number)
row.createCell(i++).setCellValue(invoice.date)
row.createCell(i++).setCellValue(invoice.poNum)
row.createCell(i++).setCellValue(invoice.status.toString())
row.createCell(i++).setCellValue(invoice.vendor?.name)
row.createCell(i++).setCellValue(invoice.vendor?.address)
//6 would be repeated
row.createCell(i++).setCellValue(invoice.products?.get(j)?.productId ?: "NA")
row.createCell(i++).setCellValue(invoice.products?.get(j)?.productName ?: "NA")
invoice.products?.get(j)?.let { row.createCell(i++).setCellValue(it.unitPrice) }
invoice.products?.get(j)?.let { row.createCell(i++).setCellValue(it.quantity) }
row.createCell(i++).setCellValue(invoice.totalAmount)
}
}
saveExcelFileLocally("Invoices.xls", wb)
}
fun main() {
//ImportFromExcel(FileType.QUOTES, "C:\\Users\\arsalan\\Downloads\\Book.xlsx")
TemplateExcelFile(FileType.PRODS)

View File

@ -246,7 +246,7 @@ fun searchInvoices(commonFilters: CommonFilters, invoiceFilters: InvoiceFilters)
.where()
.ilike("number", "%" + invoiceFilters.numLike + "%")
if (invoiceFilters.status != InvoiceStatus.ALL) {
q.eq("status", invoiceFilters.status)
q.ne("status", invoiceFilters.status)
}
applyFromToHelper(q, commonFilters.from, commonFilters.to, "date")
applyVendorHelper(q, commonFilters.vendor)
@ -258,8 +258,8 @@ fun searchPayments(commonFilters: CommonFilters, paymentFilters: PaymentFilters)
val q = database.find(Payment::class.java)
.where()
.ilike("refNumber", "%" + paymentFilters.refNumberLike + "%")
.ge("amount", paymentFilters.amountExceeds)
.le("amount", paymentFilters.amountLessThan)
// .ge("amount", paymentFilters.amountExceeds)
//.le("amount", paymentFilters.amountLessThan)
applyFromToHelper(q, commonFilters.from, commonFilters.to, "date")
applyVendorHelper(q, commonFilters.vendor)
applySortHelper(q, commonFilters.sortBy, commonFilters.sortAsc)

View File

@ -509,10 +509,12 @@ open class Payment : BaseTenantModel() {
var amount: Double = 0.0
var date: LocalDate? = null
var remark: String? = null
var excessAmount : Double ?= null
var amountDeducted: Double? = null
var excessAmount: Double? = null
@DbJsonB
var invoicesAffected: Map<Long, Double>? = null
var invoicesAffected: MutableMap<Long, Double>? = mutableMapOf()
@ManyToOne
var vendor: Vendor? = null
}

View File

@ -0,0 +1,2 @@
-- apply alter tables
alter table payment add column if not exists amount_deducted float;

View File

@ -0,0 +1,8 @@
<?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="amount_deducted" type="double"/>
</addColumn>
</changeSet>
</migration>