unit U_MakeEditObjCurrency;

interface

uses
  Windows, U_LNG, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, RzCmboBx, RzRadChk, siComp, siLngLnk, RzButton,
  RzEdit, Mask, RzSpnEdt, ExtCtrls, RzPanel,

  U_BaseCommon, U_BaseConstants, Buttons;

type
  TF_MakeEditObjCurrency = class(TForm)
    RzGroupBox1: TRzGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    seKolvo: TRzSpinEdit;
    neRatio: TRzNumericEdit;
    RzGroupBox2: TRzGroupBox;
    btOk: TRzBitBtn;
    btCancel: TRzBitBtn;
    lng_Forms: TsiLangLinked;
    RzGroupBox3: TRzGroupBox;
    rbSimple: TRzRadioButton;
    rbMain: TRzRadioButton;
    rbSecond: TRzRadioButton;
    cbName: TRzComboBox;
    cbNameBrief: TRzComboBox;
    btChoiceCurrencyName: TSpeedButton;
    btChoiceCurrencyNameBrief: TSpeedButton;
    procedure btChoiceCurrencyNameClick(Sender: TObject);
    procedure btChoiceCurrencyNameBriefClick(Sender: TObject);
    procedure cbNameChange(Sender: TObject);
    procedure cbNameBriefChange(Sender: TObject);
    procedure btOkClick(Sender: TObject);
    procedure neRatioChange(Sender: TObject);
  private
    GForm: TForm;
    FFormMode: TFormMode;
    FChangedRatio: Boolean;

    procedure ChangeComboItemByOtherCombo(ATrgCombo, AOtherCombo: TRzComboBox);
    procedure LoadCurrencyFromForm(ATrgCombo, AOtherCombo: TRzComboBox);
  public
    Constructor Create(AOwner: TComponent; AForm: TForm);
    Destructor  Destroy; override;

    function Execute(AMakeEdit: TMakeEdit; AObjectCurrency: PObjectCurrencyRel): Boolean;
  end;

//var
//  F_MakeEditObjCurrency: TF_MakeEditObjCurrency;

implementation
Uses Unit_DM_SCS, U_Main;
{$R *.dfm}

{ TForm1 }

constructor TF_MakeEditObjCurrency.Create(AOwner: TComponent; AForm: TForm);
begin
  GForm := AForm;
  inherited Create(AOwner);
  // Tolik 01/11/2019 --
  seKolvo.Max := 10000000000;
  seKolvo.Min := 0;
  //
end;

destructor TF_MakeEditObjCurrency.Destroy;
begin
  inherited;
end;

function TF_MakeEditObjCurrency.Execute(AMakeEdit: TMakeEdit; AObjectCurrency: PObjectCurrencyRel): Boolean;
begin
  Result := false;

  Caption := '';
  rbSimple.Enabled := true;
  rbSimple.Checked := false;
  rbMain.Enabled := true;
  rbMain.Checked := false;
  rbSecond.Enabled := true;
  rbSecond.Checked := false;

  FFormMode := fmMake;
  FChangedRatio := false;
  case AMakeEdit of
    meMake:
      begin
        FFormMode := fmMake;
        Caption := cMakeEditObjCurrency_Msg1_1;

        FillComboBoxRz(cbName, F_NormBase, true, tnCurrency, fnID, fnName, '', -1);
        FillComboBoxRz(cbNameBrief, F_NormBase, true, tnCurrency, fnID, fnNameBrief, '', -1);

        seKolvo.Value := 100;
        neRatio.Value := 0.1;
        rbSimple.Checked := true;

        FChangedRatio := true;
      end;
    meEdit:
      begin
        FFormMode := fmEdit;

        Caption := cMakeEditObjCurrency_Msg1_2;

        FillComboBoxRz(cbName, F_NormBase, false, tnCurrency, fnID, fnName, '', AObjectCurrency.IDCurrency);
        FillComboBoxRz(cbNameBrief, F_NormBase, false, tnCurrency, fnID, fnNameBrief, '', AObjectCurrency.IDCurrency);

        seKolvo.Value := AObjectCurrency.Data.Kolvo;

        neRatio.OnChange := nil;
        try
          neRatio.Value := Round3(AObjectCurrency.Data.Ratio);
        finally
          neRatio.OnChange := neRatioChange;
        end;
        if AObjectCurrency.Data.Main = ctSimple then
          rbSimple.Checked := true
        else
        if AObjectCurrency.Data.Main = ctMain then
        begin
          rbMain.Checked := true;
          rbSimple.Enabled := false;
          //rbSecond.Enabled := false;
        end
        else
        if AObjectCurrency.Data.Main = ctSecond then
        begin
          rbSecond.Checked := true;
          //rbMain.Enabled := false;
          rbSimple.Enabled := false;
        end;
      end;
  end;

  if ShowModal = mrOk then
  begin
    Result := true;
    AObjectCurrency.IDCurrency := GetIDFromComboBoxRz(cbName);
    AObjectCurrency.Data.Name := cbName.Text;
    AObjectCurrency.Data.NameBrief := cbNameBrief.Text;
    AObjectCurrency.Data.Kolvo := Trunc(seKolvo.Value);
    if FChangedRatio then
      AObjectCurrency.Data.Ratio := Round3(neRatio.Value);
    if rbSimple.Checked then
      AObjectCurrency.Data.Main := ctSimple
    else
    if rbMain.Checked then
      AObjectCurrency.Data.Main := ctMain
    else
    if rbSecond.Checked then
      AObjectCurrency.Data.Main := ctSecond;

    Result := true;
  end;
