Statistiques
| Révision:

root / LicensesMercure / UFOrder.pas

Historique | Voir | Annoter | Télécharger (16,808 ko)

1
unit UFOrder;
2

    
3
interface
4

    
5
uses
6
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7
  Dialogs, StdCtrls, Buttons, PBNumEdit, IdBaseComponent, IdComponent,
8
  IdTCPConnection, IdTCPClient, IdFTP, ShellAPI, IdMessage, IdMessageClient,
9
  IdAttachmentFile, IdSMTP, IdSMTPBase, IdExplicitTLSClientServerBase,
10
  ExtCtrls, mysql, IdIMAP4, IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack,
11
  IdSSL, IdSSLOpenSSL, WinInet;
12

    
13
type
14
  TFOrder = class(TForm)
15
    EKey: TEdit;
16
    BBSkip: TBitBtn;
17
    BBProcess: TBitBtn;
18
    BBRemove: TBitBtn;
19
    EContact: TEdit;
20
    LContact: TLabel;
21
    EMail: TEdit;
22
    LMail: TLabel;
23
    ECompany: TEdit;
24
    LCompany: TLabel;
25
    LPostalCode: TLabel;
26
    LCity: TLabel;
27
    LCountry: TLabel;
28
    EPostalCode: TEdit;
29
    ECity: TEdit;
30
    ECountry: TEdit;
31
    GBAddress: TGroupBox;
32
    EOrderNumber: TEdit;
33
    LOrderNumber: TLabel;
34
    BBOrderFile: TBitBtn;
35
    LWarning: TLabel;
36
    LLicenses: TLabel;
37
    EFirstName1: TEdit;
38
    LFirstName: TLabel;
39
    ELastName1: TEdit;
40
    LLastName: TLabel;
41
    LLicenseNumber: TLabel;
42
    EFirstName2: TEdit;
43
    ELastName2: TEdit;
44
    EFirstName3: TEdit;
45
    ELastName3: TEdit;
46
    EFirstName4: TEdit;
47
    ELastName4: TEdit;
48
    EFirstName5: TEdit;
49
    ELastName5: TEdit;
50
    PBLicenses: TPBNumEdit;
51
    PBLicenseNumber1: TPBNumEdit;
52
    PBLicenseNumber2: TPBNumEdit;
53
    PBLicenseNumber3: TPBNumEdit;
54
    PBLicenseNumber4: TPBNumEdit;
55
    PBLicenseNumber5: TPBNumEdit;
56
    SMTP: TIdSMTP;
57
    MInraTransfert: TIdMessage;
58
    MObservations: TMemo;
59
    MAcknowledgement: TIdMessage;
60
    PLicenses: TPanel;
61
    EAddress1: TEdit;
62
    EAddress2: TEdit;
63
    PLicense1: TPanel;
64
    PLicense2: TPanel;
65
    PLicense3: TPanel;
66
    PLicense4: TPanel;
67
    PLicense5: TPanel;
68
    PLicense6: TPanel;
69
    EFirstName6: TEdit;
70
    ELastName6: TEdit;
71
    PBLicenseNumber6: TPBNumEdit;
72
    PLicense7: TPanel;
73
    EFirstName7: TEdit;
74
    ELastName7: TEdit;
75
    PBLicenseNumber7: TPBNumEdit;
76
    PLicense8: TPanel;
77
    EFirstName8: TEdit;
78
    ELastName8: TEdit;
79
    PBLicenseNumber8: TPBNumEdit;
80
    PLicense9: TPanel;
81
    EFirstName9: TEdit;
82
    ELastName9: TEdit;
83
    PBLicenseNumber9: TPBNumEdit;
84
    PLicense10: TPanel;
85
    EFirstName10: TEdit;
86
    ELastName10: TEdit;
87
    PBLicenseNumber10: TPBNumEdit;
88
    EVAT: TEdit;
89
    IMAP4: TIdIMAP4;
90
    OpenSSL_IMAP4: TIdSSLIOHandlerSocketOpenSSL;
