-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShipmentTracking.lua
216 lines (166 loc) · 6.75 KB
/
ShipmentTracking.lua
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
-- About TrackShipping.lua
-- Author: Atlas Systems, Inc.
--
-- TrackShipping.lua checks the value of the transaction field specified by the ConstIlliadField variable. If it contains a
-- value, it then does a very basic comparison to determine if it is a UPS or Fedex tracking number and searchings the tracking number
-- on the appropriate tracking form.
local settings = {};
settings.AutoSearch = GetSetting("AutoSearch");
settings.ShippingOptions_USPS = GetSetting("ShippingOptions_USPS");
settings.ShippingOptions_Fedex = GetSetting("ShippingOptions_Fedex");
settings.ShippingOptions_UPS = GetSetting("ShippingOptions_UPS");
local interfaceMngr = nil;
local trackingForm = {};
trackingForm.Form = nil;
trackingForm.Browser = nil;
trackingForm.RibbonPage = nil;
-- Constants
local ConstTypeFedex = "Fedex";
local ConstTypeUps = "UPS";
local ConstTypeUsps = "USPS";
local ConstFedexAddress = "https://www.fedex.com/wtrk/track/?trknbr=";
local ConstUpsAddress = "http://wwwapps.ups.com/WebTracking/track?loc=en_US&track.x=Track&trackNums=";
local ConstUspsAddress = "http://trkcnfrm1.smi.usps.com/PTSInternetWeb/InterLabelInquiry.do?trackGo.x=10&trackGo.y=7&strOrigTrackNum=";
local ConstIlliadField = GetSetting("ILLiadField");
luanet.load_assembly("log4net");
local Types = {};
Types["log4net.LogManager"] = luanet.import_type("log4net.LogManager");
local log = Types["log4net.LogManager"].GetLogger("AtlasSystems.Addons.ShipmentTracking");
require "AtlasHelpers";
function Init()
if IsValidTransaction() then
log:Debug("ShipmentTracking Init");
interfaceMngr = GetInterfaceManager();
-- Create a form
trackingForm.Form = interfaceMngr:CreateForm("Shipments", "Script");
-- Add a browser
trackingForm.Browser = trackingForm.Form:CreateBrowser("Shipments", "Shipments Browser", "Shipments", "WebView2");
-- Hide the text label
trackingForm.Browser.TextVisible = false;
-- Since we didn't create a ribbon explicitly before creating our browser, it will have created one using the name we passed the CreateBrowser method. We can retrieve that one and add our buttons to it.
trackingForm.RibbonPage = trackingForm.Form:GetRibbonPage("Shipments");
-- Create the search button
trackingForm.RibbonPage:CreateButton("Smart Search", GetClientImage("Search32"), "Search", "Tracking Number");
trackingForm.RibbonPage:CreateButton("Search Fedex", GetClientImage("Search32"), "SearchFedex", "Tracking Number");
trackingForm.RibbonPage:CreateButton("Search USPS", GetClientImage("Search32"), "SearchUSPS", "Tracking Number");
trackingForm.RibbonPage:CreateButton("Search UPS", GetClientImage("Search32"), "SearchUPS", "Tracking Number");
-- After we add all of our buttons and form elements, we can show the form.
trackingForm.Form:Show();
-- Search when opened if autoSearch is true
if settings.AutoSearch then
Search();
end
end
end
function Search()
log:Debug("Search");
local trackingType = GetTrackingNumberType(GetFieldValue("Transaction", ConstIlliadField));
log:Debug("Tracking Type = " .. trackingType);
if trackingType == ConstTypeUps then
SearchUPS();
elseif trackingType == ConstTypeFedex then
SearchFedex();
elseif trackingType == ConstTypeUsps then
SearchUSPS();
end;
end
function SearchFedex()
trackingForm.Browser:Navigate(ConstFedexAddress .. GetSearchableTrackingNumber());
end
function SearchUSPS()
trackingForm.Browser:Navigate(ConstUspsAddress .. GetSearchableTrackingNumber());
end
function SearchUPS()
trackingForm.Browser:Navigate(ConstUpsAddress .. GetSearchableTrackingNumber());
end
function GetSearchableTrackingNumber()
local trackingNumber = GetFieldValue("Transaction", ConstIlliadField);
local separatorIndex = string.find(trackingNumber, ":", 1, true);
if (separatorIndex ~= nil) then
trackingNumber = string.sub(trackingNumber, separatorIndex + 1);
end
return AtlasHelpers.UrlEncode(trackingNumber);
end
function IsValidTransaction()
log:Debug("IsValidTransaction()");
return (GetFieldValue("Transaction", "ProcessType") == "Lending"
and GetFieldValue("Transaction", "TransactionStatus") == "Item Shipped");
end
function GetTrackingNumberType(trackingNumString)
log:Debug("Getting Tracking Number Type");
trackingNumString = trackingNumString:gsub(" ", "");
local trackingType = GetServiceFromNumberPrefix(trackingNumString);
if (trackingType ~= nil) then
return trackingType;
else
trackingType = GetServiceFromShippingMethod();
if (trackingType ~= nil) then
return trackingType;
else
trackingType = GetServiceFromNumber(trackingNumString);
return trackingType;
end
end
end
function GetServiceFromShippingMethod()
local shippingOptions = GetFieldValue("Transaction", "ShippingOptions");
if (shippingOptions == nil) then
return nil;
end
shippingOptions = string.lower(shippingOptions);
if(ShippingOptionMatches(shippingOptions, settings.ShippingOptions_UPS, ConstTypeUps)) then
return ConstTypeUps;
elseif(ShippingOptionMatches(shippingOptions, settings.ShippingOptions_Fedex, ConstTypeFedex)) then
return ConstTypeFedex;
elseif(ShippingOptionMatches(shippingOptions, settings.ShippingOptions_USPS, ConstTypeUsps)) then
return ConstTypeUsps;
end
end
function ShippingOptionMatches(optionString, keywordString, shippingType)
if (shippingType == ConstTypeUsps and string.find(optionString, "usps") ~= nil) then
return true;
elseif (shippingType == ConstTypeFedex and string.find(optionString, "fedex") ~= nil) then
return true;
elseif (shippingType == ConstTypeUps and string.find(optionString, "ups") ~= nil) then
return true;
else
local keywords = AtlasHelpers.StringSplit(',', keywordString);
for i = 1, #keywords do
if (string.lower(keywords[i]) == optionString) then
return true;
end
end
return false;
end
end
function GetServiceFromNumberPrefix(trackingNumString)
local seperatorIndex = string.find(trackingNumString, ":", 1, true);
if (seperatorIndex == nil) then
return nil;
end;
local prefix = string.lower(string.sub(trackingNumString, 1, seperatorIndex - 1));
if (string.find(prefix, "usps") ~= nil) then
return ConstTypeUsps;
elseif (string.find(prefix, "fedex") ~= nil) then
return ConstTypeFedex;
elseif (string.find(prefix, "ups") ~= nil) then
return ConstTypeUps;
else
return nil;
end
end
function GetServiceFromNumber(trackingNumString)
log:Debug("Tracking Number Length = " .. string.len(trackingNumString));
local trackingType = nil;
if string.len(trackingNumString) == 0 then
trackingType = "UNKNOWN";
elseif string.len(trackingNumString) < 18 then
trackingType = ConstTypeFedex;
elseif string.len(trackingNumString) < 20 then
trackingType = ConstTypeUps;
else
trackingType = ConstTypeUsps;
end
log:Debug("Returning " .. trackingType .. " from GetTrackingNumberType");
return trackingType;
end