-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUSPWriter.java
More file actions
65 lines (57 loc) · 2.32 KB
/
USPWriter.java
File metadata and controls
65 lines (57 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.usp.auth;
import com.google.gson.Gson;
import com.openbravo.pos.ticket.TicketInfo;
import com.openbravo.pos.ticket.TicketLineInfo;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.swing.JOptionPane;
/**
*
* @author smbuthia
*/
public class USPWriter {
public void writeUSPReceipt(TicketInfo ticket) {
Users users;
String mobileNumber = JOptionPane.showInputDialog("Input a mobile phone number.");
if (mobileNumber != null && !mobileNumber.equalsIgnoreCase("")) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("unicentaPU");
if (emf.isOpen()) {
EntityManager em = emf.createEntityManager();
Query query = em.createNamedQuery("Users.findByMobile");
query.setParameter("mobile", mobileNumber);
users = (Users) query.getSingleResult();
if (users.getEmailaddress() != null) {
Map map = new HashMap<>();
Gson gson = new Gson();
TicketLineInfo tLine;
String prodName;
String prodPrice;
for (int i = 0; i < ticket.getLinesCount(); i++) {
tLine = ticket.getLine(i);
prodName = tLine.getProductName();
prodPrice = Double.toString(tLine.getPrice());
map.put(prodName, prodPrice);
}
String receiptJsonString = gson.toJson(map);
em.getTransaction().begin();
Transactions transactions = new Transactions();
transactions.setReceipt(receiptJsonString);
transactions.setUserid(users.getEmailaddress());
transactions.setId(ticket.getId());
em.persist(transactions);
em.getTransaction().commit();
}
emf.close();
}
} else {
}
}
}