Subversion Repositories SmartDukaan

Rev

Rev 30192 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 30192 Rev 32724
Line 19... Line 19...
19
import java.util.Properties;
19
import java.util.Properties;
20
 
20
 
21
@Service
21
@Service
22
public class PurchaseInvoiceService {
22
public class PurchaseInvoiceService {
23
 
23
 
24
	@Autowired
24
    @Autowired
25
	PurchaseRepository purchaseRepository;
25
    PurchaseRepository purchaseRepository;
26
 
26
 
27
	@Autowired
27
    @Autowired
28
	OrderRepository orderRepository;
28
    OrderRepository orderRepository;
29
 
29
 
30
	@Autowired
30
    @Autowired
31
	RestClient restClient;
31
    RestClient restClient;
32
 
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(String fofoId, String invoiceNumber, 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);
41
        List<Order> orders = orderRepository.selectByInvoiceNumber(invoiceNumber);
42
		if (orders.size() == 0) {
42
        if (orders.size() == 0) {
43
			throw new ProfitMandiBusinessException("Invalid invoice Number", invoiceNumber, "Invalid invoice number");
43
            throw new ProfitMandiBusinessException("Invalid invoice Number", invoiceNumber, "Invalid invoice number");
44
		}
44
        }
45
		LocalDateTime billingDate = orders.get(0).getBillingTimestamp();
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,
46
        String invoicePath = String.format("%s%d-%d/%d/%s.pdf", invoicepath, billingDate.getYear(), billingDate.getMonth().getValue() - 1,
47
				fofoId, invoiceNumber);
47
                fofoId, invoiceNumber);
48
		Session session = createSession("root", remoteHost, 22, passowrd);
48
        Session session = createSession("root", remoteHost, 22, passowrd);
-
 
49
        try {
49
		this.copyRemoteToLocal(session, invoicePath, outputStream);
50
            this.copyRemoteToLocal(session, invoicePath, outputStream);
-
 
51
        } catch (Exception e) {
-
 
52
 
-
 
53
        }
50
		Document document = new Document();
54
        Document document = new Document();
51
		document.setMargins(0, 0, 25, 0);
55
        document.setMargins(0, 0, 25, 0);
52
		PdfWriter.getInstance(document, outputStream);
56
        PdfWriter.getInstance(document, outputStream);
53
		document.open();
57
        document.open();
54
	}
58
    }
55
 
59
 
56
	private Session createSession(String user, String host, int port, String password) {
60
    private Session createSession(String user, String host, int port, String password) {
57
		try {
61
        try {
58
			JSch jsch = new JSch();
62
            JSch jsch = new JSch();
59
 
63
 
60
			Properties config = new java.util.Properties();
64
            Properties config = new java.util.Properties();
61
			config.put("StrictHostKeyChecking", "no");
65
            config.put("StrictHostKeyChecking", "no");
62
 
66
 
63
			Session session = jsch.getSession(user, host, port);
67
            Session session = jsch.getSession(user, host, port);
64
			session.setPassword(passowrd);
68
            session.setPassword(passowrd);
65
			session.setConfig(config);
69
            session.setConfig(config);
66
			session.connect();
70
            session.connect();
67
 
71
 
68
			return session;
72
            return session;
69
		} catch (JSchException e) {
73
        } catch (JSchException e) {
70
			System.out.println(e);
74
            System.out.println(e);
71
			return null;
75
            return null;
72
		}
76
        }
73
	}
77
    }
74
 
78
 
75
	private void copyRemoteToLocal(Session session, String filePath, OutputStream outputStream)
79
	private void copyRemoteToLocal(Session session, String filePath, OutputStream outputStream)
76
			throws JSchException, IOException, SftpException {
80
			throws JSchException, IOException, SftpException {
77
		ChannelSftp sftp = (ChannelSftp) session.openChannel("sftp");
81
		ChannelSftp sftp = (ChannelSftp) session.openChannel("sftp");
78
		// Once you've opened an sftp channel, there are methods to read a remote file
82
		// Once you've opened an sftp channel, there are methods to read a remote file
Line 85... Line 89...
85
		 * try { BufferedReader br = new BufferedReader(new InputStreamReader(stream));
89
		 * try { BufferedReader br = new BufferedReader(new InputStreamReader(stream));
86
		 * // read from br } finally { stream.close(); }
90
		 * // read from br } finally { stream.close(); }
87
		 */
91
		 */
88
	}
92
	}
89
 
93
 
90
	public static int checkAck(InputStream in) throws IOException {
94
    public static int checkAck(InputStream in) throws IOException {
91
		int b = in.read();
95
        int b = in.read();
92
		// b may be 0 for success,
96
        // b may be 0 for success,
93
		// 1 for error,
97
        // 1 for error,
94
		// 2 for fatal error,
98
        // 2 for fatal error,
95
		// -1
99
        // -1
96
		if (b == 0)
100
        if (b == 0)
97
			return b;
101
            return b;
98
		if (b == -1)
102
        if (b == -1)
99
			return b;
103
            return b;
100
 
104
 
101
		if (b == 1 || b == 2) {
105
        if (b == 1 || b == 2) {
102
			StringBuffer sb = new StringBuffer();
106
            StringBuffer sb = new StringBuffer();
103
			int c;
107
            int c;
104
			do {
108
            do {
105
				c = in.read();
109
                c = in.read();
106
				sb.append((char) c);
110
                sb.append((char) c);
107
			} while (c != '\n');
111
            } while (c != '\n');
108
			if (b == 1) { // error
112
            if (b == 1) { // error
109
				System.out.print(sb.toString());
113
                System.out.print(sb.toString());
110
			}
114
            }
111
			if (b == 2) { // fatal error
115
            if (b == 2) { // fatal error
112
				System.out.print(sb.toString());
116
                System.out.print(sb.toString());
113
			}
117
            }
114
		}
118
        }
115
		return b;
119
        return b;
116
	}
120
    }
117
 
121
 
118
}
122
}