unit U_CPFHSQList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxGrid, DBClient, cxCheckBox, cxCalendar, cxSplitter, RM_Dataset, RM_System,
  RM_Common, RM_Class, RM_GridReport, RM_e_Xls, Menus, cxPC, cxButtonEdit,
  RM_E_llPDF, cxDropDownEdit, cxTextEdit, IdBaseComponent, IdComponent,
  IdTCPConnection, IdTCPClient, IdHTTP, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator, dxBarBuiltInMenu, dxSkinsCore, dxSkinsDefaultPainters,
  dxSkinscxPCPainter, dxSkinBlack, dxSkinBlue, dxSkinBlueprint,
  dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom, dxSkinDarkSide,
  dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray,
  dxSkinOffice2013LightGray, dxSkinOffice2013White, dxSkinPumpkin,
  dxSkinSeven, dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus,
  dxSkinSilver, dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008,
  dxSkinTheAsphaltWorld, dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint,
  dxSkinXmas2008Blue;

type
  TfrmCPFHSQList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    v1OrdDate: TcxGridDBColumn;
    v1OrdPerson1: TcxGridDBColumn;
    Order_Main: TClientDataSet;
    RMDB_Main: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    ToolButton1: TToolButton;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    v1Column4: TcxGridDBColumn;
    Panel1: TPanel;
    Label1: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    SHDWName: TEdit;
    YwyName: TEdit;
    MPRTCodeName: TEdit;
    toconno: TEdit;
    MPRTKZ: TEdit;
    MPRTMF: TEdit;
    cxTabControl1: TcxTabControl;
    tchk: TToolButton;
    Tnochk: TToolButton;
    cxSplitter1: TcxSplitter;
    ADOQuerySub: TADOQuery;
    DataSource2: TDataSource;
    v1Column8: TcxGridDBColumn;
    Label2: TLabel;
    //RMllPDFExport1: TRMllPDFExport;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v1Column5: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ClientDataSet2: TClientDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    CDS_Note: TClientDataSet;
    RM1: TRMGridReport;
    v1Column11: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    v1Column25: TcxGridDBColumn;
    v2Column8: TcxGridDBColumn;
    v2Column12: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    CDSUser: TClientDataSet;
    IdHTTP1: TIdHTTP;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Label3: TLabel;
    HZP_CODE: TEdit;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    ToolButton4: TToolButton;
    Tv1Column7: TcxGridDBColumn;
    cktype: TComboBox;
    Label6: TLabel;
    Tv2Column1: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure SHDWNameChange(Sender: TObject);
    procedure tchkClick(Sender: TObject);
    procedure TnochkClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure cktypeChange(Sender: TObject);
  private
    procedure InitGrid();
    procedure InitSubGrid();
    procedure InitForm();
    procedure SetStatus();
    function DelData(): Boolean; 
    { Private declarations }
  public
    FFInt: Integer;
    canshu1: string;
    { Public declarations }
  end;

implementation

uses
  U_DataLink, U_CPFHSQInPut, U_Fun, U_ZDYHelp;

{$R *.dfm}
procedure TfrmCPFHSQList.SetStatus();
begin
  tchk.Visible := False;
  Tnochk.Visible := False;
  TBDel.Visible := False;
  TBEdit.Visible := False;
  TBAdd.Visible := False;
  ToolButton2.Visible := False;
  ToolButton3.Visible := False;
  if canshu1 = '录入' then
  begin
    TBAdd.Visible := true;
    case cxTabControl1.TabIndex of
      0:
        begin
          TBEdit.Visible := true;
          TBDel.Visible := true;
        end;
      1:
        begin

        end;
      2:
        begin

        end;
    end;
  end
  else if canshu1 = '审核' then
  begin
    TBAdd.Visible := true;
    case cxTabControl1.TabIndex of
      0:
        begin
          tchk.Visible := true;
          TBEdit.Visible := true;
          TBDel.Visible := true;
        end;
      1:
        begin
          Tnochk.Visible := true;
          ToolButton2.Visible := True;
        end;
      2:
        begin
          ToolButton3.Visible := True;
        end;
    end;
  end;
