unit U_ZdyAttInputGYS_sj;

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;

type
  TfrmZdyAttInputGYS_FL = class(TForm)
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    ToolBar1: TToolBar;
    ToolButton3: TToolButton;
    TBClose: TToolButton;
    Panel2: TPanel;
    Panel3: TPanel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    V2Column1: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn20: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    DS_HZ2: TDataSource;
    CDS_HZ2: TClientDataSet;
    ADOQueryMain2: TADOQuery;
    cxGridPopupMenu3: TcxGridPopupMenu;
    DS_HZ3: TDataSource;
    ADOQueryMain3: TADOQuery;
    CDS_HZ3: TClientDataSet;
    ToolBar2: TToolBar;
    ToolButton2: TToolButton;
    ToolButton4: TToolButton;
    ToolBar3: TToolBar;
    ToolButton8: TToolButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    Panel4: TPanel;
    Label3: TLabel;
    Label1: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    GYSName: TEdit;
    GYSNo: TEdit;
    GYSFZRen: TEdit;
    GYSNameZC: TEdit;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    GYSTel: TEdit;
    GYSAdd: TEdit;
    Label9: TLabel;
    Label10: TLabel;
    GYSCodeFW: TEdit;
    V2Column2: TcxGridDBColumn;
    Note: TMemo;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBTableView;
    V3Column1: TcxGridDBColumn;
    V3Column3: TcxGridDBColumn;
    V3Column4: TcxGridDBColumn;
    cxGridDBColumn29: TcxGridDBColumn;
    cxGridDBColumn26: TcxGridDBColumn;
    cxGridDBColumn28: TcxGridDBColumn;
    cxGridDBColumn25: TcxGridDBColumn;
    cxGridDBColumn27: TcxGridDBColumn;
    cxGridDBColumn30: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    Panel1: TPanel;
    Label2: TLabel;
    V2ZHXX_JC: TcxGridDBColumn;
    TFJ: TToolButton;
    Panel5: TPanel;
    Label11: TLabel;
    Panel6: TPanel;
    Label12: TLabel;
    Label13: 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);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure TFJClick(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
    function  SaveData():Boolean;
    function  SaveData2():Boolean;
  public
    { Public declarations }
    RKFlag,FCYID,FCYID2:String;
    fDEFstr5:string;
  end;

var
  frmZdyAttInputGYS_FL: TfrmZdyAttInputGYS_FL;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp,U_FjList_RZ;

{$R *.dfm}

procedure TfrmZdyAttInputGYS_FL.InitGrid();
begin
  with ADOQueryMain do
  begin
    Filtered:=False;
    Close;
    SQL.Clear;
    sql.Add('select * from KH_Zdy_AttachmentX where ATID='''+Trim(FCYID)+'''');
    Open;
  end;
  //主表
  if ADOQueryMain.IsEmpty=false then
    SCSHDataNew(ADOQueryMain,Panel4,2)
  else
  begin
    GYSFZRen.Text:=Trim(DName);
  end;
  //表2
  with ADOQueryMain2 do
  begin
    Filtered:=False;
    Close;
    SQL.Clear;
    sql.Add(' select * from GYS_ZHXX where ATID='''+Trim(FCYID)+'''');
    sql.Add(' order by XHNO');
    Open;
  end;
  SCreateCDS20(ADOQueryMain2,CDS_HZ2);
  SInitCDSData20(ADOQueryMain2,CDS_HZ2);
  //表3
  with ADOQueryMain3 do
  begin
    Filtered:=False;
    Close;
    SQL.Clear;
    sql.Add(' select * from GYS_LXFS where ATID='''+Trim(FCYID)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryMain3,CDS_HZ3);
  SInitCDSData20(ADOQueryMain3,CDS_HZ3);
end;


procedure TfrmZdyAttInputGYS_FL.FormDestroy(Sender: TObject);
begin
  frmZdyAttInputGYS:=nil;
end;

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

procedure TfrmZdyAttInputGYS_FL.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('辅料供应商资料录入1',Tv2,'客户自定义');
  WriteCxGrid('辅料供应商资料录入2',Tv3,'客户自定义');
  Close;
end;
procedure TfrmZdyAttInputGYS_FL.FormShow(Sender: TObject);
begin
  ReadCxGrid('辅料供应商资料录入1',Tv2,'客户自定义');
  ReadCxGrid('辅料供应商资料录入2',Tv3,'客户自定义');
  InitGrid();
end;

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

function TfrmZdyAttInputGYS_FL.SaveData():Boolean;
var
  maxId,CRID,fzdyCode,FYSSubId,maxno,maxnum:String;
begin
///取maxId，得出ATID
  result:=false;
  ADOQueryCmd.Connection.BeginTrans;
  if Trim(FCYID)='' then
  begin
    if GetLSNo(ADOQueryCmd,maxId,'AT','KH_Zdy_AttachmentX',4,1)=False then
    begin
      Result:=False;
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取最大号失败！','提示',0);
      Exit;
    end;
    if GetLSNo(ADOQueryCmd,fzdyCode,'PB','KH_Zdy_AttachmentX',4,0)=False then
    begin
      Result:=False;
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取最大号失败！','提示',0);
      Exit;
    end;
    GYSNo.Text:=Trim(fzdyCode);
  end else
  begin
    maxId:=Trim(FCYID);
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from KH_Zdy_AttachmentX where ATID='''+Trim(FCYID)+'''');
    Open;
    If IsEmpty then
    begin
      append;
      FieldByName('Filler').Value:=Trim(DName);
    end
    else
    begin
      Edit;
      FieldByName('Editer').Value:=Trim(DName);
      FieldByName('Edittime').Value:=SGetServerDateTime(ADOQueryTemp);
    end;
    FieldByName('ATID').Value:=Trim(maxId);
    FieldByName('GYSType').Value:='辅料';
    SSetsaveSqlNew(ADOQueryCmd,'KH_Zdy_AttachmentX',panel4,2);
    post;
  end;
  if Trim(FCYID)='' then
  begin
    with CDS_HZ2 do
    begin
      Append;
      FieldByName('ZdyCode').Value:=Trim(GYSNo.Text);
      FieldByName('ZdyName').Value:=Trim(GYSName.Text);
      FieldByName('ZHXX_JC').Value:=Trim(GYSNameZC.Text);
      FieldByName('XHNo').Value:=1;
      Post;
    end;
  end;
  ///表2
  with CDS_HZ2 do
  begin
    First;
    while not Eof do
    begin
      if Trim(CDS_HZ2.fieldbyname('ZHXX_ZJ').AsString)='' then
      begin
        if GetLSNo(ADOQueryCmd,maxno,'ZH','GYS_ZHXX',4,1)=False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('取系人最大ID失败!','提示',0);
          Exit;
        end;
      end else
      begin
        maxno:=Trim(CDS_HZ2.fieldbyname('ZHXX_ZJ').AsString);
      end;
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from GYS_ZHXX where ZHXX_ZJ='''+Trim(maxno)+'''');
        Open;
        If IsEmpty then
        begin
          append;
          FieldByName('XHNo').Value:=CDS_HZ2.fieldbyname('XHNo').AsInteger;
        end
        else
        begin
          Edit;
        end;
        RTSetSaveDataCDS(ADOQueryCmd,Tv2,CDS_HZ2,'GYS_ZHXX',0);
        FieldByName('ZHXX_ZJ').Value:=Trim(maxno);
        FieldByName('ATID').Value:=Trim(maxId);
        post;
      end;
      CDS_HZ2.Next;
    end;
  end;
  //表3
  with CDS_HZ3 do
  begin
    CDS_HZ3.First;
    while not Eof do
    begin
      if Trim(CDS_HZ3.fieldbyname('LXFS_ZJ').AsString)='' then
      begin
        if GetLSNo(ADOQueryCmd,maxnum,'LX','LXFS_ZJ',4,1)=False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('取系人最大ID失败!','提示',0);
          Exit;
        end;
      end else
      begin
        maxnum:=Trim(CDS_HZ3.fieldbyname('LXFS_ZJ').AsString);
      end;
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from GYS_LXFS where LXFS_ZJ='''+Trim(maxnum)+'''');
        Open;
        If IsEmpty then
        begin
          append;
        end
        else
        begin
          Edit;
        end;
        RTSetSaveDataCDS(ADOQueryCmd,Tv3,CDS_HZ3,'GYS_LXFS',0);
        FieldByName('LXFS_ZJ').Value:=Trim(maxnum);
        FieldByName('ATID').Value:=Trim(maxId);
        FieldByName('ZHXX_ZJ').Value:=Trim(CDS_HZ2.fieldbyname('ZHXX_ZJ').AsString);
        post;
      end;
      CDS_HZ3.Next;
    end;
  end;
  ADOQueryCmd.Connection.CommitTrans;
  result:=true;
end;

procedure TfrmZdyAttInputGYS_FL.ToolButton3Click(Sender: TObject);
begin
  Panel4.SetFocus;
  IF trim(GYSName.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;

procedure TfrmZdyAttInputGYS_FL.ToolButton2Click(Sender: TObject);
var
   maxId:String;
begin
  ToolBar2.SetFocus;
  with CDS_HZ2 do
  begin
    Append;
    FieldByName('XHNo').AsInteger:=CDS_HZ2.RecordCount+2;
    post;
  end;
end;

procedure TfrmZdyAttInputGYS_FL.ToolButton4Click(Sender: TObject);
begin
  IF CDS_HZ2.IsEmpty then exit;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  //主表删除
  CDS_HZ2.DisableControls;
  with CDS_HZ2 do
  begin
    if Trim(CDS_HZ2.fieldbyname('ZHXX_ZJ').AsString)<>'' then
    begin

      with ADOQueryMain2 do
      begin
        Close;
        SQL.Clear;
        sql.Add('delete from GYS_ZHXX where ZHXX_ZJ ='''+Trim(CDS_HZ2.fieldbyname('ZHXX_ZJ').AsString)+'''');
        ExecSQL;
      end;
    end;
    Delete;
  end;
  CDS_HZ2.EnableControls;
end;

procedure TfrmZdyAttInputGYS_FL.ToolButton5Click(Sender: TObject);
begin
  ToolBar3.SetFocus;
  with CDS_HZ3 do
  begin
    Append;

    post;
  end;
end;

procedure TfrmZdyAttInputGYS_FL.ToolButton6Click(Sender: TObject);
begin
  IF CDS_HZ3.IsEmpty then exit;
  CDS_HZ3.DisableControls;
  with CDS_HZ3 do
  begin
    if Trim(CDS_HZ3.fieldbyname('LXFS_ZJ').AsString)<>'' then
    begin
      if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
      with ADOQueryMain3 do
      begin
        Close;
        SQL.Clear;
        sql.Add('delete from GYS_LXFS where LXFS_ZJ ='''+Trim(CDS_HZ3.fieldbyname('LXFS_ZJ').AsString)+'''');
        ExecSQL;
      end;
    end;
    Delete;
  end;
  CDS_HZ3.EnableControls;
end;

procedure TfrmZdyAttInputGYS_FL.ToolButton8Click(Sender: TObject);
begin
  ToolBar3.SetFocus;
  if CDS_HZ3.IsEmpty then exit;
  if Application.MessageBox('确定要保存数据吗？','提示',32+4)<>IDYES then Exit;

  if SaveData2() then
  begin
    Application.MessageBox('保存成功!','提示',0);
    ModalResult:=1;
  end;
end;

function TfrmZdyAttInputGYS_FL.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('ATID').Value:=Trim(maxId); //ZhuG
        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('ATID').Value:=Trim(CDS_HZ2.fieldbyname('ATID').AsString);
        FieldByName('ZHXX_ZJ').Value:=Trim(CDS_HZ2.fieldbyname('ZHXX_ZJ').AsString);
        post;
      end;
      CDS_HZ3.Next;
    end;
  end;
  ADOQueryCmd.Connection.CommitTrans;  //代表完结事务
  result:=true;
end;

procedure TfrmZdyAttInputGYS_FL.TFJClick(Sender: TObject);
begin
  if CDS_HZ2.IsEmpty then exit;
  if Self.CDS_HZ2.fieldbyname('ZHXX_ZJ').AsString='' then
  begin
    Application.MessageBox('请保存数据','提示');
    exit;
  end;
  try
    frmFjList_RZ:=TfrmFjList_RZ.Create(Application);
    with frmFjList_RZ do
    begin
      fkeyNO:=Trim(Self.CDS_HZ2.fieldbyname('ZHXX_ZJ').AsString);
      fType:='供应商财务';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmFjList_RZ.Free;
  end;
end;

end.
