unit U_ContractList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, DBClient, cxCheckBox, cxCalendar, cxSplitter,
  RM_Dataset, RM_System, RM_Common, RM_Class, RM_GridReport, RM_e_Xls,
  Menus, cxPC, cxDropDownEdit, cxTextEdit;

type
  TfrmContractList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    v1OrdDate: TcxGridDBColumn;
    v1ConNo: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    v1MPRTCF: TcxGridDBColumn;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBDataSet1: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    v1CustomerNoName: TcxGridDBColumn;
    v1MPRTCodeName: TcxGridDBColumn;
    v1MPRTMF: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    v1PRTPrice: TcxGridDBColumn;
    ToolButton1: TToolButton;
    v1Column1: TcxGridDBColumn;
    ToolButton2: TToolButton;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    v1Column4: TcxGridDBColumn;
    Panel1: TPanel;
    Label1: TLabel;
    Label4: TLabel;
    Label6: TLabel;
    Label9: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CustomerNoName: TEdit;
    SYRName: TEdit;
    ConNo: TEdit;
    v1Column2: TcxGridDBColumn;
    ComboBox1: TComboBox;
    cxTabControl1: TcxTabControl;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    Label2: TLabel;
    PRTCode: TEdit;
    Label3: TLabel;
    Label5: TLabel;
    PRTSpec: TEdit;
    PRTCF: TEdit;
    v1Column13: TcxGridDBColumn;
    Panel2: TPanel;
    cxSplitter1: TcxSplitter;
    Label7: TLabel;
    Panel3: TPanel;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    VDate: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    VYB: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    VNote: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v2Column3: TcxGridDBColumn;
    ClientDataSet2: TClientDataSet;
    DataSource2: TDataSource;
    v1Column14: TcxGridDBColumn;
    cxGridPopupMenu2: TcxGridPopupMenu;
    v1Column15: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    CDS_PRT10: TClientDataSet;
    RMDBDataSet2: TRMDBDataSet;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Tv1StylesGetContentStyle(Sender: TcxCustomGridTableView;
      ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem;
      out AStyle: TcxStyle);
    procedure v1DeliveryDateCustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure CustomerNoNameChange(Sender: TObject);
    procedure ConNoKeyPress(Sender: TObject; var Key: Char);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
  private
    canshu1:string;
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    { Private declarations }
  public
    FFInt,FCloth:Integer;


    { Public declarations }
  end;

var
  frmContractList: TfrmContractList;

implementation
uses
  U_DataLink,U_ConInPut,U_RTFun,U_SKCRFenPei;

{$R *.dfm}

procedure TfrmContractList.FormDestroy(Sender: TObject);
begin
  frmContractList:=nil;
end;

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

procedure TfrmContractList.FormCreate(Sender: TObject);
begin
  cxgrid1.Align:=alClient;
  //BegDate.DateTime:=SGetServerDateTime(ADOQueryTemp)-7;
  //EndDate.DateTime:=SGetServerDateTime(ADOQueryTemp);
  DQdate:=SGetServerDate(ADOQueryTemp);
  canshu1:=Trim(DParameters1);
end;

procedure TfrmContractList.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('订单合同列表选择1',Tv1,'生产指示单管理');
  WriteCxGrid('定金分配1',Tv1,'生产指示单管理');
end;

