unit U_MakeEditSupplyKind;

interface

uses
  Windows, U_LNG, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,
  U_BaseCommon, U_BaseConstants, RzButton, ExtCtrls, RzPanel, StdCtrls, RzEdit, Mask, kbmMemTable,
  siComp, siLngLnk;

type
  TF_MakeEditSupplyKind = class(TForm)
    RzGroupBox1: TRzGroupBox;
    RzGroupBox2: TRzGroupBox;
    btOk: TRzBitBtn;
    btCancel: TRzBitBtn;
    lng_Forms: TsiLangLinked;
    RzGroupBox3: TRzGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    edName: TRzEdit;
    edIzm: TRzEdit;
    neUnitKolvo: TRzNumericEdit;
    RzGroupBox4: TRzGroupBox;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    edNameTradUOM: TRzEdit;
    edIzmTradUOM: TRzEdit;
    neUnitKolvoTradUOM: TRzNumericEdit;
    procedure FormCreate(Sender: TObject);
    procedure RzGroupBox2Resize(Sender: TObject);
  private
    GForm: TForm;
  public
    NewID: Integer;

    constructor Create(AOwner: TComponent; AForm: TForm);
    destructor  Destroy; override;

    function Execute(AMakeEdit: TMakeEdit; AMemTable: TkbmMemTable): Boolean;
  end;

//var
//  F_MakeEditSupplyKind: TF_MakeEditSupplyKind;

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

{ TF_MakeEditSupplyKind }

constructor TF_MakeEditSupplyKind.Create(AOwner: TComponent; AForm: TForm);
begin
  GForm := AForm;
  inherited Create(AOwner);
end;

destructor TF_MakeEditSupplyKind.Destroy;
begin

  inherited;
end;

function TF_MakeEditSupplyKind.Execute(AMakeEdit: TMakeEdit; AMemTable: TkbmMemTable): Boolean;
var
  SupplyKindID: Integer;
  SuppliesKind: TSuppliesKind;
begin
  Result := false;
  try
    Caption := '';
    NewID := 0;
    SupplyKindID := 0;
    case AMakeEdit of
      meMake:
        begin
          Caption := cMakeEditSupplyKind_Msg1_1;
          edName.Text := '';
          edIzm.Text := '';
          neUnitKolvo.Value := 1;
        end;
      meEdit:
        begin
          Caption := cMakeEditSupplyKind_Msg1_2;
          if AMemTable <> nil then
          begin 
            SupplyKindID := AMemTable.FieldByName(fnID).AsInteger;
            edName.Text := AMemTable.FieldByName(fnName).AsString;
            edNameTradUOM.Text := AMemTable.FieldByName(fnNameTradUOM).AsString;
            edIzm.Text :=  AMemTable.FieldByName(fnIzm).AsString;
            edIzmTradUOM.Text :=  AMemTable.FieldByName(fnIzmTradUOM).AsString;
            neUnitKolvo.Value :=  RoundCP(AMemTable.FieldByName(fnUnitKolvo).AsFloat);
            neUnitKolvoTradUOM.Value :=  RoundCP(AMemTable.FieldByName(fnUnitKolvoTradUOM).AsFloat);
          end
          else
          with TF_Main(GForm).DM do
          begin
            SupplyKindID := DataSet_SuppliesKinds.FN(fnID).AsInteger;
            edName.Text := DataSet_SuppliesKinds.FN(fnName).AsString;
            edNameTradUOM.Text := DataSet_SuppliesKinds.FN(fnNameTradUOM).AsString;
            edIzm.Text :=  DataSet_SuppliesKinds.FN(fnIzm).AsString;
            edIzmTradUOM.Text :=  DataSet_SuppliesKinds.FN(fnIzmTradUOM).AsString;
            neUnitKolvo.Value :=  RoundCP(DataSet_SuppliesKinds.FN(fnUnitKolvo).AsFloat);
            neUnitKolvoTradUOM.Value :=  RoundCP(DataSet_SuppliesKinds.FN(fnUnitKolvoTradUOM).AsFloat);
          end;
        end;
    end;

    if ShowModal = mrOk then
    begin
      Result := true;
      if AMemTable <> nil then
      begin
        case AMakeEdit of
          meMake:
            AMemTable.Append;
          meEdit:
            AMemTable.Edit;
        end;
        if AMemTable.State <> dsBrowse then
        begin
          AMemTable.FieldByName(fnName).AsString := edName.Text;
          AMemTable.FieldByName(fnNameTradUOM).AsString := edNameTradUOM.Text;
          AMemTable.FieldByName(fnIzm).AsString := edIzm.Text;
          AMemTable.FieldByName(fnIzmTradUOM).AsString := edIzmTradUOM.Text;
          AMemTable.FieldByName(fnUnitKolvo).AsFloat := RoundCP(neUnitKolvo.Value);
          AMemTable.FieldByName(fnUnitKolvoTradUOM).AsFloat := RoundCP(neUnitKolvoTradUOM.Value);
          AMemTable.FieldByName(fnIsModified).AsBoolean := true;
          AMemTable.Post;
          if AMakeEdit = meMake then
            NewID := AMemTable.FieldByName(fnID).AsInteger;
        end;
      end
      else
      with TF_Main(GForm).DM do
      begin
        ZeroMemory(@SuppliesKind, SizeOf(TSuppliesKind));
        SuppliesKind.ID := SupplyKindID;
        SuppliesKind.Name := edName.Text;
        SuppliesKind.NameTradUOM := edNameTradUOM.Text;
        SuppliesKind.Izm := edIzm.Text;
        SuppliesKind.IzmTradUOM := edIzmTradUOM.Text;
        SuppliesKind.UnitKolvo := RoundCP(neUnitKolvo.Value);
        SuppliesKind.UnitKolvoTradUOM := RoundCP(neUnitKolvoTradUOM.Value);
        SaveSuppliesKind(AMakeEdit, @SuppliesKind);
        if AMakeEdit = meMake then
          NewID := SuppliesKind.ID
        else
        if AMakeEdit = meEdit then
          DataSet_SuppliesKinds.FullRefresh;

        {case AMakeEdit of
          meMake:
            DataSet_SuppliesKinds.Append;
          meEdit:
            DataSet_SuppliesKinds.Edit;
        end;
        if DataSet_SuppliesKinds.State <> dsBrowse then
        begin
          DataSet_SuppliesKinds.FN(fnName).AsString := edName.Text;
          DataSet_SuppliesKinds.FN(fnIzm).AsString := edIzm.Text;
          DataSet_SuppliesKinds.FN(fnUnitKolvo).AsFloat := RoundCP(neUnitKolvo.Value);
          DataSet_SuppliesKinds.Post;
          if AMakeEdit = meMake then
            NewID := DataSet_SuppliesKinds.FN(fnID).AsInteger;
        end;}
      end;
    end;
  except
    on E: Exception do AddExceptionToLog('TF_MakeEditSupplyKind.Execute: '+E.Message);
  end;
end;

procedure TF_MakeEditSupplyKind.FormCreate(Sender: TObject);
begin
  neUnitKolvo.DisplayFormat := GetDisplayFormatForFloat;
  neUnitKolvoTradUOM.DisplayFormat := GetDisplayFormatForFloat;
end;

procedure TF_MakeEditSupplyKind.RzGroupBox2Resize(Sender: TObject);
begin
  SetMiddleControlChilds(TControl(Sender), TControl(Self));
end;

end.
