-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCartDetail.ascx.cs
415 lines (361 loc) · 15.8 KB
/
CartDetail.ascx.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
' DNN (formerly DotNetNuke) - http://www.dnnsoftware.com
' Copyright (c) 2002-2018
' by DNN Corp
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Globalization;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Modules.Store.Core.Admin;
using DotNetNuke.Modules.Store.Core.Catalog;
using DotNetNuke.Modules.Store.Core.Cart;
using DotNetNuke.Modules.Store.Core.Components;
using DotNetNuke.Modules.Store.Core.Providers.Tax;
namespace DotNetNuke.Modules.Store.WebControls
{
public partial class CartDetail : StoreControlBase
{
#region Private Members
private decimal _cartTotal;
private int _itemsCount;
private readonly NumberFormatInfo _localFormat = (NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone();
private decimal _defaultTaxRate;
private bool _showTax;
private ProductController _productControler;
private bool _showThumbnail;
private int _thumbnailWidth;
private string _gifBgColor;
private string _productColumn;
private bool _linkToDetail;
private bool _includeVAT;
private bool _enableImageCaching;
private int _cacheImageDuration;
private string _templatePath;
private string _linkTitle;
private string _imageUpdate;
private string _imageDelete;
#endregion
#region Properties
public bool ShowThumbnail
{
get { return _showThumbnail; }
set { _showThumbnail = value; }
}
public int ThumbnailWidth
{
get { return _thumbnailWidth; }
set { _thumbnailWidth = value; }
}
public string GIFBgColor
{
get { return _gifBgColor; }
set { _gifBgColor = value; }
}
public string ProductColumn
{
get { return _productColumn; }
set { _productColumn = value; }
}
public bool LinkToDetail
{
get { return _linkToDetail; }
set { _linkToDetail = value; }
}
public bool IncludeVAT
{
get { return _includeVAT; }
set { _includeVAT = value; }
}
public bool EnableImageCaching
{
get { return _enableImageCaching; }
set { _enableImageCaching = value; }
}
public int CacheImageDuration
{
get { return _cacheImageDuration; }
set { _cacheImageDuration = value; }
}
public string TemplatePath
{
get { return _templatePath; }
set { _templatePath = value; }
}
public int ItemsCount
{
get { return _itemsCount; }
}
#endregion
#region Events
protected void Page_Load(object sender, EventArgs e)
{
if (StoreSettings != null)
{
try
{
if (!string.IsNullOrEmpty(StoreSettings.CurrencySymbol))
_localFormat.CurrencySymbol = StoreSettings.CurrencySymbol;
_imageUpdate = _templatePath + "images/" + Localization.GetString("ImageUpdate", LocalResourceFile);
_imageDelete = _templatePath + "images/" + Localization.GetString("ImageDelete", LocalResourceFile);
ITaxProvider taxProvider = StoreController.GetTaxProvider(StoreSettings.TaxName);
ITaxInfo taxInfo = taxProvider.GetDefautTaxRates(PortalId);
_defaultTaxRate = taxInfo.DefaultTaxRate;
_showTax = taxInfo.ShowTax;
if (_linkToDetail)
{
_linkTitle = Localization.GetString("lnkTitle", LocalResourceFile);
if (StoreSettings.SEOFeature)
_productControler = new ProductController();
}
if (IsPostBack == false)
UpdateCartGrid();
}
catch (Exception ex)
{
Exceptions.ProcessModuleLoadException(this, ex);
}
}
}
protected void grdItems_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
ItemInfo itemInfo = (ItemInfo)e.Item.DataItem;
if (_showThumbnail)
{
Image imgThumbnail = (Image)e.Item.FindControl("imgThumbnail");
if (imgThumbnail != null)
{
imgThumbnail.ImageUrl = GetImageUrl(itemInfo.ProductImage);
imgThumbnail.AlternateText = itemInfo.ProductTitle;
imgThumbnail.Width = _thumbnailWidth;
}
}
string productTitle = null;
switch (_productColumn)
{
case "modelnumber":
productTitle = itemInfo.ModelNumber;
break;
case "modelname":
productTitle = itemInfo.ModelName;
break;
case "producttitle":
productTitle = itemInfo.ProductTitle;
break;
}
if (_linkToDetail)
{
HtmlAnchor lnkTitle = (HtmlAnchor)e.Item.FindControl("lnkTitle");
if (lnkTitle != null)
{
StringDictionary rplcTitle = new StringDictionary
{
{ "ProductID", itemInfo.ProductID.ToString() }
};
if (StoreSettings.SEOFeature)
{
ProductInfo product = _productControler.GetProduct(PortalId, itemInfo.ProductID);
rplcTitle.Add("Product", product.SEOName);
}
CatalogNavigation catalogNav = new CatalogNavigation();
lnkTitle.InnerText = productTitle;
lnkTitle.Title = _linkTitle;
lnkTitle.HRef = catalogNav.GetNavigationUrl(StoreSettings.StorePageID, rplcTitle);
lnkTitle.Visible = true;
}
}
else
{
Label lblTitle = (Label)e.Item.FindControl("lblTitle");
if (lblTitle != null)
{
lblTitle.Text = productTitle;
lblTitle.Visible = true;
}
}
Label lblPrice = (Label)e.Item.FindControl("lblPrice");
if (lblPrice != null)
{
decimal unitCost = itemInfo.UnitCost;
if (_showTax && _includeVAT)
{
unitCost = (unitCost + (unitCost * (_defaultTaxRate / 100)));
}
lblPrice.Text = (unitCost).ToString("C", _localFormat);
}
Label lblSubtotal = (Label)e.Item.FindControl("lblSubtotal");
if (lblSubtotal != null)
{
decimal subTotal = itemInfo.SubTotal;
if (_showTax && _includeVAT)
{
subTotal = (subTotal + (subTotal * (_defaultTaxRate / 100)));
}
lblSubtotal.Text = (subTotal).ToString("C", _localFormat);
_cartTotal += subTotal;
_itemsCount += itemInfo.Quantity;
}
TextBox txtQty = (TextBox)e.Item.FindControl("txtQuantity");
if (txtQty != null)
txtQty.Text = itemInfo.Quantity.ToString();
RequiredFieldValidator valReqQuantity = (RequiredFieldValidator)e.Item.FindControl("valReqQuantity");
if (valReqQuantity != null)
valReqQuantity.ErrorMessage = LocalizeString("valReqQuantity");
CompareValidator valCompQuantity = (CompareValidator)e.Item.FindControl("valCompQuantity");
if (valCompQuantity != null)
valCompQuantity.ErrorMessage = LocalizeString("valCompQuantity");
CustomValidator valCustQuantity = (CustomValidator)e.Item.FindControl("valCustQuantity");
if (valCustQuantity != null)
{
if (StoreSettings.InventoryManagement && StoreSettings.AvoidNegativeStock)
{
valCustQuantity.Attributes.Add("ItemID", itemInfo.ItemID.ToString());
}
else
valCustQuantity.Visible = false;
}
ImageButton ibUpdate = (ImageButton)e.Item.FindControl("ibUpdate");
if (ibUpdate != null)
{
ibUpdate.ImageUrl = _imageUpdate;
ibUpdate.CommandArgument = e.Item.ItemIndex.ToString();
ibUpdate.CommandName = "Update";
}
if (txtQty != null && ibUpdate != null)
txtQty.Attributes.Add("onkeydown", "javascript:if((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)){document.getElementById('" + ibUpdate.ClientID + "').click();return false;}else return true;");
ImageButton ibDelete = (ImageButton)e.Item.FindControl("ibDelete");
if (ibDelete != null)
{
ibDelete.ImageUrl = _imageDelete;
ibDelete.CommandArgument = e.Item.ItemIndex.ToString();
ibDelete.CommandName = "Delete";
}
}
else if (e.Item.ItemType == ListItemType.Footer)
{
Label lblCount = (Label)e.Item.FindControl("lblCount");
if (lblCount != null)
lblCount.Text = _itemsCount.ToString();
Label lblTotal = (Label)e.Item.FindControl("lblTotal");
if (lblTotal != null)
lblTotal.Text = _cartTotal.ToString("C", _localFormat);
}
}
protected void valCustQuantity_ServerValidate(object source, ServerValidateEventArgs args)
{
CustomValidator valCustQuantity = (CustomValidator)source;
int quantity;
if (StoreSettings.InventoryManagement && StoreSettings.AvoidNegativeStock && int.TryParse(args.Value, out quantity))
{
string itemID = valCustQuantity.Attributes["ItemID"];
ItemInfo itemInfo = CurrentCart.GetItem(int.Parse(itemID));
ProductController controler = new ProductController();
ProductInfo currentProduct = controler.GetProduct(PortalId, itemInfo.ProductID);
if (currentProduct.StockQuantity < quantity)
{
if (quantity > 1)
valCustQuantity.ErrorMessage = string.Format(Localization.GetString("ErrorQuantityPlural", LocalResourceFile), currentProduct.StockQuantity, currentProduct.ProductTitle);
else if (quantity == 1)
valCustQuantity.ErrorMessage = string.Format(Localization.GetString("ErrorQuantitySingular", LocalResourceFile), currentProduct.ProductTitle);
else
valCustQuantity.ErrorMessage = string.Format(Localization.GetString("ErrorQuantityNegative", LocalResourceFile), currentProduct.ProductTitle);
args.IsValid = false;
}
}
}
protected void grdItems_ItemCommand(object source, DataGridCommandEventArgs e)
{
Page.Validate("vgCart");
if (!Page.IsValid)
return;
int itemID;
int quantity;
switch (e.CommandName)
{
case "Update":
itemID = (int)grdItems.DataKeys[Convert.ToInt32(e.CommandArgument)];
TextBox txtQuantity = (TextBox)((WebControl)e.CommandSource).Parent.FindControl("txtQuantity");
quantity = int.Parse(txtQuantity.Text);
if (quantity > 0)
CurrentCart.UpdateItem(PortalId, StoreSettings.SecureCookie, itemID, quantity);
else
CurrentCart.RemoveItem(itemID);
UpdateCartGrid();
InvokeEditComplete();
break;
case "Delete":
itemID = (int)grdItems.DataKeys[Convert.ToInt32(e.CommandArgument)];
CurrentCart.RemoveItem(itemID);
UpdateCartGrid();
InvokeEditComplete();
break;
case "UpdateCart":
foreach (DataGridItem dgItem in grdItems.Items)
{
itemID = (int)grdItems.DataKeys[dgItem.ItemIndex];
quantity = int.Parse(((TextBox)dgItem.FindControl("txtQuantity")).Text);
if (quantity > 0)
CurrentCart.UpdateItem(PortalId, StoreSettings.SecureCookie, itemID, quantity);
else
CurrentCart.RemoveItem(itemID);
}
UpdateCartGrid();
InvokeEditComplete();
break;
default:
break;
}
}
#endregion
#region Private Methods
private void UpdateCartGrid()
{
_cartTotal = 0;
_itemsCount = 0;
List<ItemInfo> cartItems = CurrentCart.GetItems(PortalId, StoreSettings.SecureCookie);
if (cartItems.Count == 0)
{
grdItems.Visible = false;
lblCartEmpty.Visible = true;
}
else
{
grdItems.Visible = true;
if (_showThumbnail)
grdItems.Columns[0].Visible = true;
else
grdItems.Columns[0].Visible = false;
grdItems.DataSource = cartItems;
grdItems.DataBind();
lblCartEmpty.Visible = false;
}
}
private string GetImageUrl(string image)
{
if (_enableImageCaching)
return ControlPath + "Thumbnail.ashx?IP=" + image + "&IW=" + _thumbnailWidth + "&BC=" + GIFBgColor + "&CD=" + _cacheImageDuration; // + "&portalID=" + PortalId.ToString();
return ControlPath + "Thumbnail.ashx?IP=" + image + "&IW=" + _thumbnailWidth + "&BC=" + GIFBgColor; // +"&portalID=" + PortalId.ToString();
}
#endregion
}
}