| Line 1... |
Line 1... |
| 1 |
package com.spice.profitmandi.service;
|
1 |
package com.spice.profitmandi.service;
|
| 2 |
|
2 |
|
| - |
|
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 |
|
| 3 |
import java.io.IOException;
|
14 |
import java.io.IOException;
|
| 4 |
import java.io.InputStream;
|
15 |
import java.io.InputStream;
|
| 5 |
import java.io.OutputStream;
|
16 |
import java.io.OutputStream;
|
| 6 |
import java.time.LocalDateTime;
|
17 |
import java.time.LocalDateTime;
|
| - |
|
18 |
import java.util.List;
|
| 7 |
import java.util.Properties;
|
19 |
import java.util.Properties;
|
| 8 |
import java.util.Vector;
|
- |
|
| 9 |
|
- |
|
| 10 |
import org.springframework.beans.factory.annotation.Autowired;
|
- |
|
| 11 |
import org.springframework.stereotype.Service;
|
- |
|
| 12 |
|
- |
|
| 13 |
import com.itextpdf.text.Document;
|
- |
|
| 14 |
import com.itextpdf.text.pdf.PdfWriter;
|
- |
|
| 15 |
import com.jcraft.jsch.ChannelSftp;
|
- |
|
| 16 |
import com.jcraft.jsch.ChannelSftp.LsEntry;
|
- |
|
| 17 |
import com.jcraft.jsch.JSch;
|
- |
|
| 18 |
import com.jcraft.jsch.JSchException;
|
- |
|
| 19 |
import com.jcraft.jsch.Session;
|
- |
|
| 20 |
import com.jcraft.jsch.SftpException;
|
- |
|
| 21 |
import com.spice.profitmandi.dao.repository.fofo.PurchaseRepository;
|
- |
|
| 22 |
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
|
- |
|
| 23 |
|
20 |
|
| 24 |
@Service
|
21 |
@Service
|
| 25 |
public class PurchaseInvoiceService {
|
22 |
public class PurchaseInvoiceService {
|
| 26 |
|
23 |
|
| 27 |
@Autowired
|
24 |
@Autowired
|
| 28 |
PurchaseRepository purchaseRepository;
|
25 |
PurchaseRepository purchaseRepository;
|
| 29 |
|
26 |
|
| 30 |
@Autowired
|
27 |
@Autowired
|
| 31 |
OrderRepository orderRepository;
|
28 |
OrderRepository orderRepository;
|
| 32 |
|
29 |
|
| - |
|
30 |
@Autowired
|
| - |
|
31 |
RestClient restClient;
|
| - |
|
32 |
|
| 33 |
//private static final String remoteHost = "192.168.149.148";
|
33 |
//private static final String remoteHost = "192.168.149.148";
|
| 34 |
private static final String remoteHost = "support.shop2020.in";
|
34 |
private static final String remoteHost = "support.shop2020.in";
|
| 35 |
private static final String invoicepath = "/SaholicInvoices/";
|
35 |
private static final String invoicepath = "/SaholicInvoices/";
|
| 36 |
|
36 |
|
| 37 |
private static final String passowrd = "spic@2015support";
|
37 |
private static final String passowrd = "spic@2015support";
|
| 38 |
|
38 |
|
| 39 |
public void getInvoices(int fofoId, LocalDateTime startDate, LocalDateTime endDate, OutputStream outputStream)
|
39 |
public void getInvoices(String fofoId, String invoiceNumber, OutputStream outputStream)
|
| 40 |
throws Exception {
|
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();
|
| 41 |
String invoicePath = String.format("%s%d-%d/%d", invoicepath, startDate.getYear(), startDate.getMonth().getValue() - 1,
|
46 |
String invoicePath = String.format("%s%d-%d/%d/%s.pdf", invoicepath, billingDate.getYear(), billingDate.getMonth().getValue() - 1,
|
| 42 |
fofoId);
|
47 |
fofoId, invoiceNumber);
|
| 43 |
Session session = createSession("root", remoteHost, 22, passowrd);
|
48 |
Session session = createSession("root", remoteHost, 22, passowrd);
|
| 44 |
this.copyRemoteToLocal(session, invoicePath, outputStream);
|
49 |
this.copyRemoteToLocal(session, invoicePath, outputStream);
|
| 45 |
Document document = new Document();
|
50 |
Document document = new Document();
|
| 46 |
document.setMargins(0, 0, 25, 0);
|
51 |
document.setMargins(0, 0, 25, 0);
|
| 47 |
PdfWriter.getInstance(document, outputStream);
|
52 |
PdfWriter.getInstance(document, outputStream);
|
| Line 71... |
Line 76... |
| 71 |
throws JSchException, IOException, SftpException {
|
76 |
throws JSchException, IOException, SftpException {
|
| 72 |
ChannelSftp sftp = (ChannelSftp) session.openChannel("sftp");
|
77 |
ChannelSftp sftp = (ChannelSftp) session.openChannel("sftp");
|
| 73 |
// Once you've opened an sftp channel, there are methods to read a remote file
|
78 |
// Once you've opened an sftp channel, there are methods to read a remote file
|
| 74 |
// which let you access the file's content as an InputStream. You can convert
|
79 |
// which let you access the file's content as an InputStream. You can convert
|
| 75 |
// that to a Reader if you need to read line-by-line:
|
80 |
// that to a Reader if you need to read line-by-line:
|
| 76 |
Vector fileVector = sftp.ls(filePath);
|
81 |
/*Vector fileVector = sftp.l;
|
| 77 |
fileVector.stream().forEach(x -> System.out.println(((LsEntry) x).getFilename()));
|
82 |
fileVector.stream().forEach(x -> System.out.println(((LsEntry) x).getFilename()));*/
|
| 78 |
// InputStream stream = sftp.get("/some/file");
|
83 |
// InputStream stream = sftp.get("/some/file");
|
| 79 |
/*
|
84 |
/*
|
| 80 |
* try { BufferedReader br = new BufferedReader(new InputStreamReader(stream));
|
85 |
* try { BufferedReader br = new BufferedReader(new InputStreamReader(stream));
|
| 81 |
* // read from br } finally { stream.close(); }
|
86 |
* // read from br } finally { stream.close(); }
|
| 82 |
*/
|
87 |
*/
|