def _create_from_draft(self, draft: types.CartDraft, id: typing.Optional[str] = None) -> types.Cart: object_id = str(uuid.UUID(id) if id is not None else uuid.uuid4()) if draft.line_items: line_items = [ self._create_line_item_from_draft(draft, line_item) for line_item in draft.line_items ] else: line_items = [] total_price = None taxed_price = None if line_items: total_price = types.CentPrecisionMoney( currency_code=draft.currency, cent_amount=sum(line_item.taxed_price.total_gross.cent_amount for line_item in line_items if line_item.taxed_price and line_item.taxed_price.total_gross), fraction_digits=2, ) taxed_price = types.TaxedPrice( total_net=types.CentPrecisionMoney( currency_code=draft.currency, cent_amount=sum(line_item.taxed_price.total_net.cent_amount for line_item in line_items if line_item.taxed_price and line_item.taxed_price.total_net), fraction_digits=2, ), total_gross=types.CentPrecisionMoney( currency_code=draft.currency, cent_amount=sum( line_item.taxed_price.total_gross.cent_amount for line_item in line_items if line_item.taxed_price and line_item.taxed_price.total_gross), fraction_digits=2, ), tax_portions=[ types.TaxPortion( name="0% VAT", rate=0, amount=types.CentPrecisionMoney( currency_code=draft.currency, cent_amount=0, fraction_digits=2, ), ) ], ) # Some fields such as itemShippingAddresses are currently missing. See # https://docs.commercetools.com/http-api-projects-carts for a complete overview return types.Cart( id=str(object_id), version=1, cart_state=types.CartState.ACTIVE, customer_id=draft.customer_id, customer_email=draft.customer_email, customer_group=draft.customer_group, anonymous_id=draft.anonymous_id, country=draft.country, inventory_mode=draft.inventory_mode, tax_mode=draft.tax_mode or types.TaxMode.PLATFORM, tax_rounding_mode=draft.tax_rounding_mode or types.RoundingMode.HALF_EVEN, tax_calculation_mode=draft.tax_calculation_mode or types.TaxCalculationMode.LINE_ITEM_LEVEL, line_items=line_items, custom_line_items=[], refused_gifts=[], shipping_address=draft.shipping_address, billing_address=draft.billing_address, locale=draft.locale, origin=draft.origin or types.CartOrigin.CUSTOMER, created_at=datetime.datetime.now(datetime.timezone.utc), last_modified_at=datetime.datetime.now(datetime.timezone.utc), custom=utils.create_from_draft(draft.custom), total_price=total_price, taxed_price=taxed_price, )
def _create_from_draft(self, draft: types.CartDraft, id: typing.Optional[str] = None) -> types.Cart: object_id = str(uuid.UUID(id) if id is not None else uuid.uuid4()) line_items = [ self._create_line_item_from_draft(draft, line_item) for line_item in draft.line_items ] total_price = None taxed_price = None if line_items: total_price = types.TypedMoney( type=types.MoneyType.CENT_PRECISION, currency_code=draft.currency, cent_amount=sum(line_item.taxed_price.total_gross.cent_amount for line_item in line_items), fraction_digits=2, ) taxed_price = types.TaxedPrice( total_net=types.Money( currency_code=draft.currency, cent_amount=sum(line_item.taxed_price.total_net.cent_amount for line_item in line_items), ), total_gross=types.Money( currency_code=draft.currency, cent_amount=sum( line_item.taxed_price.total_gross.cent_amount for line_item in line_items), ), tax_portions=[ types.TaxPortion( name="0% VAT", amount=types.Money(currency_code=draft.currency, cent_amount=0), ) ], ) # Some fields such as itemShippingAddresses are currently missing. See # https://docs.commercetools.com/http-api-projects-carts for a complete overview return types.Cart( id=str(object_id), version=1, cart_state=types.CartState.ACTIVE, customer_id=draft.customer_id, customer_email=draft.customer_email, customer_group=draft.customer_group, anonymous_id=draft.anonymous_id, country=draft.country, inventory_mode=draft.inventory_mode, tax_mode=draft.tax_mode, tax_rounding_mode=draft.tax_rounding_mode, tax_calculation_mode=draft.tax_calculation_mode, line_items=line_items, shipping_address=draft.shipping_address, billing_address=draft.billing_address, locale=draft.locale, origin=draft.origin, created_at=datetime.datetime.now(datetime.timezone.utc), last_modified_at=datetime.datetime.now(datetime.timezone.utc), custom=utils.create_from_draft(draft.custom), total_price=total_price, taxed_price=taxed_price, )