unit U_ZdyAttInputGYS_ZXX;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit, DB, ADODB,
  cxInplaceContainer, cxDBTL, cxControls, cxTLData, ComCtrls, ToolWin,
  StdCtrls, cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, DBClient,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ExtCtrls,
  cxSplitter, cxGridLevel, cxClasses, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP,ShellAPI,IniFiles, cxCheckBox, cxCalendar,
  cxButtonEdit, cxTextEdit, cxDropDownEdit, cxPC;

type
  TfrmZdyAttInputGYS_ZXX = class(TForm)
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    DS_HZ2: TDataSource;
    CDS_HZ2: TClientDataSet;
    ADOQueryMain2: TADOQuery;
    cxGridPopupMenu3: TcxGridPopupMenu;
    DS_HZ3: TDataSource;
    ADOQueryMain3: TADOQuery;
    CDS_HZ3: TClientDataSet;
    Panel2: TPanel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label10: TLabel;
    GYSName: TEdit;
    GYSFZRen: TEdit;
    GYSNameZC: TEdit;
    GYSTel: TEdit;
    GYSCodeFW: TMemo;
    Note: TMemo;
    GYSAdd: TMemo;
    GYSNo: TEdit;
    ToolBar1: TToolBar;
    ToolButton3: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label2: TLabel;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
    function  SaveData():Boolean;
    function  SaveData2():Boolean;
    procedure SSetsaveSqlNewTClient(ADOQueryCmd:TClientDataSet;
                                       Myparent:TWinControl;MyTag:integer);
    procedure SCSHDataNewTclient(ADOQueryTmp:TClientDataSet;mParent:TWinControl;FTag:Integer);
  public
    { Public declarations }
    RKFlag,FKHMainid,formid:String;
    fDEFstr5:string;
  end;
var
  frmZdyAttInputGYS_ZXX: TfrmZdyAttInputGYS_ZXX;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp,U_FjList_RZ;

{$R *.dfm}

