| 25162 |
amit.gupta |
1 |
package com.spice.profitmandi.service;
|
|
|
2 |
|
| 30192 |
amit.gupta |
3 |
import com.itextpdf.text.Document;
|
|
|
4 |
import com.itextpdf.text.pdf.PdfWriter;
|
|
|
5 |
import com.jcraft.jsch.*;
|
|
|
6 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
7 |
import com.spice.profitmandi.common.web.client.RestClient;
|
|
|
8 |
import com.spice.profitmandi.dao.entity.transaction.Order;
|
|
|
9 |
import com.spice.profitmandi.dao.repository.fofo.PurchaseRepository;
|
|
|
10 |
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
|
|
|
11 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
12 |
import org.springframework.stereotype.Service;
|
|
|
13 |
|
| 25162 |
amit.gupta |
14 |
import java.io.IOException;
|
|
|
15 |
import java.io.InputStream;
|
|
|
16 |
import java.io.OutputStream;
|
|
|
17 |
import java.time.LocalDateTime;
|
| 30192 |
amit.gupta |
18 |
import java.util.List;
|
| 25162 |
amit.gupta |
19 |
import java.util.Properties;
|
|
|
20 |
|
|
|
21 |
@Service
|
|
|
22 |
public class PurchaseInvoiceService {
|
|
|
23 |
|
| 32724 |
amit.gupta |
24 |
@Autowired
|
|
|
25 |
PurchaseRepository purchaseRepository;
|
| 25162 |
amit.gupta |
26 |
|
| 32724 |
amit.gupta |
27 |
@Autowired
|
|
|
28 |
OrderRepository orderRepository;
|
| 25162 |
amit.gupta |
29 |
|
| 32724 |
amit.gupta |
30 |
@Autowired
|
|
|
31 |
RestClient restClient;
|
| 30192 |
amit.gupta |
32 |
|
| 32724 |
amit.gupta |
33 |
//private static final String remoteHost = "192.168.149.148";
|
|
|
34 |
private static final String remoteHost = "support.shop2020.in";
|
|
|
35 |
private static final String invoicepath = "/SaholicInvoices/";
|
| 25162 |
amit.gupta |
36 |
|
| 32724 |
amit.gupta |
37 |
private static final String passowrd = "spic@2015support";
|
| 25162 |
amit.gupta |
38 |
|
| 32724 |
amit.gupta |
39 |
public void getInvoices(String fofoId, String invoiceNumber, OutputStream outputStream)
|
|
|
40 |
throws Exception {
|
|
|
41 |
List<Order> orders = orderRepository.selectByInvoiceNumber(invoiceNumber);
|
|
|
42 |
if (orders.size() == 0) {
|
|
|
43 |
throw new ProfitMandiBusinessException("Invalid invoice Number", invoiceNumber, "Invalid invoice number");
|
|
|
44 |
}
|
|
|
45 |
LocalDateTime billingDate = orders.get(0).getBillingTimestamp();
|
|
|
46 |
String invoicePath = String.format("%s%d-%d/%d/%s.pdf", invoicepath, billingDate.getYear(), billingDate.getMonth().getValue() - 1,
|
|
|
47 |
fofoId, invoiceNumber);
|
|
|
48 |
Session session = createSession("root", remoteHost, 22, passowrd);
|
|
|
49 |
try {
|
|
|
50 |
this.copyRemoteToLocal(session, invoicePath, outputStream);
|
|
|
51 |
} catch (Exception e) {
|
| 25162 |
amit.gupta |
52 |
|
| 32724 |
amit.gupta |
53 |
}
|
|
|
54 |
Document document = new Document();
|
|
|
55 |
document.setMargins(0, 0, 25, 0);
|
|
|
56 |
PdfWriter.getInstance(document, outputStream);
|
|
|
57 |
document.open();
|
|
|
58 |
}
|
| 25162 |
amit.gupta |
59 |
|
| 32724 |
amit.gupta |
60 |
private Session createSession(String user, String host, int port, String password) {
|
|
|
61 |
try {
|
|
|
62 |
JSch jsch = new JSch();
|
| 25162 |
amit.gupta |
63 |
|
| 32724 |
amit.gupta |
64 |
Properties config = new java.util.Properties();
|
|
|
65 |
config.put("StrictHostKeyChecking", "no");
|
| 25162 |
amit.gupta |
66 |
|
| 32724 |
amit.gupta |
67 |
Session session = jsch.getSession(user, host, port);
|
|
|
68 |
session.setPassword(passowrd);
|
|
|
69 |
session.setConfig(config);
|
|
|
70 |
session.connect();
|
| 25162 |
amit.gupta |
71 |
|
| 32724 |
amit.gupta |
72 |
return session;
|
|
|
73 |
} catch (JSchException e) {
|
|
|
74 |
System.out.println(e);
|
|
|
75 |
return null;
|
|
|
76 |
}
|
|
|
77 |
}
|
|
|
78 |
|
| 25162 |
amit.gupta |
79 |
private void copyRemoteToLocal(Session session, String filePath, OutputStream outputStream)
|
|
|
80 |
throws JSchException, IOException, SftpException {
|
|
|
81 |
ChannelSftp sftp = (ChannelSftp) session.openChannel("sftp");
|
|
|
82 |
// Once you've opened an sftp channel, there are methods to read a remote file
|
|
|
83 |
// which let you access the file's content as an InputStream. You can convert
|
|
|
84 |
// that to a Reader if you need to read line-by-line:
|
| 30192 |
amit.gupta |
85 |
/*Vector fileVector = sftp.l;
|
|
|
86 |
fileVector.stream().forEach(x -> System.out.println(((LsEntry) x).getFilename()));*/
|
| 25162 |
amit.gupta |
87 |
// InputStream stream = sftp.get("/some/file");
|
|
|
88 |
/*
|
|
|
89 |
* try { BufferedReader br = new BufferedReader(new InputStreamReader(stream));
|
|
|
90 |
* // read from br } finally { stream.close(); }
|
|
|
91 |
*/
|
|
|
92 |
}
|
|
|
93 |
|
| 32724 |
amit.gupta |
94 |
public static int checkAck(InputStream in) throws IOException {
|
|
|
95 |
int b = in.read();
|
|
|
96 |
// b may be 0 for success,
|
|
|
97 |
// 1 for error,
|
|
|
98 |
// 2 for fatal error,
|
|
|
99 |
// -1
|
|
|
100 |
if (b == 0)
|
|
|
101 |
return b;
|
|
|
102 |
if (b == -1)
|
|
|
103 |
return b;
|
| 25162 |
amit.gupta |
104 |
|
| 32724 |
amit.gupta |
105 |
if (b == 1 || b == 2) {
|
|
|
106 |
StringBuffer sb = new StringBuffer();
|
|
|
107 |
int c;
|
|
|
108 |
do {
|
|
|
109 |
c = in.read();
|
|
|
110 |
sb.append((char) c);
|
|
|
111 |
} while (c != '\n');
|
|
|
112 |
if (b == 1) { // error
|
|
|
113 |
System.out.print(sb.toString());
|
|
|
114 |
}
|
|
|
115 |
if (b == 2) { // fatal error
|
|
|
116 |
System.out.print(sb.toString());
|
|
|
117 |
}
|
|
|
118 |
}
|
|
|
119 |
return b;
|
|
|
120 |
}
|
| 25162 |
amit.gupta |
121 |
|
|
|
122 |
}
|