1   /**
2    * Copyright (c) 2000-2009 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.CalendarUtil;
29  import com.liferay.portal.kernel.util.GetterUtil;
30  import com.liferay.portal.kernel.util.StringUtil;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.model.Company;
33  import com.liferay.portal.model.User;
34  import com.liferay.portal.util.PortalUtil;
35  import com.liferay.portal.util.PortletKeys;
36  import com.liferay.portlet.shopping.BillingCityException;
37  import com.liferay.portlet.shopping.BillingCountryException;
38  import com.liferay.portlet.shopping.BillingEmailAddressException;
39  import com.liferay.portlet.shopping.BillingFirstNameException;
40  import com.liferay.portlet.shopping.BillingLastNameException;
41  import com.liferay.portlet.shopping.BillingPhoneException;
42  import com.liferay.portlet.shopping.BillingStateException;
43  import com.liferay.portlet.shopping.BillingStreetException;
44  import com.liferay.portlet.shopping.BillingZipException;
45  import com.liferay.portlet.shopping.CCExpirationException;
46  import com.liferay.portlet.shopping.CCNameException;
47  import com.liferay.portlet.shopping.CCNumberException;
48  import com.liferay.portlet.shopping.CCTypeException;
49  import com.liferay.portlet.shopping.CartMinOrderException;
50  import com.liferay.portlet.shopping.NoSuchOrderException;
51  import com.liferay.portlet.shopping.ShippingCityException;
52  import com.liferay.portlet.shopping.ShippingCountryException;
53  import com.liferay.portlet.shopping.ShippingEmailAddressException;
54  import com.liferay.portlet.shopping.ShippingFirstNameException;
55  import com.liferay.portlet.shopping.ShippingLastNameException;
56  import com.liferay.portlet.shopping.ShippingPhoneException;
57  import com.liferay.portlet.shopping.ShippingStateException;
58  import com.liferay.portlet.shopping.ShippingStreetException;
59  import com.liferay.portlet.shopping.ShippingZipException;
60  import com.liferay.portlet.shopping.model.ShoppingCart;
61  import com.liferay.portlet.shopping.model.ShoppingCartItem;
62  import com.liferay.portlet.shopping.model.ShoppingItem;
63  import com.liferay.portlet.shopping.model.ShoppingItemField;
64  import com.liferay.portlet.shopping.model.ShoppingOrder;
65  import com.liferay.portlet.shopping.model.ShoppingOrderItem;
66  import com.liferay.portlet.shopping.model.impl.ShoppingCartItemImpl;
67  import com.liferay.portlet.shopping.model.impl.ShoppingOrderImpl;
68  import com.liferay.portlet.shopping.service.base.ShoppingOrderLocalServiceBaseImpl;
69  import com.liferay.portlet.shopping.util.ShoppingPreferences;
70  import com.liferay.portlet.shopping.util.ShoppingUtil;
71  import com.liferay.portlet.shopping.util.comparator.OrderDateComparator;
72  import com.liferay.util.CreditCard;
73  import com.liferay.util.PwdGenerator;
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, false);
113 
114         // Inventory
115 
116         if (updateInventory &&
117             ppPaymentStatus.equals(ShoppingOrderImpl.STATUS_COMPLETED)) {
118 
119             List<ShoppingOrderItem> orderItems =
120                 shoppingOrderItemLocalService.getOrderItems(order.getOrderId());
121 
122             for (ShoppingOrderItem orderItem : orderItems) {
123                 ShoppingItem item = shoppingItemLocalService.getItem(
124                     ShoppingUtil.getItemId(orderItem.getItemId()));
125 
126                 if (!item.isFields()) {
127                     int quantity =
128                         item.getStockQuantity() - orderItem.getQuantity();
129 
130                     item.setStockQuantity(quantity);
131                 }
132                 else {
133                     List<ShoppingItemField> itemFields =
134                         shoppingItemFieldLocalService.getItemFields(
135                             item.getItemId());
136 
137                     ShoppingItemField[] itemFieldsArray = itemFields.toArray(
138                         new ShoppingItemField[itemFields.size()]);
139 
140                     String[] fieldsArray = ShoppingCartItemImpl.getFieldsArray(
141                         ShoppingUtil.getItemFields(orderItem.getItemId()));
142 
143                     int rowPos = ShoppingUtil.getFieldsQuantitiesPos(
144                         item, itemFieldsArray, fieldsArray);
145 
146                     String[] fieldsQuantities = item.getFieldsQuantitiesArray();
147 
148                     try {
149                         int quantity =
150                             GetterUtil.getInteger(fieldsQuantities[rowPos]) -
151                             orderItem.getQuantity();
152 
153                         fieldsQuantities[rowPos] = String.valueOf(quantity);
154 
155                         item.setFieldsQuantitiesArray(fieldsQuantities);
156                     }
157                     catch (Exception e) {
158                     }
159                 }
160 
161                 shoppingItemPersistence.update(item, false);
162             }
163         }
164 
165         // Email
166 
167         try {
168             doSendEmail(order, "confirmation");
169         }
170         catch (IOException ioe) {
171             throw new SystemException(ioe);
172         }
173     }
174 
175     public void deleteOrder(long orderId)
176         throws PortalException, SystemException {
177 
178         ShoppingOrder order = shoppingOrderPersistence.findByPrimaryKey(
179             orderId);
180 
181         deleteOrder(order);
182     }
183 
184     public void deleteOrder(ShoppingOrder order)
185         throws PortalException, SystemException {
186 
187         // Items
188 
189         shoppingOrderItemPersistence.removeByOrderId(order.getOrderId());
190 
191         // Message boards
192 
193         mbMessageLocalService.deleteDiscussionMessages(
194             ShoppingOrder.class.getName(), order.getOrderId());
195 
196         // Order
197 
198         shoppingOrderPersistence.remove(order);
199     }
200 
201     public void deleteOrders(long groupId)
202         throws PortalException, SystemException {
203 
204         List<ShoppingOrder> orders = shoppingOrderPersistence.findByGroupId(
205             groupId);
206 
207         for (ShoppingOrder order : orders) {
208             deleteOrder(order);
209         }
210     }
211 
212     public ShoppingOrder getLatestOrder(long userId, long groupId)
213         throws PortalException, SystemException {
214 
215         List<ShoppingOrder> orders = shoppingOrderPersistence.findByG_U_PPPS(
216             groupId, userId, ShoppingOrderImpl.STATUS_LATEST, 0, 1);
217 
218         ShoppingOrder order = null;
219 
220         if (orders.size() == 1) {
221             order = orders.get(0);
222         }
223         else {
224             User user = userPersistence.findByPrimaryKey(userId);
225             Date now = new Date();
226 
227             String number = getNumber();
228 
229             List<ShoppingOrder> pastOrders =
230                 shoppingOrderPersistence.findByG_U_PPPS(
231                     groupId, userId, ShoppingOrderImpl.STATUS_CHECKOUT, 0, 1);
232 
233             if (pastOrders.size() > 0) {
234                 ShoppingOrder pastOrder = 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, false);
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 getPayPalTxnIdOrder(String ppTxnId)
299         throws PortalException, SystemException {
300 
301         return shoppingOrderPersistence.findByPPTxnId(ppTxnId);
302     }
303 
304     public ShoppingOrder saveLatestOrder(ShoppingCart cart)
305         throws PortalException, SystemException {
306 
307         Map<ShoppingCartItem, Integer> items = cart.getItems();
308         Date now = new Date();
309 
310         ShoppingPreferences shoppingPrefs = ShoppingPreferences.getInstance(
311             cart.getCompanyId(), cart.getGroupId());
312 
313         if (!ShoppingUtil.meetsMinOrder(shoppingPrefs, items)) {
314             throw new CartMinOrderException();
315         }
316 
317         ShoppingOrder order = getLatestOrder(
318             cart.getUserId(), cart.getGroupId());
319 
320         order.setCreateDate(now);
321         order.setModifiedDate(now);
322         order.setPpPaymentStatus(ShoppingOrderImpl.STATUS_CHECKOUT);
323 
324         shoppingOrderPersistence.update(order, false);
325 
326         boolean requiresShipping = false;
327 
328         Iterator<Map.Entry<ShoppingCartItem, Integer>> itr =
329             items.entrySet().iterator();
330 
331         while (itr.hasNext()) {
332             Map.Entry<ShoppingCartItem, Integer> entry = itr.next();
333 
334             ShoppingCartItem cartItem = entry.getKey();
335             Integer count = entry.getValue();
336 
337             ShoppingItem item = cartItem.getItem();
338 
339             if (item.isRequiresShipping()) {
340                 requiresShipping = true;
341             }
342 
343             long orderItemId = counterLocalService.increment();
344 
345             ShoppingOrderItem orderItem = shoppingOrderItemPersistence.create(
346                 orderItemId);
347 
348             orderItem.setOrderId(order.getOrderId());
349             orderItem.setItemId(cartItem.getCartItemId());
350             orderItem.setSku(item.getSku());
351             orderItem.setName(item.getName());
352             orderItem.setDescription(item.getDescription());
353             orderItem.setProperties(item.getProperties());
354             orderItem.setPrice(
355                 ShoppingUtil.calculateActualPrice(item, count.intValue()) /
356                     count.intValue());
357             orderItem.setQuantity(count.intValue());
358 
359             shoppingOrderItemPersistence.update(orderItem, false);
360         }
361 
362         order.setModifiedDate(new Date());
363         order.setTax(
364             ShoppingUtil.calculateTax(items, order.getBillingState()));
365         order.setShipping(
366             ShoppingUtil.calculateAlternativeShipping(
367                 items, cart.getAltShipping()));
368         order.setAltShipping(
369             shoppingPrefs.getAlternativeShippingName(cart.getAltShipping()));
370         order.setRequiresShipping(requiresShipping);
371         order.setInsure(cart.isInsure());
372         order.setInsurance(ShoppingUtil.calculateInsurance(items));
373         order.setCouponCodes(cart.getCouponCodes());
374         order.setCouponDiscount(
375             ShoppingUtil.calculateCouponDiscount(
376                 items, order.getBillingState(), cart.getCoupon()));
377         order.setSendOrderEmail(true);
378         order.setSendShippingEmail(true);
379 
380         shoppingOrderPersistence.update(order, false);
381 
382         return order;
383     }
384 
385     public List<ShoppingOrder> search(
386             long groupId, long companyId, long userId, String number,
387             String billingFirstName, String billingLastName,
388             String billingEmailAddress, String shippingFirstName,
389             String shippingLastName, String shippingEmailAddress,
390             String ppPaymentStatus, boolean andOperator, int start, int end)
391         throws SystemException {
392 
393         OrderDateComparator obc = new OrderDateComparator(false);
394 
395         return shoppingOrderFinder.findByG_C_U_N_PPPS(
396             groupId, companyId, userId, number, billingFirstName,
397             billingLastName, billingEmailAddress, shippingFirstName,
398             shippingLastName, shippingEmailAddress, ppPaymentStatus,
399             andOperator, start, end, obc);
400     }
401 
402     public int searchCount(
403             long groupId, long companyId, long userId, String number,
404             String billingFirstName, String billingLastName,
405             String billingEmailAddress, String shippingFirstName,
406             String shippingLastName, String shippingEmailAddress,
407             String ppPaymentStatus, boolean andOperator)
408         throws SystemException {
409 
410         return shoppingOrderFinder.countByG_C_U_N_PPPS(
411             groupId, companyId, userId, number, billingFirstName,
412             billingLastName, billingEmailAddress, shippingFirstName,
413             shippingLastName, shippingEmailAddress, ppPaymentStatus,
414             andOperator);
415     }
416 
417     public void sendEmail(long orderId, String emailType)
418         throws PortalException, SystemException {
419 
420         ShoppingOrder order = shoppingOrderPersistence.findByPrimaryKey(
421             orderId);
422 
423         try {
424             doSendEmail(order, emailType);
425         }
426         catch (IOException ioe) {
427             throw new SystemException(ioe);
428         }
429     }
430 
431     public void sendEmail(ShoppingOrder order, String emailType)
432         throws PortalException, SystemException {
433 
434         try {
435             doSendEmail(order, emailType);
436         }
437         catch (IOException ioe) {
438             throw new SystemException(ioe);
439         }
440     }
441 
442     public ShoppingOrder updateLatestOrder(
443             long userId, long groupId, String billingFirstName,
444             String billingLastName, String billingEmailAddress,
445             String billingCompany, String billingStreet, String billingCity,
446             String billingState, String billingZip, String billingCountry,
447             String billingPhone, boolean shipToBilling,
448             String shippingFirstName, String shippingLastName,
449             String shippingEmailAddress, String shippingCompany,
450             String shippingStreet, String shippingCity, String shippingState,
451             String shippingZip, String shippingCountry, String shippingPhone,
452             String ccName, String ccType, String ccNumber, int ccExpMonth,
453             int ccExpYear, String ccVerNumber, String comments)
454         throws PortalException, SystemException {
455 
456         ShoppingOrder order = getLatestOrder(userId, groupId);
457 
458         return updateOrder(
459             order.getOrderId(), billingFirstName, billingLastName,
460             billingEmailAddress, billingCompany, billingStreet, billingCity,
461             billingState, billingZip, billingCountry, billingPhone,
462             shipToBilling, shippingFirstName, shippingLastName,
463             shippingEmailAddress, shippingCompany, shippingStreet, shippingCity,
464             shippingState, shippingZip, shippingCountry, shippingPhone,
465             ccName, ccType, ccNumber, ccExpMonth, ccExpYear, ccVerNumber,
466             comments);
467     }
468 
469     public ShoppingOrder updateOrder(
470             long orderId, String billingFirstName, String billingLastName,
471             String billingEmailAddress, String billingCompany,
472             String billingStreet, String billingCity, String billingState,
473             String billingZip, String billingCountry, String billingPhone,
474             boolean shipToBilling, String shippingFirstName,
475             String shippingLastName, String shippingEmailAddress,
476             String shippingCompany, String shippingStreet, String shippingCity,
477             String shippingState, String shippingZip, String shippingCountry,
478             String shippingPhone, String ccName, String ccType, String ccNumber,
479             int ccExpMonth, int ccExpYear, String ccVerNumber, String comments)
480         throws PortalException, SystemException {
481 
482         ShoppingOrder order = shoppingOrderPersistence.findByPrimaryKey(
483             orderId);
484 
485         ShoppingPreferences shoppingPrefs = ShoppingPreferences.getInstance(
486             order.getCompanyId(), order.getGroupId());
487 
488         validate(
489             shoppingPrefs, billingFirstName, billingLastName,
490             billingEmailAddress, billingStreet, billingCity, billingState,
491             billingZip, billingCountry, billingPhone, shipToBilling,
492             shippingFirstName, shippingLastName, shippingEmailAddress,
493             shippingStreet, shippingCity, shippingState, shippingZip,
494             shippingCountry, shippingPhone, ccName, ccType, ccNumber,
495             ccExpMonth, ccExpYear, ccVerNumber);
496 
497         order.setModifiedDate(new Date());
498         order.setBillingFirstName(billingFirstName);
499         order.setBillingLastName(billingLastName);
500         order.setBillingEmailAddress(billingEmailAddress);
501         order.setBillingCompany(billingCompany);
502         order.setBillingStreet(billingStreet);
503         order.setBillingCity(billingCity);
504         order.setBillingState(billingState);
505         order.setBillingZip(billingZip);
506         order.setBillingCountry(billingCountry);
507         order.setBillingPhone(billingPhone);
508         order.setShipToBilling(shipToBilling);
509 
510         if (shipToBilling) {
511             order.setShippingFirstName(billingFirstName);
512             order.setShippingLastName(billingLastName);
513             order.setShippingEmailAddress(billingEmailAddress);
514             order.setShippingCompany(billingCompany);
515             order.setShippingStreet(billingStreet);
516             order.setShippingCity(billingCity);
517             order.setShippingState(billingState);
518             order.setShippingZip(billingZip);
519             order.setShippingCountry(billingCountry);
520             order.setShippingPhone(billingPhone);
521         }
522         else {
523             order.setShippingFirstName(shippingFirstName);
524             order.setShippingLastName(shippingLastName);
525             order.setShippingEmailAddress(shippingEmailAddress);
526             order.setShippingCompany(shippingCompany);
527             order.setShippingStreet(shippingStreet);
528             order.setShippingCity(shippingCity);
529             order.setShippingState(shippingState);
530             order.setShippingZip(shippingZip);
531             order.setShippingCountry(shippingCountry);
532             order.setShippingPhone(shippingPhone);
533         }
534 
535         order.setCcName(ccName);
536         order.setCcType(ccType);
537         order.setCcNumber(ccNumber);
538         order.setCcExpMonth(ccExpMonth);
539         order.setCcExpYear(ccExpYear);
540         order.setCcVerNumber(ccVerNumber);
541         order.setComments(comments);
542 
543         shoppingOrderPersistence.update(order, false);
544 
545         return order;
546     }
547 
548     public ShoppingOrder updateOrder(
549             long orderId, String ppTxnId, String ppPaymentStatus,
550             double ppPaymentGross, String ppReceiverEmail, String ppPayerEmail)
551         throws PortalException, SystemException {
552 
553         ShoppingOrder order = shoppingOrderPersistence.findByPrimaryKey(
554             orderId);
555 
556         order.setModifiedDate(new Date());
557         order.setPpTxnId(ppTxnId);
558         order.setPpPaymentStatus(ppPaymentStatus);
559         order.setPpPaymentGross(ppPaymentGross);
560         order.setPpReceiverEmail(ppReceiverEmail);
561         order.setPpPayerEmail(ppPayerEmail);
562 
563         shoppingOrderPersistence.update(order, false);
564 
565         return order;
566     }
567 
568     protected void doSendEmail(ShoppingOrder order, String emailType)
569         throws IOException, PortalException, SystemException {
570 
571         ShoppingPreferences shoppingPrefs = ShoppingPreferences.getInstance(
572             order.getCompanyId(), order.getGroupId());
573 
574         if (emailType.equals("confirmation") &&
575             shoppingPrefs.getEmailOrderConfirmationEnabled()) {
576         }
577         else if (emailType.equals("shipping") &&
578                  shoppingPrefs.getEmailOrderShippingEnabled()) {
579         }
580         else {
581             return;
582         }
583 
584         Company company = companyPersistence.findByPrimaryKey(
585             order.getCompanyId());
586 
587         User user = userPersistence.findByPrimaryKey(order.getUserId());
588 
589         Currency currency = Currency.getInstance(shoppingPrefs.getCurrencyId());
590 
591         String billingAddress =
592             order.getBillingFirstName() + " " + order.getBillingLastName() +
593                 "<br>" +
594             order.getBillingEmailAddress() + "<br>" +
595             order.getBillingStreet() + "<br>" +
596             order.getBillingCity() + "<br>" +
597             order.getBillingState() + "<br>" +
598             order.getBillingZip() + "<br>" +
599             order.getBillingCountry() + "<br>" +
600             order.getBillingPhone() + "<br>";
601 
602         String shippingAddress =
603             order.getShippingFirstName() + " " + order.getShippingLastName() +
604                 "<br>" +
605             order.getShippingEmailAddress() + "<br>" +
606             order.getShippingStreet() + "<br>" +
607             order.getShippingCity() + "<br>" +
608             order.getShippingState() + "<br>" +
609             order.getShippingZip() + "<br>" +
610             order.getShippingCountry() + "<br>" +
611             order.getShippingPhone() + "<br>";
612 
613         double total = ShoppingUtil.calculateTotal(order);
614 
615         String portletName = PortalUtil.getPortletTitle(
616             PortletKeys.SHOPPING, user);
617 
618         String fromName = shoppingPrefs.getEmailFromName();
619         String fromAddress = shoppingPrefs.getEmailFromAddress();
620 
621         String toName = user.getFullName();
622         String toAddress = user.getEmailAddress();
623 
624         String subject = null;
625         String body = null;
626 
627         if (emailType.equals("confirmation")) {
628             subject = shoppingPrefs.getEmailOrderConfirmationSubject();
629             body = shoppingPrefs.getEmailOrderConfirmationBody();
630         }
631         else if (emailType.equals("shipping")) {
632             subject = shoppingPrefs.getEmailOrderShippingSubject();
633             body = shoppingPrefs.getEmailOrderShippingBody();
634         }
635 
636         subject = StringUtil.replace(
637             subject,
638             new String[] {
639                 "[$FROM_ADDRESS$]",
640                 "[$FROM_NAME$]",
641                 "[$ORDER_BILLING_ADDRESS$]",
642                 "[$ORDER_CURRENCY$]",
643                 "[$ORDER_NUMBER$]",
644                 "[$ORDER_SHIPPING_ADDRESS$]",
645                 "[$ORDER_TOTAL$]",
646                 "[$PORTAL_URL$]",
647                 "[$PORTLET_NAME$]",
648                 "[$TO_ADDRESS$]",
649                 "[$TO_NAME$]"
650             },
651             new String[] {
652                 fromAddress,
653                 fromName,
654                 billingAddress,
655                 currency.getSymbol(),
656                 order.getNumber(),
657                 shippingAddress,
658                 String.valueOf(total),
659                 company.getVirtualHost(),
660                 portletName,
661                 toAddress,
662                 toName
663             });
664 
665         body = StringUtil.replace(
666             body,
667             new String[] {
668                 "[$FROM_ADDRESS$]",
669                 "[$FROM_NAME$]",
670                 "[$ORDER_BILLING_ADDRESS$]",
671                 "[$ORDER_CURRENCY$]",
672                 "[$ORDER_NUMBER$]",
673                 "[$ORDER_SHIPPING_ADDRESS$]",
674                 "[$ORDER_TOTAL$]",
675                 "[$PORTAL_URL$]",
676                 "[$PORTLET_NAME$]",
677                 "[$TO_ADDRESS$]",
678                 "[$TO_NAME$]"
679             },
680             new String[] {
681                 fromAddress,
682                 fromName,
683                 billingAddress,
684                 currency.getSymbol(),
685                 order.getNumber(),
686                 shippingAddress,
687                 String.valueOf(total),
688                 company.getVirtualHost(),
689                 portletName,
690                 toAddress,
691                 toName
692             });
693 
694         InternetAddress from = new InternetAddress(fromAddress, fromName);
695 
696         InternetAddress to = new InternetAddress(toAddress, toName);
697 
698         MailMessage message = new MailMessage(from, to, subject, body, true);
699 
700         mailService.sendEmail(message);
701 
702         if (emailType.equals("confirmation") && order.isSendOrderEmail()) {
703             order.setSendOrderEmail(false);
704 
705             shoppingOrderPersistence.update(order, false);
706         }
707         else if (emailType.equals("shipping") &&
708                  order.isSendShippingEmail()) {
709 
710             order.setSendShippingEmail(false);
711 
712             shoppingOrderPersistence.update(order, false);
713         }
714     }
715 
716     protected String getNumber() throws SystemException {
717         String number =
718             PwdGenerator.getPassword(PwdGenerator.KEY1 + PwdGenerator.KEY2, 12);
719 
720         try {
721             shoppingOrderPersistence.findByNumber(number);
722 
723             return getNumber();
724         }
725         catch (NoSuchOrderException nsoe) {
726             return number;
727         }
728     }
729 
730     protected void validate(
731             ShoppingPreferences shoppingPrefs, String billingFirstName,
732             String billingLastName, String billingEmailAddress,
733             String billingStreet, String billingCity, String billingState,
734             String billingZip, String billingCountry, String billingPhone,
735             boolean shipToBilling, String shippingFirstName,
736             String shippingLastName, String shippingEmailAddress,
737             String shippingStreet, String shippingCity, String shippingState,
738             String shippingZip, String shippingCountry, String shippingPhone,
739             String ccName, String ccType, String ccNumber, int ccExpMonth,
740             int ccExpYear, String ccVerNumber)
741         throws PortalException {
742 
743         if (Validator.isNull(billingFirstName)) {
744             throw new BillingFirstNameException();
745         }
746         else if (Validator.isNull(billingLastName)) {
747             throw new BillingLastNameException();
748         }
749         else if (!Validator.isEmailAddress(billingEmailAddress)) {
750             throw new BillingEmailAddressException();
751         }
752         else if (Validator.isNull(billingStreet)) {
753             throw new BillingStreetException();
754         }
755         else if (Validator.isNull(billingCity)) {
756             throw new BillingCityException();
757         }
758         else if (Validator.isNull(billingState)) {
759             throw new BillingStateException();
760         }
761         else if (Validator.isNull(billingZip)) {
762             throw new BillingZipException();
763         }
764         else if (Validator.isNull(billingCountry)) {
765             throw new BillingCountryException();
766         }
767         else if (Validator.isNull(billingPhone)) {
768             throw new BillingPhoneException();
769         }
770 
771         if (!shipToBilling) {
772             if (Validator.isNull(shippingFirstName)) {
773                 throw new ShippingFirstNameException();
774             }
775             else if (Validator.isNull(shippingLastName)) {
776                 throw new ShippingLastNameException();
777             }
778             else if (!Validator.isEmailAddress(shippingEmailAddress)) {
779                 throw new ShippingEmailAddressException();
780             }
781             else if (Validator.isNull(shippingStreet)) {
782                 throw new ShippingStreetException();
783             }
784             else if (Validator.isNull(shippingCity)) {
785                 throw new ShippingCityException();
786             }
787             else if (Validator.isNull(shippingState)) {
788                 throw new ShippingStateException();
789             }
790             else if (Validator.isNull(shippingZip)) {
791                 throw new ShippingZipException();
792             }
793             else if (Validator.isNull(shippingCountry)) {
794                 throw new ShippingCountryException();
795             }
796             else if (Validator.isNull(shippingPhone)) {
797                 throw new ShippingPhoneException();
798             }
799         }
800 
801         if ((!shoppingPrefs.usePayPal()) &&
802             (shoppingPrefs.getCcTypes().length > 0)) {
803 
804             if (Validator.isNull(ccName)) {
805                 throw new CCNameException();
806             }
807             else if (Validator.isNull(ccType)) {
808                 throw new CCTypeException();
809             }
810             else if (!CreditCard.isValid(ccNumber, ccType)) {
811                 throw new CCNumberException();
812             }
813             else if (!CalendarUtil.isFuture(ccExpMonth, ccExpYear)) {
814                 throw new CCExpirationException();
815             }
816         }
817     }
818 
819 }