/
CatalogueHandler.cs
268 lines (221 loc) · 9.46 KB
/
CatalogueHandler.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Anacle.DataFramework;
using LogicLayer;
using DataMigration.Infrastructure;
namespace DataMigration.Logic
{
public class CatalogueHanlder : Migratable
{
public CatalogueHanlder(string mapfrom, string mapto)
: base(mapfrom, mapto)
{
catalogueGroupList = new Dictionary<string, Guid?>();
//UnitOfMeasureList = new Dictionary<string, Guid?>();
}
public CatalogueHanlder(string mapfrom, string mapto, string sourcefile)
: base(mapfrom, mapto, sourcefile)
{ }
private static IDictionary<string, Guid?> catalogueGroupList = new Dictionary<string, Guid?>();
private static IDictionary<string, Guid?> UnitOfMeasureList = new Dictionary<string, Guid?>();
//private const string UnitOfMeasureTypeName = "UnitOfMeasure";
private static OCodeType UnitOfMeasureType = null;
private static OCode unitOfMeasureRoot = null;
Hashtable catalogTypes = new Hashtable();
Hashtable unitOfMeasures = new Hashtable();
public override void Migarate()
{
DataTable dt = GetDatasource();
using (Connection c = new Connection())
{
List<OCatalogue> list = TablesLogic.tCatalogue.LoadList(TablesLogic.tCatalogue.IsCatalogueItem == 0);
foreach (OCatalogue catalog in list)
catalogTypes[catalog.Path] = catalog;
}
foreach (DataRow row in dt.Rows)
{
ImportCatalogue(row);
}
LogHelper.LogDataImport(mapfrom, dt, Map.Values);
}
#region UnitOfMeasure
/// <summary>
/// Get UnitOfMeasure by name,if it isn't exist,create it.
/// </summary>
/// <param name="unit"></param>
/// <returns></returns>
private static OCode GetUnitOfMeasure(string unit)
{
OCode code = null;
//retrive from cache
Guid? id = null;
if (UnitOfMeasureList.ContainsKey(unit))
id = UnitOfMeasureList[unit];
if (id != null)
{
code = TablesLogic.tCode[id];
}
//if it isn't in cahce,load it from db
if (code == null)
{
code = TablesLogic.tCode.Load(TablesLogic.tCode.ObjectName == unit, true, null);
//if it can be load from db
if (code != null)
{
ActivateObject(code);
}
//if it can't be load from db,create it
else
{
code = CreateUnitOfMeasure(unit);
}
}
//To improve performance,add it to a list
if (!UnitOfMeasureList.ContainsKey(unit))
UnitOfMeasureList.Add(unit, code.ObjectID);
return code;
}
private static OCode CreateUnitOfMeasure(string unit)
{
if (UnitOfMeasureType == null)
{
UnitOfMeasureType = TablesLogic.tCodeType.Load(TablesLogic.tCodeType.ObjectName == Strings.UnitOfMeasureTypeName, true, null);
if (UnitOfMeasureType != null)
{
ActivateObject(UnitOfMeasureType);
}
else
{
throw new ApplicationException(String.Format("CodeType '{0}' doesn't exist.", Strings.UnitOfMeasureTypeName));
}
}
if (unitOfMeasureRoot == null)
{
unitOfMeasureRoot = TablesLogic.tCode.Load(TablesLogic.tCode.ObjectName == Strings.UnitOfMeasureRootName, true, null);
}
if (String.IsNullOrEmpty(unit))
{
throw new ApplicationException("UnitOfMeasure required.");
}
OCode code = TablesLogic.tCode.Create();
code.ObjectName = unit;
code.CodeTypeID = UnitOfMeasureType.ObjectID;
code.ParentID = unitOfMeasureRoot.ObjectID;
SaveObject(code);
return code;
}
#endregion
private void ImportCatalogue(DataRow row)
{
try
{
string name = ConvertToString(row[Map["CatalogueItemName"]]);
string catalogueType = ConvertToString(row[Map["CatalogueItemType"]]);
string unitOfMeasure = ConvertToString(row[Map["UnitOfMeasure"]]);
string inventoryCatalogType = ConvertToString(row[Map["InventoryCatalogType"]]);
decimal sellingPrice = Decimal.Zero;
try
{
if (!String.IsNullOrEmpty(row[Map["SellingPrice"]].ToString()))
sellingPrice = Convert.ToDecimal(row[Map["SellingPrice"]]);
}
catch { }
string stockCode = ConvertToString(row[Map["StockCode"]]);
if (name == null && catalogueType == null && unitOfMeasure == null && stockCode == null)
{
return;
}
CreateCatalogueHierarchy(catalogueType, name, unitOfMeasure, sellingPrice, stockCode, inventoryCatalogType);
}
catch (Exception ex)
{
row[Migratable.ERROR_MSG_COL] = ex.Message;
}
}
public static Guid? CreateCatalogueHierarchy(string catalogueType, string name, string unitOfMeasure, decimal sellingPrice, string StockCode, string inventoryCatalogType)
{
string[] folders = catalogueType.Split(',');
Guid? parentid = null;
for (int i = 0; i < folders.Length; i++)
{
string path = "";
for (int j = 0; j <= i; j++)
path += "," + folders[j];
parentid = GetCatalogue(parentid, folders[i], 0, unitOfMeasure, sellingPrice, StockCode, inventoryCatalogType);
}
parentid = GetCatalogue(parentid, name, 1, unitOfMeasure, sellingPrice, StockCode, inventoryCatalogType);
return parentid;
}
private static Guid? GetCatalogue(Guid? parentid, string name, int type, string unitOfMeasure, decimal sellingPrice, string StockCode, string inventoryCatalogType)
{
OCatalogue o;
string item = name;
OCode measure = null;
if (unitOfMeasure != null && unitOfMeasure.Trim() != String.Empty)
{
measure = GetUnitOfMeasure(unitOfMeasure);
}
//***********************
// Assumption that items under the same parent has unique names.
//***********************
if (type == 1)
o = TablesLogic.tCatalogue.Load(TablesLogic.tCatalogue.StockCode != "" & TablesLogic.tCatalogue.ParentID == parentid & TablesLogic.tCatalogue.ObjectName == item, true, null);
// o = TablesLogic.tCatalogue.Load(
// TablesLogic.tCatalogue.IsCatalogueItem==type &
// TablesLogic.tCatalogue.StockCode == StockCode, true, null);
else o = TablesLogic.tCatalogue.Load(TablesLogic.tCatalogue.ParentID == parentid & TablesLogic.tCatalogue.ObjectName == item, true, null);
// End Assumption
if (o == null)
{
o = CreateCatalogue(item, parentid, type, measure, sellingPrice, StockCode, inventoryCatalogType);
}
else if (o != null)
{
if (o.IsDeleted == 1)
ActivateObject(o);
o.IsCatalogueItem = type;
o.ObjectName = item;
if (StockCode != "") o.StockCode = StockCode;
o.ParentID = parentid;
o.UnitOfMeasure = measure;
o.InventoryCatalogType =
inventoryCatalogType.ToUpper() == "CONSUMABLE" ? InventoryCatalogType.Consumable :
InventoryCatalogType.NonConsumable;
}
SaveObject(o);
if (!catalogueGroupList.ContainsKey(name))
{
catalogueGroupList.Add(name, o.ObjectID);
}
return o.ObjectID;
}
private static OCatalogue CreateCatalogue(string catalogueName, Guid? parentId, int type, OCode unitOfMeasure, decimal sellingPrice, string stockCode, string inventoryCatalogType)
{
OCatalogue catalogue = TablesLogic.tCatalogue.Create();
catalogue.ObjectName = catalogueName;
//if (type == 0)
//{
catalogue.ParentID = parentId;
//}
catalogue.IsCatalogueItem = type;
catalogue.UnitOfMeasureID = unitOfMeasure == null ? null : unitOfMeasure.ObjectID;
catalogue.UnitPrice = sellingPrice;
catalogue.StockCode = stockCode;
catalogue.InventoryCatalogType =
inventoryCatalogType.ToUpper() == "CONSUMABLE" ? InventoryCatalogType.Consumable :
InventoryCatalogType.NonConsumable;
SaveObject(catalogue);
if (type == 0)
{
if (!catalogueGroupList.ContainsKey(catalogueName))
{
catalogueGroupList.Add(catalogueName, catalogue.ObjectID);
}
}
return catalogue;
}
}
}