View Javadoc
1   /*
2    * Copyright (c) Patrick Magauran 2018.
3    *   Licensed under the AGPLv3. All conditions of said license apply.
4    *       This file is part of ABOS.
5    *
6    *       ABOS is free software: you can redistribute it and/or modify
7    *       it under the terms of the GNU Affero General Public License as published by
8    *       the Free Software Foundation, either version 3 of the License, or
9    *       (at your option) any later version.
10   *
11   *       ABOS is distributed in the hope that it will be useful,
12   *       but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   *       GNU Affero General Public License for more details.
15   *
16   *       You should have received a copy of the GNU Affero General Public License
17   *       along with ABOS.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package Controllers;
21  
22  import Exceptions.CustomerNotFoundException;
23  import Utilities.*;
24  import javafx.collections.FXCollections;
25  import javafx.collections.ObservableList;
26  import javafx.event.ActionEvent;
27  import javafx.fxml.FXML;
28  import javafx.scene.control.TableColumn;
29  import javafx.scene.control.TableView;
30  import javafx.scene.control.cell.PropertyValueFactory;
31  import javafx.scene.layout.VBox;
32  
33  import java.math.BigDecimal;
34  import java.util.ArrayList;
35  import java.util.List;
36  
37  //import javax.swing.*;
38  //import javax.swing.table.DefaultTableModel;
39  @SuppressWarnings("WeakerAccess")
40  
41  public class CustomerController {
42      public static String year = "2017";
43      //public String year;
44      private String name;
45      private int cID;
46  
47      private Customer customerDbInfo;
48      @FXML
49      private VBox customerInfo;
50      @FXML
51      private TableView customerOrders;
52      private String totQuant = "0";
53      private String totCost = "$0.00";
54      private Boolean columnsFilled = false;
55      private ObservableList<formattedProductProps> data;
56      private MainController mainController;
57  
58      public void initCustomer(String cYear, String cName, MainController mainCont) {
59          year = cYear;
60          name = cName;
61          mainController = mainCont;
62          customerDbInfo = new Customer(name, year);
63          cID = customerDbInfo.getId();
64          fillTable();
65  
66          //frame.setTitle("ABOS - Utilities.Customer View - " + name + " - " + year);
67          List<infoValPair> customerInfoStrings = new ArrayList<>();
68          customerInfoStrings.add(new infoValPair("Name", name));
69          customerInfoStrings.add(new infoValPair("Address", customerDbInfo.getAddr()));
70          customerInfoStrings.add(new infoValPair("Phone #", customerDbInfo.getPhone()));
71          customerInfoStrings.add(new infoValPair("Email", customerDbInfo.getEmail()));
72          customerInfoStrings.add(new infoValPair("Total Quantity", totQuant));
73          customerInfoStrings.add(new infoValPair("Donations", customerDbInfo.getDontation().toPlainString()));
74  
75          customerInfoStrings.add(new infoValPair("Total Cost", totCost));
76          customerInfoStrings.add(new infoValPair("Paid", customerDbInfo.getPaid().toPlainString()));
77          customerInfoStrings.add(new infoValPair("Delivered", customerDbInfo.getDelivered() ? "yes" : "no"));
78  
79  
80          customerInfoStrings.forEach((pair) -> {
81              javafx.scene.control.Label keyLabel = new javafx.scene.control.Label(pair.info + ":");
82              javafx.scene.control.Label valLabel = new javafx.scene.control.Label(pair.value);
83              keyLabel.setId("CustomerDescription");
84              valLabel.setId("CustomerValue");
85              VBox info = new VBox(keyLabel, valLabel);
86              info.getStyleClass().add("informationPane");
87              customerInfo.getChildren().add(info);
88          });
89  
90  
91      }
92  
93      public void initCustomer(Customer customer, MainController mainCont) {
94          customerDbInfo = customer;
95  
96          year = customer.getYear();
97          name = customer.getName();
98          mainController = mainCont;
99          cID = customerDbInfo.getId();
100         fillTable();
101 
102         //frame.setTitle("ABOS - Utilities.Customer View - " + name + " - " + year);
103 
104         List<infoValPair> customerInfoStrings = new ArrayList<>();
105         customerInfoStrings.add(new infoValPair("Name", name));
106         customerInfoStrings.add(new infoValPair("Address", customerDbInfo.getAddr()));
107         customerInfoStrings.add(new infoValPair("Phone #", customerDbInfo.getPhone()));
108         customerInfoStrings.add(new infoValPair("Email", customerDbInfo.getEmail()));
109 
110         //customerInfoStrings.add(new infoValPair("Paid", customerDbInfo.getPaid()));
111         //customerInfoStrings.add(new infoValPair("Delivered", customerDbInfo.getDelivered()));
112         customerInfoStrings.add(new infoValPair("Total Quantity", totQuant));
113         customerInfoStrings.add(new infoValPair("Donations", customerDbInfo.getDontation().toPlainString()));
114 
115         customerInfoStrings.add(new infoValPair("Total Cost", totCost));
116         customerInfoStrings.add(new infoValPair("Paid", customerDbInfo.getPaid().toPlainString()));
117         customerInfoStrings.add(new infoValPair("Delivered", customerDbInfo.getDelivered() ? "yes" : "no"));
118 
119 
120         customerInfoStrings.forEach((pair) -> {
121             javafx.scene.control.Label keyLabel = new javafx.scene.control.Label(pair.info + ":");
122             javafx.scene.control.Label valLabel = new javafx.scene.control.Label(pair.value);
123             keyLabel.setId("CustomerDescription");
124             valLabel.setId("CustomerValue");
125             VBox info = new VBox(keyLabel, valLabel);
126             info.getStyleClass().add("informationPane");
127             customerInfo.getChildren().add(info);
128         });
129 
130 
131     }
132 
133     /**
134      * Initialize the contents of the frame.
135      */
136     private void initialize() {
137         try {
138             customerDbInfo = new Customer(cID, year);
139         } catch (CustomerNotFoundException e) {
140             LogToFile.log(e, Severity.WARNING, "Customer could not be found. Please try restarting the application.");
141         }
142         name = customerDbInfo.getName();
143         //frame.setTitle("ABOS - Utilities.Customer View - " + name + " - " + year);
144         fillTable();
145 
146         List<infoValPair> customerInfoStrings = new ArrayList<>();
147         customerInfoStrings.add(new infoValPair("Name", name));
148         customerInfoStrings.add(new infoValPair("Address", customerDbInfo.getAddr()));
149         customerInfoStrings.add(new infoValPair("Phone #", customerDbInfo.getPhone()));
150         customerInfoStrings.add(new infoValPair("Email", customerDbInfo.getEmail()));
151         //customerInfoStrings.add(new infoValPair("Paid", customerDbInfo.getPaid()));
152         //customerInfoStrings.add(new infoValPair("Delivered", customerDbInfo.getDelivered()));
153         customerInfoStrings.add(new infoValPair("Total Quantity", totQuant));
154         customerInfoStrings.add(new infoValPair("Total Cost", totCost));
155 
156         customerInfo.getChildren().remove(0, 6);
157         customerInfoStrings.forEach((pair) -> {
158             javafx.scene.control.Label keyLabel = new javafx.scene.control.Label(pair.info + ":");
159             javafx.scene.control.Label valLabel = new javafx.scene.control.Label(pair.value);
160             keyLabel.setId("CustomerDescription");
161             valLabel.setId("CustomerValue");
162             customerInfo.getChildren().add(new VBox(keyLabel, valLabel));
163         });
164 
165     }
166 
167     @FXML
168     public void editCustomer(ActionEvent event) {
169         mainController.openEditCustomer(customerDbInfo);
170     }
171 
172     @FXML
173     public void refresh(ActionEvent event) {
174         initialize();
175     }
176 
177     private void fillTable() {
178         Order.orderArray order;
179         order = Order.createOrderArray(year, cID, true);
180         data = FXCollections.observableArrayList();
181 
182         int i = 0;
183         for (formattedProduct productOrder : order.orderData) {
184             //String productID, String productName, String productSize, String productUnitPrice, String productCategory, int orderedQuantity, BigDecimal extendedCost
185             formattedProductProps prodProps = new formattedProductProps(productOrder.productKey, productOrder.productID, productOrder.productName, productOrder.productSize, productOrder.productUnitPrice, productOrder.productCategory, productOrder.orderedQuantity, productOrder.extendedCost);
186             data.add(prodProps);
187             i++;
188         }
189         if (!columnsFilled) {
190             String[][] columnNames = {{"Item", "productName"}, {"Size", "productSize"}, {"Price/Item", "productUnitPrice"}, {"Quantity", "orderedQuantity"}, {"Price", "extendedCost"}};
191             for (String[] column : columnNames) {
192                 TableColumn<formattedProductProps, String> tbCol = new TableColumn<>(column[0]);
193                 tbCol.setCellValueFactory(new PropertyValueFactory<>(column[1]));
194                 customerOrders.getColumns().add(tbCol);
195             }
196         }
197         columnsFilled = true;
198 
199 
200         customerOrders.setItems(data);
201 
202         //final Object[] columnNames = {"Product Name", "Size", "Price/Item", "Quantity", "Total Cost"};
203 
204 
205         totQuant = (Integer.toString(order.totalQuantity));
206         totCost = ("$" + order.totalCost.toPlainString());
207         customerOrders.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
208 
209     }
210 
211     @FXML
212     private void deleteCustomer(ActionEvent event) {
213         customerDbInfo.deleteCustomer();
214         mainController.fillTreeView();
215 
216 
217     }
218 
219     /**
220      * Loops through Table to get total amount of Bulk Mulch ordered.
221      *
222      * @return The amount of Bulk mulch ordered
223      */
224     private int getNoMulchOrdered() {
225         int quantMulchOrdered = 0;
226         for (formattedProductProps aData : data) {
227             if ((aData.getProductName().contains("Mulch")) && (aData.getProductName().contains("Bulk"))) {
228                 quantMulchOrdered += aData.getOrderedQuantity();
229             }
230         }
231         return quantMulchOrdered;
232     }
233 
234     /**
235      * Loops through Table to get total amount of Lawn and Garden Products ordered.
236      *
237      * @return The amount of Lawn and Garden Products ordered
238      */
239     private int getNoLivePlantsOrdered() {
240         int livePlantsOrdered = 0;
241         for (formattedProductProps aData : data) {
242             if (aData.getProductName().contains("-P") || aData.getProductName().contains("-FV")) {
243                 livePlantsOrdered += aData.getOrderedQuantity();
244             }
245         }
246         return livePlantsOrdered;
247     }
248 
249     /**
250      * Loops through Table to get total amount of Lawn Products ordered.
251      *
252      * @return The amount of Live Plants ordered
253      */
254     private int getNoLawnProductsOrdered() {
255         int lawnProductsOrdered = 0;
256         for (formattedProductProps aData : data) {
257             if (aData.getProductName().contains("-L")) {
258                 lawnProductsOrdered += aData.getOrderedQuantity();
259             }
260         }
261         return lawnProductsOrdered;
262     }
263 
264     /**
265      * Calculates the amount of commission to be earned.
266      *
267      * @param totalCost the Sub total for all orders
268      * @return Commission to be earned
269      */
270     private BigDecimal getCommission(BigDecimal totalCost) {
271         BigDecimal commision = BigDecimal.ZERO;
272         if ((totalCost.compareTo(new BigDecimal("299.99")) > 0) && (totalCost.compareTo(new BigDecimal("500.01")) < 0)) {
273             commision = totalCost.multiply(new BigDecimal("0.05"));
274         } else if ((totalCost.compareTo(new BigDecimal("500.01")) > 0) && (totalCost.compareTo(new BigDecimal("1000.99")) < 0)) {
275             commision = totalCost.multiply(new BigDecimal("0.1"));
276         } else if (totalCost.compareTo(new BigDecimal("1000")) >= 0) {
277             commision = totalCost.multiply(new BigDecimal("0.15"));
278         }
279         return commision;
280     }
281 
282     // --Commented out by Inspection START (1/2/2016 12:01 PM):
283 //    public void setTable(JTable customerOrders) {
284 //        this.customerOrders = customerOrders;
285 //    }
286 // --Commented out by Inspection STOP (1/2/2016 12:01 PM)
287     private class infoValPair {
288         public String info;
289         public String value;
290 
291         public infoValPair(String inf, String val) {
292             this.info = inf;
293             this.value = val;
294         }
295     }
296 
297 }