Subversion Repositories SmartDukaan

Rev

Rev 4313 | Rev 4550 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4313 Rev 4361
Line 7... Line 7...
7
import in.shop2020.model.v1.catalog.InventoryServiceException;
7
import in.shop2020.model.v1.catalog.InventoryServiceException;
8
import in.shop2020.model.v1.catalog.Warehouse;
8
import in.shop2020.model.v1.catalog.Warehouse;
9
import in.shop2020.model.v1.catalog.InventoryService.Client;
9
import in.shop2020.model.v1.catalog.InventoryService.Client;
10
import in.shop2020.model.v1.order.LineItem;
10
import in.shop2020.model.v1.order.LineItem;
11
import in.shop2020.model.v1.order.Order;
11
import in.shop2020.model.v1.order.Order;
12
import in.shop2020.model.v1.order.TransactionServiceException;
12
import in.shop2020.model.v1.order.OrderStatus;
13
import in.shop2020.thrift.clients.CatalogClient;
13
import in.shop2020.thrift.clients.CatalogClient;
14
import in.shop2020.thrift.clients.LogisticsClient;
14
import in.shop2020.thrift.clients.LogisticsClient;
15
import in.shop2020.thrift.clients.TransactionClient;
15
import in.shop2020.thrift.clients.TransactionClient;
16
import in.shop2020.thrift.clients.config.ConfigClient;
16
import in.shop2020.thrift.clients.config.ConfigClient;
17
 
17
 
Line 19... Line 19...
19
import java.io.File;
19
import java.io.File;
20
import java.io.FileOutputStream;
20
import java.io.FileOutputStream;
21
import java.io.IOException;
21
import java.io.IOException;
22
import java.text.DateFormat;
22
import java.text.DateFormat;
23
import java.text.DecimalFormat;
23
import java.text.DecimalFormat;
-
 
24
import java.util.ArrayList;
24
import java.util.Date;
25
import java.util.Date;
25
import java.util.Enumeration;
26
import java.util.Enumeration;
26
import java.util.List;
27
import java.util.List;
27
import java.util.Locale;
28
import java.util.Locale;
28
import java.util.Properties;
29
import java.util.Properties;
Line 63... Line 64...
63
    private static Logger logger = LoggerFactory.getLogger(InvoiceServlet.class);
64
    private static Logger logger = LoggerFactory.getLogger(InvoiceServlet.class);
64
    
65
    
65
    @Override
66
    @Override
66
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
67
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
67
        long orderId = Long.parseLong(request.getParameter("id"));
68
        long orderId = Long.parseLong(request.getParameter("id"));
-
 
69
        long warehouseId = Long.parseLong(request.getParameter("warehouse"));
68
        boolean withBill = false;
70
        boolean withBill = false;
-
 
71
        boolean printAll = false;
69
        try {
72
        try {
70
            withBill = Boolean.parseBoolean(request.getParameter("withBill"));
73
            withBill = Boolean.parseBoolean(request.getParameter("withBill"));
71
        } catch(Exception e){
74
        } catch(Exception e){
72
            logger.warn("Couldn't infer whether bill should be printed. Not printing the bill.", e);
75
            logger.warn("Couldn't infer whether bill should be printed. Not printing the bill.", e);
73
        }
76
        }
-
 
77
        try {
-
 
78
        	printAll = Boolean.parseBoolean(request.getParameter("printAll"));
-
 
79
        } catch(Exception e){
-
 
80
            logger.warn("Couldn't infer whether bill should be printed. Not printing the bill.", e);
74
          
81
        }
-
 
82
        
75
        logger.info("Printing invoice for order id: " + orderId);
83
        logger.info("Printing invoice for order id: " + orderId);
76
        
84
        
77
        InvoiceGenerationService invoiceGenerationService = new InvoiceGenerationService();
85
        InvoiceGenerationService invoiceGenerationService = new InvoiceGenerationService();
78
        ByteArrayOutputStream baos = invoiceGenerationService.generateInvoice(orderId, withBill);
86
        ByteArrayOutputStream baos = invoiceGenerationService.generateInvoice(orderId, withBill, printAll, warehouseId);
79
        response.setContentType("application/pdf");
87
        response.setContentType("application/pdf");
80
        response.setHeader("Content-disposition", "inline; filename=invoice-"+orderId+".pdf" );
88
        response.setHeader("Content-disposition", "inline; filename=invoice-"+orderId+".pdf" );
81
        
89
        
82
        ServletOutputStream sos;
90
        ServletOutputStream sos;
