unit U_CPXiaoShouList;

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, cxButtonEdit,
  cxDropDownEdit, cxPC, Buttons, cxTextEdit, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxSkinsCore, dxSkinDarkRoom,
  dxSkinOffice2013White, dxSkinSharpPlus, dxSkinSpringTime,
  dxSkinsDefaultPainters, dxSkinscxPCPainter, dxSkinBlack, dxSkinBlue,
  dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkSide,
  dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray,
  dxSkinOffice2013LightGray, dxSkinPumpkin, dxSkinSeven,
  dxSkinSevenClassic, dxSkinSharp, dxSkinSilver, dxSkinStardust,
  dxSkinSummer2008, dxSkinTheAsphaltWorld, dxSkinValentine, dxSkinVS2010,
  dxSkinWhiteprint, dxSkinXmas2008Blue;

type
  TfrmCPXiaoShouList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    ToolButton1: TToolButton;
    Panel1: TPanel;
    Label34: TLabel;
    FactoryName: TEdit;
    Label4: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    ToolButton4: TToolButton;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport2: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    labMYType: TLabel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column1: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    Label9: TLabel;
    YWY: TEdit;
    v1Column23: TcxGridDBColumn;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    DataSource2: TDataSource;
    cxGridPopupMenu2: TcxGridPopupMenu;
    ClientDataSet2: TClientDataSet;
    Panel2: TPanel;
    SpeedButton1: TSpeedButton;
    Image2: TImage;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    CheckBox1: TCheckBox;
    Panel3: TPanel;
    Image1: TImage;
    SpeedButton8: TSpeedButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    v1Ssel: TcxGridDBColumn;
    ADOQuery1: TADOQuery;
    SpeedButton4: TSpeedButton;
    v1Column2: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBExportClick(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 CustomerNoNameChange(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ZJStatusChange(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure SpeedButton3Click(Sender: TObject);
    procedure Tv2MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure Image1Click(Sender: TObject);
    procedure SpeedButton8Click(Sender: TObject);
    procedure SpeedButton4Click(Sender: TObject);
  private
    canshu1: string;
    DQdate: TDateTime;
    procedure InitGrid();
    procedure InitGridKH();
    procedure InitGridSub();
    procedure InitForm();
    function DelData(): Boolean;
    { Private declarations }
  public
    FFInt, FCloth: Integer;

    { Public declarations }
  end;

var
  frmCPXiaoShouList: TfrmCPXiaoShouList;

implementation

uses
  U_DataLink, U_RTFun, U_XiShouInPut, U_ZDYHelp, U_XiShouInPutWKC,
  U_XiaoShouDataOldListQB, U_CKProductBCPOutList, U_XiaoShouDataNewListQB;

{$R *.dfm}

procedure TfrmCPXiaoShouList.FormDestroy(Sender: TObject);
begin
  frmCPXiaoShouList := nil;
end;

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

procedure TfrmCPXiaoShouList.FormCreate(Sender: TObject);
begin
  cxgrid1.Align := alClient;
  canshu1 := Trim(DParameters1);
end;

procedure TfrmCPXiaoShouList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('销售码单登记YXMX', Tv1, '贸易生产管理');
  WriteCxGrid('销售码单登记YXZY', Tv2, '贸易生产管理');
  Close;
end;

procedure TfrmCPXiaoShouList.InitGrid();
begin
  try
    ADOQueryCmd.DisableControls;
    with ADOQueryCmd do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select AA.*,B.* ');
      sql.Add(',KHNameJC=(select KHNameJC from KH_Main KM where KM.KHNO=B.FactoryNo)');
      sql.Add(' from(');
      sql.Add(' select A.SMID,BZType,JQty=sum(JQty),HSQty=sum(HSQty),HZFee=sum(HZFee),Money=sum(Money)');
      sql.Add(' from CK_YCL_CR A ');
      sql.Add(' inner join CK_YCL_CR_Main B on A.SMID=B.SMID ');
      sql.add(' where 1=1');
      if CheckBox1.Checked = True then
      begin
        SQL.Add(' and B.CRTime>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)) + '''');
        sql.Add(' and B.CRTime<''' + Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)) + '''');
        BegDate.Visible := True;
        EndDate.Visible := true;
      end
      else
      begin
        SQL.Add(' and B.CRTime>=''' + Trim(FormatDateTime('yyyy-MM-dd', now)) + '''');
        sql.Add(' and B.CRTime<''' + Trim(FormatDateTime('yyyy-MM-dd', now + 1)) + '''');
        BegDate.Visible := false;
        EndDate.Visible := false;
      end;
      sql.Add(' and isnull(B.CKName,'''')=''销售出库'' ');
      sql.Add(' Group by A.SMID,BZType )AA');
      sql.Add(' inner join CK_YCL_CR_Main B on AA.SMID=B.SMID ');
      SQL.Add(' order by B.CRTime desc');
      Open;
    end;
    SCreateCDS20(ADOQueryCmd, Order_Main);
    SInitCDSData20(ADOQueryCmd, Order_Main);
  finally
    ADOQueryCmd.EnableControls;
  end;
end;

procedure TfrmCPXiaoShouList.InitGridSub();
begin
  try
    ADOQueryTemp.DisableControls;
    with ADOQueryTemp do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select AA.*,B.* ');
      sql.Add(',KHNameJC=(select KHNameJC from KH_Main KM where KM.KHNO=B.FactoryNo)');
      sql.Add(' from(');
      sql.Add(' select A.SMID,BZType,JQty=sum(JQty),HSQty=sum(HSQty),HZFee=sum(HZFee),Money=sum(Money)');
      sql.Add(' from CK_YCL_CR A ');
      sql.Add(' inner join CK_YCL_CR_Main B on A.SMID=B.SMID ');
      sql.Add(' where 1=1');
      if CheckBox1.Checked = true then
      begin
        sql.add(' and B.CRTime>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)) + '''');
        sql.Add(' and B.CRTime<''' + Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)) + '''');
      end
      else
      begin
        sql.add(' and B.CRTime>=''' + Trim(FormatDateTime('yyyy-MM-dd', Now)) + '''');
        sql.Add(' and B.CRTime<''' + Trim(FormatDateTime('yyyy-MM-dd', Now + 1)) + '''');
      end;
      sql.Add(' and isnull(B.CKName,'''')=''销售出库'' ');
      sql.Add(' and isnull(B.FactoryNo,'''')=''' + Trim(ClientDataSet2.fieldbyname('FactoryNo').AsString) + '''');
      sql.Add(' and isnull(B.CRType,'''')=''' + Trim(ClientDataSet2.fieldbyname('CRType').AsString) + '''');
      sql.Add(' and isnull(B.YWY,'''')=''' + Trim(ClientDataSet2.fieldbyname('YWY').AsString) + '''');
      if ClientDataSet2.IsEmpty then
      begin
        sql.Add(' and 1=2');
      end;
      sql.Add(' Group by A.SMID,BZType )AA');
      sql.Add(' inner join CK_YCL_CR_Main B on AA.SMID=B.SMID ');
      SQL.Add(' order by B.CRTime desc');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, Order_Main);
    SInitCDSData20(ADOQueryTemp, Order_Main);
  finally
    ADOQueryTemp.EnableControls;
  end;
end;

procedure TfrmCPXiaoShouList.InitGridKH();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select B.CRType,B.FactoryNo,B.YWY,FactoryName=KHNameJC ');
      sql.Add(' from CK_YCL_CR_Main B ');
      sql.Add(' inner join KH_Main A on B.FactoryNo=A.KHNO');
      sql.Add(' where isnull(B.CKName,'''')=''销售出库'' ');
      sql.Add(' Group by B.CRType,B.FactoryNo,B.YWY,A.KHNameJC');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, ClientDataSet2);
    SInitCDSData20(ADOQueryMain, ClientDataSet2);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCPXiaoShouList.InitForm();