91
    OpenSSL_SMTP: TIdSSLIOHandlerSocketOpenSSL;
92
    procedure FormShow(Sender: TObject);
93
    procedure BBSkipClick(Sender: TObject);
94
    procedure BBProcessClick(Sender: TObject);
95
    procedure BBRemoveClick(Sender: TObject);
96
    procedure BBOrderFileClick(Sender: TObject);
97
    procedure FormCreate(Sender: TObject);
98
  private
99
    { D?clarations priv?es }
100
    Key: String;
101
    Language: String;
102
    Contact, Mail, Company, VAT: String;
103
    Address1, Address2, PostalCode, City, Country: String;
104
    OrderNumber, OrderFile: String;
105
    Licenses: Integer;
106
    FirstName, LastName: array[1..10] of String;
107
    Observations: String;
108
    procedure DisplayRequest;
109
  public
110
    { D?clarations publiques }
111
    function GetRequest: Boolean;
112
  end;
113

    
114
var
115
  FOrder: TFOrder;
116

    
117
implementation
118

    
119
uses
120
  UFMenu;
121

    
122
{$R *.dfm}
123

    
124
procedure TFOrder.FormCreate(Sender: TObject);
125
begin
126
  Top := 0;
127
  Left := 0;
128
  with IMAP4 do
129
  begin
130
    Host := IMAP_SERVER;
131
//    Username := EXCHANGE_USERNAME + '\' + EXCHANGE_SHARED_MAILBOX;
132
    Username := 'INRA\' + FMenu.EUsername.Text + '\' + EXCHANGE_SHARED_MAILBOX;
133
//    Password := EXCHANGE_PASSWORD;
134
    Password := FMenu.EPassword.Text;
135
  end;
136
  with SMTP do
137
  begin
138
    Host := SMTP_SERVER;
139
//    Username := EXCHANGE_USERNAME;
140
    Username := 'INRA\' + FMenu.EUsername.Text;
141
//    Password := EXCHANGE_PASSWORD;
142
    Password := FMenu.EPassword.Text;
143
  end;
144
end;
145

    
146
procedure TFOrder.FormShow(Sender: TObject);
147
begin
148
  DisplayRequest;
149
end;
150

    
151
procedure TFOrder.BBSkipClick(Sender: TObject);
152
begin
153
  Inc(cur);
154
  if (cur < SLOrder.Count) and GetRequest
155
  then
156
    DisplayRequest
157
  else
158
    Close;
159
end;
160

    
161
procedure TFOrder.BBProcessClick(Sender: TObject);
162
var
163
  i: Integer;
164
  p: TPanel;
165
  StrMessage: TStrings;
166
begin
167
  FMenu.MLog.Lines.Add('Recording order...');
168
  with FMenu.TOrder do
169
  begin
170
    Insert;
171
    FieldByName('Key').AsString := Key;
172
    FieldByName('Language').AsString := Language;
173
    FieldByName('Contact').AsString := EContact.Text;
174
    FieldByName('Mail').AsString := EMail.Text;
175
    FieldByName('Company').AsString := ECompany.Text;
176
    FieldByName('VAT').AsString := EVAT.Text;
177
    FieldByName('Address1').AsString := EAddress1.Text;
178
    FieldByName('Address2').AsString := EAddress2.Text;
179
    FieldByName('PostalCode').AsString := EPostalCode.Text;
180
    FieldByName('City').AsString := ECity.Text;
181
    FieldByName('Country').AsString := ECountry.Text;
182
    FieldByName('OrderNumber').AsString := EOrderNumber.Text;
183
    FieldByName('OrderFile').AsString := OrderFile;
184
    FieldByName('Licenses').AsInteger := PBLicenses.AsInteger;
185
    Post;
186
  end;
187
  FMenu.MLog.Lines.Add('Recording licenses (type 0)...');
188
  for i := 1 to PBLicenses.AsInteger do
189
    with FMenu.TLicense do
190
    begin
191
      Insert;
192
      FieldByName('Validity').AsBoolean := False;
193
      FieldByName('Type').AsInteger := 0;