end;

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

procedure TfrmCPFHSQList.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid(trim(self.Caption), Tv1, '发货申请管理3');
  WriteCxGrid(trim(self.Caption) + 'TV2', Tv2, '发货申请管理4');
end;

procedure TfrmCPFHSQList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      Sql.Add(' select A.*');
      SQL.Add(' ,HZP_CODE= STUFF(( SELECT  '','' +convert(VARCHAR, P_CODE) FROM SQ_FH_SUB B WHERE B.FHMAINID=A.FHMAINID FOR XML PATH('''')), 1, 1, '''')  ');
      sql.Add(' from SQ_FH_Main A');
      sql.Add(' where 1=1');
      if cxTabControl1.TabIndex <> 0 then
      begin
        sql.Add(' and filltime>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
        sql.Add(' and filltime<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
      end;
      if cxTabControl1.TabIndex = 0 then
      begin
        SQL.Add(' and isnull(A.Chker,'''')='''' ');
      end
      else if cxTabControl1.TabIndex = 1 then
      begin
        SQL.Add(' and isnull(A.Chker,'''')<>'''' ');
        SQL.Add(' and isnull(A.ChkStatus,'''')=''审核通过'' ');
      end
      else if cxTabControl1.TabIndex = 2 then
      begin
        SQL.Add(' and isnull(A.Chker,'''')<>'''' ');
        SQL.Add(' and isnull(A.ChkStatus,'''')=''已完成'' ');
      end;
 
//      showmessage(sql.text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCPFHSQList.InitForm();
begin

  ReadCxGrid(trim(self.Caption), Tv1, '发货申请管理3');
  ReadCxGrid(trim(self.Caption) + 'TV2', Tv2, '发货申请管理4');

  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime;
  SetStatus();
  InitGrid();

end;

procedure TfrmCPFHSQList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQueryMain, Order_Main);
  SInitCDSData20(ADOQueryMain, Order_Main);
end;

procedure TfrmCPFHSQList.TBEditClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  if cxTabControl1.TabIndex <> 0 then
    Exit;
 
//  with ADOQueryTemp do
//  begin
//    close;
//    sql.Clear;
//    sql.Add('select * from SQ_FH_Main');
//    sql.Add('where FHMainId=' + quotedstr(trim(Order_Main.fieldbyname('FHMainId').AsString)));
//    Open;
//  end;
//  if Trim(ADOQueryTemp.fieldbyname('YSID').AsString) <> '' then
//  begin
//    Application.MessageBox('已生成应收账款不能修改数据!', '提示', 0);
//    Exit;
//  end;
  try
    frmPBFHSQInPut := TfrmPBFHSQInPut.Create(Application);
    with frmPBFHSQInPut do
    begin
      PState := 1;
      FMainId := Trim(Self.Order_Main.fieldbyname('FHMainId').AsString);

      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmPBFHSQInPut.Free;
  end;
end;

procedure TfrmCPFHSQList.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  if cxTabControl1.TabIndex <> 0 then
    Exit;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from SQ_FH_Sub where FHMainId=''' + Trim(Order_Main.fieldbyname('FHMainId').AsString) + '''');
    sql.add(' and isnull(FHPiQty,0)>0');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Application.MessageBox('已发货不能删除数据!', '提示', 0);
    exit;
  end;
//  with ADOQueryTemp do
//  begin
//    close;
//    sql.Clear;
//    sql.Add('select * from SQ_FH_Main');
//    sql.Add('where FHMainId=' + quotedstr(trim(Order_Main.fieldbyname('FHMainId').AsString)));
//    Open;
//  end;
//  if Trim(ADOQueryTemp.fieldbyname('YSID').AsString) <> '' then
//  begin
//    Application.MessageBox('已生成应收账款不能删除!', '提示', 0);
//    Exit;
//  end;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  if DelData() then
  begin
    Order_Main.Delete;
    InitSubGrid();
  end;
end;

function TfrmCPFHSQList.DelData(): Boolean;
begin
  try
    Result := false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete SQ_FH_Sub where FHMainId=''' + Trim(Order_Main.fieldbyname('FHMainId').AsString) + '''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete SQ_FH_Main where FHMainId=''' + Trim(Order_Main.fieldbyname('FHMainId').AsString) + '''');
      ExecSQL;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result := False;
    Application.MessageBox('数据删除异常！', '提示', 0);
  end;
end;

procedure TfrmCPFHSQList.TBPrintClick(Sender: TObject);
var
  fPrintFile: string;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  if Order_Main.IsEmpty then
    Exit;
  if cxTabControl1.TabIndex <> 1 then
    Exit;
  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据', '提示', 0);
    Exit;
  end;

  try
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      First;
      while Order_Main.Locate('SSel', True, []) = True do
      begin
        begin
          with ADOQueryPrint do
          begin
            Close;
            sql.Clear;
            Sql.Add(' select A.*,B.*');                                                                                               //and Y.ClothType=B.ClothType
            sql.Add(' from SQ_FH_Main A inner join SQ_FH_Sub B on A.FHMainId=B.FHMainId ');
            sql.Add(' where A.FHMainId=''' + Trim(Order_Main.fieldbyname('FHMainId').AsString) + '''');
            open;

          end;
          ExportFtErpFile('坯布发货申请单.rmf', ADOQueryTemp);
          fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\坯布发货申请单.rmf';
          if FileExists(fPrintFile) then
          begin
            try
              Moudle := LoadLibrary('MakeQRBarcode.dll');
              @Makebar := GetProcAddress(Moudle, 'Make');
              @Mixtext := GetProcAddress(Moudle, 'MixText');
              Txt := Trim(Order_Main.fieldbyname('FHMainId').AsString);
              fImagePath := ExtractFilePath(Application.ExeName) + 'image\temp.bmp';
              if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName) + 'image')) then
                CreateDirectory(pchar(ExtractFilePath(Application.ExeName) + 'image'), nil);
              if FileExists(fImagePath) then
                DeleteFile(fImagePath);
              Makebar(pchar(Txt), Length(Txt), 3, 3, 0, PChar(fImagePath), 3);
            except
              application.MessageBox('条形码生成失败！', '提示信息', MB_ICONERROR);
              exit;
            end;

            RMVariables['QRBARCODE'] := fImagePath;
            RM1.LoadFromFile(fPrintFile);
            RM1.printReport;
          end
          else
          begin
            Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\坯布发货申请单.rmf'), '提示', 0);
          end;
          Order_Main.Delete;
        end;
      end;
    end;
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
//    application.MessageBox('审核成功！', '提示信息');
    Exit;
  except

    Order_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
//    application.MessageBox('审核失败！', '提示信息', 0);
  end;

//  with ADOQueryPrint do
//  begin
//    Close;
//    sql.Clear;
//    Sql.Add(' select A.*,B.*');                                                                                               //and Y.ClothType=B.ClothType
//    sql.Add(' from SQ_FH_Main A inner join SQ_FH_Sub B on A.FHMainId=B.FHMainId ');
//    sql.Add(' where A.FHMainId=''' + Trim(Order_Main.fieldbyname('FHMainId').AsString) + '''');
//    open;
//
//  end;
//  ExportFtErpFile('坯布发货申请单.rmf', ADOQueryTemp);
//  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\坯布发货申请单.rmf';
//  if FileExists(fPrintFile) then
//  begin
//    try
//      Moudle := LoadLibrary('MakeQRBarcode.dll');
//      @Makebar := GetProcAddress(Moudle, 'Make');
//      @Mixtext := GetProcAddress(Moudle, 'MixText');
//      Txt := Trim(Order_Main.fieldbyname('FHMainId').AsString);
//      fImagePath := ExtractFilePath(Application.ExeName) + 'image\temp.bmp';
//      if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName) + 'image')) then
//        CreateDirectory(pchar(ExtractFilePath(Application.ExeName) + 'image'), nil);
//      if FileExists(fImagePath) then
//        DeleteFile(fImagePath);
//      Makebar(pchar(Txt), Length(Txt), 3, 3, 0, PChar(fImagePath), 3);
//    except
//      application.MessageBox('条形码生成失败！', '提示信息', MB_ICONERROR);
//      exit;
//    end;
//
//    RMVariables['QRBARCODE'] := fImagePath;
//    RM1.LoadFromFile(fPrintFile);
//    RM1.ShowReport;
//  end
//  else
//  begin
//    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\坯布发货申请单.rmf'), '提示', 0);
//  end;
end;

procedure TfrmCPFHSQList.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
  TBFind.Click;
  InitSubGrid();
end;

procedure TfrmCPFHSQList.TBAddClick(Sender: TObject);
var
  maxno: string;
begin
  try
    frmPBFHSQInPut := TfrmPBFHSQInPut.Create(Application);
    with frmPBFHSQInPut do
    begin
      PState := 0;
      FMainId := '';
      frmPBFHSQInPut.CKTYPE.text := '明细';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmPBFHSQInPut.Free;
  end;
  InitGrid();
end;

procedure TfrmCPFHSQList.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmCPFHSQList.CheckBox1Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmCPFHSQList.CheckBox2Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmCPFHSQList.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  try
    frmPBFHSQInPut := TfrmPBFHSQInPut.Create(Application);
    with frmPBFHSQInPut do
    begin
      PState := 1;
      FMainId := Trim(Self.Order_Main.fieldbyname('FHMainId').AsString);
      ToolBar2.Visible := False;
      TBSave.Visible := False;
      ScrollBox1.Enabled := False;
      Tv1.OptionsSelection.CellSelect := False;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmPBFHSQInPut.Free;
  end;
end;

procedure TfrmCPFHSQList.SHDWNameChange(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQueryMain, Order_Main);
  SInitCDSData20(ADOQueryMain, Order_Main);
end;

procedure TfrmCPFHSQList.tchkClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  if cxTabControl1.TabIndex <> 0 then
    exit;
  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      First;
      while Order_Main.Locate('SSel', True, []) = True do
      begin
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update  SQ_FH_Main SET Chker=''' + Trim(DName) + ''',ChkTime=getdate(),ChkStatus=''审核通过'' ');
            sql.Add('where FHMainId=' + quotedstr(trim(Order_Main.fieldbyname('FHMainId').AsString)));
            execsql;
          end;

          Order_Main.Delete;
        end;
      end;
    end;
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('审核成功！', '提示信息');
    Exit;
  except

    Order_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('审核失败！', '提示信息', 0);
  end;

end;

procedure TfrmCPFHSQList.InitSubGrid();
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    if Order_Main.IsEmpty = False then
      sql.Add('select * from SQ_FH_Sub A where A.FHMainId=''' + Trim(Order_Main.fieldbyname('FHMainId').AsString) + '''')
    else
      sql.Add('select * from SQ_FH_Sub A where 1=2');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, ClientDataSet2);
  SInitCDSData20(ADOQueryTemp, ClientDataSet2);
end;

procedure TfrmCPFHSQList.TnochkClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  if cxTabControl1.TabIndex <> 1 then
    exit;

  try
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      First;
      while Order_Main.Locate('SSel', True, []) = True do
      begin
        begin
          with ADOQueryTemp do
          begin
            close;
            sql.Clear;
            sql.Add('select * from SQ_FH_Main');
            sql.Add('where FHMainId=' + quotedstr(trim(Order_Main.fieldbyname('FHMainId').AsString)));

            Open;
          end;
          if Trim(ADOQueryTemp.fieldbyname('CWID').AsString) <> '' then
          begin
            Application.MessageBox('已生成应收账款不能撤销!', '提示', 0);
            ADOQueryCmd.Connection.RollbackTrans;
            Exit;
          end;
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update  SQ_FH_Main SET Chker=Null,ChkTime=Null,ChkStatus=Null ');
            sql.Add('where FHMainId=' + quotedstr(trim(Order_Main.fieldbyname('FHMainId').AsString)));
            execsql;
          end;

          Order_Main.Delete;
        end;
      end;
    end;
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('撤销审核成功！', '提示信息');
    Exit;
  except

    Order_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('撤销审核失败！', '提示信息', 0);
  end;

end;

procedure TfrmCPFHSQList.cxTabControl1Change(Sender: TObject);
begin
  SetStatus();
  InitGrid();
end;

procedure TfrmCPFHSQList.Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    if Order_Main.IsEmpty = False then
    begin
      sql.Add('select A.* from SQ_FH_Sub A ');
//      sql.Add(' inner join PD_Plan_Sub B on A.FromSubId=B.SubId');
//      sql.Add(' inner join PD_Plan_Main C on B.MainId=C.MainId');
      sql.Add(' where A.FHMainId=''' + Trim(Order_Main.fieldbyname('FHMainId').AsString) + '''');
    end
    else
    begin
      sql.Add('select A.* from SQ_FH_Sub A ');
//      sql.Add(' inner join PD_Plan_Sub B on A.FromSubId=B.SubId');
//      sql.Add(' inner join PD_Plan_Main C on B.MainId=C.MainId');
      sql.Add('  where 1=2');
    end;

    Open;
  end;
  SCreateCDS20(ADOQueryTemp, ClientDataSet2);
  SInitCDSData20(ADOQueryTemp, ClientDataSet2);
end;

procedure TfrmCPFHSQList.ToolButton2Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  if cxTabControl1.TabIndex <> 1 then
    exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  SQ_FH_Main SET Chker=''' + Trim(DName) + ''',ChkTime=getdate(),ChkStatus=''已完成'' ');
      sql.Add('where FHMainId=' + quotedstr(trim(Order_Main.fieldbyname('FHMainId').AsString)));
      execsql;
    end;
    Order_Main.Delete;
    InitSubGrid();
    application.MessageBox('已完成成功！', '提示信息');
  except
    application.MessageBox('已完成失败！', '提示信息', 0);
  end;
end;

procedure TfrmCPFHSQList.ToolButton3Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  if cxTabControl1.TabIndex <> 2 then
    exit;

  try

    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  SQ_FH_Main SET Chker=''' + Trim(DName) + ''',ChkTime=getdate(),ChkStatus=''审核通过'' ');
      sql.Add('where FHMainId=' + quotedstr(trim(Order_Main.fieldbyname('FHMainId').AsString)));
      execsql;
    end;
    Order_Main.Delete;
    InitSubGrid();
    application.MessageBox('撤销完成成功！', '提示信息');
  except
    application.MessageBox('撤销完成失败！', '提示信息', 0);
  end;
end;

procedure TfrmCPFHSQList.ToolButton4Click(Sender: TObject);
var
  maxno: string;
begin
  try
    frmPBFHSQInPut := TfrmPBFHSQInPut.Create(Application);
    with frmPBFHSQInPut do
    begin
      PState := 0;
      FMainId := '';
      frmPBFHSQInPut.CKTYPE.text := '汇总';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmPBFHSQInPut.Free;
  end;
  InitGrid();
end;

procedure TfrmCPFHSQList.cktypeChange(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQueryMain, Order_Main);
  SInitCDSData20(ADOQueryMain, Order_Main);
end;

end.

