ptcoffee_django/core/shipping.py
2023-11-20 08:21:57 -07:00

86 lines
2.1 KiB
Python

import logging
from decimal import Decimal
from django.conf import settings
from django.db.models import Q
from measurement.measures import Weight
from core.xps import get_quote
from core.models import (
ShippingRate,
SiteSettings,
)
from core import (
ShippingService,
ShippingProvider,
ShippingContainer
)
logger = logging.getLogger(__name__)
def get_shipping_container_choices_from_weight(weight):
is_selectable = Q(
is_selectable=True
)
min_weight_matched = Q(
min_order_weight__lte=weight) | Q(
min_order_weight__isnull=True
)
max_weight_matched = Q(
max_order_weight__gte=weight) | Q(
max_order_weight__isnull=True
)
containers = ShippingRate.objects.filter(
is_selectable & min_weight_matched & max_weight_matched
)
return containers
def get_shipping_container_from_choices(choices):
if len(choices) == 0:
return SiteSettings.load().default_shipping_rate.container
return choices[0].container
# - A 48 36
# - B 80 48 60 72 64 80 96
# - Small mailing box: 12 24 16 32
# - Shoe box: 48 64
# - Large flat rate box: 96 128
def get_shipping_cost(total_weight, postal_code):
if not total_weight > Weight(lb=0):
return Decimal('0.00')
container = "box_a"
if total_weight <= Weight(lb=3):
container = "box_a"
elif total_weight <= Weight(lb=6):
container = "box_b"
else:
container = "large_flat_rate_box"
shipping_cost = get_quote(postal_code, str(total_weight.lb), container)
return Decimal(shipping_cost)
def build_usps_rate_request(weight, container, zip_destination):
service = ShippingContainer.get_shipping_service_from_container(container)
return \
{
'service': service,
'zip_origination': SiteSettings.load().default_zip_origination,
'zip_destination': zip_destination,
'pounds': weight,
'ounces': '0',
'container': container,
'width': '',
'length': '',
'height': '',
'girth': '',
'machinable': 'TRUE'
}