procedure TfrmContractList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select A.*,B.* ');
     // sql.Add(' ,DJMoney=(select isnull(Sum(FPMoney),0) from SK_Money_FP FP where FP.WBID=B.Subid and FP.FPType=''定金'' )');
      sql.Add(' from JYOrderCon_Main A  ');
      sql.Add(' inner join JYOrderCon_Sub B on A.MainId=B.MainId');
      SQL.Add('where  A.OrdDate>='''+FormatDateTime('yyyy-MM-dd',BegDate.DateTime)+'''');
      SQL.Add('and  A.OrdDate<'''+FormatDateTime('yyyy-MM-dd',enddate.DateTime+1)+'''');
      if Trim(canshu1)<>'高权限'  then
      begin
        sql.Add('and A.Filler='''+Trim(DName)+'''');
      end;
      if cxTabControl1.TabIndex=0 then
      begin
        SQL.Add(' and isnull(SChker,'''')='''' ');
      end else
      if cxTabControl1.TabIndex=1 then
      begin
        SQL.Add(' and isnull(SChker,'''')<>'''' and ZuoFeiFlag=0 ');
      end  else
      if cxTabControl1.TabIndex=2 then
      begin
        SQL.Add('  and ZuoFeiFlag=1 ');
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;


procedure TfrmContractList.InitForm();
begin
  ReadCxGrid('订单合同列表选择1',Tv1,'生产指示单管理');
  ReadCxGrid('定金分配1',Tv1,'生产指示单管理');
  BegDate.DateTime:=SGetServerDate10(ADOQueryTemp)-7;
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  InitGrid();
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select Top 10* from JYOrderCon_Main Order by FillTime desc');
    Open;
  end;
  ComboBox1.Clear;
  with ADOQueryTemp do
  begin
    First;
    while not Eof do
    begin
      ComboBox1.Items.Add(Trim(ADOQueryTemp.fieldbyname('ConNO').AsString));
      Next;
    end;
  end;
end;

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

procedure TfrmContractList.TBEditClick(Sender: TObject);
begin
  if cxTabControl1.TabIndex<>0 then Exit;
  if Order_Main.IsEmpty then Exit;
  if Trim(Order_Main.fieldbyname('Filler').AsString)<>Trim(DName) then
  begin
    Application.MessageBox('不能操作他人的数据!','提示',0);
    Exit;
  end;
  try
    frmConInPut:=TfrmConInPut.Create(Application);
    with frmConInPut do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmConInPut.Free;
  end;
end;

procedure TfrmContractList.TBDelClick(Sender: TObject);
begin
  if cxTabControl1.TabIndex<>0 then Exit;
  if Order_Main.IsEmpty then Exit;
  if Trim(Order_Main.fieldbyname('Filler').AsString)<>Trim(DName) then
  begin
    Application.MessageBox('不能操作他人的数据!','提示',0);
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from SK_Money_FP where WBID='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
    sql.Add(' and FPType=''定金'' ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('已分配定金不能删除数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if DelData() then
  begin
    Order_Main.Delete;
  end;
end;

function TfrmContractList.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrderCon_Sub where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrderCon_Sub where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      Open;
    end;
    if ADOQueryTemp.IsEmpty then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete JYOrderCon_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
        ExecSQL;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

procedure TfrmContractList.TBExportClick(Sender: TObject);
begin
  //if ADOQueryMain.IsEmpty then Exit;
  //SelExportData(Tv1,ADOQueryMain,'生产指示单列表');
  if Order_Main.IsEmpty then Exit;
  TcxGridToExcel('订单合同',cxGrid1);
end;

procedure TfrmContractList.TBPrintClick(Sender: TObject);
var
   fPrintFile:string;
   EngMoney:string;
begin
  if Order_Main.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\英文合同.rmf' ;
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,B.*,ConMoney=B.PRTOrderQty*B.PRTPrice+isnull(BaoGangFee,0),BaoGangFee10=isnull(B.BaoGangFee,0)  ');
    sql.Add(',KHName=(select KHName from ZH_KH_Info KH where KH.KHNameJC=A.CustomerNoName)');
    sql.Add(',KHNameEng=(select KHNameEng from ZH_KH_Info KH where KH.KHNameJC=A.CustomerNoName)');
    sql.Add(' from JYOrderCon_Main A inner join JYOrderCon_Sub B on A.MainId=B.MainId ');
    sql.Add(' where A.MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryPrint,CDS_Print);
  SInitCDSData20(ADOQueryPrint,CDS_Print);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrderCon_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_PRT10);
  SInitCDSData20(ADOQueryTemp,CDS_PRT10);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    //RMVariables['printer']:=Trim(gUserName);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\英文合同.rmf'),'提示',0);
  end;
end;

procedure TfrmContractList.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select Top 10* from JYOrderCon_Main Order by FillTime desc ');
    Open;
  end;
  ComboBox1.Clear;
  with ADOQueryTemp do
  begin
    First;
    while not Eof do
    begin
      ComboBox1.Items.Add(Trim(ADOQueryTemp.fieldbyname('ConNO').AsString));
      Next;
    end;
  end;
end;

procedure TfrmContractList.TBAddClick(Sender: TObject);
var
  maxno:string;
begin
  try
    frmConInPut:=TfrmConInPut.Create(Application);
    with frmConInPut do
    begin
      PState:=0;
      FMainId:='';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmConInPut.Free;
  end;
end;

procedure TfrmContractList.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmContractList.Tv1StylesGetContentStyle(
  Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
  AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
var
  id,id10:Integer;
begin
  {try
    if Tv1.GroupedItemCount=0 then
    begin
      Id:=Tv1.GetColumnByFieldName('DeliveryDate').Index-tv1.GroupedItemCount;
      Id10:=Tv1.GetColumnByFieldName('SubStatus').Index-tv1.GroupedItemCount;
      if Trim(VarToStr(ARecord.Values[id]))='' then Exit;
      if Id<0 then Exit;
      if ARecord.Values[id10]='完成' then exit;
      if (ARecord.Values[id]-DQdate)>=4 then Exit;
      if ((ARecord.Values[id]-DQdate)>=0) and ((ARecord.Values[id]-DQdate)<4) then
        AStyle:=DataLink_.QHuangSe
      else
      if ARecord.Values[id]-DQdate<0 then
      begin
        AStyle:=DataLink_OrderManage.FenHongS;
      end;
    end else
    begin

    end;
  except
  end; }
end;

procedure TfrmContractList.v1DeliveryDateCustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
 { Id:=TV1.GetColumnByFieldName('DeliveryDate').Index;//;-TV1.GroupedItemCount;
  Id10:=TV1.GetColumnByFieldName('SubStatus').Index;
  if Id<0 then Exit;
  if AViewInfo.GridRecord.Values[Id10]='完成' then Exit;
  if AViewInfo.GridRecord.Values[Id]-SGetServerDate(ADOQueryTemp)>=4 then Exit;
  if ((AViewInfo.GridRecord.Values[id]-SGetServerDate10(ADOQueryTemp))>=0) and ((AViewInfo.GridRecord.Values[id]-SGetServerDate(ADOQueryTemp))<4) then
    ACanvas.Brush.Color:=clYellow
  else
  if (AViewInfo.GridRecord.Values[id])-(SGetServerDate10(ADOQueryTemp)<0) then
  begin
    ACanvas.Brush.Color:=clRed;
  end;
  begin
    ACanvas.Brush.Color:=clRed;
  end else
  if AViewInfo.GridRecord.Values[Id]='Purple' then
  begin
    ACanvas.Brush.Color:=clPurple;
  end else
  if AViewInfo.GridRecord.Values[Id]='Olive' then
  begin
    ACanvas.Brush.Color:=clOlive;
  end else
  if AViewInfo.GridRecord.Values[Id]='Teal' then
  begin
    ACanvas.Brush.Color:=clTeal;
  end else
  if AViewInfo.GridRecord.Values[Id]='Background' then
  begin
    ACanvas.Brush.Color:=clBackground;
  end;   }
end;

procedure TfrmContractList.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    frmConInPut:=TfrmConInPut.Create(Application);
    with frmConInPut do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      ToolBar2.Visible:=False;
      TBSave.Visible:=False;
      ScrollBox1.Enabled:=False;
      Tv1.OptionsSelection.CellSelect:=False;
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmConInPut.Free;
  end;
end;

procedure TfrmContractList.ToolButton2Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    frmConInPut:=TfrmConInPut.Create(Application);
    with frmConInPut do
    begin
      PState:=1;
      CopyInt:=99;
      FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmConInPut.Free;
  end;
end;

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

procedure TfrmContractList.ConNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    if Length(conno.Text)<3 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        SQL.Clear;
        sql.Add('select A.*,B.* ');
        sql.Add(' from JYOrderCon_Main A  ');
        sql.Add(' inner join JYOrderCon_Sub B on A.MainId=B.MainId');
        SQL.Add(' where  1=1');
        if Trim(canshu1)<>'高权限'  then
        begin
          sql.Add('and A.Filler='''+Trim(DName)+'''');
        end;
        if cxTabControl1.TabIndex=0 then
        begin
          SQL.Add(' and isnull(A.SChker,'''')='''' ');
        end else
        if cxTabControl1.TabIndex=1 then
        begin
          SQL.Add(' and isnull(SChker,'''')<>'''' and ZuoFeiFlag=0 ');
        end  else
        if cxTabControl1.TabIndex=2 then
        begin
          SQL.Add('  and ZuoFeiFlag=1 ');
        end;
        sql.Add(' and A.ConNo like '''+'%'+Trim(ConNo.Text)+'%'+'''');
          Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

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

procedure TfrmContractList.ToolButton3Click(Sender: TObject);
var
  FFFId:String;
begin
  if cxTabControl1.TabIndex<>0 then Exit;
  if Order_Main.IsEmpty then Exit;
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrderCon_Main Set SChker='''+Trim(DName)+'''');
    SQL.Add(',SChkTime=getdate() ');
    sql.Add(' where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    ExecSQL;
  end;
  FFFId:=Trim(Order_Main.fieldbyname('MainId').AsString);
  Order_Main.DisableControls;
  with Order_Main do
  begin
    while Locate('MainId',Trim(FFFId),[]) do
    begin
      Order_Main.Delete;
    end;
  end;
  Order_Main.EnableControls;
end;

procedure TfrmContractList.ToolButton4Click(Sender: TObject);
var
  FFFId:String;
begin
  if cxTabControl1.TabIndex<>1 then Exit;
  if Order_Main.IsEmpty then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select * from JYOrderCon_Main ');
    sql.Add(' where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    sql.Add(' and isnull(ChkStatus,'''')<>'''' ');
    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 JYOrderCon_Main Set SChker=NUll');
    SQL.Add(',SChkTime=NUll ');
    sql.Add(' where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    ExecSQL;
  end;
  FFFId:=Trim(Order_Main.fieldbyname('MainId').AsString);
  Order_Main.DisableControls;
  with Order_Main do
  begin
    while Locate('MainId',Trim(FFFId),[]) do
    begin
      Order_Main.Delete;
    end;
  end;
  Order_Main.EnableControls;
end;

procedure TfrmContractList.Tv1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select YS.CRTime,YS.BZType,YS.Money,FP.* ');
    sql.Add(' from SK_Money_FP FP');
    sql.Add(' inner join YF_Money_CR YS on FP.YFID=YS.YFID');
    sql.Add(' where FP.WBID='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
    sql.Add(' and FPType=''定金'' ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet2);
  SInitCDSData20(ADOQueryTemp,ClientDataSet2);
end;

procedure TfrmContractList.Button1Click(Sender: TObject);
begin
  try
    frmSKCRFenPei:=TfrmSKCRFenPei.Create(Application);
    with frmSKCRFenPei do
    begin
      KHName:=Trim(Order_Main.fieldbyname('CustomerNoName').AsString);
      if ShowModal=1 then
      begin
        with Self.ClientDataSet2 do
        begin
          Append;
          FieldByName('YFID').Value:=frmSKCRFenPei.CDS_HZ.fieldbyname('YFID').Value;
          FieldByName('CRTime').Value:=frmSKCRFenPei.CDS_HZ.fieldbyname('CRTime').Value;
          FieldByName('BZType').Value:=frmSKCRFenPei.CDS_HZ.fieldbyname('BZType').Value;
          FieldByName('Money').Value:=frmSKCRFenPei.CDS_HZ.fieldbyname('Money').Value;
          FieldByName('FPDate').Value:=Now;
          Post;
        end;
      end;
    end;
  finally
    frmSKCRFenPei.Free;
  end;
end;

procedure TfrmContractList.Button3Click(Sender: TObject);
var
  maxsubno:String;
begin
  if ClientDataSet2.IsEmpty then Exit;
  if ClientDataSet2.Locate('FPDate',Null,[]) then
  begin
    Application.MessageBox('分配日期不能为空!','提示',0);
    Exit;
  end;
  if ClientDataSet2.Locate('FPMoney',Null,[]) then
  begin
    Application.MessageBox('分配定金不能为空!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要保存数据吗？','提示',32+4)<>IDYES then Exit;
  Panel1.SetFocus;
  try
    ADOQueryCmd.Connection.BeginTrans;
    ///保存子表
    with ClientDataSet2 do
    begin
      First;
      while not Eof do
      begin
        if Trim(ClientDataSet2.fieldbyname('FPId').AsString)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxsubno,'FP','SK_Money_FP',3,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxsubno:=Trim(ClientDataSet2.fieldbyname('FPId').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from SK_Money_FP where FPId='''+Trim(maxsubno)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(ClientDataSet2.fieldbyname('FPId').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('FPId').Value:=Trim(maxsubno);
          FieldByName('YFID').Value:=Trim(ClientDataSet2.fieldbyname('YFID').AsString);
          FieldByName('FPTYpe').Value:='定金';
          FieldByName('FPDate').Value:=ClientDataSet2.fieldbyname('FPDate').Value;
          FieldByName('WBID').Value:=Trim(Order_Main.fieldbyname('SubId').AsString);
          if Trim(ClientDataSet2.fieldbyname('FPMoney').AsString)='' then
          begin
            fieldbyname('FPMoney').Value:=0
          end else
          begin
            fieldbyname('FPMoney').Value:=Trim(ClientDataSet2.fieldbyname('FPMoney').AsString);
          end;
          FieldByName('Note').Value:=Trim(ClientDataSet2.fieldbyname('Note').AsString);
          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate JYOrderCon_Sub Set DJMoney=(select Sum(FPMoney) from SK_Money_FP A ');
          sql.Add(' where A.WBID=JYOrderCon_Sub.SubID and A.FPType=''定金'' )');
          sql.Add('where SubID='''+Trim(Order_Main.fieldbyname('SubID').AsString)+'''');
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYOrderCon_Sub Set WFPDJMoney=isnull(DJMoney,0)-isnull(FPDJMoney,0)');
          sql.Add('where SubID='''+Trim(Order_Main.fieldbyname('SubID').AsString)+'''');
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_CR Set FPDJMoney=(select Sum(FPMoney) from SK_Money_FP A ');
          sql.Add(' where A.YFID=YF_Money_CR.YFID and A.FPType=''定金'' )');
          sql.Add('where YFID='''+Trim(ClientDataSet2.fieldbyname('YFID').AsString)+'''');
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update YF_Money_CR Set WFPMoney=isnull(Money,0)-isnull(FPDJMoney,0)-isnull(FPHKMoney,0)');
          sql.Add('where YFID='''+Trim(ClientDataSet2.fieldbyname('YFID').AsString)+'''');
          ExecSQL;
        end;
        ClientDataSet2.Edit;
        ClientDataSet2.FieldByName('FPId').Value:=Trim(maxsubno);
        //Order_Sub.Post;
        Next;
      end;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('保存成功!','提示',0);
    Exit;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmContractList.Button2Click(Sender: TObject);
begin
  if ClientDataSet2.IsEmpty then Exit;
  if Trim(ClientDataSet2.fieldbyname('FPID').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete SK_Money_FP where FPID='''+Trim(ClientDataSet2.fieldbyname('FPID').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate JYOrderCon_Sub Set DJMoney=(select Sum(FPMoney) from SK_Money_FP A ');
      sql.Add(' where A.WBID=JYOrderCon_Sub.SubID and A.FPType=''定金'' )');
      sql.Add('where SubID='''+Trim(Order_Main.fieldbyname('SubID').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update JYOrderCon_Sub Set WFPDJMoney=isnull(DJMoney,0)-isnull(FPDJMoney,0)');
      sql.Add('where SubID='''+Trim(Order_Main.fieldbyname('SubID').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate YF_Money_CR Set FPDJMoney=(select Sum(FPMoney) from SK_Money_FP A ');
      sql.Add(' where A.YFID=YF_Money_CR.YFID and A.FPType=''定金'' )');
      sql.Add('where YFID='''+Trim(ClientDataSet2.fieldbyname('YFID').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update YF_Money_CR Set WFPMoney=isnull(Money,0)-isnull(FPDJMoney,0)-isnull(FPHKMoney,0)');
      sql.Add('where YFID='''+Trim(ClientDataSet2.fieldbyname('YFID').AsString)+'''');
      ExecSQL;
    end;
  end;
  ClientDataSet2.Delete;
end;

procedure TfrmContractList.ToolButton5Click(Sender: TObject);
var
  FFFId:String;
begin
  if cxTabControl1.TabIndex<>1 then Exit;
  if Order_Main.IsEmpty then Exit;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select * from JYOrderCon_Main where Mainid='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.FieldByName('PChker').AsString<>'' then Exit;
  if ADOQueryTemp.FieldByName('ZuoFeiPerson').AsString<>'' then Exit;
  if ADOQueryTemp.FieldByName('Chker').AsString='' then Exit;
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrderCon_Main Set ZuoFeiPerson='''+Trim(DName)+'''');
    SQL.Add(',ZuoFeiTime=getdate(),ZuoFeiFlag=1 ');
    sql.Add(' where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    ExecSQL;
  end;
  FFFId:=Trim(Order_Main.fieldbyname('MainId').AsString);
  Order_Main.DisableControls;
  with Order_Main do
  begin
    while Locate('MainId',Trim(FFFId),[]) do
    begin
      Order_Main.Delete;
    end;
  end;
  Order_Main.EnableControls;
end;

procedure TfrmContractList.ToolButton6Click(Sender: TObject);
var
  FFFId:String;
begin
  if cxTabControl1.TabIndex<>2 then Exit;
  if Order_Main.IsEmpty then Exit;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select * from JYOrderCon_Main where Mainid='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.FieldByName('PChker').AsString<>'' then
  begin
    Application.MessageBox('已经审批通过，不能撤销!','提示',0);
    Exit;
  end;
  if ADOQueryTemp.FieldByName('ZuoFeiPerson').AsString='' then Exit;
  if Application.MessageBox('确定要执行作废撤销操作吗？','提示',32+4)<>IDYES then exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrderCon_Main Set ZuoFeiPerson=Null');
    SQL.Add(',ZuoFeiTime=Null,ZuoFeiFlag=0 ');
    sql.Add(' where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    ExecSQL;
  end;
  FFFId:=Trim(Order_Main.fieldbyname('MainId').AsString);
  Order_Main.DisableControls;
  with Order_Main do
  begin
    while Locate('MainId',Trim(FFFId),[]) do
    begin
      Order_Main.Delete;
    end;
  end;
  Order_Main.EnableControls;
end;


end.
