flamingo.me/flamingo-commerce/v3@v3.11.0/cart/interfaces/graphql/dto/cartSummary.go (about)

     1  package dto
     2  
     3  import (
     4  	"flamingo.me/flamingo-commerce/v3/cart/domain/cart"
     5  	"flamingo.me/flamingo-commerce/v3/price/domain"
     6  )
     7  
     8  type (
     9  	// CartSummary provides custom graphql interface methods
    10  	CartSummary struct {
    11  		cart *cart.Cart
    12  	}
    13  )
    14  
    15  // Discounts collects up discounts of cart based on its deliveries
    16  // All discounts with the same campaign code are aggregated and returned as one with a summed price
    17  func (cs *CartSummary) Discounts() *CartAppliedDiscounts {
    18  	result, err := cs.cart.MergeDiscounts()
    19  
    20  	if err != nil {
    21  		return nil
    22  	}
    23  
    24  	return &CartAppliedDiscounts{discounts: result}
    25  }
    26  
    27  // HasAppliedDiscounts check whether there are any discounts currently applied to the cart
    28  func (cs *CartSummary) HasAppliedDiscounts() bool {
    29  	result, _ := cs.cart.HasAppliedDiscounts()
    30  	return result
    31  }
    32  
    33  // SumTotalDiscountWithGiftCardsAmount returns sum price of total discounts with applied gift cards
    34  func (cs *CartSummary) SumTotalDiscountWithGiftCardsAmount() domain.Price {
    35  	totalDiscount := cs.cart.TotalDiscountAmount
    36  	appliedGiftCardsAmount := cs.cart.TotalGiftCardAmount
    37  
    38  	price, _ := totalDiscount.Sub(appliedGiftCardsAmount)
    39  	return price
    40  }
    41  
    42  // TotalDiscountAmount returns the sum of the applied values of the AppliedDiscounts
    43  func (cs CartSummary) TotalDiscountAmount() *domain.Price {
    44  	sum := cs.cart.TotalDiscountAmount
    45  
    46  	return &sum
    47  }
    48  
    49  // TotalGiftCardAmount sums applied gift cards
    50  func (cs CartSummary) TotalGiftCardAmount() *domain.Price {
    51  	return &cs.cart.TotalGiftCardAmount
    52  }
    53  
    54  // GrandTotalWithGiftCards sums grand total with gift cards
    55  func (cs CartSummary) GrandTotalWithGiftCards() *domain.Price {
    56  	return &cs.cart.GrandTotalWithGiftCards
    57  }
    58  
    59  // SumTaxes sums taxes
    60  func (cs CartSummary) SumTaxes() *Taxes {
    61  	items := cs.cart.SumTaxes()
    62  	taxes := make([]cart.Tax, 0, len(items))
    63  	for _, tax := range items {
    64  		taxes = append(taxes, tax)
    65  	}
    66  
    67  	if len(taxes) > 0 {
    68  		return nil
    69  	}
    70  
    71  	return &Taxes{Items: taxes}
    72  }
    73  
    74  // SumPaymentSelectionCartSplitValueAmountByMethods sum
    75  func (cs CartSummary) SumPaymentSelectionCartSplitValueAmountByMethods(methods []string) *domain.Price {
    76  	if cs.cart.PaymentSelection == nil {
    77  		return nil
    78  	}
    79  
    80  	prices := make([]domain.Price, 0, len(methods))
    81  
    82  	for qualifier, charge := range cs.cart.PaymentSelection.CartSplit() {
    83  		found := contains(methods, qualifier.Method)
    84  		if !found {
    85  			continue
    86  		}
    87  
    88  		prices = append(prices, charge.Value)
    89  	}
    90  
    91  	if len(prices) == 0 {
    92  		return nil
    93  	}
    94  
    95  	price, err := domain.SumAll(prices...)
    96  	if err != nil {
    97  		return nil
    98  	}
    99  
   100  	return &price
   101  }
   102  
   103  // SumPaymentSelectionCartSplitPriceAmountByMethods sum
   104  func (cs CartSummary) SumPaymentSelectionCartSplitPriceAmountByMethods(methods []string) *domain.Price {
   105  	if cs.cart.PaymentSelection == nil {
   106  		return nil
   107  	}
   108  
   109  	prices := make([]domain.Price, 0, len(methods))
   110  
   111  	for qualifier, charge := range cs.cart.PaymentSelection.CartSplit() {
   112  		found := contains(methods, qualifier.Method)
   113  		if !found {
   114  			continue
   115  		}
   116  
   117  		prices = append(prices, charge.Price)
   118  	}
   119  
   120  	if len(prices) == 0 {
   121  		return nil
   122  	}
   123  
   124  	price, err := domain.SumAll(prices...)
   125  	if err != nil {
   126  		return nil
   127  	}
   128  
   129  	return &price
   130  }
   131  
   132  // Contains tells whether a contains x.
   133  func contains(a []string, x string) bool {
   134  	for _, n := range a {
   135  		if x == n {
   136  			return true
   137  		}
   138  	}
   139  	return false
   140  }