BrightSide Workbench Full Report + Source Code
CartItem.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2014 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.financials.cart;
20 
21 import java.io.Serializable;
22 import org.turro.financials.db.FinancialsPU;
23 import org.turro.financials.entity.Product;
24 import org.turro.math.Round;
25 import org.turro.util.CompareUtil;
26 
31 public class CartItem implements Serializable, Comparable<CartItem> {
32 
33  private int order;
34  private long productId;
35  private double quantity, tax, price;
36  private boolean needsDelivery, deliveryItem, needsConcept;
37  private String concept;
38 
39  public int getOrder() {
40  return order;
41  }
42 
43  public void setOrder(int order) {
44  this.order = order;
45  }
46 
47  public long getProductId() {
48  return productId;
49  }
50 
51  public void setProductId(long productId) {
52  this.productId = productId;
53  }
54 
55  public double getQuantity() {
56  return quantity;
57  }
58 
59  public void setQuantity(double quantity) {
60  this.quantity = Math.round(quantity);
61  }
62 
63  public double getTax() {
64  return tax;
65  }
66 
67  public void setTax(double tax) {
68  this.tax = tax;
69  }
70 
71  public double getPrice() {
72  return price;
73  }
74 
75  public void setPrice(double price) {
76  this.price = price;
77  }
78 
79  public String getConcept() {
80  return concept;
81  }
82 
83  public void setConcept(String concept) {
84  this.concept = concept;
85  }
86 
87  public boolean isNeedsDelivery() {
88  return needsDelivery;
89  }
90 
91  public boolean isDeliveryItem() {
92  return deliveryItem;
93  }
94 
95  public void setDeliveryItem(boolean deliveryItem) {
96  this.deliveryItem = deliveryItem;
97  }
98 
99  public boolean isNeedsConcept() {
100  return needsConcept;
101  }
102 
103  /* Helpers */
104 
105  public Double getTaxable() {
106  return new Round(price * quantity).decimals(2).value();
107  }
108 
109  public double getAmount() {
110  return new Round((getTaxable() * (1 + tax / 100))).decimals(2).value();
111  }
112 
113  public double getTaxAmount() {
114  return getAmount() - getTaxable();
115  }
116 
117  public boolean isValid() {
118  return getProduct() != null &&
119  quantity != 0.0;
120  }
121 
122  public void makeValid() {
123  if(getProduct() != null) {
124  if(quantity == 0.0) {
125  quantity = 1.0;
126  }
127  if(tax == 0.0) {
128  tax = getProduct().getTax();
129  }
130  if(price == 0.0) {
131  price = getProduct().getPrice();
132  }
133  needsDelivery = getProduct().isDelivery();
134  needsConcept = getProduct().isConcept();
135  }
136  }
137 
138  private transient Product product;
139 
140  public Product getProduct() {
141  if(productId != 0) {
142  if(product == null || productId != product.getId()) {
143  product = new FinancialsPU().find(Product.class, productId);
144  }
145  } else {
146  product = null;
147  }
148  return product;
149  }
150 
151  /* Interaction */
152 
153  @Override
154  public int compareTo(CartItem o) {
155  int result = CompareUtil.compare(getOrder(), o.getOrder());
156  if(result == 0) {
157  result = CompareUtil.compare(isValid(), o.isValid());
158  }
159  if(result == 0) {
160  result = CompareUtil.compare(getProduct().getId(), o.getProduct().getId());
161  }
162  return result;
163  }
164 
165 }
void setProductId(long productId)
Definition: CartItem.java:51
void setQuantity(double quantity)
Definition: CartItem.java:59
void setConcept(String concept)
Definition: CartItem.java:83
void setDeliveryItem(boolean deliveryItem)
Definition: CartItem.java:95
Round decimals(int digits)
Definition: Round.java:32