unit U_CPBCKOutList;

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, cxCheckBox, RM_Common, RM_Class, RM_e_Xls, RM_Dataset,
  RM_System, RM_GridReport, Menus, cxCalendar, cxButtonEdit, cxTextEdit,
  RM_BarCode, cxPC, Math, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxBarBuiltInMenu, dxSkinsCore, dxSkinsDefaultPainters, dxSkinscxPCPainter,
  BtnEdit;

type
  TfrmCPBCKOutList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    RMDBHZ: TRMDBDataSet;
    CDS_HZ: TClientDataSet;
    CDS_PRT: TClientDataSet;
    TBDel: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column4: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Label10: TLabel;
    QCOrderNo: TEdit;
    v1Column15: TcxGridDBColumn;
    ToolButton3: TToolButton;
    ADOQueryPrint: TADOQuery;
    v1Column21: TcxGridDBColumn;
    Label17: TLabel;
    spcode: TEdit;
    RM1: TRMGridReport;
    RMBarCodeObject1: TRMBarCodeObject;
    v1Column23: TcxGridDBColumn;
    v1Column24: TcxGridDBColumn;
    Label18: TLabel;
    N3: TMenuItem;
    cxTabControl1: TcxTabControl;
    btnOK: TToolButton;
    btnOKCX: TToolButton;
    Panel3: TPanel;
    Label25: TLabel;
    btnChk: TButton;
    Button3: TButton;
    MMJYNote: TMemo;
    edtSPID: TEdit;
    Panel2: TPanel;
    cxGrid1: TcxGrid;
    TvMX: TcxGridDBTableView;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton4: TToolButton;
    CDS_MX: TClientDataSet;
    DS_MX: TDataSource;
    ToolButton2: TToolButton;
    ToolButton5: TToolButton;
    v1Column18: TcxGridDBColumn;
    ComboBox1: TComboBox;
    vMXColumn1: TcxGridDBColumn;
    vMXColumn2: TcxGridDBColumn;
    vMXColumn3: TcxGridDBColumn;
    CustomerNoName: TComboBox;
    Tv1Column1: TcxGridDBColumn;
    ToolButton6: TToolButton;
    TvMXColumn1: TcxGridDBColumn;
    ToolButton7: TToolButton;
    ADOQuery1: TADOQuery;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    TvMXColumn2: TcxGridDBColumn;
    PopupMenu2: TPopupMenu;
    MenuItem1: TMenuItem;
    MenuItem3: TMenuItem;
    Label3: TLabel;
    SPColor: TEdit;
    ToolButton8: TToolButton;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Label4: TLabel;
    FHSTATUS: TComboBox;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    ToolButton9: TToolButton;
    ToolButton10: TToolButton;
    ToFactoryName: TBtnEditA;
    Label9: TLabel;
    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 SPNameChange(Sender: TObject);
    procedure SPIDKeyPress(Sender: TObject; var Key: Char);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure N3Click(Sender: TObject);
    procedure btnOKClick(Sender: TObject);
    procedure btnChkClick(Sender: TObject);
    procedure btnOKCXClick(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure cxGridDBColumn8PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Tv1Column1PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure MenuItem1Click(Sender: TObject);
    procedure MenuItem3Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure Tv1Column5PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton9Click(Sender: TObject);
    procedure ToolButton10Click(Sender: TObject);
    procedure ToFactoryNameBtnClick(Sender: TObject);
  private
    procedure InitGrid();
    procedure SetStatus();
    procedure InitMXGrid(MCRNO: string);
    { Private declarations }
  public
    canshu1, canshu2: string;
    { Public declarations }
  end;

implementation

uses
  U_DataLink, U_RTFun, U_Fun, U_CPBCKOutPut, U_CPCKMXLR, U_FjList_RZ,
  U_ZdyAttachGYS;

{$R *.dfm}
procedure TfrmCPBCKOutList.InitMXGrid(MCRNO: string);
begin

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_SXPB_CRMX A where CRNO=' + quotedstr(Trim(MCRNO)));
    if cxTabControl1.TabIndex = 0 then
    begin
      sql.Add('and CRZT=''入库''');
      SQL.Add('AND NOT EXISTS(SELECT MXID FROM CK_SXPB_CRMX C WHERE C.FZMXID=A.MXID)');
    end
    else
    begin
      sql.Add('and CRZT=''出库''');
    end;
    sql.Add(' order by MXID');
//    ShowMessage(SQL.Text);
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_MX);
  SInitCDSData20(ADOQueryTemp, CDS_MX);
end;

procedure TfrmCPBCKOutList.SetStatus();
begin
  TBEdit.Visible := False;
  btnOK.Visible := False;
  btnOKCX.Visible := False;
  TBDel.Visible := False;
  Tv1Column2.Visible := False;
  Tv1Column2.Hidden := true;
  Tv1Column3.Visible := False;
  Tv1Column3.Hidden := true;
  v1Column9.visible := False;
  v1Column9.Hidden := true;
  v2Column6.visible := False;
  v2Column6.Hidden := true;
  ToolButton7.Visible := False;
  ToolButton8.Visible := False;
  ToolButton9.Visible := False;
  Tv1Column5.Visible := False;
  Tv1Column5.Hidden := True;
  case cxTabControl1.TabIndex of
    0:
      begin
//        if canshu1 = '出库确认' then
//        begin
        Tv1Column4.Caption := '入库日期';
        ToolButton8.Visible := TRUE;
        ToolButton7.Visible := True;
        Tv1Column2.Visible := true;
        Tv1Column2.Hidden := False;
        Tv1Column3.Visible := true;
        Tv1Column3.Hidden := False;

      end;
    1:
      begin

        TBEdit.Visible := True;
        TBDel.Visible := True;

        v1Column9.visible := true;
        v1Column9.Hidden := False;
        v2Column6.visible := true;
        v2Column6.Hidden := False;
        Tv1Column4.Caption := '出库日期';
        Tv1Column5.Visible := true;
        Tv1Column5.Hidden := False;

      end;
    2:
      begin
        TBEdit.Visible := True;
        TBDel.Visible := True;

        v1Column9.visible := true;
        v1Column9.Hidden := False;
        v2Column6.visible := true;
        v2Column6.Hidden := False;
        Tv1Column4.Caption := '出库日期';
        Tv1Column5.Visible := true;
        Tv1Column5.Hidden := False;

        ToolButton9.Visible := true;
      end;
    3:
      begin
        v1Column9.visible := true;
        v1Column9.Hidden := False;
        v2Column6.visible := true;
        v2Column6.Hidden := False;

      end;
  end;
end;

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

procedure TfrmCPBCKOutList.FormCreate(Sender: TObject);
begin
  //cxGrid1.Align:=alClient;
  canshu1 := Trim(DParameters1);
  canshu2 := Trim(DParameters2);
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime - 30;
end;

procedure TfrmCPBCKOutList.InitGrid();
var
  fwhere, Pwhere: string;
