diff --git a/pom.xml b/pom.xml index 0784109..c71e6d7 100644 --- a/pom.xml +++ b/pom.xml @@ -1,12 +1,13 @@ - 4.0.0 org.springframework.boot spring-boot-starter-parent 2.2.6.RELEASE - + me.anant PMS @@ -44,7 +45,7 @@ h2 runtime - + org.springframework.boot spring-boot-starter-test @@ -56,35 +57,77 @@ - + - org.apache.tomcat - tomcat-jasper - 9.0.33 + org.apache.tomcat + tomcat-jasper + 9.0.33 - + - org.springframework.boot - spring-boot-starter-security + org.springframework.boot + spring-boot-starter-security - + - org.springframework.security - spring-security-taglibs + org.springframework.security + spring-security-taglibs - + org.springframework.boot spring-boot-devtools - + - org.springframework - spring-context-support + org.springframework + spring-context-support + + + + javax.mail + mail + 1.4.7 + + + + javax.servlet + jstl + 1.2 + + + + javax.validation + validation-api + 1.1.0.Final + + + + org.hibernate + hibernate-validator + 5.1.0.Final + + + + org.thymeleaf + thymeleaf + 3.0.11.RELEASE + + + + org.thymeleaf + thymeleaf-spring5 + 3.0.11.RELEASE + + + + org.xhtmlrenderer + flying-saucer-pdf + 9.1.20 - + javax.mail mail diff --git a/src/main/java/me/anant/PMS/controller/OrderController.java b/src/main/java/me/anant/PMS/controller/OrderController.java index b82de65..1fe7e13 100644 --- a/src/main/java/me/anant/PMS/controller/OrderController.java +++ b/src/main/java/me/anant/PMS/controller/OrderController.java @@ -1,6 +1,5 @@ package me.anant.PMS.controller; -import java.awt.Dialog.ModalExclusionType; import java.security.Principal; import java.util.HashSet; import java.util.List; @@ -26,6 +25,7 @@ import me.anant.PMS.service.OrderService; import me.anant.PMS.service.ProductService; import me.anant.PMS.service.UserService; +import me.anant.PMS.util.GenerateInvoice; /** * This Order Controller has rest endpoints which are responsible to perform following functionalities : @@ -43,13 +43,13 @@ public class OrderController { @Autowired ProductService productService; - + @Autowired UserService userService; - + @Autowired OrderService orderService; - + @Autowired EmailService emailService; @@ -59,7 +59,7 @@ public class OrderController { */ @GetMapping("/customer/order_place") public ModelAndView customerHome() { - List pList = productService.get(); + List pList = productService.get(); ModelAndView modelAndView = new ModelAndView("customer/home"); modelAndView.addObject("pList", pList); return modelAndView; @@ -75,7 +75,7 @@ public ModelAndView customerHome() { public ModelAndView orderPlace(HttpServletRequest request, Principal principal) throws ProductNotFoundException{ String[] pIds = request.getParameterValues("productId"); Set opList = new HashSet<>(); - for(String pId: pIds) { + for (String pId : pIds) { long pid = Long.parseLong(pId); Product product = productService.findById(pid).get(); int buyqty = Integer.parseInt(request.getParameter(pId)); @@ -84,30 +84,24 @@ public ModelAndView orderPlace(HttpServletRequest request, Principal principal) } User user = userService.findByEmail(principal.getName()); orderService.save(new Order(user, "PROCESSING", opList)); - + String message = "Hello,

Your order has been placed successfuly. Following is the detail of your order.

" - + "" + - "" + - "" + - "" + - "" + - "" + - ""; + + "
NamePriceQtyAmount
" + "" + "" + "" + "" + "" + + ""; float sum = 0; - for (OrderProduct op : opList) - { + for (OrderProduct op : opList) { sum = sum + op.getProduct().getProductPrice() * op.getBuyqty(); - message = message + "" + - "" + - "" + - "" + - "" + - ""; + message = message + "" + "" + "" + "" + "" + ""; } - message = message + "" + - "
NamePriceQtyAmount
"+op.getProduct().getProductName()+"Rs. "+op.getProduct().getProductPrice()+""+op.getBuyqty()+"Rs. "+op.getProduct().getProductPrice() * op.getBuyqty()+"
" + op.getProduct().getProductName() + "Rs. " + + op.getProduct().getProductPrice() + "" + op.getBuyqty() + "Rs. " + + op.getProduct().getProductPrice() * op.getBuyqty() + "
Total Amount
Rs. "+sum+"
"; - emailService.send(principal.getName(), "Order Placed successfully", message); + message = message + "
Total Amount
Rs. " + sum + + "" + ""; + + GenerateInvoice.exportToPdfBox(opList, sum); + emailService.send(principal.getName(), "Order Placed successfully", message); + ModelAndView modelAndView = new ModelAndView("customer/order_place"); modelAndView.addObject("opList", opList); return modelAndView; @@ -165,6 +159,7 @@ public ModelAndView orderDetailCustomer(@RequestParam("id") long id) throws Prod return modelAndView; } + /** * This GET api is responsible to cancel an Order. * @param id @@ -177,7 +172,8 @@ public String orderCancel(@RequestParam("id") long id, final RedirectAttributes redirectAttributes.addFlashAttribute("msg", "Order cancelled successfully"); redirectAttributes.addFlashAttribute("class", "alert-success"); } else { - redirectAttributes.addFlashAttribute("msg", "Order not cancelled, since you can cencel PROCESSING or CONFIRMED order with 24 hours only."); + redirectAttributes.addFlashAttribute("msg", + "Order not cancelled, since you can cencel PROCESSING or CONFIRMED order with 24 hours only."); redirectAttributes.addFlashAttribute("class", "alert-danger"); } return "redirect:/customer/order/list"; diff --git a/src/main/java/me/anant/PMS/service/EmailService.java b/src/main/java/me/anant/PMS/service/EmailService.java index 334c216..ccd5105 100644 --- a/src/main/java/me/anant/PMS/service/EmailService.java +++ b/src/main/java/me/anant/PMS/service/EmailService.java @@ -1,5 +1,7 @@ package me.anant.PMS.service; +import java.io.File; + import javax.mail.internet.MimeMessage; import org.springframework.beans.factory.annotation.Autowired; @@ -31,6 +33,7 @@ public void prepare(MimeMessage mimeMessage) throws Exception { message.setSubject(subject); message.setText(template, true); + message.addAttachment("invoice.pdf", new File("invoice.pdf")); } }; diff --git a/src/main/java/me/anant/PMS/util/GenerateInvoice.java b/src/main/java/me/anant/PMS/util/GenerateInvoice.java new file mode 100644 index 0000000..cf6fade --- /dev/null +++ b/src/main/java/me/anant/PMS/util/GenerateInvoice.java @@ -0,0 +1,49 @@ +package me.anant.PMS.util; + +import java.io.FileOutputStream; +import java.io.OutputStream; +import java.util.Date; +import java.util.Set; + +import org.thymeleaf.TemplateEngine; +import org.thymeleaf.context.Context; +import org.thymeleaf.templatemode.TemplateMode; +import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver; +import org.xhtmlrenderer.pdf.ITextRenderer; + +import me.anant.PMS.model.OrderProduct; + +public class GenerateInvoice { + + public static void exportToPdfBox(Set opList,float sum) { + + try { + ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver(); + templateResolver.setSuffix(".html"); + templateResolver.setTemplateMode(TemplateMode.HTML); + + TemplateEngine templateEngine = new TemplateEngine(); + templateEngine.setTemplateResolver(templateResolver); + + Context context = new Context(); + context.setVariable("invoiceDate", new Date()); + context.setVariable("totalAmount", sum); + context.setVariable("orderedProductList", opList); + + String html = templateEngine.process("InvoiceTemplate", context); + + String outputFolder = "invoice.pdf"; + System.out.println(outputFolder); + OutputStream outputStream = new FileOutputStream(outputFolder); + + ITextRenderer renderer = new ITextRenderer(); + renderer.setDocumentFromString(html); + renderer.layout(); + renderer.createPDF(outputStream); + + outputStream.close(); + } catch (Exception e) { + System.out.println(e); + } + } +} \ No newline at end of file diff --git a/src/main/resources/InvoiceTemplate.html b/src/main/resources/InvoiceTemplate.html new file mode 100644 index 0000000..a5058ad --- /dev/null +++ b/src/main/resources/InvoiceTemplate.html @@ -0,0 +1,335 @@ + + + + + Invoice + + + + +
+

Invoice

+
+

Jonathan Neal

+

101 E. Chapman Ave

Orange, CA 92866

+

(800) 555-1234

+
+ +
+
+
+

Some Company

c/o Some Guy

+
+ + + + + + + + + + + + + +
Invoice #101138
Date
Amount Due$
+ + + + + + + + + + + + + + + + + +
NamePriceQuantityAmount
+ + + + + +
Total$
+
+ + + + \ No newline at end of file