-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.pas
381 lines (350 loc) · 10.7 KB
/
Main.pas
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
unit Main;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.RegularExpressions,
System.Variants, System.Generics.Collections, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Files, Records, Mail,
Vcl.ComCtrls, DateUtils;
type
TForm2 = class(TForm)
Button1: TButton;
Button2: TButton;
Memo1: TMemo;
edtFilename: TEdit;
lblPVL1: TLabel;
lblCustomers1: TLabel;
lblPVL2: TLabel;
lblCustomers2: TLabel;
lblCustomers3: TLabel;
lblPVL3: TLabel;
lblCustomers4: TLabel;
lblPVL4: TLabel;
lblCustomers6: TLabel;
lblPVL6: TLabel;
lblCustomers5: TLabel;
lblPVL5: TLabel;
lbxCustomers1: TListBox;
lbxCustomers2: TListBox;
lbxCustomers3: TListBox;
lbxCustomers4: TListBox;
lbxCustomers5: TListBox;
lblData: TLabel;
edtWantedRoutes: TEdit;
edDate: TEdit;
edRoutes1: TEdit;
edRoutes4: TEdit;
edRoutes3: TEdit;
edRoutes2: TEdit;
edRoutes6: TEdit;
edRoutes5: TEdit;
edTruck1: TEdit;
edDeliverer1: TEdit;
edDeliverer3: TEdit;
edTruck3: TEdit;
edDeliverer5: TEdit;
edTruck5: TEdit;
edDeliverer2: TEdit;
edTruck2: TEdit;
edDeliverer4: TEdit;
edTruck4: TEdit;
edDeliverer6: TEdit;
edTruck6: TEdit;
lbxCustomers6: TListBox;
edtEmailAdress: TEdit;
edtEmailpassword: TEdit;
btnMail: TButton;
cbxMailServer: TComboBoxEx;
edtFolder: TEdit;
edtDateAfter: TEdit;
edtDateBefore: TEdit;
lblDownloadedFile: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
function getWantedRoutes(name: String = 'edtWantedRoutes'): TWantedRoutes;
procedure FillListBoxes(dctRoutes: TDictionary<String, TRoute>);
procedure lbxCustomersDragDrop(Sender, Source: TObject; X, Y: Integer);
procedure lbxCustomersDragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure recalculateItems(lbxTarget: TListBox);
procedure btnMailClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure edtSenderDblClick(Sender: TObject);
procedure edtSenderExit(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.btnMailClick(Sender: TObject);
var
strUser, strPassword, strServer : String;
mail : GetMail;
params : TMailParams;
begin
strUser := edtEmailAdress.Text;
strPassword := edtEmailpassword.Text;
strServer := cbxMailServer.Items[cbxMailServer.ItemIndex];
params.folder := edtFolder.Text;
params.dateAfter := edtDateAfter.Text;
params.dateBefore := edtDateBefore.Text;
params.lblDnloadNotif := lblDownloadedFile;
params.edtFilename := edtFilename;
mail := GetMail.Create;
if (strUser <> '') and (strPassword <> '') and (strServer <> '') then
begin
mail.RetrieveMail(strServer, strUser, strPassword, params);
end;
end;
procedure TForm2.Button1Click(Sender: TObject);
var
dctRoutes: TDictionary<String, TRoute>;
lines, routeID, date : String;
data : TReadFile;
route : TRoute;
customer : TCustomer;
i, j: Integer;
routeLoad, absoluteLoad: Single;
customers : word;
wantedRoutes : TArray<String>;
begin
data := TReadFile.Create(edtFilename.Text);
data.LoadFile;
date := data.getDate;
edDate.Text := date;
Memo1.Hint := date;
dctRoutes := data.getRoutes;
wantedRoutes := TArray<String>(self.getWantedRoutes.arrRoutes);
lines := '';
absoluteLoad := 0.0;
customers := 0;
for i := 0 to High(wantedRoutes) do
begin
routeID := wantedRoutes[i];
if dctRoutes.ContainsKey(routeID) then
begin
route := dctRoutes[routeID];
lines := lines + Format('Ruta %s\t%s\n', [route.id, route.name]);
routeLoad := 0.0;
for j := 0 to High(route.customers) do
begin
customer := route.customers[j];
lines := lines + Format('%2d %-35s %-20s %8.3f\n', [
j+1, customer.name, customer.town, customer.load
]);
routeLoad := routeLoad + customer.load;
customers := customers + 1
end;
absoluteLoad := absoluteLoad + routeLoad;
lines := lines + Format('%68.3f\n\n', [routeLoad]);
end;
end;
{tab: [^I, #9], lf: [^J, #10], cr: [^M, #13]}
lines := lines + Format('%41s %3d %11s %10.3f', ['clients:', customers, 'Total PVL:', absoluteLoad]);
lines := lines.Replace('\t', #9).Replace('\n', #13#10);
Memo1.Text := lines;
data.Free;
FillListBoxes(dctRoutes);
end;
function TForm2.getWantedRoutes(name: String = 'edtWantedRoutes'): TWantedRoutes;
var
edtTarget : TEdit;
arrRoutes : TArray<String>;
strRoute, strRoutes : String;
rcdRoutes : TWantedRoutes;
i, j: Integer;
begin
edtTarget := TEdit(FindComponent(name));
if Length(edtTarget.Text) = 0 then
begin
rcdRoutes.strRoutes := '';
rcdRoutes.arrRoutes := [];
end
else
begin
strRoutes := edtTarget.Text;
strRoutes := strRoutes.Trim;
arrRoutes := strRoutes.Split([' ']);
strRoutes := '';
setLength(rcdRoutes.arrRoutes, Length(arrRoutes));
if Length(arrRoutes) > 1 then
begin
j := 0;
for i := 0 to High(arrRoutes) - 1 do
begin
strRoute := arrRoutes[i].Trim;
if strRoute = '' then continue;
strRoutes.Insert(strRoutes.Length, strRoute + ', ');
rcdRoutes.arrRoutes[j] := strRoute;
j := j + 1;
end;
strRoutes := strRoutes.TrimRight([',', ' ']);
strRoutes.Insert(strRoutes.Length, ' i ' + arrRoutes[High(arrRoutes)]);
rcdRoutes.strRoutes := strRoutes;
rcdRoutes.arrRoutes[j] := arrRoutes[High(arrRoutes)];
end
else
begin
strRoutes := edtTarget.Text;
strRoutes := strRoutes.Trim;
rcdRoutes.strRoutes := strRoutes;
rcdRoutes.arrRoutes := [strRoutes];
end;
end;
Result := rcdRoutes
end;
procedure TForm2.lbxCustomersDragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
Accept := True;
end;
procedure TForm2.lbxCustomersDragDrop(Sender, Source: TObject; X, Y: Integer);
var
lbxSource, lbxTarget : TListBox;
begin
if Source is TListBox then
begin
lbxSource := TListBox(Source);
lbxTarget := TListBox(Sender);
if(lbxSource.ItemIndex <> -1) then
begin
lbxTarget.Items.Add(lbxSource.Items[lbxSource.ItemIndex]);
lbxSource.Items.Delete(lbxSource.ItemIndex);
recalculateItems(lbxTarget);
recalculateItems(lbxSource);
end;
end;
end;
procedure TForm2.RecalculateItems(lbxTarget : TListBox);
var
item: String;
pvl : Single;
regexp : TRegEx;
index : shortint;
lblObject : TLabel;
begin
index := ShortInt(
String(lbxTarget.Name).SubString(Length(lbxTarget.Name)-1, 1).ToInteger()
);
regexp := TRegEx.Create('\s+([\d,]+)');
pvl := 0.0;
for item in lbxTarget.Items do
begin
pvl := pvl + regexp.Match(item).Groups[1].Value.ToSingle;
end;
lblObject := TLabel(FindComponent(Format('lblPVL%d', [index])));
lblObject.Caption := Format('%.3f', [pvl]);
lblObject := TLabel(FindComponent(Format('lblCustomers%d', [index])));
lblObject.Caption := lbxTarget.Items.Count.ToString;
end;
procedure TForm2.Button2Click(Sender: TObject);
var
rcdRoutes : TWantedRoutes;
begin
rcdRoutes := self.getWantedRoutes();
if Length(rcdRoutes.arrRoutes) = 0 then
begin
edtWantedRoutes.Text := '678 679 680 681 682 686 688 696';
Button2Click(Sender);
Exit;
end;
ShowMessage('Rutes: '+ rcdRoutes.strRoutes);
end;
procedure TForm2.edtSenderDblClick(Sender: TObject);
var
edtSender : TEdit;
begin
edtSender := TEdit(Sender);
if edtSender.ReadOnly = true then
begin
edtSender.ReadOnly := false;
edtSender.Color := clWindow;
end
else
begin
edtSender.ReadOnly := true;
edtSender.Color := clBtnFace;
end;
end;
procedure TForm2.edtSenderExit(Sender: TObject);
var
edtSender : TEdit;
begin
edtSender := TEdit(Sender);
edtSender.ReadOnly := true;
edtSender.Color := clBtnFace;
end;
procedure TForm2.FillListBoxes(dctRoutes: TDictionary<String, TRoute>);
var
i : Integer;
strName : String;
currentTEdit : TEdit;
currentListBox : TListBox;
currentLabel1, currentLabel2 : TLabel;
arrWantedRoutes : TArray<String>;
routeID : String;
currentLoad : Single;
customer : TCustomer;
customers : Word;
begin
for i := 1 to 6 do
begin
strName := Format('edRoutes%d', [i]);
currentTEdit := TEdit(FindComponent(strName));
currentListBox := TLIstBox(FindComponent(Format('lbxCustomers%d', [i])));
currentLabel1 := TLabel(FindComponent(Format('lblCustomers%d', [i])));
currentLabel2 := TLabel(FindComponent(Format('lblPVL%d', [i])));
if currentTEdit.Text <> '' then
begin
arrWantedRoutes := TArray<String>(self.getWantedRoutes(strName).arrRoutes);
currentListBox.Clear;
currentLoad := 0.0;
customers := 0;
for routeID in arrWantedRoutes do
begin
if dctRoutes.ContainsKey(routeID) then
begin
for customer in dctRoutes[routeID].customers do
begin
currentListBox.Items.Add(
Format(
'%-35s %8.3f', [
customer.name,
customer.load
]
)
);
customers := customers + 1;
currentLoad := currentLoad + customer.load;
end;
end;
end;
currentLabel1 := TLabel(FindComponent(Format('lblCustomers%d', [i])));
currentLabel1.Caption := customers.ToString();
currentLabel2 := TLabel(FindComponent(Format('lblPVL%d', [i])));
currentLabel2.Caption := Format('%.3f', [currentLoad]);
end
else
begin
currentListBox.Clear;
currentLabel1.Caption := '';
currentLabel2.Caption := '';
end;
end;
end;
procedure TForm2.FormCreate(Sender: TObject);
var
tmpFile : TReadFile;
rcdFile: TSearchRec;
begin
cbxMailServer.ItemIndex := 0;
edtDateAfter.Text := DateToStr(IncDay(Today, -10));
edtDateBefore.Text := DateToStr(Today);
tmpFile := TreadFile.Create();
rcdFile := tmpFile.getFisrtFile();
edtFilename.Text := rcdFile.Name
end;
end.