Skip to content

Commit

Permalink
VCST-967: add approve/decline mutations (#109)
Browse files Browse the repository at this point in the history
Co-authored-by: Oleg Zhuk <zhukoo@gmail.com>
  • Loading branch information
basilkot and OlegoO authored May 8, 2024
1 parent c277274 commit b72c6c4
Show file tree
Hide file tree
Showing 15 changed files with 642 additions and 72 deletions.
7 changes: 7 additions & 0 deletions src/VirtoCommerce.QuoteModule.Core/Models/QuoteItem.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using VirtoCommerce.CartModule.Core.Model;
Expand Down Expand Up @@ -57,6 +58,7 @@ public TierPrice SelectedTierPrice

public ICollection<TierPrice> ProposalPrices { get; set; }

[Obsolete("Use IQuoteConverter instead", DiagnosticId = "VC0008", UrlFormat = "https://docs.virtocommerce.org/products/products-virto3-versions")]
public LineItem ToCartModel(LineItem target)
{
var _ = this;
Expand All @@ -79,6 +81,11 @@ public LineItem ToCartModel(LineItem target)
target.SalePrice = SelectedTierPrice.Price;
target.Quantity = (int)SelectedTierPrice.Quantity;
}

if (target.SalePrice > 0 && target.ListPrice > target.SalePrice)
{
target.DiscountAmount = target.ListPrice - target.SalePrice;
}
return target;
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/VirtoCommerce.QuoteModule.Core/Models/QuoteRequest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using VirtoCommerce.CartModule.Core.Model;
Expand All @@ -9,6 +9,8 @@
using VirtoCommerce.Platform.Core.DynamicProperties;
using VirtoCommerce.TaxModule.Core.Model;

using TaxAddress = VirtoCommerce.TaxModule.Core.Model.Address;

namespace VirtoCommerce.QuoteModule.Core.Models
{
public class QuoteRequest : AuditableEntity, IHasTaxDetalization, ISupportCancellation, IHasDynamicProperties, IHasLanguageCode, IHasChangesHistory
Expand Down Expand Up @@ -83,6 +85,7 @@ public class QuoteRequest : AuditableEntity, IHasTaxDetalization, ISupportCancel
public ICollection<DynamicObjectProperty> DynamicProperties { get; set; }
#endregion

[Obsolete("Use IQuoteConverter instead", DiagnosticId = "VC0008", UrlFormat = "https://docs.virtocommerce.org/products/products-virto3-versions")]
public ShoppingCart ToCartModel(ShoppingCart target)
{
var _ = this;
Expand Down Expand Up @@ -115,20 +118,22 @@ public ShoppingCart ToCartModel(ShoppingCart target)
{
Currency = Currency,
ShipmentMethodCode = ShipmentMethod.ShipmentMethodCode,
ShipmentMethodOption = ShipmentMethod.OptionName
ShipmentMethodOption = ShipmentMethod.OptionName,
Price = _.ManualShippingTotal,
};
target.Shipments = new List<Shipment>(new[] { shipment });
}

return target;
}

[Obsolete("Use IQuoteTotalsCalculator instead", DiagnosticId = "VC0008", UrlFormat = "https://docs.virtocommerce.org/products/products-virto3-versions")]
public TaxEvaluationContext ToTaxEvalContext(TaxEvaluationContext target)
{
target.Id = Id;
target.Code = Number;
target.Currency = Currency;
target.Address = Addresses?.FirstOrDefault()?.ToTaxModel(AbstractTypeFactory<TaxModule.Core.Model.Address>.TryCreateInstance());
target.Address = Addresses?.FirstOrDefault()?.ToTaxModel(AbstractTypeFactory<TaxAddress>.TryCreateInstance());
target.Type = GetType().Name;
foreach (var quoteItem in Items)
{
Expand Down
5 changes: 4 additions & 1 deletion src/VirtoCommerce.QuoteModule.Core/ModuleConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ public static class QuoteStatus
public const string Draft = "Draft";
public const string Processing = "Processing";
public const string Cancelled = "Cancelled";
public const string ProposalSent = "Proposal sent";
public const string Ordered = "Ordered";
public const string Declined = "Declined";
}

public static class ModuleConstants
Expand Down Expand Up @@ -39,7 +42,7 @@ public static class General
ValueType = SettingValueType.ShortText,
IsDictionary = true,
DefaultValue = "New",
AllowedValues = new object[] { QuoteStatus.Draft, "New", QuoteStatus.Processing, "Proposal sent", "Ordered", QuoteStatus.Cancelled }
AllowedValues = new object[] { QuoteStatus.Draft, "New", QuoteStatus.Processing, QuoteStatus.ProposalSent, QuoteStatus.Ordered, QuoteStatus.Cancelled, QuoteStatus.Declined }
};

public static SettingDescriptor DefaultStatus { get; } = new SettingDescriptor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ public interface IQuoteConverter
{
Task<QuoteRequest> ConvertFromCart(ShoppingCart cart);
ShoppingCart ConvertToCart(QuoteRequest quote);
Task<ShoppingCart> ConvertToCartWithTax(QuoteRequest quote);
}
Original file line number Diff line number Diff line change
@@ -1,83 +1,34 @@
using System.Linq;
using System.Threading.Tasks;
using VirtoCommerce.CartModule.Core.Model;
using VirtoCommerce.Platform.Core.Common;
using VirtoCommerce.QuoteModule.Core.Models;
using VirtoCommerce.QuoteModule.Core.Services;
using VirtoCommerce.ShippingModule.Core.Model.Search;
using VirtoCommerce.ShippingModule.Core.Services;
using VirtoCommerce.TaxModule.Core.Model;
using VirtoCommerce.TaxModule.Core.Model.Search;
using VirtoCommerce.TaxModule.Core.Services;

namespace VirtoCommerce.QuoteModule.Data.Services
{
public class DefaultQuoteTotalsCalculator : IQuoteTotalsCalculator
{
private readonly IShippingMethodsSearchService _shippingMethodsSearchService;
private readonly ITaxProviderSearchService _taxProviderSearchService;
private readonly IQuoteConverter _quoteConverter;

public DefaultQuoteTotalsCalculator(
IShippingMethodsSearchService shippingMethodsSearchService,
ITaxProviderSearchService taxProviderSearchService)
public DefaultQuoteTotalsCalculator(IQuoteConverter quoteConverter)
{
_shippingMethodsSearchService = shippingMethodsSearchService;
_taxProviderSearchService = taxProviderSearchService;
_quoteConverter = quoteConverter;
}

public virtual async Task<QuoteRequestTotals> CalculateTotalsAsync(QuoteRequest quote)
{
var retVal = new QuoteRequestTotals();
var cartFromQuote = quote.ToCartModel(AbstractTypeFactory<ShoppingCart>.TryCreateInstance());
var cartFromQuote = await _quoteConverter.ConvertToCartWithTax(quote);

//Calculate shipment total
//first try to get manual amount
retVal.ShippingTotal = quote.ManualShippingTotal;
if (retVal.ShippingTotal == 0 && quote.ShipmentMethod != null)
{
//calculate total by using shipment gateways
var evalContext = new ShippingEvaluationContext(cartFromQuote);

var searchCriteria = AbstractTypeFactory<ShippingMethodsSearchCriteria>.TryCreateInstance();
searchCriteria.StoreId = quote.StoreId;
searchCriteria.IsActive = true;
searchCriteria.Codes = new[] { quote.ShipmentMethod.ShipmentMethodCode };
var storeShippingMethods = await _shippingMethodsSearchService.SearchAsync(searchCriteria);
var rate = storeShippingMethods.Results
.SelectMany(x => x.CalculateRates(evalContext))
.FirstOrDefault(x => (quote.ShipmentMethod.OptionName == null) || quote.ShipmentMethod.OptionName == x.OptionName);
retVal.SubTotalExlTax = cartFromQuote.SubTotal;
retVal.OriginalSubTotalExlTax = cartFromQuote.SubTotal + cartFromQuote.SubTotalDiscount;

retVal.ShippingTotal = rate != null ? rate.Rate : 0m;
}
//Calculate shipment total
retVal.ShippingTotal = cartFromQuote.ShippingTotal;

//Calculate taxes
var taxSearchCriteria = AbstractTypeFactory<TaxProviderSearchCriteria>.TryCreateInstance();
taxSearchCriteria.StoreIds = new[] { quote.StoreId };
taxSearchCriteria.Sort = nameof(TaxProvider.Priority);
var taxProvider = (await _taxProviderSearchService.SearchAsync(taxSearchCriteria)).Results.FirstOrDefault();

if (taxProvider != null)
{
var taxEvalContext = quote.ToTaxEvalContext(AbstractTypeFactory<TaxEvaluationContext>.TryCreateInstance());
retVal.TaxTotal = taxProvider.CalculateRates(taxEvalContext).Select(x => x.Rate).DefaultIfEmpty(0).Sum(x => x);
}
retVal.TaxTotal = cartFromQuote.TaxTotal;

//Calculate subtotal
var items = quote.Items.Where(x => x.SelectedTierPrice != null);
if (quote.Items != null)
{
retVal.OriginalSubTotalExlTax = items.Sum(x => x.SalePrice * x.SelectedTierPrice.Quantity);
retVal.SubTotalExlTax = items.Sum(x => x.SelectedTierPrice.Price * x.SelectedTierPrice.Quantity);
if (quote.ManualSubTotal > 0)
{
retVal.DiscountTotal = retVal.SubTotalExlTax - quote.ManualSubTotal;
retVal.SubTotalExlTax = quote.ManualSubTotal;
}
else if (quote.ManualRelDiscountAmount > 0)
{
retVal.DiscountTotal = retVal.SubTotalExlTax * quote.ManualRelDiscountAmount * 0.01m;
}
}
retVal.DiscountTotal = cartFromQuote.DiscountTotal;

return retVal;
}
Expand Down
Loading

0 comments on commit b72c6c4

Please sign in to comment.