KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > ipanema > accounting > Invoice


1 /*
2  * Created on Feb 15, 2005
3  * by alex
4  *
5  */

6 package com.nightlabs.ipanema.accounting;
7
8 import java.io.Serializable JavaDoc;
9 import java.util.Collection JavaDoc;
10 import java.util.Date JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.Map JavaDoc;
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 /**
26  * @author Alexander Bieber <alex[AT]nightlabs[DOT]de>
27  *
28  * @jdo.persistence-capable
29  * identity-type = "application"
30  * objectid-class = "com.nightlabs.ipanema.trade.id.InvoiceID"
31  * detachable = "true"
32  *
33  * @jdo.inheritance strategy = "new-table"
34  **/

35 public class Invoice implements Serializable JavaDoc
36 {
37     /**
38      * @jdo.field primary-key="true"
39      * @jdo.column length="100"
40      */

41     private String JavaDoc organisationID;
42
43     /**
44      * @jdo.field primary-key="true"
45      */

46     private long invoiceID;
47     
48     
49     protected Invoice() {}
50
51     public Invoice(User creator, String JavaDoc _organisationID, long _invoiceID) {
52         this.organisationID = _organisationID;
53         this.invoiceID = _invoiceID;
54         this.createDT = new Date JavaDoc(System.currentTimeMillis());
55         this.createUser = creator;
56     }
57     
58     private String JavaDoc primaryKey;
59
60     /**
61      * @jdo.field persistence-modifier="persistent"
62      */

63     private OrganisationLegalEntity vendor;
64
65     /**
66      * @jdo.field persistence-modifier="persistent"
67      */

68     private LegalEntity customer;
69
70     /**
71      * This Map contains all OfferItems that should be sold.
72      * One Product can only be once in an offer for sale and once for refund.
73      * Thus, we can use the productPK as key to the OfferItem.
74      *
75      * key: String productPK (organisationID + / + productID)<br/>
76      * value: OfferItem offerItem
77      * <br/><br/>
78      *
79      * @jdo.field
80      * persistence-modifier="persistent"
81      * collection-type="map"
82      * key-type="java.lang.String"
83      * value-type="OfferItem"
84      * dependent="true"
85      *
86      * @jdo.join
87      */

88     private Map JavaDoc itemsToSell = new HashMap JavaDoc();
89
90     /**
91      * This Map contains all OfferItems that should be refunded.
92      * One Product can only be once in an offer for sale and once for refund.
93      * Thus, we can use the productPK as key to the OfferItem.
94      *
95      * key: String productPK (organisationID + / + productID)<br/>
96      * value: OfferItem offerItem
97      * <br/><br/>
98      *
99      * @jdo.field
100      * persistence-modifier="persistent"
101      * collection-type="map"
102      * key-type="java.lang.String"
103      * value-type="OfferItem"
104      * dependent="true"
105      *
106      * @jdo.join
107      */

108     private Map JavaDoc itemsToRefund = new HashMap JavaDoc();
109     
110     
111     /**
112      * Adds an offerItem, if this Invoice is not yet finalized,
113      * the offerItems offer has the same vendor and customer as this Invoice,
114      * and the offerItem is not yet part of another invoice.
115      *
116      * @param item
117      */

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 JavaDoc 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     /**
192      * Creation date of this Invoice.
193      *
194      * @jdo.field persistence-modifier="persistent"
195      */

196     private Date JavaDoc createDT;
197     
198     /**
199      * The user who created this Invoice.
200      *
201      * @jdo.field persistence-modifier="persistent"
202      */

203     private User createUser = null;
204     
205     /**
206      * This member represents the sum of all prices of all invoice items.
207      *
208      * @jdo.field persistence-modifier="persistent"
209      */

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 JavaDoc items = i == 0 ? itemsToSell.values() : itemsToRefund.values();
219             for (Iterator JavaDoc it = items.iterator(); it.hasNext(); ) {
220                 OfferItem offerItem = (OfferItem)it.next();
221                 // TODO: can price be dependent on offer here as well?
222

223 // if (offerItem.isPriceDependentOnOffer()) {
224
// if (offerItem.isPriceDependentOnOffer())
225
// containsPricesDependentOnOffer = true;
226
//
227
// if (accounting == null) {
228
// PersistenceManager pm = JDOHelper.getPersistenceManager(this);
229
// if (pm == null)
230
// throw new IllegalStateException("This instance of Offer is currently not persistent! Cannot obtain a PersistenceManager");
231
//
232
// accounting = Accounting.getAccounting(pm);
233
// } // if (accounting == null) {
234
// ProductInfo productInfo = accounting.getProductInfo(offerItem.getProduct(), true);
235
// offerItem.setPriceDependentOnOffer(productInfo.getPriceConfig().isDependentOnOffer());
236
// offerItem.getPrice().assign(productInfo.getPriceConfig().getPrice(offerItem));
237
// } // if (all || offerItem.isPriceDependentOnOffer()) {
238
price.sumPrice(offerItem.getPrice());
239             }
240         }
241     }
242
243     /**
244      * This member represents the amount already paid.
245      *
246      * @jdo.field persistence-modifier="persistent"
247      */

248     private long amountPaid;
249
250     /**
251      * This flag is true as long as the vendor still hopes to get the money. This means,
252      * it is set false automatically when paid becomes true or when the vendor gives up
253      * - e.g. because the customer is bankrupt.
254      *
255      * @jdo.field persistence-modifier="persistent"
256      */

257     private boolean outstanding = true;
258
259     /**
260      * This member is set true when the complete amount has been paid.
261      *
262      * @jdo.field persistence-modifier="persistent"
263      */

264     private boolean paid = false;
265
266     /**
267      * This member is set to true as soon as the money is booked on the various
268      * accounts.
269      *
270      * @jdo.field persistence-modifier="persistent"
271      */

272     private boolean booked = false;
273     
274     /**
275      * This member stores the user who booked this Invoice.
276      *
277      * @jdo.field persistence-modifier="persistent"
278      */

279     private User bookUser = null;
280     
281     /**
282      * This member stores when this Invoice was booked.
283      *
284      * @jdo.field persistence-modifier="persistent"
285      */

286     private Date JavaDoc bookDT = null;
287
288     /**
289      * This member is set to true as soon as all desired
290      * OfferItems where added to this offer. A finalized
291      * Invoice can not be altered any more.
292      *
293      * @jdo.field persistence-modifier="persistent"
294      */

295     private boolean finalized = false;
296     
297     /**
298      * This member stores the user who finilized this Invoice.
299      *
300      * @jdo.field persistence-modifier="persistent"
301      */

302     private User finalizeUser = null;
303     
304     /**
305      * This member stores when this Invoice was finalized.
306      *
307      * @jdo.field persistence-modifier="persistent"
308      */

309     private Date JavaDoc finalizeDT = null;
310     
311     /**
312      * Represents the currency of all offerItems within this invoice.
313      * An Invoice can only contain offerItems with the same currency.
314      */

315     private Currency currency;
316
317     public static String JavaDoc getPrimaryKey(String JavaDoc organisationID, long invoiceID)
318     {
319         return organisationID + '/' + Long.toHexString(invoiceID);
320     }
321
322     public String JavaDoc 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 JavaDoc(System.currentTimeMillis());
352     }
353     public User getBookUser() {
354         return bookUser;
355     }
356     public Date JavaDoc getBookDT() {
357         return bookDT;
358     }
359     public boolean isBooked() {
360         return booked;
361     }
362     public Date JavaDoc 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 JavaDoc getItemsToRefund() {
375         return itemsToRefund;
376     }
377     public Map JavaDoc getItemsToSell() {
378         return itemsToSell;
379     }
380     public String JavaDoc 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 JavaDoc(System.currentTimeMillis());
399     }
400     public boolean isFinalized() {
401         return finalized;
402     }
403     public User getFinalizeUser() {
404         return finalizeUser;
405     }
406     public Date JavaDoc getFinilizeDT() {
407         return finalizeDT;
408     }
409 }
410
Popular Tags