Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Skybrud.Umbraco.Module.Extensions.Strings;
using TeaCommerce.Api.Marketing.Models.Rules;
using TeaCommerce.Api.Models;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
namespace myProject.Models.Website.Campaign
{
public class CampaignRepository
{
public string GetCampaignPrice(IPublishedContent Product, Price PriceObj)
{
string returnValue = "";
try
{
var m = TeaCommerce.Api.Marketing.Services.CampaignService.Instance.GetAll(1);
int hasDiscount = DoesProductHaveDiscount(Product, m);
if (hasDiscount != -999)
{
var campaign = m.FirstOrDefault(x => x.Id == hasDiscount);
var reward = JsonConvert.DeserializeObject<RewardSettings>(campaign.Awards.First(x => x.AwardAlias == "OrderLineAmountAward").Settings);
if (reward != null && reward.type == "0")
{
if (reward.usePercentage)
{
decimal percentCalc = (Convert.ToDecimal(reward.percentage) / 100); //Eks. 0.2 for 20%
decimal p = PriceObj.WithVat;
decimal result = p - (p * percentCalc);
return result.ToString("N2") + " DKK";
}
else if (reward.amounts != null && reward.amounts.Any())
{
decimal p = PriceObj.WithVat;
decimal reduction = Convert.ToDecimal(reward.amounts.First().Value);
decimal result = p - reduction;
return result.ToString("N2") + " DKK";
}
}
}
}
catch (Exception ex)
{
LogHelper.Error<CampaignRepository>("FEJL i rabat udregning", ex);
}
return returnValue;
}
private int DoesProductHaveDiscount(IPublishedContent Product, IEnumerable<TeaCommerce.Api.Marketing.Models.Campaign> Campaigns)
{
foreach (TeaCommerce.Api.Marketing.Models.Campaign x1 in Campaigns.Where(x => x.IsActive))
{
foreach (var rg in x1.RulesGroups)
{
foreach (var r in rg.Rules.Where(x => x.RuleAlias == "ProductRule"))
{
ProductRuleResult result = JsonConvert.DeserializeObject<ProductRuleResult>(r.Settings);
if (Product.Path.CsvToInt().Any(x => x.ToString() == result.nodeId))
{
return (int)x1.Id;
}
}
}
}
return -999;
}
}
public class ProductRuleResult
{
public string nodeId { get; set; }
}
public class RewardSettings
{
public string type { get; set; }
public object nodeId { get; set; }
public bool usePercentage { get; set; }
public string percentage { get; set; }
public Dictionary<string, string> amounts { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.