unit U_TJCKRKOK;   //8

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxButtonEdit, cxCalendar,
  cxDropDownEdit, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridBandedTableView, cxGridDBBandedTableView, cxClasses, cxControls,
  cxGridCustomView, cxGrid, StdCtrls, ExtCtrls, ComCtrls, ToolWin, DBClient,
  ADODB, cxCheckBox, cxPC, BtnEdit, cxGridDBTableView,
  cxGridCustomPopupMenu, cxGridPopupMenu;

type
  TfrmTJCKRKOK = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label3: TLabel;
    OrderNo: TEdit;
    cxGrid4: TcxGrid;
    Tv6: TcxGridDBBandedTableView;
    cxGridDBBandedColumn1: TcxGridDBBandedColumn;
    cxGridDBBandedColumn2: TcxGridDBBandedColumn;
    Tv6Column1: TcxGridDBBandedColumn;
    Tv6Column2: TcxGridDBBandedColumn;
    v6DutyPerson: TcxGridDBBandedColumn;
    v6DutyFactoryName: TcxGridDBBandedColumn;
    v6DutyReason: TcxGridDBBandedColumn;
    v6Column4: TcxGridDBBandedColumn;
    v6Column5: TcxGridDBBandedColumn;
    v6Column6: TcxGridDBBandedColumn;
    v6Column8: TcxGridDBBandedColumn;
    v6Column9: TcxGridDBBandedColumn;
    v6Column10: TcxGridDBBandedColumn;
    v6Column11: TcxGridDBBandedColumn;
    v6Column12: TcxGridDBBandedColumn;
    v6Column13: TcxGridDBBandedColumn;
    v6ToFactoryName: TcxGridDBBandedColumn;
    v6Column7: TcxGridDBBandedColumn;
    cxGridLevel3: TcxGridLevel;
    v6Column15: TcxGridDBBandedColumn;
    v6Column16: TcxGridDBBandedColumn;
    v6Column17: TcxGridDBBandedColumn;
    v6Column18: TcxGridDBBandedColumn;
    ToolButton2: TToolButton;
    ClientDataSet6: TClientDataSet;
    DataSource6: TDataSource;
    ADOQuery1: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Label1: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    v6Column20: TcxGridDBBandedColumn;
    v6Column21: TcxGridDBBandedColumn;
    cxTabControl1: TcxTabControl;
    ScrollBox1: TScrollBox;
    Label10: TLabel;
    RKPS: TEdit;
    Label11: TLabel;
    ClotheType: TComboBox;
    RKYongTu: TBtnEditA;
    Label12: TLabel;
    Label4: TLabel;
    RKQty: TEdit;
    Label13: TLabel;
    RKDate: TDateTimePicker;
    ScrollBox2: TScrollBox;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBBandedTableView;
    v2Fty: TcxGridDBBandedColumn;
    cxGridDBBandedColumn7: TcxGridDBBandedColumn;
    cxGridDBBandedColumn8: TcxGridDBBandedColumn;
    v2Pon: TcxGridDBBandedColumn;
    v2Column2: TcxGridDBBandedColumn;
    v2Column3: TcxGridDBBandedColumn;
    cxGridLevel2: TcxGridLevel;
    Panel2: TPanel;
    Button1: TButton;
    Button2: TButton;
    ScrollBox3: TScrollBox;
    Label15: TLabel;
    HZNote: TMemo;
    v6Column1: TcxGridDBBandedColumn;
    v6Column2: TcxGridDBBandedColumn;
    cxGrid5: TcxGrid;
    Tv5: TcxGridDBTableView;
    cxGridDBColumn9: TcxGridDBColumn;
    v5Column2: TcxGridDBColumn;
    v5Column7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    v5Column3: TcxGridDBColumn;
    v5Column4: TcxGridDBColumn;
    v5Column5: TcxGridDBColumn;
    v5Column1: TcxGridDBColumn;
    v5Column9: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Panel3: TPanel;
    Label16: TLabel;
    v5Column6: TcxGridDBColumn;
    RKPrice: TEdit;
    Label18: TLabel;
    ConCode: TEdit;
    Label19: TLabel;
    ConName: TEdit;
    Label20: TLabel;
    RKQtyUnit: TComboBox;
    v5Column8: TcxGridDBColumn;
    v6Column3: TcxGridDBBandedColumn;
    DataSource2: TDataSource;
    ClientDataSet2: TClientDataSet;
    DataSource5: TDataSource;
    ClientDataSet5: TClientDataSet;
    v6Column14: TcxGridDBBandedColumn;
    v6Column19: TcxGridDBBandedColumn;
    ADOQueryMain: TADOQuery;
    Label21: TLabel;
    ClotheType10: TComboBox;
    cxGridPopupMenu1: TcxGridPopupMenu;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxGridPopupMenu4: TcxGridPopupMenu;
    ToolButton3: TToolButton;
    Label22: TLabel;
    LiYongXS: TEdit;
    v6Column22: TcxGridDBBandedColumn;
    v6Column23: TcxGridDBBandedColumn;
    Label23: TLabel;
    Label24: TLabel;
    ToolButton1: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure v6Column2PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v6Column14PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton2Click(Sender: TObject);
    procedure OrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure RKYongTuBtnClick(Sender: TObject);
    procedure Tv6CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure v2FtyPropertiesEditValueChanged(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure OrderNoChange(Sender: TObject);
    procedure ConCodeChange(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();

  public
    { Public declarations }
    procedure InitGridKK();
  end;

var
  frmTJCKRKOK: TfrmTJCKRKOK;

implementation
uses
  U_DataLink,U_RTFun, U_GYSList,U_ZDYHelp, U_ModuleNote;
{$R *.dfm}

procedure TfrmTJCKRKOK.FormDestroy(Sender: TObject);
begin
  frmTJCKRKOK:=nil;
end;

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

procedure TfrmTJCKRKOK.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;
procedure TfrmTJCKRKOK.InitGrid();
begin
  with ADOQueryMain do
  begin
    Filtered:=False;
    Close;
    sql.Clear;
    sql.Add('select JM.OrderNo, A.*,B.PSName OrdPSName,C.ConNo,C.FactoryNoName,D.Price, ');
    sql.Add(' JS.PRTHX,JS.PRTColor,JS.PRTColorNo,D.C_Code,D.C_CodeName,ClotheType10=A.ClotheType,');
    sql.Add(' JSKCPS=A.RKPS-A.CKPS,');
    sql.Add(' JSKCQty=A.RKQty-A.CKQty,');
    sql.Add(' JM.MPRTCodeName,MPRTCode=RTrim(JM.MPRTCode)');
    sql.Add(' from JYOrder_Main_MD  A');
    sql.Add(' inner join JYOrder_Main JM on A.OrdMainId=JM.MainId');
    sql.Add(' left join JYOrder_Sub JS on A.OrdSubId=JS.SubId');
    sql.Add(' inner join JYOrder_Process B on A.PSID=B.PSID');
    sql.Add(' left join Contract_Main C on A.ConMainid=C.MainID');
    sql.Add(' left join Contract_Sub D on A.ConSubid=D.SubID');
    if cxTabControl1.TabIndex<>0 then
    begin
      sql.Add(' where A.MDDate>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''');
      sql.Add(' and A.MDDate<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date+1))+'''');
      SQL.Add(' and isnull(A.CKName,'''')=''调节仓库'' ');
    end else
    begin
      SQL.Add(' where isnull(A.CKName,'''')=''调节仓库'' ');
    end;
    if cxTabControl1.TabIndex=0 then
    begin
      sql.Add(' and isnull(A.Status,'''')=''''  ');
    end else
    if cxTabControl1.TabIndex=1 then
    begin
      sql.Add(' and isnull(A.Status,'''')=''已确认''  ');
    end;
    sql.Add(' and isnull(A.ClotheType,'''')=''次品'' ');
    Open;
  end;
  SCreateCDS20(ADOQueryMain,ClientDataSet6);
  SInitCDSData20(ADOQueryMain,ClientDataSet6);
end;
procedure TfrmTJCKRKOK.InitGridKK();
var
  fsj:string;
begin
  if ClientDataSet6.IsEmpty=False then
  begin
    fsj:='select Code=FactoryNo,Name=FactoryName from JYOrder_PCS_Sub where MainId='''+Trim(ClientDataSet6.fieldbyname('ORDMainId').AsString)+''''+
       ' group by FactoryNo,FactoryName';
    SInitCxBandGridComboBoxBySql(ADOQueryTemp,v2fty,fsj,1,True,'');

    fsj:='select Code=PFGenDanPerson,Name=PFGenDanPerson from JYOrder_PCS_Sub where MainId='''+Trim(ClientDataSet6.fieldbyname('ORDMainId').AsString)+''''+
         ' group by PFGenDanPerson';
    SInitCxBandGridComboBoxBySql(ADOQueryTemp,v2Pon,fsj,0,True,'');
    
  end;

  try
    ADOQuery1.DisableControls;
    with ADOQuery1 do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_Main_MD_KK ');
      if ClientDataSet6.IsEmpty=False then
        sql.Add(' where MDID='''+Trim(ClientDataSet6.fieldbyname('MDID').AsString)+'''')
      else
        sql.Add(' where 1<>1');
      Open;
    end;
    SCreateCDS20(ADOQuery1,ClientDataSet2);
    SInitCDSData20(ADOQuery1,ClientDataSet2);
   
    with ADOQuery1 do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Contract_Main A inner join Contract_Sub B on A.MainId=B.MainId ');
      if ClientDataSet6.IsEmpty=False then
        sql.Add(' where A.ConNo like '''+'%'+Trim(ClientDataSet6.fieldbyname('OrderNo').AsString)+'%'+'''')
      else
        sql.Add(' where 1<>1');
      Open;
    end;
    SCreateCDS20(ADOQuery1,ClientDataSet5);
    SInitCDSData20(ADOQuery1,ClientDataSet5);
  finally
    ADOQuery1.EnableControls;
  end;

end;
procedure TfrmTJCKRKOK.FormShow(Sender: TObject);
begin
  ReadCxBandedGrid('Tv6',Tv6,'调节仓库');
  ReadCxBandedGrid('Tv2',Tv2,'调节仓库');

  ReadCxGrid('Tv5',Tv5,'调节仓库');
  EndDate.Date:=SGetServerDate(ADOQuery1);
  BegDate.Date:=EndDate.Date-60;




  RKDate.Date:=EndDate.Date;
  RKDate.Checked:=False;
  //cxTabControl1.TabIndex:=0;
end;

procedure TfrmTJCKRKOK.TBCloseClick(Sender: TObject);
begin
  WriteCxBandedGrid('Tv6',Tv6,'调节仓库');
  WriteCxBandedGrid('Tv2',Tv2,'调节仓库');
  WriteCxGrid('Tv5',Tv5,'调节仓库');
  close;
end;

procedure TfrmTJCKRKOK.cxTabControl1Change(Sender: TObject);
begin
  InitGrid();
  if ClientDataSet6.IsEmpty then
  begin
    SClearData(ScrollBox1,2);
  end;
  InitGridKK();
  if cxTabControl1.TabIndex=0 then
  begin
    RKPS.ReadOnly:=False;
    RKQty.ReadOnly:=False;
  end;
end;

procedure TfrmTJCKRKOK.v6Column2PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmGYSList:=TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      if ShowModal=1 then
      begin
        with Self.ClientDataSet6 do
        begin
          Edit;
          FieldByName('DutyFactoryNo').Value:=Trim(frmGYSList.Order_Main.fieldbyname('ZKID').AsString) ;
          FieldByName('DutyFactoryName').Value:=Trim(frmGYSList.Order_Main.fieldbyname('KHNameJC').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmGYSList.Free;
  end;
end;

procedure TfrmTJCKRKOK.v6Column14PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmGYSList:=TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      if ShowModal=1 then
      begin
        with Self.ClientDataSet6 do
        begin
          Edit;
          FieldByName('ToFactoryNo').Value:=Trim(frmGYSList.Order_Main.fieldbyname('ZKID').AsString) ;
          FieldByName('ToFactoryName').Value:=Trim(frmGYSList.Order_Main.fieldbyname('KHNameJC').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmGYSList.Free;
  end;
end;

procedure TfrmTJCKRKOK.ToolButton2Click(Sender: TObject);
begin
  if ClientDataSet6.IsEmpty then Exit;
  if cxTabControl1.TabIndex<>1 then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select * from JYOrder_Main_MD where RKMDID='''+Trim(ClientDataSet6.fieldbyname('MDID').AsString)+'''');
    sql.Add(' and isnull(CRFlag,'''')=''出库'' ');
    Open;
  end;
  if ADOQueryTemp.RecordCount>0 then
  begin
    Application.MessageBox('已有出库数据不能撤销!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要执行确认撤销操作吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_Main_MD where MDID='''+Trim(ClientDataSet6.fieldbyname('MDID').AsString)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Edit;
      FieldByName('CRDate').Value:=NUll;
      FieldByName('CRFlag').Value:='';
      FieldByName('CRType').Value:='';
      FieldByName('QtyFlag').Value:=Null;
      FieldByName('RKMDID').Value:='';
      FieldByName('CRPerson').Value:='';
      FieldByName('Status').Value:=Null;
      Post;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    ClientDataSet6.Delete;
    InitGridKK();
    SClearData(ScrollBox1,2);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('确认撤销异常!','提示',0);
  end;
end;

procedure TfrmTJCKRKOK.OrderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    if Trim(OrderNo.Text)='' then Exit;
    with ADOQuery1 do
    begin
      Close;
      sql.Clear;
      sql.Add('select JM.OrderNo, A.*,B.PSName OrdPSName,C.ConNo,C.FactoryNoName,D.Price, ');
      sql.Add(' JS.PRTHX,JS.PRTColor,JS.PRTColorNo,D.C_Code,D.C_CodeName,ClotheType10=A.ClotheType,');
      sql.Add(' JSKCPS=A.RKPS-A.CKPS,');
      sql.Add(' JSKCQty=A.RKQty-A.CKQty,');
      sql.Add(' JM.MPRTCodeName,MPRTCode=RTrim(JM.MPRTCode)+RTrim(JM.MPRTGY)');
      sql.Add(' from JYOrder_Main_MD  A');
      sql.Add(' inner join JYOrder_Main JM on A.OrdMainId=JM.MainId');
      sql.Add(' left join JYOrder_Sub JS on A.OrdSubId=JS.SubId');
      sql.Add(' inner join JYOrder_Process B on A.PSID=B.PSID');
      sql.Add(' inner join Contract_Main C on A.ConMainid=C.MainID');
      sql.Add(' inner join Contract_Sub D on A.ConSubid=D.SubID');
      sql.Add(' where JM.OrderNo like '''+'%'+Trim(OrderNo.Text)+'%'+'''');
      SQL.Add(' and isnull(A.CKName,'''')=''调节仓库'' ');
      if cxTabControl1.TabIndex=0 then
      begin
        sql.Add(' and isnull(Status,'''')=''''  ');
      end else
      if cxTabControl1.TabIndex=1 then
      begin
        sql.Add(' and isnull(Status,'''')=''已确认''  ');
      end;
      Open;
    end;
    SCreateCDS20(ADOQuery1,ClientDataSet6);
    SInitCDSData20(ADOQuery1,ClientDataSet6);
  end;

end;

procedure TfrmTJCKRKOK.RKYongTuBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='RKYongTu';
      flagname:='用途';
      if ShowModal=1 then
      begin
        RKYongTu.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmTJCKRKOK.Tv6CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
var
  fsj:string;
begin
  fsj:='select Code=FactoryNo,Name=FactoryName from JYOrder_Main_MD where ORDMainId='''+Trim(ClientDataSet6.fieldbyname('ORDMainId').AsString)+''''+
       ' group by FactoryNo,FactoryName';
  SInitCxBandGridComboBoxBySql(ADOQueryTemp,v2fty,fsj,1,True,'');
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main_MD where MDID='''+Trim(ClientDataSet6.fieldbyname('MDID').AsString)+'''');
    Open;
  end;
  SCSHDataNew(ADOQuery1,ScrollBox1,2);
  InitGridKK();
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main_MD_Out where MDID='''+Trim(ClientDataSet6.fieldbyname('MDID').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    RKPS.ReadOnly:=True;
    RKQty.ReadOnly:=True;
  end;
  if ClientDataSet6.FieldByName('OtherOrdFlag').AsBoolean=True then
  begin
    LiYongXS.ReadOnly:=False;
  end else
  begin
    LiYongXS.ReadOnly:=True;
    LiYongXS.Text:='';
  end;
end;

procedure TfrmTJCKRKOK.Button1Click(Sender: TObject);
begin
  if ClientDataSet6.IsEmpty then Exit;
  with ClientDataSet2 do
  begin
    Append;
    Post;
  end;

end;

procedure TfrmTJCKRKOK.Button2Click(Sender: TObject);
begin
  if ClientDataSet2.IsEmpty then Exit;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    SQL.Add('delete JYOrder_Main_MD_KK where KKID='''+Trim(ClientDataSet2.fieldbyname('KKID').AsString)+'''');
    ExecSQL;
  end;
  ClientDataSet2.Delete;
  if ClientDataSet2.IsEmpty then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_Main_MD ');
      sql.Add(' where MDID='''+Trim(ClientDataSet6.fieldbyname('MDID').AsString)+'''');
      Open;
    end;
    if Trim(ADOQueryTemp.fieldbyname('Status').AsString)='协商' then
    //if ClientDataSet3.IsEmpty then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update JYOrder_Main_MD Set Status='''' ');
        sql.Add(' where MDID='''+Trim(ClientDataSet6.fieldbyname('MDID').AsString)+'''');
        ExecSQL;
      end;
    end;
  end;
end;

procedure TfrmTJCKRKOK.v2FtyPropertiesEditValueChanged(Sender: TObject);
var
  mvalue,fsj:string;
begin
  mvalue:=TcxComboBox(Sender).EditingText;
  with ClientDataSet2 do
  begin
    Edit;
    FieldByName('ZZFactoryName').Value:=Trim(mvalue);
    fsj:=TA((v2Fty.Properties as TcxComboBoxProperties).Items.Objects[(v2Fty.Properties as TcxComboBoxProperties).Items.IndexOf(Trim(mvalue))]).S;
    FieldByName('ZZFactoryNo').Value:=Trim(fsj);
    Post;
  end;

end;

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

procedure TfrmTJCKRKOK.OrderNoChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmTJCKRKOK.ConCodeChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmTJCKRKOK.ToolButton3Click(Sender: TObject);
begin
  try
    frmModuleNote:=TfrmModuleNote.Create(Application);
    with frmModuleNote do
    begin
      flag:='待处理仓库确认';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmModuleNote.Free;
  end;
end;

procedure TfrmTJCKRKOK.ToolButton1Click(Sender: TObject);
var
  Freal:Double;
  maxno:string;
begin
  if ClientDataSet6.IsEmpty then Exit;
  if Trim(LiYongXS.Text)<>'' then
  begin
    if TryStrToFloat(Trim(LiYongXS.Text),Freal)=False then
    begin
      Application.MessageBox('再利用系数非法!','提示',0);
      Exit;
    end;
  end;
  if Trim(RKPrice.Text)<>'' then
  begin
    if TryStrToFloat(Trim(RKPrice.Text),Freal)=False then
    begin
      Application.MessageBox('再利用价格非法!','提示',0);
      Exit;
    end;
  end;
  if ClientDataSet2.IsEmpty then
  begin
    if Application.MessageBox('扣款数据为空,确定要保存数据吗？','提示',32+4)<>IDYES then Exit;
  end else
  begin
    if Application.MessageBox('确定要保存数据吗？','提示',32+4)<>IDYES then Exit;
  end;

  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_Main_MD where MDID='''+Trim(ClientDataSet6.fieldbyname('MDID').AsString)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Edit;
      RTSetsavedata(ADOQueryCmd,'JYOrder_Main_MD',ScrollBox1,2);
      FieldByName('Status').Value:='已确认';
      Post;
    end;

    ClientDataSet2.DisableControls;
    with ClientDataSet2 do
    begin
      First;
      while not Eof do
      begin
        if Trim(ClientDataSet2.fieldbyname('KKID').AsString)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxno,'KK','JYOrder_Main_MD_KK',3,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取流水号失败!','提示',0);
            Exit;
          end;
        end  else
        begin
          maxno:=Trim(ClientDataSet2.fieldbyname('KKID').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from JYOrder_Main_MD_KK where KKID='''+Trim(ClientDataSet2.fieldbyname('KKID').AsString)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(ClientDataSet2.fieldbyname('KKID').AsString)='' then
          begin
            Append;
            FieldByName('Filler').Value:=Trim(DName);
          end else
          begin
            Edit;
            FieldByName('Editer').Value:=Trim(DName);
            FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
          end;
          FieldByName('KKID').Value:=Trim(maxno);
          FieldByName('MDID').Value:=Trim(ClientDataSet6.fieldbyname('MDID').AsString);
          FieldByName('ZZFactoryNo').Value:=Trim(ClientDataSet2.fieldbyname('ZZFactoryNo').AsString);
          RTSetSaveDataCDSBand(ADOQueryCmd,Tv2,ClientDataSet2,'JYOrder_Main_MD_KK',2);
          Post;
        end;
        Edit;
        FieldByName('KKID').Value:=Trim(maxno);
        Post;
        Next;
      end;
    end;
    ClientDataSet2.EnableControls;
    if ClientDataSet2.IsEmpty=False then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update JYOrder_Main_MD Set ChuLiStatus=''有扣款'' ');
        sql.Add(' where MDID='''+Trim(ClientDataSet6.fieldbyname('MDID').AsString)+'''');
        ExecSQL;
      end;
    end else
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update JYOrder_Main_MD Set ChuLiStatus=''无扣款'' ');
        sql.Add(' where MDID='''+Trim(ClientDataSet6.fieldbyname('MDID').AsString)+'''');
        ExecSQL;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    if cxTabControl1.TabIndex=0 then
    begin
      ClientDataSet6.Delete;
      SClearData(ScrollBox1,2);
    end;
    InitGridKK();
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('入库撤销异常!','提示',0);
  end;
end;

end.
