1 package org.campware.cream.modules.actions; 2 3 42 43 import java.util.Date ; 44 import java.util.Enumeration ; 45 import java.math.BigDecimal ; 46 import org.apache.velocity.context.Context; 47 48 import org.apache.turbine.util.RunData; 49 import org.apache.turbine.util.parser.ParameterParser; 50 import org.apache.torque.util.Criteria; 51 import org.apache.torque.util.Transaction; 52 import java.sql.Connection ; 53 54 import org.campware.cream.om.Payment; 55 import org.campware.cream.om.PaymentPeer; 56 import org.campware.cream.om.PaymentItem; 57 import org.campware.cream.om.PaymentItemPeer; 58 59 63 public class PaymentSQL extends CreamAction 64 { 65 protected void initScreen() 66 { 67 setModuleType(DOCUMENT); 68 setModuleName("PAYMENT"); 69 } 70 71 81 public void doInsert(RunData data, Context context) 82 throws Exception 83 { 84 Payment entry = new Payment(); 85 data.getParameters().setProperties(entry); 86 87 entry.setPaymentCode(getTempCode()); 88 89 entry.setIssuedDate(parseDate(data.getParameters().getString("issueddate"))); 90 entry.setClosedDate(parseDate(data.getParameters().getString("closeddate"))); 91 entry.setCreatedBy(data.getUser().getName()); 92 entry.setCreated(new Date ()); 93 entry.setModifiedBy(data.getUser().getName()); 94 entry.setModified(new Date ()); 95 96 ParameterParser pp= data.getParameters(); 97 Enumeration paramKeys= pp.keys(); 98 BigDecimal currTotal = new BigDecimal (0); 99 BigDecimal locTotal = new BigDecimal (0); 100 BigDecimal currRate = entry.getCurrencyRate(); 101 int currId = entry.getCurrencyId(); 102 int sordId = entry.getSorderId(); 103 int custId = entry.getCustomerId(); 104 int projId = entry.getProjectId(); 105 106 while(paramKeys.hasMoreElements()) { 107 String paramName = paramKeys.nextElement().toString(); 108 if(paramName.startsWith("productid")) { 109 String suffix=paramName.substring(9, paramName.length()); 110 PaymentItem entryItem= new PaymentItem(); 111 112 entryItem.setProductId(pp.getInt("productid" + suffix)); 113 entryItem.setDescription(pp.getString("description" + suffix)); 114 BigDecimal itmUnitPrice= pp.getBigDecimal("unitprice" + suffix); 115 int itmQuantity= pp.getInt("quantity" + suffix); 116 BigDecimal itmCurrTotal= itmUnitPrice.multiply(BigDecimal.valueOf(itmQuantity)); 117 BigDecimal itmTotal= itmCurrTotal.multiply(currRate); 118 119 entryItem.setUnitPrice(itmUnitPrice); 120 entryItem.setQuantity(itmQuantity); 121 entryItem.setItemCurrTotal(itmCurrTotal); 122 entryItem.setItemTotal(itmTotal); 123 124 entryItem.setCurrencyId(currId); 125 entryItem.setSorderId(sordId); 126 entryItem.setCustomerId(custId); 127 entryItem.setProjectId(projId); 128 129 currTotal= currTotal.add(itmCurrTotal); 130 locTotal= locTotal.add(itmTotal); 131 entry.addPaymentItem(entryItem); 132 } 133 } 134 135 entry.setCurrencyAmount(currTotal); 136 entry.setTotalAmount(locTotal); 137 138 Connection conn = Transaction.begin(PaymentPeer.DATABASE_NAME); 139 boolean success = false; 140 try { 141 entry.save(conn); 142 entry.setPaymentCode(getRowCode("PA", entry.getPaymentId())); 143 entry.save(conn); 144 Transaction.commit(conn); 145 success = true; 146 147 } finally { 148 if (!success) Transaction.safeRollback(conn); 149 } 150 } 151 152 160 public void doUpdate(RunData data, Context context) 161 throws Exception 162 { 163 Payment entry = new Payment(); 164 data.getParameters().setProperties(entry); 165 166 entry.setIssuedDate(parseDate(data.getParameters().getString("issueddate"))); 167 entry.setClosedDate(parseDate(data.getParameters().getString("closeddate"))); 168 entry.setCreated(parseDateTime(data.getParameters().getString("created"))); 169 entry.setModifiedBy(data.getUser().getName()); 170 entry.setModified(new Date ()); 171 172 ParameterParser pp= data.getParameters(); 173 Enumeration paramKeys= pp.keys(); 174 BigDecimal currTotal = new BigDecimal (0); 175 BigDecimal locTotal = new BigDecimal (0); 176 BigDecimal currRate = entry.getCurrencyRate(); 177 int currId = entry.getCurrencyId(); 178 int sordId = entry.getSorderId(); 179 int custId = entry.getCustomerId(); 180 int projId = entry.getProjectId(); 181 182 while(paramKeys.hasMoreElements()) { 183 String paramName = paramKeys.nextElement().toString(); 184 if(paramName.startsWith("productid")) { 185 String suffix=paramName.substring(9, paramName.length()); 186 PaymentItem entryItem= new PaymentItem(); 187 188 entryItem.setProductId(pp.getInt("productid" + suffix)); 189 entryItem.setDescription(pp.getString("description" + suffix)); 190 BigDecimal itmUnitPrice= pp.getBigDecimal("unitprice" + suffix); 191 int itmQuantity= pp.getInt("quantity" + suffix); 192 BigDecimal itmCurrTotal= itmUnitPrice.multiply(BigDecimal.valueOf(itmQuantity)); 193 BigDecimal itmTotal= itmCurrTotal.multiply(currRate); 194 195 entryItem.setUnitPrice(itmUnitPrice); 196 entryItem.setQuantity(itmQuantity); 197 entryItem.setItemCurrTotal(itmCurrTotal); 198 entryItem.setItemTotal(itmTotal); 199 200 entryItem.setCurrencyId(currId); 201 entryItem.setSorderId(sordId); 202 entryItem.setCustomerId(custId); 203 entryItem.setProjectId(projId); 204 205 currTotal= currTotal.add(itmCurrTotal); 206 locTotal= locTotal.add(itmTotal); 207 entry.addPaymentItem(entryItem); 208 } 209 } 210 211 entry.setCurrencyAmount(currTotal); 212 entry.setTotalAmount(locTotal); 213 214 entry.setModified(true); 215 entry.setNew(false); 216 217 Criteria crit = new Criteria(); 218 crit.add(PaymentItemPeer.PAYMENT_ID, entry.getPaymentId()); 219 220 Connection conn = Transaction.begin(PaymentPeer.DATABASE_NAME); 221 boolean success = false; 222 try { 223 PaymentItemPeer.doDelete(crit, conn); 224 entry.save(conn); 225 Transaction.commit(conn); 226 success = true; 227 228 } finally { 229 if (!success) Transaction.safeRollback(conn); 230 } 231 } 232 233 237 public void doDelete(RunData data, Context context) 238 throws Exception 239 { 240 Criteria criteria = new Criteria(); 241 criteria.add(PaymentPeer.PAYMENT_ID, data.getParameters().getInt("paymentid")); 242 PaymentPeer.doDelete(criteria); 243 } 244 245 249 public void doDeleteselected(RunData data, Context context) 250 throws Exception 251 { 252 int[] delIds= data.getParameters().getInts("rowid"); 253 Criteria criteria = new Criteria(); 254 criteria.addIn(PaymentPeer.PAYMENT_ID, delIds); 255 PaymentPeer.doDelete(criteria); 256 } 257 258 259 } 260 | Popular Tags |