Exemple #1
0
def get_methods():
  '''
    Fires off shipper.Shipper() for each choice that's
    enabled in /settings/
  '''

  return [shipper.Shipper(service_type=value) for value in config_choice_values('satchmo.shipping.modules.fedex', 'SHIPPING_CHOICES')]
Exemple #2
0
def satchmo_language_selection_form():
    """
    Display the set language form, if enabled in shop settings.
    """
    enabled = config_value('LANGUAGE', 'ALLOW_TRANSLATION')
    languages = []
    if enabled:
        try:
            url = urlresolvers.reverse('satchmo_set_language')
            languages = config_choice_values('LANGUAGE', 'LANGUAGES_AVAILABLE')

        except urlresolvers.NoReverseMatch:
            url = ""
            log.warning('No url found for satchmo_set_language (OK if running tests)')

    else:
        url = ""

    return {
        'enabled': enabled,
        'set_language_url': url,
        'languages': languages,
    }
Exemple #3
0
def satchmo_language_selection_form():
    """
    Display the set language form, if enabled in shop settings.
    """
    enabled = config_value('LANGUAGE', 'ALLOW_TRANSLATION')
    languages = []
    if enabled:
        try:
            url = urlresolvers.reverse('satchmo_set_language')
            languages = config_choice_values('LANGUAGE', 'LANGUAGES_AVAILABLE')

        except urlresolvers.NoReverseMatch:
            url = ""
            log.warning(
                'No url found for satchmo_set_language (OK if running tests)')

    else:
        url = ""

    return {
        'enabled': enabled,
        'set_language_url': url,
        'languages': languages,
    }
