Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for multiple prices (price matrix) through PriceProvider #18

Merged
merged 2 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<Copyright>Copyright © 2020 Dynamicweb Software A/S</Copyright>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<IncludeSymbols>true</IncludeSymbols>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
Expand All @@ -23,10 +23,10 @@
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dynamicweb" Version="10.0.33" />
<PackageReference Include="Dynamicweb.Core" Version="10.0.33" />
<PackageReference Include="Dynamicweb.DataIntegration" Version="10.0.33" />
<PackageReference Include="Dynamicweb.Ecommerce" Version="10.0.33" />
<PackageReference Include="Dynamicweb" Version="10.4.0" />
<PackageReference Include="Dynamicweb.Core" Version="10.4.0" />
<PackageReference Include="Dynamicweb.DataIntegration" Version="10.4.0" />
<PackageReference Include="Dynamicweb.Ecommerce" Version="10.4.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Dynamicweb.Ecommerce.DynamicwebLiveIntegration\Dynamicweb.Ecommerce.DynamicwebLiveIntegration.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>10.0.14</Version>
<Version>10.4.0</Version>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<Title>Live Integration</Title>
<Description>Live Integration</Description>
Expand All @@ -14,7 +14,7 @@
<Copyright>Copyright © 2023 Dynamicweb Software A/S</Copyright>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<IncludeSymbols>true</IncludeSymbols>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
Expand All @@ -23,9 +23,9 @@
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dynamicweb.Core" Version="10.0.33" />
<PackageReference Include="Dynamicweb.DataIntegration" Version="10.0.33" />
<PackageReference Include="Dynamicweb.Ecommerce" Version="10.0.33" />
<PackageReference Include="Dynamicweb.Core" Version="10.4.0" />
<PackageReference Include="Dynamicweb.DataIntegration" Version="10.4.0" />
<PackageReference Include="Dynamicweb.Ecommerce" Version="10.4.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,27 @@ public PriceInfo FindPriceInfo(PriceContext context, PriceProductSelection selec
Diagnostics.ExecutionTable.Current.Add("DynamicwebLiveIntegration.ProductPriceProvider.FindPriceInfoWithContext END");
}
}

IEnumerable<KeyValuePair<PriceQuantityInfo, PriceInfo>> IPriceInfoProvider.FindQuantityPriceInfos(PriceContext context, Product product)
{
var settings = SettingsManager.GetSettingsByShop(context.Shop?.Id);
if (!Global.IsIntegrationActive(settings))
return Enumerable.Empty<KeyValuePair<PriceQuantityInfo, PriceInfo>>();

var result = new List<KeyValuePair<PriceQuantityInfo, PriceInfo>>();
var unitPrices = ProductViewModelExtensions.GetUnitPrices(settings, context.Customer, product);
foreach (var unitPrice in unitPrices)
{
result.Add(new KeyValuePair<PriceQuantityInfo, PriceInfo>(
new PriceQuantityInfo()
{
Quantity = unitPrice.Quantity ?? 0,
UnitId = unitPrice.UnitId,
},
ProductProviderBase.GetPriceInfo(context, unitPrice.Amount, unitPrice.AmountWithVat)
));
}
return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,33 +124,7 @@ public virtual PriceInfo GetPriceInfo(LiveContext context, ProductInfo product,
priceWithVat = erpPriceResponse?.AmountWithVat;
}

PriceInfo result = context.PriceContext != null ? new PriceInfo(context.PriceContext.Currency) : new PriceInfo(context.Currency);
result.PriceWithoutVAT = priceWithoutVat != null ? priceWithoutVat.Value : 0;
if (priceWithVat != null)
{
result.PriceWithVAT = priceWithVat.Value;
if (result.PriceWithVAT >= result.PriceWithoutVAT && result.PriceWithoutVAT > 0)
{
//This is to handle the case where the ERP returns VAT and VAT percent, but DW is setup to i.e. 0% vat.
result.VAT = result.PriceWithVAT - result.PriceWithoutVAT;
}
}
else
{
var price = new PriceRaw
{
Price = result.PriceWithoutVAT,
Currency = context.Currency
};
PriceContext priceContext = new PriceContext(context.Currency, context.Country);
var calculated = PriceCalculated.Create(priceContext, price);
if (calculated != null)
{
calculated.Calculate();
result.PriceWithVAT = calculated.PriceWithVAT;
}
}

PriceInfo result = GetPriceInfo(context.PriceContext != null ? context.PriceContext : new PriceContext(context.Currency, context.Country), priceWithoutVat, priceWithVat);
return result;
}

