diff --git a/excel/Invoices.xls b/excel/Invoices.xls new file mode 100644 index 0000000..7d8d375 Binary files /dev/null and b/excel/Invoices.xls differ diff --git a/excel/Payments.xls b/excel/Payments.xls new file mode 100644 index 0000000..d1f5292 Binary files /dev/null and b/excel/Payments.xls differ diff --git a/excel/Products.xls b/excel/Products.xls index dd86121..e32eb73 100644 Binary files a/excel/Products.xls and b/excel/Products.xls differ diff --git a/excel/Quotes.xls b/excel/Quotes.xls index 54a01e6..07f1789 100644 Binary files a/excel/Quotes.xls and b/excel/Quotes.xls differ diff --git a/src/main/kotlin/com/restapi/controllers/Entities.kt b/src/main/kotlin/com/restapi/controllers/Entities.kt index eb497ae..5e28adf 100644 --- a/src/main/kotlin/com/restapi/controllers/Entities.kt +++ b/src/main/kotlin/com/restapi/controllers/Entities.kt @@ -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? = 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() 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 { diff --git a/src/main/kotlin/com/restapi/controllers/Excel.kt b/src/main/kotlin/com/restapi/controllers/Excel.kt index b518e40..a33dfc6 100644 --- a/src/main/kotlin/com/restapi/controllers/Excel.kt +++ b/src/main/kotlin/com/restapi/controllers/Excel.kt @@ -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) { val sh = wb.createSheet() val headers: List = - 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) { saveExcelFileLocally("Pos.xls", wb) } -fun exportIncomingInventory(tickets : List) { +fun exportIncomingInventory(tickets: List) { val wb = HSSFWorkbook() val sh = wb.createSheet() @@ -379,7 +390,7 @@ fun exportIncomingInventory(tickets : List) { saveExcelFileLocally("IncomingInventory.xls", wb) } -fun exportOutgoingInventory(tickets : List) { +fun exportOutgoingInventory(tickets: List) { val wb = HSSFWorkbook() val sh = wb.createSheet() @@ -417,6 +428,73 @@ fun exportOutgoingInventory(tickets : List) { } saveExcelFileLocally("OutgoingInventory.xls", wb) } + +fun exportPayments(payments: List) { + val wb = HSSFWorkbook() + val sh = wb.createSheet() + + val headers: List = 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) { + val wb = HSSFWorkbook() + val sh = wb.createSheet() + + val headers: List = 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..? = null + var invoicesAffected: MutableMap? = mutableMapOf() + @ManyToOne var vendor: Vendor? = null } \ No newline at end of file diff --git a/src/main/resources/dbmigration/1.14.sql b/src/main/resources/dbmigration/1.14.sql new file mode 100644 index 0000000..ee8d733 --- /dev/null +++ b/src/main/resources/dbmigration/1.14.sql @@ -0,0 +1,2 @@ +-- apply alter tables +alter table payment add column if not exists amount_deducted float; diff --git a/src/main/resources/dbmigration/model/1.14.model.xml b/src/main/resources/dbmigration/model/1.14.model.xml new file mode 100644 index 0000000..910acb6 --- /dev/null +++ b/src/main/resources/dbmigration/model/1.14.model.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file