| 490 |
rajveer |
1 |
package in.shop2020.support.services;
|
|
|
2 |
|
| 1517 |
ankur.sing |
3 |
import in.shop2020.config.ConfigException;
|
| 3044 |
chandransh |
4 |
import in.shop2020.logistics.DeliveryType;
|
| 670 |
chandransh |
5 |
import in.shop2020.logistics.LogisticsServiceException;
|
|
|
6 |
import in.shop2020.logistics.Provider;
|
| 490 |
rajveer |
7 |
import in.shop2020.model.v1.catalog.InventoryService.Client;
|
|
|
8 |
import in.shop2020.model.v1.catalog.InventoryServiceException;
|
|
|
9 |
import in.shop2020.model.v1.catalog.Warehouse;
|
| 505 |
rajveer |
10 |
import in.shop2020.model.v1.order.LineItem;
|
| 490 |
rajveer |
11 |
import in.shop2020.model.v1.order.Order;
|
|
|
12 |
import in.shop2020.model.v1.order.TransactionServiceException;
|
|
|
13 |
import in.shop2020.thrift.clients.CatalogServiceClient;
|
| 670 |
chandransh |
14 |
import in.shop2020.thrift.clients.LogisticsServiceClient;
|
| 490 |
rajveer |
15 |
import in.shop2020.thrift.clients.TransactionServiceClient;
|
| 1517 |
ankur.sing |
16 |
import in.shop2020.thrift.clients.config.ConfigClient;
|
| 490 |
rajveer |
17 |
|
|
|
18 |
import java.io.ByteArrayOutputStream;
|
|
|
19 |
import java.io.File;
|
|
|
20 |
import java.io.FileOutputStream;
|
| 505 |
rajveer |
21 |
import java.io.IOException;
|
| 2784 |
chandransh |
22 |
import java.io.InputStream;
|
| 670 |
chandransh |
23 |
import java.text.DateFormat;
|
| 734 |
chandransh |
24 |
import java.text.DecimalFormat;
|
| 490 |
rajveer |
25 |
import java.util.Date;
|
| 505 |
rajveer |
26 |
import java.util.List;
|
| 2784 |
chandransh |
27 |
import java.util.Locale;
|
|
|
28 |
import java.util.Properties;
|
| 490 |
rajveer |
29 |
|
| 2784 |
chandransh |
30 |
import org.apache.commons.lang.WordUtils;
|
| 490 |
rajveer |
31 |
import org.apache.thrift.TException;
|
| 2784 |
chandransh |
32 |
import org.slf4j.Logger;
|
|
|
33 |
import org.slf4j.LoggerFactory;
|
| 490 |
rajveer |
34 |
|
| 2784 |
chandransh |
35 |
import com.ibm.icu.text.RuleBasedNumberFormat;
|
|
|
36 |
|
| 490 |
rajveer |
37 |
import com.itextpdf.text.Document;
|
|
|
38 |
import com.itextpdf.text.Element;
|
|
|
39 |
import com.itextpdf.text.Font;
|
|
|
40 |
import com.itextpdf.text.Font.FontFamily;
|
|
|
41 |
import com.itextpdf.text.FontFactory;
|
|
|
42 |
import com.itextpdf.text.FontFactoryImp;
|
| 670 |
chandransh |
43 |
import com.itextpdf.text.Image;
|
| 490 |
rajveer |
44 |
import com.itextpdf.text.Paragraph;
|
| 505 |
rajveer |
45 |
import com.itextpdf.text.Phrase;
|
| 670 |
chandransh |
46 |
import com.itextpdf.text.Rectangle;
|
| 490 |
rajveer |
47 |
import com.itextpdf.text.pdf.BaseFont;
|
|
|
48 |
import com.itextpdf.text.pdf.PdfPCell;
|
|
|
49 |
import com.itextpdf.text.pdf.PdfPTable;
|
|
|
50 |
import com.itextpdf.text.pdf.PdfWriter;
|
| 2784 |
chandransh |
51 |
import com.itextpdf.text.pdf.draw.DottedLineSeparator;
|
| 490 |
rajveer |
52 |
|
|
|
53 |
public class InvoiceGenerationService {
|
| 2784 |
chandransh |
54 |
|
|
|
55 |
private static Logger logger = LoggerFactory.getLogger(InvoiceGenerationService.class);
|
|
|
56 |
|
| 734 |
chandransh |
57 |
private TransactionServiceClient tsc = null;
|
|
|
58 |
private CatalogServiceClient csc = null;
|
|
|
59 |
private LogisticsServiceClient lsc = null;
|
| 2784 |
chandransh |
60 |
|
|
|
61 |
private static Locale indianLocale = new Locale("en", "IN");
|
| 734 |
chandransh |
62 |
private DecimalFormat amountFormat = new DecimalFormat("#,##0.00");
|
| 2784 |
chandransh |
63 |
|
|
|
64 |
private static final Font helvetica8 = FontFactory.getFont(FontFactory.HELVETICA, 8);
|
|
|
65 |
private static final Font helvetica10 = FontFactory.getFont(FontFactory.HELVETICA, 10);
|
|
|
66 |
private static final Font helvetica12 = FontFactory.getFont(FontFactory.HELVETICA, 12);
|
|
|
67 |
private static final Font helvetica16 = FontFactory.getFont(FontFactory.HELVETICA, 16);
|
| 490 |
rajveer |
68 |
|
| 2784 |
chandransh |
69 |
private static final Font helveticaBold8 = FontFactory.getFont(FontFactory.HELVETICA_BOLD, 8);
|
|
|
70 |
private static final Font helveticaBold12 = FontFactory.getFont(FontFactory.HELVETICA_BOLD, 12);
|
|
|
71 |
|
|
|
72 |
private static final Properties properties = readProperties();
|
|
|
73 |
private static final String ourAddress = properties.getProperty("sales_tax_address",
|
|
|
74 |
"Spice Online Retail Pvt. Ltd.\nKhasra No. 819, Block-K\nMahipalpur, New Delhi-110037\n");
|
|
|
75 |
private static final String tinNo = properties.getProperty("sales_tax_tin", "07250399732");
|
|
|
76 |
|
|
|
77 |
private static final String delhiPincodePrefix = "11";
|
|
|
78 |
|
|
|
79 |
private static final double salesTaxLowRate = Double.parseDouble(properties.getProperty("sales_tax_low_rate", "5.0"));
|
|
|
80 |
private static final double salesTaxHighRate = Double.parseDouble(properties.getProperty("sales_tax_high_rate", "12.5"));
|
|
|
81 |
private static final double salesTaxCutOff = (Double.parseDouble(properties.getProperty("sales_tax_cutoff", "10000")) * (100 + salesTaxLowRate))/100;
|
|
|
82 |
|
|
|
83 |
private static Properties readProperties(){
|
|
|
84 |
Properties props = new Properties();
|
|
|
85 |
try {
|
|
|
86 |
InputStream is = InvoiceGenerationService.class.getResourceAsStream("/in/shop2020/support/services/InvoiceGenerationService.properties");
|
|
|
87 |
props.load(is);
|
|
|
88 |
} catch (IOException e) {
|
|
|
89 |
logger.error("Error while reading properties file.", e);
|
|
|
90 |
}
|
|
|
91 |
return props;
|
|
|
92 |
}
|
|
|
93 |
|
| 734 |
chandransh |
94 |
public InvoiceGenerationService() {
|
|
|
95 |
try {
|
| 490 |
rajveer |
96 |
tsc = new TransactionServiceClient();
|
|
|
97 |
csc = new CatalogServiceClient();
|
| 670 |
chandransh |
98 |
lsc = new LogisticsServiceClient();
|
| 490 |
rajveer |
99 |
} catch (Exception e) {
|
| 2784 |
chandransh |
100 |
logger.error("Error while instantiating thrift clients.", e);
|
| 490 |
rajveer |
101 |
}
|
|
|
102 |
}
|
| 734 |
chandransh |
103 |
|
|
|
104 |
public ByteArrayOutputStream generateInvoice(long orderId) {
|
| 670 |
chandransh |
105 |
ByteArrayOutputStream baosPDF = null;
|
|
|
106 |
in.shop2020.model.v1.order.TransactionService.Client tclient = tsc.getClient();
|
|
|
107 |
Client iclient = csc.getClient();
|
|
|
108 |
in.shop2020.logistics.LogisticsService.Client logisticsClient = lsc.getClient();
|
| 734 |
chandransh |
109 |
|
| 670 |
chandransh |
110 |
Order order = null;
|
|
|
111 |
Warehouse warehouse = null;
|
|
|
112 |
Provider provider = null;
|
| 734 |
chandransh |
113 |
String destCode = null;
|
| 1517 |
ankur.sing |
114 |
int barcodeFontSize = 0;
|
| 670 |
chandransh |
115 |
try {
|
| 490 |
rajveer |
116 |
order = tclient.getOrder(orderId);
|
|
|
117 |
warehouse = iclient.getWarehouse(order.getWarehouse_id());
|
| 734 |
chandransh |
118 |
long providerId = order.getLogistics_provider_id();
|
|
|
119 |
provider = logisticsClient.getProvider(providerId);
|
|
|
120 |
destCode = logisticsClient.getDestinationCode(providerId, order.getCustomer_pincode());
|
| 1517 |
ankur.sing |
121 |
barcodeFontSize = Integer.parseInt(ConfigClient.getClient().get(provider.getName().toLowerCase() + "_barcode_fontsize"));
|
| 2784 |
chandransh |
122 |
} catch (TransactionServiceException tse) {
|
|
|
123 |
logger.error("Error while getting order information", tse);
|
| 670 |
chandransh |
124 |
return baosPDF;
|
| 2784 |
chandransh |
125 |
} catch (InventoryServiceException ise) {
|
|
|
126 |
logger.error("Error while getting the warehouse information.", ise);
|
| 670 |
chandransh |
127 |
return baosPDF;
|
| 2784 |
chandransh |
128 |
} catch (LogisticsServiceException lse) {
|
|
|
129 |
logger.error("Error while getting the provider information.", lse);
|
| 670 |
chandransh |
130 |
return baosPDF;
|
| 2784 |
chandransh |
131 |
} catch (ConfigException ce) {
|
|
|
132 |
logger.error("Error while getting the fontsize for the given provider", ce);
|
| 670 |
chandransh |
133 |
return baosPDF;
|
| 2784 |
chandransh |
134 |
} catch (TException te) {
|
|
|
135 |
logger.error("Error while getting some essential information from the services", te);
|
| 1517 |
ankur.sing |
136 |
return baosPDF;
|
| 490 |
rajveer |
137 |
}
|
| 734 |
chandransh |
138 |
|
|
|
139 |
try {
|
|
|
140 |
baosPDF = new ByteArrayOutputStream();
|
|
|
141 |
|
|
|
142 |
Document document = new Document();
|
|
|
143 |
PdfWriter.getInstance(document, baosPDF);
|
|
|
144 |
document.addAuthor("shop2020");
|
|
|
145 |
document.addTitle("Invoice No: " + order.getInvoice_number());
|
|
|
146 |
document.open();
|
| 2784 |
chandransh |
147 |
|
|
|
148 |
PdfPTable dispatchAdviceTable = getDispatchAdviceTable(order, warehouse, provider, barcodeFontSize, destCode);
|
|
|
149 |
dispatchAdviceTable.setSpacingAfter(10.0f);
|
|
|
150 |
dispatchAdviceTable.setWidthPercentage(90.0f);
|
|
|
151 |
|
|
|
152 |
PdfPTable taxTable = getTaxCumRetailInvoiceTable(order, provider);
|
|
|
153 |
taxTable.setSpacingBefore(5.0f);
|
|
|
154 |
taxTable.setWidthPercentage(90.0f);
|
| 734 |
chandransh |
155 |
|
| 2784 |
chandransh |
156 |
document.add(dispatchAdviceTable);
|
|
|
157 |
document.add(new DottedLineSeparator());
|
|
|
158 |
document.add(taxTable);
|
|
|
159 |
document.close();
|
|
|
160 |
baosPDF.close();
|
|
|
161 |
} catch (Exception e) {
|
|
|
162 |
logger.error("Error while generating Invoice: ", e);
|
|
|
163 |
}
|
|
|
164 |
return baosPDF;
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
private PdfPTable getDispatchAdviceTable(Order order, Warehouse warehouse, Provider provider, float barcodeFontSize, String destCode){
|
|
|
168 |
Font barCodeFont = getBarCodeFont(provider, barcodeFontSize);
|
|
|
169 |
|
|
|
170 |
PdfPTable table = new PdfPTable(1);
|
|
|
171 |
table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
|
| 734 |
chandransh |
172 |
|
| 2784 |
chandransh |
173 |
PdfPCell logoCell = getLogoCell();
|
|
|
174 |
PdfPCell titleCell = getTitleCell();
|
|
|
175 |
PdfPTable customerTable = getCustomerAddressTable(order, destCode, false, helvetica12);
|
|
|
176 |
PdfPTable providerInfoTable = getProviderTable(order, provider, barCodeFont);
|
|
|
177 |
|
|
|
178 |
PdfPTable dispatchTable = new PdfPTable(new float[]{0.5f, 0.1f, 0.4f});
|
|
|
179 |
dispatchTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
|
|
|
180 |
dispatchTable.addCell(customerTable);
|
|
|
181 |
dispatchTable.addCell(new Phrase(" "));
|
|
|
182 |
dispatchTable.addCell(providerInfoTable);
|
|
|
183 |
|
|
|
184 |
PdfPTable invoiceTable = getTopInvoiceTable(order);
|
| 734 |
chandransh |
185 |
|
| 2784 |
chandransh |
186 |
PdfPCell addressCell = getAddressCell();
|
|
|
187 |
|
|
|
188 |
table.addCell(logoCell);
|
|
|
189 |
table.addCell(titleCell);
|
|
|
190 |
table.addCell(dispatchTable);
|
|
|
191 |
table.addCell(invoiceTable);
|
|
|
192 |
table.addCell(new Phrase("If undelivered, return to:", helvetica10));
|
|
|
193 |
table.addCell(addressCell);
|
|
|
194 |
table.addCell(new Phrase("Do not pay any extra charges to the Courier."));
|
|
|
195 |
return table;
|
|
|
196 |
}
|
| 734 |
chandransh |
197 |
|
| 2784 |
chandransh |
198 |
private Font getBarCodeFont(Provider provider, float barcodeFontSize) {
|
|
|
199 |
String fontPath = InvoiceGenerationService.class.getResource("/" + provider.getName().toLowerCase() + "/barcode.TTF").getPath();
|
|
|
200 |
FontFactoryImp ttfFontFactory = new FontFactoryImp();
|
|
|
201 |
ttfFontFactory.register(fontPath, "barcode");
|
|
|
202 |
Font barCodeFont = ttfFontFactory.getFont("barcode", BaseFont.CP1252, true, barcodeFontSize);
|
|
|
203 |
return barCodeFont;
|
|
|
204 |
}
|
| 734 |
chandransh |
205 |
|
| 2784 |
chandransh |
206 |
private PdfPCell getLogoCell() {
|
|
|
207 |
String logoPath = InvoiceGenerationService.class.getResource("/logo.jpg").getPath();
|
|
|
208 |
PdfPCell logoCell;
|
|
|
209 |
try {
|
|
|
210 |
logoCell = new PdfPCell(Image.getInstance(logoPath), false);
|
|
|
211 |
} catch (Exception e) {
|
|
|
212 |
//Too Many exceptions to catch here: BadElementException, MalformedURLException and IOException
|
|
|
213 |
logger.warn("Couldn't load the Saholic logo: ", e);
|
|
|
214 |
logoCell = new PdfPCell(new Phrase("Saholic Logo"));
|
|
|
215 |
}
|
|
|
216 |
logoCell.setBorder(Rectangle.NO_BORDER);
|
|
|
217 |
return logoCell;
|
|
|
218 |
}
|
| 734 |
chandransh |
219 |
|
| 2784 |
chandransh |
220 |
private PdfPCell getTitleCell() {
|
|
|
221 |
PdfPCell titleCell = new PdfPCell(new Phrase("Dispatch Advice", helveticaBold12));
|
|
|
222 |
titleCell.setHorizontalAlignment(Element.ALIGN_CENTER);
|
|
|
223 |
titleCell.setBorder(Rectangle.NO_BORDER);
|
|
|
224 |
return titleCell;
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
private PdfPTable getProviderTable(Order order, Provider provider, Font barCodeFont) {
|
|
|
228 |
PdfPTable providerInfoTable = new PdfPTable(1);
|
|
|
229 |
providerInfoTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
|
|
|
230 |
PdfPCell providerNameCell = new PdfPCell(new Phrase(provider.getName(), helveticaBold12));
|
|
|
231 |
providerNameCell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
|
|
232 |
providerNameCell.setBorder(Rectangle.NO_BORDER);
|
|
|
233 |
|
|
|
234 |
PdfPCell awbNumberCell = new PdfPCell(new Paragraph("*" + order.getAirwaybill_no() + "*", barCodeFont));
|
|
|
235 |
awbNumberCell.setPaddingTop(20.0f);
|
|
|
236 |
awbNumberCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
|
|
|
237 |
awbNumberCell.setBorder(Rectangle.NO_BORDER);
|
|
|
238 |
|
|
|
239 |
providerInfoTable.addCell(providerNameCell);
|
|
|
240 |
providerInfoTable.addCell(awbNumberCell);
|
| 3044 |
chandransh |
241 |
providerInfoTable.addCell(new Phrase("Account No : " + provider.getDetails().get(DeliveryType.PREPAID).getAccountNo(), helvetica8));
|
| 2784 |
chandransh |
242 |
providerInfoTable.addCell(new Phrase("AWB Date : " + DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date()), helvetica8));
|
|
|
243 |
providerInfoTable.addCell(new Phrase("Weight : " + order.getTotal_weight() + " Kg", helvetica8));
|
|
|
244 |
return providerInfoTable;
|
|
|
245 |
}
|
|
|
246 |
|
|
|
247 |
private PdfPTable getTopInvoiceTable(Order order){
|
|
|
248 |
PdfPTable invoiceTable = new PdfPTable(new float[]{0.2f, 0.2f, 0.3f, 0.1f, 0.1f, 0.1f});
|
|
|
249 |
invoiceTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
|
|
|
250 |
|
|
|
251 |
invoiceTable.addCell(getInvoiceTableHeader(6));
|
|
|
252 |
|
|
|
253 |
invoiceTable.addCell(new Phrase("Order No", helvetica8));
|
|
|
254 |
invoiceTable.addCell(new Phrase("Paymode", helvetica8));
|
|
|
255 |
invoiceTable.addCell(new Phrase("Product Name", helvetica8));
|
|
|
256 |
invoiceTable.addCell(new Phrase("Quantity", helvetica8));
|
|
|
257 |
invoiceTable.addCell(new Phrase("Rate", helvetica8));
|
|
|
258 |
invoiceTable.addCell(new Phrase("Amount", helvetica8));
|
|
|
259 |
populateTopInvoiceTable(order, invoiceTable);
|
|
|
260 |
|
|
|
261 |
invoiceTable.addCell(getTotalCell(4));
|
|
|
262 |
invoiceTable.addCell(getRupeesCell());
|
|
|
263 |
invoiceTable.addCell(getTotalAmountCell(order.getTotal_amount()));
|
|
|
264 |
|
|
|
265 |
PdfPCell tinCell = new PdfPCell(new Phrase("TIN NO. " + tinNo, helvetica8));
|
|
|
266 |
tinCell.setColspan(6);
|
|
|
267 |
tinCell.setPadding(2);
|
|
|
268 |
invoiceTable.addCell(tinCell);
|
|
|
269 |
|
|
|
270 |
return invoiceTable;
|
|
|
271 |
}
|
|
|
272 |
|
|
|
273 |
private void populateTopInvoiceTable(Order order, PdfPTable invoiceTable) {
|
|
|
274 |
List<LineItem> lineitems = order.getLineitems();
|
|
|
275 |
for (LineItem lineitem : lineitems) {
|
|
|
276 |
invoiceTable.addCell(new Phrase(order.getId() + "", helvetica8));
|
|
|
277 |
invoiceTable.addCell(new Phrase("Prepaid", helvetica8));
|
| 734 |
chandransh |
278 |
|
| 2784 |
chandransh |
279 |
invoiceTable.addCell(getProductNameCell(lineitem, false));
|
|
|
280 |
|
|
|
281 |
invoiceTable.addCell(new Phrase(lineitem.getQuantity() + "", helvetica8));
|
|
|
282 |
|
|
|
283 |
invoiceTable.addCell(getPriceCell(lineitem.getUnit_price()));
|
|
|
284 |
|
|
|
285 |
invoiceTable.addCell(getPriceCell(lineitem.getTotal_price()));
|
|
|
286 |
}
|
|
|
287 |
}
|
|
|
288 |
|
|
|
289 |
private PdfPCell getAddressCell() {
|
|
|
290 |
Paragraph addressParagraph = new Paragraph(ourAddress, new Font(FontFamily.TIMES_ROMAN, 8f));
|
|
|
291 |
PdfPCell addressCell = new PdfPCell();
|
|
|
292 |
addressCell.addElement(addressParagraph);
|
|
|
293 |
addressCell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
|
|
294 |
addressCell.setBorder(Rectangle.NO_BORDER);
|
|
|
295 |
return addressCell;
|
|
|
296 |
}
|
|
|
297 |
|
|
|
298 |
private PdfPTable getTaxCumRetailInvoiceTable(Order order, Provider provider){
|
|
|
299 |
PdfPTable taxTable = new PdfPTable(1);
|
|
|
300 |
taxTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
|
|
|
301 |
taxTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
|
|
|
302 |
|
|
|
303 |
PdfPCell retailInvoiceTitleCell = new PdfPCell(new Phrase("TAX CUM RETAIL INVOICE", helveticaBold12));
|
|
|
304 |
retailInvoiceTitleCell.setHorizontalAlignment(Element.ALIGN_CENTER);
|
|
|
305 |
retailInvoiceTitleCell.setBorder(Rectangle.NO_BORDER);
|
|
|
306 |
|
|
|
307 |
Paragraph sorlAddress = new Paragraph(ourAddress + "TIN NO. " + tinNo, new Font(FontFamily.TIMES_ROMAN, 8f, Element.ALIGN_CENTER));
|
|
|
308 |
PdfPCell sorlAddressCell = new PdfPCell(sorlAddress);
|
|
|
309 |
sorlAddressCell.addElement(sorlAddress);
|
|
|
310 |
sorlAddressCell.setHorizontalAlignment(Element.ALIGN_CENTER);
|
|
|
311 |
|
|
|
312 |
PdfPTable customerAddress = getCustomerAddressTable(order, null, true, helvetica8);
|
|
|
313 |
PdfPTable orderDetails = getOrderDetails(order, provider);
|
|
|
314 |
|
|
|
315 |
PdfPTable addrAndOrderDetailsTable = new PdfPTable(new float[]{0.5f, 0.1f, 0.4f});
|
|
|
316 |
addrAndOrderDetailsTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
|
|
|
317 |
addrAndOrderDetailsTable.addCell(customerAddress);
|
|
|
318 |
addrAndOrderDetailsTable.addCell(new Phrase(" "));
|
|
|
319 |
addrAndOrderDetailsTable.addCell(orderDetails);
|
|
|
320 |
|
|
|
321 |
boolean isVAT = order.getCustomer_pincode().startsWith(delhiPincodePrefix);
|
|
|
322 |
PdfPTable invoiceTable = getBottomInvoiceTable(order, isVAT);
|
|
|
323 |
|
|
|
324 |
PdfPCell disclaimerCell = new PdfPCell(new Phrase("Goods once sold will not be taken back.\nAll disputes subject to Delhi Jurisdiction.", helvetica8));
|
|
|
325 |
disclaimerCell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
|
|
326 |
disclaimerCell.setBorder(Rectangle.NO_BORDER);
|
|
|
327 |
|
|
|
328 |
taxTable.addCell(retailInvoiceTitleCell);
|
|
|
329 |
taxTable.addCell(sorlAddress);
|
|
|
330 |
taxTable.addCell(addrAndOrderDetailsTable);
|
|
|
331 |
taxTable.addCell(invoiceTable);
|
|
|
332 |
taxTable.addCell(disclaimerCell);
|
|
|
333 |
|
|
|
334 |
return taxTable;
|
|
|
335 |
}
|
|
|
336 |
|
|
|
337 |
private PdfPTable getCustomerAddressTable(Order order, String destCode, boolean showPaymentMode, Font font){
|
|
|
338 |
PdfPTable customerTable = new PdfPTable(1);
|
|
|
339 |
customerTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
|
|
|
340 |
customerTable.addCell(new Phrase(order.getCustomer_name(), font));
|
|
|
341 |
customerTable.addCell(new Phrase(order.getCustomer_address1(), font));
|
|
|
342 |
customerTable.addCell(new Phrase(order.getCustomer_address2(), font));
|
|
|
343 |
customerTable.addCell(new Phrase(order.getCustomer_city() + "," + order.getCustomer_state(), font));
|
|
|
344 |
if(destCode != null)
|
| 734 |
chandransh |
345 |
customerTable.addCell(new Phrase(order.getCustomer_pincode() + " - " + destCode, helvetica16));
|
| 2784 |
chandransh |
346 |
else
|
|
|
347 |
customerTable.addCell(new Phrase(order.getCustomer_pincode(), font));
|
|
|
348 |
customerTable.addCell(new Phrase("Phone :" + order.getCustomer_mobilenumber(), font));
|
|
|
349 |
if(showPaymentMode){
|
|
|
350 |
customerTable.addCell(new Phrase(" ", font));
|
|
|
351 |
customerTable.addCell(new Phrase("Payment Mode: Prepaid", font));
|
|
|
352 |
}
|
|
|
353 |
return customerTable;
|
|
|
354 |
}
|
|
|
355 |
|
|
|
356 |
private PdfPTable getOrderDetails(Order order, Provider provider){
|
|
|
357 |
PdfPTable orderTable = new PdfPTable(new float[]{0.4f, 0.6f});
|
|
|
358 |
orderTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
|
|
|
359 |
|
|
|
360 |
orderTable.addCell(new Phrase("Invoice No:", helvetica8));
|
|
|
361 |
orderTable.addCell(new Phrase(order.getInvoice_number(), helvetica8));
|
|
|
362 |
|
|
|
363 |
orderTable.addCell(new Phrase("Date:", helvetica8));
|
|
|
364 |
orderTable.addCell(new Phrase(DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date(order.getBilling_timestamp())), helvetica8));
|
|
|
365 |
|
|
|
366 |
orderTable.addCell(new Phrase(" "));
|
|
|
367 |
orderTable.addCell(new Phrase(" "));
|
|
|
368 |
|
|
|
369 |
orderTable.addCell(new Phrase("Order ID:", helvetica8));
|
|
|
370 |
orderTable.addCell(new Phrase("" + order.getId(), helvetica8));
|
|
|
371 |
|
|
|
372 |
orderTable.addCell(new Phrase("Order Date:", helvetica8));
|
|
|
373 |
orderTable.addCell(new Phrase(DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date(order.getCreated_timestamp())), helvetica8));
|
|
|
374 |
|
|
|
375 |
orderTable.addCell(new Phrase("Courier:", helvetica8));
|
|
|
376 |
orderTable.addCell(new Phrase(provider.getName(), helvetica8));
|
|
|
377 |
|
|
|
378 |
orderTable.addCell(new Phrase("AWB No:", helvetica8));
|
|
|
379 |
orderTable.addCell(new Phrase(order.getAirwaybill_no(), helvetica8));
|
|
|
380 |
|
|
|
381 |
orderTable.addCell(new Phrase("AWB Date:", helvetica8));
|
|
|
382 |
orderTable.addCell(new Phrase(DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date()), helvetica8));
|
|
|
383 |
|
|
|
384 |
return orderTable;
|
|
|
385 |
}
|
|
|
386 |
|
|
|
387 |
private PdfPTable getBottomInvoiceTable(Order order, boolean isVAT){
|
|
|
388 |
PdfPTable invoiceTable = new PdfPTable(new float[]{0.2f, 0.5f, 0.1f, 0.1f, 0.1f});
|
|
|
389 |
invoiceTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
|
| 734 |
chandransh |
390 |
|
| 2784 |
chandransh |
391 |
invoiceTable.addCell(getInvoiceTableHeader(5));
|
| 734 |
chandransh |
392 |
|
| 2784 |
chandransh |
393 |
invoiceTable.addCell(new Phrase("Sl. No.", helveticaBold8));
|
|
|
394 |
invoiceTable.addCell(new Phrase("Description", helveticaBold8));
|
|
|
395 |
invoiceTable.addCell(new Phrase("Quantity", helveticaBold8));
|
|
|
396 |
invoiceTable.addCell(new Phrase("Rate (Rs)", helveticaBold8));
|
|
|
397 |
invoiceTable.addCell(new Phrase("Amount (Rs)", helveticaBold8));
|
|
|
398 |
|
|
|
399 |
double orderAmount = order.getTotal_amount();
|
|
|
400 |
double rate = getTaxRate(orderAmount);
|
|
|
401 |
double salesTax = (rate * orderAmount)/(100 + rate);
|
|
|
402 |
|
|
|
403 |
populateBottomInvoiceTable(order, invoiceTable, rate);
|
|
|
404 |
|
|
|
405 |
PdfPCell salesTaxCell = getPriceCell(salesTax);
|
|
|
406 |
|
|
|
407 |
invoiceTable.addCell(getVATLabelCell(isVAT));
|
|
|
408 |
invoiceTable.addCell(new Phrase(rate + "%", helvetica8));
|
|
|
409 |
invoiceTable.addCell(salesTaxCell);
|
|
|
410 |
|
|
|
411 |
invoiceTable.addCell(getEmptyCell(5));
|
|
|
412 |
|
|
|
413 |
invoiceTable.addCell(getTotalCell(3));
|
|
|
414 |
invoiceTable.addCell(getRupeesCell());
|
|
|
415 |
invoiceTable.addCell(getTotalAmountCell(orderAmount));
|
|
|
416 |
|
|
|
417 |
invoiceTable.addCell(new Phrase("Amount in Words:", helvetica8));
|
|
|
418 |
invoiceTable.addCell(getAmountInWordsCell(orderAmount));
|
|
|
419 |
|
|
|
420 |
invoiceTable.addCell(getEOECell(5));
|
|
|
421 |
|
|
|
422 |
return invoiceTable;
|
|
|
423 |
}
|
| 734 |
chandransh |
424 |
|
| 2784 |
chandransh |
425 |
private PdfPCell getInvoiceTableHeader(int colspan) {
|
|
|
426 |
PdfPCell invoiceTableHeader = new PdfPCell(new Phrase("Order Details:", helveticaBold12));
|
|
|
427 |
invoiceTableHeader.setBorder(Rectangle.NO_BORDER);
|
|
|
428 |
invoiceTableHeader.setColspan(colspan);
|
|
|
429 |
invoiceTableHeader.setPaddingTop(10);
|
|
|
430 |
return invoiceTableHeader;
|
|
|
431 |
}
|
|
|
432 |
|
|
|
433 |
private double getTaxRate(double orderAmount) {
|
|
|
434 |
double rate;
|
|
|
435 |
if(orderAmount <= salesTaxCutOff){
|
|
|
436 |
rate = salesTaxLowRate;
|
|
|
437 |
} else {
|
|
|
438 |
rate = salesTaxHighRate;
|
|
|
439 |
}
|
|
|
440 |
return rate;
|
|
|
441 |
}
|
|
|
442 |
|
|
|
443 |
private void populateBottomInvoiceTable(Order order, PdfPTable invoiceTable, double rate) {
|
|
|
444 |
for (LineItem lineitem : order.getLineitems()) {
|
|
|
445 |
invoiceTable.addCell(new Phrase("" + order.getId() , helvetica8));
|
| 734 |
chandransh |
446 |
|
| 2784 |
chandransh |
447 |
invoiceTable.addCell(getProductNameCell(lineitem, true));
|
| 734 |
chandransh |
448 |
|
| 2784 |
chandransh |
449 |
invoiceTable.addCell(new Phrase("" + lineitem.getQuantity(), helvetica8));
|
|
|
450 |
|
|
|
451 |
double itemPrice = lineitem.getUnit_price();
|
|
|
452 |
double showPrice = (100 * itemPrice)/(100 + rate);
|
|
|
453 |
invoiceTable.addCell(getPriceCell(showPrice)); //Unit Price Cell
|
|
|
454 |
|
|
|
455 |
double totalPrice = lineitem.getTotal_price();
|
|
|
456 |
showPrice = (100 * totalPrice)/(100 + rate);
|
|
|
457 |
invoiceTable.addCell(getPriceCell(showPrice)); //Total Price Cell
|
|
|
458 |
}
|
|
|
459 |
}
|
| 734 |
chandransh |
460 |
|
| 2784 |
chandransh |
461 |
private PdfPCell getProductNameCell(LineItem lineitem, boolean appendIMEI) {
|
|
|
462 |
String itemName = getItemDisplayName(lineitem, appendIMEI);
|
|
|
463 |
PdfPCell productNameCell = new PdfPCell(new Phrase(itemName, helvetica8));
|
|
|
464 |
productNameCell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
|
|
465 |
return productNameCell;
|
|
|
466 |
}
|
| 734 |
chandransh |
467 |
|
| 2784 |
chandransh |
468 |
private PdfPCell getPriceCell(double price) {
|
|
|
469 |
PdfPCell totalPriceCell = new PdfPCell(new Phrase(amountFormat.format(price), helvetica8));
|
|
|
470 |
totalPriceCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
|
|
|
471 |
return totalPriceCell;
|
|
|
472 |
}
|
| 734 |
chandransh |
473 |
|
| 2784 |
chandransh |
474 |
private PdfPCell getVATLabelCell(boolean isVAT) {
|
|
|
475 |
PdfPCell vatCell = null;
|
|
|
476 |
if(isVAT){
|
|
|
477 |
vatCell = new PdfPCell(new Phrase("VAT", helveticaBold8));
|
|
|
478 |
} else {
|
|
|
479 |
vatCell = new PdfPCell(new Phrase("CST", helveticaBold8));
|
|
|
480 |
}
|
|
|
481 |
vatCell.setColspan(3);
|
|
|
482 |
vatCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
|
|
|
483 |
return vatCell;
|
|
|
484 |
}
|
|
|
485 |
|
|
|
486 |
private PdfPCell getEmptyCell(int colspan) {
|
|
|
487 |
PdfPCell emptyCell = new PdfPCell(new Phrase(" ", helvetica8));
|
|
|
488 |
emptyCell.setColspan(colspan);
|
|
|
489 |
return emptyCell;
|
|
|
490 |
}
|
| 734 |
chandransh |
491 |
|
| 2784 |
chandransh |
492 |
private PdfPCell getTotalCell(int colspan) {
|
|
|
493 |
PdfPCell totalCell = new PdfPCell(new Phrase("Total", helveticaBold8));
|
|
|
494 |
totalCell.setColspan(colspan);
|
|
|
495 |
totalCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
|
|
|
496 |
return totalCell;
|
|
|
497 |
}
|
|
|
498 |
|
|
|
499 |
private PdfPCell getRupeesCell() {
|
|
|
500 |
PdfPCell rupeesCell = new PdfPCell(new Phrase("Rs.", helveticaBold8));
|
|
|
501 |
rupeesCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
|
|
|
502 |
return rupeesCell;
|
|
|
503 |
}
|
| 734 |
chandransh |
504 |
|
| 2784 |
chandransh |
505 |
private PdfPCell getTotalAmountCell(double orderAmount) {
|
|
|
506 |
PdfPCell totalAmountCell = new PdfPCell(new Phrase(amountFormat.format(orderAmount), helveticaBold8));
|
|
|
507 |
totalAmountCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
|
|
|
508 |
return totalAmountCell;
|
|
|
509 |
}
|
|
|
510 |
|
|
|
511 |
/**
|
|
|
512 |
* This method uses ICU4J libraries to convert the given amount into words
|
|
|
513 |
* of Indian locale.
|
|
|
514 |
*
|
|
|
515 |
* @param orderAmount
|
|
|
516 |
* The amount to convert.
|
|
|
517 |
* @return the string representation of the given amount.
|
|
|
518 |
*/
|
|
|
519 |
private PdfPCell getAmountInWordsCell(double orderAmount) {
|
|
|
520 |
RuleBasedNumberFormat amountInWordsFormat = new RuleBasedNumberFormat(indianLocale, RuleBasedNumberFormat.SPELLOUT);
|
|
|
521 |
StringBuilder amountInWords = new StringBuilder("Rs. ");
|
|
|
522 |
amountInWords.append(WordUtils.capitalize(amountInWordsFormat.format((int)orderAmount)));
|
|
|
523 |
amountInWords.append(" and ");
|
|
|
524 |
amountInWords.append(WordUtils.capitalize(amountInWordsFormat.format((int)(orderAmount*100)%100)));
|
|
|
525 |
amountInWords.append(" paise");
|
|
|
526 |
|
|
|
527 |
PdfPCell amountInWordsCell= new PdfPCell(new Phrase(amountInWords.toString(), helveticaBold8));
|
|
|
528 |
amountInWordsCell.setColspan(4);
|
|
|
529 |
return amountInWordsCell;
|
|
|
530 |
}
|
|
|
531 |
|
|
|
532 |
/**
|
|
|
533 |
* Returns the item name to be displayed in the invoice table.
|
|
|
534 |
*
|
|
|
535 |
* @param lineitem
|
|
|
536 |
* The line item whose name has to be displayed
|
|
|
537 |
* @param appendIMEI
|
|
|
538 |
* Whether to attach the IMEI No. to the item name
|
|
|
539 |
* @return The name to be displayed for the given line item.
|
|
|
540 |
*/
|
|
|
541 |
private String getItemDisplayName(LineItem lineitem, boolean appendIMEI){
|
|
|
542 |
StringBuffer itemName = new StringBuffer();
|
|
|
543 |
if(lineitem.getBrand()!= null)
|
|
|
544 |
itemName.append(lineitem.getBrand() + " ");
|
|
|
545 |
if(lineitem.getModel_name() != null)
|
|
|
546 |
itemName.append(lineitem.getModel_name() + " ");
|
|
|
547 |
if(lineitem.getModel_number() != null )
|
|
|
548 |
itemName.append(lineitem.getModel_number() + " ");
|
|
|
549 |
if(lineitem.getColor() != null && !lineitem.getColor().trim().equals("NA"))
|
|
|
550 |
itemName.append("("+lineitem.getColor()+")");
|
|
|
551 |
if(appendIMEI && lineitem.isSetImei_number()){
|
|
|
552 |
itemName.append("\nIMEI No. " + lineitem.getImei_number());
|
| 734 |
chandransh |
553 |
}
|
| 2784 |
chandransh |
554 |
|
|
|
555 |
return itemName.toString();
|
| 734 |
chandransh |
556 |
}
|
|
|
557 |
|
| 2784 |
chandransh |
558 |
/**
|
|
|
559 |
*
|
|
|
560 |
* @param colspan
|
|
|
561 |
* @return a PdfPCell containing the E&OE text and spanning the given
|
|
|
562 |
* no. of columns
|
|
|
563 |
*/
|
|
|
564 |
private PdfPCell getEOECell(int colspan) {
|
|
|
565 |
PdfPCell eoeCell = new PdfPCell(new Phrase("E & O.E", helvetica8));
|
|
|
566 |
eoeCell.setColspan(colspan);
|
|
|
567 |
eoeCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
|
|
|
568 |
return eoeCell;
|
|
|
569 |
}
|
|
|
570 |
|
| 505 |
rajveer |
571 |
public static void main(String[] args) throws IOException {
|
| 734 |
chandransh |
572 |
InvoiceGenerationService invoiceGenerationService = new InvoiceGenerationService();
|
| 2784 |
chandransh |
573 |
long orderId = 434;
|
| 1476 |
ankur.sing |
574 |
ByteArrayOutputStream baos = invoiceGenerationService.generateInvoice(orderId);
|
|
|
575 |
String userHome = System.getProperty("user.home");
|
|
|
576 |
File f = new File(userHome + "/invoice-" + orderId + ".pdf");
|
| 505 |
rajveer |
577 |
FileOutputStream fos = new FileOutputStream(f);
|
|
|
578 |
baos.writeTo(fos);
|
| 1476 |
ankur.sing |
579 |
System.out.println("Invoice generated.");
|
| 490 |
rajveer |
580 |
}
|
|
|
581 |
}
|