001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.shopping.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.kernel.workflow.WorkflowConstants;
022    import com.liferay.portal.model.User;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portal.util.PortletKeys;
025    import com.liferay.portal.util.PropsValues;
026    import com.liferay.portal.util.SubscriptionSender;
027    import com.liferay.portlet.shopping.BillingCityException;
028    import com.liferay.portlet.shopping.BillingCountryException;
029    import com.liferay.portlet.shopping.BillingEmailAddressException;
030    import com.liferay.portlet.shopping.BillingFirstNameException;
031    import com.liferay.portlet.shopping.BillingLastNameException;
032    import com.liferay.portlet.shopping.BillingPhoneException;
033    import com.liferay.portlet.shopping.BillingStateException;
034    import com.liferay.portlet.shopping.BillingStreetException;
035    import com.liferay.portlet.shopping.BillingZipException;
036    import com.liferay.portlet.shopping.CCExpirationException;
037    import com.liferay.portlet.shopping.CCNameException;
038    import com.liferay.portlet.shopping.CCNumberException;
039    import com.liferay.portlet.shopping.CCTypeException;
040    import com.liferay.portlet.shopping.CartMinOrderException;
041    import com.liferay.portlet.shopping.ShippingCityException;
042    import com.liferay.portlet.shopping.ShippingCountryException;
043    import com.liferay.portlet.shopping.ShippingEmailAddressException;
044    import com.liferay.portlet.shopping.ShippingFirstNameException;
045    import com.liferay.portlet.shopping.ShippingLastNameException;
046    import com.liferay.portlet.shopping.ShippingPhoneException;
047    import com.liferay.portlet.shopping.ShippingStateException;
048    import com.liferay.portlet.shopping.ShippingStreetException;
049    import com.liferay.portlet.shopping.ShippingZipException;
050    import com.liferay.portlet.shopping.model.ShoppingCart;
051    import com.liferay.portlet.shopping.model.ShoppingCartItem;
052    import com.liferay.portlet.shopping.model.ShoppingItem;
053    import com.liferay.portlet.shopping.model.ShoppingItemField;
054    import com.liferay.portlet.shopping.model.ShoppingOrder;
055    import com.liferay.portlet.shopping.model.ShoppingOrderConstants;
056    import com.liferay.portlet.shopping.model.ShoppingOrderItem;
057    import com.liferay.portlet.shopping.model.impl.ShoppingCartItemImpl;
058    import com.liferay.portlet.shopping.service.base.ShoppingOrderLocalServiceBaseImpl;
059    import com.liferay.portlet.shopping.util.ShoppingPreferences;
060    import com.liferay.portlet.shopping.util.ShoppingUtil;
061    import com.liferay.portlet.shopping.util.comparator.OrderDateComparator;
062    import com.liferay.util.CreditCard;
063    import com.liferay.util.PwdGenerator;
064    
065    import java.util.Currency;
066    import java.util.Date;
067    import java.util.List;
068    import java.util.Map;
069    
070    /**
071     * @author Brian Wing Shun Chan
072     */
073    public class ShoppingOrderLocalServiceImpl
074            extends ShoppingOrderLocalServiceBaseImpl {
075    
076            public ShoppingOrder addLatestOrder(long userId, long groupId)
077                    throws PortalException, SystemException {
078    
079                    // Order
080    
081                    User user = userPersistence.findByPrimaryKey(userId);
082                    Date now = new Date();
083    
084                    String number = getNumber();
085    
086                    ShoppingOrder order = null;
087    
088                    long orderId = counterLocalService.increment();
089    
090                    List<ShoppingOrder> pastOrders =
091                            shoppingOrderPersistence.findByG_U_PPPS(
092                                    groupId, userId, ShoppingOrderConstants.STATUS_CHECKOUT, 0, 1);
093    
094                    if (pastOrders.size() > 0) {
095                            ShoppingOrder pastOrder = pastOrders.get(0);
096    
097                            order = shoppingOrderPersistence.create(orderId);
098    
099                            order.setBillingCompany(pastOrder.getBillingCompany());
100                            order.setBillingStreet(pastOrder.getBillingStreet());
101                            order.setBillingCity(pastOrder.getBillingCity());
102                            order.setBillingState(pastOrder.getBillingState());
103                            order.setBillingZip(pastOrder.getBillingZip());
104                            order.setBillingCountry(pastOrder.getBillingCountry());
105                            order.setBillingPhone(pastOrder.getBillingPhone());
106                            order.setShipToBilling(pastOrder.isShipToBilling());
107                            order.setShippingCompany(pastOrder.getShippingCompany());
108                            order.setShippingStreet(pastOrder.getShippingStreet());
109                            order.setShippingCity(pastOrder.getShippingCity());
110                            order.setShippingState(pastOrder.getShippingState());
111                            order.setShippingZip(pastOrder.getShippingZip());
112                            order.setShippingCountry(pastOrder.getShippingCountry());
113                            order.setShippingPhone(pastOrder.getShippingPhone());
114                    }
115                    else {
116                            order = shoppingOrderPersistence.create(orderId);
117                    }
118    
119                    order.setGroupId(groupId);
120                    order.setCompanyId(user.getCompanyId());
121                    order.setUserId(user.getUserId());
122                    order.setUserName(user.getFullName());
123                    order.setCreateDate(now);
124                    order.setModifiedDate(now);
125                    order.setNumber(number);
126                    order.setBillingFirstName(user.getFirstName());
127                    order.setBillingLastName(user.getLastName());
128                    order.setBillingEmailAddress(user.getEmailAddress());
129                    order.setShippingFirstName(user.getFirstName());
130                    order.setShippingLastName(user.getLastName());
131                    order.setShippingEmailAddress(user.getEmailAddress());
132                    order.setCcName(user.getFullName());
133                    order.setPpPaymentStatus(ShoppingOrderConstants.STATUS_LATEST);
134                    order.setSendOrderEmail(true);
135                    order.setSendShippingEmail(true);
136    
137                    shoppingOrderPersistence.update(order);
138    
139                    // Message boards
140    
141                    if (PropsValues.SHOPPING_ORDER_COMMENTS_ENABLED) {
142                            mbMessageLocalService.addDiscussionMessage(
143                                    userId, order.getUserName(), groupId,
144                                    ShoppingOrder.class.getName(), orderId,
145                                    WorkflowConstants.ACTION_PUBLISH);
146                    }
147    
148                    return order;
149            }
150    
151            public void completeOrder(
152                            String number, String ppTxnId, String ppPaymentStatus,
153                            double ppPaymentGross, String ppReceiverEmail, String ppPayerEmail,
154                            boolean updateInventory, ServiceContext serviceContext)
155                    throws PortalException, SystemException {
156    
157                    // Order
158    
159                    ShoppingOrder order = shoppingOrderPersistence.findByNumber(number);
160    
161                    order.setModifiedDate(new Date());
162                    order.setPpTxnId(ppTxnId);
163                    order.setPpPaymentStatus(ppPaymentStatus);
164                    order.setPpPaymentGross(ppPaymentGross);
165                    order.setPpReceiverEmail(ppReceiverEmail);
166                    order.setPpPayerEmail(ppPayerEmail);
167    
168                    shoppingOrderPersistence.update(order);
169    
170                    // Inventory
171    
172                    if (updateInventory &&
173                            ppPaymentStatus.equals(ShoppingOrderConstants.STATUS_COMPLETED)) {
174    
175                            List<ShoppingOrderItem> orderItems =
176                                    shoppingOrderItemLocalService.getOrderItems(order.getOrderId());
177    
178                            for (ShoppingOrderItem orderItem : orderItems) {
179                                    ShoppingItem item = shoppingItemLocalService.getItem(
180                                            ShoppingUtil.getItemId(orderItem.getItemId()));
181    
182                                    if (!item.isFields()) {
183                                            int quantity =
184                                                    item.getStockQuantity() - orderItem.getQuantity();
185    
186                                            item.setStockQuantity(quantity);
187                                    }
188                                    else {
189                                            List<ShoppingItemField> itemFields =
190                                                    shoppingItemFieldLocalService.getItemFields(
191                                                            item.getItemId());
192    
193                                            ShoppingItemField[] itemFieldsArray = itemFields.toArray(
194                                                    new ShoppingItemField[itemFields.size()]);
195    
196                                            String[] fieldsArray = ShoppingCartItemImpl.getFieldsArray(
197                                                    ShoppingUtil.getItemFields(orderItem.getItemId()));
198    
199                                            int rowPos = ShoppingUtil.getFieldsQuantitiesPos(
200                                                    item, itemFieldsArray, fieldsArray);
201    
202                                            String[] fieldsQuantities = item.getFieldsQuantitiesArray();
203    
204                                            try {
205                                                    int quantity =
206                                                            GetterUtil.getInteger(fieldsQuantities[rowPos]) -
207                                                            orderItem.getQuantity();
208    
209                                                    fieldsQuantities[rowPos] = String.valueOf(quantity);
210    
211                                                    item.setFieldsQuantitiesArray(fieldsQuantities);
212                                            }
213                                            catch (Exception e) {
214                                            }
215                                    }
216    
217                                    shoppingItemPersistence.update(item);
218                            }
219                    }
220    
221                    // Email
222    
223                    sendEmail(order, "confirmation", serviceContext);
224            }
225    
226            public void deleteOrder(long orderId)
227                    throws PortalException, SystemException {
228    
229                    ShoppingOrder order = shoppingOrderPersistence.findByPrimaryKey(
230                            orderId);
231    
232                    deleteOrder(order);
233            }
234    
235            public void deleteOrder(ShoppingOrder order)
236                    throws PortalException, SystemException {
237    
238                    // Order
239    
240                    shoppingOrderPersistence.remove(order);
241    
242                    // Subscriptions
243    
244                    subscriptionLocalService.deleteSubscriptions(
245                            order.getCompanyId(), ShoppingOrder.class.getName(),
246                            order.getOrderId());
247    
248                    // Items
249    
250                    shoppingOrderItemPersistence.removeByOrderId(order.getOrderId());
251    
252                    // Message boards
253    
254                    mbMessageLocalService.deleteDiscussionMessages(
255                            ShoppingOrder.class.getName(), order.getOrderId());
256            }
257    
258            public void deleteOrders(long groupId)
259                    throws PortalException, SystemException {
260    
261                    List<ShoppingOrder> orders = shoppingOrderPersistence.findByGroupId(
262                            groupId);
263    
264                    for (ShoppingOrder order : orders) {
265                            deleteOrder(order);
266                    }
267            }
268    
269            public ShoppingOrder getLatestOrder(long userId, long groupId)
270                    throws PortalException, SystemException {
271    
272                    List<ShoppingOrder> orders = shoppingOrderPersistence.findByG_U_PPPS(
273                            groupId, userId, ShoppingOrderConstants.STATUS_LATEST, 0, 1);
274    
275                    ShoppingOrder order = null;
276    
277                    if (orders.size() == 1) {
278                            order = orders.get(0);
279                    }
280                    else {
281                            order = shoppingOrderLocalService.addLatestOrder(userId, groupId);
282                    }
283    
284                    return order;
285            }
286    
287            public ShoppingOrder getOrder(long orderId)
288                    throws PortalException, SystemException {
289    
290                    return shoppingOrderPersistence.findByPrimaryKey(orderId);
291            }
292    
293            public ShoppingOrder getOrder(String number)
294                    throws PortalException, SystemException {
295    
296                    return shoppingOrderPersistence.findByNumber(number);
297            }
298    
299            public ShoppingOrder getPayPalTxnIdOrder(String ppTxnId)
300                    throws PortalException, SystemException {
301    
302                    return shoppingOrderPersistence.findByPPTxnId(ppTxnId);
303            }
304    
305            public ShoppingOrder saveLatestOrder(ShoppingCart cart)
306                    throws PortalException, SystemException {
307    
308                    Map<ShoppingCartItem, Integer> items = cart.getItems();
309                    Date now = new Date();
310    
311                    ShoppingPreferences shoppingPrefs = ShoppingPreferences.getInstance(
312                            cart.getCompanyId(), cart.getGroupId());
313    
314                    if (!ShoppingUtil.meetsMinOrder(shoppingPrefs, items)) {
315                            throw new CartMinOrderException();
316                    }
317    
318                    ShoppingOrder order = getLatestOrder(
319                            cart.getUserId(), cart.getGroupId());
320    
321                    order.setCreateDate(now);
322                    order.setModifiedDate(now);
323                    order.setPpPaymentStatus(ShoppingOrderConstants.STATUS_CHECKOUT);
324    
325                    shoppingOrderPersistence.update(order);
326    
327                    boolean requiresShipping = false;
328    
329                    for (Map.Entry<ShoppingCartItem, Integer> entry : items.entrySet()) {
330                            ShoppingCartItem cartItem = entry.getKey();
331                            Integer count = entry.getValue();
332    
333                            ShoppingItem item = cartItem.getItem();
334    
335                            if (item.isRequiresShipping()) {
336                                    requiresShipping = true;
337                            }
338    
339                            long orderItemId = counterLocalService.increment();
340    
341                            ShoppingOrderItem orderItem = shoppingOrderItemPersistence.create(
342                                    orderItemId);
343    
344                            orderItem.setOrderId(order.getOrderId());
345                            orderItem.setItemId(cartItem.getCartItemId());
346                            orderItem.setSku(item.getSku());
347                            orderItem.setName(item.getName());
348                            orderItem.setDescription(item.getDescription());
349                            orderItem.setProperties(item.getProperties());
350                            orderItem.setPrice(
351                                    ShoppingUtil.calculateActualPrice(item, count.intValue()) /
352                                            count.intValue());
353                            orderItem.setQuantity(count.intValue());
354    
355                            shoppingOrderItemPersistence.update(orderItem);
356                    }
357    
358                    order.setModifiedDate(new Date());
359                    order.setTax(ShoppingUtil.calculateTax(items, order.getBillingState()));
360                    order.setShipping(
361                            ShoppingUtil.calculateAlternativeShipping(
362                                    items, cart.getAltShipping()));
363                    order.setAltShipping(
364                            shoppingPrefs.getAlternativeShippingName(cart.getAltShipping()));
365                    order.setRequiresShipping(requiresShipping);
366                    order.setInsure(cart.isInsure());
367                    order.setInsurance(ShoppingUtil.calculateInsurance(items));
368                    order.setCouponCodes(cart.getCouponCodes());
369                    order.setCouponDiscount(
370                            ShoppingUtil.calculateCouponDiscount(
371                                    items, order.getBillingState(), cart.getCoupon()));
372                    order.setSendOrderEmail(true);
373                    order.setSendShippingEmail(true);
374    
375                    shoppingOrderPersistence.update(order);
376    
377                    return order;
378            }
379    
380            public List<ShoppingOrder> search(
381                            long groupId, long companyId, long userId, String number,
382                            String billingFirstName, String billingLastName,
383                            String billingEmailAddress, String shippingFirstName,
384                            String shippingLastName, String shippingEmailAddress,
385                            String ppPaymentStatus, boolean andOperator, int start, int end)
386                    throws SystemException {
387    
388                    OrderDateComparator obc = new OrderDateComparator(false);
389    
390                    return shoppingOrderFinder.findByG_C_U_N_PPPS(
391                            groupId, companyId, userId, number, billingFirstName,
392                            billingLastName, billingEmailAddress, shippingFirstName,
393                            shippingLastName, shippingEmailAddress, ppPaymentStatus,
394                            andOperator, start, end, obc);
395            }
396    
397            public int searchCount(
398                            long groupId, long companyId, long userId, String number,
399                            String billingFirstName, String billingLastName,
400                            String billingEmailAddress, String shippingFirstName,
401                            String shippingLastName, String shippingEmailAddress,
402                            String ppPaymentStatus, boolean andOperator)
403                    throws SystemException {
404    
405                    return shoppingOrderFinder.countByG_C_U_N_PPPS(
406                            groupId, companyId, userId, number, billingFirstName,
407                            billingLastName, billingEmailAddress, shippingFirstName,
408                            shippingLastName, shippingEmailAddress, ppPaymentStatus,
409                            andOperator);
410            }
411    
412            public void sendEmail(
413                            long orderId, String emailType, ServiceContext serviceContext)
414                    throws PortalException, SystemException {
415    
416                    ShoppingOrder order = shoppingOrderPersistence.findByPrimaryKey(
417                            orderId);
418    
419                    sendEmail(order, emailType, serviceContext);
420            }
421    
422            public void sendEmail(
423                            ShoppingOrder order, String emailType,
424                            ServiceContext serviceContext)
425                    throws PortalException, SystemException {
426    
427                    ShoppingPreferences shoppingPrefs = ShoppingPreferences.getInstance(
428                            order.getCompanyId(), order.getGroupId());
429    
430                    if (emailType.equals("confirmation") &&
431                            shoppingPrefs.getEmailOrderConfirmationEnabled()) {
432                    }
433                    else if (emailType.equals("shipping") &&
434                                     shoppingPrefs.getEmailOrderShippingEnabled()) {
435                    }
436                    else {
437                            return;
438                    }
439    
440                    User user = userPersistence.findByPrimaryKey(order.getUserId());
441    
442                    Currency currency = Currency.getInstance(shoppingPrefs.getCurrencyId());
443    
444                    String billingAddress =
445                            order.getBillingFirstName() + " " + order.getBillingLastName() +
446                                    "<br>" +
447                            order.getBillingEmailAddress() + "<br>" +
448                            order.getBillingStreet() + "<br>" +
449                            order.getBillingCity() + "<br>" +
450                            order.getBillingState() + "<br>" +
451                            order.getBillingZip() + "<br>" +
452                            order.getBillingCountry() + "<br>" +
453                            order.getBillingPhone() + "<br>";
454    
455                    String shippingAddress =
456                            order.getShippingFirstName() + " " + order.getShippingLastName() +
457                                    "<br>" +
458                            order.getShippingEmailAddress() + "<br>" +
459                            order.getShippingStreet() + "<br>" +
460                            order.getShippingCity() + "<br>" +
461                            order.getShippingState() + "<br>" +
462                            order.getShippingZip() + "<br>" +
463                            order.getShippingCountry() + "<br>" +
464                            order.getShippingPhone() + "<br>";
465    
466                    double total = ShoppingUtil.calculateTotal(order);
467    
468                    String fromName = shoppingPrefs.getEmailFromName(order.getCompanyId());
469                    String fromAddress = shoppingPrefs.getEmailFromAddress(
470                            order.getCompanyId());
471    
472                    String toName = user.getFullName();
473                    String toAddress = user.getEmailAddress();
474    
475                    String subject = null;
476                    String body = null;
477    
478                    if (emailType.equals("confirmation")) {
479                            subject = shoppingPrefs.getEmailOrderConfirmationSubject();
480                            body = shoppingPrefs.getEmailOrderConfirmationBody();
481                    }
482                    else if (emailType.equals("shipping")) {
483                            subject = shoppingPrefs.getEmailOrderShippingSubject();
484                            body = shoppingPrefs.getEmailOrderShippingBody();
485                    }
486    
487                    SubscriptionSender subscriptionSender = new SubscriptionSender();
488    
489                    subscriptionSender.setBody(body);
490                    subscriptionSender.setCompanyId(order.getCompanyId());
491                    subscriptionSender.setContextAttributes(
492                            "[$ORDER_BILLING_ADDRESS$]", billingAddress, "[$ORDER_CURRENCY$]",
493                            currency.getSymbol(), "[$ORDER_NUMBER$]", order.getNumber(),
494                            "[$ORDER_SHIPPING_ADDRESS$]", shippingAddress, "[$ORDER_TOTAL$]",
495                            total);
496                    subscriptionSender.setFrom(fromAddress, fromName);
497                    subscriptionSender.setHtmlFormat(true);
498                    subscriptionSender.setMailId("shopping_order", order.getOrderId());
499                    subscriptionSender.setPortletId(PortletKeys.SHOPPING);
500                    subscriptionSender.setScopeGroupId(order.getGroupId());
501                    subscriptionSender.setServiceContext(serviceContext);
502                    subscriptionSender.setSubject(subject);
503                    subscriptionSender.setUserId(order.getUserId());
504    
505                    subscriptionSender.addRuntimeSubscribers(toAddress, toName);
506    
507                    subscriptionSender.flushNotificationsAsync();
508    
509                    if (emailType.equals("confirmation") && order.isSendOrderEmail()) {
510                            order.setSendOrderEmail(false);
511    
512                            shoppingOrderPersistence.update(order);
513                    }
514                    else if (emailType.equals("shipping") && order.isSendShippingEmail()) {
515                            order.setSendShippingEmail(false);
516    
517                            shoppingOrderPersistence.update(order);
518                    }
519            }
520    
521            public ShoppingOrder updateLatestOrder(
522                            long userId, long groupId, String billingFirstName,
523                            String billingLastName, String billingEmailAddress,
524                            String billingCompany, String billingStreet, String billingCity,
525                            String billingState, String billingZip, String billingCountry,
526                            String billingPhone, boolean shipToBilling,
527                            String shippingFirstName, String shippingLastName,
528                            String shippingEmailAddress, String shippingCompany,
529                            String shippingStreet, String shippingCity, String shippingState,
530                            String shippingZip, String shippingCountry, String shippingPhone,
531                            String ccName, String ccType, String ccNumber, int ccExpMonth,
532                            int ccExpYear, String ccVerNumber, String comments)
533                    throws PortalException, SystemException {
534    
535                    ShoppingOrder order = getLatestOrder(userId, groupId);
536    
537                    return updateOrder(
538                            order.getOrderId(), billingFirstName, billingLastName,
539                            billingEmailAddress, billingCompany, billingStreet, billingCity,
540                            billingState, billingZip, billingCountry, billingPhone,
541                            shipToBilling, shippingFirstName, shippingLastName,
542                            shippingEmailAddress, shippingCompany, shippingStreet, shippingCity,
543                            shippingState, shippingZip, shippingCountry, shippingPhone, ccName,
544                            ccType, ccNumber, ccExpMonth, ccExpYear, ccVerNumber, comments);
545            }
546    
547            public ShoppingOrder updateOrder(
548                            long orderId, String ppTxnId, String ppPaymentStatus,
549                            double ppPaymentGross, String ppReceiverEmail, String ppPayerEmail)
550                    throws PortalException, SystemException {
551    
552                    ShoppingOrder order = shoppingOrderPersistence.findByPrimaryKey(
553                            orderId);
554    
555                    order.setModifiedDate(new Date());
556                    order.setPpTxnId(ppTxnId);
557                    order.setPpPaymentStatus(ppPaymentStatus);
558                    order.setPpPaymentGross(ppPaymentGross);
559                    order.setPpReceiverEmail(ppReceiverEmail);
560                    order.setPpPayerEmail(ppPayerEmail);
561    
562                    shoppingOrderPersistence.update(order);
563    
564                    return order;
565            }
566    
567            public ShoppingOrder updateOrder(
568                            long orderId, String billingFirstName, String billingLastName,
569                            String billingEmailAddress, String billingCompany,
570                            String billingStreet, String billingCity, String billingState,
571                            String billingZip, String billingCountry, String billingPhone,
572                            boolean shipToBilling, String shippingFirstName,
573                            String shippingLastName, String shippingEmailAddress,
574                            String shippingCompany, String shippingStreet, String shippingCity,
575                            String shippingState, String shippingZip, String shippingCountry,
576                            String shippingPhone, String ccName, String ccType, String ccNumber,
577                            int ccExpMonth, int ccExpYear, String ccVerNumber, String comments)
578                    throws PortalException, SystemException {
579    
580                    ShoppingOrder order = shoppingOrderPersistence.findByPrimaryKey(
581                            orderId);
582    
583                    ShoppingPreferences shoppingPrefs = ShoppingPreferences.getInstance(
584                            order.getCompanyId(), order.getGroupId());
585    
586                    validate(
587                            shoppingPrefs, billingFirstName, billingLastName,
588                            billingEmailAddress, billingStreet, billingCity, billingState,
589                            billingZip, billingCountry, billingPhone, shipToBilling,
590                            shippingFirstName, shippingLastName, shippingEmailAddress,
591                            shippingStreet, shippingCity, shippingState, shippingZip,
592                            shippingCountry, shippingPhone, ccName, ccType, ccNumber,
593                            ccExpMonth, ccExpYear, ccVerNumber);
594    
595                    order.setModifiedDate(new Date());
596                    order.setBillingFirstName(billingFirstName);
597                    order.setBillingLastName(billingLastName);
598                    order.setBillingEmailAddress(billingEmailAddress);
599                    order.setBillingCompany(billingCompany);
600                    order.setBillingStreet(billingStreet);
601                    order.setBillingCity(billingCity);
602                    order.setBillingState(billingState);
603                    order.setBillingZip(billingZip);
604                    order.setBillingCountry(billingCountry);
605                    order.setBillingPhone(billingPhone);
606                    order.setShipToBilling(shipToBilling);
607    
608                    if (shipToBilling) {
609                            order.setShippingFirstName(billingFirstName);
610                            order.setShippingLastName(billingLastName);
611                            order.setShippingEmailAddress(billingEmailAddress);
612                            order.setShippingCompany(billingCompany);
613                            order.setShippingStreet(billingStreet);
614                            order.setShippingCity(billingCity);
615                            order.setShippingState(billingState);
616                            order.setShippingZip(billingZip);
617                            order.setShippingCountry(billingCountry);
618                            order.setShippingPhone(billingPhone);
619                    }
620                    else {
621                            order.setShippingFirstName(shippingFirstName);
622                            order.setShippingLastName(shippingLastName);
623                            order.setShippingEmailAddress(shippingEmailAddress);
624                            order.setShippingCompany(shippingCompany);
625                            order.setShippingStreet(shippingStreet);
626                            order.setShippingCity(shippingCity);
627                            order.setShippingState(shippingState);
628                            order.setShippingZip(shippingZip);
629                            order.setShippingCountry(shippingCountry);
630                            order.setShippingPhone(shippingPhone);
631                    }
632    
633                    order.setCcName(ccName);
634                    order.setCcType(ccType);
635                    order.setCcNumber(ccNumber);
636                    order.setCcExpMonth(ccExpMonth);
637                    order.setCcExpYear(ccExpYear);
638                    order.setCcVerNumber(ccVerNumber);
639                    order.setComments(comments);
640    
641                    shoppingOrderPersistence.update(order);
642    
643                    return order;
644            }
645    
646            protected String getNumber() throws SystemException {
647                    String number = PwdGenerator.getPassword(
648                            PwdGenerator.KEY1 + PwdGenerator.KEY2, 12);
649    
650                    ShoppingOrder order = shoppingOrderPersistence.fetchByNumber(number);
651    
652                    if (order != null) {
653                            return order.getNumber();
654                    }
655    
656                    return number;
657            }
658    
659            protected void validate(
660                            ShoppingPreferences shoppingPrefs, String billingFirstName,
661                            String billingLastName, String billingEmailAddress,
662                            String billingStreet, String billingCity, String billingState,
663                            String billingZip, String billingCountry, String billingPhone,
664                            boolean shipToBilling, String shippingFirstName,
665                            String shippingLastName, String shippingEmailAddress,
666                            String shippingStreet, String shippingCity, String shippingState,
667                            String shippingZip, String shippingCountry, String shippingPhone,
668                            String ccName, String ccType, String ccNumber, int ccExpMonth,
669                            int ccExpYear, String ccVerNumber)
670                    throws PortalException {
671    
672                    if (Validator.isNull(billingFirstName)) {
673                            throw new BillingFirstNameException();
674                    }
675                    else if (Validator.isNull(billingLastName)) {
676                            throw new BillingLastNameException();
677                    }
678                    else if (!Validator.isEmailAddress(billingEmailAddress)) {
679                            throw new BillingEmailAddressException();
680                    }
681                    else if (Validator.isNull(billingStreet)) {
682                            throw new BillingStreetException();
683                    }
684                    else if (Validator.isNull(billingCity)) {
685                            throw new BillingCityException();
686                    }
687                    else if (Validator.isNull(billingState)) {
688                            throw new BillingStateException();
689                    }
690                    else if (Validator.isNull(billingZip)) {
691                            throw new BillingZipException();
692                    }
693                    else if (Validator.isNull(billingCountry)) {
694                            throw new BillingCountryException();
695                    }
696                    else if (Validator.isNull(billingPhone)) {
697                            throw new BillingPhoneException();
698                    }
699    
700                    if (!shipToBilling) {
701                            if (Validator.isNull(shippingFirstName)) {
702                                    throw new ShippingFirstNameException();
703                            }
704                            else if (Validator.isNull(shippingLastName)) {
705                                    throw new ShippingLastNameException();
706                            }
707                            else if (!Validator.isEmailAddress(shippingEmailAddress)) {
708                                    throw new ShippingEmailAddressException();
709                            }
710                            else if (Validator.isNull(shippingStreet)) {
711                                    throw new ShippingStreetException();
712                            }
713                            else if (Validator.isNull(shippingCity)) {
714                                    throw new ShippingCityException();
715                            }
716                            else if (Validator.isNull(shippingState)) {
717                                    throw new ShippingStateException();
718                            }
719                            else if (Validator.isNull(shippingZip)) {
720                                    throw new ShippingZipException();
721                            }
722                            else if (Validator.isNull(shippingCountry)) {
723                                    throw new ShippingCountryException();
724                            }
725                            else if (Validator.isNull(shippingPhone)) {
726                                    throw new ShippingPhoneException();
727                            }
728                    }
729    
730                    if (!shoppingPrefs.usePayPal() &&
731                            (shoppingPrefs.getCcTypes().length > 0)) {
732    
733                            if (Validator.isNull(ccName)) {
734                                    throw new CCNameException();
735                            }
736                            else if (Validator.isNull(ccType)) {
737                                    throw new CCTypeException();
738                            }
739                            else if (!CreditCard.isValidNumber(ccNumber, ccType)) {
740                                    throw new CCNumberException();
741                            }
742                            else if (!CreditCard.isValidExpirationDate(ccExpMonth, ccExpYear)) {
743                                    throw new CCExpirationException();
744                            }
745                    }
746            }
747    
748    }