Skip to content

Commit

Permalink
Merge pull request #27 from Nikola-Mircic/gui
Browse files Browse the repository at this point in the history
Gui
  • Loading branch information
Nikola-Mircic authored Mar 13, 2024
2 parents 3162639 + 28ae2b5 commit 2c89fea
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<hr>

### TO-DO list:
- [ ] - Ploting both sales and purchases
- [x] - Ploting both sales and purchases
- [ ] - Payment description
- [ ] - Password check
- [ ] - Form validation
Expand Down
24 changes: 18 additions & 6 deletions src/efakturaplus/gui/MainPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,15 @@ private JLabel centeredLabel(StretchIcon ico) {
}

public void printPurchaseInvoices() {
InvoiceStatus[] pStatusArr = {InvoiceStatus.ReNotified, InvoiceStatus.New, InvoiceStatus.Approved, InvoiceStatus.Reminded, InvoiceStatus.Seen, InvoiceStatus.Rejected};

new Thread(()->{
InvoiceStatus[] pStatusArr = {InvoiceStatus.ReNotified, InvoiceStatus.New, InvoiceStatus.Approved, InvoiceStatus.Reminded, InvoiceStatus.Seen, InvoiceStatus.Rejected};

displayInvoicesByStatus(InvoiceType.PURCHASE, pStatusArr);
}).start();

InvoiceStatus[] sStatusArr = {InvoiceStatus.ReNotified, InvoiceStatus.New, InvoiceStatus.Seen, InvoiceStatus.Approved};

new Thread(()->{
InvoiceStatus[] sStatusArr = {InvoiceStatus.ReNotified, InvoiceStatus.New, InvoiceStatus.Seen, InvoiceStatus.Approved};

displayInvoicesByStatus(InvoiceType.SALES, sStatusArr);
}).start();
}
Expand All @@ -193,6 +193,7 @@ private void displayInvoicesByStatus(InvoiceType type, InvoiceStatus[] statusArr
LocalDate from = LocalDate.now().minusMonths(3);

for (int i=0;i<3;++i) {

for(InvoiceStatus status : statusArr) {
ArrayList<Invoice> invoices = efu.getInvoices(type, status, from, from.plusMonths(1));
Collections.reverse(invoices);
Expand All @@ -201,14 +202,25 @@ private void displayInvoicesByStatus(InvoiceType type, InvoiceStatus[] statusArr
if(type == InvoiceType.PURCHASE) {
purchaseIl.addInvoice(element);
statsPanel.addInvoice(element);
}else
}else {
salesIl.addInvoice(element);
}
statsPanel.addInvoice(element);
}
}
}

dataPanel.revalidate();

System.out.println(from.toString());

from = from.plusMonths(1);

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


Expand Down
34 changes: 17 additions & 17 deletions src/efakturaplus/gui/StatisticsPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@

import ch.randelshofer.util.ArrayUtil;
import efakturaplus.models.Invoice;
import efakturaplus.models.InvoiceType;
import efakturaplus.util.Pair;

