1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.shopping.service.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.mail.MailMessage;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.kernel.util.StringUtil;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.model.Company;
32  import com.liferay.portal.model.User;
33  import com.liferay.portal.util.PortalUtil;
34  import com.liferay.portal.util.PortletKeys;
35  import com.liferay.portlet.shopping.BillingCityException;
36  import com.liferay.portlet.shopping.BillingCountryException;
37  import com.liferay.portlet.shopping.BillingEmailAddressException;
38  import com.liferay.portlet.shopping.BillingFirstNameException;
39  import com.liferay.portlet.shopping.BillingLastNameException;
40  import com.liferay.portlet.shopping.BillingPhoneException;
41  import com.liferay.portlet.shopping.BillingStateException;
42  import com.liferay.portlet.shopping.BillingStreetException;
43  import com.liferay.portlet.shopping.BillingZipException;
44  import com.liferay.portlet.shopping.CCExpirationException;
45  import com.liferay.portlet.shopping.CCNameException;
46  import com.liferay.portlet.shopping.CCNumberException;
47  import com.liferay.portlet.shopping.CCTypeException;
48  import com.liferay.portlet.shopping.CartMinOrderException;
49  import com.liferay.portlet.shopping.NoSuchOrderException;
50  import com.liferay.portlet.shopping.ShippingCityException;
51  import com.liferay.portlet.shopping.ShippingCountryException;
52  import com.liferay.portlet.shopping.ShippingEmailAddressException;
53  import com.liferay.portlet.shopping.ShippingFirstNameException;
54  import com.liferay.portlet.shopping.ShippingLastNameException;
55  import com.liferay.portlet.shopping.ShippingPhoneException;
56  import com.liferay.portlet.shopping.ShippingStateException;
57  import com.liferay.portlet.shopping.ShippingStreetException;
58  import com.liferay.portlet.shopping.ShippingZipException;
59  import com.liferay.portlet.shopping.model.ShoppingCart;
60  import com.liferay.portlet.shopping.model.ShoppingCartItem;
61  import com.liferay.portlet.shopping.model.ShoppingItem;
62  import com.liferay.portlet.shopping.model.ShoppingItemField;
63  import com.liferay.portlet.shopping.model.ShoppingOrder;
64  import com.liferay.portlet.shopping.model.ShoppingOrderItem;
65  import com.liferay.portlet.shopping.model.impl.ShoppingCartItemImpl;
66  import com.liferay.portlet.shopping.model.impl.ShoppingOrderImpl;
67  import com.liferay.portlet.shopping.service.base.ShoppingOrderLocalServiceBaseImpl;
68  import com.liferay.portlet.shopping.util.ShoppingPreferences;
69  import com.liferay.portlet.shopping.util.ShoppingUtil;
70  import com.liferay.portlet.shopping.util.comparator.OrderDateComparator;
71  import com.liferay.util.CreditCard;
72  import com.liferay.util.PwdGenerator;
73  import com.liferay.util.cal.CalendarUtil;
74  
75  import java.io.IOException;
76  
77  import java.util.Currency;
78  import java.util.Date;
79  import java.util.Iterator;
80  import java.util.List;
81  import java.util.Map;
82  
83  import javax.mail.internet.InternetAddress;
84  
85  /**
86   * <a href="ShoppingOrderLocalServiceImpl.java.html"><b><i>View Source</i></b>
87   * </a>
88   *
89   * @author Brian Wing Shun Chan
90   *
91   */
92  public class ShoppingOrderLocalServiceImpl
93      extends ShoppingOrderLocalServiceBaseImpl {
94  
95      public void completeOrder(
96              String number, String ppTxnId, String ppPaymentStatus,
97              double ppPaymentGross, String ppReceiverEmail, String ppPayerEmail,
98              boolean updateInventory)
99          throws PortalException, SystemException {
100 
101         // Order
102 
103         ShoppingOrder order = shoppingOrderPersistence.findByNumber(number);
104 
105         order.setModifiedDate(new Date());
106         order.setPpTxnId(ppTxnId);
107         order.setPpPaymentStatus(ppPaymentStatus);
108         order.setPpPaymentGross(ppPaymentGross);
109         order.setPpReceiverEmail(ppReceiverEmail);
110         order.setPpPayerEmail(ppPayerEmail);
111 
112         shoppingOrderPersistence.update(order);
113 
114         // Inventory
115 
116         if (updateInventory &&
117             ppPaymentStatus.equals(ShoppingOrderImpl.STATUS_COMPLETED)) {
118 
119             List orderItems = shoppingOrderItemLocalService.getOrderItems(
120                 order.getOrderId());
121 
122             for (int i = 0; i < orderItems.size(); i++) {
123                 ShoppingOrderItem orderItem =
124                     (ShoppingOrderItem)orderItems.get(i);
125 
126                 ShoppingItem item = shoppingItemLocalService.getItem(
127                     ShoppingUtil.getItemId(orderItem.getItemId()));
128 
129                 if (!item.isFields()) {
130                     int quantity =
131                         item.getStockQuantity() - orderItem.getQuantity();
132 
133                     item.setStockQuantity(quantity);
134                 }
135                 else {
136                     List itemFields =
137                         shoppingItemFieldLocalService.getItemFields(
138                             item.getItemId());
139 
140                     ShoppingItemField[] itemFieldsArray =
141                         (ShoppingItemField[])itemFields.toArray(
142                             new ShoppingItemField[0]);
143 
144                     String[] fieldsArray = ShoppingCartItemImpl.getFieldsArray(
145                         ShoppingUtil.getItemFields(orderItem.getItemId()));
146 
147                     int rowPos = ShoppingUtil.getFieldsQuantitiesPos(
148                         item, itemFieldsArray, fieldsArray);
149 
150                     String[] fieldsQuantities = item.getFieldsQuantitiesArray();
151 
152                     try {
153                         int quantity =
154                             GetterUtil.getInteger(fieldsQuantities[rowPos]) -
155                             orderItem.getQuantity();
156 
157                         fieldsQuantities[rowPos] = Integer.toString(quantity);
158 
159                         item.setFieldsQuantitiesArray(fieldsQuantities);
160                     }
161                     catch (Exception e) {
162                     }
163                 }
164 
165                 shoppingItemPersistence.update(item);
166             }
167         }
168 
169         // Email
170 
171         sendEmail(order, "confirmation");
172     }
173 
174     public void deleteOrder(long orderId)
175         throws PortalException, SystemException {
176 
177         ShoppingOrder order = shoppingOrderPersistence.findByPrimaryKey(
178             orderId);
179 
180         deleteOrder(order);
181     }
182 
183     public void deleteOrder(ShoppingOrder order)
184         throws PortalException, SystemException {
185 
186         // Items
187 
188         shoppingOrderItemPersistence.removeByOrderId(order.getOrderId());
189 
190         // Message boards
191 
192         mbMessageLocalService.deleteDiscussionMessages(
193             ShoppingOrder.class.getName(), order.getOrderId());
194 
195         // Order
196 
197         shoppingOrderPersistence.remove(order.getOrderId());
198     }
199 
200     public void deleteOrders(long groupId)
201         throws PortalException, SystemException {
202 
203         Iterator itr = shoppingOrderPersistence.findByGroupId(
204             groupId).iterator();
205 
206         while (itr.hasNext()) {
207             ShoppingOrder order = (ShoppingOrder)itr.next();
208 
209             deleteOrder(order);
210         }
211     }
212 
213     public ShoppingOrder getLatestOrder(long userId, long groupId)
214         throws PortalException, SystemException {
215 
216         List orders = shoppingOrderPersistence.findByG_U_PPPS(
217             groupId, userId, ShoppingOrderImpl.STATUS_LATEST, 0, 1);
218 
219         ShoppingOrder order = null;
220 
221         if (orders.size() == 1) {
222             order = (ShoppingOrder)orders.get(0);
223         }
224         else {
225             User user = userPersistence.findByPrimaryKey(userId);
226             Date now = new Date();
227 
228             String number = getNumber();
229 
230             List pastOrders = shoppingOrderPersistence.findByG_U_PPPS(
231                 groupId, userId, ShoppingOrderImpl.STATUS_CHECKOUT, 0, 1);
232 
233             if (pastOrders.size() > 0) {
234                 ShoppingOrder pastOrder = (ShoppingOrder)pastOrders.get(0);
235 
236                 long orderId = counterLocalService.increment();
237 
238                 order = shoppingOrderPersistence.create(orderId);
239 
240                 order.setBillingCompany(pastOrder.getBillingCompany());
241                 order.setBillingStreet(pastOrder.getBillingStreet());
242                 order.setBillingCity(pastOrder.getBillingCity());
243                 order.setBillingState(pastOrder.getBillingState());
244                 order.setBillingZip(pastOrder.getBillingZip());
245                 order.setBillingCountry(pastOrder.getBillingCountry());
246                 order.setBillingPhone(pastOrder.getBillingPhone());
247                 order.setShipToBilling(pastOrder.isShipToBilling());
248                 order.setShippingCompany(pastOrder.getShippingCompany());
249                 order.setShippingStreet(pastOrder.getShippingStreet());
250                 order.setShippingCity(pastOrder.getShippingCity());
251                 order.setShippingState(pastOrder.getShippingState());
252                 order.setShippingZip(pastOrder.getShippingZip());
253                 order.setShippingCountry(pastOrder.getShippingCountry());
254                 order.setShippingPhone(pastOrder.getShippingPhone());
255             }
256             else {
257                 long orderId = counterLocalService.increment();
258 
259                 order = shoppingOrderPersistence.create(orderId);
260             }
261 
262             order.setGroupId(groupId);
263             order.setCompanyId(user.getCompanyId());
264             order.setUserId(user.getUserId());
265             order.setUserName(user.getFullName());
266             order.setCreateDate(now);
267             order.setModifiedDate(now);
268             order.setNumber(number);
269             order.setBillingFirstName(user.getFirstName());
270             order.setBillingLastName(user.getLastName());
271             order.setBillingEmailAddress(user.getEmailAddress());
272             order.setShippingFirstName(user.getFirstName());
273             order.setShippingLastName(user.getLastName());
274             order.setShippingEmailAddress(user.getEmailAddress());
275             order.setCcName(user.getFullName());
276             order.setPpPaymentStatus(ShoppingOrderImpl.STATUS_LATEST);
277             order.setSendOrderEmail(true);
278             order.setSendShippingEmail(true);
279 
280             shoppingOrderPersistence.update(order);
281         }
282 
283         return order;
284     }
285 
286     public ShoppingOrder getOrder(long orderId)
287         throws PortalException, SystemException {
288 
289         return shoppingOrderPersistence.findByPrimaryKey(orderId);
290     }
291 
292     public ShoppingOrder getOrder(String number)
293         throws PortalException, SystemException {
294 
295         return shoppingOrderPersistence.findByNumber(number);
296     }
297 
298     public ShoppingOrder saveLatestOrder(ShoppingCart cart)
299         throws PortalException, SystemException {
300 
301         Map items = cart.getItems();
302         Date now = new Date();
303 
304         ShoppingPreferences shoppingPrefs = ShoppingPreferences.getInstance(
305             cart.getCompanyId(), cart.getGroupId());
306 
307         if (!ShoppingUtil.meetsMinOrder(shoppingPrefs, items)) {
308             throw new CartMinOrderException();
309         }
310 
311         ShoppingOrder order = getLatestOrder(
312             cart.getUserId(), cart.getGroupId());
313 
314         order.setCreateDate(now);
315         order.setModifiedDate(now);
316         order.setPpPaymentStatus(ShoppingOrderImpl.STATUS_CHECKOUT);
317 
318         shoppingOrderPersistence.update(order);
319 
320         boolean requiresShipping = false;
321 
322         Iterator itr = items.entrySet().iterator();
323 
324         while (itr.hasNext()) {
325             Map.Entry entry = (Map.Entry)itr.next();
326 
327             ShoppingCartItem cartItem = (ShoppingCartItem)entry.getKey();
328             Integer count = (Integer)entry.getValue();
329 
330             ShoppingItem item = cartItem.getItem();
331 
332             if (item.isRequiresShipping()) {
333                 requiresShipping = true;
334             }
335 
336             long orderItemId = counterLocalService.increment();
337 
338             ShoppingOrderItem orderItem = shoppingOrderItemPersistence.create(
339                 orderItemId);
340 
341             orderItem.setOrderId(order.getOrderId());
342             orderItem.setItemId(cartItem.getCartItemId());
343             orderItem.setSku(item.getSku());
344             orderItem.setName(item.getName());
345             orderItem.setDescription(item.getDescription());
346             orderItem.setProperties(item.getProperties());
347             orderItem.setPrice(
348                 ShoppingUtil.calculateActualPrice(item, count.intValue()) /
349                     count.intValue());
350             orderItem.setQuantity(count.intValue());
351 
352             shoppingOrderItemPersistence.update(orderItem);
353         }
354 
355         order.setModifiedDate(new Date());
356         order.setTax(
357             ShoppingUtil.calculateTax(items, order.getBillingState()));
358         order.setShipping(
359             ShoppingUtil.calculateAlternativeShipping(
360                 items, cart.getAltShipping()));
361         order.setAltShipping(
362             shoppingPrefs.getAlternativeShippingName(cart.getAltShipping()));
363         order.setRequiresShipping(requiresShipping);
364         order.setInsure(cart.isInsure());
365         order.setInsurance(ShoppingUtil.calculateInsurance(items));
366         order.setCouponCodes(cart.getCouponCodes());
367         order.setCouponDiscount(
368             ShoppingUtil.calculateCouponDiscount(
369                 items, order.getBillingState(), cart.getCoupon()));
370         order.setSendOrderEmail(true);
371         order.setSendShippingEmail(true);
372 
373         shoppingOrderPersistence.update(order);
374 
375         return order;
376     }
377 
378     public List search(
379             long groupId, long companyId, long userId, String number,
380             String billingFirstName, String billingLastName,
381             String billingEmailAddress, String shippingFirstName,
382             String shippingLastName, String shippingEmailAddress,
383             String ppPaymentStatus, boolean andOperator, int begin, int end)
384         throws PortalException, SystemException {
385 
386         OrderDateComparator obc = new OrderDateComparator(false);
387 
388         return shoppingOrderFinder.findByG_C_U_N_PPPS(
389             groupId, companyId, userId, number, billingFirstName,
390             billingLastName, billingEmailAddress, shippingFirstName,
391             shippingLastName, shippingEmailAddress, ppPaymentStatus,
392             andOperator, begin, end, obc);
393     }
394 
395     public int searchCount(
396             long groupId, long companyId, long userId, String number,
397             String billingFirstName, String billingLastName,
398             String billingEmailAddress, String shippingFirstName,
399             String shippingLastName, String shippingEmailAddress,
400             String ppPaymentStatus, boolean andOperator)
401         throws PortalException, SystemException {
402 
403         return shoppingOrderFinder.countByG_C_U_N_PPPS(
404             groupId, companyId, userId, number, billingFirstName,
405             billingLastName, billingEmailAddress, shippingFirstName,
406             shippingLastName, shippingEmailAddress, ppPaymentStatus,
407             andOperator);
408     }
409 
410     public void sendEmail(long orderId, String emailType)
411         throws PortalException, SystemException {
412 
413         ShoppingOrder order = shoppingOrderPersistence.findByPrimaryKey(
414             orderId);
415 
416         sendEmail(order, emailType);
417     }
418 
419     public void sendEmail(ShoppingOrder order, String emailType)
420         throws PortalException, SystemException {
421 
422         try {
423             ShoppingPreferences shoppingPrefs =
424                 ShoppingPreferences.getInstance(
425                     order.getCompanyId(), order.getGroupId());
426 
427             if (emailType.equals("confirmation") &&
428                 shoppingPrefs.getEmailOrderConfirmationEnabled()) {
429             }
430             else if (emailType.equals("shipping") &&
431                      shoppingPrefs.getEmailOrderShippingEnabled()) {
432             }
433             else {
434                 return;
435             }
436 
437             Company company = companyPersistence.findByPrimaryKey(
438                 order.getCompanyId());
439 
440             User user = userPersistence.findByPrimaryKey(order.getUserId());
441 
442             Currency currency =
443                 Currency.getInstance(shoppingPrefs.getCurrencyId());
444 
445             String billingAddress =
446                 order.getBillingFirstName() + " " + order.getBillingLastName() +
447                     "<br>" +
448                 order.getBillingEmailAddress() + "<br>" +
449                 order.getBillingStreet() + "<br>" +
450                 order.getBillingCity() + "<br>" +
451                 order.getBillingState() + "<br>" +
452                 order.getBillingZip() + "<br>" +
453                 order.getBillingCountry() + "<br>" +
454                 order.getBillingPhone() + "<br>";
455 
456             String shippingAddress =
457                 order.getShippingFirstName() + " " +
458                     order.getShippingLastName() + "<br>" +
459                 order.getShippingEmailAddress() + "<br>" +
460                 order.getShippingStreet() + "<br>" +
461                 order.getShippingCity() + "<br>" +
462                 order.getShippingState() + "<br>" +
463                 order.getShippingZip() + "<br>" +
464                 order.getShippingCountry() + "<br>" +
465                 order.getShippingPhone() + "<br>";
466 
467             double total = ShoppingUtil.calculateTotal(order);
468 
469             String portletName = PortalUtil.getPortletTitle(
470                 PortletKeys.SHOPPING, user);
471 
472             String fromName = shoppingPrefs.getEmailFromName();
473             String fromAddress = shoppingPrefs.getEmailFromAddress();
474 
475             String toName = user.getFullName();
476             String toAddress = user.getEmailAddress();
477 
478             String subject = null;
479             String body = null;
480 
481             if (emailType.equals("confirmation")) {
482                 subject = shoppingPrefs.getEmailOrderConfirmationSubject();
483                 body = shoppingPrefs.getEmailOrderConfirmationBody();
484             }
485             else if (emailType.equals("shipping")) {
486                 subject = shoppingPrefs.getEmailOrderShippingSubject();
487                 body = shoppingPrefs.getEmailOrderShippingBody();
488             }
489 
490             subject = StringUtil.replace(
491                 subject,
492                 new String[] {
493                     "[$FROM_ADDRESS$]",
494                     "[$FROM_NAME$]",
495                     "[$ORDER_BILLING_ADDRESS$]",
496                     "[$ORDER_CURRENCY$]",
497                     "[$ORDER_NUMBER$]",
498                     "[$ORDER_SHIPPING_ADDRESS$]",
499                     "[$ORDER_TOTAL$]",
500                     "[$PORTAL_URL$]",
501                     "[$PORTLET_NAME$]",
502                     "[$TO_ADDRESS$]",
503                     "[$TO_NAME$]"
504                 },
505                 new String[] {
506                     fromAddress,
507                     fromName,
508                     billingAddress,
509                     currency.getSymbol(),
510                     order.getNumber(),
511                     shippingAddress,
512                     String.valueOf(total),
513                     company.getVirtualHost(),
514                     portletName,
515                     toAddress,
516                     toName
517                 });
518 
519             body = StringUtil.replace(
520                 body,
521                 new String[] {
522                     "[$FROM_ADDRESS$]",
523                     "[$FROM_NAME$]",
524                     "[$ORDER_BILLING_ADDRESS$]",
525                     "[$ORDER_CURRENCY$]",
526                     "[$ORDER_NUMBER$]",
527                     "[$ORDER_SHIPPING_ADDRESS$]",
528                     "[$ORDER_TOTAL$]",
529                     "[$PORTAL_URL$]",
530                     "[$PORTLET_NAME$]",
531                     "[$TO_ADDRESS$]",
532                     "[$TO_NAME$]"
533                 },
534                 new String[] {
535                     fromAddress,
536                     fromName,
537                     billingAddress,
538                     currency.getSymbol(),
539                     order.getNumber(),
540                     shippingAddress,
541                     String.valueOf(total),
542                     company.getVirtualHost(),
543                     portletName,
544                     toAddress,
545                     toName
546                 });
547 
548             InternetAddress from = new InternetAddress(fromAddress, fromName);
549 
550             InternetAddress to = new InternetAddress(toAddress, toName);
551 
552             MailMessage message = new MailMessage(
553                 from, to, subject, body, true);
554 
555             mailService.sendEmail(message);
556 
557             if (emailType.equals("confirmation") && order.isSendOrderEmail()) {
558                 order.setSendOrderEmail(false);
559 
560                 shoppingOrderPersistence.update(order);
561             }
562             else if (emailType.equals("shipping") &&
563                      order.isSendShippingEmail()) {
564 
565                 order.setSendShippingEmail(false);
566 
567                 shoppingOrderPersistence.update(order);
568             }
569         }
570         catch (IOException ioe) {
571             throw new SystemException(ioe);
572         }
573         catch (PortalException pe) {
574             throw pe;
575         }
576     }
577 
578     public ShoppingOrder updateLatestOrder(
579             long userId, long groupId, String billingFirstName,
580             String billingLastName, String billingEmailAddress,
581             String billingCompany, String billingStreet, String billingCity,
582             String billingState, String billingZip, String billingCountry,
583             String billingPhone, boolean shipToBilling,
584             String shippingFirstName, String shippingLastName,
585             String shippingEmailAddress, String shippingCompany,
586             String shippingStreet, String shippingCity, String shippingState,
587             String shippingZip, String shippingCountry, String shippingPhone,
588             String ccName, String ccType, String ccNumber, int ccExpMonth,
589             int ccExpYear, String ccVerNumber, String comments)
590         throws PortalException, SystemException {
591 
592         ShoppingOrder order = getLatestOrder(userId, groupId);
593 
594         return updateOrder(
595             order.getOrderId(), billingFirstName, billingLastName,
596             billingEmailAddress, billingCompany, billingStreet, billingCity,
597             billingState, billingZip, billingCountry, billingPhone,
598             shipToBilling, shippingFirstName, shippingLastName,
599             shippingEmailAddress, shippingCompany, shippingStreet, shippingCity,
600             shippingState, shippingZip, shippingCountry, shippingPhone,
601             ccName, ccType, ccNumber, ccExpMonth, ccExpYear, ccVerNumber,
602             comments);
603     }
604 
605     public ShoppingOrder updateOrder(
606             long orderId, String billingFirstName, String billingLastName,
607             String billingEmailAddress, String billingCompany,
608             String billingStreet, String billingCity, String billingState,
609             String billingZip, String billingCountry, String billingPhone,
610             boolean shipToBilling, String shippingFirstName,
611             String shippingLastName, String shippingEmailAddress,
612             String shippingCompany, String shippingStreet, String shippingCity,
613             String shippingState, String shippingZip, String shippingCountry,
614             String shippingPhone, String ccName, String ccType, String ccNumber,
615             int ccExpMonth, int ccExpYear, String ccVerNumber, String comments)
616         throws PortalException, SystemException {
617 
618         ShoppingOrder order = shoppingOrderPersistence.findByPrimaryKey(
619             orderId);
620 
621         ShoppingPreferences shoppingPrefs = ShoppingPreferences.getInstance(
622             order.getCompanyId(), order.getGroupId());
623 
624         validate(
625             shoppingPrefs, billingFirstName, billingLastName,
626             billingEmailAddress, billingStreet, billingCity, billingState,
627             billingZip, billingCountry, billingPhone, shipToBilling,
628             shippingFirstName, shippingLastName, shippingEmailAddress,
629             shippingStreet, shippingCity, shippingState, shippingZip,
630             shippingCountry, shippingPhone, ccName, ccType, ccNumber,
631             ccExpMonth, ccExpYear, ccVerNumber);
632 
633         order.setModifiedDate(new Date());
634         order.setBillingFirstName(billingFirstName);
635         order.setBillingLastName(billingLastName);
636         order.setBillingEmailAddress(billingEmailAddress);
637         order.setBillingCompany(billingCompany);
638         order.setBillingStreet(billingStreet);
639         order.setBillingCity(billingCity);
640         order.setBillingState(billingState);
641         order.setBillingZip(billingZip);
642         order.setBillingCountry(billingCountry);
643         order.setBillingPhone(billingPhone);
644         order.setShipToBilling(shipToBilling);
645 
646         if (shipToBilling) {
647             order.setShippingFirstName(billingFirstName);
648             order.setShippingLastName(billingLastName);
649             order.setShippingEmailAddress(billingEmailAddress);
650             order.setShippingCompany(billingCompany);
651             order.setShippingStreet(billingStreet);
652             order.setShippingCity(billingCity);
653             order.setShippingState(billingState);
654             order.setShippingZip(billingZip);
655             order.setShippingCountry(billingCountry);
656             order.setShippingPhone(billingPhone);
657         }
658         else {
659             order.setShippingFirstName(shippingFirstName);
660             order.setShippingLastName(shippingLastName);
661             order.setShippingEmailAddress(shippingEmailAddress);
662             order.setShippingCompany(shippingCompany);
663             order.setShippingStreet(shippingStreet);
664             order.setShippingCity(shippingCity);
665             order.setShippingState(shippingState);
666             order.setShippingZip(shippingZip);
667             order.setShippingCountry(shippingCountry);
668             order.setShippingPhone(shippingPhone);
669         }
670 
671         order.setCcName(ccName);
672         order.setCcType(ccType);
673         order.setCcNumber(ccNumber);
674         order.setCcExpMonth(ccExpMonth);
675         order.setCcExpYear(ccExpYear);
676         order.setCcVerNumber(ccVerNumber);
677         order.setComments(comments);
678 
679         shoppingOrderPersistence.update(order);
680 
681         return order;
682     }
683 
684     public ShoppingOrder updateOrder(
685             long orderId, String ppTxnId, String ppPaymentStatus,
686             double ppPaymentGross, String ppReceiverEmail, String ppPayerEmail)
687         throws PortalException, SystemException {
688 
689         ShoppingOrder order = shoppingOrderPersistence.findByPrimaryKey(
690             orderId);
691 
692         order.setModifiedDate(new Date());
693         order.setPpTxnId(ppTxnId);
694         order.setPpPaymentStatus(ppPaymentStatus);
695         order.setPpPaymentGross(ppPaymentGross);
696         order.setPpReceiverEmail(ppReceiverEmail);
697         order.setPpPayerEmail(ppPayerEmail);
698 
699         shoppingOrderPersistence.update(order);
700 
701         return order;
702     }
703 
704     protected String getNumber() throws SystemException {
705         String number =
706             PwdGenerator.getPassword(PwdGenerator.KEY1 + PwdGenerator.KEY2, 12);
707 
708         try {
709             shoppingOrderPersistence.findByNumber(number);
710 
711             return getNumber();
712         }
713         catch (NoSuchOrderException nsoe) {
714             return number;
715         }
716     }
717 
718     protected void validate(
719             ShoppingPreferences shoppingPrefs, String billingFirstName,
720             String billingLastName, String billingEmailAddress,
721             String billingStreet, String billingCity, String billingState,
722             String billingZip, String billingCountry, String billingPhone,
723             boolean shipToBilling, String shippingFirstName,
724             String shippingLastName, String shippingEmailAddress,
725             String shippingStreet, String shippingCity, String shippingState,
726             String shippingZip, String shippingCountry, String shippingPhone,
727             String ccName, String ccType, String ccNumber, int ccExpMonth,
728             int ccExpYear, String ccVerNumber)
729         throws PortalException {
730 
731         if (Validator.isNull(billingFirstName)) {
732             throw new BillingFirstNameException();
733         }
734         else if (Validator.isNull(billingLastName)) {
735             throw new BillingLastNameException();
736         }
737         else if (!Validator.isEmailAddress(billingEmailAddress)) {
738             throw new BillingEmailAddressException();
739         }
740         else if (Validator.isNull(billingStreet)) {
741             throw new BillingStreetException();
742         }
743         else if (Validator.isNull(billingCity)) {
744             throw new BillingCityException();
745         }
746         else if (Validator.isNull(billingState)) {
747             throw new BillingStateException();
748         }
749         else if (Validator.isNull(billingZip)) {
750             throw new BillingZipException();
751         }
752         else if (Validator.isNull(billingCountry)) {
753             throw new BillingCountryException();
754         }
755         else if (Validator.isNull(billingPhone)) {
756             throw new BillingPhoneException();
757         }
758 
759         if (!shipToBilling) {
760             if (Validator.isNull(shippingFirstName)) {
761                 throw new ShippingFirstNameException();
762             }
763             else if (Validator.isNull(shippingLastName)) {
764                 throw new ShippingLastNameException();
765             }
766             else if (!Validator.isEmailAddress(shippingEmailAddress)) {
767                 throw new ShippingEmailAddressException();
768             }
769             else if (Validator.isNull(shippingStreet)) {
770                 throw new ShippingStreetException();
771             }
772             else if (Validator.isNull(shippingCity)) {
773                 throw new ShippingCityException();
774             }
775             else if (Validator.isNull(shippingState)) {
776                 throw new ShippingStateException();
777             }
778             else if (Validator.isNull(shippingZip)) {
779                 throw new ShippingZipException();
780             }
781             else if (Validator.isNull(shippingCountry)) {
782                 throw new ShippingCountryException();
783             }
784             else if (Validator.isNull(shippingPhone)) {
785                 throw new ShippingPhoneException();
786             }
787         }
788 
789         if ((!shoppingPrefs.usePayPal()) &&
790             (shoppingPrefs.getCcTypes().length > 0)) {
791 
792             if (Validator.isNull(ccName)) {
793                 throw new CCNameException();
794             }
795             else if (Validator.isNull(ccType)) {
796                 throw new CCTypeException();
797             }
798             else if (!CreditCard.isValid(ccNumber, ccType)) {
799                 throw new CCNumberException();
800             }
801             else if (!CalendarUtil.isFuture(ccExpMonth, ccExpYear)) {
802                 throw new CCExpirationException();
803             }
804         }
805     }
806 
807 }