1 4 package com.openedit.store.retailproconvert; 5 6 import java.io.BufferedInputStream ; 7 import java.io.File ; 8 import java.io.FileInputStream ; 9 import java.io.FileOutputStream ; 10 import java.io.IOException ; 11 import java.io.InputStream ; 12 import java.text.DateFormat ; 13 import java.text.SimpleDateFormat ; 14 import java.util.Date ; 15 import java.util.Iterator ; 16 import java.util.Properties ; 17 import java.util.zip.ZipEntry ; 18 import java.util.zip.ZipOutputStream ; 19 20 import org.dom4j.Element; 21 import org.openedit.money.Fraction; 22 import org.openedit.money.Money; 23 24 import com.openedit.WebPageRequest; 25 import com.openedit.store.AbstractXmlOrderArchive; 26 import com.openedit.store.CartItem; 27 import com.openedit.store.CreditPaymentMethod; 28 import com.openedit.store.Order; 29 import com.openedit.store.ShippingMethod; 30 import com.openedit.store.Store; 31 import com.openedit.store.StoreException; 32 import com.openedit.store.customer.Address; 33 import com.openedit.store.customer.Customer; 34 import com.openedit.util.FileUtils; 35 import com.openedit.util.OutputFiller; 36 37 42 public class RetailProOrderArchive extends AbstractXmlOrderArchive 43 { 44 protected static final DateFormat DATE_FORMAT = new SimpleDateFormat ( "M/d/yy h:mm:ss a" ); 45 48 public void archiveOrderData( Store inStore ) throws StoreException 49 { 50 80 } 81 82 protected void zipUpFile( File inFile, File outFile ) throws StoreException 83 { 84 ZipOutputStream zipStream = null; 85 InputStream inStream = null; 86 try 87 { 88 zipStream = new ZipOutputStream ( new FileOutputStream ( outFile ) ); 89 inStream = new BufferedInputStream ( new FileInputStream ( inFile ) ); 90 zipStream.putNextEntry( new ZipEntry ( inFile.getName() ) ); 91 int b; 92 while ( (b = inStream.read()) >= 0 ) 93 { 94 zipStream.write( b ); 95 } 96 zipStream.closeEntry(); 97 } 98 catch ( IOException e ) 99 { 100 throw new StoreException( e ); 101 } 102 finally 103 { 104 FileUtils.safeClose( zipStream ); 105 FileUtils.safeClose( inStream ); 106 } 107 } 108 109 115 public void exportNewOrder( WebPageRequest inContext, Store inStore, Order inOrder ) 116 throws StoreException 117 { 118 String zipFileNumber = nextZipFileNumber( inStore ); 119 120 try 121 { 122 exportToOrdersFile( inStore, inOrder,zipFileNumber ); 126 127 } catch ( Exception ex) 128 { 129 if ( ex instanceof StoreException) 130 { 131 throw (StoreException)ex; 132 } 133 throw new StoreException(ex); 134 } 135 } 136 protected void exportToOrdersFile( Store inStore, Order inOrder, String inZipNum ) throws Exception 137 { 138 SimpleDateFormat format = new SimpleDateFormat ("yyyyMMddHHmmssSSS"); 139 140 String filename = "SO_" + format.format(new Date ()) + ".xml"; 141 142 File salesOrdersFile = new File ( getOrdersDirectory( inStore ),filename); 143 Element root = getRootElement( salesOrdersFile, "SALEORDERS" ); 144 Element so = root.addElement( "SO" ); 145 appendHeader( so, inStore, inOrder ); 146 appendItems( so, inStore, inOrder ); 147 appendSubTotals( so, inStore, inOrder ); 148 writeXmlFile( root, salesOrdersFile ); 149 150 File outFile = getOrdersZipFile( inStore, inZipNum ); 151 zipUpFile( salesOrdersFile, outFile); 152 153 File copy = new File ( inStore.getRootDirectory(),"xferorders/" + outFile.getName() ); 154 copy = copy.getAbsoluteFile(); 155 copy.getParentFile().mkdirs(); 156 new OutputFiller().fill(outFile,copy); 157 158 salesOrdersFile.delete(); 159 160 } 161 162 167 private void appendSubTotals( Element inRoot, Store inStore, Order inOrder ) 168 { 169 Element so_subtotal = inRoot.addElement( "SO_SUBTOTALS" ); 170 so_subtotal.addAttribute( "disc_percent", "0" ); 171 so_subtotal.addAttribute( "ship_percent", "0" ); 172 so_subtotal.addAttribute( "disc_amount", "0" ); 173 so_subtotal.addAttribute( "ship_amount", inOrder.getTotalShipping().toShortString() ); 174 so_subtotal.addAttribute( "total_tax", inOrder.getTax().toShortString() ); 175 so_subtotal.addAttribute( "subtotal_used", inOrder.getSubTotal().toShortString() ); 176 so_subtotal.addAttribute( "tax_area", "clermont" ); } 178 179 184 private void appendItems( Element inRoot, Store inStore, Order inOrder ) 185 { 186 int i = -1; 187 for ( Iterator iter = inOrder.getItems().iterator(); iter.hasNext(); ) 188 { 189 i++; 190 CartItem item = (CartItem) iter.next(); 191 192 Element soitem = inRoot.addElement( "SO_ITEM" ); 193 soitem.addAttribute( "item_sid", item.getSku() ); 195 soitem.addAttribute( "item_no", item.getSku() ); 196 soitem.addAttribute( "row_num", String.valueOf( i ) ); 197 soitem.addAttribute( "tax_code", "1" ); 203 soitem.addAttribute( "price", item.getYourPrice().toShortString() ); 204 soitem.addAttribute( "orig_price", item.getProduct().getRetailPrice().toShortString() ); 205 206 soitem.addAttribute( "qty_ordered", String.valueOf( item.getQuantity() ) ); 207 Fraction rate = inOrder.getCustomer().getTaxRate(); 208 Money tax = item.calculateTax( rate ); 209 soitem.addAttribute( "tax_amount", tax.toShortString() ); 210 soitem.addAttribute( "orig_tax_code", "1" ); 211 soitem.addAttribute( "orig_tax_amount", tax.toShortString() ); 212 soitem.addAttribute( "tax_percent", rate.toString() ); 213 221 } 222 } 223 224 260 protected String nextZipFileNumber( Store inStore ) throws StoreException 262 { 263 FileInputStream inStream = null; 264 FileOutputStream outStream = null; 265 try 266 { 267 File orderProperties = getOrderPropertiesFile( inStore ); 268 Properties props = new Properties (); 269 if ( orderProperties.exists() ) 270 { 271 inStream = new FileInputStream ( orderProperties ); 272 props.load( inStream ); 273 } 274 final String ZIP_FILE_COUNT_PROPERTY = "zipFileNumber"; 275 String countString = props.getProperty(ZIP_FILE_COUNT_PROPERTY); 276 int count = 0; 277 if ( countString != null ) 278 { 279 count = Integer.valueOf( countString ).intValue(); 280 } 281 count++; 282 countString = String.valueOf(count); 283 props.setProperty( ZIP_FILE_COUNT_PROPERTY, countString ); 284 outStream = new FileOutputStream ( orderProperties ); 285 props.store( outStream, "Order properties count" ); 286 String zeros = "000000000"; 287 zeros = zeros.substring(countString.length()); 288 return zeros + countString; 289 } 290 catch ( Exception ex) 291 { 292 throw new StoreException(ex); 293 } 294 finally 295 { 296 FileUtils.safeClose(outStream); 297 FileUtils.safeClose(inStream); 298 } 299 } 300 301 protected void appendHeader( Element inRoot, Store inStore, Order inOrder ) throws StoreException 302 { 303 Element headerElem = inRoot.addElement( "SO_HEADER" ); 304 Customer customer = inOrder.getCustomer(); 305 306 headerElem.addAttribute( "order_sid", inOrder.getId().substring(5) ); 308 309 headerElem.addAttribute( "so_number", inOrder.getId() ); 310 headerElem.addAttribute( "so_type", "0" ); 311 headerElem.addAttribute( "date_ordered", DATE_FORMAT.format( inOrder.getDate() ) ); 312 313 saveAddress( inOrder, headerElem, customer, customer.getBillingAddress(), "billto" ); 314 saveAddress( inOrder, headerElem, customer, customer.getShippingAddress(), "shipto" ); 315 316 ShippingMethod method = inOrder.getShippingMethod(); 317 if ( method != null) 318 { 319 headerElem.addAttribute( "shipping_provider", method.getId() ); 320 headerElem.addAttribute( "shipping_method", method.getId() ); 321 } 322 if ( inOrder.getPaymentMethod() != null) 323 { 324 CreditPaymentMethod creditCard = (CreditPaymentMethod) inOrder.getPaymentMethod(); 325 326 headerElem.addAttribute( "cc_type", creditCard.getCreditCardType().getId() ); headerElem.addAttribute( "cc_name", creditCard.getCreditCardType().getName() ); headerElem.addAttribute( "cc_number", creditCard.getCardNumber() ); 335 headerElem.addAttribute( "cc_expire", creditCard.getExpirationMonth() + "/" 336 + creditCard.getExpirationYear() ); 337 } 338 } 339 340 345 private void saveAddress( Order inOrder, Element headerElem, Customer customer, Address inAddress, String inShipTo ) 346 { 347 headerElem.addAttribute( "so_" + inShipTo + "_cust_sid", customer.getUserName() ); headerElem.addAttribute( "so_" + inShipTo + "_date_created", DATE_FORMAT.format( inOrder 351 .getDate() ) ); headerElem.addAttribute( "so_" + inShipTo + "_title", customer.getTitle() ); 354 headerElem.addAttribute( "so_" + inShipTo + "_first_name", customer.getFirstName() ); 355 headerElem.addAttribute( "so_" + inShipTo + "_last_name", customer.getLastName() ); 356 if ( inAddress == null ) 357 { 358 inAddress = customer.getBillingAddress(); 359 } 360 headerElem.addAttribute( "so_" + inShipTo + "_address1", inAddress.getAddress1() ); 361 headerElem.addAttribute( "so_" + inShipTo + "_address2", inAddress.getAddress2() ); 362 headerElem.addAttribute( "so_" + inShipTo + "_city", inAddress.getCity() ); 363 headerElem.addAttribute( "so_" + inShipTo + "_state_or_province", inAddress.getState() ); headerElem.addAttribute( "so_" + inShipTo + "_state_short", inAddress.getState() ); headerElem.addAttribute( "so_" + inShipTo + "_country", inAddress.getCountry() ); headerElem.addAttribute( "so_" + inShipTo + "_country_short", inAddress.getCountry() ); 371 headerElem.addAttribute( "so_" + inShipTo + "_postal_code", inAddress.getZipCode() ); 372 headerElem.addAttribute( "so_" + inShipTo + "_phone1", customer.getPhone1() ); 373 headerElem.addAttribute( "so_" + inShipTo + "_email", customer.getEmail() ); 374 } 375 376 protected File getOrdersZipFile( Store inStore, String inZipFileNumber ) 377 { 378 return new File ( getOrdersDirectory( inStore ), "O" + inZipFileNumber + ".zip" ); 379 } 380 381 382 protected File getOrderPropertiesFile( Store inStore ) 383 { 384 return new File ( getOrdersDirectory( inStore ), "order.properties" ); 385 } 386 } | Popular Tags |