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