Advertisement
Guest User

Untitled

a guest
Sep 26th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 9.93 KB | None | 0 0
  1. def departureReportExcel(def params){
  2.         UserLoginData userLoginData = UserLoginData.current
  3.         Long fdPropertyId = UserLoginData.getItem("fdPropertyId")?.toString()?.toLong()
  4.         String startDateFilter = params.startDateFilter
  5.         String endDateFilter = params.endDateFilter
  6.         Date startDate = Date.parse("dd/MM/yyyy", startDateFilter)
  7.         Date endDate = Date.parse("dd/MM/yyyy", endDateFilter)
  8.         List<Reservation> reservationList = Reservation.createCriteria().list{
  9.             fdUnit{
  10.                 fdProperty{
  11.                     eq("id", fdPropertyId)
  12.                 }
  13.             }
  14.             eq("companyId", userLoginData.company_id)
  15.             isNotNull("checkOut")
  16.             gte("checkOut", startDate.clearTime())
  17.             lt("checkOut", (endDate + 1).clearTime())
  18.             order("checkOut", "asc")
  19.             ne("canceled", true)
  20.         }
  21.         FdProperty fdProperty = FdProperty.findById(fdPropertyId)
  22.         List<FdUnit> fdUnitList = FdUnit.findAllByFdPropertyAndIsActive(fdProperty, true)
  23.         def serviceTypeList = OfferedService.serviceTypeList
  24.         serviceTypeList.remove(1)
  25.  
  26.         def reservationGuestList = ReservationGuest.findAllByReservationInList(reservationList)
  27.         def reservationPaymentList = ReservationPayment.findAllByReservationInList(reservationList)
  28.         def reservationPaymentGuestList = ReservationPayment.findAllByReservationGuestInList(reservationGuestList)
  29.         String outputFilePath = "/var/www/front_desk/zipFiles/"
  30.         File outputFileDir = new File(outputFilePath)
  31.         if(!outputFileDir.exists()){
  32.             outputFileDir.mkdirs()
  33.         }
  34.  
  35.         String cmd = "chmod 777 -R " + outputFilePath
  36.         cmd.execute()
  37.         String fileName = "${fdProperty.name}_${startDate?.format("ddMMyyyy") ?: "noStartDate"}_${endDate?.format("ddMMyyyy") ?: "noEndDate"}_departures.xls"
  38.  
  39.         Workbook oldWB = Workbook.getWorkbook(new File("/var/www/front_desk/manas_apartmants_template.xls"))
  40.  
  41.         File excelFile = new File(outputFilePath + fileName)
  42.         WritableWorkbook outputWorkbook = Workbook.createWorkbook(excelFile, oldWB)
  43.         WritableSheet outputSheet = outputWorkbook.getSheet(0)
  44.         outputSheet.setName("Departures")
  45.         outputSheet.getSettings().setVerticalFreeze(1)
  46.         Cell readCell = outputSheet.getCell(0, 0)
  47.         CellFormat nameCellFormat = readCell.getCellFormat()
  48.  
  49.         WritableCellFormat numberFormat = new WritableCellFormat (NumberFormats.FLOAT)
  50.         numberFormat.setVerticalAlignment(jxl.format.VerticalAlignment.getAlignment(1))
  51.         WritableCellFormat modifiedCellFormat = ExcelCell.getWritableCellFormat(nameCellFormat, null, null, 10, false, -1, 1)
  52.         WritableCellFormat headingNumberCellFormat = ExcelCell.getWritableCellFormat(nameCellFormat, null, null, 10, true, -1, 1)
  53.         headingNumberCellFormat.setAlignment(Alignment.RIGHT)
  54.         WritableCellFormat headingCellFormat = ExcelCell.getWritableCellFormat(nameCellFormat, null, null, 10, true, -1, 1)
  55.         WritableCellFormat dayCellFormat = ExcelCell.getWritableCellFormat(nameCellFormat, null, null, 10, true, 2)
  56.         WritableCellFormat resGuestCellFormat = ExcelCell.getWritableCellFormat(nameCellFormat, Colour.VERY_LIGHT_YELLOW, null, 10, false, -1, 1)
  57.         resGuestCellFormat.setBorder(Border.ALL, BorderLineStyle.THIN)
  58.         WritableCellFormat yellowNumberFormat = new WritableCellFormat (NumberFormats.FLOAT)
  59.         yellowNumberFormat.setVerticalAlignment(jxl.format.VerticalAlignment.getAlignment(1))
  60.         yellowNumberFormat.setBackground(Colour.VERY_LIGHT_YELLOW, Pattern.SOLID)
  61.         yellowNumberFormat.setBorder(Border.ALL, BorderLineStyle.THIN)
  62.  
  63.         println("DEPARTURE*****************")
  64.         println(new Date())
  65.  
  66.         outputSheet.setColumnView(0, 10)
  67.         ExcelCell.cellModifications(outputSheet, 0, 0, "Unit", headingCellFormat, "string")
  68.         outputSheet.setColumnView(1, 25)
  69.         ExcelCell.cellModifications(outputSheet, 1, 0, "Full name", headingCellFormat, "string")
  70.         outputSheet.setColumnView(2, 10)
  71.         ExcelCell.cellModifications(outputSheet, 2, 0, "Total", headingNumberCellFormat, "string")
  72.         int i=3
  73.         for(serviceType in serviceTypeList){
  74.             outputSheet.setColumnView(i, 20)
  75.             ExcelCell.cellModifications(outputSheet, i, 0, serviceType.name, headingNumberCellFormat, "string")
  76.             i++
  77.         }
  78.         outputSheet.setColumnView(i, 15)
  79.         ExcelCell.cellModifications(outputSheet, i++, 0, "Arrival", headingCellFormat, "string")
  80.         outputSheet.setColumnView(i, 17)
  81.         ExcelCell.cellModifications(outputSheet, i++, 0, "Reservation number", headingCellFormat, "string")
  82.         outputSheet.setColumnView(i, 15)
  83.         ExcelCell.cellModifications(outputSheet, i++, 0, "Reservation status", headingCellFormat, "string")
  84.         outputSheet.setColumnView(i, 15)
  85.         ExcelCell.cellModifications(outputSheet, i, 0, "Payment status", headingCellFormat, "string")
  86.  
  87.         int currentRow = 1
  88.         for(Date d = startDate; d <= endDate; d++) {
  89.             List<Reservation> resList = reservationList.findAll{it.checkOut.format("dd/MM/yyyy") == d.format("dd/MM/yyyy")}
  90.             ExcelCell.cellModifications(outputSheet, 0, currentRow, "Departure list on ${d}", dayCellFormat, "string")
  91.             outputSheet.mergeCells(0, currentRow, 9, currentRow)
  92.             currentRow++
  93.             for(res in resList) {
  94.                 outputSheet.setRowView(currentRow, 1200)
  95.                 double totalPayment = res?.totalPayment?.round(2) ?: 0.00
  96.                 def currentStatus = res.currentStatus
  97.                 def paymentStatus = res.paymentStatus
  98.                 def rgList = reservationGuestList.findAll{it.reservationId == res.id}
  99.                 List<ReservationPayment> rpList = reservationPaymentList.findAll{it.reservationId == res.id}
  100.                 String unitName = fdUnitList.find({ it -> it.id == res?.fdUnit.id }).name
  101.                 ExcelCell.cellModifications(outputSheet, 0, currentRow, "${unitName}", modifiedCellFormat, "string")
  102.                 ExcelCell.cellModifications(outputSheet, 1, currentRow, "${res.displayTitle ?: res.accountDisplayTitle ?: res.contactDisplayTitle ?: 'No contact'}", modifiedCellFormat, "string")
  103.                 ExcelCell.cellModifications(outputSheet, 2, currentRow, "${totalPayment?:0.00}", numberFormat, "number")
  104.                 int j=3
  105.                 for(serviceType in serviceTypeList){
  106.                     List<ReservationPayment> currentRpList = rpList.findAll{it.offeredService.serviceType == serviceType.id}
  107.                     if(currentRpList) {
  108.                         ExcelCell.cellModifications(outputSheet, j, currentRow, "${currentRpList.sum{it.netPrice.round(2)?: 0.00}}", numberFormat, "number")
  109.                     }
  110.                     else {
  111.                         ExcelCell.cellModifications(outputSheet, j, currentRow, "${0.00}", numberFormat, "number")
  112.                     }
  113.                     j++
  114.                 }
  115.                 ExcelCell.cellModifications(outputSheet, j++, currentRow, "${res.checkIn.format("yyyy-MM-dd")}", modifiedCellFormat, "string")
  116.                 ExcelCell.cellModifications(outputSheet, j++, currentRow, "${res.id}", modifiedCellFormat, "string")
  117.                 ExcelCell.cellModifications(outputSheet, j++, currentRow, "${currentStatus.name}", modifiedCellFormat, "string")
  118.                 ExcelCell.cellModifications(outputSheet, j, currentRow, "${paymentStatus.name}", modifiedCellFormat, "string")
  119.                 currentRow++
  120.                 if(rgList.size() >= 1) {
  121.                     for (resG in rgList) {
  122.                         def currentStatusGuest = resG.currentStatus
  123.                         def paymentStatusGuest = resG.paymentStatus
  124.                         List<ReservationPayment> rpListGuest = reservationPaymentGuestList.findAll{it.reservationGuestId == resG.id}
  125.                         ExcelCell.cellModifications(outputSheet, 0, currentRow, "${unitName} - ${resG.id}", resGuestCellFormat, "string")
  126.                         ExcelCell.cellModifications(outputSheet, 1, currentRow, "${resG.title}", resGuestCellFormat, "string")
  127.                         ExcelCell.cellModifications(outputSheet, 2, currentRow, "${rpListGuest ? rpListGuest.sum{it.netPrice ?: 0.00} : 0.00}", yellowNumberFormat, "number")
  128.                         int k=3
  129.                         for(serviceType in serviceTypeList){
  130.                             List<ReservationPayment> currentRpListGuest = rpListGuest.findAll{it.offeredService.serviceType == serviceType.id}
  131.                             if(currentRpListGuest) {
  132.                                 ExcelCell.cellModifications(outputSheet, k, currentRow, "${currentRpListGuest.sum{it.netPrice.round(2)?: 0.00}}", yellowNumberFormat, "number")
  133.                             }
  134.                             else {
  135.                                 ExcelCell.cellModifications(outputSheet, k, currentRow, "${0.00}", yellowNumberFormat, "number")
  136.                             }
  137.                             k++
  138.                         }
  139.                         ExcelCell.cellModifications(outputSheet, k++, currentRow, "${resG.checkIn.format("yyyy-MM-dd")}", resGuestCellFormat, "string")
  140.                         ExcelCell.cellModifications(outputSheet, k++, currentRow, "${res.id}", resGuestCellFormat, "string")
  141.                         ExcelCell.cellModifications(outputSheet, k++, currentRow, "${currentStatusGuest.name}", resGuestCellFormat, "string")
  142.                         ExcelCell.cellModifications(outputSheet, k, currentRow, "${paymentStatusGuest.name}", resGuestCellFormat, "string")
  143.                         currentRow++
  144.                     }
  145.                 }
  146.             }
  147.         }
  148.  
  149.         outputWorkbook.write()
  150.         outputWorkbook.close()
  151.         println(new Date())
  152.  
  153.         return [status: 0, message: "Exporting successful!", fileName: fileName, outputFilePath: outputFilePath]
  154.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement