KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > hipergate > AbstractOrder


1 package com.knowgate.hipergate;
2
3 import java.math.BigDecimal JavaDoc;
4
5 import java.sql.SQLException JavaDoc;
6 import java.sql.PreparedStatement JavaDoc;
7 import java.sql.ResultSet JavaDoc;
8 import java.sql.Timestamp JavaDoc;
9 import java.sql.Statement JavaDoc;
10 import java.sql.Types JavaDoc;
11
12 import java.util.Date JavaDoc;
13 import java.util.Locale JavaDoc;
14
15 import java.text.NumberFormat JavaDoc;
16 import java.text.DecimalFormat JavaDoc;
17 import java.text.FieldPosition JavaDoc;
18
19 import com.knowgate.debug.DebugFile;
20 import com.knowgate.jdc.JDCConnection;
21 import com.knowgate.dataobjs.DB;
22 import com.knowgate.dataobjs.DBPersist;
23 import com.knowgate.dataobjs.DBSubset;
24 import com.knowgate.misc.Gadgets;
25 import com.knowgate.dataobjs.DBBind;
26
27 /**
28  * An abstract super class for Order and Invoice classes
29  * @author Sergio Montoro Ten
30  * @version 3.0
31  */

32 public abstract class AbstractOrder extends DBPersist {
33
34   private Locale JavaDoc oLocale;
35   private String JavaDoc sCurrencyFormat;
36   private DecimalFormat JavaDoc oCurrencyFormat = null;
37   private FieldPosition JavaDoc oCurrencyFieldP = null;
38   private StringBuffer JavaDoc oCurrencyBuffer;
39
40   protected DBSubset oLines;
41   protected DBPersist oBuyer;
42   protected DBPersist oSeller;
43   protected String JavaDoc sLinesTable, sPrimaryKey;
44
45   // ---------------------------------------------------------------------------
46

47   protected AbstractOrder(String JavaDoc sTableName, String JavaDoc sLinesName,
48                           String JavaDoc sKeyName, String JavaDoc sAuditClass) {
49     super(sTableName, sAuditClass);
50     sLinesTable = sLinesName;
51     sPrimaryKey = sKeyName;
52     oLines = null;
53     oBuyer = null;
54     oSeller = null;
55     oLocale = null;
56     setCurrencyFormat("#0.00");
57   }
58
59
60   // ---------------------------------------------------------------------------
61

62   public String JavaDoc getCurrencyFormat() {
63     return sCurrencyFormat;
64   }
65
66   // ---------------------------------------------------------------------------
67

68   public void setCurrencyFormat(String JavaDoc sFormat) throws NullPointerException JavaDoc {
69     sCurrencyFormat = sFormat;
70     oCurrencyFormat = new DecimalFormat JavaDoc(sFormat);
71     oCurrencyFieldP = new FieldPosition JavaDoc(NumberFormat.FRACTION_FIELD);
72     oCurrencyBuffer = new StringBuffer JavaDoc();
73   }
74
75   // ---------------------------------------------------------------------------
76

77   protected StringBuffer JavaDoc getDecimalFormated(String JavaDoc sColumnName) {
78     oCurrencyBuffer.setLength(0);
79     if (!isNull(sColumnName))
80       oCurrencyFormat.format(getDecimal(sColumnName).doubleValue(), oCurrencyBuffer, oCurrencyFieldP);
81     return oCurrencyBuffer;
82   }
83
84   // ---------------------------------------------------------------------------
85

86   public void setLocale(Locale JavaDoc oLoc) {
87     oLocale = oLoc;
88   }
89
90   // ---------------------------------------------------------------------------
91

92   public void setLocale(String JavaDoc sLanguage, String JavaDoc sCountry) {
93     oLocale = new Locale JavaDoc(sLanguage, sCountry);
94   }
95
96   // ---------------------------------------------------------------------------
97

98   public void setLocale(String JavaDoc sLanguage) {
99     oLocale = new Locale JavaDoc(sLanguage);
100   }
101
102   // ---------------------------------------------------------------------------
103

104   public Locale JavaDoc getLocale() {
105     return oLocale==null ? Locale.getDefault() : oLocale;
106   }
107
108   // ---------------------------------------------------------------------------
109

110   /**
111    * <p>Get billing address for this order</p>
112    * Address is loaded from value of gu_bill_addr column. If gu_bill_addr is null
113    * then this function returns null.
114    * @param oConn JDCConnection
115    * @return Address object instance or <b>null</b> if gu_bill_addr is <b>null</b>
116    * @throws SQLException
117    */

118   public Address getBillAddress(JDCConnection oConn) throws SQLException JavaDoc {
119     Address oBillAddr;
120     if (isNull(DB.gu_bill_addr)) {
121       oBillAddr = null;
122     } else {
123       oBillAddr = new Address(oConn, getString(DB.gu_bill_addr));
124     }
125     return oBillAddr;
126   } // getBillAddress
127

128   // ---------------------------------------------------------------------------
129

130   /**
131    * Get order lines as a DBSubset
132    * @return DBSubset The columns returned depend on the implementation of
133    * getLines(JDCConnection) at derived clases
134    * @throws IllegalStateException If this method is called without having
135    * loaded the order lines first.
136    */

137   public DBSubset getLines() throws IllegalStateException JavaDoc {
138     if (oLines==null)
139       throw new IllegalStateException JavaDoc("AbstractOrder.getLines() Order lines not loaded");
140     else
141       return oLines;
142   }
143
144   // ---------------------------------------------------------------------------
145

146   public abstract DBSubset getLines(JDCConnection oConn) throws SQLException JavaDoc;
147
148   // ---------------------------------------------------------------------------
149

150   public boolean load(JDCConnection oConn, Object JavaDoc[] PKVals) throws SQLException JavaDoc {
151     PreparedStatement JavaDoc oStmt;
152     ResultSet JavaDoc oRSet;
153
154     boolean bLoad = super.load(oConn, PKVals);
155
156     oLines = null;
157     oBuyer = null;
158     oSeller= null;
159
160     if (bLoad) {
161       oLines = getLines(oConn);
162
163       if (!isNull(DB.gu_contact)) {
164         oStmt = oConn.prepareStatement("SELECT "+DB.gu_address+ " FROM "+DB.k_x_contact_addr+" WHERE "+DB.gu_contact+"=?",ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
165         oStmt.setString(1, getString(DB.gu_contact));
166         oRSet = oStmt.executeQuery();
167         if (oRSet.next()) {
168           oBuyer = new DBPersist(DB.k_member_address, "Buyer");
169           oBuyer.load(oConn, new Object JavaDoc[]{oRSet.getString(1)});
170         }
171         oRSet.close();
172         oStmt.close();
173       } else if (!isNull(DB.gu_company)) {
174         oStmt = oConn.prepareStatement("SELECT "+DB.gu_address+ " FROM "+DB.k_x_company_addr+" WHERE "+DB.gu_company+"=?",ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
175         oStmt.setString(1, getString(DB.gu_company));
176         oRSet = oStmt.executeQuery();
177         if (oRSet.next()) {
178           oBuyer = new DBPersist(DB.k_member_address, "Buyer");
179           oBuyer.load(oConn, new Object JavaDoc[]{oRSet.getString(1)});
180         }
181         oRSet.close();
182         oStmt.close();
183       }
184       if (!isNull(DB.gu_shop))
185         oSeller = new Shop(oConn, getString(DB.gu_shop));
186     } // fi (bLoad)
187

188     return bLoad;
189   }
190
191   //----------------------------------------------------------------------------
192

193   private void insertLine(JDCConnection oConn, int iLine,
194                           String JavaDoc sProductId, String JavaDoc sProductNm,
195                           BigDecimal JavaDoc dSalePr, float fQuantity,
196                           BigDecimal JavaDoc dTotalPr, float fTax,
197                           short iTaxIncluded, String JavaDoc sPromotion,
198                           String JavaDoc sOptions) throws SQLException JavaDoc {
199     PreparedStatement JavaDoc oStmt;
200     String JavaDoc sSQL;
201
202     if (DebugFile.trace) {
203       DebugFile.writeln("Begin AbstractOrder.insertLine([Connection], " + String.valueOf(iLine) + "," + (sProductId!=null ? sProductId : "null") + "," + (sProductNm!=null ? sProductNm : "null") + "," + dSalePr.toString() + "," + String.valueOf(fQuantity) + "," + dTotalPr.toString() + "," + String.valueOf(iTaxIncluded) + "," + (sPromotion!=null ? sPromotion : "") + "," + (sOptions!=null ? sOptions : ""));
204       DebugFile.incIdent();
205     }
206
207     sSQL = "INSERT INTO " + sLinesTable + " (" + sPrimaryKey + "," + DB.pg_line + "," + DB.gu_product + "," + DB.nm_product + "," + DB.pr_sale + "," + DB.nu_quantity + "," + DB.pr_total + "," + DB.pct_tax_rate + "," + DB.is_tax_included + "," + DB.tx_promotion + "," + DB.tx_options + ") VALUES (?,?,?,?,?,?,?,?,?,?,?)";
208
209     if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(" + sSQL + ")");
210
211     oStmt = oConn.prepareStatement(sSQL);
212
213     oStmt.setString(1,getString(sPrimaryKey));
214     oStmt.setInt (2,iLine);
215     oStmt.setString(3, sProductId);
216     oStmt.setString(4, sProductNm);
217     oStmt.setBigDecimal (5, dSalePr);
218     oStmt.setFloat (6, fQuantity);
219     oStmt.setBigDecimal (7, dTotalPr);
220     oStmt.setFloat (8, fTax);
221     oStmt.setShort (9, iTaxIncluded);
222     oStmt.setString(10, sPromotion);
223     oStmt.setString(11, sOptions);
224
225     if (DebugFile.trace) DebugFile.writeln("Connection.execute()");
226
227     oStmt.execute();
228     oStmt.close();
229
230     oStmt = oConn.prepareStatement("UPDATE "+getTableName()+" SET "+DB.dt_modified+"="+DBBind.Functions.GETDATE+" WHERE "+sPrimaryKey+"=?");
231     oStmt.setObject(1, get(sPrimaryKey), Types.CHAR);
232     oStmt.executeUpdate();
233     oStmt.close();
234
235     if (DebugFile.trace) {
236       DebugFile.decIdent();
237       DebugFile.writeln("End AbstractOrder.insertLine()");
238     }
239   } // insertLine
240

241   //----------------------------------------------------------------------------
242

243   private boolean updateLine(JDCConnection oConn, int iLine,
244                              String JavaDoc sProductId, String JavaDoc sProductNm,
245                              BigDecimal JavaDoc dSalePr, float fQuantity,
246                              BigDecimal JavaDoc dTotalPr, float fTax,
247                              short iTaxIncluded, String JavaDoc sPromotion,
248                              String JavaDoc sOptions) throws SQLException JavaDoc {
249     boolean bUpdated;
250     PreparedStatement JavaDoc oStmt;
251     String JavaDoc sSQL;
252
253     if (DebugFile.trace) {
254       DebugFile.writeln("Begin AbstractOrder.updateLine([Connection], " + String.valueOf(iLine) + "," + (sProductId!=null ? sProductId : "null") + "," + (sProductNm!=null ? sProductNm : "null") + "," + dSalePr.toString() + "," + String.valueOf(fQuantity) + "," + dTotalPr.toString() + "," + String.valueOf(iTaxIncluded) + "," + (sPromotion!=null ? sPromotion : "") + "," + (sOptions!=null ? sOptions : ""));
255       DebugFile.incIdent();
256     }
257
258     sSQL = "UPDATE " + sLinesTable + " SET " + DB.gu_product + "=?," + DB.nm_product + "=?," + DB.pr_sale + "=?," + DB.nu_quantity + "=?," + DB.pr_total + "=?," + DB.pct_tax_rate + "=?," + DB.is_tax_included + "=?," + DB.tx_promotion + "=?," + DB.tx_options + "=? WHERE " + sPrimaryKey + "=? AND " + DB.pg_line + "=?";
259
260     if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(" + sSQL + ")");
261
262     oStmt = oConn.prepareStatement(sSQL);
263
264     oStmt.setString(1, sProductId);
265     oStmt.setString(2, sProductNm);
266     oStmt.setBigDecimal (3, dSalePr);
267     oStmt.setFloat (4, fQuantity);
268     oStmt.setBigDecimal (5, dTotalPr);
269     oStmt.setFloat (6, fTax);
270     oStmt.setShort (7, iTaxIncluded);
271     oStmt.setString(8, sPromotion);
272     oStmt.setString(9, sOptions);
273     oStmt.setString(10,getString(sPrimaryKey));
274     oStmt.setInt (11,iLine);
275
276     if (DebugFile.trace) DebugFile.writeln("Connection.executeUpdate()");
277
278     bUpdated = (oStmt.executeUpdate()>0);
279     oStmt.close();
280
281     oStmt = oConn.prepareStatement("UPDATE "+getTableName()+" SET "+DB.dt_modified+"="+DBBind.Functions.GETDATE+" WHERE "+sPrimaryKey+"=?");
282     oStmt.setObject(1, get(sPrimaryKey), Types.CHAR);
283     oStmt.executeUpdate();
284     oStmt.close();
285
286     if (DebugFile.trace) {
287       DebugFile.decIdent();
288       DebugFile.writeln("End AbstractOrder.updateLine() : " + String.valueOf(bUpdated));
289     }
290
291     return bUpdated;
292   } // updateLine
293

294   //----------------------------------------------------------------------------
295

296   /**
297    * <p>Add Product to Order or Invoice line</p>
298    * This method may be used to add order lines for products not present at
299    * k_products table.
300    * @param oConn Database Connection
301    * @param sProductId Product GUID (optional, if not set a new one will be
302    * automatically assigned).
303    * @param sProductNm Product Name
304    * @param dSalePr Product Unitary Sale Price
305    * @param fQuantity Quantity ordered
306    * @param dTotalPr Total Price for Quantity including taxes
307    * @param fTax Percentage of tax rate applicable
308    * @param iTaxIncluded 1 if tax is included in unitary price, 0 if tax is not included.
309    * @param sPromotion Promotional Text
310    * @param sOptions Additional Options
311    * @return Line Number Added
312    * @throws SQLException
313    * @throws IllegalArgumentException If sProductId does not exist
314    * @throws NullPointerException
315    */

316   public int addProduct(JDCConnection oConn, String JavaDoc sProductId, String JavaDoc sProductNm,
317                         BigDecimal JavaDoc dSalePr, float fQuantity, BigDecimal JavaDoc dTotalPr,
318                         float fTax, short iTaxIncluded, String JavaDoc sPromotion,
319                         String JavaDoc sOptions)
320     throws SQLException JavaDoc,IllegalArgumentException JavaDoc,NullPointerException JavaDoc {
321     int iPgLine;
322     Object JavaDoc oPgLine;
323     ResultSet JavaDoc oLine;
324     PreparedStatement JavaDoc oSeek;
325
326     if (isNull(sPrimaryKey)) throw new NullPointerException JavaDoc("AbstractOrder.addProduct() "+sPrimaryKey+" cannot be null");
327
328     if (DebugFile.trace) {
329       DebugFile.writeln("Begin AbstractOrder.addProduct([Connection], " + (sProductId!=null ? sProductId : "null") + "," + (sProductNm!=null ? sProductNm : "null") + "," + dSalePr.toString() + "," + String.valueOf(fQuantity) + "," + dTotalPr.toString() + "," + String.valueOf(iTaxIncluded) + "," + (sPromotion!=null ? sPromotion : "") + "," + (sOptions!=null ? sOptions : ""));
330       DebugFile.incIdent();
331     }
332
333     if (null==sProductId) {
334       sProductId = "null_"+Gadgets.leftPad(Gadgets.left(sProductNm==null ? "" : Gadgets.ASCIIEncode(sProductNm), 27),'*',27).toUpperCase();
335       iPgLine = -1;
336     }
337     else {
338       if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(" + "SELECT " + DB.pg_line + " FROM " + sLinesTable + " WHERE " + sPrimaryKey + "=? AND " + DB.gu_product + "=?" + ")");
339
340       oSeek = oConn.prepareStatement("SELECT " + DB.pg_line + " FROM " + sLinesTable + " WHERE " + sPrimaryKey + "=? AND " + DB.gu_product + "=?",
341                                      ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
342       oSeek.setString(1, getString(sPrimaryKey));
343       oSeek.setString(2, sProductId);
344       oLine = oSeek.executeQuery();
345       if (oLine.next())
346         iPgLine = oLine.getInt(1);
347       else
348         iPgLine = -1;
349       oLine.close();
350       oSeek.close();
351     }
352
353     if (-1==iPgLine) {
354       if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(" + "SELECT MAX(" + DB.pg_line + ") FROM " + sLinesTable + " WHERE " + sPrimaryKey + "=?" + ")");
355
356       oSeek = oConn.prepareStatement("SELECT MAX(" + DB.pg_line + ") FROM " + sLinesTable + " WHERE " + sPrimaryKey + "=?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
357       oSeek.setString(1, getString(sPrimaryKey));
358       oLine = oSeek.executeQuery();
359       if (oLine.next())
360         oPgLine = oLine.getObject(1);
361       else
362         oPgLine = null;
363       oLine.close();
364       oSeek.close();
365
366       if (null==oPgLine)
367         iPgLine = 1;
368       else
369         iPgLine = new Integer JavaDoc(oPgLine.toString()).intValue() + 1;
370
371       insertLine (oConn, iPgLine, sProductId, sProductNm, dSalePr, fQuantity, dTotalPr, fTax, iTaxIncluded, sPromotion, sOptions);
372     }
373     else
374       updateLine (oConn, iPgLine, sProductId, sProductNm, dSalePr, fQuantity, dTotalPr, fTax, iTaxIncluded, sPromotion, sOptions);
375
376     if (DebugFile.trace) {
377       DebugFile.decIdent();
378       DebugFile.writeln("End AbstractOrder.addProduct() : " + String.valueOf(iPgLine));
379     }
380
381     return iPgLine;
382   } // addProduct
383

384   //----------------------------------------------------------------------------
385

386   /**
387    * <p>Add a Product from a Catalog to an Order or Invoice line</p>
388    * This method takes into account pr_sale, pr_list, dt_start and dt_end fields.<br>
389    * There are two possible prices, <i>list price</i> or <i>sale</i> (bargain) <i>price</i>.<br>
390    * Sale price is returned if it exists at database and given date is between dt_start and dt_end.<br>
391    * Otherwise List price is returned.<br>
392    * Product Price is taken from k_products table following these rules:<br>
393    * <ul>
394    * <li>if dt_start AND dt_end are NULL then pr_list price is assigned.
395    * <li>if dt_start is NULL AND dt_end is NOT NULL AND pr_sale is NULL then pr_list is assigned.
396    * <li>if dt_start is NULL AND dt_end is NOT NULL AND pr_sale is NOT NULL AND current date is less than or equal to dt_end then pr_sale is assigned.
397    * <li>if dt_start is NULL AND dt_end is NOT NULL AND pr_sale is NOT NULL AND current date is greater than dt_end then pr_list is assigned.
398    * <li>if dt_start is NOT NULL AND dt_end is NULL AND pr_sale is NULL then pr_list is assigned.
399    * <li>if dt_start is NOT NULL AND dt_end is NULL AND pr_sale is NOT NULL AND current date is greater than or equal to dt_start then pr_sale is assigned.
400    * <li>if dt_start is NOT NULL AND dt_end is NULL AND pr_sale is NOT NULL AND current date is less than dt_start then pr_list is assigned.
401    * <li>if dt_start AND dt_end are NOT NULL AND pr_sale IS NULL then pr_list price is assigned.
402    * <li>if dt_start AND dt_end are NOT NULL AND pr_sale IS NOT NULL AND current date is greater than or equal to dt_start AND current_date is less than or equal to dt_end then pr_sale is assigned.
403    * <li>if dt_start AND dt_end are NOT NULL AND pr_sale IS NOT NULL AND current date is less than dt_start OR current_date is greater than dt_end then pr_sale is assigned.
404    * </ul>
405    * @param oConn Database Conenction
406    * @param sProductId Product GUID (required)
407    * @param sProductNm Product Name
408    * @param fQuantity Quantity ordered.
409    * @param sPromotion Promotional Text
410    * @param sOptions Additional Options
411    * @return Line Number Added
412    * @throws SQLException
413    * @throws IllegalArgumentException If sProductId does not exist
414    * @throws NullPointerException
415    */

416
417   public int addProduct(JDCConnection oConn, String JavaDoc sProductId, String JavaDoc sProductNm,
418                         float fQuantity, String JavaDoc sPromotion, String JavaDoc sOptions)
419       throws SQLException JavaDoc,IllegalArgumentException JavaDoc,NullPointerException JavaDoc {
420
421     if (DebugFile.trace) {
422       DebugFile.writeln("Begin AbstractOrder.addProduct([Connection], " + (sProductId!=null ? sProductId : "null") + "," + (sProductNm!=null ? sProductNm : "null") + "," + String.valueOf(fQuantity) + "," + (sPromotion!=null ? sPromotion : "") + "," + (sOptions!=null ? sOptions : ""));
423       DebugFile.incIdent();
424     }
425
426     Product oProd = new Product();
427     boolean bFound;
428     if (sProductId==null)
429       bFound = false;
430     else {
431       bFound = oProd.load(oConn, new Object JavaDoc[]{sProductId});
432
433       if (!bFound) {
434         if (DebugFile.trace) DebugFile.decIdent();
435         throw new SQLException JavaDoc("AbstractOrder.addProduct() Product "+sProductId+" not found","02000", 200);
436       }
437     } // fi (sProductId)
438

439     float fTaxRate;
440     BigDecimal JavaDoc dListPr;
441     BigDecimal JavaDoc dSalePr;
442     short iTaxIncluded;
443     boolean bDtStart = oProd.isNull(DB.dt_start);
444     boolean bDtEnd = oProd.isNull(DB.dt_end);
445
446     if (null==sProductNm) sProductNm = oProd.getString(DB.nm_product);
447     if (oProd.isNull(DB.pr_list)) dListPr = new BigDecimal JavaDoc(0); else dListPr = oProd.getDecimal(DB.pr_list);
448     if (oProd.isNull(DB.is_tax_included)) iTaxIncluded = (short)0f; else iTaxIncluded = oProd.getShort(DB.is_tax_included);
449     if (oProd.isNull(DB.pct_tax_rate)) fTaxRate = 0f; else fTaxRate = oProd.getFloat(DB.pct_tax_rate);
450
451     Date JavaDoc dNow = new Date JavaDoc();
452     dSalePr = oProd.salePrice (dNow);
453
454     if (null==dSalePr)
455       throw new NullPointerException JavaDoc("Could not find Sale Price for Product " + oProd.getStringNull(DB.gu_product,"") + " on date " + dNow.toString());
456
457     int iPg = addProduct(oConn, sProductId, sProductNm, dSalePr, fQuantity,
458                          new BigDecimal JavaDoc(dSalePr.doubleValue() * (double)fQuantity),
459                          fTaxRate, iTaxIncluded, sPromotion, sOptions);
460
461     if (DebugFile.trace) {
462       DebugFile.decIdent();
463       DebugFile.writeln("End AbstractOrder.addProduct() : " + String.valueOf(iPg));
464     }
465
466     return iPg;
467  } // addProduct
468

469   // ---------------------------------------------------------------------------
470

471   /**
472    * <p>Add Product from Catalog to an Order or Invoice line</p>
473    * @param oConn Database Connection
474    * @param sProductId Product GUID
475    * @param fQuantity Quantity
476    * @return Line Number Added
477    * @throws SQLException
478    * @throws IllegalArgumentException
479    * @throws NullPointerException
480    */

481   public int addProduct(JDCConnection oConn, String JavaDoc sProductId, float fQuantity)
482     throws SQLException JavaDoc,IllegalArgumentException JavaDoc,NullPointerException JavaDoc {
483
484     return addProduct (oConn, sProductId, null, fQuantity, null, null);
485   }
486
487   //----------------------------------------------------------------------------
488

489   /**
490    * <p>Remove Product from Order given its GUID</p>
491    * @param oConn Database Connection
492    * @param sProductId GUID of product to be removed.
493    * @throws SQLException
494    */

495   public void removeProduct(JDCConnection oConn, String JavaDoc sProductId) throws SQLException JavaDoc {
496
497     if (DebugFile.trace) {
498       DebugFile.writeln("Begin Order.removeProduct([Connection], " + (sProductId!=null ? sProductId : "null") + ")");
499       DebugFile.incIdent();
500     }
501
502     Statement JavaDoc oDlte = oConn.createStatement();
503
504     if (DebugFile.trace)
505       DebugFile.writeln("Statement.execute(DELETE FROM " + sLinesTable + " WHERE " + sPrimaryKey + "='" + getStringNull(sPrimaryKey,"null") + "' AND " + DB.gu_product + "='" + (sProductId!=null ? sProductId : "null") + "'");
506
507     oDlte.execute("DELETE FROM " + sLinesTable + " WHERE " + sPrimaryKey + "='" + getString(sPrimaryKey) + "' AND " + DB.gu_product + "='" + sProductId + "'");
508     oDlte.close();
509
510     PreparedStatement JavaDoc oStmt = oConn.prepareStatement("UPDATE "+getTableName()+" SET "+DB.dt_modified+"="+DBBind.Functions.GETDATE+" WHERE "+sPrimaryKey+"=?");
511     oStmt.setObject(1, get(sPrimaryKey), Types.CHAR);
512     oStmt.executeUpdate();
513     oStmt.close();
514
515     if (DebugFile.trace) {
516       DebugFile.decIdent();
517       DebugFile.writeln("End Order.removeProduct()");
518     }
519   } // removeProduct
520

521   //----------------------------------------------------------------------------
522

523   /**
524    * <p>Remove all products from invoice or order (empty basket)</p>
525    * @param oConn Database Connection
526    * @throws SQLException
527    */

528   public void removeAllProducts(JDCConnection oConn) throws SQLException JavaDoc {
529     if (DebugFile.trace) {
530       DebugFile.writeln("Begin Order.removeAllProducts([Connection])");
531       DebugFile.incIdent();
532     }
533
534     Statement JavaDoc oDlte = oConn.createStatement();
535
536     if (DebugFile.trace)
537       DebugFile.writeln("Statement.execute(DELETE FROM " + sLinesTable + " WHERE " + sPrimaryKey + "='" + getStringNull(sPrimaryKey,"") + "'");
538
539     oDlte.execute("DELETE FROM " + sLinesTable + " WHERE " + sPrimaryKey + "='" + getString(sPrimaryKey) + "'");
540     oDlte.close();
541
542     PreparedStatement JavaDoc oStmt = oConn.prepareStatement("UPDATE "+getTableName()+" SET "+DB.dt_modified+"="+DBBind.Functions.GETDATE+" WHERE "+sPrimaryKey+"=?");
543     oStmt.setObject(1, get(sPrimaryKey), Types.CHAR);
544     oStmt.executeUpdate();
545     oStmt.close();
546
547     if (DebugFile.trace) {
548       DebugFile.decIdent();
549       DebugFile.writeln("End Order.removeAllProducts()");
550     }
551   } // removeAllProducts
552

553   //----------------------------------------------------------------------------
554

555   /**
556    * <p>Sum over pr_sale price taking quantities into account</p>
557    * This method will compute the sum of order line prices without taxes or other charges
558    * @return BigDecimal
559    * @throws IllegalStateException if order lines have not been previously loaded
560    */

561   public BigDecimal JavaDoc computeSubtotal()
562     throws IllegalStateException JavaDoc {
563
564     if (null==oLines)
565       throw new IllegalStateException JavaDoc("AbstractOrder.computeSubtotal() Lines must be loaded before computing subtotal");
566
567     BigDecimal JavaDoc dSubTotal = new BigDecimal JavaDoc(0d);
568
569     final int iLines = oLines.getRowCount();
570
571     if (iLines>0) {
572       final int iPrSale = oLines.getColumnPosition(DB.pr_sale);
573       final int iNuQuan = oLines.getColumnPosition(DB.nu_quantity);
574       final BigDecimal JavaDoc dOne = new BigDecimal JavaDoc(1d);
575       BigDecimal JavaDoc dQuantity;
576
577       for (int l = 0; l < iLines; l++) {
578         if (!oLines.isNull(iPrSale, l)) {
579           if (oLines.isNull(iNuQuan, l))
580             dQuantity = dOne;
581           else
582             dQuantity = new BigDecimal JavaDoc(oLines.getFloat(iNuQuan, l));
583           dSubTotal = dSubTotal.add(oLines.getDecimal(iPrSale,l).multiply(dQuantity));
584         } // fi (pr_sale is not null)
585
} // next
586
}
587     return dSubTotal;
588   } // computeSubtotal
589

590   //----------------------------------------------------------------------------
591

592   /**
593    * <p>Get total price for all lines without taxes</p>
594    * @param oConn JDCConnection
595    * @return BigDecimal Sum over pr_sale price taking quantities into account
596    * @throws SQLException
597    */

598
599   public BigDecimal JavaDoc computeSubtotal(JDCConnection oConn) throws SQLException JavaDoc {
600     if (null==oLines) getLines(oConn);
601     return computeSubtotal();
602   }
603
604   //----------------------------------------------------------------------------
605

606   /**
607    * <p>Get total tax amount for all lines</p>
608    * @return BigDecimal SUM(pr_sale*nu_quantity*pct_tax_rate)
609    * @throws IllegalStateException
610    */

611   public BigDecimal JavaDoc computeTaxes()
612     throws IllegalStateException JavaDoc {
613
614     if (null == oLines)
615       throw new IllegalStateException JavaDoc("AbstractOrder.computeTaxes() Lines must be loaded before computing subtotal");
616
617     BigDecimal JavaDoc dTaxes = new BigDecimal JavaDoc(0d);
618     boolean bTaxIncluded;
619     final int iLines = oLines.getRowCount();
620
621     if (iLines > 0) {
622       final int iPrSale = oLines.getColumnPosition(DB.pr_sale);
623       final int iNuQuan = oLines.getColumnPosition(DB.nu_quantity);
624       final int iPctTax = oLines.getColumnPosition(DB.pct_tax_rate);
625       final int iTaxInc = oLines.getColumnPosition(DB.is_tax_included);
626       final BigDecimal JavaDoc dOne = new BigDecimal JavaDoc(1d);
627       BigDecimal JavaDoc dQuantity, dTax;
628
629       for (int l = 0; l < iLines; l++) {
630         if (!oLines.isNull(iPctTax,l) && !oLines.isNull(iPrSale,l)) {
631           if (oLines.isNull(iNuQuan, l))
632             dQuantity = dOne;
633           else
634             dQuantity = new BigDecimal JavaDoc(oLines.getFloat(iNuQuan, l));
635           if (getFloat(DB.pct_tax_rate)>1f)
636             dTax = new BigDecimal JavaDoc(getFloat(DB.pct_tax_rate)/100f);
637           else
638             dTax = new BigDecimal JavaDoc(getFloat(DB.pct_tax_rate));
639           if (oLines.isNull(iTaxInc,l))
640             bTaxIncluded = false;
641           else if (oLines.getShort(iTaxInc,l)==(short)0)
642             bTaxIncluded = false;
643           else
644             bTaxIncluded = true;
645           if (bTaxIncluded)
646             dTaxes = dTaxes.add(oLines.getDecimal(iPrSale,l).multiply(dQuantity).multiply(dTax));
647         }
648       } // next
649
} // fi (iLines>0)
650
return dTaxes;
651   } // computeTaxes
652

653   //----------------------------------------------------------------------------
654

655   public BigDecimal JavaDoc computeTaxes(JDCConnection oConn) throws SQLException JavaDoc {
656     if (null==oLines) getLines(oConn);
657     return computeTaxes();
658   }
659
660   //----------------------------------------------------------------------------
661

662   /**
663    * <p>Compute Invoce total</p>
664    * Sum of all line subtotals including taxes.
665    * @return BigDecimal computeSubtotal()+computeTaxes()
666    * @throws IllegalStateException if order lines have not been previously loaded
667    */

668   public BigDecimal JavaDoc computeTotal() throws IllegalStateException JavaDoc {
669     return computeSubtotal().add(computeTaxes());
670   }
671 }
672
Popular Tags