-
Notifications
You must be signed in to change notification settings - Fork 238
/
ValidateProductPriceTask.cs
112 lines (97 loc) · 4.67 KB
/
ValidateProductPriceTask.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
namespace Merchello.Web.Validation.Tasks
{
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Merchello.Core;
using Merchello.Core.Models;
using Merchello.Web.Models.ContentEditing;
using Merchello.Web.Workflow.CustomerItemCache;
using Umbraco.Core;
using Umbraco.Core.Logging;
/// <summary>
/// Validates that product pricing in Merchello has not been changed.
/// </summary>
/// <remarks>
/// Note: for multi-currency sites, you should place the extended data key (merchLineItemAllowsValidation = false)
/// into the line item extended data so that this task
/// does not reset the price to that set in the back office
/// </remarks>
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1650:ElementDocumentationMustBeSpelledCorrectly", Justification = "Reviewed. Suppression is OK here.")]
internal class ValidateProductPriceTask : CustomerItemCacheValidatationTaskBase<ValidationResult<CustomerItemCacheBase>>
{
/// <summary>
/// Initializes a new instance of the <see cref="ValidateProductPriceTask"/> class.
/// </summary>
/// <param name="merchelloContext">
/// The merchello context.
/// </param>
/// <param name="enableDataModifiers">
/// The enable data modifiers.
/// </param>
public ValidateProductPriceTask(IMerchelloContext merchelloContext, bool enableDataModifiers)
: base(merchelloContext, enableDataModifiers)
{
}
/// <summary>
/// Performs the task of validating the pricing information.
/// </summary>
/// <param name="value">
/// The value.
/// </param>
/// <returns>
/// The <see cref="Attempt"/>.
/// </returns>
public override Attempt<ValidationResult<CustomerItemCacheBase>> PerformTask(ValidationResult<CustomerItemCacheBase> value)
{
var visitor = new ProductPricingValidationVisitor(Merchello);
value.Validated.Accept(visitor);
if (visitor.InvalidPrices.Any())
{
foreach (var result in visitor.InvalidPrices.ToArray())
{
var lineItem = result.Key;
var quantity = lineItem.Quantity;
var name = lineItem.Name;
var removedEd = lineItem.ExtendedData.AsEnumerable();
value.Validated.RemoveItem(lineItem.Sku);
var extendedData = new ExtendedDataCollection();
ProductVariantDisplay variant;
if (result.Value is ProductDisplay)
{
var product = result.Value as ProductDisplay;
variant = product.AsMasterVariantDisplay();
}
else
{
variant = result.Value as ProductVariantDisplay;
}
if (variant == null)
{
var nullReference = new NullReferenceException("ProductVariantDisplay cannot be null");
LogHelper.Error<ValidateProductPriceTask>("Exception occurred when attempting to adjust pricing information", nullReference);
throw nullReference;
}
extendedData.AddProductVariantValues(variant);
extendedData.MergeDataModifierLogs(variant);
extendedData.MergeDataModifierLogs(variant);
// preserve any custom extended data values
foreach (var val in removedEd.Where(val => !extendedData.ContainsKey(val.Key)))
{
extendedData.SetValue(val.Key, val.Value);
}
var price = variant.OnSale ? extendedData.GetSalePriceValue() : extendedData.GetPriceValue();
var keys = lineItem.ExtendedData.Keys.Where(x => extendedData.Keys.Any(y => y != x));
foreach (var k in keys)
{
extendedData.SetValue(k, lineItem.ExtendedData.GetValue(k));
}
value.Validated.AddItem(string.IsNullOrEmpty(name) ? variant.Name : name, variant.Sku, quantity, price, extendedData);
value.Messages.Add("Price updated for " + variant.Sku + " to " + price);
}
value.Validated.Save();
}
return Attempt<ValidationResult<CustomerItemCacheBase>>.Succeed(value);
}
}
}