end;

procedure TF_MakeEditObjCurrency.ChangeComboItemByOtherCombo(ATrgCombo, AOtherCombo: TRzComboBox);
var
  SavedOnChange: TNotifyEvent;
begin
  try
    SavedOnChange := ATrgCombo.OnChange;
    try
      ATrgCombo.OnChange := nil;
      SelectItemByIDinComboRz(ATrgCombo, GetIDFromComboBoxRz(AOtherCombo));
    finally
      ATrgCombo.OnChange := SavedOnChange;
    end;
  except
    on E: Exception do AddExceptionToLogEx('TF_MakeEditObjCurrency.ChangeComboItemByOtherCombo', E.Message);
  end;
end;

procedure TF_MakeEditObjCurrency.LoadCurrencyFromForm(ATrgCombo, AOtherCombo: TRzComboBox);
var
  IDTrgCombo: Integer;
  CanEmptyLine: Boolean;
begin
  try
    CanEmptyLine := (FFormMode = fmMake);

    IDTrgCombo := F_NormBase.DM.GetCurrencyIDFromGuide(GetIDFromComboBoxRz(ATrgCombo), FFormMode);

    //*** Если IDTrgCombo = -1, то запись останется на прежней позиции
    FillComboBoxRz(ATrgCombo, F_NormBase, CanEmptyLine, tnCurrency, fnID, fnName, '', IDTrgCombo);
    FillComboBoxRz(AOtherCombo, F_NormBase, CanEmptyLine, tnCurrency, fnID, fnNameBrief, '', IDTrgCombo);
  except
    on E: Exception do AddExceptionToLog('TF_MakeEditObjCurrency.LoadCurrencyFromForm: '+E.Message);
  end;
end;

procedure TF_MakeEditObjCurrency.btChoiceCurrencyNameClick(
  Sender: TObject);
begin
  LoadCurrencyFromForm(cbName, cbNameBrief);
end;

procedure TF_MakeEditObjCurrency.btChoiceCurrencyNameBriefClick(
  Sender: TObject);
begin
  LoadCurrencyFromForm(cbNameBrief, cbName);
end;

procedure TF_MakeEditObjCurrency.cbNameChange(Sender: TObject);
begin
  ChangeComboItemByOtherCombo(cbNameBrief, cbName);
end;

procedure TF_MakeEditObjCurrency.cbNameBriefChange(Sender: TObject);
begin
  ChangeComboItemByOtherCombo(cbName, cbNameBrief);
end;

procedure TF_MakeEditObjCurrency.btOkClick(Sender: TObject);
begin
  if GetIDFromComboBoxRz(cbName) = 0 then
  begin
    ModalResult := mrNone;
    MessageModal(cMakeEditObjCurrency_Msg2, Caption, MB_ICONINFORMATION or MB_OK);
  end;
end;

procedure TF_MakeEditObjCurrency.neRatioChange(Sender: TObject);
begin
  FChangedRatio := true;
end;

end.