public class StatisticsPanel extends JPanel {

private static final int PREF_W = 800;
private static final int PREF_H = 650;
private static final int BORDER_GAP = 30;
private static final Color GRAPH_COLOR = new Color(0.0f, 1.0f, 0.1f, 0.2f);
private static final Color GRAPH_POINT_COLOR = new Color(150, 50, 50, 180);
private static final Stroke GRAPH_STROKE = new BasicStroke(3f);
private static final int GRAPH_POINT_WIDTH = 5;
Expand All @@ -36,7 +36,7 @@ public class StatisticsPanel extends JPanel {
public StatisticsPanel() {
this.invoices = new ArrayList<Invoice>();

this.plot = new Plot(new ArrayList<Date>(), new ArrayList<Double>());
this.plot = new Plot();
}

@Override
Expand Down Expand Up @@ -78,16 +78,18 @@ protected void paintComponent(Graphics g) {
int y1 = y0 - GRAPH_POINT_WIDTH;
g2.drawLine(x0, y0, x1, y1);
}



// Pillars
Stroke oldStroke = g2.getStroke();
g2.setColor(GRAPH_COLOR);
g2.setStroke(GRAPH_STROKE);
for (int i = 0; i < graphPoints.size() - 1; i++) {
int x1 = graphPoints.get(i).x - 5;
int y1 = graphPoints.get(i).y;
int x2 = graphPoints.get(i).x + 5;
int y2 = getHeight() - BORDER_GAP;

g2.setColor(plot.colors.get(i));
g2.fillRect(x1, y1, 10, y2-y1);
}

Expand Down Expand Up @@ -120,14 +122,20 @@ public void updatePlot() {
}

class Plot {
private final Color PURCHASE_COLOR = new Color(0.0f, 1.0f, 0.1f, 0.2f);
private final Color SALE_COLOR = new Color(1.0f, 0.0f, 0.1f, 0.2f);

public ArrayList<Date> dates;
public ArrayList<Double> values;
public ArrayList<Color> colors;

public ArrayList<Pair<Double, Double>> points;

public Plot(ArrayList<Date> dates, ArrayList<Double> values) {
this.dates = dates;
this.values = values;
public Plot() {
this.dates = new ArrayList<Date>();
this.values = new ArrayList<Double>();

this.colors = new ArrayList<Color>();

makePoints();
}
Expand All @@ -143,10 +151,12 @@ public int compare(Invoice o1, Invoice o2) {

this.dates = new ArrayList<Date>();
this.values = new ArrayList<Double>();
this.colors = new ArrayList<Color>();

for(Invoice inv : invoices) {
this.dates.add(inv.deliveryDate);
this.values.add(inv.payableAmount);
this.colors.add((inv.type.equals(InvoiceType.PURCHASE)) ? PURCHASE_COLOR : SALE_COLOR);
}
}

Expand All @@ -162,10 +172,6 @@ public void makePoints() {

int n = dates.size();

/*for(int i=1; i<n; ++i) {
values.set(i, values.get(i-1) + values.get(i));
}*/

System.out.println(values.toString());

double maxValue = Collections.max(values);
Expand All @@ -183,11 +189,5 @@ public void makePoints() {
points.add(new Pair<Double, Double>(1.0 * diff / dateDiff, values.get(i) / maxValue));
}

points.sort(new Comparator<Pair<Double, Double>>() {
@Override
public int compare(Pair<Double, Double> o1, Pair<Double, Double> o2) {
return (int) (o1.first*1000 - o2.first*1000);
}
});
}
}
11 changes: 7 additions & 4 deletions src/efakturaplus/util/EFakturaUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

public class EFakturaUtil {
//Singleton instance
private static EFakturaUtil instance = null;

private String API_KEY = "";

Expand All @@ -45,13 +44,16 @@ private EFakturaUtil(String API_KEY) {
this.loadedIds = new HashSet<String>();
}

public synchronized static EFakturaUtil getInstance() {
public static EFakturaUtil getInstance() {
return new EFakturaUtil(User.API_KEY);
}

private ArrayList<String> getIdsFromResponse(InvoiceType type, HttpResponse<String> response){
ArrayList<String> ids = new ArrayList<>();


System.out.println("[ " + type + " ] response for IDs : ");
System.out.println(response.body());

JSONObject object = new JSONObject(response.body());
JSONArray invoiceIds;

Expand All @@ -71,10 +73,11 @@ private ArrayList<String> getIdsFromResponse(InvoiceType type, HttpResponse<Stri
return ids;
}

private HttpResponse<String> sendRequest(HttpRequest request) {
private synchronized HttpResponse<String> sendRequest(HttpRequest request) {
HttpClient client = HttpClient.newHttpClient();
try {
// GetInvoiceRequest
Thread.sleep(500);
HttpResponse<String> res = client.send(request, HttpResponse.BodyHandlers.ofString());

return res;
Expand Down

0 comments on commit 2c89fea

Please sign in to comment.