Exemple #4
0
class Order(models.Model):
    """
    Orders contain a copy of all the information at the time the order was
    placed.
    """
    contact = models.ForeignKey(Contact)
    ship_street1 = models.CharField(_("Street"), max_length=50, blank=True)
    ship_street2 = models.CharField(_("Street"), max_length=50, blank=True)
    ship_city = models.CharField(_("City"), max_length=50, blank=True)
    ship_state = models.CharField(_("State"), max_length=50, blank=True)
    ship_postal_code = models.CharField(_("Zip Code"),
                                        max_length=10,
                                        blank=True)
    ship_country = models.CharField(_("Country"), max_length=50, blank=True)
    bill_street1 = models.CharField(_("Street"), max_length=50, blank=True)
    bill_street2 = models.CharField(_("Street"), max_length=50, blank=True)
    bill_city = models.CharField(_("City"), max_length=50, blank=True)
    bill_state = models.CharField(_("State"), max_length=50, blank=True)
    bill_postal_code = models.CharField(_("Zip Code"),
                                        max_length=10,
                                        blank=True)
    bill_country = models.CharField(_("Country"), max_length=50, blank=True)
    notes = models.TextField(_("Notes"), max_length=100, blank=True, null=True)
    sub_total = models.DecimalField(_("Subtotal"),
                                    max_digits=6,
                                    decimal_places=2,
                                    blank=True,
                                    null=True)
    total = models.DecimalField(_("Total"),
                                max_digits=6,
                                decimal_places=2,
                                blank=True,
                                null=True)
    discount_code = models.CharField(_("Discount Code"),
                                     max_length=20,
                                     blank=True,
                                     null=True,
                                     help_text=_("Coupon Code"))
    discount = models.DecimalField(_("Discount amount"),
                                   max_digits=6,
                                   decimal_places=2,
                                   blank=True,
                                   null=True)
    method = models.CharField(_("Order method"),
                              choices=ORDER_CHOICES,
                              max_length=50,
                              blank=True)
    shipping_description = models.CharField(_("Shipping Description"),
                                            max_length=50,
                                            blank=True,
                                            null=True)
    shipping_method = models.CharField(_("Shipping Method"),
                                       max_length=50,
                                       blank=True,
                                       null=True)
    shipping_model = models.CharField(_("Shipping Models"),
                                      choices=config_choice_values(
                                          'SHIPPING', 'MODULES'),
                                      max_length=30,
                                      blank=True,
                                      null=True)
    shipping_cost = models.DecimalField(_("Shipping Cost"),
                                        max_digits=6,
                                        decimal_places=2,
                                        blank=True,
                                        null=True)
    tax = models.DecimalField(_("Tax"),
                              max_digits=6,
                              decimal_places=2,
                              blank=True,
                              null=True)
    timestamp = models.DateTimeField(_("Timestamp"), blank=True, null=True)
    status = models.CharField(_("Status"),
                              max_length=20,
                              choices=ORDER_STATUS,
                              core=True,
                              blank=True,
                              help_text=_("This is set automatically."))

    def __unicode__(self):
        return "Order #%s: %s" % (self.id, self.contact.full_name)

    def add_status(self, status=None, notes=None):
        orderstatus = OrderStatus()
        if not status:
            if self.orderstatus_set.count() > 0:
                curr_status = self.orderstatus_set.all().order_by(
                    '-timestamp')[0]
                status = curr_status.status
            else:
                status = 'Pending'

        orderstatus.status = status
        orderstatus.notes = notes
        orderstatus.timestamp = datetime.datetime.now()
        orderstatus.order = self
        orderstatus.save()

    def copy_addresses(self):
        """
        Copy the addresses so we know what the information was at time of order.
        """
        shipaddress = self.contact.shipping_address
        billaddress = self.contact.billing_address
        self.ship_street1 = shipaddress.street1
        self.ship_street2 = shipaddress.street2
        self.ship_city = shipaddress.city
        self.ship_state = shipaddress.state
        self.ship_postal_code = shipaddress.postal_code
        self.ship_country = shipaddress.country
        self.bill_street1 = billaddress.street1
        self.bill_street2 = billaddress.street2
        self.bill_city = billaddress.city
        self.bill_state = billaddress.state
        self.bill_postal_code = billaddress.postal_code
        self.bill_country = billaddress.country

    def remove_all_items(self):
        """Delete all items belonging to this order."""
        for item in self.orderitem_set.all():
            item.delete()
        self.save()

    def _balance(self):
        payments = [p.amount for p in self.payments.all()]
        if payments:
            paid = reduce(operator.add, payments)
            return self.total - paid
        return self.total

    balance = property(fget=_balance)

    def balance_forward(self):
        return moneyfmt(self.balance)

    balance_forward = property(fget=balance_forward)

    def _credit_card(self):
        """Return the credit card associated with this payment."""
        for payment in self.payments.order_by('-timestamp'):
            try:
                if payment.creditcards.count() > 0:
                    return payment.creditcards.get()
            except payments.creditcards.model.DoesNotExist:
                pass
        return None

    credit_card = property(_credit_card)

    def _full_bill_street(self, delim="<br/>"):
        """Return both billing street entries separated by delim."""
        if self.bill_street2:
            return self.bill_street1 + delim + self.bill_street2
        else:
            return self.bill_street1

    full_bill_street = property(_full_bill_street)

    def _full_ship_street(self, delim="<br/>"):
        """Return both shipping street entries separated by delim."""
        if self.ship_street2:
            return self.ship_street1 + delim + self.ship_street2
        else:
            return self.ship_street1

    full_ship_street = property(_full_ship_street)

    def save(self):
        """
        Copy addresses from contact. If the order has just been created, set
        the create_date.
        """
        if not self.id:
            self.timestamp = datetime.datetime.now()

        self.copy_addresses()
        super(Order, self).save()  # Call the "real" save() method.

    def invoice(self):
        return ('<a href="/admin/print/invoice/%s/">View</a>' % self.id)

    invoice.allow_tags = True

    def packingslip(self):
        return ('<a href="/admin/print/packingslip/%s/">View</a>' % self.id)

    packingslip.allow_tags = True

    def recalculate_total(self, save=True):
        """Calculates subtotal, taxes and total."""
        #Can't really do a full shipping recalc - shipping is bound to the cart.

        discount_amount = Decimal("0.00")
        if self.discount_code:
            try:
                discount = Discount.objects.filter(code=self.discount_code)[0]
                if discount:
                    if discount.freeShipping:
                        self.shipping_cost = Decimal("0.00")
                    discount_amount = discount.calc(self)

            except Discount.DoesNotExist:
                pass

        self.discount = discount_amount

        itemprices = [
            item.line_item_price for item in self.orderitem_set.all()
        ]
        if itemprices:
            subtotal = reduce(operator.add, itemprices)
        else:
            subtotal = Decimal('0.00')

        self.sub_total = subtotal

        taxProcessor = tax.get_processor(self)
        self.tax = taxProcessor.process()

        log.debug("recalc: subtotal=%s, shipping=%s, discount=%s, tax=%s",
                  subtotal, self.shipping_cost, self.discount, self.tax)
        self.total = subtotal + self.shipping_cost - self.discount + self.tax

        if save:
            self.save()

    def shippinglabel(self):
        return ('<a href="/admin/print/shippinglabel/%s/">View</a>' % self.id)

    shippinglabel.allow_tags = True

    def _order_total(self):
        #Needed for the admin list display
        return moneyfmt(self.total)

    order_total = property(_order_total)

    def order_success(self):
        """Run each item's order_success method."""
        for orderitem in self.orderitem_set.all():
            subtype = orderitem.product.get_subtype_with_attr('order_success')
            if subtype:
                subtype.order_success(self, orderitem)

    def _paid_in_full(self):
        """True if total has been paid"""
        return self.balance <= 0

    paid_in_full = property(fget=_paid_in_full)

    def _has_downloads(self):
        """Determine if there are any downloadable products on this order"""
        if self.downloadlink_set.count() > 0:
            return True
        return False

    has_downloads = property(_has_downloads)

    def _is_shippable(self):
        """Determine if we will be shipping any items on this order """
        for orderitem in self.orderitem_set.all():
            if orderitem.product.is_shippable:
                return True
        return False

    is_shippable = property(_is_shippable)

    def validate(self, request):
        """
        Return whether the order is valid.
        Not guaranteed to be side-effect free.
        """
        valid = True
        for orderitem in self.orderitem_set.all():
            for subtype_name in orderitem.product.get_subtypes():
                subtype = getattr(orderitem.product, subtype_name.lower())
                validate_method = getattr(subtype, 'validate_order', None)
                if validate_method:
                    valid = valid and validate_method(request, self, orderitem)
        return valid

    class Admin:
        fields = ((None, {
            'fields': ('contact', 'method', 'status', 'notes')
        }), (_('Shipping Method'), {
            'fields': ('shipping_method', 'shipping_description')
        }), (_('Shipping Address'), {
            'classes':
            'collapse',
            'fields': ('ship_street1', 'ship_street2', 'ship_city',
                       'ship_state', 'ship_postal_code', 'ship_country')
        }), (_('Billing Address'), {
            'classes':
            'collapse',
            'fields': ('bill_street1', 'bill_street2', 'bill_city',
                       'bill_state', 'bill_postal_code', 'bill_country')
        }), (_('Totals'), {
            'fields': ('sub_total', 'shipping_cost', 'tax', 'discount',
                       'total', 'timestamp')
        }))
        list_display = ('contact', 'timestamp', 'order_total',
                        'balance_forward', 'status', 'invoice', 'packingslip',
                        'shippinglabel')
        list_filter = ['timestamp', 'contact']
        date_hierarchy = 'timestamp'

    class Meta:
        verbose_name = _("Product Order")
        verbose_name_plural = _("Product Orders")
Exemple #5
0
def get_methods():
    return [shipper.Shipper(service_type=value) for value in config_choice_values('satchmo.shipping.modules.usps', 'USPS_SHIPPING_CHOICES')]
Exemple #6
0
def shipping_choices():
    try:
        return config_choice_values('SHIPPING', 'MODULES')
    except SettingNotSet:
        return ()
Exemple #7
0
def get_methods():
    return [
        shipper.Shipper(service_type=value) for value in config_choice_values(
            'satchmo.shipping.modules.ups', 'UPS_SHIPPING_CHOICES')
    ]
Exemple #8
0
def shipping_choices():
    try:
        return config_choice_values('SHIPPING', 'MODULES')
    except SettingNotSet:
        return ()