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