procedure TfrmZdyAttInputGYS_ZXX.InitGrid();
begin
  with ADOQueryMain do
  begin
    Filtered:=False;
    Close;
    SQL.Clear;
    sql.Add('select * from KH_Main where KHMainID='''+Trim(FKHMainid)+'''');
    Open;
  end;
  //主表
  if ADOQueryMain.IsEmpty=false then
    SCSHDataNew(ADOQueryMain,Panel2,2)
  else
  begin
    //GYSFZRen.Text:=Trim(DName);
  end;
end;

procedure TfrmZdyAttInputGYS_ZXX.FormDestroy(Sender: TObject);
begin
  frmZdyAttInputGYS_ZXX:=nil;
end;

procedure TfrmZdyAttInputGYS_ZXX.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action:=caFree;
end;

procedure TfrmZdyAttInputGYS_ZXX.TBCloseClick(Sender: TObject);
begin
  Close;
end;
procedure TfrmZdyAttInputGYS_ZXX.FormShow(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmZdyAttInputGYS_ZXX.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmZdyAttInputGYS_ZXX.SSetsaveSqlNewTClient(ADOQueryCmd:TClientDataSet;
                                       Myparent:TWinControl;MyTag:integer);
var
  i:Integer;
  MCode:String;
begin
  with Myparent do
  begin
    for i:=0 to ControlCount-1 do
    begin
      if Controls[i].Tag=MyTag then
      begin
        if Controls[i] is TEdit then
        begin
          if Trim(TEdit(Controls[i]).Text)<>'' then
            ADOQueryCmd.FieldByName(Controls[i].Name).Value:=Trim(TEdit(Controls[i]).Text)
          else
            ADOQueryCmd.FieldByName(Controls[i].Name).Value:=Null;
        end else
        if Controls[i] is TRichEdit then
        begin
          ADOQueryCmd.FieldByName(Controls[i].Name).Value:=TRichEdit(Controls[i]).Text;
        end else
        if Controls[i] is TCheckBox then
        begin
          if TCheckBox(Controls[i]).Checked=True then
            ADOQueryCmd.FieldByName(Controls[i].Name).Value:=1
          else
            ADOQueryCmd.FieldByName(Controls[i].Name).Value:=0;
        end else
        if Controls[i] is TMemo then
        begin
          ADOQueryCmd.FieldByName(Controls[i].Name).Value:=TMemo(Controls[i]).Text;
        end else

        if Controls[i] is TComboBox then
        begin
          ADOQueryCmd.FieldByName(Controls[i].Name).Value:=Trim(TComboBox(Controls[i]).Text);
        end else
        if Controls[i] is TDateTimePicker then
        begin
          if TDateTimePicker(Controls[i]).ShowCheckbox then
          begin
            if TDateTimePicker(Controls[i]).Checked then
              ADOQueryCmd.FieldByName(Controls[i].Name).Value:=TDateTimePicker(Controls[i]).DateTime
            else
              ADOQueryCmd.FieldByName(Controls[i].Name).Value:=null;
          end else
            ADOQueryCmd.FieldByName(Controls[i].Name).Value:=TDateTimePicker(Controls[i]).DateTime;
        end else
        if Controls[i] is TcxButtonEdit then
        begin
          if (TcxButtonEdit(Controls[i]).BeepOnEnter=True) or (TcxButtonEdit(Controls[i]).ParentShowHint=True) then
            ADOQueryCmd.FieldByName(Controls[i].Name).Value:=Trim(TcxButtonEdit(Controls[i]).Text);
          if TcxButtonEdit(Controls[i]).ParentShowHint=False then
          begin
            MCode:=Copy(Trim(Controls[i].Name),1,Length(Trim(Controls[i].Name))-4);
            ADOQueryCmd.FieldByName(MCode).Value:=Trim(TcxButtonEdit(Controls[i]).Hint);
          end;
        end;
      end;
    end;
  end;
end;

function TfrmZdyAttInputGYS_ZXX.SaveData():Boolean;
var
  maxId,CRID,fzdyCode,FYSSubId,maxno,maxnum,FMXNO:String;
begin
///取maxId
  result:=false;
  ADOQueryCmd.Connection.BeginTrans;
  if Trim(FKHMainid)='' then
  begin
    if GetLSNo(ADOQueryCmd,maxId,'KH','KH_Main',4,1)=False then
    begin
      Result:=False;
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取最大号失败！','提示',0);
      Exit;
    end;
    if GetLSNo(ADOQueryCmd,fzdyCode,'PB','KH_Main',4,0)=False then
    begin
      Result:=False;
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取最大号失败！','提示',0);
      Exit;
    end;
    GYSNo.Text:=Trim(fzdyCode);
  end else
  begin
    maxId:=Trim(FKHMainid);
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from KH_Main where KHMainID='''+Trim(FKHMainid)+'''');
    Open;
    If IsEmpty then
    begin
      append;
      FieldByName('Filler').Value:=Trim(DName);
      FieldByName('fillcode').Value:=Trim(DCode);
    end
    else
    begin
      Edit;
      FieldByName('Editer').Value:=Trim(DName);
      FieldByName('Edittime').Value:=SGetServerDateTime(ADOQueryTemp);
    end;
    FieldByName('KHMainID').Value:=Trim(maxId);
    FieldByName('GYSType').Value:='坯布';
    SSetsaveSqlNew(ADOQueryCmd,'KH_Main',Panel2,2);
    post;
  end;
  if FKHMainid='' then
  begin
    if GetLSNo(ADOQueryCmd,maxno,'HB','KH_Sub',4,1)=False then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取系人最大ID失败!','提示',0);
      Exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from KH_Sub where KHSubid='''+Trim(maxno)+'''');
      Open;
      If IsEmpty then
      begin
        append;
        FieldByName('XHNo').Value:=1;
      end
      else
      begin
        Edit;
      end;
      FieldByName('KHSubid').Value:=Trim(maxno);
      FieldByName('KHMainID').Value:=Trim(maxId);
      FieldByName('KHMXName').Value:=Trim(GYSName.Text);
      FieldByName('KHMXJC').Value:=Trim(GYSNameZC.Text);
      FieldByName('KHMXLXR').Value:=Trim(GYSFZRen.Text);
      FieldByName('KHMXTel').Value:=Trim(GYSTel.Text);
      FieldByName('KHMXAdd').Value:=Trim(GYSAdd.Text);
      FieldByName('KHMXSJAdd').Value:=Trim(GYSAdd.Text);
      FieldByName('KHMXNo').Value:=Trim(fzdyCode);
      //FieldByName('KHMXNote').Value:=Trim(Note.Text);
      post;
    end;
  end;
  FKHMainid:=maxId;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from KH_Main where GYSName='''+Trim(GYSName.Text)+'''');
    sql.Add(' and GYSType=''坯布'' ');
    Open;
  end;
  if ADOQueryCmd.RecordCount>1 then
  begin
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('供应商重复!','提示',0);
    Exit;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from KH_Main where GYSNameZC='''+Trim(GYSNameZC.Text)+'''');
    sql.Add(' and GYSType=''坯布'' ');
    Open;
  end;
  if ADOQueryCmd.RecordCount>1 then
  begin
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('供应商简称重复!','提示',0);
    Exit;
  end;
  ADOQueryCmd.Connection.CommitTrans;
  result:=true;
end;

procedure TfrmZdyAttInputGYS_ZXX.ToolButton3Click(Sender: TObject);
begin
  Panel2.SetFocus;
  IF trim(GYSName.Text)='' then
  begin
    application.MessageBox('供应商不能为空！','提示信息',MB_ICONERROR);
    exit;
  end;
  if Trim(GYSNameZC.Text)='' then
  begin
    application.MessageBox('简称不能为空！','提示信息',MB_ICONERROR);
    exit;
  end;
  if Application.MessageBox('确定要保存数据吗？','提示',32+4)<>IDYES then Exit;
  if SaveData() then
  begin
    Application.MessageBox('保存成功!','提示',0);
    ModalResult:=1;
  end;
end;

function TfrmZdyAttInputGYS_ZXX.SaveData2():Boolean;
var
  maxId,CRID,fzdyCode,FYSSubId,maxno:String;
begin
///判断、生成主键
  with CDS_HZ3 do
  begin
    CDS_HZ3.First;
    while not Eof do
    begin
      FYSSubId:=Trim(CDS_HZ3.fieldbyname('LXFS_ZJ').AsString);
      if Trim(FYSSubId)='' then
      begin
        if GetLSNo(ADOQueryMain3,maxno,'LX','LXFS_ZJ',4,1)=False then
        begin
//          ADOQueryCmd.Connection.RollbackTrans;    //回滚事务
          Application.MessageBox('取系人最大ID失败!','提示',0);
          Exit;
        end;
      end else
      begin
        maxno:=Trim(FYSSubId);
      end;
      ADOQueryCmd.Connection.BeginTrans;  //代表开始事务
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from GYS_LXFS where LXFS_ZJ='''+Trim(maxno)+'''');
        Open;
        If Trim(FYSSubId)='' then
        begin
          append;
        end
        else
        begin
          Edit;
        end;
        FieldByName('LXFS_BH').Value:=Trim(CDS_HZ3.fieldbyname('LXFS_BH').AsString);
        FieldByName('LXFS_LXR').Value:=Trim(CDS_HZ3.fieldbyname('LXFS_LXR').AsString);
        FieldByName('LXFS_SJ').Value:=Trim(CDS_HZ3.fieldbyname('LXFS_SJ').AsString);
        FieldByName('LXFS_DZ').Value:=Trim(CDS_HZ3.fieldbyname('LXFS_DZ').AsString);
        FieldByName('LXFS_DH').Value:=Trim(CDS_HZ3.fieldbyname('LXFS_DH').AsString);
        FieldByName('LXFS_CZ').Value:=Trim(CDS_HZ3.fieldbyname('LXFS_CZ').AsString);
        FieldByName('LXFS_BZ').Value:=Trim(CDS_HZ3.fieldbyname('LXFS_BZ').AsString);
        FieldByName('LXFS_ZJ').Value:=Trim(maxno);
        FieldByName('KHMainID').Value:=Trim(CDS_HZ2.fieldbyname('KHMainID').AsString);
        FieldByName('KHSubid').Value:=Trim(CDS_HZ2.fieldbyname('KHSubid').AsString);
        post;
      end;
      CDS_HZ3.Next;
    end;
  end;
  ADOQueryCmd.Connection.CommitTrans;  //代表完结事务
  result:=true;
end;

procedure TfrmZdyAttInputGYS_ZXX.SCSHDataNewTclient(ADOQueryTmp:TClientDataSet;mParent:TWinControl;FTag:Integer);
var
  i,idx:integer;
  mfield,mfieldCode:string;
  ma:TA;
begin
  with ADOQueryTmp do
  begin
    if isEmpty then exit;
    with mParent  do
    begin
      for i:=0 to ControlCount-1 do
      begin
        if Controls[i] is TLabel then continue;
        if Controls[i].Tag<>FTag then  continue;
        mfield:=Controls[i].Name;

        if Controls[i] is TEdit then
        begin
          if Trim(fieldByName(mfield).AsString)<>'' then
            TEdit(Controls[i]).Text:=trim(fieldByName(mfield).AsString)
          else
            TEdit(Controls[i]).Text:='';
        end
        else if Controls[i] is TRichEdit then
        begin
          if Trim(fieldByName(mfield).AsString)<>'' then
            TRichEdit(Controls[i]).Text:=trim(fieldByName(mfield).AsString);
        end

        else if Controls[i] is TComboBox then
        begin
          if TComboBox(Controls[i]).Items.Count>0 then
          begin
            idx:=TComboBox(Controls[i]).Items.IndexOf(trim(fieldByName(mfield).AsString));
          end
          else
            idx:=-1;
          TComboBox(Controls[i]).ItemIndex:=idx;
        end
        else if Controls[i] is TDateTimePicker then
        begin
          if Trim(fieldByName(mfield).AsString)='' then
          begin
            TDateTimePicker(Controls[i]).Checked:=False;
          end
          else
            TDateTimePicker(Controls[i]).DateTime:=fieldByName(mfield).AsDateTime;
        end
        else if Controls[i] is Tmemo then
        begin
          Tmemo(Controls[i]).Text :=trim(fieldByName(mfield).AsString);
        end
        else if Controls[i] is TcxDateEdit then
        begin
           if not IsEmpty  and not fieldByName(mfield).IsNull then
             TcxDateEdit(Controls[i]).text :=fieldByName(mfield).AsString;
        end

        else if  Controls[i] is TcheckBox then
        begin
          if FieldByName(mfield).Value=null then
            TcheckBox(Controls[i]).Checked:=False
          else
            TcheckBox(Controls[i]).Checked:=fieldByName(mfield).asBoolean;
        end else if Controls[i] is TcxButtonEdit then
        begin
          TcxButtonEdit(Controls[i]).Text:=Trim(fieldbyname(mfield).AsString);
          if TcxButtonEdit(Controls[i]).ParentShowHint=False then
          begin
            mfieldCode:=Trim(Copy(mfield,1,Length(mfield)-4));
            TcxButtonEdit(Controls[i]).Hint:=Trim(fieldbyname(mfieldCode).AsString);
          end;
          
        end;
      end; // end for
    end; // end with
  end;  //end for with
end;

end.