83
        try {
91
        try {
Line 141... Line 149...
141
        } catch (Exception e) {
149
        } catch (Exception e) {
142
            logger.error("Error while instantiating thrift clients.", e);
150
            logger.error("Error while instantiating thrift clients.", e);
143
        }
151
        }
144
    }
152
    }
145
 
153
 
146
    public ByteArrayOutputStream generateInvoice(long orderId, boolean withBill) {
154
    public ByteArrayOutputStream generateInvoice(long orderId, boolean withBill, boolean printAll, long warehouseId) {
147
        ByteArrayOutputStream baosPDF = null;
155
        ByteArrayOutputStream baosPDF = null;
148
        in.shop2020.model.v1.order.TransactionService.Client tclient = tsc.getClient();
156
        in.shop2020.model.v1.order.TransactionService.Client tclient = tsc.getClient();
149
        Client iclient = csc.getClient();
157
        Client iclient = csc.getClient();
150
        in.shop2020.logistics.LogisticsService.Client logisticsClient = lsc.getClient();
158
        in.shop2020.logistics.LogisticsService.Client logisticsClient = lsc.getClient();
151
 
159
 
152
        Order order = null;
-
 
153
        Warehouse warehouse = null;
-
 
154
        Provider provider = null;
-
 
155
        String destCode = null;
-
 
156
        int barcodeFontSize = 0;
-
 
157
        try {
-
 
158
            order = tclient.getOrder(orderId);
-
 
159
            warehouse = iclient.getWarehouse(order.getWarehouse_id());
-
 
160
            long providerId = order.getLogistics_provider_id();
-
 
161
            provider = logisticsClient.getProvider(providerId);
-
 
162
            destCode = logisticsClient.getDestinationCode(providerId, order.getCustomer_pincode());
-
 
163
            barcodeFontSize = Integer.parseInt(ConfigClient.getClient().get(provider.getName().toLowerCase() + "_barcode_fontsize"));
-
 
164
        } catch (TransactionServiceException tse) {
-
 
165
            logger.error("Error while getting order information", tse);
-
 
166
            return baosPDF;
-
 
167
        } catch (InventoryServiceException ise) {
-
 
168
            logger.error("Error while getting the warehouse information.", ise);
-
 
169
            return baosPDF;
-
 
170
        } catch (LogisticsServiceException lse) {
-
 
171
            logger.error("Error while getting the provider information.", lse);
-
 
172
            return baosPDF;
-
 
173
        } catch (ConfigException ce) {
-
 
174
            logger.error("Error while getting the fontsize for the given provider", ce);
-
 
175
            return baosPDF;
-
 
176
        } catch (TException te) {
-
 
177
            logger.error("Error while getting some essential information from the services", te);
-
 
178
            return baosPDF;
-
 
179
        }
160
        
180
 
-
 
181
        try {
161
        try {
182
            baosPDF = new ByteArrayOutputStream();
162
            baosPDF = new ByteArrayOutputStream();
183
 
163
 
184
            Document document = new Document();
164
            Document document = new Document();
185
            PdfWriter.getInstance(document, baosPDF);
165
            PdfWriter.getInstance(document, baosPDF);
186
            document.addAuthor("shop2020");
166
            document.addAuthor("shop2020");
187
            document.addTitle("Invoice No: " + order.getInvoice_number());
167
            //document.addTitle("Invoice No: " + order.getInvoice_number());
188
            document.open();
168
            document.open();
189
            
169
        
190
            PdfPTable dispatchAdviceTable = getDispatchAdviceTable(order, warehouse, provider, barcodeFontSize, destCode, withBill);
-
 
191
            dispatchAdviceTable.setSpacingAfter(10.0f);
170
            List<Order> orders = new ArrayList<Order>();
192
            dispatchAdviceTable.setWidthPercentage(90.0f);
-
 
193
 
-
 
194
            document.add(dispatchAdviceTable);
171
            if(printAll){
195
            if(withBill){
172
	            try {
196
                PdfPTable taxTable = getTaxCumRetailInvoiceTable(order, provider);
173
	                orders = tclient.getAllOrders(OrderStatus.ACCEPTED, 0, 0, warehouseId);
197
                taxTable.setSpacingBefore(5.0f);
174
	            } catch (Exception e) {
198
                taxTable.setWidthPercentage(90.0f);
-
 
199
                document.add(new DottedLineSeparator());
175
	            	logger.error("Error while getting order information", e);
200
                document.add(taxTable);
176
	                return baosPDF; 
-
 
177
				}
201
            }else{
178
            }else{
-
 
179
            	orders.add(tclient.getOrder(orderId));	
-
 
180
            }
-
 
181
            
-
 
182
            for(Order order: orders){
-
 
183
            	Warehouse warehouse = null;
-
 
184
            	Provider provider = null;
-
 
185
            	String destCode = null;
-
 
186
            	int barcodeFontSize = 0;
-
 
187
            	try {
-
 
188
            		warehouse = iclient.getWarehouse(order.getWarehouse_id());
-
 
189
            		long providerId = order.getLogistics_provider_id();
-
 
190
            		provider = logisticsClient.getProvider(providerId);
-
 
191
            		destCode = logisticsClient.getDestinationCode(providerId, order.getCustomer_pincode());
-
 
192
            		barcodeFontSize = Integer.parseInt(ConfigClient.getClient().get(provider.getName().toLowerCase() + "_barcode_fontsize"));
-
 
193
            	} catch (InventoryServiceException ise) {
-
 
194
            		logger.error("Error while getting the warehouse information.", ise);
-
 
195
            		return baosPDF;
-
 
196
            	} catch (LogisticsServiceException lse) {
-
 
197
            		logger.error("Error while getting the provider information.", lse);
-
 
198
            		return baosPDF;
-
 
199
            	} catch (ConfigException ce) {
-
 
200
            		logger.error("Error while getting the fontsize for the given provider", ce);
-
 
201
            		return baosPDF;
-
 
202
            	} catch (TException te) {
-
 
203
            		logger.error("Error while getting some essential information from the services", te);
-
 
204
            		return baosPDF;
-
 
205
            	}
-
 
206
 
-
 
207
	            PdfPTable dispatchAdviceTable = getDispatchAdviceTable(order, warehouse, provider, barcodeFontSize, destCode, withBill);
-
 
208
	            dispatchAdviceTable.setSpacingAfter(10.0f);
-
 
209
	            dispatchAdviceTable.setWidthPercentage(90.0f);
-
 
210
 
-
 
211
	            document.add(dispatchAdviceTable);
-
 
212
	            if(withBill){
-
 
213
	                PdfPTable taxTable = getTaxCumRetailInvoiceTable(order, provider);
-
 
214
	                taxTable.setSpacingBefore(5.0f);
-
 
215
	                taxTable.setWidthPercentage(90.0f);
-
 
216
	                document.add(new DottedLineSeparator());
-
 
217
	                document.add(taxTable);
-
 
218
	            }else{
202
            	PdfPTable extraInfoTable = getExtraInfoTable(order, provider, 16);
219
	            	PdfPTable extraInfoTable = getExtraInfoTable(order, provider, 16);
203
            	extraInfoTable.setSpacingBefore(5.0f);
220
	            	extraInfoTable.setSpacingBefore(5.0f);
204
            	extraInfoTable.setWidthPercentage(90.0f);
221
	            	extraInfoTable.setWidthPercentage(90.0f);
205
                document.add(new DottedLineSeparator());
222
	                document.add(new DottedLineSeparator());
206
                document.add(extraInfoTable);
223
	                document.add(extraInfoTable);
-
 
224
	            }
-
 
225
	            document.newPage();
207
            }
226
            }
208
            document.close();
227
            document.close();
209
            baosPDF.close();
228
            baosPDF.close();
210
        } catch (Exception e) {
229
        } catch (Exception e) {
211
            logger.error("Error while generating Invoice: ", e);
230
            logger.error("Error while generating Invoice: ", e);
Line 674... Line 693...
674
        return extraInfoTable;
693
        return extraInfoTable;
675
    }
694
    }
676
    public static void main(String[] args) throws IOException {
695
    public static void main(String[] args) throws IOException {
677
        InvoiceGenerationService invoiceGenerationService = new InvoiceGenerationService();
696
        InvoiceGenerationService invoiceGenerationService = new InvoiceGenerationService();
678
        long orderId = 542;
697
        long orderId = 542;
679
        ByteArrayOutputStream baos = invoiceGenerationService.generateInvoice(orderId, false);
698
        ByteArrayOutputStream baos = invoiceGenerationService.generateInvoice(orderId, false, false, 1);
680
        String userHome = System.getProperty("user.home");
699
        String userHome = System.getProperty("user.home");
681
        File f = new File(userHome + "/invoice-" + orderId + ".pdf");
700
        File f = new File(userHome + "/invoice-" + orderId + ".pdf");
682
        FileOutputStream fos = new FileOutputStream(f);
701
        FileOutputStream fos = new FileOutputStream(f);
683
        baos.writeTo(fos);
702
        baos.writeTo(fos);
684
        System.out.println("Invoice generated.");
703
        System.out.println("Invoice generated.");