unit U_CKProductJYHZList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxGridCustomTableView, cxGridTableView,
  cxGridBandedTableView, cxGridDBBandedTableView, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridDBTableView, cxGrid, StdCtrls, ComCtrls,
  ExtCtrls, ToolWin, cxGridCustomPopupMenu, cxGridPopupMenu, ADODB, DBClient,
  cxDropDownEdit, MovePanel, cxButtonEdit, cxCalendar, RM_System, RM_Common,
  RM_Class, RM_GridReport, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxSkinsCore, 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,
  dxSkinsDefaultPainters, dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint,
  dxSkinXmas2008Blue, dxSkinscxPCPainter, RM_Dataset, dxBarBuiltInMenu, cxPC,
  cxCheckBox, Menus, RM_e_Xls;

type
  TfrmCKProductJYHZList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label3: TLabel;
    PM: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    Tv1: TcxGridDBTableView;
    cxGrid2Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    v2Column1: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    Label5: TLabel;
    orderNo: TEdit;
    Label6: TLabel;
    YS: TEdit;
    v1Column7: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    Label8: TLabel;
    MPRTKZ: TEdit;
    Label9: TLabel;
    MPRTMF: TEdit;
    Label7: TLabel;
    CPType: TComboBox;
    MovePanel2: TMovePanel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    YWY: TEdit;
    OrdPerson1: TEdit;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    MJstr4: TEdit;
    Label4: TLabel;
    Panel4: TPanel;
    Label13: TLabel;
    Panel10: TPanel;
    Image2: TImage;
    RichEdit1: TRichEdit;
    v1Column9: TcxGridDBColumn;
    CKDH: TEdit;
    Label14: TLabel;
    KH: TEdit;
    Label15: TLabel;
    v1Column13: TcxGridDBColumn;
    RMGridReport1: TRMGridReport;
    ToolButton1: TToolButton;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    RM2: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    ADOQueryPrint: TADOQuery;
    RM1: TRMGridReport;
    RMDBHZ: TRMDBDataSet;
    CDS_HZ: TClientDataSet;
    ComboBox1: TComboBox;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    cxTabControl1: TcxTabControl;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    ToolButton4: TToolButton;
    Button9: TButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    RMXLSExport1: TRMXLSExport;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure PMChange(Sender: TObject);
    procedure v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure PRTColorChange(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure Tv1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure TBPrintClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure cxTabControl1Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure Button9Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
  private
    FLeft, FTop: Integer;
    procedure InitGrid();
    { Private declarations }
  public                                  
    { Public declarations }
  end;

var
  frmCKProductJYHZList: TfrmCKProductJYHZList;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_JYOrderCDOne, U_CKProductJYMX, U_SCPersonColor;

{$R *.dfm}

procedure TfrmCKProductJYHZList.FormDestroy(Sender: TObject);
begin
  frmCKProductJYHZList := nil;
end;

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

procedure TfrmCKProductJYHZList.FormCreate(Sender: TObject);
begin
  //cxGrid1.Align:=alClient;
  BegDate.DateTime := SGetServerDateTime(ADOQueryTemp);
  EndDate.DateTime := SGetServerDateTime(ADOQueryTemp)
end;

procedure TfrmCKProductJYHZList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      Filtered := False;
      sql.Add('select convert(char(10),A.CKtime,120) as CRTime,A.MJType as CPType,A.MainId,A.MJTypeother as QtyUnit,C.OrderNo,C.ConNO,D.PRTCodeName,D.PrtColor,D.PrtHX,D.SOrddefstr4,A.CKDH, ');
      sql.Add('dj,je=dj*(SUM(A.MJLen)), count(A.MainId) as JQty,SUM(A.MJLen) as Qty,SUM(A.MJMaoZ) as KGQty,SUM(A.MJQty4) as MJQty4,');
      sql.Add('SCKROLL=(select sum(mjlen) from WFB_MJJY X where X.CKDH=A.CKDH  and X.ckflag=''已出库''),');
      sql.Add('SkcROLL=(select sum(mjlen) from WFB_MJJY X where X.CKDH=A.CKDH  and X.ckflag=''未出库''),');
      sql.Add('CKROLL=(select count(*) from WFB_MJJY X where X.CKDH=A.CKDH  and X.ckflag=''已出库''),');
      sql.Add('KCROLL=(select count(*) from WFB_MJJY X where X.CKDH=A.CKDH  and X.ckflag=''未出库''),');
      sql.add('max(KH)KH,max(YS)YS,MAX(PM)PM');
//      sql.Add('KH=(select top 1 KH from WFB_MJJY X where X.CKDH=A.CKDH and  X.dj=A.dj),');
//      sql.Add('YS=(select top 1 YS from WFB_MJJY X where X.CKDH=A.CKDH and X.dj=A.dj),');
//      sql.Add('PM=(select top 1 PM from WFB_MJJY X where X.CKDH=A.CKDH and  X.dj=A.dj)');
      sql.Add('from WFB_MJJY A ');
      sql.Add('inner join JYOrder_Main C on C.MainId=A.MainId ');
      sql.Add('inner join JYOrder_sub D on D.subID=A.subID ');
      Sql.add('where A.CKtime>=''' + formatdateTime('yyyy-MM-dd', begdate.Date) + ''' ');
      Sql.add('and A.CKtime<''' + formatdateTime('yyyy-MM-dd', enddate.Date + 1) + ''' ');
      if cxTabControl1.TabIndex = 0 then
      begin
        sql.add('and SHflag=''0'' ');                               
      end;
      if cxTabControl1.TabIndex = 1 then                      
      begin                                              
        sql.add('and SHflag=''1'' ');
      end;
      Sql.add('group by convert(char(10),A.CKtime,120),A.SubId,A.CKDH,A.MJType,A.MainId,A.MJTypeother,C.OrderNo,C.ConNO,D.PRTCodeName,D.PrtColor,D.PrtHX,D.SOrddefstr4,dj');
//      ShowMessage(SQL.Text);
      Open;
    end;

    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCKProductJYHZList.TBRafreshClick(Sender: TObject);
begin
  //BegDate.SetFocus;
  MovePanel2.Visible := True;
  MovePanel2.Refresh;
  InitGrid();
  MovePanel2.Visible := False;
end;

procedure TfrmCKProductJYHZList.ConNoMChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  end;
end;

procedure TfrmCKProductJYHZList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption + tv1.Name, Tv1, '成品仓库');
  Close;
end;

procedure TfrmCKProductJYHZList.FormShow(Sender: TObject);
begin

  ReadCxGrid(self.Caption + tv1.Name, Tv1, '成品仓库');
  if Trim(DParameters2) = '管理' then
  begin
    //v1Column5.Options.Focusing:=True;
  end
  else
  begin
    //v1Column5.Options.Focusing:=False;
  end;
  //InitGrid();
end;

procedure TfrmCKProductJYHZList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  TcxGridToExcel('库存汇总列表', cxGrid2);
end;

procedure TfrmCKProductJYHZList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  end;
end;

procedure TfrmCKProductJYHZList.PMChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCKProductJYHZList.v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'SOrdDefStr10';
      flagname := '库存存放地点';
      if ShowModal = 1 then
      begin
        with CDS_Main do
        begin
          Edit;
          FieldByName('SOrdDefStr10').Value := Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYOrder_Sub Set SOrdDefStr10=''' + Trim(ClientDataSet1.fieldbyname('ZdyName').AsString) + '''');
          sql.Add(' where SubId=''' + Trim(Self.CDS_Main.fieldbyname('SubId').AsString) + '''');
          ExecSQL;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCKProductJYHZList.PRTColorChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCKProductJYHZList.Image2Click(Sender: TObject);
begin
  Panel4.Visible := False;
end;

procedure TfrmCKProductJYHZList.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  Panel4.Left := FLeft;
  Panel4.Top := FTop + 110;
  Panel4.Visible := True;
  Panel10.Caption := Trim(TV1.Controller.FocusedColumn.Caption);
  RichEdit1.Text := CDS_Main.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString;
  application.ProcessMessages;
end;

procedure TfrmCKProductJYHZList.Tv1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  FLeft := X;
  FTop := Y;
end;

procedure TfrmCKProductJYHZList.TBPrintClick(Sender: TObject);
var
  fPrintFile, fPrintFile10, FMainID: string;
  sqlStr: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  if trim(ComboBox1.Text) = '' then
    exit;
//  if Order_Main.Locate('SSel', True, []) = False then
//  begin
//    Application.MessageBox('没有选择数据', '提示', 0);
//    Exit;
//  end;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + trim(ComboBox1.Text) + '.rmf';

  ExportFtErpFile(Trim(ComboBox1.Text) + '.rmf', ADOQueryCmd);

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete TBSubID where DName=''' + Trim(DCode) + '''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('SELECT * FROM TBSubID where 1=2 ');
    open;
  end;
  sqlStr := '';
  FMainID := '';
//  Order_Main.DisableControls;
//  try
//    ADOQueryCmd.Connection.BeginTrans;
//    with Order_Main do
//    begin
//      First;
//      while not Eof do
//      begin
//        if Fieldbyname('Ssel').AsBoolean then
//        begin
//          if FMainID = '' then
//          begin
//            FMainID := Trim(Order_Main.fieldbyname('CKFlag').AsString);
//          end
//          else
//          begin
//            if Trim(Order_Main.fieldbyname('CKFlag').AsString) <> FMainID then
//            begin
//              application.MessageBox('已出库和未出库不能一起打印！撤销出库可重新打印', '提示信息', 0);
//              ADOQueryCmd.Connection.RollbackTrans;
//              EnableControls;
//              exit;
//            end;
//          end;
//          sqlStr := sqlStr + 'insert into TBSubID(SubId,Dname) ';
//          sqlStr := sqlStr + ' values( ';
//          sqlStr := sqlStr + ' ''' + Trim(Order_Main.fieldbyname('MJID').AsString) + ''', ';
//          sqlStr := sqlStr + ' ''' + Trim(DCode) + ''' ';
//          sqlStr := sqlStr + ' ) ';
//        end;
//        Next;
//      end;
//    end;
//    ADOQueryCmd.Connection.CommitTrans;
//  except
//    ADOQueryCmd.Connection.RollbackTrans;
//  end;
//  Order_Main.EnableControls;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(sqlStr);
//    showmessage(sql.text);
    ExecSQL;
  end;

  if (trim(ComboBox1.Text) = '检验码单') then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMDYDL_HZ29 ');
      sql.add('@DName=' + quotedstr(Trim(CDS_Main.fieldbyname('CKDH').AsString)));
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_HZ);
    SInitCDSData20(ADOQueryTemp, CDS_HZ);

    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Do_PrintMd_HZ ');
      sql.add('@mainID=' + quotedstr(Trim('')));
      sql.add(',@DName=' + quotedstr(Trim(CDS_Main.fieldbyname('CKDH').AsString)));
      sql.add(',@flag=''29'' ');
      Open;
    end;
  end;

  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;

end;

procedure TfrmCKProductJYHZList.ToolButton1Click(Sender: TObject);
begin
  if cds_main.IsEmpty then
    Exit;
  frmJYOrderCDOne := TfrmJYOrderCDOne.Create(Application);
  with frmJYOrderCDOne do
  begin
    orderno.Text := trim(self.CDS_Main.fieldbyname('orderno').asstring);
//    gangno.Text := trim(self.CDS_Main.fieldbyname('MJStr4').asstring);
//    PRTColor.Text := trim(self.CDS_Main.fieldbyname('PRTColor').asstring);
    if ShowModal = 1 then
    begin
    //     InitGrid();
    end;
    free;
  end;
end;

procedure TfrmCKProductJYHZList.ToolButton2Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;
  try
    CDS_Main.DisableControls;
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      First;
      while not Eof do

      begin
        if CDS_Main.FieldByName('SSel').AsBoolean = True then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update  WFB_MJJY SET SHflag=''1'' ');
            sql.Add('where CKDH=' + quotedstr(trim(CDS_Main.fieldbyname('CKDH').AsString)));
            sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
            sql.Add(' ' + quotedstr(trim(DName)));
            sql.Add(',getdate() ');
            sql.Add(',' + quotedstr(trim(self.Caption)));
            sql.Add(',' + quotedstr(trim('出库订单审核')));
            sql.Add(',' + quotedstr(trim('出库单号：' + trim(CDS_Main.FieldByName('CKDH').AsString))));
            sql.Add(',' + quotedstr(trim('成功')));
            sql.Add(')');
            execsql;
          end;
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    CDS_Main.EnableControls;
    application.MessageBox('订单审核成功！', '提示信息');
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    CDS_Main.EnableControls;
    application.MessageBox('订单审核失败！', '提示信息', 0);
  end;
end;

procedure TfrmCKProductJYHZList.ToolButton3Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;

  try
    CDS_Main.DisableControls;
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      First;
      while not Eof do
      begin
        if CDS_Main.FieldByName('SSel').AsBoolean = True then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update  WFB_MJJY SET SHflag=''0'' ');
            sql.Add('where CKDH=' + quotedstr(trim(CDS_Main.fieldbyname('CKDH').AsString)));
            sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
            sql.Add(' ' + quotedstr(trim(DName)));
            sql.Add(',getdate() ');
            sql.Add(',' + quotedstr(trim(self.Caption)));
            sql.Add(',' + quotedstr(trim('出库订单撤销审核')));
            sql.Add(',' + quotedstr(trim('出库单号：' + trim(CDS_Main.FieldByName('CKDH').AsString))));
            sql.Add(',' + quotedstr(trim('成功')));
            sql.Add(')');
            execsql;
          end;
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    CDS_Main.EnableControls;
    application.MessageBox('订单审核成功！', '提示信息');
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    CDS_Main.EnableControls;
    application.MessageBox('订单审核失败！', '提示信息', 0);
  end;
end;

procedure TfrmCKProductJYHZList.cxTabControl1Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmCKProductJYHZList.ToolButton4Click(Sender: TObject);
begin
  if cds_main.IsEmpty then
    Exit;
  frmCKProductJYMXt := TfrmCKProductJYMXt.Create(Application);
  with frmCKProductJYMXt do
  begin
    CKDH.Text := trim(self.CDS_Main.fieldbyname('CKDH').asstring);
    if ShowModal = 1 then
    begin
    //     InitGrid();
    end;
    frmCKProductJYMXt.free;
  end;
end;

procedure TfrmCKProductJYHZList.Button9Click(Sender: TObject);
begin
  try
    frmSCPersonColor := TfrmSCPersonColor.Create(self);
    with frmSCPersonColor do
    begin
      FTYPE := 'KH';
      FCX := trim(KH.Text);
      if ShowModal = 1 then
      begin

        KH.Text := FName;

      end;
    end;
  finally
    frmSCPersonColor.close;
  end;
end;

procedure TfrmCKProductJYHZList.ToolButton5Click(Sender: TObject);
var
  fPrintFile, fPrintFile10, FMainID: string;
  sqlStr: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据', '提示', 0);
    Exit;
  end;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\对账单.rmf';

  ExportFtErpFile('对账单.rmf', ADOQueryCmd);

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete TBSubID where DName=''' + Trim(DCode) + '''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('SELECT * FROM TBSubID where 1=2 ');
    open;
  end;
  sqlStr := '';
  FMainID := '';
  CDS_Main.DisableControls;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      First;
      while not Eof do
      begin
        if Fieldbyname('Ssel').AsBoolean then
        begin
          if FMainID = '' then
          begin
            FMainID := Trim(CDS_Main.fieldbyname('mainID').AsString);
          end
          else
          begin
            if Trim(CDS_Main.fieldbyname('mainID').AsString) <> FMainID then
            begin
//              application.MessageBox('选择的不是同一个指示单，不能一起打印！', '提示信息', 0);
//              ADOQueryCmd.Connection.RollbackTrans;
//              EnableControls;
//              exit;
            end;
          end;
          sqlStr := sqlStr + 'insert into TBSubID(SubId,Dname) ';
          sqlStr := sqlStr + ' values( ';
          sqlStr := sqlStr + ' ''' + Trim(CDS_Main.fieldbyname('CKDH').AsString) + ''', ';
          sqlStr := sqlStr + ' ''' + Trim(DCode) + ''' ';
          sqlStr := sqlStr + ' ) ';
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;

  CDS_Main.EnableControls;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(sqlStr);
//    showmessage(sql.text);
    ExecSQL;
  end;

//  if (trim(ComboBox1.Text) = '检验码单') then
//  begin
//    with ADOQueryTemp do
//    begin
//      Close;
//      sql.Clear;
//      sql.add('exec P_Print_CKMD_DZD ');
//
//      sql.add('@DName=' + quotedstr(Trim(CDS_Main.fieldbyname('CKDH').AsString)));
//      Open;
//    end;
//    SCreateCDS20(ADOQueryTemp, CDS_HZ);
//    SInitCDSData20(ADOQueryTemp, CDS_HZ);

  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.add('exec P_Print_CKMD_DZD ');
    sql.add('@begdate=''' + FormatDateTime('yyyy-MM-dd', BegDate.DateTime) + '''');
    sql.add(',@enddate=''' + FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1) + '''');
    sql.add(',@DName=' + quotedstr(Trim(DCode)));
//      sql.add(',@DName=' + quotedstr(Trim(KH.text)));
//        showmessage(sql.Text);
    Open;
  end;
//  end;
                                              
  if FileExists(fPrintFile) then            
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;

end;

procedure TfrmCKProductJYHZList.ToolButton6Click(Sender: TObject);
var
  fPrintFile, fPrintFile10, FMainID: string;
  sqlStr: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据', '提示', 0);
    Exit;
  end;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\总账单.rmf';

  ExportFtErpFile('总账单.rmf', ADOQueryCmd);

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete TBSubID where DName=''' + Trim(DCode) + '''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('SELECT * FROM TBSubID where 1=2 ');
    open;
  end;
  sqlStr := '';
  FMainID := '';
  CDS_Main.DisableControls;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      First;
      while not Eof do
      begin
        if Fieldbyname('Ssel').AsBoolean then
        begin
          if FMainID = '' then
          begin
            FMainID := Trim(CDS_Main.fieldbyname('mainID').AsString);
          end
          else
          begin
            if Trim(CDS_Main.fieldbyname('mainID').AsString) <> FMainID then
            begin
//              application.MessageBox('选择的不是同一个指示单，不能一起打印！', '提示信息', 0);
//              ADOQueryCmd.Connection.RollbackTrans;
//              EnableControls;
//              exit;
            end;
          end;
          sqlStr := sqlStr + 'insert into TBSubID(SubId,Dname) ';
          sqlStr := sqlStr + ' values( ';
          sqlStr := sqlStr + ' ''' + Trim(CDS_Main.fieldbyname('CKDH').AsString) + ''', ';
          sqlStr := sqlStr + ' ''' + Trim(DCode) + ''' ';
          sqlStr := sqlStr + ' ) ';
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;

  CDS_Main.EnableControls;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(sqlStr);
//    showmessage(sql.text);
    ExecSQL;
  end;

//  if (trim(ComboBox1.Text) = '检验码单') then
//  begin
//    with ADOQueryTemp do
//    begin
//      Close;
//      sql.Clear;
//      sql.add('exec P_Print_CKMD_DZD ');
//
//      sql.add('@DName=' + quotedstr(Trim(CDS_Main.fieldbyname('CKDH').AsString)));
//      Open;
//    end;
//    SCreateCDS20(ADOQueryTemp, CDS_HZ);
//    SInitCDSData20(ADOQueryTemp, CDS_HZ);

  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;                           
    sql.add('exec P_Print_CKMD_ZZD ');
    sql.add('@begdate=''' + FormatDateTime('yyyy-MM-dd', BegDate.DateTime) + '''');
    sql.add(',@enddate=''' + FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1) + '''');
    sql.add(',@DName=' + quotedstr(Trim(DCode)));
//      sql.add(',@DName=' + quotedstr(Trim(KH.text)));
//        showmessage(sql.Text);
    Open;
  end;
//  end;
                                                       
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);                 
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;

end;

procedure TfrmCKProductJYHZList.N1Click(Sender: TObject);
begin
  SelOKNoFiler(Tv1, True);
end;

procedure TfrmCKProductJYHZList.N2Click(Sender: TObject);
begin
  SelOKNoFiler(Tv1, False);

end;

end.

