1 5 package com.dotmarketing.portlets.order_manager.action; 6 import java.net.URLDecoder ; 7 import java.util.ArrayList ; 8 import java.util.Date ; 9 import java.util.Enumeration ; 10 import java.util.Hashtable ; 11 import java.util.List ; 12 13 import javax.portlet.ActionRequest; 14 import javax.portlet.ActionResponse; 15 import javax.portlet.PortletConfig; 16 import javax.servlet.http.HttpServletRequest ; 17 import javax.servlet.http.HttpSession ; 18 19 import org.apache.commons.beanutils.BeanUtils; 20 import org.apache.struts.Globals; 21 import org.apache.struts.action.ActionErrors; 22 import org.apache.struts.action.ActionForm; 23 import org.apache.struts.action.ActionMapping; 24 import org.apache.struts.action.ActionMessage; 25 import org.apache.struts.action.ActionMessages; 26 27 import com.dotmarketing.beans.UserProxy; 28 import com.dotmarketing.cms.factories.PublicUserFactory; 29 import com.dotmarketing.db.DotHibernate; 30 import com.dotmarketing.factories.InodeFactory; 31 import com.dotmarketing.factories.UserProxyFactory; 32 import com.dotmarketing.portal.struts.DotPortletAction; 33 import com.dotmarketing.util.Config; 34 import com.dotmarketing.util.Logger; 35 import com.dotmarketing.util.UtilMethods; 36 import com.dotmarketing.util.WebKeys; 37 import com.liferay.portal.ejb.UserLocalManagerUtil; 38 import com.liferay.portal.model.User; 39 import com.liferay.portal.util.Constants; 40 import com.liferay.portlet.ActionRequestImpl; 41 import com.liferay.util.servlet.SessionMessages; 42 import com.dotmarketing.cms.product.model.Holder; 43 import com.dotmarketing.cms.product.model.ShoppingCart; 44 import com.dotmarketing.portlets.discountcode.factories.DiscountCodeFactory; 45 import com.dotmarketing.portlets.discountcode.model.DiscountCode; 46 import com.dotmarketing.portlets.order_manager.factories.OrderFactory; 47 import com.dotmarketing.portlets.order_manager.factories.OrderItemFactory; 48 import com.dotmarketing.portlets.order_manager.model.Order; 49 import com.dotmarketing.portlets.order_manager.model.OrderItem; 50 import com.dotmarketing.portlets.order_manager.struts.OrderForm; 51 import com.dotmarketing.portlets.order_manager.struts.OrderItemForm; 52 import com.dotmarketing.portlets.organization.factories.OrganizationFactory; 53 import com.dotmarketing.portlets.organization.model.Organization; 54 import com.dotmarketing.portlets.product.factories.ProductFormatFactory; 55 import com.dotmarketing.portlets.product.model.ProductFormat; 56 import com.dotmarketing.portlets.product.model.ProductPrice; 57 import com.dotmarketing.util.CalculateShippingAndHandling; 58 import com.dotmarketing.util.UtilMethods; 59 import com.dotmarketing.viewtools.ProductWebAPI; 60 61 62 66 public class EditOrderAction extends DotPortletAction { 67 public void processAction( 68 ActionMapping mapping, ActionForm form, PortletConfig config, 69 ActionRequest req, ActionResponse res) 70 throws Exception { 71 Logger.debug(this,"START VIEW ORDERS ACTION"); 72 String cmd = (req.getParameter(Constants.CMD)!=null)? req.getParameter(Constants.CMD) : ""; 73 String referrer = req.getParameter("referrer"); 74 if (referrer == null) 75 referrer = req.getParameter("referer"); 76 if ((referrer!=null) && (referrer.length()!=0)) 77 { 78 referrer = URLDecoder.decode(referrer,"UTF-8"); 79 } 80 DotHibernate.startTransaction(); 81 82 if ((cmd != null) && cmd.equals("update_shipping")) { 83 try 84 { 85 _updateShipping(mapping,form,config,req,res); 86 setForward(req,"portlet.ext.order_manager.edit_order"); 87 return; 88 } 89 catch (Exception ae) 90 { 91 _handleException(ae, req); 92 } 93 } 94 95 if ((cmd != null) && cmd.equals("update_discounts")) { 96 try 97 { 98 _updateOrderDiscounts(mapping,form,config,req,res); 99 setForward(req,"portlet.ext.order_manager.edit_order"); 100 return; 101 } 102 catch (Exception ae) 103 { 104 _handleException(ae, req); 105 } 106 } 107 108 if ((cmd != null) && cmd.equals(Constants.UPDATE)) { 109 try 110 { 111 _updateOrders(mapping,form,config,req,res); 112 113 _sendToReferral(req,res,referrer); 114 } 115 catch (Exception ae) 116 { 117 _handleException(ae, req); 118 } 119 } 120 else if ((cmd != null) && cmd.equals(Constants.DELETE)) { 121 try { 122 _removeOrder(mapping,form,config,req,res); 123 if (referrer != null){ 125 _sendToReferral(req,res,referrer); 126 return; 127 } 128 } catch (Exception ae) { 129 _handleException(ae, req); 130 } 131 } 132 else if ((cmd != null) && cmd.equals(Constants.EDIT)) 133 { 134 try { 135 _editOrder(mapping,form,config,req,res); 136 setForward(req,"portlet.ext.order_manager.edit_order"); 137 return; 138 } catch (Exception ae) { 139 _handleException(ae, req); 140 } 141 } 142 else if ((cmd != null) && cmd.equals(Constants.SAVE)) 143 { 144 try { 145 OrderForm orderForm = (OrderForm) form; 146 ActionErrors ae = orderForm.validateBackEndEdit(); 147 if (ae.size() == 0) 148 { 149 _saveOrder(req,res,config,form); 150 SessionMessages.add(req, "message", "message.order_manager.order_updated"); 151 setForward(req,"portlet.ext.order_manager.edit_order"); 152 } 154 else 155 { 156 req.setAttribute(Globals.ERROR_KEY, ae); 157 setForward(req,"portlet.ext.order_manager.edit_order"); 158 } 159 return; 160 161 } catch (Exception ae) { 162 _handleException(ae, req); 163 } 164 } 165 else if ((cmd != null) && cmd.equals("deleteitem")) 166 { 167 try { 168 _deleteItem(req,res,config,form); 169 SessionMessages.add(req, "message", "message.order_manager.item_deleted"); 170 if (referrer != null) 171 _sendToReferral(req,res,referrer); 172 else 173 { 174 _refreshOrder(form); 175 setForward(req,"portlet.ext.order_manager.edit_order"); 176 } 177 return; 178 179 } catch (Exception ae) { 180 _handleException(ae, req); 181 } 182 } 183 DotHibernate.commitTransaction(); 184 185 setForward(req,"portlet.ext.order_manager.view"); 186 Logger.debug(this,"END VIEW ORDERS ACTION"); 187 } 188 189 private void _updateShipping(ActionMapping mapping, ActionForm form, PortletConfig config, ActionRequest req, ActionResponse res) { 190 _refreshOrder(form); 191 _loadAmount(req,res,config,form); 192 193 } 194 195 private void _updateOrderDiscounts(ActionMapping mapping, ActionForm form, PortletConfig config, ActionRequest req, ActionResponse res) { 196 _refreshOrder(form); 197 _loadAmount(req,res,config,form); 198 } 199 200 private ShoppingCart createShoppingCart(OrderForm orderForm) { 201 ShoppingCart sp = new ShoppingCart(); 202 List <OrderItemForm> orderItems = orderForm.getOrderItemList(); 203 OrderItemForm orderItem; 204 Holder holder; 205 List <Holder> holders = new ArrayList (); 206 for (int i=0;i<orderItems.size();i++){ 207 orderItem = orderItems.get(i); 208 holder = new Holder(); 209 holder.setQuantity(orderItem.getItemQty()); 210 holder.setFormat(ProductFormatFactory.getProductFormat(orderItem.getProductInode())); 211 holders.add(holder); 212 } 213 sp.setHolders(holders); 214 sp.setDiscountCodes(UtilMethods.getDiscountsByOrder(orderForm)); 215 return sp; 216 } 217 private void _updateOrders(ActionMapping mapping, ActionForm form, PortletConfig config,ActionRequest request, ActionResponse response) 218 { 219 Enumeration parameters = request.getParameterNames(); 220 Hashtable orders = new Hashtable <Long ,Order>(); 221 while(parameters.hasMoreElements()) 222 { 223 long orderInode = 0; 224 int value = 0; 225 Order order = null; 226 String parameterName = (String ) parameters.nextElement(); 227 if (parameterName.indexOf("orderStatus_") != -1 || 228 parameterName.indexOf("paymentStatus_") != -1) 229 { 230 orderInode = Long.parseLong(parameterName.split("_")[1]); 232 if (orders.containsKey(orderInode)) 233 { 234 order = (Order) orders.get(orderInode); 235 } 236 else 237 { 238 order = OrderFactory.getOrderById(Long.toString(orderInode)); 239 orders.put(orderInode,order); 240 } 241 value = Integer.parseInt(request.getParameter(parameterName)); 242 if (parameterName.indexOf("orderStatus") != -1) 243 { 244 order.setOrderStatus(value); 245 } 246 else if (parameterName.indexOf("paymentStatus") != -1) 247 { 248 order.setPaymentStatus(value); 249 } 250 } 251 } 252 Enumeration ordersEnum = orders.keys(); 253 while (ordersEnum.hasMoreElements()) 254 { 255 Order order = (Order) orders.get(ordersEnum.nextElement()); 256 OrderFactory.saveOrder(order); 257 } 258 } 259 260 private void _removeOrder(ActionMapping mapping, ActionForm form, PortletConfig config,ActionRequest request, ActionResponse response) 261 { 262 String orderInode = request.getParameter("inode"); 263 Order order = OrderFactory.getOrderById(orderInode); 264 265 List <OrderItem> orderItems = OrderItemFactory.getAllOrderItemsByParentOrder(order); 266 for(OrderItem orderItem : orderItems) 267 { 268 ProductFormat format = ProductFormatFactory.getProductFormat(orderItem.getProductInode()); 269 if (format.getInode() != 0){ 270 format.setInventoryQuantity(format.getInventoryQuantity() + orderItem.getItemQty()); 271 ProductFormatFactory.saveProductFormat(format); 272 } 273 OrderItemFactory.deleteOrderItem(orderItem); 274 } 275 OrderFactory.deleteOrder(order); 276 } 277 278 private void _editOrder(ActionMapping mapping, ActionForm form, PortletConfig config,ActionRequest request, ActionResponse response) 279 { 280 String orderInode = request.getParameter("inode"); 281 String reload = request.getParameter("reload"); 282 283 284 Order order = OrderFactory.getOrderById(orderInode); 285 User user = order.getUser(); 286 287 if (!UtilMethods.isSet(reload)) 288 { 289 List <OrderItem> orderItems = OrderItemFactory.getAllOrderItemsByParentOrder(order); 290 291 ShoppingCart shoppingCart = new ShoppingCart(); 292 shoppingCart.setInode(order.getInode()); 293 294 295 shoppingCart.setUser(user); 296 UserProxy up = UserProxyFactory.getUserProxy(user); 297 Organization org = (Organization) InodeFactory.getParentOfClass(up, Organization.class); 298 boolean isPartner = ((org.getPartnerKey() != null && !org.getPartnerKey().equals("") )? true : false); 299 List <Holder> holders = shoppingCart.getHolders(); 300 List <DiscountCode> discounts = shoppingCart.getDiscountCodes(); 301 for(OrderItem orderItem : orderItems) 302 { 303 ProductFormat format = ProductFormatFactory.getProductFormat(orderItem.getProductInode()); 304 305 int quantity = orderItem.getItemQty(); 306 float price = orderItem.getItemPrice(); 307 float lineTotal = quantity * price; 308 Holder holder = new Holder(); 309 holder.setFormat(format); 310 holder.setLineTotal(lineTotal); 311 holder.setPrice(price); 312 holder.setQuantity(quantity); 313 holder.setInode(orderItem.getInode()); 314 ProductPrice productPrice = format.getQuantityPrice(quantity,discounts); 315 holders.add(holder); 320 } 321 322 String discountsString = order.getDiscountCodes(); 323 String [] discountsStrings = discountsString.split(":"); 324 for(String discountString: discountsStrings) 325 { 326 discountString = discountString.trim(); 327 if(UtilMethods.isSet(discountString)) 328 { 329 DiscountCode discountCode = DiscountCodeFactory.getDiscountCodeById(discountString); 330 discounts.add(discountCode); 331 } 332 } 333 334 HttpSession session = ((ActionRequestImpl) request).getHttpServletRequest().getSession(); 335 session.setAttribute(WebKeys.SHOPPING_CART,shoppingCart); 336 } 337 338 339 OrderForm orderForm = (OrderForm) form; 340 orderForm.setUserId(user.getUserId()); 341 User backendUser; 342 String backendUserName = ""; 343 try{ 344 backendUser = UserLocalManagerUtil.getUserById(order.getBackendUser()); 345 if (backendUser != null) 346 backendUserName = (backendUser.getFirstName() != null ? backendUser.getFirstName() + " " : "") + (backendUser.getLastName() != null ? backendUser.getLastName() + " " : "") + (backendUser.getEmailAddress() != null ? "(" + backendUser.getEmailAddress() + ")" : ""); 347 } 348 catch(Exception ex){ 349 350 } 351 orderForm.setBackendUserName(backendUserName); 352 357 try 358 { 359 BeanUtils.copyProperties(orderForm,order); 360 361 orderForm.setOrderSubTotal(orderForm.getOrderSubTotal() - orderForm.getOrderDiscount()); 362 String [] paymentTypes = Config.getStringArrayProperty("EREG_PAYMENT_TYPES"); 364 int creditCard = Config.getIntProperty(paymentTypes[0]); 365 int check = Config.getIntProperty(paymentTypes[1]); 366 int paymentType = Integer.parseInt(orderForm.getPaymentType()); 367 if (paymentType == creditCard){ 368 orderForm.setPaymentType("cc"); 369 } 370 else if (paymentType == check){ 371 orderForm.setPaymentType("ch"); 372 } 373 else{ 374 orderForm.setPaymentType("po"); 375 } 376 if (UtilMethods.isSet(reload)) 377 { 378 _loadAmount(request,response,config,orderForm,true); 379 } 380 381 if (!(order.getShippingCountry().equals(Config.getStringProperty("DEFAULT_COUNTRY_CODE")))) 382 { 383 orderForm.setShippingState("otherCountry"); 384 orderForm.setShippingStateOtherCountryText(order.getShippingState()); 385 } 386 if (!(order.getBillingCountry().equals(Config.getStringProperty("DEFAULT_COUNTRY_CODE")))) 387 { 388 orderForm.setBillingState("otherCountry"); 389 orderForm.setBillingStateOtherCountryText(order.getBillingState()); 390 } 391 UserProxy orderUser = UserProxyFactory.getUserProxy(order.getUserInode()); 393 user = PublicUserFactory.getUserByUserId(orderUser.getUserId()); 394 orderForm.setContactName(user.getFullName()!=null?user.getFullName():""); 395 orderForm.setContactEmail(user.getEmailAddress()!=null?user.getEmailAddress():""); 396 if (orderUser.getInode()>0) { 397 Organization organization = (Organization) InodeFactory.getParentOfClass(orderUser,Organization.class); 398 if (organization.getInode()>0) { 399 orderForm.setContactFacility(organization.getTitle().trim()); 400 Organization parentSystem = OrganizationFactory.getParentOrganization(organization); 401 if (parentSystem.getInode()>0) { 402 orderForm.setContactSystem(parentSystem.getTitle().trim()); 403 } 404 } 405 } 406 407 } 408 catch(Exception ex) 409 { 410 Logger.debug(this,ex.toString()); 411 } 412 } 413 414 private void _refreshOrder(ActionForm form) 415 { 416 OrderForm orderForm = (OrderForm) form; 417 Order order = OrderFactory.getOrderById(String.valueOf(orderForm.getInode())); 418 User user = order.getUser(); 419 orderForm.setUserId(user.getUserId()); 420 } 421 422 public void _saveOrder(ActionRequest request,ActionResponse response,PortletConfig config,ActionForm form) 423 { 424 HttpServletRequest httpServletRequest = ((ActionRequestImpl) request).getHttpServletRequest(); 425 HttpSession session = httpServletRequest.getSession(); 426 ShoppingCart shoppingCart = (ShoppingCart) session.getAttribute(WebKeys.SHOPPING_CART); 427 OrderForm orderForm = (OrderForm) form; 428 _refreshOrder(form); 429 boolean changeTotal = _loadAmount(request,response,config,form); 430 DotHibernate.startTransaction(); 431 try 432 { 433 session.setAttribute(WebKeys.SHOPPING_CART_ERRORS,null); 434 session.setAttribute(WebKeys.SHOPPING_CART_ORDER_FORM,null); 435 436 User user = (User) shoppingCart.getUser(); 437 UserProxy userProxy = UserProxyFactory.getUserProxy(user); 438 Date now = new Date (); 439 440 String orderInode = Long.toString(shoppingCart.getInode()); 442 Order order = OrderFactory.getOrderById(orderInode); 443 order.setUserInode(userProxy.getInode()); 444 order.setLastModDate(now); 446 order.setInvoiceNumber(orderForm.getInvoiceNumber()); 447 order.setBillingAddress1(orderForm.getBillingAddress1()); 449 order.setBillingAddress2(orderForm.getBillingAddress2()); 450 order.setBillingCity(orderForm.getBillingCity()); 451 if (orderForm.getBillingState().equals("otherCountry")) 452 order.setBillingState(orderForm.getBillingStateOtherCountryText()); 453 else 454 order.setBillingState(orderForm.getBillingState()); 455 order.setBillingZip(orderForm.getBillingZip()); 456 order.setBillingCountry(orderForm.getBillingCountry()); 457 if (orderForm.getBillingPhone() != null) 458 order.setBillingPhone(orderForm.getBillingPhone()); 459 order.setBillingFax(orderForm.getBillingFax()); 460 order.setBillingContactName(orderForm.getBillingContactName()); 461 order.setBillingContactPhone(orderForm.getBillingContactPhone()); 462 order.setBillingContactEmail(orderForm.getBillingContactEmail()); 463 order.setShippingAddress1(orderForm.getShippingAddress1()); 465 order.setShippingAddress2(orderForm.getShippingAddress2()); 466 order.setShippingLabel(orderForm.getShippingLabel()); 467 order.setShippingCity(orderForm.getShippingCity()); 468 if (orderForm.getShippingState().equals("otherCountry")) 469 order.setShippingState(orderForm.getShippingStateOtherCountryText()); 470 else 471 order.setShippingState(orderForm.getShippingState()); 472 order.setShippingZip(orderForm.getShippingZip()); 473 order.setShippingCountry(orderForm.getShippingCountry()); 474 if (orderForm.getShippingPhone() != null) 475 order.setShippingPhone(orderForm.getShippingPhone()); 476 order.setShippingFax(orderForm.getShippingFax()); 477 if (orderForm.getPaymentType().equals("cc")) 481 { 482 order.setNameOnCard(orderForm.getNameOnCard()); 483 String cardType = orderForm.getCardType(); 484 String [] creditCardTypes = Config.getStringArrayProperty("EREG_CREDIT_CARD_TYPES"); 485 if (cardType.equals("vs")) 486 { 487 cardType = creditCardTypes[0]; 488 } 489 else if (cardType.equals("mc")) 490 { 491 cardType = creditCardTypes[1]; 492 } 493 else if (cardType.equals("dc")) 494 { 495 cardType = creditCardTypes[2]; 496 } 497 else if (cardType.equals("ae")) 498 { 499 cardType = creditCardTypes[3]; 500 } 501 order.setCardType(cardType); 502 order.setCardNumber(UtilMethods.obfuscateCreditCard(orderForm.getCardNumber())); order.setCardExpMonth(orderForm.getCardExpMonth()); 504 order.setCardExpYear(orderForm.getCardExpYear()); 505 order.setCardVerificationValue(""); order.setPaymentType(Config.getStringProperty("ECOM_CREDIT_CARD")); 507 } 508 else if (orderForm.getPaymentType().equals("ch")) 509 { 510 order.setCheckNumber(orderForm.getCheckNumber()); 512 order.setCheckBankName(orderForm.getCheckBankName()); 513 order.setPaymentType(Config.getStringProperty("ECOM_CHECK")); 514 } 515 else if (orderForm.getPaymentType().equals("po")) 516 { 517 order.setPoNumber(orderForm.getPoNumber()); 519 order.setPaymentType(Config.getStringProperty("ECOM_PURCHASE_ORDER")); 520 } 521 522 order.setOrderTotalPaid(orderForm.getOrderTotalPaid()); 523 float totalDue = orderForm.getOrderTotal() - orderForm.getOrderTotalPaid(); 524 order.setOrderTotalDue(totalDue); 525 526 int paymentStatus = 0; 527 if (orderForm.getPaymentStatus() == Config.getIntProperty("ECOM_PAY_INVOICED")) 528 order.setPaymentStatus(Config.getIntProperty("ECOM_PAY_INVOICED")); 529 else{ 530 if (changeTotal) 531 { 532 if (totalDue > 0) 533 { 534 paymentStatus = Config.getIntProperty("ECOM_PAY_WAITING"); 535 } 536 else if (totalDue < 0) 537 { 538 paymentStatus = Config.getIntProperty("ECOM_PAY_REIMBURSEMENT"); 539 } 540 else 541 { 542 paymentStatus = Config.getIntProperty("ECOM_PAY_PAID"); 543 } 544 } 545 else 546 { 547 paymentStatus = orderForm.getPaymentStatus(); 548 } 549 order.setPaymentStatus(paymentStatus); 550 } 551 int orderStatus = orderForm.getOrderStatus(); 552 order.setOrderStatus(orderStatus); 553 554 order.setTaxExemptNumber(orderForm.getTaxExemptNumber()); 556 order.setOrderSubTotal(orderForm.getOrderSubTotal()); 558 order.setOrderDiscount(orderForm.getOrderDiscount()); 559 order.setOrderShipping(orderForm.getOrderShipping()); 560 order.setOrderShipType(orderForm.getOrderShipType()); 561 order.setOrderTax(orderForm.getOrderTax()); 562 order.setOrderTotal(orderForm.getOrderTotal()); 563 order.setModified_QB(true); 564 order.setModified_FH(true); 565 List <DiscountCode> discounts = shoppingCart.getDiscountCodes(); 567 String discountCodes = ""; 568 for(DiscountCode discount : discounts) 569 { 570 discountCodes += discount.getCodeId() + ":"; 571 } 572 if (UtilMethods.isSet(discountCodes)) 573 { 574 discountCodes = discountCodes.substring(0,discountCodes.lastIndexOf(":")); 575 } 576 order.setDiscountCodes(discountCodes); 577 578 OrderFactory.saveOrder(order); 580 orderForm.setInode(order.getInode()); 581 583 int itemQuantity; 585 int inventoryQuantity; 586 List <Holder> holders = shoppingCart.getHolders(); 587 for(Holder holder : holders) 588 { 589 ProductFormat format = holder.getFormat(); 590 OrderItem orderItem = new OrderItem(); 591 itemQuantity = 0; 592 if (holder.getInode() != 0) 593 { 594 orderItem = OrderItemFactory.GetOrderItemById(holder.getInode()); 595 itemQuantity = orderItem.getItemQty(); 596 } 597 orderItem.setOrderInode(order.getInode()); 598 orderItem.setProductInode(format.getInode()); 599 orderItem.setItemQty(holder.getQuantity()); 600 orderItem.setItemPrice(holder.getPrice()); 601 OrderItemFactory.saveOrderItem(orderItem); 602 holder.setInode(orderItem.getInode()); 603 if (holder.getQuantity() != itemQuantity){ 604 inventoryQuantity = format.getInventoryQuantity(); 605 format.setInventoryQuantity(inventoryQuantity + itemQuantity - holder.getQuantity()); 606 ProductFormatFactory.saveProductFormat(format); 607 } 608 } 609 610 session.setAttribute(WebKeys.SHOPPING_CART_ORDER_FORM,form); 611 DotHibernate.commitTransaction(); 612 try 614 { 615 629 } 630 catch(Exception ex) 631 { 632 Logger.debug(this,ex.toString()); 633 } 634 setForward(request,"portlet.ext.order_manager.view_invoice"); 635 } 636 catch (Exception e) 637 { 638 Logger.error(this,"Unknown Error placing a credit card order: "+ e.getMessage(), e); 640 DotHibernate.rollbackTransaction(); 641 ActionMessages ae = new ActionMessages(); 642 ae.add(Globals.ERROR_KEY, new ActionMessage("error.cc_processing.unknown")); 643 saveErrors(httpServletRequest, ae); 645 session.setAttribute(WebKeys.SHOPPING_CART_ERRORS, ae); 646 generateToken(httpServletRequest); 648 saveToken(httpServletRequest); 649 setForward(request,"portlet.ext.order_manager.view_invoice"); 651 } 652 } 653 private boolean _loadAmount(ActionRequest request,ActionResponse response,PortletConfig config,ActionForm form) 654 { 655 String changeQualityString = request.getParameter("changeQuantity"); 656 boolean changeQuantity = (UtilMethods.isSet(changeQualityString) ? Boolean.parseBoolean(changeQualityString) : false); 657 return _loadAmount( request, response, config, form,changeQuantity); 658 } 659 660 private boolean _loadAmount(ActionRequest request,ActionResponse response,PortletConfig config,ActionForm form,boolean changeQuantity) 661 { 662 HttpSession session = ((ActionRequestImpl)request).getHttpServletRequest().getSession(); 663 ShoppingCart shoppingCart = (ShoppingCart)session.getAttribute(WebKeys.SHOPPING_CART); 664 boolean changeTotal = false; 666 667 String changeBillingAddressString = request.getParameter("changeBillingAddress"); 668 String changeShippingAddressString = request.getParameter("changeShippingAddress"); 669 String changeTaxExceptionNumberString = request.getParameter("changeTaxExceptionNumber"); 670 String changeDiscountString = request.getParameter("changeDiscount"); 671 String changeShippingTypeString = request.getParameter("changeShippingType"); 672 673 boolean changeBillingAddress = (UtilMethods.isSet(changeBillingAddressString) ? Boolean.parseBoolean(changeBillingAddressString) : false); 674 boolean changeShippingAddress = (UtilMethods.isSet(changeShippingAddressString) ? Boolean.parseBoolean(changeShippingAddressString) : false); 675 boolean changeTaxExceptionNumber = (UtilMethods.isSet(changeTaxExceptionNumberString) ? Boolean.parseBoolean(changeTaxExceptionNumberString) : false); 676 boolean changeDiscount = (UtilMethods.isSet(changeDiscountString) ? Boolean.parseBoolean(changeDiscountString) : false); 677 boolean changeShippingType = (UtilMethods.isSet(changeShippingTypeString) ? Boolean.parseBoolean(changeShippingTypeString) : false); 678 if(changeQuantity) 680 { 681 _saveShoppingCart(form,config,request,response); 682 } 683 _saveDiscounts(form,config,request,response); 685 686 User userSession = shoppingCart.getUser(); 687 OrderForm orderForm = (OrderForm) form; 688 690 String userId = userSession.getUserId(); 691 692 UserProxy up = UserProxyFactory.getUserProxy(userId); 695 Organization org = (Organization) InodeFactory.getParentOfClass(up, Organization.class); 696 boolean isPartner = ((org.getPartnerKey() != null && !org.getPartnerKey().equals("") )? true : false); 697 698 List <Holder> holders = shoppingCart.getHolders(); 699 List <DiscountCode> discounts = shoppingCart.getDiscountCodes(); 700 float orderSubTotal = 0F; 702 for(Holder holder : holders) 703 { 704 ProductFormat format = holder.getFormat(); 705 int quantity = holder.getQuantity(); 706 float price = 0; 707 if (holder.getInode() != 0) 708 { 709 price = holder.getPrice(); 710 } 711 else 712 { 713 try 714 { 715 ProductPrice productPrice = format.getQuantityPrice(quantity,discounts); 716 price = (isPartner) ? productPrice.getPartnerPrice() : productPrice.getRetailPrice(); 717 } 718 catch(Exception ex) 719 { 720 Logger.debug(ex.toString()); 721 price = Float.MAX_VALUE; 722 } 723 holder.setPrice(price); 724 changeQuantity = true; 725 } 726 float lineTotal = quantity * price; 727 orderSubTotal += lineTotal; 728 holder.setLineTotal(lineTotal); 729 } 730 orderForm.setOrderSubTotal(orderSubTotal); 731 732 float orderDiscount = 0; 734 ProductWebAPI pwa = new ProductWebAPI(); 735 orderDiscount = pwa.getTotalApplicableDiscount(holders,discounts,isPartner); 736 orderForm.setOrderDiscount(orderDiscount); 737 738 float orderSubTotalDiscount = orderSubTotal - orderDiscount; 740 orderSubTotalDiscount = (orderSubTotalDiscount < 0 ? 0 : orderSubTotalDiscount); 741 orderForm.setOrderSubTotalDiscount(orderSubTotalDiscount); 743 request.setAttribute("orderSubTotal",String.valueOf(orderSubTotalDiscount)); 744 float orderTax = orderForm.getOrderTax(); 746 if(changeQuantity || changeBillingAddress || changeTaxExceptionNumber) 747 { 748 if (orderForm.getBillingState().equals("FL") && !UtilMethods.isSet(orderForm.getTaxExemptNumber())) 749 { 750 orderTax = ((float) Math.round(orderSubTotalDiscount * 6.5)) / ((float) 100); 751 } 752 else 753 { 754 orderTax = 0; 755 } 756 orderForm.setOrderTax(orderTax); 757 request.setAttribute("orderTax",String.valueOf(orderTax)); 758 } 759 760 float orderShippingValue = orderForm.getOrderShipping(); 762 if(changeQuantity || changeShippingAddress || changeShippingType) 763 { 764 orderShippingValue = CalculateShippingAndHandling.calculateShippingAndHandling(shoppingCart,orderForm.getOrderShipType() , orderForm.getShippingZip()); 765 orderForm.setOrderShipping(orderShippingValue); 766 request.setAttribute("orderShipping",String.valueOf(orderShippingValue)); 767 768 } 769 770 if (changeQuantity || changeBillingAddress || changeBillingAddress || changeTaxExceptionNumber || changeDiscount || changeShippingType) 772 { 773 float orderTotal = orderSubTotalDiscount + orderTax + orderShippingValue; 774 orderTotal = Math.round(orderTotal * 100) / (float) 100; 775 orderForm.setOrderTotal(orderTotal); 776 changeTotal = true; 777 request.setAttribute("orderTotal",String.valueOf(orderTotal)); 778 } 779 return changeTotal; 780 } 781 782 private void _saveShoppingCart(ActionForm form, PortletConfig config,ActionRequest request, ActionResponse response) 783 { 784 HttpSession session = ((ActionRequestImpl)request).getHttpServletRequest().getSession(); 785 ShoppingCart shoppingCart = (ShoppingCart)session.getAttribute(WebKeys.SHOPPING_CART); 786 787 Enumeration parameters = request.getParameterNames(); 788 while(parameters.hasMoreElements()) 789 { 790 long HolderInode = 0; 791 int quantity = 0; 792 String parameterName = (String ) parameters.nextElement(); 793 if (parameterName.indexOf("holder") != -1) 794 { 795 try 797 { 798 HolderInode = Long.parseLong(parameterName.split("_")[1]); 799 quantity = Integer.parseInt(request.getParameter(parameterName)); 800 shoppingCart.setQuantityItemByHolderInode(quantity,HolderInode); 801 } 802 catch(Exception ex) 803 { 804 Logger.debug(this,ex.toString()); 805 } 806 } 807 } 808 } 809 810 private void _saveDiscounts(ActionForm form, PortletConfig config,ActionRequest request, ActionResponse response) 811 { 812 HttpSession session = ((ActionRequestImpl)request).getHttpServletRequest().getSession(); 813 ShoppingCart shoppingCart = (ShoppingCart)session.getAttribute(WebKeys.SHOPPING_CART); 814 shoppingCart.getDiscountCodes().clear(); 815 816 OrderForm orderForm = (OrderForm) form; 817 String discountsString = orderForm.getDiscountCodes(); 818 String [] discountIds = discountsString.split(":"); 819 for(int i = 0;i < discountIds.length;i++) 820 { 821 String discountId = discountIds[i]; 822 DiscountCode discountCode = DiscountCodeFactory.getDiscountCodeById(discountId); 823 if(discountCode.getInode() != 0) 824 { 825 shoppingCart.addDiscount(discountCode); 826 } 827 } 828 } 829 830 private void _deleteItem(ActionRequest req,ActionResponse res,PortletConfig config,ActionForm form) 831 { 832 String formatInodeString = req.getParameter("formatInode"); 833 long formatInode = Long.parseLong(formatInodeString); 834 835 HttpSession session = ((ActionRequestImpl)req).getHttpServletRequest().getSession(); 836 ShoppingCart shoppingCart = (ShoppingCart)session.getAttribute(WebKeys.SHOPPING_CART); 837 838 Holder holder = shoppingCart.findHolder(formatInode); 839 ProductFormat format = holder.getFormat(); 840 if (holder.getInode() != 0) 841 { 842 OrderItem orderItem = OrderItemFactory.GetOrderItemById(holder.getInode()); 843 format.setInventoryQuantity(format.getInventoryQuantity() + orderItem.getItemQty()); 844 OrderItemFactory.deleteOrderItem(orderItem); 845 ProductFormatFactory.saveProductFormat(format); 846 } 847 shoppingCart.removeItem(formatInode); 848 } 849 } 850 | Popular Tags |