194
      FieldByName('Source').AsString := Key;
195
      p := PLicenses.Controls[i + 2] as TPanel;
196
      FieldByName('LicenseNumber').AsInteger := (p.Controls[2] as TPBNumEdit).AsInteger;
197
      FieldByName('FirstName').AsString := (p.Controls[0] as TEdit).Text;
198
      FieldByName('LastName').AsString := (p.Controls[1] as TEdit).Text;
199
      FieldByName('Company').AsString := ECompany.Text;
200
      FieldByName('FinalDate').AsDateTime := IncMonth(Date, 2);
201
      Post;
202
    end;
203
  FMenu.MLog.Lines.Add('Mailing to ' + MAIL_INRA_TRANSFERT_1 + '...');
204
  with MInraTransfert do
205
  begin
206
    Body.Clear;
207
    MessageParts.Clear;
208
    From.Name := 'InraPorc';
209
    From.Address := MAIL_INRAPORC;
210
    Recipients.EMailAddresses := MAIL_INRA_TRANSFERT_1;
211
    CCList.EMailAddresses := MAIL_INRA_TRANSFERT_2;
212
    Body.Add(Format('Commande num?ro "%s" portant sur %d licence(s)', [Key, PBLicenses.AsInteger]));
213
    Body.Add('');
214
    Body.Add(EContact.Text);
215
    if Length(ECompany.Text) > 0
216
    then
217
      if Length(EVAT.Text) > 0
218
      then
219
        Body.Add(Format ('%s (%s)', [ECompany.Text, EVAT.Text]))
220
      else
221
        Body.Add(ECompany.Text);
222
    if Length(EAddress1.Text) > 0
223
    then
224
      Body.Add(EAddress1.Text);
225
    if Length(EAddress2.Text) > 0
226
    then
227
      Body.Add(EAddress2.Text);
228
    Body.Add(Format('%s %s', [EPostalCode.Text, ECity.Text]));
229
    Body.Add(ECountry.Text);
230
    Body.Add('');
231
    Body.Add(EMail.Text);
232
    if Length(EOrderNumber.Text) > 0
233
    then
234
    begin
235
      Body.Add('');
236
      Body.Add(Format('N? commande client : %s', [EOrderNumber.Text]));
237
    end;
