| 1 6 package com.nightlabs.ipanema.accounting; 7 8 import java.io.Serializable ; 9 import java.util.Collection ; 10 import java.util.Date ; 11 import java.util.HashMap ; 12 import java.util.Iterator ; 13 import java.util.Map ; 14 15 import com.nightlabs.ipanema.accounting.id.InvoiceID; 16 import com.nightlabs.ipanema.security.User; 17 import com.nightlabs.ipanema.trade.LegalEntity; 18 import com.nightlabs.ipanema.trade.Offer; 19 import com.nightlabs.ipanema.trade.OfferItem; 20 import com.nightlabs.ipanema.trade.OfferItemPrice; 21 import com.nightlabs.ipanema.trade.Order; 22 import com.nightlabs.ipanema.trade.OrganisationLegalEntity; 23 import com.nightlabs.ipanema.trade.id.OfferItemID; 24 25 35 public class Invoice implements Serializable  36 { 37 41 private String organisationID; 42 43 46 private long invoiceID; 47 48 49 protected Invoice() {} 50 51 public Invoice(User creator, String _organisationID, long _invoiceID) { 52 this.organisationID = _organisationID; 53 this.invoiceID = _invoiceID; 54 this.createDT = new Date (System.currentTimeMillis()); 55 this.createUser = creator; 56 } 57 58 private String primaryKey; 59 60 63 private OrganisationLegalEntity vendor; 64 65 68 private LegalEntity customer; 69 70 88 private Map itemsToSell = new HashMap (); 89 90 108 private Map itemsToRefund = new HashMap (); 109 110 111 118 public void addOfferItem(OfferItem item) 119 throws InvoiceEditException 120 { 121 Offer itemOffer = item.getOffer(); 122 Order itemOrder = itemOffer.getOrder(); 123 OfferItemID offerItemID = OfferItemID.create(item.getOrganisationID(),item.getOfferID(),item.getProductOrganisationID(),item.getProductID()); 124 if (isFinalized()) 125 throw new InvoiceEditException( 126 InvoiceEditException.REASON_INVOICE_FINALIZED, 127 "Invoice is finalized, can not change any more!", 128 offerItemID 129 ); 130 131 if (!vendor.getPrimaryKey().equals(itemOrder.getVendor().getPrimaryKey()) 132 || 133 !customer.getPrimaryKey().equals(itemOrder.getCustomer().getPrimaryKey()) 134 ) 135 { 136 throw new InvoiceEditException( 137 InvoiceEditException.REASON_ANCHORS_DONT_MATCH, 138 "Vendor and customer are not equal for the OfferItem to add and the invoice, can not add the offerItem!!" 139 ); 140 } 141 142 if (item.getInvoice() != null) { 143 InvoiceID invoiceID = InvoiceID.create(item.getInvoice().getOrganisationID(), item.getInvoice().getInvoiceID()); 144 throw new InvoiceEditException( 145 InvoiceEditException.REASON_OFFERITEM_ALREADY_IN_INVOICE, 146 "OfferItem already in an invoice. OfferItem "+offerItemID+", Invoice "+invoiceID, 147 offerItemID, 148 invoiceID 149 ); 150 } 151 152 if (!item.getOffer().isConfirmed()) { 153 throw new InvoiceEditException( 154 InvoiceEditException.REASON_OFFER_NOT_CONFIRMED, 155 "At least one involved offer is not confirmed!", 156 offerItemID 157 ); 158 } 159 160 if (!getCurrency().getCurrencyID().equals(item.getPrice().getCurrency().getCurrencyID())) 161 throw new InvoiceEditException( 162 InvoiceEditException.REASON_MULTIPLE_CURRENCIES, 163 "Can not add an offerItem with an different currency than this Invoice's one!" 164 ); 165 166 if (item.isRefund()) 167 itemsToRefund.put(item.getPrimaryKey(),item); 168 else 169 itemsToSell.put(item.getPrimaryKey(),item); 170 171 calculateInvoicePrice(); 172 item.setInvoice(this); 173 } 174 175 public void removeOfferItem(OfferItem item) 176 throws InvoiceEditException 177 { 178 if (isFinalized()) 179 throw new InvoiceEditException(InvoiceEditException.REASON_INVOICE_FINALIZED, "Invoice is finalized, can not change any more!"); 180 String itemPK = item.getPrimaryKey(); 181 if (itemsToSell.containsKey(itemPK)) 182 itemsToSell.remove(itemPK); 183 else if (itemsToRefund.containsKey(itemPK)) 184 itemsToRefund.remove(itemPK); 185 186 calculateInvoicePrice(); 187 item.setInvoice(null); 188 } 189 190 191 196 private Date createDT; 197 198 203 private User createUser = null; 204 205 210 private OfferItemPrice price; 211 212 213 protected void calculateInvoicePrice() { 214 price.clearFragments(); 215 price.setAmount(0); 216 217 for (int i = 0; i<2; i++) { 218 Collection items = i == 0 ? itemsToSell.values() : itemsToRefund.values(); 219 for (Iterator it = items.iterator(); it.hasNext(); ) { 220 OfferItem offerItem = (OfferItem)it.next(); 221 223 price.sumPrice(offerItem.getPrice()); 239 } 240 } 241 } 242 243 248 private long amountPaid; 249 250 257 private boolean outstanding = true; 258 259 264 private boolean paid = false; 265 266 272 private boolean booked = false; 273 274 279 private User bookUser = null; 280 281 286 private Date bookDT = null; 287 288 295 private boolean finalized = false; 296 297 302 private User finalizeUser = null; 303 304 309 private Date finalizeDT = null; 310 311 315 private Currency currency; 316 317 public static String getPrimaryKey(String organisationID, long invoiceID) 318 { 319 return organisationID + '/' + Long.toHexString(invoiceID); 320 } 321 322 public String getPrimaryKey() 323 { 324 return primaryKey; 325 } 326 327 public long getAmountPaid() { 328 return amountPaid; 329 } 330 public void setAmountPaid(long amountPaid) { 331 this.amountPaid = amountPaid; 332 } 333 public long getAmountToPay() { 334 return getPrice().getAmount() - getAmountPaid(); 335 } 336 public boolean isOutstanding() { 337 return outstanding; 338 } 339 public void setOutstanding(boolean outstanding) { 340 this.outstanding = outstanding; 341 } 342 public boolean isPaid() { 343 return paid; 344 } 345 public void setPaid(boolean paid) { 346 this.paid = paid; 347 } 348 protected void setBooked(User booker, boolean booked) { 349 this.booked = true; 350 this.bookUser = booker; 351 this.bookDT = new Date (System.currentTimeMillis()); 352 } 353 public User getBookUser() { 354 return bookUser; 355 } 356 public Date getBookDT() { 357 return bookDT; 358 } 359 public boolean isBooked() { 360 return booked; 361 } 362 public Date getCreateDT() { 363 return createDT; 364 } 365 public User getCreateUser() { 366 return createUser; 367 } 368 public LegalEntity getCustomer() { 369 return customer; 370 } 371 public long getInvoiceID() { 372 return invoiceID; 373 } 374 public Map getItemsToRefund() { 375 return itemsToRefund; 376 } 377 public Map getItemsToSell() { 378 return itemsToSell; 379 } 380 public String getOrganisationID() { 381 return organisationID; 382 } 383 public OfferItemPrice getPrice() { 384 return price; 385 } 386 public OrganisationLegalEntity getVendor() { 387 return vendor; 388 } 389 protected void setCurrency(Currency currency) { 390 this.currency = currency; 391 } 392 public Currency getCurrency() { 393 return currency; 394 } 395 protected void setFinalized(User finalizer, boolean finalized) { 396 this.finalized = true; 397 this.finalizeUser = finalizer; 398 this.finalizeDT = new Date (System.currentTimeMillis()); 399 } 400 public boolean isFinalized() { 401 return finalized; 402 } 403 public User getFinalizeUser() { 404 return finalizeUser; 405 } 406 public Date getFinilizeDT() { 407 return finalizeDT; 408 } 409 } 410 | Popular Tags |