unit U_CKProductBCPOutth;

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, RM_Common, RM_Class,
  RM_e_Xls, RM_Dataset, RM_System, RM_GridReport, cxCheckBox, Menus,
  MovePanel, BtnEdit;

type
  TfrmCKProductBCPOutth = 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;
    Label4: TLabel;
    PRTCodeName: TEdit;
    PRTColor: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    Tv1: TcxGridDBTableView;
    cxGrid2Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    Label5: TLabel;
    orderNo: TEdit;
    Label6: TLabel;
    MJID: TEdit;
    v1Column7: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    Label8: TLabel;
    MPRTKZ: TEdit;
    Label9: TLabel;
    MPRTMF: TEdit;
    Label7: TLabel;
    CPType: TComboBox;
    v1Column3: TcxGridDBColumn;
    CDS_PRT: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    v1Column4: TcxGridDBColumn;
    Label10: TLabel;
    CkOrdNo: TEdit;
    v1Column5: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    RMDBHZ: TRMDBDataSet;
    CDS_HZ: TClientDataSet;
    TBCKCX: TToolButton;
    v1Column11: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    MovePanel2: TMovePanel;
    CDS_CX: TClientDataSet;
    v1CRTYPE: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1gangNo: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    ThreeColorBase: TcxStyleRepository;
    SHuangSe: TcxStyle;
    SkyBlue: TcxStyle;
    Default: TcxStyle;
    QHuangSe: TcxStyle;
    Red: TcxStyle;
    FontBlue: TcxStyle;
    TextSHuangSe: TcxStyle;
    FonePurple: TcxStyle;
    FoneClMaroon: TcxStyle;
    FoneRed: TcxStyle;
    RowColor: TcxStyle;
    handBlack: TcxStyle;
    cxBlue: TcxStyle;
    ToolButton1: TToolButton;
    Panel2: TPanel;
    GroupBox1: TGroupBox;
    Label11: TLabel;
    CRTIME: TDateTimePicker;
    Label12: TLabel;
    kcKw: TBtnEditA;
    v1customerNoname: TcxGridDBColumn;
    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 PRTCodeNameChange(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure orderNoChange(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBCKCXClick(Sender: TObject);
    procedure orderNoKeyPress(Sender: TObject; var Key: Char);
    procedure kcKwBtnClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
  private
    procedure InitGrid();
    function  YSData(Order_Main10:TClientDataSet):Boolean;
    function savedate():boolean;
    { Private declarations }
  public
    fkeyNo:string;
    { Public declarations }
  end;

var
  frmCKProductBCPOutth: TfrmCKProductBCPOutth;

implementation
uses
  U_DataLink,U_Fun, U_Printckmd, U_ZDYHelp;

{$R *.dfm}
function TfrmCKProductBCPOutth.YSData(Order_Main10:TClientDataSet):Boolean;
var
  CRID,YFID,Price,PriceUnit,OrderUnit,FComTaiTou:String;
begin
  Result:=False;
  with Order_Main10 do
  begin
      with ADOQueryTemp do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from YF_Money_KC where FactoryName='''+Trim(Order_Main10.fieldbyname('CustomerNoName').AsString)+'''');
        Open;
      end;
    if not ADOQueryTemp.IsEmpty then
    begin
      CRID:=ADOQueryTemp.fieldbyname('CRID').AsString;
    end
    else
    begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update YF_Money_CRID set CRID=CRID+1');
          sql.Add('select * from YF_Money_CRID ');
          Open;
        end;
        CRID:=ADOQueryCmd.fieldbyname('CRID').AsString;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from YF_Money_KC where 1<>1');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Append;
          FieldByName('CRID').Value:=StrToInt(CRID);
          FieldByName('FactoryName').Value:=Trim(Order_Main10.fieldbyname('CustomerNoName').AsString);
          FieldByName('ZdyStr1').Value:='应收收';
          Post;
        end;
    end;
    with ADOQueryTemp do
    begin
        Close;
        sql.Clear;
        sql.Add('select * from YF_Money_CR ');
        sql.Add(' where MainID='''+Trim(Order_Main10.fieldbyname('MainID').AsString)+'''');
        sql.Add(' and subID='''+Trim(Order_Main10.fieldbyname('subID').AsString)+'''');
        sql.Add(' and YFName=''退货入库'' ');
        Open;
    end;
      if ADOQueryTemp.IsEmpty then
      begin
        if GetLSNo(ADOQueryCmd,YFID,'CS','YF_Money_CR',3,1)=False then
        begin
          Application.MessageBox('取坯布应付最大号失败!','提示',0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from YF_Money_CR where 1<>1');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Append;
          FieldByName('YFID').Value:=Trim(YFID);
          FieldByName('YFTypeId').Value:=Trim(Order_Main10.fieldbyname('MainId').AsString);
          FieldByName('CRID').Value:=StrToInt(CRID);
          FieldByName('Filler').Value:=Trim(DName);
          FieldByName('CRType').Value:='应收款登记';
          FieldByName('CRFlag').Value:='应收收';
          FieldByName('QtyFlag').Value:=1;
          FieldByName('FactoryName').Value:=Trim(Order_Main10.fieldbyname('CustomerNoName').AsString);
          FieldByName('CRTime').Value:=Trim(FormatDateTime('yyyy-MM-dd',date()));
          FieldByName('YFType').Value:='自动生成';
          FieldByName('Price').Value:=Order_Main10.fieldbyname('PRTPrice').Value;
          //FieldByName('HuiLv').Value:=1;
          FieldByName('HuiLv').Value:=1;
          FieldByName('BZType').Value:='人民币';
          FieldByName('QtyUnit').Value:=Trim(Order_Main10.fieldbyname('QtyUnit').AsString);
          FieldByName('ComTaiTou').Value:=Trim(Order_Main10.fieldbyname('customerNoname').AsString);
          FieldByName('YFName').Value:='退货入库';
          FieldByName('MainId').Value:=Trim(Order_Main10.fieldbyname('Mainid').AsString);
          FieldByName('subID').Value:=Trim(Order_Main10.fieldbyname('subID').AsString);
          Post;
        end;
      end
      else
      begin
        YFID:=Trim(ADOQueryTemp.fieldbyname('YFID').AsString);
      end;

      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('update YF_Money_CR Set Qty=(select  -1*isnull(Sum(Qty),0) from CK_BanCP_CR A  ');
        sql.Add(' where A.defstr1=YF_Money_CR.mainID and  A.defstr2=YF_Money_CR.subiD and A.CRType=''退货入库''  )');
        sql.Add(',PS=(select -1* isnull(count(*),0) from CK_BanCP_CR A  ');
        sql.Add(' where A.defstr1=YF_Money_CR.mainID and  A.defstr2=YF_Money_CR.subiD and A.CRType=''退货入库''  )');
        sql.Add(' where YFID='''+Trim(YFID)+'''');
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update YF_Money_CR Set Money=Price*Qty,BBMoney=Price*Qty*HuiLv');
        sql.Add(' where YFID='''+Trim(YFID)+'''');
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update YF_Money_KC Set KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
        sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
        sql.Add(' where CRID='+CRID);
        ExecSQL;
      end;
  end;
  Result:=True;
end;

function TfrmCKProductBCPOutth.savedate():boolean;
var
  maxno:String;
  CRID:Integer;
  MaxCkNo:String;
begin
  result:=false;
  ADOQueryCmd.Connection.BeginTrans;
  CDS_Main.DisableControls;
  try
    if GetLSNo(ADOQueryCmd,fkeyNo,'RK','CK_BanCP_CR',4,1)=False then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取成品仓库最大号失败！','提示',0);
      Exit;
    end;

    with CDS_Main do
    begin
      DisableControls;
      First;
      while not Eof do
      begin
        if fieldbyname('ssel').AsBoolean then
        begin
          with ADOQueryCmd  do
          begin
            Close;
            sql.Clear;
            SQL.add('Update CK_BanCP_CRID Set CRID=CRID+1');
            sql.Add('select * from CK_BanCP_CRID');
            Open;
          end;
          CRID:=ADOQueryCmd.fieldbyname('CRID').AsInteger;
          if GetLSNo(ADOQueryCmd,MaxCkNo,'JR','CK_BanCP_CR',4,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Exit;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CK_BanCP_CR ');
            sql.Add('where 1=2 ');
            Open;
          end;
          with ADOQueryCmd do
          begin
            if IsEmpty then Append else edit;
            FieldByName('MainId').Value:=Trim(CDS_Main.fieldbyname('PMainId').AsString);
            FieldByName('SubId').Value:=Trim(CDS_Main.fieldbyname('PSubId').AsString);
            FieldByName('C_Code').Value:=Trim(CDS_Main.fieldbyname('C_Code').AsString);
            FieldByName('C_CodeName').Value:=Trim(CDS_Main.fieldbyname('C_CodeName').AsString);
            FieldByName('C_SPec').Value:=Trim(CDS_Main.fieldbyname('C_SPec').AsString);
            FieldByName('C_Color').Value:=Trim(CDS_Main.fieldbyname('C_Color').AsString);
            FieldByName('APID').Value:=Trim(CDS_Main.fieldbyname('APID').AsString);
            FieldByName('MJID').Value:=Trim(CDS_Main.fieldbyname('MJId').AsString);
            FieldByName('MJxh').Value:=Trim(CDS_Main.fieldbyname('MJxh').AsString);
            FieldByName('kcKw').Value:=Trim(CDS_Main.fieldbyname('kcKw').AsString);
            FieldByName('RCGangNo').Value:=Trim(CDS_Main.fieldbyname('RCGangNo').AsString);
            FieldByName('BCGangNo').Value:=Trim(CDS_Main.fieldbyname('BCGangNo').AsString);
            FieldByName('BCID').Value:=Trim(MaxCkNo);
            FieldByName('ckordNo').Value:=Trim(fkeyNo);
            FieldByName('CRTime').Value:=FormatDateTime('yyyy-MM-dd',CRTime.DateTime);
            FieldByName('CRFlag').Value:='入库';
            FieldByName('CRType').Value:='退货入库';
            //FieldByName('JTType').Value:=Trim(XJFlag);
            FieldByName('CRID').Value:=CRID;
            FieldByName('KGQty').Value:=CDS_Main.fieldbyname('KGQty').AsFloat;
            FieldByName('Qty').Value:=CDS_Main.fieldbyname('T_Qty').AsFloat;
            FieldByName('RollNum').Value:=CDS_Main.fieldbyname('T_RollNum').AsFloat;
            FieldByName('QtyUnit').Value:=CDS_Main.fieldbyname('QtyUnit').Value;
            FieldByName('CPType').Value:=Trim(CDS_Main.fieldbyname('CPType').AsString);
            FieldByName('Filler').Value:=Trim(DName);
            FieldByName('FillTime').Value:=SGetServerDateTime(ADOQueryTemp);
            FieldByName('kcKw').Value:=Trim(kcKw.Text);
            FieldByName('Thcust').Value:=Trim(CDS_Main.fieldbyname('customerNoname').AsString);
            FieldByName('defstr1').Value:=Trim(CDS_Main.fieldbyname('mainID').AsString);
            FieldByName('defstr2').Value:=Trim(CDS_Main.fieldbyname('SubID').AsString);
            Post;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CK_BanCP_KC ');
            sql.Add('where 1=2 ');
            Open;
          end;
          with ADOQueryCmd do
          begin
            if IsEmpty then Append else edit;
            FieldByName('CRID').Value:=CRID;
            FieldByName('BCID').Value:=Trim(MaxCkNo);
            FieldByName('MJID').Value:=Trim(CDS_Main.fieldbyname('MJID').AsString);
            FieldByName('MJxh').Value:=Trim(CDS_Main.fieldbyname('MJxh').AsString);
            FieldByName('kcKw').Value:=Trim(CDS_Main.fieldbyname('kcKw').AsString);
            FieldByName('KCKGQty').Value:=0;
            if IsEmpty then
            begin
              FieldByName('KCQty').Value:=CDS_Main.fieldbyname('Qty').AsFloat;
              FieldByName('KCRollNum').Value:=CDS_Main.fieldbyname('RollNum').AsFloat;
            end
            else
            begin
              FieldByName('KCQty').Value:=FieldByName('KCQty').AsFloat+ CDS_Main.fieldbyname('Qty').AsFloat;
              FieldByName('KCRollNum').Value:=FieldByName('KCRollNum').AsFloat+ CDS_Main.fieldbyname('RollNum').AsFloat;
            end;
            FieldByName('KCQtyUnit').Value:=CDS_Main.fieldbyname('QtyUnit').Value;
            FieldByName('kcKw').Value:=Trim(kcKw.Text);
            Post;
          end;

          if trim(CDS_Main.fieldbyname('CRType').AsString)='配货出库' then
          begin
            IF not YSdata(CDS_Main) then
            begin
               CDS_Main.EnableControls;
               ADOQueryCmd.Connection.RollbackTrans;
               exit;
            end;
          end;
        end;
       CDS_Main.Next;
      end;
    end;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    result:=true;
  except
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
  end;
end;

procedure TfrmCKProductBCPOutth.FormDestroy(Sender: TObject);
begin
  frmCKProductBCPOutth:=nil;
end;

procedure TfrmCKProductBCPOutth.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:=cahide;
end;

procedure TfrmCKProductBCPOutth.FormCreate(Sender: TObject);
begin
  cxGrid2.Align:=alClient;
  BegDate.DateTime:=SGetServerDateTime(ADOQueryTemp);
  EndDate.DateTime:=BegDate.DateTime;
  crTime.DateTime:=BegDate.DateTime;
end;

procedure TfrmCKProductBCPOutth.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      SQL.Add('select A.*,T_rollNum=A.RollNum,T_Qty=A.Qty,B.OrderNo,ISNULL(C.PRTCode,A.C_Code) as PRTCode,ISNULL(C.PRTCodeName,A.C_COdeName) as PRTCodeName,C.PRTSpec,isnull(C.prtColor,A.C_Color) as prtColor,B.MPRTMF,B.MPRTKZ,B.customerNoname,C.PRTPrice ');
      sql.Add(',isnull(customerNoName,B.OrderNo) KHName');
      sql.Add(',E.AOrdDefStr1,E.gangNo,F.PmainID,F.PsubID');
      sql.add('from CK_BanCP_CR A ');
      Sql.add(' LEFT join JYOrder_Main B on A.MainId=B.MainId');
      Sql.add(' LEFT join JYOrder_Sub C on A.SubId=C.SubId');
      Sql.add(' LEFT join WFB_MJJY D on A.MJId=D.MJId');
      sql.Add(' LEFT join JYOrder_Sub_AnPai E on A.APID=E.APID');
      sql.Add(' LEFT join CK_BanCP_PH F on F.PHID=A.PHID');
      sql.add('where A.CRTime>=:begdate and A.CRTime<:enddate');
      SQL.Add(' and CRflag  =''出库'' ');
      Parameters.ParamByName('begdate').Value:=Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime));
      Parameters.ParamByName('enddate').Value:=Trim(FormatDateTime('yyyy-MM-dd',enddate.DateTime+1));
      Open;
      //ShowMessage(SQL.Text);
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCKProductBCPOutth.TBRafreshClick(Sender: TObject);
begin
  BegDate.SetFocus;
  InitGrid();
end;

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

procedure TfrmCKProductBCPOutth.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(SELF.Caption,Tv1,'成品仓库');
  Close;
end;

procedure TfrmCKProductBCPOutth.FormShow(Sender: TObject);
begin
  ReadCxGrid(SELF.Caption,Tv1,'成品仓库');
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select Cast('''' as varchar(20)) MainId,Cast('''' as varchar(80)) KHName,Cast(0 as decimal(18,4)) PRTPrice ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_CX);
  SInitCDSData20(ADOQueryTemp,CDS_CX);
  CDS_CX.Delete;
  //InitGrid();
 { if Trim(DParameters2)='管理' then
  begin
    TBCKCX.Visible:=True;
  end;  }
end;

procedure TfrmCKProductBCPOutth.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then exit;
  SelExportData(Tv1,ADOQueryMain,'');
end;

procedure TfrmCKProductBCPOutth.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 TfrmCKProductBCPOutth.PRTCodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCKProductBCPOutth.TBPrintClick(Sender: TObject);
var
  fPrintFile:String;
begin
  if CDS_Main.IsEmpty then Exit;
  frmPrintckmd:=TfrmPrintckmd.create(self);
  with frmPrintckmd do
  begin
    flag:=0;
    fckNo:= Trim(self.CDS_Main.fieldbyname('ckordNo').AsString);
    showmodal;
    free;
  end;
{  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\出库码单.rmf' ;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select AA.* from (select A.*,');
    sql.Add('GangNo=(select AOrdDefStr1 from JYOrder_Sub_AnPai B where B.APID=A.APID),');
    sql.Add('OrderNo=(select OrderNo from JYOrder_Main B where B.MainID=A.MainID),');
    sql.Add('SOrddefstr4=(select SOrddefstr4 from JYOrder_Sub B where B.SubID=A.SubID),');
    sql.Add('PRTHX=(select PRTHX from JYOrder_Sub B where B.SubID=A.SubID),');
    SQL.Add('TaiTou=(select Top 1 Note from KH_Zdy B where B.ZdyName=DD.OrdDefStr2 and Type=''OrdDefStr2'' ),');
    sql.Add('MJXH=(select MJXH from WFB_MJJY B where B.MJID=A.MJID)');
    sql.Add(' from CK_BanCP_CR A');
    sql.Add(' inner join JYOrder_Main DD on A.MainId=DD.MainId');
    sql.Add(' where A.MainId='''+Trim(CDS_Main.fieldbyname('MainId').AsString)+'''');
    if Trim(CDS_Main.FieldByName('CKOrdNo').AsString)<>'' then
    begin
      sql.Add(' and A.CKOrdNo='''+Trim(CDS_Main.FieldByName('CKOrdNo').AsString)+'''');
    end;
    sql.Add(' and CRType=''正常出库'')AA ');
    sql.Add(' order by SOrddefstr4,GangNo,MJXH ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_PRT);
  SInitCDSData20(ADOQueryTemp,CDS_PRT);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select * from (');
    sql.Add('select GangNo,OrderNo,SOrddefstr4,PRTHX,Sum(Qty) Qty,Count(*) PS ');
    SQL.Add('from (select A.Qty,');
    sql.Add('GangNo=(select AOrdDefStr1 from JYOrder_Sub_AnPai B where B.APID=A.APID),');
    sql.Add('OrderNo=(select OrderNo from JYOrder_Main B where B.MainID=A.MainID),');
    sql.Add('PRTHX=(select PRTHX from JYOrder_Sub B where B.SubID=A.SubID),');
    sql.Add('SOrddefstr4=(select SOrddefstr4 from JYOrder_Sub B where B.SubID=A.SubID)');
    sql.Add(' from CK_BanCP_CR A');
    sql.Add(' inner join JYOrder_Main DD on A.MainId=DD.MainId');
    sql.Add(' where A.MainId='''+Trim(CDS_Main.fieldbyname('MainId').AsString)+'''');
    if Trim(CDS_Main.FieldByName('CKOrdNo').AsString)<>'' then
    begin
      sql.Add(' and A.CKOrdNo='''+Trim(CDS_Main.FieldByName('CKOrdNo').AsString)+'''');
    end;
    sql.Add(' and CRType=''正常出库'')AA ');
    sql.Add('Group by GangNo,OrderNo,SOrddefstr4,PRTHX');
    sql.Add(')AAA order by SOrddefstr4,PRTHX,Cast(GangNo as int)');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_HZ);
  SInitCDSData20(ADOQueryTemp,CDS_HZ);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['begindate']:=begindate.DateTime;
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    //RMVariables['printer']:=Trim(gUserName);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\出库码单.rmf'),'提示',0);
  end;    }
end;

procedure TfrmCKProductBCPOutth.orderNoChange(Sender: TObject);
begin
//  if Length(orderNo.Text)<4 then Exit;
  TBFind.Click;
end;

procedure TfrmCKProductBCPOutth.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main,True);
end;

procedure TfrmCKProductBCPOutth.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main,False);
end;

procedure TfrmCKProductBCPOutth.TBCKCXClick(Sender: TObject);
var
  FFMainId,FPrice:String;
begin
  if CDS_Main.IsEmpty then Exit;
  if CDS_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要撤销数据吗？','提示',32+4)<>IDYES then Exit;
  MovePanel2.Visible:=True;
  MovePanel2.Refresh;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      //First;
      while CDS_Main.Locate('SSel',True,[])=True do
      begin
        //if CDS_Main.FieldByName('SSel').AsBoolean=True then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('UPDATE CK_BanCp_KC SET KCKgQty =A.KCKgQty + B.KgQty,KCQty =A.KCQty + B.Qty,KCRollNum =A.KCRollNum + B.RollNum ');
            sql.Add('FROM  CK_BanCp_KC A ');
            sql.Add('INNER JOIN CK_BanCP_CR B on B.CRID=A.CRID ');
            sql.Add('WHERE B.BCID ='+Quotedstr(trim(CDS_Main.fieldbyname('BCID').AsString)));

            sql.Add('update JYOrder_sub SET substatus=''0'' ');
            sql.Add('where mainID='+quotedstr(trim(CDS_Main.fieldbyname('mainID').AsString)));
            sql.Add('and subID='+quotedstr(trim(CDS_Main.fieldbyname('subID').AsString)));
            sql.Add('and isnull(substatus,''0'')>''0'' ');

            sql.Add('delete CK_BanCP_CR where BCID='''+Trim(CDS_Main.fieldbyname('BCID').AsString)+'''');

     {       sql.Add('UPdate CK_BanCP_KC Set KCKgQty=(select KgQty from CK_BanCP_CR A where  A.CRID=CK_BanCP_KC.CRID and A.CRFlag=''入库'') ');
            sql.Add(',KCQty=(select Qty from CK_BanCP_CR A where  A.CRID=CK_BanCP_KC.CRID and A.CRFlag=''入库'') ');
            SQL.Add(' where CRID='+CDS_Main.fieldbyname('CRID').AsString);  }
            ExecSQL;
          end;
          {if YSData(CDS_Main)=False then
          begin
            CDS_Main.EnableControls;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('撤销失败!','提示',0);
            Exit;
          end; }
          if Trim(FFMainId)='' then
          begin
            with CDS_CX do
            begin
              Append;
              FieldByName('Mainid').Value:=Trim(CDS_Main.fieldbyname('Mainid').AsString);
              FieldByName('PRTPrice').Value:=CDS_Main.fieldbyname('PRTPrice').Value;
              FieldByName('KHName').Value:=Trim(CDS_Main.fieldbyname('KHName').AsString);
              Post;
            end;
            FFMainId:=Trim(CDS_Main.fieldbyname('Mainid').AsString);
            FPrice:=Trim(CDS_Main.fieldbyname('PRTPrice').AsString);
          end else
          if Trim(FFMainId)<>Trim(CDS_Main.fieldbyname('Mainid').AsString) then
          begin
            with CDS_CX do
            begin
              Append;
              FieldByName('Mainid').Value:=Trim(CDS_Main.fieldbyname('Mainid').AsString);
              FieldByName('KHName').Value:=Trim(CDS_Main.fieldbyname('KHName').AsString);
              FieldByName('PRTPrice').Value:=CDS_Main.fieldbyname('PRTPrice').Value;
              Post;
            end;
            FFMainId:=Trim(CDS_Main.fieldbyname('Mainid').AsString);
            FPrice:=Trim(CDS_Main.fieldbyname('PRTPrice').AsString);
          end else
          begin
            if Trim(FPrice)<>Trim(CDS_Main.fieldbyname('PRTPrice').AsString) then
            begin
              with CDS_CX do
              begin
                Append;
                FieldByName('Mainid').Value:=Trim(CDS_Main.fieldbyname('Mainid').AsString);
                FieldByName('KHName').Value:=Trim(CDS_Main.fieldbyname('KHName').AsString);
                FieldByName('PRTPrice').Value:=CDS_Main.fieldbyname('PRTPrice').Value;
                Post;
              end;
              FPrice:=Trim(CDS_Main.fieldbyname('PRTPrice').AsString);
            end;
          end;
          CDS_Main.Delete;
        end;
      end;
    end;
    CDS_Main.EnableControls;
  {  with CDS_CX do
    begin
      First;
      while not Eof do
      begin
        if YSData(CDS_CX)=False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('撤销失败!','提示',0);
          Exit;
        end;
        Next;
      end;
    end;   }

    ADOQueryCmd.Connection.CommitTrans;
    MovePanel2.Visible:=False;
    Exit;
  except
    MovePanel2.Visible:=False;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('撤销异常！','提示',0);
  end;

end;


procedure TfrmCKProductBCPOutth.orderNoKeyPress(Sender: TObject;
  var Key: Char);
begin
{  if Key=#13 then
  begin
    if Length(Trim(orderNo.Text))<4 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        SQL.Add('select A.*,B.OrderNo,B.MPRTCodeName,C.PRTColor,B.MPRTMF,B.MPRTKZ,D.MJXH,C.PRTPrice');
        sql.Add(',isnull(customerNoName,B.OrderNo) KHName');
        sql.Add(',E.AOrdDefStr1');
        sql.add('from CK_BanCP_CR A ');
        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(' inner join WFB_MJJY D on A.MJId=D.MJId');
        sql.Add(' inner join JYOrder_Sub_AnPai E on A.APID=E.APID');
        sql.add('where B.OrderNo like :orderNo');
        SQL.Add(' and CRType=''正常出库'' ');
        Parameters.ParamByName('orderNo').Value:='%'+Trim(orderNo.Text)+'%';

        Open;
        //ShowMessage(SQL.Text);
      end;
      SCreateCDS20(ADOQueryMain,CDS_Main);
      SInitCDSData20(ADOQueryMain,CDS_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;   }
end;

procedure TfrmCKProductBCPOutth.kcKwBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='CPKWFlag';
      flagname:='库位信息';
      V1Name.Caption:='库位名称';
      V1Note.Caption:='英文名称';
//      MainType:=Trim(DName);
      fnote:=FALSE;
      if ShowModal=1 then
      begin
        kcKw.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCKProductBCPOutth.ToolButton1Click(Sender: TObject);
begin
 if CDS_main.IsEmpty then exit;
  if savedate() then
  begin
    application.MessageBox('数据保存成功！','提示信息');
    ModalResult:=1;
  end
  else
  application.MessageBox('数据保存失败！','提示信息',0);
end;

end.