begin
  BegDate.Date := StrToDate(FormatDateTime('yyyy-MM', SGetServerDate(ADOQueryTemp)) + '-01');
  EndDate.Date := SGetServerDate(ADOQueryTemp);
  ReadCxGrid('销售码单登记YXMX', Tv1, '贸易生产管理');
  ReadCxGrid('销售码单登记YXZY', Tv2, '贸易生产管理');
  InitGridKH();
end;

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

procedure TfrmCPXiaoShouList.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YS_Money_CR where YFTypeId in');
    sql.Add('(select SPID from CK_YCL_CR where SMID=''' + Trim(Order_Main.fieldbyname('SMID').AsString) + ''')');
    sql.Add(' and (isnull(ChkerP,'''')<>'''' ');
    sql.Add(' or isnull(ChkerQ,'''')<>'''') ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Application.MessageBox('单价或数量已审核不能删除!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' delete YS_Money_CR where YFTypeId in');
      sql.Add('(select SPID from CK_YCL_CR where SMID=''' + Trim(Order_Main.fieldbyname('SMID').AsString) + ''')');
      sql.Add(' delete CK_YCL_CR where SMID=''' + Trim(Order_Main.fieldbyname('SMID').AsString) + '''');
      sql.Add(' delete CK_YCL_CR_Main where SMID=''' + Trim(Order_Main.fieldbyname('SMID').AsString) + '''');

      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Order_Main.Delete;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('删除异常!', '提示', 0);
  end;

end;

function TfrmCPXiaoShouList.DelData(): Boolean;
begin
  try
    Result := false;
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      //First;
     // while Order_Main.Locate('SSel',True,[]) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' insert into CK_MYSC_CR_Log  ');
          sql.Add(' select * from CK_MYSC_CR where MYID=''' + Trim(Order_Main.fieldbyname('MYID').AsString) + '''');
          sql.Add(' UPdate CK_MYSC_CR_Log Set DelTime=getdate(),DelerCode=''' + Trim(DCode) + ''',Deler=''' + Trim(DName) + '''');
          sql.Add(' where MYID=''' + Trim(Order_Main.fieldbyname('MYID').AsString) + '''');
          sql.Add('Delete CK_MYSC_CR  where MYId=''' + Trim(Order_Main.fieldbyname('MYId').AsString) + '''');
          ExecSQL;
        end;
        Order_Main.Delete;
      end;
    end;
    Order_Main.EnableControls;

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

procedure TfrmCPXiaoShouList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  TcxGridToExcel(Self.Caption, cxGrid1);
end;

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

procedure TfrmCPXiaoShouList.TBAddClick(Sender: TObject);
begin
  Panel2.Visible := True;
end;

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

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

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

procedure TfrmCPXiaoShouList.ToolButton1Click(Sender: TObject);
var
  FFMYType: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  FFMYType := Trim(Order_Main.fieldbyname('CRType').AsString);
  if Trim(FFMYType) = '老系统销售' then
  begin
    try
      frmXiShouInPut := TfrmXiShouInPut.Create(Application);
      with frmXiShouInPut do
      begin
        TBSave.Visible := False;
        PState := 1;
        FMainId := Trim(Self.Order_Main.fieldbyname('SMID').AsString);
        Label21.Caption := FFMYType;
        if ShowModal = 1 then
        begin

        end;
      end;
    finally
      frmXiShouInPut.Free;
    end;
  end
  else
  begin
    try
      frmXiShouInPutWKC := TfrmXiShouInPutWKC.Create(Application);
      with frmXiShouInPutWKC do
      begin
        TBSave.Visible := False;
        PState := 1;
        FMainId := Trim(Self.Order_Main.fieldbyname('SMID').AsString);
        Label21.Caption := FFMYType;
        if ShowModal = 1 then
        begin

        end;
      end;
    finally
      frmXiShouInPutWKC.Free;
    end;
  end;

end;

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

procedure TfrmCPXiaoShouList.N2Click(Sender: TObject);
begin
  SelOKNo(Order_Main, True);
end;

procedure TfrmCPXiaoShouList.N1Click(Sender: TObject);
begin
  speedbutton8.click;
end;

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

procedure TfrmCPXiaoShouList.ZJStatusChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCPXiaoShouList.ToolButton4Click(Sender: TObject);
var
  fPrintFile: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  if Trim(Order_Main.fieldbyname('Chker').AsString) = '' then
    exit;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\检测报告.rmf';
  RMXLSExport2 := TRMXLSExport.Create(RMXLSExport2);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Test_Report where TRId=''' + Trim(Order_Main.fieldbyname('TRId').AsString) + '''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_PRT);
  SInitCDSData20(ADOQueryTemp, CDS_PRT);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['OrderUnit']:=Order_Main.fieldbyname('OrderUnit').Value;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\检测报告.rmf'), '提示', 0);
  end;
end;

procedure TfrmCPXiaoShouList.Tv1DblClick(Sender: TObject);
begin
  frmCKProductBCPOutList := TfrmCKProductBCPOutList.Create(self);
  with frmCKProductBCPOutList do
  begin
    FCKNO := trim(Order_Main.fieldbyname('CKNo').AsString);
    if ShowModal = 1 then
    begin

    end;
    free;
  end;
end;

procedure TfrmCPXiaoShouList.TBEditClick(Sender: TObject);
var
  FFMYType: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YS_Money_CR where YFTypeId in');
    sql.Add('(select SPID from CK_YCL_CR where SMID=''' + Trim(Order_Main.fieldbyname('SMID').AsString) + ''')');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Application.MessageBox('已经生成应收款不能修改!', '提示', 0);
    Exit;
  end;
  FFMYType := Trim(Order_Main.fieldbyname('CRType').AsString);
  if Trim(FFMYType) = '老系统销售' then
  begin
    try
      frmXiShouInPut := TfrmXiShouInPut.Create(Application);
      with frmXiShouInPut do
      begin
        PState := 1;
        FMainId := Trim(Self.Order_Main.fieldbyname('SMID').AsString);
        Label21.Caption := FFMYType;
        Button1.Visible := False;
        if ShowModal = 1 then
        begin
          InitGridSub();
        end;
      end;
    finally
      frmXiShouInPut.Free;
    end;
  end
  else
  begin
    try
      frmXiShouInPutWKC := TfrmXiShouInPutWKC.Create(Application);
      with frmXiShouInPutWKC do
      begin
        PState := 1;
        FMainId := Trim(Self.Order_Main.fieldbyname('SMID').AsString);
        Label21.Caption := FFMYType;
        if ShowModal = 1 then
        begin
          InitGridSub();
        end;
      end;
    finally
      frmXiShouInPutWKC.Free;
    end;
  end;

end;

procedure TfrmCPXiaoShouList.Image2Click(Sender: TObject);
begin
  Panel2.Visible := False;
end;

procedure TfrmCPXiaoShouList.SpeedButton1Click(Sender: TObject);
begin
  Panel2.Visible := False;
  try
    frmXiaoShouDataOldListQB := TfrmXiaoShouDataOldListQB.Create(Application);
    with frmXiaoShouDataOldListQB do
    begin
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmXiaoShouDataOldListQB.Free;
  end;
end;

procedure TfrmCPXiaoShouList.SpeedButton2Click(Sender: TObject);
begin
  Panel2.Visible := False;
  try
    frmXiShouInPutWKC := TfrmXiShouInPutWKC.Create(Application);
    with frmXiShouInPutWKC do
    begin
      PState := 0;
      FMainId := '';
      Label21.Caption := '无库存销售';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmXiShouInPutWKC.Free;
  end;
end;

procedure TfrmCPXiaoShouList.SpeedButton3Click(Sender: TObject);
begin
  Panel2.Visible := False;
  try
    frmXiShouInPutWKC := TfrmXiShouInPutWKC.Create(Application);
    with frmXiShouInPutWKC do
    begin
      PState := 0;
      FMainId := '';
      Label21.Caption := '免单销售';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmXiShouInPutWKC.Free;
  end;
end;

procedure TfrmCPXiaoShouList.Tv2MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  InitGridSub();
end;

procedure TfrmCPXiaoShouList.Image1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  Panel3.Visible := false;
end;

procedure TfrmCPXiaoShouList.SpeedButton8Click(Sender: TObject);
var
  fPrintFile: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  if Order_Main.Locate('ssel', True, []) = false then
  begin
    application.MessageBox('没有选择数据', '提示');
    exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('delete TBSubID where DName=''' + Trim(DCode) + '''');
    ExecSQL;
  end;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\销售码单新.rmf';
  RMXLSExport2 := TRMXLSExport.Create(RMXLSExport2);
  with Order_Main do
  begin
    first;
    while not Eof do
    begin
      if FieldByName('Ssel').AsBoolean = true then
      begin
        with ADOQuery1 do
        begin
          Filtered := False;
          Close;
          sql.Clear;
          sql.Add(' select distinct isnull(D.MJID,C.MJID) MJID');
          sql.Add(' from CK_YCL_CR A ');
          sql.Add(' inner join CK_YCL_CR_Main B on A.SMID=B.SMID ');
          sql.Add(' left join CP_InOut1 C on C.InOutNo=B.CKNo');
          sql.Add(' left join CK_BanCP_CR D on D.CKOrdNo=B.CKNo');
          sql.add(' where B.CKNO=''' + Trim(Order_Main.fieldbyname('CKNO').AsString) + '''');
          Open;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('SELECT * FROM TBSubID where 1=2 ');
          open;
        end;
        with ADOQuery1 do
        begin
          first;
          while not eof do
          begin
            ADOQueryTemp.append;
            ADOQueryTemp.fieldbyname('SubId').Value := Trim(ADOQuery1.fieldbyname('MJID').AsString);
            ADOQueryTemp.fieldbyname('Dname').Value := Trim(DCode);
            ADOQueryTemp.post;
            next;
          end;
        end;
      end;
      next;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.add('exec P_Print_CKMD3 ');
    sql.add('@mainID=' + quotedstr(Trim('')));
    sql.add(',@DName=' + quotedstr(Trim(DCode)));
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_PRT);
  SInitCDSData20(ADOQueryTemp, CDS_PRT);
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\销售码单新.rmf'), '提示', 0);
  end;
end;

procedure TfrmCPXiaoShouList.SpeedButton4Click(Sender: TObject);
begin
  Panel2.Visible := False;
  try
    frmXiaoShouDataNewListQB := TfrmXiaoShouDataNewListQB.Create(Application);
    with frmXiaoShouDataNewListQB do
    begin
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmXiaoShouDataNewListQB.Free;
  end;
end;

end.

