unit U_TJCKRKOKQty;

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
  TfrmTJCKRKOKQty = 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;
    v6ToFactoryName: TcxGridDBBandedColumn;
    v6Column7: TcxGridDBBandedColumn;
    cxGridLevel3: TcxGridLevel;
    v6Column15: TcxGridDBBandedColumn;
    v6Column16: TcxGridDBBandedColumn;
    v6Column17: TcxGridDBBandedColumn;
    v6Column18: TcxGridDBBandedColumn;
    ToolButton1: TToolButton;
    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;
    Label9: TLabel;
    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;
    v2Pon: TcxGridDBBandedColumn;
    v2Column2: TcxGridDBBandedColumn;
    v2Column3: TcxGridDBBandedColumn;
    cxGridLevel2: TcxGridLevel;
    Panel2: TPanel;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button7: TButton;
    Button8: TButton;
    Button9: TButton;
    Button10: TButton;
    Label15: TLabel;
    HZNote: TMemo;
    v6Column1: TcxGridDBBandedColumn;
    v6Column2: TcxGridDBBandedColumn;
    Label18: TLabel;
    MPRTCode: TEdit;
    Label19: TLabel;
    MPRTCodeName: TEdit;
    ScrollBox4: TScrollBox;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBBandedTableView;
    v3Fty: TcxGridDBBandedColumn;
    v3Pon: TcxGridDBBandedColumn;
    cxGridDBBandedColumn16: TcxGridDBBandedColumn;
    cxGridDBBandedColumn17: TcxGridDBBandedColumn;
    cxGridLevel4: TcxGridLevel;
    Label20: TLabel;
    RKQtyUnit: TComboBox;
    Panel4: TPanel;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    v3Column1: TcxGridDBBandedColumn;
    v6Column3: TcxGridDBBandedColumn;
    DataSource2: TDataSource;
    ClientDataSet2: TClientDataSet;
    DataSource3: TDataSource;
    ClientDataSet3: TClientDataSet;
    v6Column14: TcxGridDBBandedColumn;
    v6Column19: TcxGridDBBandedColumn;
    ADOQueryMain: TADOQuery;
    Button11: TButton;
    Button12: TButton;
    Label21: TLabel;
    ClotheType10: TComboBox;
    cxGridPopupMenu1: TcxGridPopupMenu;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxGridPopupMenu3: TcxGridPopupMenu;
    ToolButton3: TToolButton;
    Button13: TButton;
    Button14: TButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure ToolButton1Click(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 Button10Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure RKYongTuBtnClick(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Tv6CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure Button1Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure v2FtyPropertiesEditValueChanged(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure OrderNoChange(Sender: TObject);
    procedure MPRTCodeChange(Sender: TObject);
    procedure Button11Click(Sender: TObject);
    procedure Button12Click(Sender: TObject);
    procedure Button9Click(Sender: TObject);
    procedure Tv3MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure v3FtyPropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure Button13Click(Sender: TObject);
    procedure Button14Click(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
    procedure InitGridKK();
  public
    { Public declarations }
  end;

var
  frmTJCKRKOKQty: TfrmTJCKRKOKQty;

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

procedure TfrmTJCKRKOKQty.FormDestroy(Sender: TObject);
begin
  frmTJCKRKOKQty:=nil;
end;

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

procedure TfrmTJCKRKOKQty.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;
procedure TfrmTJCKRKOKQty.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)+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');
    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 RKQty=0 ');
    end else
    if cxTabControl1.TabIndex=1 then
    begin
      sql.Add('  and RKQty>0 ');
    end else
    if cxTabControl1.TabIndex=2 then
    begin
      sql.Add('  and isnull(A.Status,'''')=''协商'' ');
    end else
    if cxTabControl1.TabIndex=3 then
    begin
      sql.Add('  and isnull(A.Status,'''')=''结果'' ');
    end;
   // ShowMessage(SQL.Text);
    Open;
  end;
  SCreateCDS20(ADOQueryMain,ClientDataSet6);
  SInitCDSData20(ADOQueryMain,ClientDataSet6);
end;
procedure TfrmTJCKRKOKQty.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,'');
    SInitCxBandGridComboBoxBySql(ADOQueryTemp,v3fty,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,'');
    SInitCxBandGridComboBoxBySql(ADOQueryTemp,v3Pon,fsj,0,True,'');
  end;

  try
    ADOQuery1.DisableControls;
    with ADOQuery1 do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_Main_MD_KK where MDID='''+Trim(ClientDataSet6.fieldbyname('MDID').AsString)+'''');
      sql.Add(' and isnull(KKTYpe,'''')=''协商'' ');
      Open;
    end;
    SCreateCDS20(ADOQuery1,ClientDataSet2);
    SInitCDSData20(ADOQuery1,ClientDataSet2);
    with ADOQuery1 do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_Main_MD_KK where MDID='''+Trim(ClientDataSet6.fieldbyname('MDID').AsString)+'''');
      sql.Add(' and isnull(KKTYpe,'''')=''结果'' ');
      Open;
    end;
    SCreateCDS20(ADOQuery1,ClientDataSet3);
    SInitCDSData20(ADOQuery1,ClientDataSet3);
   
  finally
    ADOQuery1.EnableControls;
  end;

end;
procedure TfrmTJCKRKOKQty.FormShow(Sender: TObject);
begin
  ReadCxBandedGrid('Tv61',Tv6,'调节仓库');
  ReadCxBandedGrid('Tv21',Tv2,'调节仓库');
  ReadCxBandedGrid('Tv31',Tv3,'调节仓库');

  EndDate.Date:=SGetServerDate(ADOQuery1);
  BegDate.Date:=EndDate.Date-60;
  RKDate.Date:=EndDate.Date;
  RKDate.Checked:=False;
  cxTabControl1.TabIndex:=0;
end;

procedure TfrmTJCKRKOKQty.TBCloseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmTJCKRKOKQty.ToolButton1Click(Sender: TObject);
begin
  {if ClientDataSet6.IsEmpty then Exit;
  if cxTabControl1.TabIndex=2 then Exit;
  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;
      RTSetSaveDataCDSBand(ADOQueryCmd,tv6,ClientDataSet6,'JYOrder_Main_MD',3);
      if ClientDataSet6.FieldByName('CostFlag').AsBoolean=True then
      begin
        FieldByName('CostFlag').Value:=1
      end else
      begin
        FieldByName('CostFlag').Value:=0;
      end;
      FieldByName('CRDate').Value:=SGetServerDateTime(ADOQueryTemp);
      FieldByName('CRFlag').Value:='入库';
      FieldByName('CRType').Value:='正常入库';
      FieldByName('CRPerson').Value:=Trim(DName);
      FieldByName('QtyFlag').Value:=1;
      FieldByName('RKMDID').Value:=ClientDataSet6.fieldbyname('MDID').Value;
      FieldByName('DutyFactoryNo').Value:=ClientDataSet6.fieldbyname('DutyFactoryNo').Value;
      FieldByName('ToFactoryNo').Value:=ClientDataSet6.fieldbyname('ToFactoryNo').Value;
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update JYOrder_Main_MD Set KCPS=(select Sum(PS*QtyFlag) from JYOrder_Main_MD A where A.RKMDID=JYOrder_Main_MD.MDID)');
      sql.Add(',KCQty=(select Sum(Qty*QtyFlag) from JYOrder_Main_MD A where A.RKMDID=JYOrder_Main_MD.MDID)');
      sql.Add(' where MDID='''+Trim(ClientDataSet6.fieldbyname('MDID').AsString)+'''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    if cxTabControl1.TabIndex=0 then
      ClientDataSet6.Delete;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('入库异常!','提示',0);
  end;  }
end;

procedure TfrmTJCKRKOKQty.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 TfrmTJCKRKOKQty.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 TfrmTJCKRKOKQty.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 TfrmTJCKRKOKQty.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('CostFlag').Value:=0;
      FieldByName('CRDate').Value:=NUll;
      FieldByName('CRFlag').Value:='';
      FieldByName('CRType').Value:='';
      FieldByName('QtyFlag').Value:=Null;
      FieldByName('RKMDID').Value:='';
      FieldByName('CRPerson').Value:='';
      FieldByName('KCPS').Value:=0;
      FieldByName('KCQty').Value:=0;
      //FieldByName('DutyFactoryNo').Value:=ClientDataSet6.fieldbyname('DutyFactoryNo').Value;
      //FieldByName('ToFactoryNo').Value:=ClientDataSet6.fieldbyname('ToFactoryNo').Value;
      Post;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    ClientDataSet6.Delete;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('入库撤销异常!','提示',0);
  end;
end;

procedure TfrmTJCKRKOKQty.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,');
      sql.Add('JSKCPS=A.RKPS-A.CKPS,');
      sql.Add('JSKCQty=A.RKQty-A.CKQty');
      //sql.Add(' JSKCPS=(select sum(PS*QtyFlag) from JYOrder_Main_MD JMM where JMM.RKMDID=A.MDID ),');
      //sql.Add(' JSKCQty=(select sum(Qty*QtyFlag) from JYOrder_Main_MD JMM where JMM.RKMDID=A.MDID )');
      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(CRFlag,'''')='''' ');
        sql.Add(' and A.RKQty=0 ');
      end else
      if cxTabControl1.TabIndex=1 then
      begin
       // sql.Add(' and isnull(CRFlag,'''')=''入库'' ');
        sql.Add(' and A.RKQty>0 ');
      end else
      {if cxTabControl1.TabIndex=2 then
      begin
        sql.Add(' and isnull(CRFlag,'''')<>''出库'' ');
      end}
      if cxTabControl1.TabIndex=2 then
      begin
        sql.Add('  and isnull(A.Status,'''')=''协商'' ');
      end else
      if cxTabControl1.TabIndex=3 then
      begin
        sql.Add('  and isnull(A.Status,'''')=''结果'' ');
      end;
      Open;
    end;
    SCreateCDS20(ADOQuery1,ClientDataSet6);
    SInitCDSData20(ADOQuery1,ClientDataSet6);
  end;

end;

procedure TfrmTJCKRKOKQty.Button10Click(Sender: TObject);
begin
  WriteCxBandedGrid('Tv61',Tv6,'调节仓库');
  WriteCxBandedGrid('Tv21',Tv2,'调节仓库');
  WriteCxBandedGrid('Tv31',Tv3,'调节仓库');
  close;

end;

procedure TfrmTJCKRKOKQty.Button8Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmTJCKRKOKQty.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 TfrmTJCKRKOKQty.Button7Click(Sender: TObject);
begin
  if ClientDataSet6.IsEmpty then Exit;
  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;
      RTSetsavedata(ADOQueryCmd,'JYOrder_Main_MD',ScrollBox1,2);
      Post;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    if cxTabControl1.TabIndex=0 then
    begin
      if Trim(RKQty.Text)<>'' then
      begin
        if StrToFloat(RKQty.Text)>0 then
        begin
          ClientDataSet6.Delete;
          InitGridKK();
          SClearData(ScrollBox1,2);
        end;
      end;
    end;
    if cxTabControl1.TabIndex=1 then
    begin
      if Trim(RKQty.Text)<>'' then
      begin
        if StrToFloat(RKQty.Text)=0 then
        begin
          ClientDataSet6.Delete;
          InitGridKK();
          SClearData(ScrollBox1,2);
        end;
      end else
      begin
        ClientDataSet6.Delete;
        InitGridKK();
        SClearData(ScrollBox1,2);
      end;

    end;


  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('入库撤销异常!','提示',0);
  end;
end;

procedure TfrmTJCKRKOKQty.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;
end;

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

end;

procedure TfrmTJCKRKOKQty.Button4Click(Sender: TObject);
begin
  if ClientDataSet3.IsEmpty=False then
  begin
    if ClientDataSet3.Locate('CWHX',True,[]) then
    begin
      Application.MessageBox('财务已核销不能增加数据!','提示',0);
      Exit;
    end;
  end;
  with ClientDataSet3 do
  begin
    Append;
    Post;
  end;
end;

procedure TfrmTJCKRKOKQty.Button3Click(Sender: TObject);
var
  maxno:string;
begin
  Panel2.SetFocus;
  if ClientDataSet6.IsEmpty then Exit;
  if ClientDataSet2.IsEmpty then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    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);
          FieldByName('KKType').Value:='协商';
          RTSetSaveDataCDSBand(ADOQueryCmd,Tv2,ClientDataSet2,'JYOrder_Main_MD_KK',2);
          Post;
        end;
        Edit;
        FieldByName('KKID').Value:=Trim(maxno);
        Post;
        Next;
      end;
    end;
    ClientDataSet2.EnableControls;
    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;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('保存成功!','提示',0);
  except
  end;
end;

procedure TfrmTJCKRKOKQty.Button6Click(Sender: TObject);
var
  maxno:string;
begin
  if ClientDataSet6.IsEmpty then Exit;
  if ClientDataSet3.IsEmpty then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    ClientDataSet3.DisableControls;
    with ClientDataSet3 do
    begin
      First;
      while not Eof do
      begin
        if Trim(ClientDataSet3.fieldbyname('KKID').AsString)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxno,'KZ','JYOrder_Main_MD_KK',3,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取流水号失败!','提示',0);
            Exit;
          end;
        end  else
        begin
          maxno:=Trim(ClientDataSet3.fieldbyname('KKID').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from JYOrder_Main_MD_KK where KKID='''+Trim(ClientDataSet3.fieldbyname('KKID').AsString)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(ClientDataSet3.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('KKType').Value:='结果';
          FieldByName('ZZFactoryNo').Value:=Trim(ClientDataSet3.fieldbyname('ZZFactoryNo').AsString);
          RTSetSaveDataCDSBand(ADOQueryCmd,Tv3,ClientDataSet3,'JYOrder_Main_MD_KK',2);
          Post;
        end;
        Edit;
        FieldByName('KKID').Value:=Trim(maxno);
        Post;
        Next;
      end;
    end;
    ClientDataSet3.EnableControls;
   { 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;  }
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('保存成功!','提示',0);
  except
  end;
end;

procedure TfrmTJCKRKOKQty.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 TfrmTJCKRKOKQty.Button5Click(Sender: TObject);
begin
  if ClientDataSet3.IsEmpty then Exit;
  if ClientDataSet3.IsEmpty=False then
  begin
    if ClientDataSet3.Locate('CWHX',True,[]) then
    begin
      Application.MessageBox('财务已核销不能删除数据!','提示',0);
      Exit;
    end;
  end;
  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(ClientDataSet3.fieldbyname('KKID').AsString)+'''');
    ExecSQL;
  end;
  ClientDataSet3.Delete;
  {if ClientDataSet3.IsEmpty then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      if ClientDataSet2.IsEmpty then
        sql.Add('Update JYOrder_Main_MD Set Status='''' ')
      else
        sql.Add('Update JYOrder_Main_MD Set Status=''协商中'' ');
      sql.Add(' where MDID='''+Trim(ClientDataSet6.fieldbyname('MDID').AsString)+'''');
      ExecSQL;
    end;
  end;}
end;

procedure TfrmTJCKRKOKQty.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 TfrmTJCKRKOKQty.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,ClientDataSet6);
  SInitCDSData20(ADOQueryMain,ClientDataSet6);
end;

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

procedure TfrmTJCKRKOKQty.MPRTCodeChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmTJCKRKOKQty.Button11Click(Sender: TObject);
begin
  if ClientDataSet6.IsEmpty then Exit;
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  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;
  if cxTabControl1.TabIndex<3 then
  begin
    ClientDataSet6.Delete;
  end;
end;

procedure TfrmTJCKRKOKQty.Button12Click(Sender: TObject);
begin
  if ClientDataSet6.IsEmpty then Exit;
  if cxTabControl1.TabIndex<>3 then Exit;
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    if ClientDataSet2.IsEmpty=False then
      sql.Add('Update JYOrder_Main_MD Set Status=''协商'' ')
    else
      sql.Add('Update JYOrder_Main_MD Set Status='''' ');
    sql.Add(' where MDID='''+Trim(ClientDataSet6.fieldbyname('MDID').AsString)+'''');
    ExecSQL;
  end;
  if cxTabControl1.TabIndex=3 then
  begin
    ClientDataSet6.Delete;
  end;
end;

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

procedure TfrmTJCKRKOKQty.Tv3MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if ClientDataSet3.FieldByName('CWHX').AsBoolean=True then
  begin
    Tv3.OptionsSelection.CellSelect:=False;
  end else
  begin
    Tv3.OptionsSelection.CellSelect:=True;
  end;
end;

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

end;

procedure TfrmTJCKRKOKQty.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 TfrmTJCKRKOKQty.Button13Click(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 TfrmTJCKRKOKQty.Button14Click(Sender: TObject);
begin
  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
    Application.MessageBox('已执行调入不能执行入库撤销!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要执行入库撤销吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Main_MD Set RKPS=0,RKQty=0,RKYongTu=Null,RKQtyUnit=Null,RKPrice=0,');
    sql.Add('RKDate=Null,ORDSSMoney=0,ORDSSDate=null,DSDate=null,DSMoney=0,HZNote=null');
    sql.Add('where MDID='''+Trim(ClientDataSet6.fieldbyname('MDID').AsString)+'''');
    ExecSQL;
  end;
  ClientDataSet6.Delete;
end;

end.