begin
  Pwhere := SGetFilters(Panel1, 1, 2);

  begin
    if trim(Pwhere) <> '' then
      fwhere := fwhere + ' and ' + trim(Pwhere);
  end;
  try
    ADOQueryMain.DisableControls;
    if cxTabControl1.TabIndex = 0 then
    begin
      with ADOQueryMain do
      begin
        Filtered := False;
        Close;
        sql.Clear;
        sql.Add(' select A.*,gangno1=gangno,JSQTY=0 ');
        SQL.Add(',FQTY=(CASE WHEN qtyunit=''KG'' THEN (SELECT SUM(MXQTY2) FROM CK_SXPB_CRMX D WHERE D.CRNO=A.SPID)');
        SQL.Add('WHEN qtyunit=''M'' OR qtyunit=''Y'' THEN (SELECT SUM(MXQTY) FROM CK_SXPB_CRMX D WHERE D.CRNO=A.SPID) END )');

        SQL.Add(',FUNIT=(CASE WHEN qtyunit=''KG'' THEN (SELECT TOP 1 MXUnit FROM CK_SXPB_CRMX D WHERE D.CRNO=A.SPID)');
        SQL.Add('WHEN qtyunit=''M'' OR qtyunit=''Y'' THEN ''KG'' END )');
        sql.Add(' from CK_SXPB_CR A where isnull(CKName,'''')=''成品布'' ');
        SQL.Add(' and A.CRFlag=''入库'' and MXKCQty>0 AND ISNULL(QCFLAG,''0'')=''0''');
        if canshu1 <> '管理' then
        begin
          SQL.Add('AND (FILLER =' + QuotedStr(Trim(dname)));
          sql.Add('or (select lidan from JYOrder_Main c where c.orderno=a.qcorderno)=' + QuotedStr(Trim(dname)));
          sql.Add('or (select ywy from JYOrder_Main c where c.orderno=a.qcorderno)=' + QuotedStr(Trim(dname)));
          sql.Add(')');
        end;
        sql.add(' and A.CRTime>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)) + '''');
        sql.Add(' and A.CRTime<=''' + Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)) + '''');
        SQL.Add(fwhere);
//      showmessage(sql.Text);
        Open;
      end;
    end
    else
    begin
      with ADOQueryMain do
      begin
        Filtered := False;
        Close;
        sql.Clear;
        sql.Add(' select A.*,gangno1=rcgangno  ');
        SQL.Add(',FQTY=(CASE WHEN JSUNIT=''KG'' THEN (SELECT SUM(MXQTY2) FROM CK_SXPB_CRMX D WHERE D.CRNO=A.SPID)');
        SQL.Add('WHEN JSUNIT=''M'' OR JSUNIT=''Y'' THEN (SELECT SUM(MXQTY) FROM CK_SXPB_CRMX D WHERE D.CRNO=A.SPID) END )');

        SQL.Add(',FUNIT=(CASE WHEN JSUNIT=''KG'' THEN (SELECT TOP 1 MXUnit FROM CK_SXPB_CRMX D WHERE D.CRNO=A.SPID)');
        SQL.Add('WHEN JSUNIT=''M'' OR JSUNIT=''Y'' THEN ''KG'' END )');
        SQL.Add(',JSQTY=(CASE WHEN JSUNIT=''KG'' THEN (SELECT SUM(MXQTY) FROM CK_SXPB_CRMX D WHERE D.CRNO=A.SPID)');
        SQL.Add('WHEN JSUNIT=''M'' OR JSUNIT=''Y'' THEN (SELECT SUM(MXQTY2) FROM CK_SXPB_CRMX D WHERE D.CRNO=A.SPID) END )');
        sql.Add(',ISSC=cast((case when isnull((select count(WBID) from TP_File X where X.WBID=A.spid and X.TFType=''成品出库''),0)>0 then 1 else 0 end) as bit)');
        sql.Add(' from CK_SXPB_CR A');
        SQL.Add(' where CRFlag=''出库''   and isnull(CKName,'''')=''成品布'' ');
        if canshu1 <> '管理' then
        begin
          SQL.Add('AND (FILLER =' + QuotedStr(Trim(dname)));
          sql.Add('or (select lidan from JYOrder_Main c where c.orderno=a.qcorderno)=' + QuotedStr(Trim(dname)));
          sql.Add('or (select ywy from JYOrder_Main c where c.orderno=a.qcorderno)=' + QuotedStr(Trim(dname)));
          sql.Add(')');
        end;
        case cxTabControl1.TabIndex of

          1:
            begin
              SQL.Add(' and isnull(chkstatus,0)=''1''  ');
              sql.add(' and A.CRTime>=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime))) + ' and A.CRTime<' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1))));
            end;
          2:
            begin
              SQL.Add(' and isnull(chkstatus,0)=''1''  ');
              sql.add(' and A.CRTime>=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime))) + ' and A.CRTime<' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1))));
              sql.Add('and FHSTATUS=''未发货''');
            end;
        end;
        SQL.Add(fwhere);
//      showmessage(sql.Text);
        Open;
      end;
    end;

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

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

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

procedure TfrmCPBCKOutList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('成品出库列表5', Tv1, '成品出库列表0327');
  Close;
end;

procedure TfrmCPBCKOutList.FormShow(Sender: TObject);
var
  fsj: string;
begin

  ReadCxGrid('成品出库列表5', Tv1, '成品出库列表0327');
  SetStatus();
  fsj := 'select distinct(CustomerNoName) Name from CK_SXPB_CR  order by CustomerNoName ';
  SInitComBoxBySql(ADOQueryCmd, CustomerNoName, False, fsj);
//  FILLER.Text := Trim(DName);
  InitGrid();
end;

procedure TfrmCPBCKOutList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  TcxGridToExcel('外加工出库列表', cxGrid2);
end;

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

procedure TfrmCPBCKOutList.SPIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    {if Length(Trim(SPID.Text))<4 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add(' select A.* ');
        sql.Add(' from CK_SXPB_CR A');
        sql.add(' where SPID like :SXID');
        Parameters.ParamByName('SXID').Value:='%'+Trim(SXID.Text)+'%';
        Open;
        //ShowMessage(SQL.Text);
      end;
      SCreateCDS20(ADOQueryMain,CDS_Main);
      SInitCDSData20(ADOQueryMain,CDS_Main);
    finally
      ADOQueryMain.EnableControls;
    end; }
  end;
end;

procedure TfrmCPBCKOutList.N1Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  CDS_Main.DisableControls;
  with CDS_Main do
  begin
    First;
    while not eof do
    begin
      if Trim(CDS_Main.fieldbyname('CRType').AsString) = '检验出库' then
      begin
        Edit;
        FieldByName('SSel').Value := True;
        Post;
      end;
      Next;
    end;
  end;
  CDS_Main.EnableControls;
end;

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

procedure TfrmCPBCKOutList.TBDelClick(Sender: TObject);
var
  strsql: 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;
  try
    ADOQueryCmd.Connection.BeginTrans;
    while CDS_Main.Locate('SSel', True, []) do
    begin
      if Trim(CDS_Main.fieldbyname('SPID').AsString) <> '' then
      begin
        if Trim(CDS_Main.fieldbyname('CRType').AsString) = '加工出库' then
        begin
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CK_SXPB_CR where FZSPID=');
            sql.Add('(select SPID from CK_SXPB_CR where PYID=''' + Trim(CDS_Main.fieldbyname('SPID').AsString) + '''');
            sql.Add(' and isnull(CRType,'''')=''平移入库'')');
            sql.Add(' and CRQtyFlag=-1');
            Open;
          end;
          if ADOQueryTemp.IsEmpty = False then
          begin
            Application.MessageBox('已有出库记录，不能删除!', '提示', 0);
            Exit;
          end;
        end;
        strsql := '来自记录：' + Trim(CDS_Main.fieldbyname('FZSPID').AsString) + ';当前记录:' + Trim(CDS_Main.fieldbyname('SPID').AsString);

        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select  A.YFID from YF_Money_CR_Sub A INNER  JOIN YF_Money_CR B ON A.YFID=B.YFID where yfpzno=' + QuotedStr(Trim(CDS_Main.fieldbyname('SPID').AsString)));
          sql.Add('and B.status=1');
          Open;
        end;
        if ADOQueryTemp.IsEmpty = FALSE then
        begin
          Application.MessageBox('财务数据已审核，请先撤销!', '提示', 0);
          ADOQueryCmd.Connection.RollbackTrans;
          Exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;

          sql.Add(' delete YF_Money_CR where YFID=(SELECT YFID FROM YF_Money_CR_SUB D WHERE D.YFPZNO=' + QuotedStr(Trim(CDS_Main.fieldbyname('SPID').AsString)));
          SQL.Add(')');
          sql.Add(' delete YF_Money_CR_SUB where YFPZNO=''' + Trim(CDS_Main.fieldbyname('SPID').AsString) + '''');
          sql.Add(' delete CK_SXPB_CRMX where CRNO=''' + Trim(CDS_Main.fieldbyname('SPID').AsString) + '''');
          sql.Add(' delete CK_SXPB_CR where SPID=''' + Trim(CDS_Main.fieldbyname('SPID').AsString) + '''');
          sql.Add(' Update CK_SXPB_CR Set MXKCQty=(select sum(isnull(Qty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
          sql.Add(',MXKCPiQty=(select sum(isnull(PiQty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
          sql.Add(' where SPID=''' + Trim(CDS_Main.fieldbyname('FZSPID').AsString) + '''');
          if Trim(CDS_Main.fieldbyname('CRType').AsString) = '加工出库' then
          begin
            sql.Add(' delete CK_SXPB_CR where CRType=''平移入库''  and  PYID=''' + Trim(CDS_Main.fieldbyname('SPID').AsString) + '''');
          end;
          sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
          sql.Add(' ' + quotedstr(trim(DName)));
          sql.Add(',getdate() ');
          sql.Add(',' + quotedstr(trim(self.Caption)));
          sql.Add(',' + quotedstr(trim('删除待检布出库')));
          sql.Add(',' + quotedstr(trim(strsql)));
          sql.Add(',' + quotedstr(trim('成功')));
          sql.Add(')');
          ExecSQL;

        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update CK_SXPB_CR Set MXKCQty=(select sum(isnull(Qty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
          sql.Add(',MXKCPiQty=(select sum(isnull(PiQty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
          sql.Add(' where SPID=''' + Trim(CDS_Main.fieldbyname('fzSPID').Value) + '''');
          ExecSQL;
        end;
      end;
      CDS_Main.Delete;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('删除异常!', '提示', 0);
  end;
end;

procedure TfrmCPBCKOutList.TBAddClick(Sender: TObject);
begin
  try
    frmCPBCKOutPut := TfrmCPBCKOutPut.Create(Application);
    with frmCPBCKOutPut do
    begin
      FBCId := '';
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmCPBCKOutPut.Free;
  end;
end;

procedure TfrmCPBCKOutList.TBEditClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_SXPB_CR where FZSPID=');
    sql.Add('(select SPID from CK_SXPB_CR where PYID=''' + Trim(CDS_Main.fieldbyname('SPID').AsString) + '''');
    sql.Add(' and isnull(CRType,'''')=''平移入库'')');
    sql.Add(' and CRQtyFlag=-1');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Application.MessageBox('已有出库记录，不能修改!', '提示', 0);
    Exit;
  end;
  try
    frmCPBCKOutPut := TfrmCPBCKOutPut.Create(Application);
    with frmCPBCKOutPut do
    begin
      FBCId := Trim(CDS_Main.fieldbyname('SPID').AsString);
//      TBDel.Visible := False;
//      TBAdd.Visible := False;
      if ShowModal = 1 then
      begin
        Self.SetStatus();
        Self.InitGrid();
      end;
    end;
  finally
    frmCPBCKOutPut.Free;
  end;

end;

procedure TfrmCPBCKOutList.ToolButton3Click(Sender: TObject);
var
  fPrintFile: string;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
  wsql, wsql2: string;
  FMAINID: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  //if Trim(CDS_Main.fieldbyname('CRType').AsString)<>'检验出库' then Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  ExportFtErpFile(Trim(combobox1.Text) + '.rmf', ADOQueryTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(combobox1.Text) + '.rmf';

  with CDS_Main do
  begin
//    CDS_Main.DisableControls;
//    wsql := ' ';
//    First;
//    while not Eof do
//    begin
//      if CDS_Main.fieldbyname('SSEL').AsBoolean then
//      begin
//        if wsql = ' ' then
//          wsql := wsql + quotedstr(trim(Trim(CDS_Main.fieldbyname('spid').AsString)))
//        else
//          wsql := wsql + ',' + quotedstr(trim(Trim(CDS_Main.fieldbyname('spid').AsString)));
//
//      end;
//      Next;
//
//      CDS_Main.EnableControls;
//    end;
//    wsql2 := '(' + wsql + ')';
////    wsql := wsql + ')';
//    CDS_Main.DisableControls;
  end;

  with CDS_Main do
  begin
//    First;
//    while CDS_Main.Locate('SSel', True, []) do
//    begin
    if (Trim(combobox1.Text) = '外销码单') or (Trim(combobox1.Text) = '外销码单2') then
    begin
      CDS_Main.DisableControls;
      wsql := ' ';
      First;
      while not Eof do
      begin
        if CDS_Main.fieldbyname('SSEL').AsBoolean then
        begin
          if wsql = ' ' then
            wsql := wsql + quotedstr(trim(Trim(CDS_Main.fieldbyname('spid').AsString)))
          else
            wsql := wsql + ',' + quotedstr(trim(Trim(CDS_Main.fieldbyname('spid').AsString)));

        end;
        Next;

        CDS_Main.EnableControls;
      end;
      wsql2 := '(' + wsql + ')';

      CDS_Main.DisableControls;
      with ADOQueryPrint do
      begin
        Close;
        sql.Clear;
        sql.Add('select MXKH=C.CustomerNoName,MXKUANNO=PRTKuanNO,KHConNo,D.MainId,D.SubId,MXGangHao,isnull(c.MPRTKZ,spkz)MPRTKZ, ');
        sql.Add('B.MXQty ,B.MXQTY2,B.MXUnit,MXMJXH,MXFM,isnull(PRTCOLOR,spcolor)PRTCOLOR,c.orderno,mprtcode,prtecolor');
        SQL.Add(',sellname=(select top 1 sellname from SalesContract_Main f where f.conno=c.conno)');
        sql.Add('from CK_SXPB_CR A');
        sql.Add('INNER JOIN CK_SXPB_CRMX B ON A.SPID=B.CRNO');
        sql.Add('left JOIN JYOrder_Main C ON C.MainId=A.ORDMainIdRK');
        sql.Add('left JOIN JYOrder_Sub D ON A.ORDSubIdRK=D.SUBID ');
        sql.Add('WHERE A.SPID in ' + (wsql2));
        if cxTabControl1.TabIndex = 0 then
        begin
          sql.Add('and crzt=''入库''');
          sql.Add(' AND NOT EXISTS(SELECT MXID FROM CK_SXPB_CRMX E WHERE E.FZMXID=B.MXID) ');
        end
        else
        begin
          sql.Add('and crzt=''出库''');
        end;

        sql.Add('order by D.subid,prtcolor,prtecolor,MXGangHao,MXMJXH');
//        ShowMessage(sql.Text);
        Open;
      end;
      SCreateCDS20(ADOQueryPrint, CDS_HZ);
      SInitCDSData20(ADOQueryPrint, CDS_HZ);
      with ADOQueryPrint do
      begin
        Close;
        sql.Clear;
        sql.add('select D.MainId,D.SubId,MXGangHao, isnull(c.MPRTKZ,spkz)MPRTKZ, ');
        sql.Add(' sum(B.MXQty) as ZKGQty ,cast(sum(B.MXQTY2) as varchar) as ZQty,count(MXMJXH) as PS,sum(b.mxfm)zfm,B.MXUnit,  ');
        sql.Add('ISNULL(D.PRTColor,SPCOLOR)PRTColor,PRTEColor,PRTKUANNO');
        sql.Add('from CK_SXPB_CR A');
        sql.Add('  INNER JOIN CK_SXPB_CRMX B ON A.SPID=B.CRNO  left JOIN JYOrder_Main C ON C.MainId=A.ORDMainIdRK');
        sql.Add('left JOIN JYOrder_Sub D ON A.ORDSubIdRK=D.SUBID ');
        sql.Add('WHERE A.SPID in ' + (wsql2));
        if cxTabControl1.TabIndex = 0 then
        begin
          sql.Add('and B.crzt=''入库''');
          sql.Add(' AND NOT EXISTS(SELECT MXID FROM CK_SXPB_CRMX E WHERE E.FZMXID=B.MXID) ');
        end
        else
        begin
          sql.Add('and b.crzt=''出库''');
        end;
        SQL.add(' group by D.MainId,D.SubId,MXGangHao,isnull(c.MPRTKZ,spkz),D.subid,GangNo,B.MXUnit,ISNULL(D.PRTColor,SPCOLOR),D.PRTEColor,PRTKUANNO');
        sql.Add('order by D.SubId');
//      ShowMessage(sql.Text);
        Open;
      end;
      SCreateCDS20(ADOQueryPrint, CDS_PRT);
      SInitCDSData20(ADOQueryPrint, CDS_PRT);
    end;
    if Trim(combobox1.Text) = '内销码单' then
    begin
      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;
      FMAINID := '';
      CDS_Main.DisableControls;
      try
        ADOQueryCmd.Connection.BeginTrans;
        with CDS_Main do
        begin
          First;
          while not Eof do
          begin
            if Fieldbyname('Ssel').AsBoolean then
            begin
              if FMAINID = '' then
              begin
                FMAINID := Trim(CDS_Main.fieldbyname('MAINID').AsString);
              end
              else
              begin
                if Trim(CDS_Main.fieldbyname('mainID').AsString) <> FMAINID then
                begin
                  application.MessageBox('选择的不是同一个指示单，不能一起打印！', '提示信息', 0);
                  ADOQueryCmd.Connection.RollbackTrans;
                  EnableControls;
                  exit;
                end;
              end;
              ADOQueryCmd.append;
              ADOQueryCmd.fieldbyname('SubId').Value := Trim(CDS_Main.fieldbyname('spid').AsString);
              ADOQueryCmd.fieldbyname('Dname').Value := Trim(DCode);
              ADOQueryCmd.post;
            end;
            Next;
          end;
        end;
        ADOQueryCmd.Connection.CommitTrans;
      except
        ADOQueryCmd.Connection.RollbackTrans;
      end;
      CDS_Main.EnableControls;
      if (cxTabControl1.TabIndex = 1) or (cxTabControl1.TabIndex = 2) then
      begin
        with ADOQueryPrint do
        begin
          Close;
          sql.Clear;
//        sql.add('exec P_Print_CKMDYDLS ' + quotedstr(CDS_Main.fieldbyname('SPID').asstring));
          sql.add('exec P_Print_CKMDYDLS ');
          sql.add('@spid=' + quotedstr(Trim(DCode)));

          Open;
        end;
      end
      else
      begin
        with ADOQueryPrint do
        begin
          Close;
          sql.Clear;
//        sql.add('exec P_Print_CKMDYDLS ' + quotedstr(CDS_Main.fieldbyname('SPID').asstring));
          sql.add('exec P_Print_CKMDYDLS2 ');
          sql.add('@spid=' + quotedstr(Trim(DCode)));

          Open;
        end;
      end;

      SCreateCDS20(ADOQueryPrint, CDS_HZ);
      SInitCDSData20(ADOQueryPrint, CDS_HZ);
    end;
//    ShowMessage(wsql);
//      try
//        Moudle := LoadLibrary('MakeQRBarcode.dll');
//        @Makebar := GetProcAddress(Moudle, 'Make');
//        @Mixtext := GetProcAddress(Moudle, 'MixText');
//        Txt := Trim(CDS_PRT.fieldbyname('SPID').AsString);
//        fImagePath := ExtractFilePath(Application.ExeName) + 'image\temp.bmp';
//        if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName) + 'image')) then
//          CreateDirectory(pchar(ExtractFilePath(Application.ExeName) + 'image'), nil);
//        if FileExists(fImagePath) then
//          DeleteFile(fImagePath);
//        Makebar(pchar(Txt), Length(Txt), 3, 3, 0, PChar(fImagePath), 3);
//      except
//        CDS_Main.EnableControls;
//        application.MessageBox('条形码生成失败！', '提示信息', MB_ICONERROR);
//        exit;
//      end;
    RMXLSExport1 := TRMXLSExport.Create(RMXLSExport1);
    if FileExists(fPrintFile) then
    begin
//        RMVariables['QRBARCODE'] := fImagePath;
      RM1.LoadFromFile(fPrintFile);
      RM1.ShowReport;
//        RM1.PrintReport;
    end
    else
    begin
      CDS_Main.EnableControls;
      Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\' + Trim(combobox1.Text) + '.rmf'), '提示', 0);
      Exit;
    end;
    with CDS_Main do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('SSel').Value := False;
        Post;
        Next;
      end;
    end;

//    end;
  end;
  CDS_Main.EnableControls;

end;

procedure TfrmCPBCKOutList.Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
//  if CDS_Main.IsEmpty then Exit;
//  if Trim(CDS_Main.fieldbyname('CRType').AsString)<>'检验出库' then
//  begin
//    Tv1.OptionsSelection.CellSelect:=False;
//  end else
//  begin
//    Tv1.OptionsSelection.CellSelect:=True;
//  end;

  if CDS_MAIN.ISEMPTY = TRUE then
    EXIT;
  InitMXGrid(Trim(CDS_Main.fieldbyname('SPID').AsString));
end;

procedure TfrmCPBCKOutList.N3Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  CDS_Main.DisableControls;
  with CDS_Main do
  begin
    First;
    while not eof do
    begin
      Edit;
      FieldByName('SSel').Value := True;
      Post;
      Next;
    end;
  end;
  CDS_Main.EnableControls;
end;

procedure TfrmCPBCKOutList.btnOKClick(Sender: TObject);
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select  A.YFID from YF_Money_CR_Sub A INNER  JOIN YF_Money_CR B ON A.YFID=B.YFID where yfpzno=' + QuotedStr(Trim(CDS_Main.fieldbyname('SPID').AsString)));
      sql.Add('and B.status=1');
      Open;
    end;
    if ADOQueryTemp.IsEmpty = FALSE then
    begin
      Application.MessageBox('财务数据已审核，请先撤销!', '提示', 0);
      ADOQueryCmd.Connection.RollbackTrans;
      Exit;
    end;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  CK_SXPB_CR SET chkstatus=''1'',Chktime=getdate() ');
      sql.Add(',Chker=' + quotedstr(Trim(DName)));

      sql.Add(',CRTime=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', Now))));
      sql.Add('where SPID=' + quotedstr(CDS_Main.fieldbyname('SPID').asstring));
      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim('出库审核')));
      sql.Add(',' + quotedstr(trim('入库单号：' + trim(edtSPID.Text))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    if (trim(CDS_Main.fieldbyname('crtype').asstring) = '销售出库') or (trim(CDS_Main.fieldbyname('crtype').asstring) = '样品出库') then
    begin

      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('EXEC P_Do_CWSC3 ''应收'',' + quotedstr(Trim(CDS_Main.fieldbyname('SPID').asstring)));

        ExecSQL;

      end;
    end;
    application.MessageBox('确认成功！', '提示信息');
    CDS_Main.Delete;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('确认失败！', '提示信息', 0);

  end;
end;

procedure TfrmCPBCKOutList.btnChkClick(Sender: TObject);
begin
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  CK_SXPB_CR SET chkstatus=''1'',Chktime=getdate() ');
      sql.Add(',Chker=' + quotedstr(Trim(DName)));
      sql.Add(',JYNote=' + quotedstr(Trim(MMJYNote.Text)));
      sql.Add(',CRTime=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', Now))));
      sql.Add('where SPID=' + quotedstr(trim(edtSPID.Text)));
      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim('入库审核')));
      sql.Add(',' + quotedstr(trim('入库单号：' + trim(edtSPID.Text))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    application.MessageBox('检验成功！', '提示信息');
    CDS_Main.Delete;

    Panel3.Visible := False;
  except
    application.MessageBox('检验失败！', '提示信息', 0);
  end;
end;

procedure TfrmCPBCKOutList.btnOKCXClick(Sender: TObject);
begin
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  CK_SXPB_CR SET chkstatus=''0'',Chktime=null ,Chker= null');
      sql.Add('where SPID=' + quotedstr(trim(CDS_Main.fieldbyname('SPID').AsString)));
      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim('撤销入库审核')));
      sql.Add(',' + quotedstr(trim('入库单号：' + trim(CDS_Main.FieldByName('SPID').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    application.MessageBox('撤销出库成功！', '提示信息');
    CDS_Main.Delete;
  except
    application.MessageBox('撤销出库失败！', '提示信息', 0);
  end;
end;

procedure TfrmCPBCKOutList.Button3Click(Sender: TObject);
begin
  Panel3.Visible := False;
end;

procedure TfrmCPBCKOutList.FormResize(Sender: TObject);
begin
  Panel3.Left := Ceil((self.Width - Panel3.Width) / 2) - 30;
  Panel3.Top := Ceil((self.Height - Panel3.Height) / 2) - 80;
end;

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

procedure TfrmCPBCKOutList.ToolButton1Click(Sender: TObject);
var
  i: Integer;
  MaxNo, MMXID, MCRNO: string;
begin
  MCRNO := Trim(CDS_MAIN.fieldbyname('SPID').AsString);
  if GetLSNo(ADOQueryTemp, MaxNo, 'MCM', 'CK_SXPB_CRMX', 3, 1) = False then
  begin
    Application.MessageBox('取最大号失败!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    for i := 1 to 10 do
    begin
      MMXID := Trim(MaxNo) + Trim(inttostr(i));
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('insert into  CK_SXPB_CRMX (MXID,CRNO,MXQty)');
        sql.Add('values(' + quotedstr(MMXID));
        sql.Add(',' + quotedstr(Trim(MCRNO)));
        sql.Add(',0');
        sql.Add(')');
        ExecSQL;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    InitMXGrid(MCRNO);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('添加失败!', '提示', 0);
  end;
end;

procedure TfrmCPBCKOutList.cxGridDBColumn8PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(TvMX.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_MX do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
  end;

  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('update CK_SXPB_CRMX set ' + FFieldName + ' = ' + mvalue);
    sql.Add('  where MXID=' + quotedstr(Trim(CDS_MX.FieldByName('MXID').AsString)));
    ExecSQL;
  end;
  TvMX.Controller.EditingController.ShowEdit();
end;

procedure TfrmCPBCKOutList.ToolButton4Click(Sender: TObject);
begin
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('delete CK_SXPB_CRMX   where MXID=' + quotedstr(Trim(CDS_MX.FieldByName('MXID').AsString)));
    ExecSQL;
  end;
  CDS_MX.Delete;
end;

procedure TfrmCPBCKOutList.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('DELETE CK_SXPB_CRMX where crno=' + quotedstr(Trim(cds_main.FieldByName('spid').AsString)));
    sql.Add(' AND (MXQTY=0 and mxqty2=0)');
//    ShowMessage(sql.Text);
    EXECSQL;
  end;
end;

procedure TfrmCPBCKOutList.ToolButton2Click(Sender: TObject);
begin
  if cds_main.IsEmpty = true then
  begin
    exit;
  end;
  if CDS_Main.fieldbyname('ssel').value <> true then
  begin
    Application.MessageBox('没有选择数据或勾选数据不准确!', '提示', 0);
    Exit;
  end;
  frmCPCKMXLR := TfrmCPCKMXLR.create(self);
  with frmCPCKMXLR do
  begin
    fkeyNo := self.cds_main.fieldbyname('spid').AsString;
    FMXGangHao := self.cds_main.fieldbyname('Gangno').AsString;
    Edit3.Text := self.cds_main.fieldbyname('qtyunit').AsString;
    Edit2.Text := self.cds_main.fieldbyname('FZSPID').AsString;
    if cxTabControl1.TabIndex = 0 then
    begin
      FCRTYPE := '入库';
    end
    else
    begin
      FCRTYPE := '出库';
    end;

    fType := 0;
    if showmodal = 1 then
    begin

//      self.InitGrid();
//      self.CDS_Main.Locate('spid', fkeyNo, [])
    end;
    free;
  end;
end;

procedure TfrmCPBCKOutList.ToolButton5Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  try
    frmFjList_RZ := TfrmFjList_RZ.Create(Application);
    with frmFjList_RZ do
    begin
      fkeyNO := Trim(Self.CDS_Main.fieldbyname('SPID').AsString);
      fType := '成品出库';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmFjList_RZ.Free;
  end;
end;

procedure TfrmCPBCKOutList.Button1Click(Sender: TObject);
begin
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  CK_SXPB_CR SET chkstatus=''1'',Chktime=getdate() ');
      sql.Add(',Chker=' + quotedstr(Trim(DName)));
      sql.Add(',JYNote=' + quotedstr(Trim(MMJYNote.Text)));
      sql.Add(',CRTime=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', Now))));
      sql.Add('where SPID=' + quotedstr(trim(edtSPID.Text)));
      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim('入库审核')));
      sql.Add(',' + quotedstr(trim('入库单号：' + trim(edtSPID.Text))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    application.MessageBox('检验成功！', '提示信息');
    CDS_Main.Delete;

    Panel3.Visible := False;
  except
    application.MessageBox('检验失败！', '提示信息', 0);
  end;
end;

procedure TfrmCPBCKOutList.Tv1Column1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  if (cxTabControl1.TabIndex = 0) then
    Exit;
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_Main do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    SQL.Add('EXEC P_Do_XGDW @SPID=' + quotedstr(Trim(CDS_Main.fieldbyname('spid').AsString)));
    SQL.Add(',@UNIT=' + quotedstr(Trim(CDS_Main.fieldbyname('JSUNIT').AsString)));
//    sql.Add('update CK_SXPB_CR set  ' + FFieldName + '=' + QuotedStr(Trim(mvalue)));
//    sql.Add('where spid=' + quotedstr(Trim(CDS_Main.fieldbyname('spid').AsString)));
//
//    sql.Add('update YF_MONEY_CR_SUB set QTYUNIT=' + QuotedStr(Trim(mvalue)));
//    sql.Add('where YFPZNO=' + quotedstr(Trim(CDS_Main.fieldbyname('spid').AsString)));
//    ShowMessage(SQL.Text);
    ExecSQL;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmCPBCKOutList.ToolButton7Click(Sender: TObject);
var
  maxno, maxno2: string;
begin
  if CDS_MX.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if GetLSNo(ADOQueryCmd, maxno, 'CC', 'CK_SXPB_CR', 3, 1) = False then
    begin
      Application.MessageBox('取待出库编号失败!', '提示', 0);
      Exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CK_SXPB_CR where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('SPID').Value := Trim(maxno);
      FieldByName('CKName').Value := '成品布';
      FieldByName('ORDMainIdRK').Value := Trim(CDS_Main.fieldbyname('ORDMainIdRK').AsString);
      FieldByName('ORDSubIdRK').Value := Trim(CDS_Main.fieldbyname('ORDSubIdRK').AsString);
      FieldByName('ORDMainIdCK').Value := Trim(CDS_Main.fieldbyname('ORDMainIdRK').AsString);
      FieldByName('ORDSubIdCK').Value := Trim(CDS_Main.fieldbyname('ORDSubIdRK').AsString);
      FieldByName('QCOrderNo').Value := CDS_Main.fieldbyname('OrderNo').Value;
      FieldByName('CKOrderNo').Value := CDS_Main.fieldbyname('OrderNo').Value;
      FieldByName('CustomerNoName').Value := CDS_Main.fieldbyname('CustomerNoName').AsString;
      FieldByName('spcode').Value := CDS_Main.fieldbyname('spcode').AsString;
      FieldByName('SPColor').Value := CDS_Main.fieldbyname('SPColor').AsString;
      FieldByName('SPHX').Value := CDS_Main.fieldbyname('SPHX').AsString;
      FieldByName('FactoryName').Value := CDS_Main.fieldbyname('FactoryName').Value;
      FieldByName('FactoryNo').Value := CDS_Main.fieldbyname('FactoryNo').Value;
      FieldByName('SSType').Value := CDS_Main.fieldbyname('SSType').Value;
      FieldByName('SPName').Value := CDS_Main.fieldbyname('SPName').Value;
      FieldByName('SPSpec').Value := CDS_Main.fieldbyname('SPSpec').Value;
      FieldByName('SPCF').Value := CDS_Main.fieldbyname('SPCF').Value;
      FieldByName('SPMF').Value := CDS_Main.fieldbyname('SPMF').Value;
      FieldByName('SPKZ').Value := CDS_Main.fieldbyname('SPKZ').Value;
      FieldByName('RCGangNo').Value := CDS_Main.fieldbyname('GangNo').Value;
      FieldByName('FromFactoryName').Value := CDS_Main.fieldbyname('ToFactoryName').Value;
      FieldByName('FromFactoryNo').Value := CDS_Main.fieldbyname('ToFactoryNo').Value;
      FieldByName('ToFactoryName').Value := CDS_Main.fieldbyname('ToFactoryName').Value;
      FieldByName('ToFactoryNo').Value := CDS_Main.fieldbyname('ToFactoryNo').Value;
      FieldByName('QtyUnit').Value := CDS_Main.fieldbyname('QtyUnit').Value;

      FieldByName('jsUnit').Value := CDS_Main.fieldbyname('QtyUnit').Value;
      FieldByName('PiQty').Value := CDS_Main.fieldbyname('MXKCPiQty').Value;
      FieldByName('Qty').Value := CDS_Main.fieldbyname('MXKCQty').Value;
      FieldByName('FZSPID').Value := CDS_Main.fieldbyname('SPID').Value;
      FieldByName('Note').Value := CDS_Main.fieldbyname('Note').Value;
      FieldByName('CRTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
      FieldByName('CRFlag').Value := '出库';
      FieldByName('CRQtyFlag').Value := -1;
      FieldByName('CRTYPE').Value := '销售出库';
      post;
    end;

    CDS_MX.DisableControls;
    with CDS_MX do
    begin
      First;
      while not Eof do
      begin
        if CDS_MX.FieldByName('SSel').AsBoolean = True then
        begin
          if GetLSNo(ADOQueryTemp, maxno2, 'MCM', 'CK_SXPB_CRMX', 4, 1) = False then
          begin
            Application.MessageBox('取最大号失败!', '提示', 0);
            Exit;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CK_SXPB_CRMX where 1<>1');
            Open;
          end;
          with ADOQueryCmd do
          begin
            Append;
            ADOQueryCmd.FieldByName('MXID').Value := trim(maxno2);
            FieldByName('crno').Value := Trim(maxno);
            FieldByName('mxqty').Value := CDS_MX.fieldbyname('mxqty').value;
            FieldByName('mxganghao').Value := CDS_MX.fieldbyname('mxganghao').value;
            FieldByName('mxqty2').Value := CDS_MX.fieldbyname('mxqty2').value;
            FieldByName('mxmjxh').Value := CDS_MX.fieldbyname('mxmjxh').value;
            FieldByName('mxunit').Value := CDS_MX.fieldbyname('mxunit').value;
            FieldByName('mxfm').Value := CDS_MX.fieldbyname('mxfm').value;

            FieldByName('MXTT').Value := CDS_MX.fieldbyname('MXTT').value;
            FieldByName('MXKH').Value := CDS_MX.fieldbyname('MXKH').value;
            FieldByName('MXKUANNO').Value := CDS_MX.fieldbyname('MXKUANNO').value;
            FieldByName('MXSPEC').Value := CDS_MX.fieldbyname('MXSPEC').value;
            FieldByName('crzt').Value := '出库';
            FieldByName('fzmxid').Value := CDS_MX.fieldbyname('mxid').value;
            Post;
          end;
        end;
        Next;
      end;
    end;
    if Trim(CDS_Main.fieldbyname('QtyUnit').Value) = 'Kg' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update CK_SXPB_CR Set Qty=(select sum(isnull(mxqty,0)) from CK_SXPB_CRMX A where A.crno=CK_SXPB_CR.spid');
        sql.Add('),piqty=(select count(mxid) from CK_SXPB_CRMX A where A.crno=CK_SXPB_CR.spid');
        sql.Add(' ) where SPID=''' + Trim(maxno) + '''');
//            ShowMessage(sql.Text);
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update CK_SXPB_CR Set MXKCQty=(select sum(isnull(Qty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
        sql.Add(',MXKCPiQty=(select sum(isnull(PiQty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
        sql.Add(' where SPID=''' + Trim(CDS_Main.fieldbyname('SPID').Value) + '''');
        ExecSQL;
      end;

    end
    else
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update CK_SXPB_CR Set Qty=(select sum(mxqty2) from CK_SXPB_CRMX A where A.crno=CK_SXPB_CR.spid');
        sql.Add('),piqty=(select count(mxid) from CK_SXPB_CRMX A where A.crno=CK_SXPB_CR.spid');
        sql.Add(' ) where SPID=''' + Trim(maxno) + '''');
//            showmessage(sql.text);
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update CK_SXPB_CR Set MXKCQty=(select sum(isnull(Qty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
        sql.Add(',MXKCPiQty=(select sum(isnull(PiQty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
        sql.Add(' where SPID=''' + Trim(CDS_Main.fieldbyname('SPID').Value) + '''');
        ExecSQL;
      end;

    end;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  CK_SXPB_CR SET chkstatus=''1'',Chktime=getdate() ');
      sql.Add(',Chker=' + quotedstr(Trim(DName)));

      sql.Add(',CRTime=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', Now))));
      sql.Add('where SPID=' + quotedstr(Trim(maxno)));
      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim('出库审核')));
      sql.Add(',' + quotedstr(trim('出库单号：' + Trim(CDS_Main.fieldbyname('SPID').Value))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('EXEC P_Do_CWSC3 ''应收'',' + quotedstr(Trim(maxno)));

      ExecSQL;

    end;
    CDS_MX.EnableControls;

    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('出库成功！', '提示信息', 0);
    InitGrid();
    InitMXGrid(Trim(CDS_Main.fieldbyname('SPID').AsString));
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('出库失败！', '提示信息', 0);
  end;
end;

procedure TfrmCPBCKOutList.MenuItem1Click(Sender: TObject);
begin
  if CDS_mx.IsEmpty then
    Exit;
  CDS_mx.DisableControls;
  with CDS_mx do
  begin
    First;
    while not eof do
    begin
      Edit;
      FieldByName('SSel').Value := True;
      Post;
      Next;
    end;
  end;
  CDS_mx.EnableControls;
end;

procedure TfrmCPBCKOutList.MenuItem3Click(Sender: TObject);
begin
  SelOKNo(cds_mx, False);
end;

procedure TfrmCPBCKOutList.ToolButton8Click(Sender: TObject);
begin
  try
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  CK_SXPB_CR SET QCFLAG=''1'' ');
      sql.Add('where SPID=' + quotedstr(CDS_Main.fieldbyname('SPID').asstring));
      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim('清库存')));
      sql.Add(',' + quotedstr(trim('入库单号：' + trim(edtSPID.Text))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;

    application.MessageBox('操作成功！', '提示信息');
    CDS_Main.Delete;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！', '提示信息', 0);

  end;
end;

procedure TfrmCPBCKOutList.Tv1Column5PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  if cxTabControl1.TabIndex = 0 then
    Exit;
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_Main do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;

    sql.Add('update CK_SXPB_CR set  ' + FFieldName + '=' + QuotedStr(Trim(mvalue)));
    sql.Add('where spid=' + quotedstr(Trim(CDS_Main.fieldbyname('spid').AsString)));

    ExecSQL;
  end;

  tv1.Controller.EditingController.ShowEdit();
  CDS_Main.Delete;
end;

procedure TfrmCPBCKOutList.ToolButton9Click(Sender: TObject);
var
  FSPID, maxno: string;
begin
  if CDS_MX.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  FSPID := Trim(CDS_Main.fieldbyname('SPID').AsString);
//  with ADOQueryTemp do
//  begin
//    Close;
//    sql.Clear;
//    sql.Add('select  A.YFID from YF_Money_CR_Sub A INNER  JOIN YF_Money_CR B ON A.YFID=B.YFID where yfpzno=' + QuotedStr(Trim(CDS_Main.fieldbyname('SPID').AsString)));
//    sql.Add('and B.status=1');
//    Open;
//  end;
//  if ADOQueryTemp.IsEmpty = FALSE then
//  begin
//    Application.MessageBox('财务数据已审核，请先撤销!', '提示', 0);
//    Exit;
//  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if GetLSNo(ADOQueryCmd, maxno, 'CC', 'CK_SXPB_CR', 3, 1) = False then
    begin
      Application.MessageBox('取待出库编号失败!', '提示', 0);
      Exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CK_SXPB_CR where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('SPID').Value := Trim(maxno);
      FieldByName('CKName').Value := '成品布';
      FieldByName('ORDMainIdRK').Value := Trim(CDS_Main.fieldbyname('ORDMainIdRK').AsString);
      FieldByName('ORDSubIdRK').Value := Trim(CDS_Main.fieldbyname('ORDSubIdRK').AsString);
      FieldByName('ORDMainIdCK').Value := Trim(CDS_Main.fieldbyname('ORDMainIdRK').AsString);
      FieldByName('ORDSubIdCK').Value := Trim(CDS_Main.fieldbyname('ORDSubIdRK').AsString);
      FieldByName('QCOrderNo').Value := CDS_Main.fieldbyname('QCOrderNo').Value;
      FieldByName('CKOrderNo').Value := CDS_Main.fieldbyname('CKOrderNo').Value;
      FieldByName('CustomerNoName').Value := CDS_Main.fieldbyname('CustomerNoName').AsString;
      FieldByName('spcode').Value := CDS_Main.fieldbyname('spcode').AsString;
      FieldByName('SPColor').Value := CDS_Main.fieldbyname('SPColor').AsString;
      FieldByName('SPHX').Value := CDS_Main.fieldbyname('SPHX').AsString;
      FieldByName('FactoryName').Value := CDS_Main.fieldbyname('FactoryName').Value;
      FieldByName('FactoryNo').Value := CDS_Main.fieldbyname('FactoryNo').Value;
      FieldByName('SSType').Value := CDS_Main.fieldbyname('SSType').Value;
      FieldByName('SPName').Value := CDS_Main.fieldbyname('SPName').Value;
      FieldByName('SPSpec').Value := CDS_Main.fieldbyname('SPSpec').Value;
      FieldByName('SPCF').Value := CDS_Main.fieldbyname('SPCF').Value;
      FieldByName('SPMF').Value := CDS_Main.fieldbyname('SPMF').Value;
      FieldByName('SPKZ').Value := CDS_Main.fieldbyname('SPKZ').Value;
      FieldByName('RCGangNo').Value := CDS_Main.fieldbyname('RCGangNo').Value;
      FieldByName('FromFactoryName').Value := CDS_Main.fieldbyname('FromFactoryName').Value;
      FieldByName('FromFactoryNo').Value := CDS_Main.fieldbyname('FromFactoryNo').Value;
      FieldByName('ToFactoryName').Value := CDS_Main.fieldbyname('ToFactoryName').Value;
      FieldByName('ToFactoryNo').Value := CDS_Main.fieldbyname('ToFactoryNo').Value;
      FieldByName('QtyUnit').Value := CDS_Main.fieldbyname('QtyUnit').Value;

      FieldByName('jsUnit').Value := CDS_Main.fieldbyname('QtyUnit').Value;
      FieldByName('PiQty').Value := CDS_Main.fieldbyname('MXKCPiQty').Value;
      FieldByName('Qty').Value := CDS_Main.fieldbyname('MXKCQty').Value;
      FieldByName('FZSPID').Value := CDS_Main.fieldbyname('FZSPID').Value;
      FieldByName('Note').Value := CDS_Main.fieldbyname('Note').Value;
      FieldByName('CRTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
      FieldByName('CRFlag').Value := '出库';
      FieldByName('CRQtyFlag').Value := -1;
      FieldByName('CRTYPE').Value := '销售出库';
      post;
    end;

    CDS_MX.DisableControls;
    with CDS_MX do
    begin
      First;
      while not Eof do
      begin
        if CDS_MX.FieldByName('SSel').AsBoolean = True then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('update CK_SXPB_CRMX set crno=' + QuotedStr(Trim(maxno)));
            sql.Add('where MXID=' + QuotedStr(Trim(CDS_MX.fieldbyname('MXID').AsString)));
            execsql;
          end;

        end;
        Next;
      end;
    end;
    if Trim(CDS_Main.fieldbyname('QtyUnit').Value) = 'Kg' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update CK_SXPB_CR Set Qty=(select sum(isnull(mxqty,0)) from CK_SXPB_CRMX A where A.crno=CK_SXPB_CR.spid');
        sql.Add('),piqty=(select count(mxid) from CK_SXPB_CRMX A where A.crno=CK_SXPB_CR.spid');
        sql.Add(' ) where SPID=''' + Trim(maxno) + '''');
//            ShowMessage(sql.Text);
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update CK_SXPB_CR Set MXKCQty=(select sum(isnull(Qty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
        sql.Add(',MXKCPiQty=(select sum(isnull(PiQty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
        sql.Add(' where SPID=''' + Trim(CDS_Main.fieldbyname('SPID').Value) + '''');
        ExecSQL;
      end;

      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update CK_SXPB_CR Set Qty=(select sum(isnull(mxqty,0)) from CK_SXPB_CRMX A where A.crno=CK_SXPB_CR.spid');
        sql.Add('),piqty=(select count(mxid) from CK_SXPB_CRMX A where A.crno=CK_SXPB_CR.spid');
        sql.Add(' ) where SPID=''' + Trim(FSPID) + '''');
//            ShowMessage(sql.Text);
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update CK_SXPB_CR Set MXKCQty=(select sum(isnull(Qty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
        sql.Add(',MXKCPiQty=(select sum(isnull(PiQty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
        sql.Add(' where SPID=''' + Trim(FSPID) + '''');
        ExecSQL;
      end;

    end
    else
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update CK_SXPB_CR Set Qty=(select sum(mxqty2) from CK_SXPB_CRMX A where A.crno=CK_SXPB_CR.spid');
        sql.Add('),piqty=(select count(mxid) from CK_SXPB_CRMX A where A.crno=CK_SXPB_CR.spid');
        sql.Add(' ) where SPID=''' + Trim(maxno) + '''');
//            showmessage(sql.text);
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update CK_SXPB_CR Set MXKCQty=(select sum(isnull(Qty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
        sql.Add(',MXKCPiQty=(select sum(isnull(PiQty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
        sql.Add(' where SPID=''' + Trim(CDS_Main.fieldbyname('SPID').Value) + '''');
        ExecSQL;
      end;

      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update CK_SXPB_CR Set Qty=(select sum(mxqty2) from CK_SXPB_CRMX A where A.crno=CK_SXPB_CR.spid');
        sql.Add('),piqty=(select count(mxid) from CK_SXPB_CRMX A where A.crno=CK_SXPB_CR.spid');
        sql.Add(' ) where SPID=''' + Trim(FSPID) + '''');
//            showmessage(sql.text);
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update CK_SXPB_CR Set MXKCQty=(select sum(isnull(Qty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
        sql.Add(',MXKCPiQty=(select sum(isnull(PiQty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
        sql.Add(' where SPID=''' + Trim(FSPID) + '''');
        ExecSQL;
      end;

    end;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  CK_SXPB_CR SET chkstatus=''1'',Chktime=getdate() ');
      sql.Add(',Chker=' + quotedstr(Trim(DName)));

      sql.Add(',CRTime=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', Now))));
      sql.Add('where SPID=' + quotedstr(Trim(maxno)));
      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim('出库审核')));
      sql.Add(',' + quotedstr(trim('出库单号：' + Trim(CDS_Main.fieldbyname('SPID').Value))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
//    with ADOQueryTemp do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add('EXEC P_Do_CWSC3 ''应收'',' + quotedstr(Trim(maxno)));
//
//      ExecSQL;
//
//    end;
//
//    with ADOQueryTemp do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add('EXEC P_Do_CWSC3 ''应收'',' + quotedstr(Trim(FSPID)));
//
//      ExecSQL;
//
//    end;
    CDS_MX.EnableControls;

    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('出库成功！', '提示信息', 0);
    InitGrid();
    InitMXGrid(Trim(CDS_Main.fieldbyname('SPID').AsString));
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('出库失败！', '提示信息', 0);
  end;

end;

procedure TfrmCPBCKOutList.ToolButton10Click(Sender: TObject);
var
  maxno, maxno2: string;
begin
  if CDS_MX.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if ToFactoryName.Text = '' then
  begin
    Application.MessageBox('退货单位不能为空!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if GetLSNo(ADOQueryCmd, maxno, 'CC', 'CK_SXPB_CR', 3, 1) = False then
    begin
      Application.MessageBox('取待出库编号失败!', '提示', 0);
      Exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CK_SXPB_CR where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('SPID').Value := Trim(maxno);
      FieldByName('CKName').Value := '成品布';
      FieldByName('ORDMainIdRK').Value := Trim(CDS_Main.fieldbyname('ORDMainIdRK').AsString);
      FieldByName('ORDSubIdRK').Value := Trim(CDS_Main.fieldbyname('ORDSubIdRK').AsString);
      FieldByName('ORDMainIdCK').Value := Trim(CDS_Main.fieldbyname('ORDMainIdRK').AsString);
      FieldByName('ORDSubIdCK').Value := Trim(CDS_Main.fieldbyname('ORDSubIdRK').AsString);
      FieldByName('QCOrderNo').Value := CDS_Main.fieldbyname('OrderNo').Value;
      FieldByName('CKOrderNo').Value := CDS_Main.fieldbyname('OrderNo').Value;
      FieldByName('CustomerNoName').Value := CDS_Main.fieldbyname('CustomerNoName').AsString;
      FieldByName('spcode').Value := CDS_Main.fieldbyname('spcode').AsString;
      FieldByName('SPColor').Value := CDS_Main.fieldbyname('SPColor').AsString;
      FieldByName('SPHX').Value := CDS_Main.fieldbyname('SPHX').AsString;
      FieldByName('FactoryName').Value := ToFactoryName.Text;
      FieldByName('FactoryNo').Value := ToFactoryName.hint;
      FieldByName('SSType').Value := CDS_Main.fieldbyname('SSType').Value;
      FieldByName('SPName').Value := CDS_Main.fieldbyname('SPName').Value;
      FieldByName('SPSpec').Value := CDS_Main.fieldbyname('SPSpec').Value;
      FieldByName('SPCF').Value := CDS_Main.fieldbyname('SPCF').Value;
      FieldByName('SPMF').Value := CDS_Main.fieldbyname('SPMF').Value;
      FieldByName('SPKZ').Value := CDS_Main.fieldbyname('SPKZ').Value;
      FieldByName('RCGangNo').Value := CDS_Main.fieldbyname('GangNo').Value;
      FieldByName('FromFactoryName').Value := CDS_Main.fieldbyname('ToFactoryName').Value;
      FieldByName('FromFactoryNo').Value := CDS_Main.fieldbyname('ToFactoryNo').Value;
      FieldByName('ToFactoryName').Value := ToFactoryName.Text;
      FieldByName('ToFactoryNo').Value := ToFactoryName.hint;
      FieldByName('QtyUnit').Value := CDS_Main.fieldbyname('QtyUnit').Value;

      FieldByName('jsUnit').Value := CDS_Main.fieldbyname('QtyUnit').Value;
      FieldByName('PiQty').Value := CDS_Main.fieldbyname('MXKCPiQty').Value;
      FieldByName('Qty').Value := CDS_Main.fieldbyname('MXKCQty').Value;
      FieldByName('FZSPID').Value := CDS_Main.fieldbyname('SPID').Value;
      FieldByName('Note').Value := CDS_Main.fieldbyname('Note').Value;
      FieldByName('CRTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
      FieldByName('CRFlag').Value := '出库';
      FieldByName('CRQtyFlag').Value := -1;
      FieldByName('CRTYPE').Value := '退货出库';
      post;
    end;

    CDS_MX.DisableControls;
    with CDS_MX do
    begin
      First;
      while not Eof do
      begin
        if CDS_MX.FieldByName('SSel').AsBoolean = True then
        begin
          if GetLSNo(ADOQueryTemp, maxno2, 'MCM', 'CK_SXPB_CRMX', 4, 1) = False then
          begin
            Application.MessageBox('取最大号失败!', '提示', 0);
            Exit;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CK_SXPB_CRMX where 1<>1');
            Open;
          end;
          with ADOQueryCmd do
          begin
            Append;
            ADOQueryCmd.FieldByName('MXID').Value := trim(maxno2);
            FieldByName('crno').Value := Trim(maxno);
            FieldByName('mxqty').Value := CDS_MX.fieldbyname('mxqty').value;
            FieldByName('mxganghao').Value := CDS_MX.fieldbyname('mxganghao').value;
            FieldByName('mxqty2').Value := CDS_MX.fieldbyname('mxqty2').value;
            FieldByName('mxmjxh').Value := CDS_MX.fieldbyname('mxmjxh').value;
            FieldByName('mxunit').Value := CDS_MX.fieldbyname('mxunit').value;
            FieldByName('mxfm').Value := CDS_MX.fieldbyname('mxfm').value;

            FieldByName('MXTT').Value := CDS_MX.fieldbyname('MXTT').value;
            FieldByName('MXKH').Value := CDS_MX.fieldbyname('MXKH').value;
            FieldByName('MXKUANNO').Value := CDS_MX.fieldbyname('MXKUANNO').value;
            FieldByName('MXSPEC').Value := CDS_MX.fieldbyname('MXSPEC').value;
            FieldByName('crzt').Value := '出库';
            FieldByName('fzmxid').Value := CDS_MX.fieldbyname('mxid').value;
            Post;
          end;
        end;
        Next;
      end;
    end;
    if Trim(CDS_Main.fieldbyname('QtyUnit').Value) = 'Kg' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update CK_SXPB_CR Set Qty=(select sum(isnull(mxqty,0)) from CK_SXPB_CRMX A where A.crno=CK_SXPB_CR.spid');
        sql.Add('),piqty=(select count(mxid) from CK_SXPB_CRMX A where A.crno=CK_SXPB_CR.spid');
        sql.Add(' ) where SPID=''' + Trim(maxno) + '''');
//            ShowMessage(sql.Text);
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update CK_SXPB_CR Set MXKCQty=(select sum(isnull(Qty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
        sql.Add(',MXKCPiQty=(select sum(isnull(PiQty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
        sql.Add(' where SPID=''' + Trim(CDS_Main.fieldbyname('SPID').Value) + '''');
        ExecSQL;
      end;

    end
    else
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update CK_SXPB_CR Set Qty=(select sum(mxqty2) from CK_SXPB_CRMX A where A.crno=CK_SXPB_CR.spid');
        sql.Add('),piqty=(select count(mxid) from CK_SXPB_CRMX A where A.crno=CK_SXPB_CR.spid');
        sql.Add(' ) where SPID=''' + Trim(maxno) + '''');
//            showmessage(sql.text);
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update CK_SXPB_CR Set MXKCQty=(select sum(isnull(Qty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
        sql.Add(',MXKCPiQty=(select sum(isnull(PiQty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
        sql.Add(' where SPID=''' + Trim(CDS_Main.fieldbyname('SPID').Value) + '''');
        ExecSQL;
      end;

    end;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  CK_SXPB_CR SET chkstatus=''1'',Chktime=getdate() ');
      sql.Add(',Chker=' + quotedstr(Trim(DName)));

      sql.Add(',CRTime=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', Now))));
      sql.Add('where SPID=' + quotedstr(Trim(maxno)));
      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim('出库审核')));
      sql.Add(',' + quotedstr(trim('出库单号：' + Trim(CDS_Main.fieldbyname('SPID').Value))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('EXEC P_Do_CWSC5 ''应付'',' + quotedstr(Trim(maxno)));

      ExecSQL;

    end;
    CDS_MX.EnableControls;

    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('出库成功！', '提示信息', 0);
    InitGrid();
    InitMXGrid(Trim(CDS_Main.fieldbyname('SPID').AsString));
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('出库失败！', '提示信息', 0);
  end;
end;

procedure TfrmCPBCKOutList.ToFactoryNameBtnClick(Sender: TObject);
begin
  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal = 1 then
      begin

        ToFactoryName.Text := Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('COName').AsString);
        ToFactoryName.hint := Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('COCode').AsString);
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;

end;

end.

