Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invoice pdf Generator #27

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 61 additions & 18 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>me.anant</groupId>
<artifactId>PMS</artifactId>
Expand Down Expand Up @@ -44,7 +45,7 @@
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand All @@ -56,35 +57,77 @@
</exclusion>
</exclusions>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>9.0.33</version>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>9.0.33</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>

<!-- Spring Context Support -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>

<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.0.Final</version>
</dependency>

<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>

<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>

<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf</artifactId>
<version>9.1.20</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
Expand Down
48 changes: 22 additions & 26 deletions src/main/java/me/anant/PMS/controller/OrderController.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 :
Expand All @@ -43,13 +43,13 @@
public class OrderController {
@Autowired
ProductService productService;

@Autowired
UserService userService;

@Autowired
OrderService orderService;

@Autowired
EmailService emailService;

Expand All @@ -59,7 +59,7 @@ public class OrderController {
*/
@GetMapping("/customer/order_place")
public ModelAndView customerHome() {
List<Product> pList = productService.get();
List<Product> pList = productService.get();
ModelAndView modelAndView = new ModelAndView("customer/home");
modelAndView.addObject("pList", pList);
return modelAndView;
Expand All @@ -75,7 +75,7 @@ public ModelAndView customerHome() {
public ModelAndView orderPlace(HttpServletRequest request, Principal principal) throws ProductNotFoundException{
String[] pIds = request.getParameterValues("productId");
Set<OrderProduct> 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));
Expand All @@ -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,<br><br>Your order has been placed successfuly. Following is the detail of your order.<br><br>"
+ "<table>" +
"<tr>" +
"<th>Name</th>" +
"<th>Price</th>" +
"<th>Qty</th>" +
"<th>Amount</th>" +
"</tr>";
+ "<table>" + "<tr>" + "<th>Name</th>" + "<th>Price</th>" + "<th>Qty</th>" + "<th>Amount</th>"
+ "</tr>";
float sum = 0;
for (OrderProduct op : opList)
{
for (OrderProduct op : opList) {
sum = sum + op.getProduct().getProductPrice() * op.getBuyqty();
message = message + "<tr>" +
"<td>"+op.getProduct().getProductName()+"</td>" +
"<td>Rs. "+op.getProduct().getProductPrice()+"</td>" +
"<td>"+op.getBuyqty()+"</td>" +
"<td>Rs. "+op.getProduct().getProductPrice() * op.getBuyqty()+"</td>" +
"</tr>";
message = message + "<tr>" + "<td>" + op.getProduct().getProductName() + "</td>" + "<td>Rs. "
+ op.getProduct().getProductPrice() + "</td>" + "<td>" + op.getBuyqty() + "</td>" + "<td>Rs. "
+ op.getProduct().getProductPrice() * op.getBuyqty() + "</td>" + "</tr>";
}
message = message + "<tr><td colspan=\"3\"><center><b>Total Amount</b></center></td><td>Rs. "+sum+"</td></tr>" +
"</table>";
emailService.send(principal.getName(), "Order Placed successfully", message);
message = message + "<tr><td colspan=\"3\"><center><b>Total Amount</b></center></td><td>Rs. " + sum
+ "</td></tr>" + "</table>";

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;
Expand Down Expand Up @@ -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
Expand All @@ -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";
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/me/anant/PMS/service/EmailService.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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"));
}
};

Expand Down
49 changes: 49 additions & 0 deletions src/main/java/me/anant/PMS/util/GenerateInvoice.java
Original file line number Diff line number Diff line change
@@ -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<OrderProduct> 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);
}
}
}
Loading