Expand Down Expand Up @@ -297,5 +271,35 @@ public virtual void FillProductFieldValues(Product product, ProductInfo productI
}
}
}

internal static PriceInfo GetPriceInfo(PriceContext priceContext, double? priceWithoutVat, double? priceWithVat)
{
PriceInfo result = new PriceInfo(priceContext.Currency);
result.PriceWithoutVAT = priceWithoutVat != null ? priceWithoutVat.Value : 0;
if (priceWithVat != null)
{
result.PriceWithVAT = priceWithVat.Value;
if (result.PriceWithVAT >= result.PriceWithoutVAT && result.PriceWithoutVAT > 0)
{
//This is to handle the case where the ERP returns VAT and VAT percent, but DW is setup to i.e. 0% vat.
result.VAT = result.PriceWithVAT - result.PriceWithoutVAT;
}
}
else
{
var price = new PriceRaw
{
Price = result.PriceWithoutVAT,
Currency = priceContext.Currency
};
var calculated = PriceCalculated.Create(priceContext, price);
if (calculated != null)
{
calculated.Calculate();
result.PriceWithVAT = calculated.PriceWithVAT;
}
}
return result;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Configuration;
using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Products;
using Dynamicweb.Ecommerce.ProductCatalog;
using Dynamicweb.Ecommerce.Products;
using Dynamicweb.Security.UserManagement;
using System.Collections.Generic;
using System.Linq;

Expand All @@ -24,34 +26,40 @@ public static List<PriceListViewModel> GetUnitPrices(this ProductViewModel produ
var user = Helpers.GetCurrentExtranetUser();
var product = Services.Products.GetProductById(productViewModel.Id, productViewModel.VariantId, productViewModel.LanguageId);

if (settings is not null && Helpers.CanCheckPrice(settings, product, user))
var prices = GetUnitPrices(settings, user, product);
if (prices is not null && prices.Count > 0)
{
var productInfo = ProductManager.GetProductInfo(product, settings, user);
if (productInfo is not null)
{
var prices = (IList<ProductPrice>)productInfo["Prices"];
if (prices is not null && prices.Count > 0)
foreach (var price in prices.OrderBy(p => p.Quantity.GetValueOrDefault()))
{
result.Add(new()
{
foreach (var price in prices.OrderBy(p => p.Quantity.GetValueOrDefault()))
Quantity = price.Quantity ?? 0,
UnitId = price.UnitId,
Price = new()
{
result.Add(new()
{
Quantity = price.Quantity ?? 0,
UnitId = price.UnitId,
Price = new()
{
Price = price.Amount.GetValueOrDefault(),
PriceWithVat = price.AmountWithVat.GetValueOrDefault(),
PriceWithoutVat = price.Amount.GetValueOrDefault(),
CurrencyCode = Common.Context.Currency.Code,
ShowPricesWithVat = Common.Context.DisplayPricesWithVat
}
});
Price = price.Amount.GetValueOrDefault(),
PriceWithVat = price.AmountWithVat.GetValueOrDefault(),
PriceWithoutVat = price.Amount.GetValueOrDefault(),
CurrencyCode = Common.Context.Currency.Code,
ShowPricesWithVat = Common.Context.DisplayPricesWithVat
}
}
});
}
}
return result;
}

internal static List<ProductPrice> GetUnitPrices(Settings settings, User user, Product product)
{
if (settings is not null && Helpers.CanCheckPrice(settings, product, user))
{
var productInfo = ProductManager.GetProductInfo(product, settings, user);
if (productInfo is not null)
{
return ((IList<ProductPrice>)productInfo["Prices"]).ToList();
}
}
return new List<ProductPrice>();
}
}
}
Loading