Commits

Chris Moffitt committed 2a2ad78

If a giftcertificate has an email recipient, send a notification to the recipient.
Closes #1209.

Comments (0)

Files changed (6)

satchmo/apps/payment/modules/giftcertificate/config.py

         help_text=_("False if you want to be in test mode"),
         default=False),
 
+    BooleanValue(PAYMENT_GROUP,
+        'EMAIL_RECIPIENT',
+        description=_("Send email to recipients"),
+        help_text=_("If the purchaser includes an email address, should we send a notification to them?"),
+        default=True),
+
     StringValue(PAYMENT_GROUP,
         'URL_BASE',
         description=_('The url base used for constructing urlpatterns which will use this module'),

satchmo/apps/payment/templates/shop/checkout/success.html

 <p>{% trans 'Please print out this page as a receipt.  You will also receive an email with this information.' %}</p>
     
 {% order_tracking_details order %}
+{% if gc_email_sent %}<p>{% trans 'The recipients of your gift certificates will receive an email notification.' %}</p>{% endif %}
 </div>
 {% endblock %}
 

satchmo/apps/payment/utils.py

 from decimal import Decimal
-from livesettings import config_get_group
+from livesettings import config_get_group, config_value
 from satchmo_store.shop.models import Order, OrderItem, OrderItemDetail, OrderCart
 from satchmo_store.shop.signals import satchmo_post_copy_item_to_order
 from shipping.utils import update_shipping
+from satchmo_store.mail import send_store_mail
+from django.utils.translation import ugettext as _
 import logging
 
 log = logging.getLogger('payment.utils')
                     )
 
     new_order.recalculate_total()
+
+def send_gift_certificate_by_email(gc):
+    """ Helper function to send email messages to gift certificate recipients
+    """
+    ctx = {
+        'message': gc.message,
+        'addressee': gc.recipient_email,
+        'code': gc.code,
+        'balance':gc.balance,
+        'purchased_by': gc.purchased_by
+        }
+    subject = _('Your Gift Certificate')
+
+    send_store_mail(
+        subject,
+        ctx,
+        template = 'shop/email/gift_certificate_recipient.txt',
+        template_html='shop/email/gift_certificate_recipient.html',
+        recipients_list=[gc.recipient_email,],
+        fail_silently=True,
+        )
+
+def gift_certificate_processor(order):
+    """ If an order has gift certificates, then we'll try to send emails to the recipients
+    """
+    from payment.modules.giftcertificate.models import GiftCertificate
+    email_sent = False
+    send_email = config_value('PAYMENT_GIFTCERTIFICATE','EMAIL_RECIPIENT')
+    if send_email:
+        gcs = GiftCertificate.objects.filter(order=order)
+        for gc in gcs:
+            if gc.recipient_email:
+                send_gift_certificate_by_email(gc)
+                email_sent = True
+    return email_sent

satchmo/apps/payment/views/checkout.py

 from django.views.decorators.cache import never_cache
 from satchmo_store.shop.models import Order
 from satchmo_utils.views import bad_or_missing
+from payment.utils import gift_certificate_processor
 
 def success(request):
     """
         return bad_or_missing(request, _('Your order has already been processed.'))
 
     del request.session['orderID']
+    # We check to see if there are any gift certificates in the order
+    gc_email_sent = False
+    gc_in_orderitems = len(filter(lambda x: 'GiftCertificateProduct' in x.product.get_subtypes(), order.orderitem_set.all()))
+    if gc_in_orderitems:
+        gc_email_sent = gift_certificate_processor(order)
     return render_to_response('shop/checkout/success.html',
-                              {'order': order},
+                              {'order': order,
+                              'gc_email_sent': gc_email_sent},
                               context_instance=RequestContext(request))
 success = never_cache(success)
 

satchmo/apps/satchmo_store/shop/templates/shop/email/gift_certificate_recipient.html

+{% load i18n satchmo_currency satchmo_discounts %}
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html><head><title></title></head>
+<body style="font-family:'Helvetica Neue', Helvetica, Arial;font-size:90%;">
+<h1>{{ shop_name}}</h1>
+
+<p>{% blocktrans %}Congratulations, you have received a gift certificate for {{ shop_name }}.{% endblocktrans %}</p>
+<p>{% trans "Your gift certificate was purchased by"%} {{purchased_by.full_name}}.</p>
+<p>{% trans "This gift certificate is for" %} {{ balance|currency }} {% trans "and may be redeemed online using the following code" %} {{code}}</p>
+{% if message %}
+    <p>{% blocktrans %}This gift certificate includes the following message: {% endblocktrans %}</p>
+    <blockquote>{{ message|linebreaksbr }}</blockquote>
+{% endif %}
+<p>{% trans "Regards" %},</p>
+<p>{% trans "Store Management" %}</p>
+
+</body>
+</html>

satchmo/apps/satchmo_store/shop/templates/shop/email/gift_certificate_recipient.txt

+{% load i18n satchmo_currency satchmo_discounts %}
+
+{{ shop_name}}
+
+{% blocktrans %}Congratulations, you have received a gift certificate for {{ shop_name }}.{% endblocktrans %}
+{% trans "Your gift certificate was purchased by"%} {{purchased_by.full_name}}.
+{% trans "This gift certificate is for" %} {{ balance|currency }} {% trans "and may be redeemed online using the following code" %} {{code}}
+{% if message %}
+    {% blocktrans %}This gift certificate includes the following message: {% endblocktrans %}
+    {{ message }}
+{% endif %}
+
+{% trans "Regards" %},
+{% trans "Store Management" %}