Repository URL to install this package:
|
Version:
0.4.198 ▾
|
lib-py-b2b
/
ship_request.py
|
|---|
from .errors import ContainerContentsError
from .package import Package
from .container import Container
import logging
from os import environ
from aws_xray_sdk.core import xray_recorder
logger = logging.getLogger(__name__)
class ShipRequest:
def ship(self):
pass
class OrderShipRequest(ShipRequest):
def __init__(self, ccn: str, sales_order: str, sales_order_line: str, inventory_id: str =None, quantity: int = 1, force = False):
self.ccn = ccn
self.sales_order = sales_order
self.sales_order_line = sales_order_line
self.inventory_id = inventory_id
self.quantity = quantity
self.force = force
@xray_recorder.capture()
def ship(self):
"""
First create a container for the order line, then create a ContainerShipRequest, and ship it.
:return:
"""
# Create containers then do the normal flow
container = Container.create_for_order_line(ccn=self.ccn,
sales_order=self.sales_order,
sales_order_line=self.sales_order_line,
inventory_id=self.inventory_id,
force=self.force)
container_ship_request = ContainerShipRequest(ccn=self.ccn,
container_id=container.container_id,
force=self.force)
return container_ship_request.ship()
class ContainerShipRequest(ShipRequest):
def __init__(self, ccn: str, container_id: str, container_support=True, force=False):
self.ccn = ccn
self.container_id = container_id
self.container_support = container_support
self.force = force
@xray_recorder.capture()
def ship(self):
from .carrier import CarrierIntegration, CarrierType
from .orders import Orders
from .profile import Profile
from .fulfillment import Fulfillment
xray_recorder.begin_subsegment('setup_data')
container = Container.fetch(ccn=self.ccn, container_id=self.container_id)
customer_edi_id = container.customer_edi_id
profile = Profile.profile_for(customer=customer_edi_id)
carrier_type = CarrierType(profile.fulfillment_config.carrier_type)
_carrier = CarrierIntegration.get_carrier(profile=profile, carrier_type=carrier_type, container_support=self.container_support)
if not container.contents:
raise ContainerContentsError(f"The scanned container has no contents. [{container.ccn}, {container.container_id}]")
# Assuming that a container will not span orders - per Dennis :)
b2b_order_id = container.contents[0].get_b2b_order_id()
edi_order = Orders.for_(b2b_order_id)
xray_recorder.end_subsegment()
# xray_recorder.begin_subsegment('validate shipment')
# for content in container.contents:
# lines = list(filter(lambda x: (x['product_id'] == content.cus_item) or (x['product_id'] == content.item),
# edi_order['order_lines']))
# if lines:
# order_qty = sum(l['quantity'] for l in lines)
# if content.quantity > order_qty:
# raise InvalidQuantityError(f"The quantity {content.quantity} requested exceeds "
# f"the order amount ({order_qty}) for order {b2b_order_id}")
# else:
# raise ContainerContentsError(f"The contents [{content.item}] is not found on the b2b order [{b2b_order_id}]")
# xray_recorder.end_subsegment()
# MARK: - Package Validation and Rule Processing
xray_recorder.begin_subsegment('packaging')
package = Package.create_from(profile, container)
package.validate()
logger.info(f"Container:[{container}], Package: [{str(package)}]")
xray_recorder.end_subsegment()
xray_recorder.begin_subsegment('create_fulfillment')
fulfillment = Fulfillment.create(container=container, order=edi_order, force=self.force)
xray_recorder.end_subsegment()
xray_recorder.begin_subsegment('create_shipping_records')
_carrier.create_shipment(fulfillment=fulfillment)
xray_recorder.end_subsegment()
updated_fulfillment = Fulfillment.for_(fulfillment['id'])
return updated_fulfillment