unit U_MakeEditProducer;

interface

uses
  Windows, U_LNG, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, RzPanel, RzButton, StdCtrls, RzEdit, Mask,

  U_BaseCommon, U_BaseConstants, kbmMemTable, siComp, siLngLnk;

type
  TF_MakeEditProducer = class(TForm)
    RzGroupBox1: TRzGroupBox;
    RzGroupBox2: TRzGroupBox;
    btOk: TRzBitBtn;
    btCancel: TRzBitBtn;
    edName: TRzEdit;
    meDescription: TRzMemo;
    Label1: TLabel;
    Label2: TLabel;
    lng_Forms: TsiLangLinked;
  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_MakeEditProducer: TF_MakeEditProducer;

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

{ TF_MakeEditProducer }

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

destructor TF_MakeEditProducer.Destroy;
begin

  inherited;
end;

function TF_MakeEditProducer.Execute(AMakeEdit: TMakeEdit; AMemTable: TkbmMemTable): Boolean;
var
  Producer: TProducer;
  ProducerID: Integer;
begin
  Result := false;
  try
    Caption := '';
    NewID := 0;
    ProducerID := 0;
    case AMakeEdit of
      meMake:
        begin
          Caption := cMakeEditProducer_Msg1_1;
          edName.Text := '';
          meDescription.Lines.Clear;
        end;
      meEdit:
        begin
          Caption := cMakeEditProducer_Msg1_2;
          if AMemTable <> nil then
          begin
            ProducerID := AMemTable.FieldByName(fnID).AsInteger;
            edName.Text := AMemTable.FieldByName(fnName).AsString;
            meDescription.Lines.Text := AMemTable.FieldByName(fnDescription).AsString;
          end
          else
          with TF_Main(GForm).DM do
          begin
            ProducerID := DataSet_PRODUCERS.FN(fnID).AsInteger;
            edName.Text := DataSet_PRODUCERS.FN(fnName).AsString;
            meDescription.Lines.Text := DataSet_PRODUCERS.FN(fnDescription).AsString;
          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(fnDescription).AsString := meDescription.Lines.Text;
          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(@Producer, SizeOf(TProducer));
        Producer.ID := ProducerID;
        Producer.Name := edName.Text;
        Producer.Description := meDescription.Lines.Text;

        SaveProducer(AMakeEdit, @Producer);
        if AMakeEdit = meMake then
          NewID := Producer.ID
        else
        if AMakeEdit = meEdit then
          DataSet_PRODUCERS.ReopenLocate(fnID); //DataSet_PRODUCERS.FullRefresh;


        {case AMakeEdit of
          meMake:
            DataSet_PRODUCERS.Append;
          meEdit:
            DataSet_PRODUCERS.Edit;
        end;
        if DataSet_PRODUCERS.State <> dsBrowse then
        begin
          DataSet_PRODUCERS.FN(fnName).AsString := edName.Text;
          DataSet_PRODUCERS.FN(fnDescription).AsString := meDescription.Lines.Text;
          DataSet_PRODUCERS.Post;
          if AMakeEdit = meMake then
            NewID := DataSet_PRODUCERS.FN(fnID).AsInteger;
        end;}
      end;
    end;
  except
    on E: Exception do AddExceptionToLog('TF_MakeEditProducer.Execute: '+E.Message);
  end;
end;

end.
