Statistiques
| Révision:

root / UnitFeedEdition.pas @ 1

Historique | Voir | Annoter | Télécharger (29,218 ko)

1
unit UnitFeedEdition;
2

    
3
interface
4

    
5
uses
6
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7
  Dialogs, gnugettext, StdCtrls, Grids, DBGrids, Buttons, Mask, DBCtrls, DB,
8
  JvExStdCtrls, JvEdit, JvValidateEdit, JvExControls, JvEnterTab, JvExMask,
9
  JvSpin, ExtCtrls, ComCtrls, ToolWin, ActnList, ImgList, JvButton, JvCtrls;
10

    
11
type
12
  TFormFeedEdition = class(TForm)
13
    GroupBoxIngredients: TGroupBox;
14
    GroupBoxComposition: TGroupBox;
15
    DBGridIngredients: TDBGrid;
16
    SpeedButtonAppend: TSpeedButton;
17
    SpeedButtonDelete: TSpeedButton;
18
    SpeedButtonReplace: TSpeedButton;
19
    DBEditTotal: TDBEdit;
20
    SpeedButtonUp: TSpeedButton;
21
    SpeedButtonDown: TSpeedButton;
22
    SpeedButtonName: TSpeedButton;
23
    SpeedButtonLevel: TSpeedButton;
24
    DBEditRemain: TDBEdit;
25
    CheckBoxPhytase: TCheckBox;
26
    DBGridComposition: TDBGrid;
27
    DBEditName: TDBEdit;
28
    GroupBoxPhytase: TGroupBox;
29
    DBEditPhytaseLevel: TDBEdit;
30
    DBEditPhytaseConcentration: TDBEdit;
31
    DBEditPhytaseIncorporation: TDBEdit;
32
    DBLookupComboBoxPhytase: TDBLookupComboBox;
33
    EnterAsTab: TJvEnterAsTab;
34
    SpinEditBonusC: TJvSpinEdit;
35
    SpinEditBonusT: TJvSpinEdit;
36
    GroupBoxPresentation: TGroupBox;
37
    GroupBoxBonus: TGroupBox;
38
    PanelHeader: TPanel;
39
    PanelButtons: TPanel;
40
    PanelMain: TPanel;
41
    ImageListIcons: TImageList;
42
    ActionListButtons: TActionList;
43
    ActionRename: TAction;
44
    ActionComment: TAction;
45
    ActionHelp: TAction;
46
    ActionOK: TAction;
47
    ActionCancel: TAction;
48
    LabelName: TLabel;
49
    LabelWarning: TLabel;
50
    LabelCrois: TLabel;
51
    LabelTruie: TLabel;
52
    LabelTotal: TLabel;
53
    LabelRemain: TLabel;
54
    LabelType: TLabel;
55
    LabelConcentration: TLabel;
56
    LabelIncorporation: TLabel;
57
    LabelClass: TLabel;
58
    ComboBoxClass: TComboBox;
59
    ComboBoxPresentation: TComboBox;
60
    JvImgBtnCancel: TJvImgBtn;
61
    JvImgBtnOK: TJvImgBtn;
62
    JvImgBtnHelp: TJvImgBtn;
63
    JvImgBtnRename: TJvImgBtn;
64
    JvImgBtnComment: TJvImgBtn;
65
    procedure FormCreate(Sender: TObject);
66
    procedure ComboBoxClassChange(Sender: TObject);
67
    procedure DBGridIngredientsDrawColumnCell(Sender: TObject;
68
      const Rect: TRect; DataCol: Integer; Column: TColumn;
69
      State: TGridDrawState);
70
    procedure DBGridIngredientsTitleClick(Column: TColumn);
71
    procedure SpeedButtonAppendClick(Sender: TObject);
72
    procedure SpeedButtonDeleteClick(Sender: TObject);
73
    procedure SpeedButtonReplaceClick(Sender: TObject);
74
    procedure SpeedButtonUpClick(Sender: TObject);
75
    procedure SpeedButtonDownClick(Sender: TObject);
76
    procedure SpeedButtonNameClick(Sender: TObject);
77
    procedure SpeedButtonLevelClick(Sender: TObject);
78
    procedure DBGridCompositionDrawColumnCell(Sender: TObject;
79
      const Rect: TRect; DataCol: Integer; Column: TColumn;
80
      State: TGridDrawState);
81
    procedure DBGridCompositionKeyPress(Sender: TObject; var Key: Char);
82
    procedure CheckBoxPhytaseClick(Sender: TObject);
83
    procedure FormDestroy(Sender: TObject);
84
    procedure DBGridIngredientsDblClick(Sender: TObject);
85
    procedure DBEditPhytaseConcentrationEnter(Sender: TObject);
86
    procedure DBEditPhytaseConcentrationExit(Sender: TObject);
87
    procedure FormShow(Sender: TObject);
