unit U_CPKCCX;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, StdCtrls, ExtCtrls, ADODB, DBClient,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls, cxGridCustomView,
  cxGrid, MovePanel, cxCheckBox, Menus, ComCtrls, BtnEdit, ShellAPI, cxTextEdit,
  RM_Common, RM_Class, RM_GridReport, RM_System, RM_Dataset, RM_e_Xls,
  cxCalendar, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxSkinsCore,
  dxSkinsDefaultPainters, dxSkinscxPCPainter;

type
  TfrmCPKCCX = class(TForm)
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    DS_MainSel: TDataSource;
    cxGridPopupMenu4: TcxGridPopupMenu;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    CDS_YanSe: TClientDataSet;
    RMXLSExport1: TRMXLSExport;
    RMDBMX: TRMDBDataSet;
    RM1: TRMGridReport;
    DS_YanSe: TDataSource;
    Panel1: TPanel;
    Panel3: TPanel;
    Label5: TLabel;
    OrderNo: TEdit;
    cxGrid5: TcxGrid;
    TV5: TcxGridDBTableView;
    v1Column12: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    cxGrid5Level1: TcxGridLevel;
    Button2: TButton;
    Button1: TButton;
    Panel2: TPanel;
    Button3: TButton;
    Button4: TButton;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    cxGridLevel2: TcxGridLevel;
    Panel4: TPanel;
    V1Column15: TcxGridDBColumn;
    V1Column17: TcxGridDBColumn;
    V1Column19: TcxGridDBColumn;
    V1Column20: TcxGridDBColumn;
    V1Column21: TcxGridDBColumn;
    V1Column22: TcxGridDBColumn;
    V1Column23: TcxGridDBColumn;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBTableView;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    Panel6: TPanel;
    Panel5: TPanel;
    Panel7: TPanel;
    Panel8: TPanel;
    LabHZ: TLabel;
    CDS_MainSel: TClientDataSet;
    CDS_GangHao: TClientDataSet;
    CDS_TuoTuan: TClientDataSet;
    DS_GangHao: TDataSource;
    DS_TuoTuan: TDataSource;
    V1Column10: TcxGridDBColumn;
    V2Column2: TcxGridDBColumn;
    V3Column1: TcxGridDBColumn;
    V3Column2: TcxGridDBColumn;
    V2Column4: TcxGridDBColumn;
    V2Column7: TcxGridDBColumn;
    V1Column11: TcxGridDBColumn;
    V2Column8: TcxGridDBColumn;
    V3Column3: TcxGridDBColumn;
    CDS_YanSeSel: TClientDataSet;
    CDS_GangHaoSel: TClientDataSet;
    CDS_TuoTuanSel: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxGridPopupMenu3: TcxGridPopupMenu;
    btnCX: TButton;
    DataSource6: TDataSource;
    ClientDataSet6: TClientDataSet;
    cxStyleRepository2: TcxStyleRepository;
    cxStyle2: TcxStyle;
    TV5Column1: TcxGridDBColumn;
    PopupMenu2: TPopupMenu;
    MenuItem1: TMenuItem;
    N2: TMenuItem;
    pmCDS_YanSe: TPopupMenu;
    MenuItem2: TMenuItem;
    MenuItem3: TMenuItem;
    pmGangHao: TPopupMenu;
    MenuItem4: TMenuItem;
    MenuItem5: TMenuItem;
    pmTuoTuan: TPopupMenu;
    MenuItem6: TMenuItem;
    MenuItem7: TMenuItem;
    Label1: TLabel;
    MPRTCodeName: TEdit;
    Label2: TLabel;
    MPRTMF: TEdit;
    Label3: TLabel;
    MPRTKZ: TEdit;
    Label4: TLabel;
    lidan: TEdit;
    Label6: TLabel;
    ConNo: TEdit;
    Label7: TLabel;
    ywy: TEdit;
    Label8: TLabel;
    MainId: TEdit;
    TV5Column2: TcxGridDBColumn;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    Button8: TButton;
    RMXLSExport2: TRMXLSExport;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure btnCXClick(Sender: TObject);
    procedure V1Column10PropertiesEditValueChanged(Sender: TObject);
    procedure V2Column2PropertiesEditValueChanged(Sender: TObject);
    procedure V3Column1PropertiesEditValueChanged(Sender: TObject);
    procedure v1Column12PropertiesEditValueChanged(Sender: TObject);
    procedure MenuItem1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure MenuItem4Click(Sender: TObject);
    procedure MenuItem5Click(Sender: TObject);
    procedure MenuItem2Click(Sender: TObject);
    procedure MenuItem3Click(Sender: TObject);
    procedure MenuItem6Click(Sender: TObject);
    procedure MenuItem7Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
  private
    { Private declarations }
    fsjYS, fsjGH, fsjTP: string;
    XZPS: Integer;
    XZMS, XZKg, XZYS: Double;
    procedure InitKC();
    procedure initMXKC();
    procedure initHZData();
    procedure initLab();
  public
    { Public declarations }
  end;

