-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShoppingCartItem.cs
223 lines (197 loc) · 6.2 KB
/
ShoppingCartItem.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
using System;
using System.Collections.Generic;
using Rock;
using Rock.Data;
using Rock.Model;
namespace com.shepherdchurch.CubeDown
{
[Serializable]
public class ShoppingCartItem : Rock.Lava.ILiquidizable
{
#region Properties
/// <summary>
/// Gets the original content channel item.
/// </summary>
/// <value>
/// The original content channel item.
/// </value>
[Newtonsoft.Json.JsonIgnore]
public ContentChannelItem Item
{
get
{
if ( _item == null && ItemId.HasValue )
{
_item = new ContentChannelItemService( new RockContext() ).Get( ItemId.Value );
}
return _item;
}
}
[NonSerialized]
private ContentChannelItem _item;
/// <summary>
/// Gets or sets the unique identifier.
/// </summary>
/// <value>
/// The unique identifier.
/// </value>
[Newtonsoft.Json.JsonIgnore]
public Guid Guid { get; set; }
/// <summary>
/// Gets or sets the item identifier of the original content channel item.
/// </summary>
/// <value>
/// The item identifier of the original content channel item.
/// </value>
public int? ItemId { get; set; }
/// <summary>
/// Gets or sets the name of this line item.
/// </summary>
/// <value>
/// The name of this line item.
/// </value>
public string Name { get; set; }
/// <summary>
/// Gets or sets the price.
/// </summary>
/// <value>
/// The price.
/// </value>
public decimal Price { get; set; }
/// <summary>
/// Gets or sets the quantity.
/// </summary>
/// <value>
/// The quantity.
/// </value>
public int Quantity { get; set; }
/// <summary>
/// Gets the extended price, which is base price times quantity.
/// </summary>
/// <value>
/// The extended price.
/// </value>
public decimal ExtendedPrice
{
get
{
return Price * Quantity;
}
}
/// <summary>
/// Gets or sets a value indicating whether this instance is taxable.
/// </summary>
/// <value>
/// <c>true</c> if this instance is taxable; otherwise, <c>false</c>.
/// </value>
public bool IsTaxable { get; set; }
/// <summary>
/// Gets or sets the account identifier.
/// </summary>
/// <value>
/// The account identifier.
/// </value>
public int? AccountId { get; set; }
#endregion
#region ILiquidizable Interface
/// <summary>
/// Gets the available keys (for debugging info).
/// </summary>
/// <value>
/// The available keys.
/// </value>
[Newtonsoft.Json.JsonIgnore]
public List<string> AvailableKeys
{
get
{
return new List<string>
{
"Item",
"ExtendedPrice",
"Guid",
"ItemId",
"Name",
"Price",
"Quantity",
"IsTaxable"
};
}
}
/// <summary>
/// Gets the <see cref="System.Object"/> with the specified key.
/// </summary>
/// <value>
/// The <see cref="System.Object"/>.
/// </value>
/// <param name="key">The key.</param>
/// <returns></returns>
[System.Runtime.CompilerServices.IndexerName( "IndexItem" )]
public object this[object key]
{
get
{
var pi = GetType().GetProperty( key.ToString() );
return pi?.GetValue( this );
}
}
/// <summary>
/// Retrieves an object that can be used to represent this object in Lava.
/// </summary>
/// <returns>An object that can be used to represent this object in Lava</returns>
public object ToLiquid()
{
return this;
}
/// <summary>
/// Determines whether the lava object contains the key.
/// </summary>
/// <param name="key">The key.</param>
/// <returns>
/// <c>true</c> if the lava object contains the key; otherwise, <c>false</c>.
/// </returns>
public bool ContainsKey( object key )
{
return AvailableKeys.Contains( key.ToString() );
}
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="ShoppingCartItem"/> class.
/// </summary>
public ShoppingCartItem()
{
Guid = Guid.NewGuid();
}
/// <summary>
/// Initializes a new instance of the <see cref="ShoppingCartItem"/> class.
/// </summary>
/// <param name="item">The content channel item to build from.</param>
public ShoppingCartItem( ContentChannelItem item )
: this()
{
if ( item.Attributes == null )
{
item.LoadAttributes();
}
ItemId = item.Id;
Name = item.Title;
Quantity = 1;
Price = item.GetAttributeValue( "CubeDown.Price" ).AsDecimal();
IsTaxable = item.GetAttributeValue( "CubeDown.Taxable" ).AsBoolean( false );
//
// If an Account Guid was provided, convert it to an Id number.
//
var accountGuid = item.GetAttributeValue( "CubeDown.Account" ).AsGuidOrNull();
if ( accountGuid.HasValue )
{
using ( var rockContext = new RockContext() )
{
var account = new FinancialAccountService( rockContext ).Get( accountGuid.Value );
AccountId = account?.Id;
}
}
}
#endregion
}
}