88
    procedure ActionRenameExecute(Sender: TObject);
89
    procedure ActionCommentExecute(Sender: TObject);
90
    procedure ActionHelpExecute(Sender: TObject);
91
    procedure ActionOKExecute(Sender: TObject);
92
    procedure ActionCancelExecute(Sender: TObject);
93
    procedure NumEditKeyDown(Sender: TObject; var Key: Word;
94
      Shift: TShiftState);
95
    procedure NumEditKeyPress(Sender: TObject; var Key: Char);
96
  private
97
    { D?clarations priv?es }
98
  public
99
    { D?clarations publiques }
100
  end;
101

    
102
var
103
  FormFeedEdition: TFormFeedEdition;
104

    
105
implementation
106

    
107
{$R *.dfm}
108

    
109
uses
110
  UnitDeclaration, UnitOptions, UnitRename, UnitComment;
111

    
112
var
113
  VK_Separator: boolean;
114

    
115
procedure TFormFeedEdition.ActionCancelExecute(Sender: TObject);
116
begin
117
  ModalResult := mrCancel;
118
end;
119

    
120
procedure TFormFeedEdition.ActionCommentExecute(Sender: TObject);
121
begin
122
  FormComment := TFormComment.Create(nil);
123
  try
124
//    FormComment.Caption := ButtonComment.Hint;
125
    FormComment.Name := DataModuleDeclaration.ClientDataSetFeedsName.Value;
126
    FormComment.OldComment := DataModuleDeclaration.ClientDataSetFeedsDescription.Value;
127
    if (FormComment.ShowModal = mrOk) and (FormComment.NewComment <> FormComment.OldComment)
128
    then // Commenter
129
      with DataModuleDeclaration do
130
      begin
131
        DBUser.BeginTransaction;
132
        try
133
          DBUser.ExecSQL('UPDATE Feeds '
134
            + Format('SET Description = %s ', [QuotedStr(FormComment.NewComment)])
135
            + Format('WHERE Id = %d', [ClientDataSetFeedsId.Value]));
136
          DBUser.Commit;
137
        except
138
          DBUser.RollBack;
139
          MessageDlg(Format(_('Unknown error: %s %s %s'), ['UnitFeedEdition', 'ActionCommentExecute', 'DBUser (UPDATE Feeds)']), mtError, [mbOK], 0);
140
          Exit;
141
        end;
142
        // Commenter dans ClientDataSetFeeds
143
        ClientDataSetFeeds.Edit;
144
        ClientDataSetFeedsDescription.Value := FormComment.NewComment;
145
        ClientDataSetFeeds.Post;
146
      end;
147
  finally
148
    FormComment.Release;
149
  end;
150
end;
151

    
152
procedure TFormFeedEdition.ActionHelpExecute(Sender: TObject);
153
begin
154
  Application.HelpContext(HelpContext);
155
end;
156

    
157
procedure TFormFeedEdition.ActionOKExecute(Sender: TObject);
158
var
159
  total: Double;
160
  USFormatSettings: TFormatSettings;
161
begin
162
  with DataModuleDeclaration do
163
  begin
164
    if CheckBoxPhytase.Checked
165
    then // V?rifier la saisie de la phytase
166
    begin
167
      if ClientDataSetFeedsPhytaseId.IsNull
168
      then
169
      begin
170
        MessageDlg(_('You must select a phytase name.'), mtError, [mbOK], 0);
171
        ActiveControl := DBLookupComboBoxPhytase;
172
        Exit;
173
      end;
174
      if ClientDataSetFeedsPhytaseConcentration.Value = 0
175
      then
176
      begin
177
        MessageDlg(_('You must specify a phytase concentration level.'), mtError, [mbOK], 0);
178
        ActiveControl := DBEditPhytaseConcentration;
179
        Exit;
180
      end;
181
      if ClientDataSetFeedsPhytaseIncorporation.Value = 0
182
      then
183
      begin
184
        MessageDlg(_('You must specify a phytase incorporation level.'), mtError, [mbOK], 0);
185
        ActiveControl := DBEditPhytaseIncorporation;
186
        Exit;
187
      end;
188
    end;
189
    total := 0;
190
    ClientDataSetComposition.DisableControls;
191
    try
192
      ClientDataSetComposition.First;
193
      while not ClientDataSetComposition.Eof do
194
      begin // V?rifier la saisie de chaques mati?res premi?res utilis?es
195
        if ClientDataSetCompositionMS.Value = 0
196
        then
197
        begin
198
          MessageDlg(_('You must specify a dry matter value for this ingredient.'), mtError, [mbOK], 0);
199
          ActiveControl := DBGridComposition;
200
          DBGridComposition.SelectedField := ClientDataSetCompositionMS;
201
          Exit;
202
        end;
203
        if ClientDataSetCompositionLevel.Value = 0