var
  frmCPKCCX: TfrmCPKCCX;

implementation

uses
  U_DataLink, U_Fun, U_RTFun, MMSystem, U_FHSQListSel, U_CPKCHZSel;

{$R *.dfm}

procedure TfrmCPKCCX.InitKC();
var
  fwhere, Pwhere: string;
begin

  Pwhere := SGetFilters(Panel1, 1, 2);
  if trim(Pwhere) <> '' then
    fwhere := fwhere + ' where ' + trim(Pwhere);

  with ADOQueryTemp do
  begin
    Filtered := False;
    Close;
    sql.Clear;
    sql.Add(' Select AA.* ');
    sql.Add(' ,B.OrderNo,B.MPRTCode,B.MPRTCodeName,B.MPRTMF,B.MPRTKZ ');
    sql.Add('  ,C.PRTColor,C.PRTEColor,C.PRTHX,C.PrtKuanNo,C.PRTColorNo ');
    sql.Add('  from(select A.MainId,A.Subid,QtyY=sum(Cast(A.Qty*1.0936 as decimal(18,1))),QtyM=sum(Qty),QtyKg=Sum(KgQty),PS=Count(*) ');
    sql.Add('  from  CK_BanCP_CR A inner join CK_BanCP_KC D on A.MJID=D.MJID ');
    sql.Add('  where A.CRFlag=''入库'' and A.CPType=''正品'' and D.KCValid=''Y''  ');
    sql.Add('  and A.MainId=' + QUOTEDSTR(MainId.Text));
    sql.Add('  Group by A.MainId,A.Subid) AA ');
    sql.Add('  inner join JYOrder_Main B on AA.MainId=B.MainId  ');
    sql.Add('  inner join JYOrder_Sub C on AA.SubId=C.SubId  ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_YanSe);
  SInitCDSData20(ADOQueryTemp, CDS_YanSe);
//  SelOKNo(CDS_YanSe, True);

  with ADOQueryTemp do
  begin
    Filtered := False;
    Close;
    sql.Clear;
    sql.Add(' Select AA.* ');
    sql.Add(' ,B.OrderNo,B.MPRTCode,B.MPRTCodeName,B.MPRTMF,B.MPRTKZ ');
    sql.Add('  ,C.PRTColor,C.PRTEColor,C.PRTHX,C.PrtKuanNo,C.PRTColorNo ');
    sql.Add('  from(select A.MainId,A.Subid,A.APID,A.GangHao,QtyY=sum(Cast(A.Qty*1.0936 as decimal(18,1))),QtyM=sum(Qty),QtyKg=Sum(KgQty),PS=Count(*) ');
    sql.Add('  from  CK_BanCP_CR A inner join CK_BanCP_KC D on A.MJID=D.MJID ');
    sql.Add('  where A.CRFlag=''入库'' and A.CPType=''正品'' and D.KCValid=''Y''  ');
    sql.Add('  and A.MainId=' + QUOTEDSTR(MainId.Text));
    sql.Add('  Group by A.MainId,A.Subid,A.APID,A.GangHao) AA ');
    sql.Add('  inner join JYOrder_Main B on AA.MainId=B.MainId  ');
    sql.Add('  inner join JYOrder_Sub C on AA.SubId=C.SubId  ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_GangHao);
  SInitCDSData20(ADOQueryTemp, CDS_GangHao);
//  SelOKNo(CDS_GangHao, True);

  with ADOQueryTemp do
  begin
    Filtered := False;
    Close;
    sql.Clear;
    sql.Add(' Select AA.* ');
    sql.Add(' ,B.OrderNo,B.MPRTCode,B.MPRTCodeName,B.MPRTMF,B.MPRTKZ ');
    sql.Add('  from(select A.MainId,A.RKOrdID,A.KuangHao,QtyY=sum(Cast(A.Qty*1.0936 as decimal(18,1))),QtyM=sum(Qty),QtyKg=Sum(KgQty),PS=Count(*) ');
    sql.Add('  from  CK_BanCP_CR A inner join CK_BanCP_KC D on A.MJID=D.MJID ');
    sql.Add('  where A.CRFlag=''入库''   and A.CPType=''正品'' and D.KCValid=''Y''  ');
    sql.Add('  and A.MainId=' + QUOTEDSTR(MainId.Text));
    sql.Add('  Group by A.MainId,A.RKOrdID,A.KuangHao) AA ');
    sql.Add('  inner join JYOrder_Main B on AA.MainId=B.MainId  ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_TuoTuan);
  SInitCDSData20(ADOQueryTemp, CDS_TuoTuan);
//  SelOKNo(CDS_TuoTuan, True);

  with ADOQueryTemp do
  begin
    Filtered := False;
    Close;
    sql.Clear;

    sql.Add('  select A.*,QtyY=Cast(A.Qty*1.0936 as decimal(18,1)) ');
    sql.Add(' ,B.OrderNo,B.MPRTCode,B.MPRTCodeName,B.MPRTMF,B.MPRTKZ ');
    sql.Add('  ,C.PRTColor,C.PRTEColor,C.PRTHX,C.PrtKuanNo ');
    sql.Add('  from  CK_BanCP_CR A inner join CK_BanCP_KC D on A.MJID=D.MJID ');
    sql.Add('  inner join JYOrder_Main B on A.MainId=B.MainId  ');
    sql.Add('   inner join JYOrder_Sub C on A.SubId=C.SubId  ');
    sql.Add('   where A.CRFlag=''入库'' and A.CPType=''正品'' and D.KCValid=''Y''  ');
    sql.Add('  and A.MainId=' + QUOTEDSTR(MainId.Text));

    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_MainSel);
  SInitCDSData20(ADOQueryTemp, CDS_MainSel);
//  SelOKNo(CDS_MainSel, True);

end;

procedure TfrmCPKCCX.initMXKC();
var
  fsj: string;
begin
  fsj := '';
  with CDS_YanSeSel do
  begin
    First;
    while not eof do
    begin
      if Trim(fsj) = '' then
      begin
        fsj := fsj + '''' + Trim(fieldbyname('SubId').AsString) + '''';
      end
      else
        fsj := fsj + ',' + '''' + Trim(fieldbyname('SubId').AsString) + '''';
      Next;
    end;
  end;
  fsjYS := fsj;

  fsj := '';
  with CDS_GangHaoSel do
  begin
    First;
    while not eof do
    begin
      if Trim(fsj) = '' then
      begin
        fsj := fsj + '''' + Trim(fieldbyname('APId').AsString) + '''';
      end
      else
        fsj := fsj + ',' + '''' + Trim(fieldbyname('APId').AsString) + '''';
      Next;
    end;
  end;
  fsjGH := fsj;

  fsj := '';
  with CDS_TuoTuanSel do
  begin
    First;
    while not eof do
    begin
      if Trim(fsj) = '' then
      begin
        fsj := fsj + '''' + Trim(fieldbyname('RKOrdID').AsString) + '''';
      end
      else
        fsj := fsj + ',' + '''' + Trim(fieldbyname('RKOrdID').AsString) + '''';
      Next;
    end;
  end;
  fsjTP := fsj;

  with ADOQueryTemp do
  begin
    Filtered := False;
    Close;
    sql.Clear;
    sql.Add('  select A.*,QtyY=Cast(A.Qty*1.0936 as decimal(18,1)) ');
    sql.Add(' ,B.OrderNo,B.MPRTCode,B.MPRTCodeName,B.MPRTMF,B.MPRTKZ ');
    sql.Add('  ,C.PRTColor,C.PRTEColor,C.PRTHX,C.PrtKuanNo ');
    sql.Add('  from  CK_BanCP_CR A inner join CK_BanCP_KC D on A.MJID=D.MJID ');
    sql.Add('  inner join JYOrder_Main B on A.MainId=B.MainId  ');
    sql.Add('   inner join JYOrder_Sub C on A.SubId=C.SubId  ');
    sql.Add('   where A.CRFlag=''入库'' and A.CPType=''正品'' and D.KCValid=''Y''  ');
    sql.Add('  and A.MainId=' + QUOTEDSTR(MainId.Text));
    if Trim(fsjYS) <> '' then
    begin
      sql.Add('  and A.Subid in(' + Trim(fsjYS) + ')');
    end;
    if Trim(fsjGH) <> '' then
    begin
      sql.Add('  and A.APid in(' + Trim(fsjGH) + ')');
    end;
    if Trim(fsjTP) <> '' then
    begin
      sql.Add('  and A.RKOrdID in(' + Trim(fsjTP) + ')');
    end;
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_MainSel);
  SInitCDSData20(ADOQueryTemp, CDS_MainSel);
  SelOKNo(CDS_MainSel, True);
  initHZData();
  initLab();
end;

procedure TfrmCPKCCX.initHZData();
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('  select PS=count(*),QtyY=sum(MJYLen),QtyKg=sum(KgQty),QtyM=sum(Qty) ');
    sql.Add('  from  CK_BanCP_CR A inner join CK_BanCP_KC D on A.MJID=D.MJID ');
    sql.Add('   where A.CRFlag=''入库'' and A.CPType=''正品'' and D.KCValid=''Y''  ');
    sql.Add('  and A.MainId=' + QUOTEDSTR(MainId.Text));
    if Trim(fsjYS) <> '' then
    begin
      sql.Add('  and A.Subid in(' + Trim(fsjYS) + ')');
    end;
    if Trim(fsjGH) <> '' then
    begin
      sql.Add('  and A.APid in(' + Trim(fsjGH) + ')');
    end;
    if Trim(fsjTP) <> '' then
    begin
      sql.Add('  and A.RKOrdID in(' + Trim(fsjTP) + ')');
    end;
    Open;
  end;
  if Trim(ADOQueryTemp.fieldbyname('PS').AsString) <> '' then
  begin
    XZPS := ADOQueryTemp.fieldbyname('PS').Value;
  end
  else
  begin
    XZPS := 0;
  end;
  if Trim(ADOQueryTemp.fieldbyname('QtyM').AsString) <> '' then
  begin
    XZMS := ADOQueryTemp.fieldbyname('QtyM').Value;
  end
  else
  begin
    XZMS := 0;
  end;
  if Trim(ADOQueryTemp.fieldbyname('QtyKg').AsString) <> '' then
  begin
    XZKg := ADOQueryTemp.fieldbyname('QtyKg').Value;
  end
  else
  begin
    XZKg := 0;
  end;
  if Trim(ADOQueryTemp.fieldbyname('QtyY').AsString) <> '' then
  begin
    XZYS := ADOQueryTemp.fieldbyname('QtyY').Value;
  end
  else
  begin
    XZYS := 0;
  end;
end;

procedure TfrmCPKCCX.initLab();
begin
  LabHZ.Caption := '选中：' + inttostr(XZPS) + '匹,' + ' ' + FloatToStr(XZMS) + '米,' + ' ' + FloatToStr(XZYS) + '码,' + ' ' + FloatToStr(XZKg) + '公斤';
end;

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

procedure TfrmCPKCCX.FormDestroy(Sender: TObject);
begin
  frmCPKCCX := nil;
end;

procedure TfrmCPKCCX.FormShow(Sender: TObject);
begin

  ReadCxGrid('订单库存查询1', Tv1, '成品仓库');
  ReadCxGrid('订单库存查询2', Tv2, '成品仓库');
  ReadCxGrid('订单库存查询3', Tv3, '成品仓库');
  ReadCxGrid('订单库存查询5', Tv5, '成品仓库');
end;

procedure TfrmCPKCCX.Button2Click(Sender: TObject);
begin
  Close;
  WriteCxGrid('订单库存查询1', Tv1, '成品仓库');
  WriteCxGrid('订单库存查询2', Tv2, '成品仓库');
  WriteCxGrid('订单库存查询3', Tv3, '成品仓库');
  WriteCxGrid('订单库存查询5', Tv5, '成品仓库');
end;

procedure TfrmCPKCCX.Button1Click(Sender: TObject);
var
  MKey: Char;
begin

  try
    frmCPKCHZSel := TfrmCPKCHZSel.Create(self);
    with frmCPKCHZSel do
    begin
      if ShowModal = 1 then
      begin
        self.orderNo.Text := Trim(CDS_Main.fieldbyname('orderNo').AsString);
      end;
    end;
  finally
    frmCPKCHZSel.Free;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main where orderNo=' + QUOTEDSTR(OrderNo.Text));
    Open;
  end;
  SCSHDataNew(ADOQueryTemp, Panel4, 1);

  fsjYS := '';
  fsjGH := '';
  fsjTP := '';

  btnCX.Click;
end;

procedure TfrmCPKCCX.btnCXClick(Sender: TObject);
begin
  if Trim(OrderNo.Text) = '' then
    Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select SubId=CAST('''' AS VARCHAR(60)) ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_YanSeSel);

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select APId=CAST('''' AS VARCHAR(60))  ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_GangHaoSel);

  with ADOQueryTemp do
  begin
    Filtered := False;
    Close;
    sql.Clear;
    sql.Add(' select RKOrdID =CAST('''' AS VARCHAR(60)) ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_TuoTuanSel);

  InitKC();
  initHZData();
  initLab();
end;

procedure TfrmCPKCCX.V1Column10PropertiesEditValueChanged(Sender: TObject);
var
  mvalue: Boolean;
  fsj: string;
begin
  mvalue := TcxCheckBox(Sender).EditingValue;
  with CDS_YanSe do
  begin
    Edit;
    FieldByName('SSel').Value := mvalue;
  end;
  if mvalue = True then
  begin
    with CDS_YanSeSel do
    begin
      Append;
      FieldByName('SubId').Value := Trim(CDS_YanSe.fieldbyname('SubId').AsString);
      Post;
    end;
  end
  else
  begin
    CDS_YanSeSel.Locate('SubId', Trim(CDS_YanSe.fieldbyname('SubId').AsString), []);
    CDS_YanSeSel.Delete;
  end;
  fsj := '';
  with CDS_YanSeSel do
  begin
    First;
    while not eof do
    begin
      if Trim(fsj) = '' then
      begin
        fsj := fsj + '''' + Trim(fieldbyname('SubId').AsString) + '''';
      end
      else
        fsj := fsj + ',' + '''' + Trim(fieldbyname('SubId').AsString) + '''';
      Next;
    end;
  end;
  with ADOQueryTemp do
  begin
    Filtered := False;
    Close;
    sql.Clear;
    sql.Add(' Select AA.* ');
    sql.Add(' ,B.OrderNo,B.MPRTCode,B.MPRTCodeName,B.MPRTMF,B.MPRTKZ ');
    sql.Add('  ,C.PRTColor,C.PRTEColor,C.PRTHX,C.PrtKuanNo,C.PRTColorNo ');
    sql.Add('  from(select A.MainId,A.Subid,A.GangHao,A.APID,QtyY=sum(Cast(A.Qty*1.0936 as decimal(18,1))),QtyM=sum(Qty),QtyKg=Sum(KgQty),PS=Count(*) ');
    sql.Add('  from  CK_BanCP_CR A inner join CK_BanCP_KC D on A.MJID=D.MJID ');
    sql.Add('  where A.CRFlag=''入库'' and D.KCValid=''Y'' and A.CPType=''正品'' ');
    sql.Add('  and A.MainId=' + QUOTEDSTR(MainId.Text));
    if Trim(fsj) <> '' then
    begin
      sql.Add('  and A.Subid in(' + Trim(fsj) + ')');
    end
    else
    begin
      sql.Add('  and 1=2');
    end;
    sql.Add('  Group by A.MainId,A.Subid,A.GangHao,A.APID) AA ');
    sql.Add('  inner join JYOrder_Main B on AA.MainId=B.MainId  ');
    sql.Add('  inner join JYOrder_Sub C on AA.SubId=C.SubId  ');
//    ShowMessage(SQL.Text);
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_GangHao);
  SInitCDSData20(ADOQueryTemp, CDS_GangHao);
//  SelOKNo(CDS_GangHao, True);

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select APId=Cast(Null as varchar(20))');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_GangHaoSel);
  SInitCDSData20(ADOQueryTemp, CDS_GangHaoSel);
  if CDS_GangHaoSel.IsEmpty = False then
  begin
    CDS_GangHaoSel.Delete;
  end;
  CDS_GangHao.DisableControls;
  with CDS_GangHao do
  begin
    First;
    while not Eof do
    begin
      if FieldByName('SSel').AsBoolean = True then
      begin
        with CDS_GangHaoSel do
        begin
          Append;
          CDS_GangHaoSel.FieldByName('APID').Value := CDS_GangHao.FieldByName('APID').Value;
          Post;
        end;
      end;
      Next;
    end;
  end;
  CDS_GangHao.EnableControls;
  initMXKC();
end;

procedure TfrmCPKCCX.V2Column2PropertiesEditValueChanged(Sender: TObject);
var
  mvalue: Boolean;
begin
  mvalue := TcxCheckBox(Sender).EditingValue;
  with CDS_GangHao do
  begin
    Edit;
    FieldByName('SSel').Value := mvalue;
  end;
  if mvalue = True then
  begin
    with CDS_GangHaoSel do
    begin
      Append;
      FieldByName('APId').Value := Trim(CDS_GangHao.fieldbyname('APId').AsString);
      Post;
    end;
  end
  else
  begin
    CDS_GangHaoSel.Locate('APId', Trim(CDS_GangHao.fieldbyname('APId').AsString), []);
    CDS_GangHaoSel.Delete;
  end;
  initMXKC();
end;

procedure TfrmCPKCCX.V3Column1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue: Boolean;
begin
  mvalue := TcxCheckBox(Sender).EditingValue;
  with CDS_TuoTuan do
  begin
    Edit;
    FieldByName('SSel').Value := mvalue;
  end;
  if mvalue = True then
  begin
    with CDS_TuoTuanSel do
    begin
      Append;
      FieldByName('RKOrdID').Value := Trim(CDS_TuoTuan.fieldbyname('RKOrdID').AsString);
      Post;
    end;
  end
  else
  begin
    CDS_TuoTuanSel.Locate('RKOrdID', Trim(CDS_TuoTuan.fieldbyname('RKOrdID').AsString), []);
    CDS_TuoTuanSel.Delete;
  end;
  initMXKC();
end;

procedure TfrmCPKCCX.v1Column12PropertiesEditValueChanged(Sender: TObject);
var
  mvalue: Boolean;
begin
  mvalue := TcxCheckBox(Sender).EditingValue;
  with CDS_MainSel do
  begin
    Edit;
    FieldByName('SSel').Value := mvalue;
    Post;
  end;
  if mvalue = False then
  begin
    XZPS := XZPS - 1;
    XZMS := XZMS - CDS_MainSel.fieldbyname('Qty').Value;
    XZYS := XZYS - CDS_MainSel.fieldbyname('MJYLen').Value;
    XZKg := XZKg - CDS_MainSel.fieldbyname('KGQty').Value;
  end
  else
  begin
    XZPS := XZPS + 1;
    XZMS := XZMS + CDS_MainSel.fieldbyname('Qty').Value;
    XZYS := XZYS + CDS_MainSel.fieldbyname('MJYLen').Value;
    XZKg := XZKg + CDS_MainSel.fieldbyname('KGQty').Value;
  end;
  initLab();
end;

procedure TfrmCPKCCX.MenuItem1Click(Sender: TObject);
begin
  SelOKNo(CDS_MainSel, True);
//  XZPS := 0;
//  XZMS := 0;
//  XZYS := 0;
//  XZKg := 0;
//  initLab();
end;

procedure TfrmCPKCCX.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_MainSel, false);
  XZPS := 0;
  XZMS := 0;
  XZYS := 0;
  XZKg := 0;
  initLab();
end;

procedure TfrmCPKCCX.MenuItem4Click(Sender: TObject);
begin
  with CDS_GangHao do
  begin
    CDS_GangHao.DisableControls;
    First;
    while not Eof do
    begin
      if not CDS_GangHao.FieldByName('SSel').AsBoolean then
      begin
        with CDS_GangHao do
        begin
          Edit;
          FieldByName('SSel').Value := True;
          Post
        end;
        with CDS_GangHaoSel do
        begin
          Append;
          FieldByName('APId').Value := Trim(CDS_GangHao.fieldbyname('APId').AsString);
          Post;
        end;
      end;
      Next;
    end;
    CDS_GangHao.EnableControls;
  end;

  initMXKC();
end;

procedure TfrmCPKCCX.MenuItem5Click(Sender: TObject);
begin
  with CDS_GangHao do
  begin
    CDS_GangHao.DisableControls;
    First;
    while not Eof do
    begin
      if CDS_GangHao.FieldByName('SSel').AsBoolean then
      begin
        with CDS_GangHao do
        begin
          Edit;
          FieldByName('SSel').Value := False;
          Post
        end;
      end;
      Next;
    end;
    CDS_GangHao.EnableControls;
  end;
  CDS_GangHaoSel.EmptyDataSet;
  initMXKC();
end;

procedure TfrmCPKCCX.MenuItem2Click(Sender: TObject);
var
  fsj: string;
begin

  with CDS_YanSe do
  begin
    CDS_YanSe.DisableControls;
    First;
    while not Eof do
    begin
      if not CDS_YanSe.FieldByName('SSel').AsBoolean then
      begin
        with CDS_YanSe do
        begin
          Edit;
          FieldByName('SSel').Value := True;
          Post
        end;
        with CDS_YanSeSel do
        begin
          Append;
          FieldByName('SubId').Value := Trim(CDS_YanSe.fieldbyname('SubId').AsString);
          Post;
        end;
      end;
      Next;
    end;
    CDS_YanSe.EnableControls;
  end;

  fsj := '';
  with CDS_YanSeSel do
  begin
    First;
    while not eof do
    begin
      if Trim(fsj) = '' then
      begin
        fsj := fsj + '''' + Trim(fieldbyname('SubId').AsString) + '''';
      end
      else
        fsj := fsj + ',' + '''' + Trim(fieldbyname('SubId').AsString) + '''';
      Next;
    end;
  end;
  with ADOQueryTemp do
  begin
    Filtered := False;
    Close;
    sql.Clear;
    sql.Add(' Select AA.* ');
    sql.Add(' ,B.OrderNo,B.MPRTCode,B.MPRTCodeName,B.MPRTMF,B.MPRTKZ ');
    sql.Add('  ,C.PRTColor,C.PRTEColor,C.PRTHX,C.PrtKuanNo,C.PRTColorNo ');
    sql.Add('  from(select A.MainId,A.Subid,A.GangHao,A.APID,QtyY=sum(Cast(A.Qty*1.0936 as decimal(18,1))),QtyM=sum(Qty),QtyKg=Sum(KgQty),PS=Count(*) ');
    sql.Add('  from  CK_BanCP_CR A inner join CK_BanCP_KC D on A.MJID=D.MJID ');
    sql.Add('  where A.CRFlag=''入库'' and D.KCValid=''Y'' and A.CPType=''正品'' ');
    sql.Add('  and A.MainId=' + QUOTEDSTR(MainId.Text));
    if Trim(fsj) <> '' then
    begin
      sql.Add('  and A.Subid in(' + Trim(fsj) + ')');
    end
    else
    begin
      sql.Add('  and 1=2');
    end;
    sql.Add('  Group by A.MainId,A.Subid,A.GangHao,A.APID) AA ');
    sql.Add('  inner join JYOrder_Main B on AA.MainId=B.MainId  ');
    sql.Add('  inner join JYOrder_Sub C on AA.SubId=C.SubId  ');
//    ShowMessage(SQL.Text);
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_GangHao);
  SInitCDSData20(ADOQueryTemp, CDS_GangHao);
//  SelOKNo(CDS_GangHao, True);

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select APId=Cast(Null as varchar(20))');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_GangHaoSel);
  SInitCDSData20(ADOQueryTemp, CDS_GangHaoSel);
  if CDS_GangHaoSel.IsEmpty = False then
  begin
    CDS_GangHaoSel.Delete;
  end;
  CDS_GangHao.DisableControls;
  with CDS_GangHao do
  begin
    First;
    while not Eof do
    begin
      if FieldByName('SSel').AsBoolean = True then
      begin
        with CDS_GangHaoSel do
        begin
          Append;
          CDS_GangHaoSel.FieldByName('APID').Value := CDS_GangHao.FieldByName('APID').Value;
          Post;
        end;
      end;
      Next;
    end;
  end;
  CDS_GangHao.EnableControls;
  initMXKC();
end;

procedure TfrmCPKCCX.MenuItem3Click(Sender: TObject);
var
  fsj: string;
begin

  with CDS_YanSe do
  begin
    CDS_YanSe.DisableControls;
    First;
    while not Eof do
    begin
      if CDS_YanSe.FieldByName('SSel').AsBoolean then
      begin
        with CDS_YanSe do
        begin
          Edit;
          FieldByName('SSel').Value := False;
          Post
        end;

      end;
      Next;
    end;
    CDS_YanSe.EnableControls;
  end;
  CDS_YanSeSel.EmptyDataSet;
  fsj := '';

  with ADOQueryTemp do
  begin
    Filtered := False;
    Close;
    sql.Clear;
    sql.Add(' Select AA.* ');
    sql.Add(' ,B.OrderNo,B.MPRTCode,B.MPRTCodeName,B.MPRTMF,B.MPRTKZ ');
    sql.Add('  ,C.PRTColor,C.PRTEColor,C.PRTHX,C.PrtKuanNo,C.PRTColorNo ');
    sql.Add('  from(select A.MainId,A.Subid,A.GangHao,A.APID,QtyY=sum(Cast(A.Qty*1.0936 as decimal(18,1))),QtyM=sum(Qty),QtyKg=Sum(KgQty),PS=Count(*) ');
    sql.Add('  from  CK_BanCP_CR A inner join CK_BanCP_KC D on A.MJID=D.MJID ');
    sql.Add('  where A.CRFlag=''入库'' and D.KCValid=''Y'' and A.CPType=''正品'' ');
    sql.Add('  and A.MainId=' + QUOTEDSTR(MainId.Text));
    if Trim(fsj) <> '' then
    begin
      sql.Add('  and A.Subid in(' + Trim(fsj) + ')');
    end
    else
    begin
      sql.Add('  and 1=2');
    end;
    sql.Add('  Group by A.MainId,A.Subid,A.GangHao,A.APID) AA ');
    sql.Add('  inner join JYOrder_Main B on AA.MainId=B.MainId  ');
    sql.Add('  inner join JYOrder_Sub C on AA.SubId=C.SubId  ');
//    ShowMessage(SQL.Text);
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_GangHao);
  SInitCDSData20(ADOQueryTemp, CDS_GangHao);
//  SelOKNo(CDS_GangHao, True);

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select APId=Cast(Null as varchar(20))');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_GangHaoSel);
  SInitCDSData20(ADOQueryTemp, CDS_GangHaoSel);
  if CDS_GangHaoSel.IsEmpty = False then
  begin
    CDS_GangHaoSel.Delete;
  end;
  CDS_GangHao.DisableControls;
  with CDS_GangHao do
  begin
    First;
    while not Eof do
    begin
      if FieldByName('SSel').AsBoolean = True then
      begin
        with CDS_GangHaoSel do
        begin
          Append;
          CDS_GangHaoSel.FieldByName('APID').Value := CDS_GangHao.FieldByName('APID').Value;
          Post;
        end;
      end;
      Next;
    end;
  end;
  CDS_GangHao.EnableControls;
  initMXKC();
end;

procedure TfrmCPKCCX.MenuItem6Click(Sender: TObject);
begin

  with CDS_TuoTuan do
  begin
    CDS_TuoTuan.DisableControls;
    First;
    while not Eof do
    begin
      if not CDS_TuoTuan.FieldByName('SSel').AsBoolean then
      begin
        with CDS_TuoTuan do
        begin
          Edit;
          FieldByName('SSel').Value := True;
          Post
        end;
        with CDS_TuoTuanSel do
        begin
          Append;
          FieldByName('RKOrdID').Value := Trim(CDS_TuoTuan.fieldbyname('RKOrdID').AsString);
          Post;
        end;
      end;
      Next;
    end;
    CDS_TuoTuan.EnableControls;
  end;

  initMXKC();
end;

procedure TfrmCPKCCX.MenuItem7Click(Sender: TObject);
begin

  with CDS_TuoTuan do
  begin
    CDS_TuoTuan.DisableControls;
    First;
    while not Eof do
    begin
      if CDS_TuoTuan.FieldByName('SSel').AsBoolean then
      begin
        with CDS_TuoTuan do
        begin
          Edit;
          FieldByName('SSel').Value := False;
          Post
        end;
      end;
      Next;
    end;
    CDS_TuoTuan.EnableControls;
  end;
  CDS_GangHaoSel.EmptyDataSet;
  initMXKC();
end;

procedure TfrmCPKCCX.Button5Click(Sender: TObject);
begin
  if CDS_YanSe.IsEmpty then
    Exit;
  TcxGridToExcel('颜色库存', cxGrid1);
//  SelExportData(Tv1, ADOQueryTemp, '颜色库存查询');
end;

procedure TfrmCPKCCX.Button6Click(Sender: TObject);
begin
  if CDS_GangHao.IsEmpty then
    Exit;
  TcxGridToExcel('缸号库存', cxGrid2);
//  SelExportData(Tv2, ADOQueryTemp, '缸号库存查询');
end;

procedure TfrmCPKCCX.Button7Click(Sender: TObject);
begin
  if CDS_TuoTuan.IsEmpty then
    Exit;
  TcxGridToExcel('托盘库存', cxGrid3);
//  SelExportData(Tv3, ADOQueryTemp, '托盘库存查询');
end;

procedure TfrmCPKCCX.Button8Click(Sender: TObject);
begin
  if CDS_MainSel.IsEmpty then
    Exit;
  TcxGridToExcel('明细库存', cxGrid5);
//  SelExportData(Tv5, ADOQueryTemp, '明细库存查询');

end;

end.