238
    if (Length(OrderFile) > 0) and FileExists(SHARED_DRIVE + SHARED_FOLDER + 'Fichiers\' + OrderFile)
239
    then
240
    begin
241
      ContentType := 'multipart/mixed';
242
      TIdAttachmentFile.Create(MessageParts, SHARED_DRIVE + SHARED_FOLDER + '\Fichiers\' + OrderFile);
243
    end;
244
    Body.Add('');
245
    Body.Add('Les num?ros de licence suivants ont ?t? attribu?s :');
246
    for i := 1 to PBLicenses.AsInteger do
247
    begin
248
      p := PLicenses.Controls[i + 2] as TPanel;
249
      Body.Add(Format('%d : %s %s', [(p.Controls[2] as TPBNumEdit).AsInteger, (p.Controls[0] as TEdit).Text, (p.Controls[1] as TEdit).Text]));
250
    end;
251
    if Length(MObservations.Text) > 0
252
    then
253
    begin
254
      Body.Add('');
255
      Body.Add('OBSERVATIONS');
256
      Body.Add('------------');
257
      Body.Add(MObservations.Text);
258
    end;
259
    Body.Add('');
260
    Body.Add('Cordialement');
261
    Body.Add('--');
262
    Body.Add(FMenu.EAdmin.Text);
263
  end;
264
  FMenu.MLog.Lines.Add('Mailing to ' + EMail.Text + '...');
265
  with MAcknowledgement do
266
  begin
267
    Body.Clear;
268
    From.Name := 'InraPorc';
269
    From.Address := MAIL_INRAPORC;
270
    Recipients.EMailAddresses := EMail.Text;
271
    StrMessage := TStringList.Create;
272
    if Language = 'FR'
273
    then // Accus? de r?ception (fran?ais)
274
    begin
275
      Subject := '[InraPorc] Confirmation de commande';
276
      Body.Add(Format('Votre bon de commande pour %d licence(s) d''InraPorc a ?t? enregistr?e sous le num?ro "%s".', [PBLicenses.AsInteger, Key]));
277
      Body.Add('Votre demande a ?t? transf?r?e ? Inra Transfert, qui vous enverra la facture.');
278
      Body.Add('');
279
      Body.Add('Les num?ros de licence suivants ont ?t? attribu?s :');
280
      for i := 1 to PBLicenses.AsInteger do
281
      begin
282
        p := PLicenses.Controls[i + 2] as TPanel;
283
        Body.Add(Format('%d : %s %s', [(p.Controls[2] as TPBNumEdit).AsInteger, (p.Controls[0] as TEdit).Text, (p.Controls[1] as TEdit).Text]));
284
      end;
285
      Body.Add('');
286
      StrMessage.LoadFromFile(SHARED_DRIVE + SHARED_FOLDER + 'Messages\Commande_FR.txt');
287
      Body.AddStrings(StrMessage);
288
      Body.Add('');
289
      StrMessage.LoadFromFile(SHARED_DRIVE + SHARED_FOLDER + 'Messages\Signature_FR.txt');
290
      Body.AddStrings(StrMessage);
291
    end
292
    else // Acknowledgement (english)
293
    begin
294
      Subject := '[InraPorc] Order confirmation';
295
      Body.Add(Format('Your purchase order for %d license(s) of InraPorc has been registered as number "%s".', [PBLicenses.AsInteger, Key]));
296
      Body.Add('Your request has been forwarded to Inra Transfert, who will send you an invoice.');
297
      Body.Add('');
298
      Body.Add('The following license numbers have been assigned:');
299
      for i := 1 to PBLicenses.AsInteger do
300
      begin
301
        p := PLicenses.Controls[i + 2] as TPanel;
302
        Body.Add(Format('%d: %s %s', [(p.Controls[2] as TPBNumEdit).AsInteger, (p.Controls[0] as TEdit).Text, (p.Controls[1] as TEdit).Text]));
303
      end;
304
      Body.Add('');
305
      StrMessage.LoadFromFile(SHARED_DRIVE + SHARED_FOLDER + 'Messages\Commande_EN.txt');
306
      Body.AddStrings(StrMessage);
307
      Body.Add('');
308
      StrMessage.LoadFromFile(SHARED_DRIVE + SHARED_FOLDER + 'Messages\Signature_EN.txt');
309
      Body.AddStrings(StrMessage);
310
    end;
311
    StrMessage.Free;
312
  end;
313
  FMenu.MLog.Lines.Add('SMTP...');
314
  try
315
    SMTP.Connect;
316
    try
317
      SMTP.Send(MInraTransfert);
318
      SMTP.Send(MAcknowledgement);
319
    finally
320
      SMTP.Disconnect;
321
      if IMAP4.Connect
322
      then
323
        try
324
          if not IMAP4.AppendMsg('?l?ments envoy?s', MInraTransfert, MInraTransfert.LastGeneratedHeaders)
325
          or not IMAP4.AppendMsg('?l?ments envoy?s', MAcknowledgement, MAcknowledgement.LastGeneratedHeaders)
326
          then
327
            FMenu.MLog.Lines.Add('Message could not be copied in IMAP server !');
328
        finally
329
          IMAP4.Disconnect;
330
        end
331
      else
332
        FMenu.MLog.Lines.Add('IMAP server could not be connected !');
333
    end;
334
  except
335
    FMenu.MLog.Lines.Add('Messages could not be sent !');
336
  end;
337
  BBRemoveClick(nil);
338
end;
339

    
340
procedure TFOrder.BBRemoveClick(Sender: TObject);
341
var
342
  i: Integer;
343
begin
344
  FMenu.MLog.Lines.Add('Logging record...');
345
  with FMenu.TOrders do
346
  begin
347
    Append;
348
    FieldByName('Date').AsDateTime := Now;
349
    FieldByName('Admin').AsString := FMenu.EAdmin.Text;
350
    FieldByName('Key').AsString := Key;
351
    FieldByName('Language').AsString := Language;
352
    FieldByName('Contact').AsString := Contact;
353
    FieldByName('Mail').AsString := Mail;
354
    FieldByName('Company').AsString := Company;
355
    FieldByName('VAT').AsString := VAT;
356
    FieldByName('Address1').AsString := Address1;
357
    FieldByName('Address2').AsString := Address2;
358
    FieldByName('PostalCode').AsString := PostalCode;
359
    FieldByName('City').AsString := City;
360
    FieldByName('Country').AsString := Country;
361
    FieldByName('OrderNumber').AsString := OrderNumber;
362
    FieldByName('OrderFile').AsString := OrderFile;
363
    FieldByName('Licenses').AsInteger := Licenses;
364
    for i := 1 to 10 do
365
    begin
366
      FieldByName(Format('FirstName%d', [i])).AsString := FirstName[i];
367
      FieldByName(Format('LastName%d', [i])).AsString := LastName[i];
368
    end;
369
    FieldByName('Observations').AsVariant := Observations;
370
    Post;
371
  end;
372
  que := 'DELETE FROM `commandes` where `Key` = ''' + SLOrder[cur] + '''';
373
  FMenu.MLog.Lines.Add(que);
374
  if mysql_query(con, PAnsiChar(que)) <> 0
375
  then
376
    FMenu.MLog.Lines.Add(mysql_error(con))
377
  else
378
  begin
379
    SLOrder.Delete(cur);
380
    FMenu.PBOrders.AsInteger := FMenu.PBOrders.AsInteger - 1;
381
    Dec(cur);
382
  end;
383
  BBSkipClick(nil);
384
end;
385

    
386
procedure TFOrder.BBOrderFileClick(Sender: TObject);
387
begin
388
  if ShellExecute(Handle, 'open', PChar(SHARED_DRIVE + SHARED_FOLDER + 'Fichiers\' + OrderFile), nil, nil, SW_SHOW) <= 32
389
  then
390
    FMenu.MLog.Lines.Add(SysErrorMessage(GetLastError));
391
end;
392

    
393
function TFOrder.GetRequest: Boolean;
394
var
395
  i: Integer;
396
begin
397
  Result := False;
398
  que := 'SELECT * FROM `commandes` where `Key` = ''' + SLOrder[cur] + '''';
399
  FMenu.MLog.Lines.Add(que);
400
  if mysql_query(con, PAnsiChar(que)) <> 0
401
  then
402
    FMenu.MLog.Lines.Add(mysql_error(con))
403
  else
404
  begin
405
    res := mysql_store_result(con);
406
    if res = nil
407
    then
408
      FMenu.MLog.Lines.Add(mysql_error(con))
409
    else
410
    begin
411
      row := mysql_fetch_row(res);
412
      if row = nil
413
      then
414
        FMenu.MLog.Lines.Add(mysql_error(con))
415
      else
416
      begin
417
        Key := row^[0];
418
        Language := row^[1];
419
        Contact := row^[2];
420
        Mail := row^[3];
421
        Company := row^[4];
422
        VAT := row^[5];
423
        Address1 := row^[6];
424
        Address2 := row^[7];
425
        PostalCode := row^[8];
426
        City := row^[9];
427
        Country := row^[10];
428
        OrderNumber := row^[11];
429
        OrderFile := row^[12];
430
        Licenses := StrToInt(row^[13]);
431
        for i := 1 to 10 do
432
        begin
433
          FirstName[i] := row^[2*i+12];
434
          LastName[i] := row^[2*i+13];
435
        end;
436
        Observations := row^[34];
437
        Result := True;
438
      end;
439
      mysql_free_result(res);
440
    end;
441
  end;
442
end;
443

    
444
procedure TFOrder.DisplayRequest;
445
const
446
  ReadBuffer = 1024;
447
var
448
  Command: String;
449
  Security: TSecurityAttributes;
450
  ReadPipe, WritePipe: THandle;
451
  Start: TStartUpInfo;
452
  ProcessInfo: TProcessInformation;
453
  Buffer: PAnsiChar;
454
  BytesRead: DWord;
455
  Apprunning: DWord;
456
  i: Integer;
457
  p: TPanel;
458
begin
459
  EKey.Text := Key;
460
  EContact.Text := Contact;
461
  EMail.Text := Mail;
462
  ECompany.Text := Company;
463
  EVAT.Text := VAT;
464
  EAddress1.Text := Address1;
465
  EAddress2.Text := Address2;
466
  EPostalCode.Text := PostalCode;
467
  ECity.Text := City;
468
  ECountry.Text := Country;
469
  EOrderNumber.Text := OrderNumber;
470
  BBOrderFile.Enabled := Length(OrderFile) > 0;
471
  BBOrderFile.Hint := Format('Afficher le bon de commande joint (%s)', [OrderFile]);
472
  if (Length(OrderFile) > 0) and not FileExists(SHARED_DRIVE + SHARED_FOLDER + 'Fichiers\' + OrderFile)
473
  then
474
    try
475
      FMenu.MLog.Lines.Add('SCP ' + SCP_HOST + '...');
476
      Security.nLength := SizeOf(TSecurityAttributes);
477
      Security.bInheritHandle := true;
478
      Security.lpSecurityDescriptor := nil;
479
      if Createpipe (ReadPipe, WritePipe, @Security, 0)
480
      then
481
      begin
482
        Buffer := AllocMem(ReadBuffer + 1);
483
        FillChar(Start, Sizeof(Start), #0);
484
        Start.cb := SizeOf(Start);
485
        Start.hStdOutput := WritePipe;
486
        Start.hStdInput := ReadPipe;
487
        Start.dwFlags := STARTF_USESTDHANDLES + STARTF_USESHOWWINDOW;
488
        Start.wShowWindow := SW_HIDE;
489
        Command := 'pscp.exe -batch -hostkey ' + SCP_HOSTKEY + ' -pw ' + SCP_PASSWD + ' ' + SCP_LOGIN + '@' + SCP_HOST + ':' + SCP_PATH + OrderFile + ' ' + SHARED_DRIVE + SHARED_FOLDER + 'Fichiers';
490
        if CreateProcess(nil, PChar(Command), @Security, @Security, true, NORMAL_PRIORITY_CLASS, nil, nil, Start, ProcessInfo)
491
        then
492
        begin
493
          repeat
494
            Apprunning := WaitForSingleObject(ProcessInfo.hProcess, 100);
495
            Application.ProcessMessages;
496
          until (Apprunning <> WAIT_TIMEOUT);
497
          repeat
498
            BytesRead := 0;
499
            ReadFile(ReadPipe,Buffer[0], ReadBuffer, BytesRead, nil);
500
            Buffer[BytesRead] := #0;
501
            OemToAnsi(Buffer, Buffer);
502
            FMenu.MLog.Text := FMenu.MLog.text + String(Buffer);
503
          until (BytesRead < ReadBuffer);
504
        end;
505
        FreeMem(Buffer);
506
        CloseHandle(ProcessInfo.hProcess);
507
        CloseHandle(ProcessInfo.hThread);
508
        CloseHandle(ReadPipe);
509
        CloseHandle(WritePipe);
510
      end;
511
    except
512
      FMenu.MLog.Lines.Add('File ' + OrderFile + ' could not be got !');
513
    end;
514
  PBLicenses.AsInteger := Licenses;
515
  for i := 1 to 10 do
516
  begin
517
    p := PLicenses.Controls[i + 2] as TPanel;
518
    p.Visible := i <= Licenses;
519
    (p.Controls[0] as TEdit).Text := FirstName[i];
520
    (p.Controls[1] as TEdit).Text := LastName[i];
521
    (p.Controls[2] as TPBNumEdit).AsInteger := FMenu.TLicense.RecordCount + i;
522
  end;
523
  MObservations.Text := Observations;
524
end;
525

    
526
end.