204
        then
205
        begin
206
          MessageDlg(_('You must specify an incorporation rate for this ingredient.'), mtError, [mbOK], 0);
207
          ActiveControl := DBGridComposition;
208
          DBGridComposition.SelectedField := ClientDataSetCompositionLevel;
209
          Exit;
210
        end;
211
        total := total + ClientDataSetCompositionLevel.Value;
212
        ClientDataSetComposition.Next;
213
      end;
214
    finally
215
      ClientDataSetComposition.EnableControls;
216
    end;
217
    if Abs(ClientDataSetFeedsRemain.Value) > 1 / 1000
218
    then
219
      if MessageDlg(_('The sum of ingredient incorporation rates is not equal to 100% of the diet composition.' + sLineBreak + 'Are you sure you want to continue?'), mtError, [mbYes, mbNo], 0) <> mrYes
220
      then
221
      begin
222
        Exit;
223
      end;
224
    FilterOnClass := False; // Mise ? jour le 22/02/2008
225
    // Modifier le r?gime
226
    GetLocaleFormatSettings(1033, USFormatSettings);
227
    DBUser.BeginTransaction;
228
    try
229
      DBUser.ExecSQL('UPDATE Feeds '
230
        + Format('SET Presentation = %d, ', [ComboBoxPresentation.ItemIndex])
231
        + Format('BonusC = %.3f, ', [SpinEditBonusC.Value / 100], USFormatSettings)
232
        + Format('BonusT = %.3f ', [SpinEditBonusT.Value / 100], USFormatSettings)
233
        + Format('WHERE Id = %d', [ClientDataSetFeedsId.Value]));
234
      // Phytase
235
      if CheckBoxPhytase.Checked
236
      then
237
        DBUser.ExecSQL('UPDATE Feeds '
238
          + Format('SET Phytase = %d, ', [ClientDataSetFeedsPhytaseId.Value])
239
          + Format('Concentration = %.0f, ', [ClientDataSetFeedsPhytaseConcentration.Value], USFormatSettings)
240
          + Format('Incorporation = %.0f ', [ClientDataSetFeedsPhytaseIncorporation.Value], USFormatSettings)
241
          + Format('WHERE Id = %d', [ClientDataSetFeedsId.Value]))
242
      else
243
        DBUser.ExecSQL('UPDATE Feeds '
244
          + 'SET Phytase = null, '
245
          + 'Concentration = null, '
246
          + 'Incorporation = null '
247
          + Format('WHERE Id = %d', [ClientDataSetFeedsId.Value]));
248
      DBUser.Commit;
249
    except
250
      DBUser.RollBack;
251
      MessageDlg(Format(_('Unknown error: %s %s %s'), ['UnitFeedEdition', 'ActionOKExecute', 'DBUser (UPDATE Feeds)']), mtError, [mbOK], 0);
252
      Exit;
253
    end;
254
    // Suppression de l'ancienne composition
255
    DBUser.BeginTransaction;
256
    try
257
      DBUser.ExecSQL('DELETE FROM Composition '
258
        + Format('WHERE Feed = %d', [ClientDataSetFeedsId.Value]));
259
      DBUser.Commit;
260
    except
261
      DBUser.RollBack;
262
      MessageDlg(Format(_('Unknown error: %s %s %s'), ['UnitFeedEdition', 'ActionOKExecute', 'DBUser (DELETE FROM Composition)']), mtError, [mbOK], 0);
263
      Exit;
264
    end;
265
    // Enregistrement de la nouvelle composition
266
    ClientDataSetComposition.DisableControls;
267
    try
268
      DBUser.BeginTransaction;
269
      try
270
        ClientDataSetComposition.First;
271
        while not ClientDataSetComposition.Eof do
272
        begin
273
          ClientDataSetCompositionUser.DisplayValues := DefaultTrueBoolStr + ';' + DefaultFalseBoolStr;
