protected MoneyContext getMoneyContext(final LineItem lineItem) { return MoneyContext.of(lineItem.getPrice().getValue().getCurrency(), userContext.locale()); } }
public static PriceDraftBuilder of(final Price template) { return of(template.getValue()) .country(template.getCountry()) .customerGroup(template.getCustomerGroup()) .channel(Optional.ofNullable(template.getChannel()).map(Referenceable::toResourceIdentifier).orElse(null)) .validFrom(template.getValidFrom()) .validUntil(template.getValidUntil()) .custom(copyCustom(template)); } }
public static PriceDraftBuilder of(final Price template) { return of(template.getValue()) .country(template.getCountry()) .customerGroup(template.getCustomerGroup()) .channel(Optional.ofNullable(template.getChannel()).map(Referenceable::toResourceIdentifier).orElse(null)) .validFrom(template.getValidFrom()) .validUntil(template.getValidUntil()) .custom(copyCustom(template)); } }
public static PriceDraftDsl of(final Price template) { final ResourceIdentifier<Channel> channelResourceIdentifier = Optional.ofNullable(template.getChannel()).map(Referenceable::toResourceIdentifier).orElse(null); return PriceDraftBuilder.of(template.getValue()) .country(template.getCountry()) .customerGroup(template.getCustomerGroup()) .channel(channelResourceIdentifier) .validFrom(template.getValidFrom()) .validUntil(template.getValidUntil()) .custom(customFieldsDraftOrNull(template)) .tiers(template.getTiers()) .build(); }
public static PriceDraftDsl of(final Price template) { final ResourceIdentifier<Channel> channelResourceIdentifier = Optional.ofNullable(template.getChannel()).map(Referenceable::toResourceIdentifier).orElse(null); return PriceDraftBuilder.of(template.getValue()) .country(template.getCountry()) .customerGroup(template.getCustomerGroup()) .channel(channelResourceIdentifier) .validFrom(template.getValidFrom()) .validUntil(template.getValidUntil()) .custom(customFieldsDraftOrNull(template)) .tiers(template.getTiers()) .build(); }
@Test public void testJsonDeserialized() { final Price actual = SphereJsonUtils.readObject("{\n" + " \"value\": {\n" + " \"currencyCode\": \"EUR\",\n" + " \"centAmount\": 2800\n" + " }\n" + " }", Price.class); assertThat(actual.getValue().isEqualTo(MoneyImpl.ofCents(2800, "EUR"))); }
@Test public void setLineItemExternalPrice() throws Exception { withFilledCart(client(), (Cart cart) -> { final LineItem originalLineItem = cart.getLineItems().get(0); assertThat(originalLineItem.getPrice().getValue()).isEqualTo(MoneyImpl.ofCents(1234, EUR)); assertThat(originalLineItem.getTotalPrice()).isEqualTo(MoneyImpl.ofCents(3702, EUR)); assertThat(originalLineItem.getQuantity()).isEqualTo(3L); assertThat(originalLineItem.getPriceMode()).isEqualTo(LineItemPriceMode.PLATFORM); final String lineItemId = originalLineItem.getId(); final MonetaryAmount externalPrice = EURO_30; final SetLineItemPrice updateAction = SetLineItemPrice.of(lineItemId, externalPrice); final Cart updatedCart = client().executeBlocking(CartUpdateCommand.of(cart, updateAction)); final LineItem lineItem = updatedCart.getLineItems().get(0); assertThat(lineItem.getPrice().getValue()).isEqualTo(externalPrice); assertThat(lineItem.getPriceMode()).isEqualTo(LineItemPriceMode.EXTERNAL_PRICE); }); }
@Test public void createCartWithALineItemWithAnExternalPrice() { withProduct(client(), product -> { final int quantity = 5; final int variantId = 1; final MonetaryAmount externalPrice = EURO_12; final LineItemDraft lineItemDraft = LineItemDraft.of(product, variantId, quantity) .withExternalPrice(externalPrice); withCartWithLineItems(client(), singletonList(lineItemDraft), cart -> { final LineItem cartLineItem = cart.getLineItems().get(0); assertThat(cartLineItem.getPrice().getValue()).isEqualTo(externalPrice); assertThat(cartLineItem.getPriceMode()).isEqualTo(EXTERNAL_PRICE); return cart; }); }); }
public static PriceBuilder of(final Price template) { return of(template.getValue()) .country(template.getCountry()) .customerGroup(template.getCustomerGroup()) .channel(template.getChannel()) .discounted(template.getDiscounted()); }
public static PriceBuilder of(final Price template) { return of(template.getValue()) .country(template.getCountry()) .customerGroup(template.getCustomerGroup()) .channel(template.getChannel()) .discounted(template.getDiscounted()); }
@Test public void changeLineItemQuantityWithoutExternalTotalPrice() throws Exception { withEmptyCartAndProduct(client(), (cart, product) -> { assertThat(cart.getLineItems()).hasSize(0); final MonetaryAmount price = EURO_1; final MonetaryAmount totalPrice = EURO_5; final ExternalLineItemTotalPrice externalTotalPrice = ExternalLineItemTotalPrice.ofPriceAndTotalPrice(price, totalPrice); final AddLineItem action = AddLineItem.of(product.getId(), MASTER_VARIANT_ID, 3L).withExternalTotalPrice(externalTotalPrice); final Cart cartWith3 = client().executeBlocking(CartUpdateCommand.of(cart, action)); final LineItem lineItem = cartWith3.getLineItems().get(0); assertThat(lineItem.getQuantity()).isEqualTo(3); assertThat(lineItem.getPriceMode()).isEqualTo(EXTERNAL_TOTAL); assertThat(lineItem.getTotalPrice()).isEqualTo(totalPrice); assertThat(lineItem.getPrice().getValue()).isEqualTo(price); final Cart cartWith2 = client().executeBlocking(CartUpdateCommand.of(cartWith3, ChangeLineItemQuantity.of(lineItem, 2L))); final LineItem updatedLineItem = cartWith2.getLineItems().get(0); assertThat(updatedLineItem.getQuantity()).isEqualTo(2); assertThat(updatedLineItem.getPriceMode()).isEqualTo(PLATFORM); assertThat(updatedLineItem.getPrice().getValue()).isEqualTo(MoneyImpl.ofCents(1234, EUR)); }); }
@Test public void setLineItemPriceWithoutExternalPrice() { withEmptyCartAndProduct(client(), (cart, product) -> { assertThat(cart.getLineItems()).hasSize(0); final MonetaryAmount externalPrice = EURO_20; final AddLineItem action = AddLineItem.of(product.getId(), MASTER_VARIANT_ID, 3L).withExternalPrice(externalPrice); final Cart cartWithLineItem = client().executeBlocking(CartUpdateCommand.of(cart, action)); final LineItem lineItem = cartWithLineItem.getLineItems().get(0); assertThat(lineItem.getQuantity()).isEqualTo(3); assertThat(lineItem.getPriceMode()).isEqualTo(EXTERNAL_PRICE); assertThat(lineItem.getPrice().getValue()).isEqualTo(externalPrice); final Cart updatedCart = client().executeBlocking(CartUpdateCommand.of(cartWithLineItem, SetLineItemPrice.of(lineItem.getId()))); final LineItem updatedLineItem = updatedCart.getLineItems().get(0); assertThat(updatedLineItem.getPriceMode()).isEqualTo(PLATFORM); assertThat(updatedLineItem.getPrice().getValue()).isEqualTo(MoneyImpl.ofCents(1234, EUR)); }); }
@Test public void createCartWithALineItemWithExternalTotalPrice() { withProduct(client(), product -> { final int quantity = 5; final int variantId = 1; final MonetaryAmount price = EURO_20; final MonetaryAmount totalPrice = EURO_25; final ExternalLineItemTotalPrice externalLineItemTotalPrice = ExternalLineItemTotalPrice.ofPriceAndTotalPrice(price, totalPrice); final LineItemDraft lineItemDraft = LineItemDraft.of(product, variantId, quantity) .withExternalTotalPrice(externalLineItemTotalPrice); withCartWithLineItems(client(), singletonList(lineItemDraft), cart -> { final LineItem cartLineItem = cart.getLineItems().get(0); assertThat(cartLineItem.getPrice().getValue()).isEqualTo(price); assertThat(cartLineItem.getTotalPrice()).isEqualTo(totalPrice); assertThat(cartLineItem.getPriceMode()).isEqualTo(EXTERNAL_TOTAL); return cart; }); }); }
/** * Creates a new object initialized with the fields of the template parameter. * * @param template the template * @return a new object initialized from the template */ public static PriceBuilder of(final Price template) { return new PriceBuilder(template.getChannel(), template.getCountry(), template.getCustom(), template.getCustomerGroup(), template.getDiscounted(), template.getId(), template.getTiers(), template.getValidFrom(), template.getValidUntil(), template.getValue()); } }
@Test public void removeLineItemWithExternalPrice() throws Exception { withProduct(client(), product -> { final int quantity = 5; final int variantId = 1; final MonetaryAmount initialExternalPrice = EURO_20; final LineItemDraft lineItemDraft = LineItemDraft.of(product, variantId, quantity).withExternalPrice(initialExternalPrice); withCartWithLineItems(client(), singletonList(lineItemDraft), cart -> { final LineItem cartLineItem = cart.getLineItems().get(0); assertThat(cartLineItem.getQuantity()).isEqualTo(quantity); final MonetaryAmount newExternalPrice = EURO_25; final Cart updatedCart = client().executeBlocking(CartUpdateCommand.of(cart, RemoveLineItem.ofLineItemAndExternalPrice(cartLineItem, 4L, newExternalPrice))); final LineItem updatedLineItem = updatedCart.getLineItems().get(0); assertThat(updatedLineItem.getQuantity()).isEqualTo(1); assertThat(updatedLineItem.getPrice().getValue()).isEqualTo(newExternalPrice); assertThat(cartLineItem.getPriceMode()).isEqualTo(EXTERNAL_PRICE); return updatedCart; }); }); }
@Test public void addLineItemWithExternalPrice() { withEmptyCartAndProduct(client(), (cart, product) -> { assertThat(cart.getLineItems()).isEmpty(); final int quantity = 5; final int variantId = 1; final String productId = product.getId(); final MonetaryAmount externalPrice = EURO_30; final AddLineItem updateAction = AddLineItem.of(productId, variantId, quantity) .withExternalPrice(externalPrice); final Cart cartWithLineItem = client().executeBlocking(CartUpdateCommand.of(cart, updateAction)); final LineItem lineItem = cartWithLineItem.getLineItems().get(0); assertThat(lineItem.getPrice().getValue()).isEqualTo(externalPrice); assertThat(lineItem.getPriceMode()).isEqualTo(EXTERNAL_PRICE); }); }
@Test public void addLineItemWithExternalTotalPrice() { withEmptyCartAndProduct(client(), (cart, product) -> { assertThat(cart.getLineItems()).isEmpty(); final int quantity = 5; final int variantId = 1; final String productId = product.getId(); final MonetaryAmount price = EURO_20; final MonetaryAmount totalPrice = EURO_25; final ExternalLineItemTotalPrice externalLineItemTotalPrice = ExternalLineItemTotalPrice.ofPriceAndTotalPrice(price, totalPrice); final AddLineItem updateAction = AddLineItem.of(productId, variantId, quantity) .withExternalTotalPrice(externalLineItemTotalPrice); final Cart cartWithLineItem = client().executeBlocking(CartUpdateCommand.of(cart, updateAction)); final LineItem lineItem = cartWithLineItem.getLineItems().get(0); assertThat(lineItem.getPrice().getValue()).isEqualTo(price); assertThat(lineItem.getTotalPrice()).isEqualTo(totalPrice); assertThat(lineItem.getPriceMode()).isEqualTo(EXTERNAL_TOTAL); }); }
public static PriceDraftDsl of(final Price template) { return PriceDraftBuilder.of(template.getValue()) .country(template.getCountry()) .customerGroup(template.getCustomerGroup()) .channel(template.getChannel()) .validFrom(template.getValidFrom()) .validUntil(template.getValidUntil()) .custom(customFieldsDraftOrNull(template)) .build(); }
public static PriceBuilder of(final Price template) { return of(template.getValue()) .country(template.getCountry()) .customerGroup(template.getCustomerGroup()) .channel(template.getChannel()) .discounted(template.getDiscounted()) .validFrom(template.getValidFrom()) .validUntil(template.getValidUntil()) .id(template.getId()); }
@Test public void createProductWithCustomPrice() { withEmptyProductType(client(), randomKey(), productType -> withUpdateableType(client(), type -> { final String value = "foo"; final PriceDraft price = PriceDraft.of(EURO_1) .withCustom(CustomFieldsDraft.ofTypeIdAndObjects(type.getId(), singletonMap(STRING_FIELD_NAME, value))); final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of().price(price).build(); final ProductDraft productDraft = ProductDraftBuilder.of(productType, randomSlug(), randomSlug(), masterVariant).build(); final Product product = client().executeBlocking(ProductCreateCommand.of(productDraft)); final Price loadedPrice = product.getMasterData().getStaged().getMasterVariant().getPrices().get(0); assertThat(loadedPrice.getValue()).isEqualTo(EURO_1); assertThat(loadedPrice.getCustom().getFieldAsString(STRING_FIELD_NAME)).isEqualTo(value); client().executeBlocking(ProductDeleteCommand.of(product)); return type; }) ); }