unit U_CKProductBCPOutList;

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, RM_E_llPDF;

type
  TfrmCKProductBCPOutList = 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;
    MPRTCodeName: 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;
    RMXLSExport1: TRMXLSExport;
    v1Column4: TcxGridDBColumn;
    Label10: TLabel;
    CkOrdNo: TEdit;
    v1Column5: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    TBCKCX: TToolButton;
    v1Column11: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    MovePanel2: TMovePanel;
    CDS_CX: TClientDataSet;
    v1Column12: TcxGridDBColumn;
    CDS_BaoNo: TClientDataSet;
    CDS_Juan: TClientDataSet;
    CDS_BaoPRT: TClientDataSet;
    DS_BaoPRT: TDataSource;
    cxGrid1: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v2Column2: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    v2Column8: TcxGridDBColumn;
    v2Column9: TcxGridDBColumn;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Tv3Column1: TcxGridDBColumn;
    Tv3Column2: TcxGridDBColumn;
    Tv3Column3: TcxGridDBColumn;
    Tv3Column4: TcxGridDBColumn;
    v3Column1: TcxGridDBColumn;
    v3Column2: TcxGridDBColumn;
    v3Column3: TcxGridDBColumn;
    v3Column4: TcxGridDBColumn;
    v3Column5: TcxGridDBColumn;
    v3Column6: TcxGridDBColumn;
    v3Column7: TcxGridDBColumn;
    v3Column8: TcxGridDBColumn;
    v3Column9: TcxGridDBColumn;
    v3Column10: TcxGridDBColumn;
    v3Column11: TcxGridDBColumn;
    v3Column12: TcxGridDBColumn;
    DS_JuanPRT: TDataSource;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxGridPopupMenu3: TcxGridPopupMenu;
    PRTColor: TComboBox;
    Label11: TLabel;
    AOrdDefStr1: TComboBox;
    ComboBox1: TComboBox;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    cxGrid4: TcxGrid;
    TV4: TcxGridDBTableView;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridDBColumn20: TcxGridDBColumn;
    cxGridDBColumn21: TcxGridDBColumn;
    cxGridDBColumn24: TcxGridDBColumn;
    cxGridDBColumn25: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    cxGridPopupMenu4: TcxGridPopupMenu;
    RMDBWXPTPrint: TRMDBDataSet;
    CDS_WXPT: TClientDataSet;
    DataSource2: TDataSource;
    CDS_WXPTPrint: TClientDataSet;
    MJXH: TEdit;
    Label12: TLabel;
    CheckBox1: TCheckBox;
    Panel4: TPanel;
    Label13: TLabel;
    Panel10: TPanel;
    Image2: TImage;
    RichEdit1: TRichEdit;
    ToolButton1: TToolButton;
    Panel2: TPanel;
    Label14: TLabel;
    THMJID: TEdit;
    v1PRTColorEng: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    Label15: TLabel;
    Label16: TLabel;
    KHConNo: TEdit;
    Label17: TLabel;
    apid: TEdit;
    v1Column22: TcxGridDBColumn;
    v1styleno: TcxGridDBColumn;
    styleno: TEdit;
    Label18: TLabel;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ADOQueryPrint: TADOQuery;
    CRNote: TEdit;
    Label19: TLabel;
    RMDBHZ: TRMDBDataSet;
    RMDBPRT: TRMDBDataSet;
    CDS_HZ: TClientDataSet;
    CDS_PRT: TClientDataSet;
    RM3: TRMGridReport;
    PRTHX: TEdit;
    Label20: TLabel;
    PRTColorEng: TEdit;
    Label21: TLabel;
    ADOQuery1: TADOQuery;
    RMDBCKHZ: TRMDBDataSet;
    Print_HZ: TClientDataSet;
    RMDBCOL: TRMDBDataSet;
    CDS_COL: TClientDataSet;
    ConNOHT: TEdit;
    v1KHName: TcxGridDBColumn;
    Label22: TLabel;
    v1CRType: TcxGridDBColumn;
    v1MJLen: TcxGridDBColumn;
    v1CRNote: TcxGridDBColumn;
    TDH: TToolButton;
    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 MPRTCodeNameChange(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 Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure PRTColorChange(Sender: TObject);
    procedure AOrdDefStr1Change(Sender: TObject);
    procedure CheckBox1Click(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 Image2Click(Sender: TObject);
    procedure Panel2DblClick(Sender: TObject);
    procedure THMJIDKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ConNOHTChange(Sender: TObject);
    procedure ConNOHTKeyPress(Sender: TObject; var Key: Char);
    procedure KHConNoKeyPress(Sender: TObject; var Key: Char);
    procedure TDHClick(Sender: TObject);
  private
    FLeft,FTop:Integer;
    FPRTColorNo1,FPRTColorNo2,FPRTColorNo3,FPRTColorNo4,FPRTColorNo5:string;
    procedure InitGrid();
    function  YSData(Order_Main10:TClientDataSet):Boolean;
    function  YFData(Order_Main10:TClientDataSet):Boolean;

    procedure Print_WXPT();
    procedure Print_WXPT1();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmCKProductBCPOutList: TfrmCKProductBCPOutList;

implementation
uses
  U_DataLink,U_Fun, U_JYMJIDPRINT;

{$R *.dfm}
procedure TfrmCKProductBCPOutList.Print_WXPT1();
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.add('exec P_Print_CKMD2 ');
    sql.add('@mainID='+quotedstr(Trim(CDS_Main.FieldByName('CKOrdNo').AsString)));
    //sql.add(',@begtime='''+trim(formatdatetime('yyyy-MM-dd',begdate.DateTime))+''' ');
    //sql.add(',@Endtime='''+trim(formatdatetime('yyyy-MM-dd',Enddate.DateTime+1))+''' ');
    sql.Add(',@begtime=''2000-01-01''');
    sql.Add(',@Endtime=''2100-01-01''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_WXPTPrint);
  SInitCDSData20(ADOQueryTemp,CDS_WXPTPrint);
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.add('exec P_Print_CKMD2 ');
    sql.add('@mainID='+quotedstr(Trim(CDS_Main.FieldByName('CKOrdNo').AsString)));
    //sql.add(',@begtime='''+trim(formatdatetime('yyyy-MM-dd',begdate.DateTime))+'''');
    //sql.add(',@Endtime='''+trim(formatdatetime('yyyy-MM-dd',Enddate.DateTime+1))+'''');
    sql.Add(',@begtime=''2000-01-01''');
    sql.Add(',@Endtime=''2100-01-01''');
    sql.add(',@flag=''2'' ');
    Open;
  end;
  SCreateCDS20(ADOQuery1,CDS_COL);
  SInitCDSData20(ADOQuery1,CDS_COL);
end;

function TfrmCKProductBCPOutList.YFData(Order_Main10:TClientDataSet):Boolean;
var
  CRID,OrdMainId,YFID,FComTaiTou,FCRID,FFactoryName,OrderUnit,PriceUnit:String;
begin
  Result:=False;
      with ADOQueryTemp do
      begin
        Close;
        SQL.Clear;
        sql.Add('select Top 1 * from JYOrder_Sub Where Subid='''+Trim(Order_Main10.fieldbyname('Subid').AsString)+'''');
        Open;
      end;
      PriceUnit:=Trim(ADOQueryTemp.fieldbyname('PriceUnit').AsString);
      OrderUnit:=Trim(ADOQueryTemp.fieldbyname('OrderUnit').AsString);
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select ComTaiTou from JYOrder_Main where Mainid='''+Trim(Order_Main10.fieldbyname('MainId').AsString)+'''');
        Open;
      end;
      FComTaiTou:=Trim(ADOQueryTemp.fieldbyname('ComTaiTou').AsString);
      with ADOQueryTemp do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from YF_Money_KC where FactoryName='''+Trim(Order_Main10.fieldbyname('KHName').AsString)+'''');
        Open;
      end;
      if ADOQueryTemp.IsEmpty=False 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=2');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Append;
          FieldByName('CRID').Value:=StrToInt(CRID);
          FieldByName('FactoryName').Value:=Trim(Order_Main10.fieldbyname('KHName').AsString);
          FieldByName('ZdyStr1').Value:='应付付';
          Post;
        end;
      end;
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from YF_Money_CR where FactoryName='''+Trim(Order_Main10.fieldbyname('KHName').AsString)+'''');
        sql.Add(' and YFTypeId='''+Trim(Order_Main10.fieldbyname('Subid').AsString)+'''');
        sql.add(' and CRFlag=''应付付''');
        Open;
      end;
      if ADOQueryTemp.IsEmpty then
      begin
        if GetLSNo(ADOQueryCmd,YFID,'CP','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=2');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Append;
          FieldByName('YFID').Value:=Trim(YFID);
          FieldByName('YFTypeId').Value:=Trim(Order_Main10.fieldbyname('Subid').AsString);
          FieldByName('CRID').Value:=StrToInt(CRID);
          FieldByName('Filler').Value:=Trim(DName);
          FieldByName('status').Value:='0';
          FieldByName('CRType').Value:='应付款登记';
          FieldByName('CRFlag').Value:='应付付';
          FieldByName('QtyFlag').Value:=1;
          FieldByName('FactoryName').Value:=Trim(Order_Main10.fieldbyname('KHName').AsString);
          //fieldbyname('CRTime').Value:=Trim(FormatDateTime('yyyy-MM-dd',CRTime.Date));
          FieldByName('YFType').Value:='自动生成';
          //fieldbyname('OrderNO').Value:=Trim(Order_Main10.fieldbyname('OrderNo').AsString);
          //fieldbyname('PBNote').Value:=trim(CRNote.Text);
          //FieldByName('Price').Value:=Order_Main10.fieldbyname('PRTPrice').Value;
          //FieldByName('HuiLv').Value:=1;
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select Top 1 HuiLv from YF_Money_CR ');
            sql.Add(' where Mainid='''+Trim(Order_Main10.fieldbyname('Mainid').AsString)+'''');
            sql.Add(' and CRType=''付款登记'' ');
            SQL.Add(' and YFDefFlag1=0');
            Open;
          end;
          if ADOQueryTemp.IsEmpty=False then
          begin
            ADOQueryCmd.FieldByName('HuiLv').Value:=ADOQueryTemp.fieldbyname('HuiLv').Value;
          end;
          FieldByName('BZType').Value:='￥';
          fieldbyname('QtyUnit').Value:=Trim(Order_Main10.fieldbyname('QtyUnit').AsString);
          FieldByName('ComTaiTou').Value:=Trim(FComTaiTou);
          FieldByName('YFName').Value:='成品回修金额';
          FieldByName('MainId').Value:=Trim(Order_Main10.fieldbyname('Mainid').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 isnull(Sum(A.KgQty),0) from CK_BanCP_CR A  ');
        sql.Add(' inner join JYOrder_Sub B on A.SubId=B.SubId where');
        SQL.Add(' A.Subid=YF_Money_CR.YFTypeId and A.CRType=''回修出库'' and CPType=''正品''  ');
        sql.add(' and A.CRTime=YF_Money_CR.CRTime');
        SQL.Add(' )');
        sql.Add(',PS=(select isnull(count(*),0) from CK_BanCP_CR A  ');
        sql.Add(' inner join JYOrder_Sub B on A.SubId=B.SubId where');
        SQL.Add(' A.Subid=YF_Money_CR.YFTypeId and A.CRType=''回修出库'' and CPType=''正品''');
        sql.add(' and A.CRTime=YF_Money_CR.CRTime');
        sql.add(') ');

        sql.Add(',JZQty=(select isnull(Sum(MJQty4),0) from CK_BanCP_CR A  ');
        sql.Add(' inner join WFB_MJJY B on A.MJID=B.MJID where');
        SQL.Add(' A.Subid=YF_Money_CR.YFTypeId and A.CRType=''回修出库'' and CPType=''正品''  ');
        sql.add(' and A.CRTime=YF_Money_CR.CRTime');
        SQL.Add(' )');
        sql.Add(',MaoQty=(select isnull(Sum(A.KgQty),0) from CK_BanCP_CR A  ');
        sql.Add(' inner join JYOrder_Sub B on A.SubId=B.SubId where');
        SQL.Add(' A.Subid=YF_Money_CR.YFTypeId and A.CRType=''回修出库'' and CPType=''正品''  ');
        sql.add(' and A.CRTime=YF_Money_CR.CRTime');
        SQL.Add(' )');
        sql.Add(' where YFTypeId='''+Trim(Order_Main10.fieldbyname('Subid').AsString)+'''');
        sql.add(' and CRType=''应付款登记''');
        ExecSQL;
      end;
      
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from YF_Money_CR where YFId='''+Trim(YFID)+'''');
        Open;
      end;
      if ADOQueryTemp.FieldByName('Qty').Value=0 then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('delete from YF_Money_CR where YFID='''+Trim(YFID)+'''');
          ExecSQL;
        end;
      end;
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from YF_Money_CR where CRId='+CRID);
        Open;
      end;
      if ADOQueryTemp.IsEmpty then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('delete from YF_Money_KC where CRId='+CRID);
          ExecSQL;
        end;
      end;
  Result:=True;
end;

procedure TfrmckproductBCPOutList.Print_WXPT();
var
  j,z,i,x:Integer;
  FSumJZQty0,FSumQty0,FSumJZQty1,FSumQty1: Double;
  FGangNo,FHZ,FPRTColorNo:String;
begin
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.add('exec P_Print_CKMD1 ');
    sql.add('@mainID='+quotedstr(Trim(CDS_Main.FieldByName('ConNOHT').AsString)));
    sql.add(',@flag=''1''');
    Open;
  end;
  SCreateCDS20(ADOQueryCmd,CDS_WXPT);
  SInitCDSData20(ADOQueryCmd,CDS_WXPT);
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('select XH=cast(0 as int),SYRName=Cast('''' as varchar(70)),ConNO=Cast('''' as varchar(30)),CustomerNoName=Cast('''' as varchar(50)),');
    sql.Add(' PRTHX=Cast('''' as varchar(50)),BaoNo=Cast('''' as varchar(50)),');
    sql.add(' KG0=Cast(0 as decimal(18,2)),KG1=Cast(0 as decimal(18,2)),KG2=Cast(0 as decimal(18,2)),KG3=Cast(0 as decimal(18,2)),KG4=Cast(0 as decimal(18,2)),');
    sql.add(' Qty0=Cast(0 as decimal(18,2)),Qty1=Cast(0 as decimal(18,2)),Qty2=Cast(0 as decimal(18,2)),Qty3=Cast(0 as decimal(18,2)),Qty4=Cast(0 as decimal(18,2)),');
    sql.Add(' sumBaoKG=cast(0 as decimal(18,2)),sumBaoQty=cast(0 as decimal(18,2))');
    open;
  end;
  SCreateCDS20(ADOQueryCmd,CDS_WXPTPrint);
  SInitCDSData20(ADOQueryCmd,CDS_WXPTPrint);
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.add('select distinct B.PRTColorEng from JYOrder_Sub B ');
    sql.add('where B.Mainid='+quotedstr(Trim(CDS_WXPT.FieldByName('Mainid').AsString)));
    sql.Add(' order by B.PRTColorEng');
    Open;
  end;
  if ADOQueryCmd.RecordCount<>1 then
  begin
    with ADOQueryCmd do
    begin
      First;
      x:=0;
      while not Eof do
      begin
        if x=0 then
        FPRTColorNo1:=Trim(fieldbyname('PRTColorEng').AsString);
        if x=1 then
        FPRTColorNo2:=Trim(fieldbyname('PRTColorEng').AsString);
        if x=2 then
        FPRTColorNo3:=Trim(fieldbyname('PRTColorEng').AsString);
        if x=3 then
        FPRTColorNo4:=Trim(fieldbyname('PRTColorEng').AsString);
        if x=4 then
        FPRTColorNo5:=Trim(fieldbyname('PRTColorEng').AsString);
        x:=x+1;
        Next;
      end;
    end;
  end
  else
  begin
    FPRTColorNo1:=Trim(ADOQueryCmd.fieldbyname('PRTColorEng').AsString);
    FPRTColorNo2:=Trim(ADOQueryCmd.fieldbyname('PRTColorEng').AsString);
    FPRTColorNo3:=Trim(ADOQueryCmd.fieldbyname('PRTColorEng').AsString);
    FPRTColorNo4:=Trim(ADOQueryCmd.fieldbyname('PRTColorEng').AsString);
    FPRTColorNo5:=Trim(ADOQueryCmd.fieldbyname('PRTColorEng').AsString);
  end;
  if CDS_WXPTPrint.IsEmpty=False then
  begin
    CDS_WXPTPrint.Delete;
  end;
  j:=0;z:=0;i:=0;x:=0;FGangNo:='';FPRTColorNo:='';
  FSumJZQty1:=0;FSumQty1:=0;FSumJZQty0:=0;FSumQty0:=0;
  CDS_WXPT.DisableControls;
  CDS_WXPTPrint.DisableControls;
  with CDS_WXPT do
  begin
    First;
    while not Eof do
    begin
      with CDS_WXPTPrint do
      begin
        if CDS_WXPTPrint.IsEmpty then
        begin
          CDS_WXPTPrint.Append;
          CDS_WXPTPrint.FieldByName('XH').Value:=0;
          CDS_WXPTPrint.FieldByName('SYRName').Value:=CDS_WXPT.fieldbyname('SYRName').Value;
          CDS_WXPTPrint.FieldByName('ConNO').Value:=CDS_WXPT.fieldbyname('ConNO').Value;
          CDS_WXPTPrint.FieldByName('CustomerNoName').Value:=CDS_WXPT.fieldbyname('CustomerNoName').Value;
          CDS_WXPTPrint.FieldByName('PRTHX').Value:=CDS_WXPT.fieldbyname('PRTHX').Value;
          CDS_WXPTPrint.FieldByName('BaoNo').Value:=CDS_WXPT.fieldbyname('BaoNo').Value;
          if FPRTColorNo1=CDS_WXPT.fieldbyname('PRTColorNo').Value then
          begin
            CDS_WXPTPrint.FieldByName('KG0').Value:=CDS_WXPT.fieldbyname('KGQty').Value;
            CDS_WXPTPrint.FieldByName('Qty0').Value:=CDS_WXPT.fieldbyname('Qty').Value;
          end
          else
          if FPRTColorNo2=CDS_WXPT.fieldbyname('PRTColorNo').Value then
          begin
            CDS_WXPTPrint.FieldByName('KG1').Value:=CDS_WXPT.fieldbyname('KGQty').Value;
            CDS_WXPTPrint.FieldByName('Qty1').Value:=CDS_WXPT.fieldbyname('Qty').Value;
          end
          else
          if FPRTColorNo3=CDS_WXPT.fieldbyname('PRTColorNo').Value then
          begin
            CDS_WXPTPrint.FieldByName('KG2').Value:=CDS_WXPT.fieldbyname('KGQty').Value;
            CDS_WXPTPrint.FieldByName('Qty2').Value:=CDS_WXPT.fieldbyname('Qty').Value;
          end
          else
          if FPRTColorNo4=CDS_WXPT.fieldbyname('PRTColorNo').Value then
          begin
            CDS_WXPTPrint.FieldByName('KG3').Value:=CDS_WXPT.fieldbyname('KGQty').Value;
            CDS_WXPTPrint.FieldByName('Qty3').Value:=CDS_WXPT.fieldbyname('Qty').Value;
          end
          else
          if FPRTColorNo5=CDS_WXPT.fieldbyname('PRTColorNo').Value then
          begin
            CDS_WXPTPrint.FieldByName('KG4').Value:=CDS_WXPT.fieldbyname('KGQty').Value;
            CDS_WXPTPrint.FieldByName('Qty4').Value:=CDS_WXPT.fieldbyname('Qty').Value;
          end;
          CDS_WXPTPrint.FieldByName('sumBaoKG').Value:=CDS_WXPT.fieldbyname('sumBaoKG').Value;
          CDS_WXPTPrint.FieldByName('sumBaoQty').Value:=CDS_WXPT.fieldbyname('sumBaoQty').AsFloat;
          CDS_WXPTPrint.post;
          Z:=0;      //列
          j:=0;      //行
          FGangNo:=Trim(CDS_WXPT.fieldbyname('BaoNo').AsString)+Trim(CDS_WXPT.fieldbyname('PRTHX').AsString);
          FPRTColorNo:=Trim(CDS_WXPT.fieldbyname('PRTColorNo').AsString);
        end
        else
        begin
          if Z<5 then
          begin
            if FGangNo<>Trim(CDS_WXPT.fieldbyname('BaoNo').AsString)+Trim(CDS_WXPT.fieldbyname('PRTHX').AsString) then
            begin
              CDS_WXPTPrint.Append;
              CDS_WXPTPrint.FieldByName('XH').Value:=j+1;
              CDS_WXPTPrint.FieldByName('SYRName').Value:=CDS_WXPT.fieldbyname('SYRName').Value;
              CDS_WXPTPrint.FieldByName('ConNO').Value:=CDS_WXPT.fieldbyname('ConNO').Value;
              CDS_WXPTPrint.FieldByName('CustomerNoName').Value:=CDS_WXPT.fieldbyname('CustomerNoName').Value;
              CDS_WXPTPrint.FieldByName('PRTHX').Value:=CDS_WXPT.fieldbyname('PRTHX').Value;
              CDS_WXPTPrint.FieldByName('BaoNo').Value:=CDS_WXPT.fieldbyname('BaoNo').Value;
              if FPRTColorNo1=CDS_WXPT.fieldbyname('PRTColorNo').Value then
              begin
                CDS_WXPTPrint.FieldByName('KG0').Value:=CDS_WXPT.fieldbyname('KGQty').Value;
                CDS_WXPTPrint.FieldByName('Qty0').Value:=CDS_WXPT.fieldbyname('Qty').Value;
              end;
              if FPRTColorNo2=CDS_WXPT.fieldbyname('PRTColorNo').Value then
              begin
                CDS_WXPTPrint.FieldByName('KG1').Value:=CDS_WXPT.fieldbyname('KGQty').Value;
                CDS_WXPTPrint.FieldByName('Qty1').Value:=CDS_WXPT.fieldbyname('Qty').Value;
              end;
              if FPRTColorNo3=CDS_WXPT.fieldbyname('PRTColorNo').Value then
              begin
                CDS_WXPTPrint.FieldByName('KG2').Value:=CDS_WXPT.fieldbyname('KGQty').Value;
                CDS_WXPTPrint.FieldByName('Qty2').Value:=CDS_WXPT.fieldbyname('Qty').Value;
              end;
              if FPRTColorNo4=CDS_WXPT.fieldbyname('PRTColorNo').Value then
              begin
                CDS_WXPTPrint.FieldByName('KG3').Value:=CDS_WXPT.fieldbyname('KGQty').Value;
                CDS_WXPTPrint.FieldByName('Qty3').Value:=CDS_WXPT.fieldbyname('Qty').Value;
              end;
              if FPRTColorNo5=CDS_WXPT.fieldbyname('PRTColorNo').Value then
              begin
                CDS_WXPTPrint.FieldByName('KG4').Value:=CDS_WXPT.fieldbyname('KGQty').Value;
                CDS_WXPTPrint.FieldByName('Qty4').Value:=CDS_WXPT.fieldbyname('Qty').Value;
              end;
              CDS_WXPTPrint.FieldByName('sumBaoKG').Value:=CDS_WXPT.fieldbyname('sumBaoKG').Value;
              CDS_WXPTPrint.FieldByName('sumBaoQty').Value:=CDS_WXPT.fieldbyname('sumBaoQty').AsFloat;
              CDS_WXPTPrint.Post;
              Z:=0;
              j:=j+1;
              FGangNo:=Trim(CDS_WXPT.fieldbyname('BaoNo').AsString)+Trim(CDS_WXPT.fieldbyname('PRTHX').AsString);
            end
            else
            begin
              CDS_WXPTPrint.Edit;
              CDS_WXPTPrint.FieldByName('SYRName').Value:=CDS_WXPT.fieldbyname('SYRName').Value;
              CDS_WXPTPrint.FieldByName('ConNO').Value:=CDS_WXPT.fieldbyname('ConNO').Value;
              CDS_WXPTPrint.FieldByName('CustomerNoName').Value:=CDS_WXPT.fieldbyname('CustomerNoName').Value;
              CDS_WXPTPrint.FieldByName('PRTHX').Value:=CDS_WXPT.fieldbyname('PRTHX').Value;
              CDS_WXPTPrint.FieldByName('BaoNo').Value:=CDS_WXPT.fieldbyname('BaoNo').Value;
              if FPRTColorNo1=CDS_WXPT.fieldbyname('PRTColorNo').Value then
              begin
                CDS_WXPTPrint.FieldByName('KG0').Value:=CDS_WXPT.fieldbyname('KGQty').Value;
                CDS_WXPTPrint.FieldByName('Qty0').Value:=CDS_WXPT.fieldbyname('Qty').Value;
              end;
              if FPRTColorNo2=CDS_WXPT.fieldbyname('PRTColorNo').Value then
              begin
                CDS_WXPTPrint.FieldByName('KG1').Value:=CDS_WXPT.fieldbyname('KGQty').Value;
                CDS_WXPTPrint.FieldByName('Qty1').Value:=CDS_WXPT.fieldbyname('Qty').Value;
              end;
              if FPRTColorNo3=CDS_WXPT.fieldbyname('PRTColorNo').Value then
              begin
                CDS_WXPTPrint.FieldByName('KG2').Value:=CDS_WXPT.fieldbyname('KGQty').Value;
                CDS_WXPTPrint.FieldByName('Qty2').Value:=CDS_WXPT.fieldbyname('Qty').Value;
              end;
              if FPRTColorNo4=CDS_WXPT.fieldbyname('PRTColorNo').Value then
              begin
                CDS_WXPTPrint.FieldByName('KG3').Value:=CDS_WXPT.fieldbyname('KGQty').Value;
                CDS_WXPTPrint.FieldByName('Qty3').Value:=CDS_WXPT.fieldbyname('Qty').Value;
              end;
              if FPRTColorNo5=CDS_WXPT.fieldbyname('PRTColorNo').Value then
              begin
                CDS_WXPTPrint.FieldByName('KG4').Value:=CDS_WXPT.fieldbyname('KGQty').Value;
                CDS_WXPTPrint.FieldByName('Qty4').Value:=CDS_WXPT.fieldbyname('Qty').Value;
              end;
              CDS_WXPTPrint.FieldByName('sumBaoKG').Value:=CDS_WXPT.fieldbyname('sumBaoKG').Value;
              CDS_WXPTPrint.FieldByName('sumBaoQty').Value:=CDS_WXPT.fieldbyname('sumBaoQty').AsFloat;
              CDS_WXPTPrint.post;
              Z:=z+1;
            end;
          end
          else
          begin
            CDS_WXPTPrint.Append;
            CDS_WXPTPrint.FieldByName('XH').Value:=j+1;
            CDS_WXPTPrint.FieldByName('SYRName').Value:=CDS_WXPT.fieldbyname('SYRName').Value;
            CDS_WXPTPrint.FieldByName('ConNO').Value:=CDS_WXPT.fieldbyname('ConNO').Value;
            CDS_WXPTPrint.FieldByName('CustomerNoName').Value:=CDS_WXPT.fieldbyname('CustomerNoName').Value;
            CDS_WXPTPrint.FieldByName('PRTHX').Value:=CDS_WXPT.fieldbyname('PRTHX').Value;
            CDS_WXPTPrint.FieldByName('BaoNo').Value:=CDS_WXPT.fieldbyname('BaoNo').Value;
            if FPRTColorNo1=CDS_WXPT.fieldbyname('PRTColorNo').Value then
            begin
              CDS_WXPTPrint.FieldByName('KG0').Value:=CDS_WXPT.fieldbyname('KGQty').Value;
              CDS_WXPTPrint.FieldByName('Qty0').Value:=CDS_WXPT.fieldbyname('Qty').Value;
            end;
            if FPRTColorNo2=CDS_WXPT.fieldbyname('PRTColorNo').Value then
            begin
              CDS_WXPTPrint.FieldByName('KG1').Value:=CDS_WXPT.fieldbyname('KGQty').Value;
              CDS_WXPTPrint.FieldByName('Qty1').Value:=CDS_WXPT.fieldbyname('Qty').Value;
            end;
            if FPRTColorNo3=CDS_WXPT.fieldbyname('PRTColorNo').Value then
            begin
              CDS_WXPTPrint.FieldByName('KG2').Value:=CDS_WXPT.fieldbyname('KGQty').Value;
              CDS_WXPTPrint.FieldByName('Qty2').Value:=CDS_WXPT.fieldbyname('Qty').Value;
            end;
            if FPRTColorNo4=CDS_WXPT.fieldbyname('PRTColorNo').Value then
            begin
              CDS_WXPTPrint.FieldByName('KG3').Value:=CDS_WXPT.fieldbyname('KGQty').Value;
              CDS_WXPTPrint.FieldByName('Qty3').Value:=CDS_WXPT.fieldbyname('Qty').Value;
            end;
            if FPRTColorNo5=CDS_WXPT.fieldbyname('PRTColorNo').Value then
            begin
              CDS_WXPTPrint.FieldByName('KG4').Value:=CDS_WXPT.fieldbyname('KGQty').Value;
              CDS_WXPTPrint.FieldByName('Qty4').Value:=CDS_WXPT.fieldbyname('Qty').Value;
            end;
            CDS_WXPTPrint.FieldByName('sumBaoKG').Value:=CDS_WXPT.fieldbyname('sumBaoKG').Value;
            CDS_WXPTPrint.FieldByName('sumBaoQty').Value:=CDS_WXPT.fieldbyname('sumBaoQty').AsFloat;
            CDS_WXPTPrint.Post;
            Z:=0;
            j:=j+1;
          end;
        end
      end;
      Next;
    end;
  end;
  CDS_WXPT.EnableControls;
  CDS_WXPTPrint.EnableControls;
end;


procedure TfrmCKProductBCPOutList.FormDestroy(Sender: TObject);
begin
  frmCKProductBCPOutList:=nil;
end;


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

procedure TfrmCKProductBCPOutList.FormCreate(Sender: TObject);
begin
  BegDate.DateTime:=SGetServerDateTime(ADOQueryTemp)-3;
  EndDate.DateTime:=SGetServerDateTime(ADOQueryTemp)
end;

procedure TfrmCKProductBCPOutList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      SQL.Add('select B.OrderNo,G.conNO,G.ConNo ConNOHT,G.KHConNO,F.styleno,B.MPRTCodeName,B.MPRTSpec,B.MprtkuanNo,C.PRTColor,C.PRTColorNO SOrddefstr1,C.PRTColorEng,B.MPRTMF,B.MPRTKZ,A.MJXH,C.PRTPrice,C.PRTHX ');
      sql.Add(',A.MJQty4,isnull(B.customerNoName,B.OrderNo) KHName,A.* ');
      sql.add('from CK_BanCP_CR A ');
      Sql.add(' left join JYOrder_Main B on B.MainId=A.MainId');
      Sql.add(' left join JYOrder_Sub C on C.mainID=A.mainID and C.SubId=A.SubId');
      sql.add(' left join JYOrderCon_Sub F on F.Subid=A.ConID ');
      sql.add(' left join JYOrderCon_Main G on G.Mainid=F.Mainid');
      sql.add(' where A.CRTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+'''');
      sql.Add(' and A.CRTime<'''+Trim(FormatDateTime('yyyy-MM-dd',enddate.DateTime+1))+'''');
      SQL.Add(' and A.CRFlag=''出库'' ');
      sql.add(' order by CRTime');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCKProductBCPOutList.TBRafreshClick(Sender: TObject);
begin
  toolbar1.SetFocus;
  InitGrid();
end;

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

procedure TfrmCKProductBCPOutList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('仓库出库列表',Tv1,'成品仓库');
  WriteCxGrid('仓库出库列表2',Tv3,'成品仓库');
  Close;
end;

procedure TfrmCKProductBCPOutList.FormShow(Sender: TObject);
begin
  ReadCxGrid('仓库出库列表',Tv1,'成品仓库');
  ReadCxGrid('仓库出库列表2',Tv3,'成品仓库');
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select Cast('''' as varchar(20)) MainId,Cast('''' as varchar(80)) KHName,Cast('''' as varchar(80)) Conid,');
    SQL.ADD('Cast('''' as varchar(80)) Subid,Cast('''' as varchar(80)) QtyUnit,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;
    ToolButton1.Visible:=true;
    v1Column11.Visible:=True;
  end
  else
  begin
    if trim(DCode)='005' then
    v1KHName.Visible:=true
    else
    v1KHName.Visible:=false;
    v1Column11.Visible:=False;
  end;
  RM3.CanExport:=true;
end;

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

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

procedure TfrmCKProductBCPOutList.MPRTCodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCKProductBCPOutList.TBPrintClick(Sender: TObject);
var
  fPrintFile,fPrintFile10,FMainID,fZMFile:String;
begin
  if CDS_Main.IsEmpty then Exit;
  IF trim(ComboBox1.Text)='出库码单1' then
  begin
    Print_WXPT();
  end;
  IF trim(ComboBox1.Text)='出库码单2' then
  begin
    Print_WXPT1();
  end;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\'+Trim(ComboBox1.Text)+'.rmf' ;
  if FileExists(fPrintFile) then
  begin
    if RM3.CanExport=true then
    begin
      fZMFile:='C:\Users\Administrator\Desktop';
      if not DirectoryExists(fZMFile) then
      begin
        fZMFile:='C:\Documents and Settings\Administrator\桌面\'+trim(CDS_Main.fieldbyname('ConNOHT').AsString)+' DETAILED PL.XLS';
      end
      else
      begin
        fZMFile:='C:\Users\Administrator\Desktop\'+trim(CDS_Main.fieldbyname('ConNOHT').AsString)+' DETAILED PL.XLS';
      end;
      RM3.ExportTo(RMXLSExport1,fZMFile);
    end;
    RM3.CanExport:=true;
    RMVariables['FPRTColorNo1']:=Trim(FPRTColorNo1);
    RMVariables['FPRTColorNo2']:=Trim(FPRTColorNo2);
    RMVariables['FPRTColorNo3']:=Trim(FPRTColorNo3);
    RMVariables['FPRTColorNo4']:=Trim(FPRTColorNo4);
    RMVariables['FPRTColorNo5']:=Trim(FPRTColorNo5);
    RM3.LoadFromFile(fPrintFile);
    RM3.ShowReport;
    RM3.CanExport:=False;
  end
  else
  begin
    Application.MessageBox(PChar('没有找'+fPrintFile),'提示',0);
  end;
end;

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

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

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

procedure TfrmCKProductBCPOutList.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
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update CK_BanCP_CR Set CRTime=NULL');
          sql.Add(',CRType='''',CRFlag=''待出库''');
          sql.Add(',CRNote=''''');
          sql.Add(',CKOrdNo=''''');
          sql.Add(',CRFlagQty=0');
          SQL.Add(',RKPlace='''',BaoNo='''',BaoID='''' ');
          sql.Add('where MJID='''+Trim(CDS_Main.fieldbyname('MJID').AsString)+'''');
          sql.Add(' and CRFlag=''出库''');
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          if CDS_Main.fieldbyname('CRID').AsFloat<>0 then
          begin
            sql.Add('Update WFB_MJJY Set MJStr2=''已入库'' where MJID='''+Trim(CDS_Main.fieldbyname('MJID').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 exists (select MJID from CK_BanCP_CR where MJID=CK_BanCP_KC.MJID and MJID='''+trim(CDS_Main.fieldbyname('MJID').AsString)+''') ');
          end;
          ExecSQL;
        end;
        CDS_Main.Delete;
      end;
    end;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    MovePanel2.Visible:=False;
    Exit;
  except
    MovePanel2.Visible:=False;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('撤销异常！','提示',0);
  end;
end;

function TfrmCKProductBCPOutList.YSData(Order_Main10:TClientDataSet):Boolean;
var
  CRID,YFID,Price,PriceUnit,OrderUnit:String;
begin
  Result:=False;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select Top 1 * from JYOrder_Sub Where Subid='''+Trim(Order_Main10.fieldbyname('Subid').AsString)+'''');
    Open;
  end;
  {Price:=ADOQueryTemp.fieldbyname('PRTPrice').AsString;
  if Trim(Price)='' then
  begin
    Price:='0';
  end; }
  PriceUnit:=Trim(ADOQueryTemp.fieldbyname('PriceUnit').AsString);
  OrderUnit:=Trim(ADOQueryTemp.fieldbyname('OrderUnit').AsString);
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from YF_Money_KC where FactoryName='''+Trim(Order_Main10.fieldbyname('KHName').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False 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('KHName').AsString);
      FieldByName('ZdyStr1').Value:='应收收';
      Post;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where FactoryName='''+Trim(Order_Main10.fieldbyname('KHName').AsString)+'''');
    sql.Add(' and YFTypeId='''+Trim(Order_Main10.fieldbyname('ConID').AsString)+'''');
    sql.add(' and CRFlag=''应收收''');
    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('ConID').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('KHName').AsString);
      FieldByName('CRTime').Value:=SGetServerDateTime(ADOQueryTemp);
      FieldByName('YFType').Value:='自动生成';
      //FieldByName('Price').Value:=Price;
      //FieldByName('HuiLv').Value:=1;
      FieldByName('BZType').Value:=Trim(PriceUnit);
      FieldByName('QtyUnit').Value:=Trim(OrderUnit);
      FieldByName('YFName').Value:='成品销售金额';
      FieldByName('MainId').Value:=Trim(Order_Main10.fieldbyname('Mainid').AsString);
      Post;
    end;
  end else
  begin
    YFID:=Trim(ADOQueryTemp.fieldbyname('YFID').AsString);
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    if Trim(OrderUnit)<>'Kg' then
    begin
      sql.Add('update YF_Money_CR Set Qty=(select isnull(Sum(A.Qty),0) from CK_BanCP_CR A  ');
    end else
    begin
      sql.Add('update YF_Money_CR Set Qty=(select isnull(Sum(A.KgQty),0) from CK_BanCP_CR A ');
    end;
    sql.add('where A.ConID=YF_Money_CR.YFTypeId and A.CRType=''正常出库'' and CPType=''正品''  ');
    sql.add(' and A.CRTime=YF_Money_CR.CRTime');
    SQL.Add(' )'); //and B.PRTPrice=YF_Money_CR.Price
    sql.Add(',PS=(select isnull(count(*),0) from CK_BanCP_CR A where');
    SQL.Add(' A.conid=YF_Money_CR.YFTypeId and A.CRType=''正常出库'' and CPType=''正品''  ');
    sql.add(' and A.CRTime=YF_Money_CR.CRTime');
    SQL.Add('  )');  //and B.PRTPrice=YF_Money_CR.Price
    sql.Add(',JZQty=(select isnull(Sum(MJQty4),0) from CK_BanCP_CR A ');
    sql.add(' inner join WFB_MJJY D on D.MJID=A.MJID where');
    SQL.Add(' A.Conid=YF_Money_CR.YFTypeId and A.CRType=''正常出库'' and CPType=''正品''  ');
    sql.add(' and A.CRTime=YF_Money_CR.CRTime');
    SQL.Add(' )');
    sql.Add(',MaoQty=(select isnull(Sum(A.KgQty),0) from CK_BanCP_CR A where');
    SQL.Add(' A.Conid=YF_Money_CR.YFTypeId and A.CRType=''正常出库'' and CPType=''正品''  ');
    sql.add(' and A.CRTime=YF_Money_CR.CRTime');
    SQL.Add(' )');
    sql.Add(',MQty=(select isnull(Sum(A.Qty),0) from CK_BanCP_CR A where');
    SQL.Add(' A.Conid=YF_Money_CR.YFTypeId and A.CRType=''正常出库'' and CPType=''正品''  ');
    sql.add(' and A.CRTime=YF_Money_CR.CRTime');
    SQL.Add(' )');
    sql.Add(' where YFTypeId='''+Trim(Order_Main10.fieldbyname('Conid').AsString)+'''');
    sql.add(' and CRType=''应收款登记''');
    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;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where YFId='''+Trim(YFID)+'''');
    Open;
  end;
  if ADOQueryTemp.FieldByName('PS').Value=0 then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete from YF_Money_CR where YFID='''+Trim(YFID)+'''');
      ExecSQL;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where CRId='+CRID);
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete from YF_Money_KC where CRId='+CRID);
      ExecSQL;
    end;
  end;
  Result:=True;
end;

procedure TfrmCKProductBCPOutList.orderNoKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    if Length(Trim(orderNo.Text))<3 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Close;
        sql.Clear;
        SQL.Add('select B.OrderNo,G.conNO,G.conNO ConNoHT,G.KHConNO,F.styleno,B.MPRTCodeName,B.MPRTSpec,B.MprtkuanNo');
        sql.Add(',C.PRTColor,C.PRTColorNO SOrddefstr1,C.PRTColorEng,B.MPRTMF,B.MPRTKZ,D.MJXH,C.PRTPrice,C.PRTHX ');
        sql.Add(',D.MJQty3,D.MJQty4');
        sql.Add(',isnull(G.customerNoName,B.OrderNo) KHName');
        sql.Add(',D.mjstr4 as AOrdDefStr1,cast(D.MJXH as varchar) as MJXHM,A.* ');
        sql.add('from CK_BanCP_CR A ');
        Sql.add(' left join JYOrder_Main B on B.MainId=A.MainId');
        Sql.add(' left join JYOrder_Sub C on C.mainID=A.mainID and C.SubId=A.SubId');
        Sql.add(' left join WFB_MJJY D on D.MJId=A.MJId');
        sql.Add(' left join CK_SXPB_CR E on A.APID=E.SPID ');
        sql.add(' left join JYOrderCon_Sub F on F.Subid=A.ConID ');
        sql.add(' left join JYOrderCon_Main G on G.Mainid=F.Mainid');
        sql.add('where B.OrderNo like '''+'%'+Trim(orderNo.Text)+'%'+'''');
        SQL.Add(' and A.CRFlag=''出库'' ');
        Open;
        //ShowMessage(SQL.Text);
      end;
      SCreateCDS20(ADOQueryMain,CDS_Main);
      SInitCDSData20(ADOQueryMain,CDS_Main);
    finally;
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmCKProductBCPOutList.Tv1CellClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if CDS_Main.IsEmpty=False then
  begin
    InitOrderColor(Trim(CDS_Main.fieldbyname('MainId').AsString),PRTColor,ADOQueryTemp);
    InitBCGangNo(Trim(CDS_Main.fieldbyname('SubId').AsString),AOrdDefStr1,ADOQueryTemp);
  end;
end;

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

procedure TfrmCKProductBCPOutList.AOrdDefStr1Change(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCKProductBCPOutList.CheckBox1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main,CheckBox1.Checked);
end;

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

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

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

procedure TfrmCKProductBCPOutList.Panel2DblClick(Sender: TObject);
begin
  Panel2.Visible:=false;
end;

procedure TfrmCKProductBCPOutList.THMJIDKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    if Trim(THMJID.Text)='' then
    begin
      Application.MessageBox('条码不能为空!','提示',0);
      Exit;
    end;
    with ADOQueryTemp do
    begin
      close;
      sql.Clear;
      sql.Add('select * from CK_BanCP_CR A ');
      sql.Add('where MJID='+quotedstr(trim(THMJID.Text)));
      sql.Add('and CRflag=''出库''');
      open;
    end;
    IF  ADOQueryTemp.IsEmpty then
    begin
      application.MessageBox('此卷未出库','提示信息',MB_ICONERROR);
      exit;
    end;
    try
       with ADOQueryCmd do
       begin
         Close;
         sql.Clear;
         sql.Add('delete CK_BanCP_CR where BCID='+quotedstr(ADOQueryTemp.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.CRType=''检验入库'') ');
         sql.Add(',KCQty=(select Qty from CK_BanCP_CR A where  A.CRID=CK_BanCP_KC.CRID and A.CRType=''检验入库'') ');
         SQL.Add(' where CRID='+quotedstr(ADOQueryTemp.fieldbyname('CRID').AsString));
         sql.Add('Update WFB_MJJY Set MJStr2=''已入库'' where MJID='+quotedstr(ADOQueryTemp.fieldbyname('MJID').AsString));
         ExecSQL;
       end;
      IF CDS_Main.Locate('BCID',trim(ADOQueryTemp.fieldbyname('BCID').AsString),[]) then
      CDS_Main.Delete;
      THMJID.Text:='';
      THMJID.SetFocus;
      Exit;
    except
      THMJID.Text:='';
      THMJID.SetFocus;
      Application.MessageBox('撤销出库异常！','提示',0);
    end;
  end;
end;

procedure TfrmCKProductBCPOutList.ToolButton1Click(Sender: TObject);
begin
  Panel2.Visible:=true;
  THMJID.Text:='';
  THMJID.SetFocus;
end;

procedure TfrmCKProductBCPOutList.ToolButton3Click(Sender: TObject);
var
  fPrintFile:string;
begin
  if CDS_Main.IsEmpty then Exit;
  if CDS_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  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;
  ADOqueryCmd.Connection.BeginTrans;
  try
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      First;
      while not Eof do
      begin
        if FieldByName('SSel').AsBoolean then
        begin
          ADOQueryCmd.append;
          ADOQueryCmd.fieldbyname('SubId').Value:=Trim(fieldbyname('MJID').AsString);
          ADOQueryCmd.fieldbyname('Dname').Value:=Trim(DCode);
          ADOQueryCmd.post;
        end;
        Next;
      end;
    end;
    CDS_Main.EnableControls;
    ADOqueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;
  with ADOqueryPrint do
  begin
    close;
    sql.Clear;
    sql.Add(' exec P_Get_JYMJID_Main ');
    sql.Add(' @MJID='+quotedstr(''));
    sql.Add(',@flag=''0'' ');
    sql.Add(',@userID='+quotedstr(trim(DCode)));
    open;
  end;

  fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\检验汇总信息.rmf';;
  if NOT FileExists(fPrintFile) then
  begin
    Application.MessageBox(PChar('没有找'+trim(fPrintFile)),'提示',MB_ICONERROR);
    exit;
  end;
end;

procedure TfrmCKProductBCPOutList.ToolButton2Click(Sender: TObject);
begin
  IF CDS_Main.IsEmpty then exit;
  frmJYMJIDPRINT:=TfrmJYMJIDPRINT.create(self);
  with frmJYMJIDPRINT do
  begin
    fMJID:=trim(self.CDS_Main.fieldbyname('MJID').AsString);
    fflag:=1;
    show;
  end;
end;

procedure TfrmCKProductBCPOutList.ConNOHTChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCKProductBCPOutList.ConNOHTKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    if Length(Trim(ConNOHT.Text))<3 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Close;
        sql.Clear;
        SQL.Add('select B.OrderNo,G.conNO,G.conNO ConNoHT,D.MJLen,G.KHConNO,F.styleno,B.MPRTCodeName,B.MPRTSpec,B.MprtkuanNo,C.PRTColor,C.PRTColorNO SOrddefstr1,C.PRTColorEng,B.MPRTMF,B.MPRTKZ,D.MJXH,C.PRTPrice,C.PRTHX ');
        sql.Add(',D.MJQty3,D.MJQty4');
        sql.Add(',isnull(G.customerNoName,B.OrderNo) KHName');
        sql.Add(',D.mjstr4 as AOrdDefStr1,cast(D.MJXH as varchar) as MJXHM,A.* ');
        sql.add('from CK_BanCP_CR A ');
        Sql.add(' left join JYOrder_Main B on B.MainId=A.MainId');
        Sql.add(' left join JYOrder_Sub C on C.mainID=A.mainID and C.SubId=A.SubId');
        Sql.add(' left join WFB_MJJY D on D.MJId=A.MJId');
        sql.Add(' left join CK_SXPB_CR E on A.APID=E.SPID ');
        sql.add(' left join JYOrderCon_Sub F on F.Subid=D.Conid ');
        sql.add(' left join JYOrderCon_Main G on G.Mainid=F.Mainid');
        sql.add('where G.conNO like '''+'%'+Trim(ConNOHT.Text)+'%'+'''');
        SQL.Add(' and A.CRFlag=''出库'' ');
        Open;
        //ShowMessage(SQL.Text);
      end;
      SCreateCDS20(ADOQueryMain,CDS_Main);
      SInitCDSData20(ADOQueryMain,CDS_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmCKProductBCPOutList.KHConNoKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    if Length(Trim(KHConNo.Text))<3 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        SQL.Add('select B.OrderNo,G.conNO,G.KHConNO,F.styleno,B.MPRTCodeName,B.MPRTSpec,B.MprtkuanNo,C.PRTColor,C.PRTColorNO SOrddefstr1,C.PRTColorEng,B.MPRTMF,B.MPRTKZ,D.MJXH,C.PRTPrice,C.PRTHX ');
        sql.Add(',D.MJQty3,D.MJQty4');
        sql.Add(',isnull(G.customerNoName,B.OrderNo) KHName');
        sql.Add(',D.mjstr4 as AOrdDefStr1,cast(D.MJXH as varchar) as MJXHM,A.* ');
        sql.add('from CK_BanCP_CR A ');
        Sql.add(' left join JYOrder_Main B on B.MainId=A.MainId');
        Sql.add(' left join JYOrder_Sub C on C.mainID=A.mainID and C.SubId=A.SubId');
        Sql.add(' left join WFB_MJJY D on D.MJId=A.MJId');
        sql.Add(' left join CK_SXPB_CR E on A.APID=E.SPID ');
        sql.add(' left join JYOrderCon_Sub F on F.Subid=D.ConID ');
        sql.add(' left join JYOrderCon_Main G on G.Mainid=F.Mainid');
        sql.add('where G.KHConNo like '''+'%'+Trim(KHConNo.Text)+'%'+'''');
        SQL.Add(' and A.CRFlag=''出库'' ');
        Open;
        //ShowMessage(SQL.Text);
      end;
      SCreateCDS20(ADOQueryMain,CDS_Main);
      SInitCDSData20(ADOQueryMain,CDS_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmCKProductBCPOutList.TDHClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  MovePanel2.Visible:=True;
  MovePanel2.Refresh;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update WFB_MJJY Set MJStr2=''已入库'' where ');
      sql.Add('exists (select MJID from CK_BanCP_CR where MJID=WFB_MJJY.MJID and CKOrdNo='''+trim(CDS_Main.fieldbyname('CKOrdNo').AsString)+''') ');
      sql.Add('UPdate CK_BanCP_CR set CRFlag=''待出库'',CRFlagQty=0 where CKOrdNo='''+Trim(CDS_Main.fieldbyname('CKOrdNo').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 exists (select MJID from CK_BanCP_CR where MJID=CK_BanCP_KC.MJID and CKOrdNo='''+trim(CDS_Main.fieldbyname('CKOrdNo').AsString)+''') ');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update CK_BanCP_CR Set ');
      sql.add(' CRTime=NULL');
      sql.Add(',CRType='''',CRFlag=''待出库''');
      sql.Add(',CRNote=''''');
      SQL.Add(',CKOrdNo='''' ');
      sql.Add(',CRFlagQty=0');
      sql.Add('where CKOrdNo='''+trim(CDS_Main.fieldbyname('CKOrdNo').AsString)+'''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('撤销完成','提示');
    InitGrid();
    MovePanel2.Visible:=False;
    Exit;
  except
    MovePanel2.Visible:=False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('撤销异常！','提示',0);
  end;
end;

end.