274
          DBUser.ExecSQL('INSERT INTO Composition (Feed, Ingredient, User, Rank, MS, Level) VALUES ('
275
            + Format('%d, ', [ClientDataSetFeedsId.Value])
276
            + Format('%d, ', [ClientDataSetCompositionIngredient.Value])
277
            + Format('''%s'', ', [ClientDataSetCompositionUser.AsString])
278
            + Format('%d, ', [ClientDataSetCompositionRank.Value])
279
            + Format('%.15f, ', [InputProximal(ClientDataSetCompositionMS.Value, 1, 1, FormOptions.Proximal)], USFormatSettings)
280
            + Format('%.15f)', [InputIncorporation(ClientDataSetCompositionLevel.Value, FormOptions.Incorporation)], USFormatSettings));
281
          ClientDataSetComposition.Next;
282
        end;
283
        DBUser.Commit;
284
      except
285
        DBUser.RollBack;
286
        MessageDlg(Format(_('Unknown error: %s %s %s'), ['UnitFeedEdition', 'ActionOKExecute', 'DBUser (INSERT INTO Composition)']), mtError, [mbOK], 0);
287
        Exit;
288
      end;
289
    finally
290
      ClientDataSetComposition.EnableControls;
291
    end;
292
    // Calculer les valeurs de ClientDataSetFeeds
293
    TableFeeds := DBUser.GetTable('SELECT * FROM Feeds '
294
      + Format('WHERE Id = %d', [ClientDataSetFeedsId.Value]));
295
    ClientDataSetFeeds.Edit;
296
    try
297
      ClientDataSetFeedsPresentation.Value := ComboBoxPresentation.ItemIndex;
298
      ClientDataSetFeedsBonusC.Value := SpinEditBonusC.Value;
299
      ClientDataSetFeedsBonusT.Value := SpinEditBonusT.Value;
300
      CalcFeed;
301
      ClientDataSetFeeds.Post;
302
    except
303
      ClientDataSetFeeds.Cancel;
304
      MessageDlg(Format(_('Unknown error: %s %s %s'), ['UnitFeedEdition', 'ActionOKExecute', 'ClientDataSetFeeds']), mtError, [mbOK], 0);
305
      Exit;
306
    end;
307
    TableFeeds.Free;
308
  end;
309
  ModalResult := mrOk;
310
end;
311

    
312
procedure TFormFeedEdition.ActionRenameExecute(Sender: TObject);
313
begin
314
  FormRename := TFormRename.Create(nil);
315
  try
316
//    FormRename.Caption := ButtonRename.Hint;
317
    FormRename.OldName := DataModuleDeclaration.ClientDataSetFeedsName.Value;
318
    FormRename.NameList.Assign(DataModuleDeclaration.FeedList);
319
    if (FormRename.ShowModal = mrOk) and (FormRename.NewName <> FormRename.OldName)
320
    then // Renommer
321
      with DataModuleDeclaration do
322
      begin
323
        DBUser.BeginTransaction;
324
        try
325
          DBUser.ExecSQL('UPDATE Feeds '
326
            + Format('SET Name = %s ', [QuotedStr(FormRename.NewName)])
327
            + Format('WHERE Id = %d', [ClientDataSetFeedsId.Value]));
328
          DBUser.Commit;
329
        except
330
          DBUser.RollBack;
331
          MessageDlg(Format(_('Unknown error: %s %s %s'), ['UnitFeedEdition', 'ActionRenameExecute', 'DBUser (UPDATE Feeds)']), mtError, [mbOK], 0);
332
          Exit;
333
        end;
334
        // Renommer dans ClientDataSetFeeds
335
        ClientDataSetFeeds.Edit;
336
        ClientDataSetFeedsName.Value := FormRename.NewName;
337
        ClientDataSetFeeds.Post;
338
        // Modifier le nom dans la liste des r?gimes
339
        FeedList[FeedList.IndexOf(FormRename.OldName)] := FormRename.NewName;
340
      end;
341
  finally
342
    FormRename.Release;
343
  end;
344
end;
345

    
346
procedure TFormFeedEdition.CheckBoxPhytaseClick(Sender: TObject);
347
begin
348
  if CheckBoxPhytase.Checked
349
  then
350
  begin
351
    DBLookupComboBoxPhytase.Enabled := True;
352
    DBEditPhytaseConcentration.Enabled := True;
353
    DBEditPhytaseIncorporation.Enabled := True;
354
    ActiveControl := DBLookupComboBoxPhytase;
355
  end
356
  else
357
  begin
358
    with DataModuleDeclaration do
359
    begin
360
      ClientDataSetFeeds.Edit;
361
      ClientDataSetFeedsPhytaseId.Clear;
362
      ClientDataSetFeedsPhytaseConcentration.Clear;
363
      ClientDataSetFeedsPhytaseIncorporation.Clear;
364
      ClientDataSetFeeds.Post;
365
    end;
366
    DBLookupComboBoxPhytase.Enabled := False;
367
    DBEditPhytaseConcentration.Enabled := False;
368
    DBEditPhytaseIncorporation.Enabled := False;
369
  end;
370
end;
371

    
372
procedure TFormFeedEdition.ComboBoxClassChange(Sender: TObject);
373
begin
374
  with DataModuleDeclaration do
375
  begin
376
    ClientDataSetIngredients.Filtered := False;
377
    FilterOnClass := True;
378
    FilteredClass := ComboBoxClass.ItemIndex;
379
    if FilteredClass <> 0
380
    then
381
      ClientDataSetIngredients.Filtered := True;
382
  end;
383
end;
384

    
385
procedure TFormFeedEdition.DBEditPhytaseConcentrationEnter(Sender: TObject);
386
begin
387
//  DataModuleDeclaration.ClientDataSetFeedsPhytaseConcentration.DisplayFormat := '0';
388
//  DBEditPhytaseConcentration.SelectAll;
389
end;
390

    
391
procedure TFormFeedEdition.DBEditPhytaseConcentrationExit(Sender: TObject);
392
begin
393
//  DataModuleDeclaration.ClientDataSetFeedsPhytaseConcentration.DisplayFormat := '#,###,##0';
394
end;
395

    
396
procedure TFormFeedEdition.DBGridCompositionDrawColumnCell(Sender: TObject;
397
  const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
398
begin
399
  with DBGridComposition do
400
  begin
401
    if Column.FieldName = 'IngredientName'
402
    then // Couleur particuli?re pour indiquer la base d'origine
403
      if gdSelected in State
404
      then // Cellule courante
405
      begin
406
        if DataModuleDeclaration.ClientDataSetCompositionUser.Value
407
        then // User
408
          Canvas.Font.Color := clWebPaleTurquoise
409
        else // InraAfz
410
          Canvas.Font.Color := clWebLightPink;
411
        Canvas.Brush.Color := clWebGray;
412
      end
413
      else // Cellule normale
414
      begin
415
        Canvas.Font.Color := clWebBlack;
416
        if DataModuleDeclaration.ClientDataSetCompositionUser.Value
417
        then // User
418
          Canvas.Brush.Color := clWebPaleTurquoise
419
        else // InraAfz
420
          Canvas.Brush.Color := clWebLightPink;
421
      end
422
    else // Cellule modifiable
423
      if gdSelected in State
424
      then // Cellule courante
425
      begin
426
        Canvas.Font.Color := clWebWhite;
427
        Canvas.Brush.Color := clWebGray;
428
      end
429
      else
430
      begin
431
        Canvas.Font.Color := clWebBlack;
432
        Canvas.Brush.Color := clWebWhite;
433
      end;
434
    DefaultDrawColumnCell(Rect, DataCol, Column, State);
435
  end;
436
end;
437

    
438
procedure TFormFeedEdition.DBGridCompositionKeyPress(Sender: TObject; var Key: Char);
439
begin
440
  if Key = AnsiChar(#13)
441
  then
442
  begin
443
    if DBGridComposition.EditorMode
444
    then
445
    begin
446
      DBGridComposition.EditorMode := False;
447
      with DataModuleDeclaration.ClientDataSetComposition do
448
        if Modified
449
        then
450
          Post;
451
    end;
452
    Key := AnsiChar(#0);
453
  end
454
  else
455
    NumEditKeyPress(Sender, Key);
456
end;
457

    
458
procedure TFormFeedEdition.DBGridIngredientsDblClick(Sender: TObject);
459
begin
460
  SpeedButtonAppendClick(nil);
461
end;
462

    
463
procedure TFormFeedEdition.DBGridIngredientsDrawColumnCell(Sender: TObject;
464
  const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
465
begin
466
  with DBGridIngredients do
467
  begin
468
    if Column.FieldName = DataModuleDeclaration.ClientDataSetIngredients.IndexFieldNames
469
    then // Colonne tri?e
470
      Canvas.Font.Style := Canvas.Font.Style + [fsBold];
471
    if Column.FieldName = 'Name'
472
    then // Couleur particuli?re pour indiquer la base d'origine
473
      if gdSelected in State
474
      then // Cellule courante
475
      begin
476
        if DataModuleDeclaration.ClientDataSetIngredientsUser.Value
477
        then // User
478
          Canvas.Font.Color := clWebPaleTurquoise
479
        else // InraAfz
480
          Canvas.Font.Color := clWebLightPink;
481
        Canvas.Brush.Color := clWebGray;
482
      end
483
      else // Cellule normale
484
      begin
485
        Canvas.Font.Color := clWebBlack;
486
        if DataModuleDeclaration.ClientDataSetIngredientsUser.Value
487
        then // User
488
          Canvas.Brush.Color := clWebPaleTurquoise
489
        else // InraAfz
490
          Canvas.Brush.Color := clWebLightPink;
491
      end
492
    else // Valeurs
493
      if gdSelected in State
494
      then // Cellule courante
495
      begin
496
        Canvas.Font.Color := clWebWhite;
497
        Canvas.Brush.Color := clWebGray;
498
      end
499
      else
500
      begin
501
        Canvas.Font.Color := clWebBlack;
502
        Canvas.Brush.Color := clWebLightGrey;
503
      end;
504
    DefaultDrawColumnCell(Rect, DataCol, Column, State);
505
  end;
506
end;
507

    
508
procedure TFormFeedEdition.DBGridIngredientsTitleClick(Column: TColumn);
509
begin
510
  with DataModuleDeclaration.ClientDataSetIngredients do
511
    if IndexFieldNames = Column.FieldName
512
    then // C'est d?j? l'index => tri selon le num?ro
513
      IndexFieldNames := 'Name'
514
    else
515
      IndexFieldNames := Column.FieldName;
516
end;
517

    
518
procedure TFormFeedEdition.FormCreate(Sender: TObject);
519
begin
520
  if Screen.Fonts.IndexOf('Arial Unicode MS') <> -1
521
  then
522
    Font.Name := 'Arial Unicode MS';
523
//  PanelButtons.Font.Size := PanelButtons.Font.Size + 2;
524
  PanelButtons.Font.Style := [fsBold];
525
  LabelName.Font.Style := [fsBold];
526
  DBEditName.Font.Style := [fsBold];
527
  LabelWarning.Font.Color := clRed;
528
  TranslateComponent(Self);
529
  with DataModuleDeclaration do
530
  begin
531
    // Classes
532
    ComboBoxClass.Items.Add(_('(All)'));
533
    ClientDataSetClasses.First;
534
    while not ClientDataSetClasses.Eof do
535
    begin
536
      ComboBoxClass.Items.Add(ClientDataSetClassesName.Value);
537
      ClientDataSetClasses.Next;
538
    end;
539
    ComboBoxClass.ItemIndex := 0; // (All) par d?faut
540
    // Composition
541
    ClientDataSetComposition.Filter := Format('Feed = %d', [ClientDataSetFeedsId.Value]);
542
    ClientDataSetComposition.Filtered := True;
543
    ClientDataSetComposition.First;
544
  end;
545
  with FormOptions do
546
  begin
547
    DBGridComposition.Columns[1].Title.Caption := Format('%s (%s)', [DBGridComposition.Columns[1].Title.Caption, ComboBoxProximal.Items[Proximal]]);
548
    DBGridComposition.Columns[2].Title.Caption := Format('%s (%s)', [DBGridComposition.Columns[2].Title.Caption, ComboBoxIncorporation.Items[Incorporation]]);
549
  end;
550
  GroupBoxBonus.Caption := GroupBoxBonus.Caption + ' (%)';
551
end;
552

    
553
procedure TFormFeedEdition.FormShow(Sender: TObject);
554
begin
555
  with DataModuleDeclaration do
556
  begin
557
    ComboBoxPresentation.ItemIndex := ClientDataSetFeedsPresentation.Value;
558
    SpinEditBonusC.Value := ClientDataSetFeedsBonusC.Value;
559
    SpinEditBonusT.Value := ClientDataSetFeedsBonusT.Value;
560
    // Phytase
561
    if ClientDataSetFeedsPhytaseId.IsNull
562
    then
563
      CheckBoxPhytase.Checked := False;
564
    // Taux d'incorporation total
565
    ClientDataSetFeeds.Edit;
566
    if VarIsEmpty(ClientDataSetComposition.Aggregates[0].Value)
567
    or VarIsNull(ClientDataSetComposition.Aggregates[0].Value)
568
    then
569
      ClientDataSetFeedsLevelSum.Clear
570
    else
571
      ClientDataSetFeedsLevelSum.Value := ClientDataSetComposition.Aggregates[0].Value;
572
    ClientDataSetFeeds.Post;
573
  end;
574
  with DBGridComposition do
575
    Columns[0].Width := Width - Columns[1].Width - Columns[2].Width - 28;
576
end;
577

    
578
procedure TFormFeedEdition.FormDestroy(Sender: TObject);
579
begin
580
  with DataModuleDeclaration do
581
  begin
582
    ClientDataSetIngredients.Filtered := False;
583
    ClientDataSetIngredients.Filter := '';
584
    FilterOnClass := False;
585
    ClientDataSetComposition.Filtered := False;
586
    ClientDataSetComposition.Filter := '';
587
  end;
588
end;
589

    
590
procedure TFormFeedEdition.SpeedButtonAppendClick(Sender: TObject);
591
begin
592
  with DataModuleDeclaration do
593
    if ClientDataSetIngredients.RecordCount > 0
594
    then
595
    begin
596
      ClientDataSetComposition.Append;
597
      ClientDataSetCompositionFeed.Value := ClientDataSetFeedsId.Value;
598
      ClientDataSetCompositionRank.Value := ClientDataSetComposition.RecordCount + 1;
599
      ClientDataSetCompositionIngredient.Value := ClientDataSetIngredientsId.Value;
600
      ClientDataSetCompositionUser.Value := ClientDataSetIngredientsUser.Value;
601
      ClientDataSetCompositionIngredientName.Value := ClientDataSetIngredientsName.Value;
602
      ClientDataSetCompositionMS.Value := ClientDataSetIngredientsMS.Value;
603
      ClientDataSetCompositionLevel.Value := 0;
604
      ClientDataSetComposition.Post;
605
      ActiveControl := DBGridComposition;
606
      DBGridComposition.SelectedField := ClientDataSetCompositionLevel;
607
    end;
608
end;
609

    
610
procedure TFormFeedEdition.SpeedButtonDeleteClick(Sender: TObject);
611
var
612
  Distance: Integer;
613
begin
614
  with DataModuleDeclaration do
615
    if ClientDataSetComposition.RecordCount > 0
616
    then
617
    begin
618
      ClientDataSetComposition.DisableControls;
619
      ClientDataSetComposition.Delete;
620
      // Recalculer l'agr?gat
621
      AggregateLevelSumUpdate(ClientDataSetComposition.Aggregates[0]);
622
      Distance := ClientDataSetComposition.RecNo - ClientDataSetComposition.RecordCount;
623
      while not ClientDataSetComposition.Eof do
624
      begin
625
        ClientDataSetComposition.Edit;
626
        ClientDataSetCompositionRank.Value := ClientDataSetCompositionRank.Value - 1;
627
        ClientDataSetComposition.Post;
628
        ClientDataSetComposition.Next;
629
      end;
630
      // Res?lectionner l'?l?ment courant
631
      if Distance < 0 then ClientDataSetComposition.MoveBy(Distance);
632
      ClientDataSetComposition.EnableControls;
633
    end;
634
end;
635

    
636
procedure TFormFeedEdition.SpeedButtonDownClick(Sender: TObject);
637
var
638
  Position: Integer;
639
begin
640
  with DataModuleDeclaration do
641
    if ClientDataSetComposition.RecNo < ClientDataSetComposition.RecordCount
642
    then
643
    begin
644
      ClientDataSetComposition.DisableControls;
645
      Position := ClientDataSetComposition.RecNo;
646
      // D?caler les ?l?ments suivant l'?l?ment courant
647
      ClientDataSetComposition.Last;
648
      while ClientDataSetComposition.RecNo > Position do
649
      begin
650
        ClientDataSetComposition.Edit;
651
        ClientDataSetCompositionRank.Value := ClientDataSetCompositionRank.Value + 1;
652
        ClientDataSetComposition.Post;
653
        ClientDataSetComposition.Prior;
654
      end;
655
      // L'?l?ment courant deviendra le suivant
656
      ClientDataSetComposition.Edit;
657
      ClientDataSetCompositionRank.Value := ClientDataSetCompositionRank.Value + 1;
658
      ClientDataSetComposition.Post;
659
      // L'?l?ment suivant devient le pr?c?dent
660
      ClientDataSetComposition.Next;
661
      ClientDataSetComposition.Edit;
662
      ClientDataSetCompositionRank.Value := ClientDataSetCompositionRank.Value - 2;
663
      ClientDataSetComposition.Post;
664
      // Ravancer les ?l?ments d?cal?s
665
      ClientDataSetComposition.Last;
666
      while ClientDataSetComposition.RecNo > Position + 1 do
667
      begin
668
        ClientDataSetComposition.Edit;
669
        ClientDataSetCompositionRank.Value := ClientDataSetCompositionRank.Value - 1;
670
        ClientDataSetComposition.Post;
671
        ClientDataSetComposition.Prior;
672
      end;
673
      ClientDataSetComposition.EnableControls;
674
    end;
675
end;
676

    
677
procedure TFormFeedEdition.SpeedButtonLevelClick(Sender: TObject);
678
var
679
  Position, Distance: Integer;
680
begin
681
  with DataModuleDeclaration do
682
  begin
683
    Distance := 0;
684
    Position := ClientDataSetCompositionRank.Value;
685
    ClientDataSetComposition.DisableControls;
686
    ClientDataSetComposition.AggregatesActive := False;
687
    ClientDataSetComposition.IndexName := 'IndexLevel';
688
    ClientDataSetComposition.First;
689
    while not ClientDataSetComposition.Eof do
690
    begin
691
      if ClientDataSetCompositionRank.Value = Position
692
      then // Enregistrement courant
693
        Distance := ClientDataSetComposition.RecNo - ClientDataSetComposition.RecordCount;
694
      ClientDataSetComposition.Edit;
695
      ClientDataSetCompositionRank.Value := ClientDataSetComposition.RecNo;
696
      ClientDataSetComposition.Post;
697
      ClientDataSetComposition.Next;
698
    end;
699
    // Res?lectionner l'?l?ment courant
700
    if Distance < 0 then ClientDataSetComposition.MoveBy(Distance);
701
    ClientDataSetComposition.IndexName := 'IndexRank';
702
    ClientDataSetComposition.AggregatesActive := True;
703
    ClientDataSetComposition.EnableControls;
704
  end;
705
end;
706

    
707
procedure TFormFeedEdition.SpeedButtonNameClick(Sender: TObject);
708
var
709
  Position, Distance: Integer;
710
begin
711
  with DataModuleDeclaration do
712
  begin
713
    Distance := 0;
714
    Position := ClientDataSetCompositionRank.Value;
715
    ClientDataSetComposition.DisableControls;
716
    ClientDataSetComposition.AggregatesActive := False;
717
    ClientDataSetComposition.IndexName := 'IndexIngredientName';
718
    ClientDataSetComposition.First;
719
    while not ClientDataSetComposition.Eof do
720
    begin
721
      if ClientDataSetCompositionRank.Value = Position
722
      then // Enregistrement courant
723
        Distance := ClientDataSetComposition.RecNo - ClientDataSetComposition.RecordCount;
724
      ClientDataSetComposition.Edit;
725
      ClientDataSetCompositionRank.Value := ClientDataSetComposition.RecNo;
726
      ClientDataSetComposition.Post;
727
      ClientDataSetComposition.Next;
728
    end;
729
    // Res?lectionner l'?l?ment courant
730
    if Distance < 0 then ClientDataSetComposition.MoveBy(Distance);
731
    ClientDataSetComposition.IndexName := 'IndexRank';
732
    ClientDataSetComposition.AggregatesActive := True;
733
    ClientDataSetComposition.EnableControls;
734
  end;
735
end;
736

    
737
procedure TFormFeedEdition.SpeedButtonReplaceClick(Sender: TObject);
738
begin
739
  with DataModuleDeclaration do
740
    if (ClientDataSetIngredients.RecordCount > 0) and (ClientDataSetComposition.RecordCount > 0)
741
    then
742
    begin
743
      ClientDataSetComposition.Edit;
744
      ClientDataSetCompositionUser.Value := ClientDataSetIngredientsUser.Value;
745
      ClientDataSetCompositionIngredient.Value := ClientDataSetIngredientsId.Value;
746
      ClientDataSetCompositionIngredientName.Value := ClientDataSetIngredientsName.Value;
747
      ClientDataSetCompositionMS.Value := ClientDataSetIngredientsMS.Value;
748
      ClientDataSetComposition.Post;
749
      ActiveControl := DBGridComposition;
750
      DBGridComposition.SelectedField := ClientDataSetCompositionMS;
751
    end;
752
end;
753

    
754
procedure TFormFeedEdition.SpeedButtonUpClick(Sender: TObject);
755
var
756
  Position: Integer;
757
begin
758
  with DataModuleDeclaration do
759
    if ClientDataSetComposition.RecNo > 1
760
    then
761
    begin
762
      ClientDataSetComposition.DisableControls;
763
      Position := ClientDataSetComposition.RecNo;
764
      // D?caler les ?l?ments ? partir de l'?l?ment courant
765
      ClientDataSetComposition.Last;
766
      while ClientDataSetComposition.RecNo > Position - 1 do
767
      begin
768
        ClientDataSetComposition.Edit;
769
        ClientDataSetCompositionRank.Value := ClientDataSetCompositionRank.Value + 1;
770
        ClientDataSetComposition.Post;
771
        ClientDataSetComposition.Prior;
772
      end;
773
      // L'?l?ment pr?c?dent deviendra le suivant
774
      ClientDataSetComposition.Edit;
775
      ClientDataSetCompositionRank.Value := ClientDataSetCompositionRank.Value + 1;
776
      ClientDataSetComposition.Post;
777
      // L'?l?ment courant devient le pr?c?dent
778
      ClientDataSetComposition.Next;
779
      ClientDataSetComposition.Edit;
780
      ClientDataSetCompositionRank.Value := ClientDataSetCompositionRank.Value - 2;
781
      ClientDataSetComposition.Post;
782
      // Ravancer les ?l?ments d?cal?s
783
      ClientDataSetComposition.Last;
784
      while ClientDataSetComposition.RecNo > Position do
785
      begin
786
        ClientDataSetComposition.Edit;
787
        ClientDataSetCompositionRank.Value := ClientDataSetCompositionRank.Value - 1;
788
        ClientDataSetComposition.Post;
789
        ClientDataSetComposition.Prior;
790
      end;
791
      // Res?lectionner l'?l?ment courant (Position - 1)
792
      ClientDataSetComposition.Prior;
793
      ClientDataSetComposition.EnableControls;
794
    end;
795
end;
796

    
797
procedure TFormFeedEdition.NumEditKeyDown(Sender: TObject;
798
  var Key: Word; Shift: TShiftState);
799
begin
800
  // D?tection de l'appui sur le s?parateur d?cimal du pav? num?rique
801
  VK_Separator := (Key = VK_DECIMAL);
802
end;
803

    
804
procedure TFormFeedEdition.NumEditKeyPress(Sender: TObject;
805
  var Key: Char);
806
begin
807
  if VK_Separator then
808
    // Renvoyer le s?parateur d?cimal des options r?gionales et linguistiques
809
    Key := DecimalSeparator;
810
end;
811

    
812
end.