unit U_ContractListNX;

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, cxButtonEdit,
  cxTextEdit, cxDropDownEdit, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxBarBuiltInMenu, dxSkinsCore, dxSkinsDefaultPainters, dxSkinscxPCPainter;

type
  TfrmContractListNX = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DS_Main: TDataSource;
    v1OrdDate: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    v1OrdPerson1: TcxGridDBColumn;
    v1ConNo: TcxGridDBColumn;
    CDS_Sub: TClientDataSet;
    RM1: TRMGridReport;
    RMDBDataSet1: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    v1CustomerNoName: TcxGridDBColumn;
    ToolButton1: TToolButton;
    v1Column1: TcxGridDBColumn;
    ToolButton2: TToolButton;
    ADOQueryPrint: TADOQuery;
    PopupMenu2: TPopupMenu;
    N11: TMenuItem;
    N21: TMenuItem;
    N31: TMenuItem;
    Panel1: TPanel;
    Label1: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Salesman: TEdit;
    MPRTCodeName: TEdit;
    ConNo: TEdit;
    MPRTKZ: TEdit;
    MPRTMF: TEdit;
    cxTabControl1: TcxTabControl;
    tchk: TToolButton;
    Tnochk: TToolButton;
    cxSplitter1: TcxSplitter;
    DS_Sub: TDataSource;
    v1Column8: TcxGridDBColumn;
    Label2: TLabel;
    Tqx: TToolButton;
    Tnoqx: TToolButton;
    ToolButton5: TToolButton;
    v1Column10: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    cxGridPopupMenu2: TcxGridPopupMenu;
    CDS_Main: TClientDataSet;
    btnTijiao: TToolButton;
    btnCXTijiao: TToolButton;
    v1Column13: TcxGridDBColumn;
    Label3: TLabel;
    Filler: TEdit;
    ToolButton3: TToolButton;
    SCJHD: TComboBox;
    v1Column14: TcxGridDBColumn;
    ToolButton4: TToolButton;
    BuyName: TComboBox;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    v1XHNo: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    v1PRTPrice: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    TV2Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Label6: TLabel;
    Label7: TLabel;
    BuyConNo: TEdit;
    HZSCSCODE: TEdit;
    Tv1Column3: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure BuyNameChange(Sender: TObject);
    procedure tchkClick(Sender: TObject);
    procedure TnochkClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure TqxClick(Sender: TObject);
    procedure TnoqxClick(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure btnTijiaoClick(Sender: TObject);
    procedure btnCXTijiaoClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
  private
    procedure InitGrid();
    procedure InitForm();
    procedure SetStatus();
    procedure InitSub();
    { Private declarations }
  public
    FFInt, FCloth: Integer;
    canshu1, FGSTT: string;
    { Public declarations }
  end;

//var
//  frmContractListNX: TfrmContractListNX;

implementation

uses
  U_DataLink, U_ConInPutNX, U_Fun, U_ZDYHelp, U_FjList_RZ, U_SysLogHelp;

{$R *.dfm}
procedure TfrmContractListNX.InitSub();
begin

  if CDS_Main.IsEmpty then
    exit;
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from SalesContract_sub ');
    sql.Add('where mainID =' + quotedstr((CDS_Main.fieldbyname('mainID').AsString)));
    open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_Sub);
  SInitCDSData20(ADOQueryTemp, CDS_Sub);
end;

procedure TfrmContractListNX.SetStatus();
begin
  TBAdd.Visible := false;
  tbedit.Visible := false;
  ToolButton2.Visible := false;
  tchk.Visible := false;
  tnochk.Visible := false;
  Tqx.Visible := false;
  TNoqx.Visible := false;
  TBPrint.Visible := False;
  btnTijiao.Visible := False;
  btnCXTijiao.Visible := False;
  toolbutton4.visible := false;
  if Trim(canshu1) = '录入' then
  begin
    ToolButton2.Visible := true;
    TBAdd.Visible := true;
    TBPrint.Visible := True;
    case cxTabControl1.TabIndex of
      0:
        begin
          tbedit.Visible := true;
          Tqx.Visible := True;
          btnTijiao.Visible := True;
          toolbutton4.visible := true;
        end;
      1:
        begin
          btnCXTijiao.Visible := True;
        end;
      2:
        begin

        end;
      3:
        begin
          TNoqx.Visible := true;
        end;
    end;
  end;
  if Trim(canshu1) = '审核' then
  begin
    ToolButton2.Visible := true;
    TBAdd.Visible := true;
    TBPrint.Visible := True;
    case cxTabControl1.TabIndex of
      0:
        begin
          tbedit.Visible := true;
          Tqx.Visible := true;
          btnTijiao.Visible := True;
          toolbutton4.visible := true;
        end;
      1:
        begin
          btnCXTijiao.Visible := True;
          tchk.Visible := True;
        end;
      2:
        begin
          tnochk.Visible := True;
        end;
      3:
        begin
          TNoqx.Visible := true;
        end;
    end;
  end;

end;

procedure TfrmContractListNX.FormDestroy(Sender: TObject);
begin
//  frmContractListNX:=nil;
end;

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

procedure TfrmContractListNX.TBCloseClick(Sender: TObject);
begin
  Close;

  writeCxGrid(self.Caption + tv1.Name + 'SD1', Tv1, '销售合同管理');
  writeCxGrid(self.Caption + tv2.Name + 'SD', Tv2, '销售合同管理');
end;

procedure TfrmContractListNX.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('select A.*');
      sql.Add(',ISSC=cast((case when isnull((select count(WBID) from TP_File X where X.WBID=A.maiNID and X.TFType=''合同''),0)>0 then 1 else 0 end) as bit)');
      SQL.ADD(',ZJINE=round((SELECT SUM(ISNULL(SCSPrice,0)*isnull(scsqty,0)+ISNULL(XIAOGANGFEI,0)+ISNULL(SJFEI,0)) FROM SalesContract_Sub B where A.MAINID=B.MAINID),2)');
      SQL.Add(',HZSCSCODE= STUFF( (select '',''+SCSCode from SalesContract_SUB C where a.MAINID = C.MAINID   for xml path('''')) ,1,1,'''')');
      sql.Add(' from SalesContract_Main A  ');
      SQL.Add('where  A.OrdDate>=''' + FormatDateTime('yyyy-MM-dd', BegDate.DateTime) + '''');
      SQL.Add('and  A.OrdDate<''' + FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1) + '''');
      sql.Add(' and A.SCMType=''内销'' ');

      if Trim(canshu1) = '录入' then
      begin
        sql.Add('and (A.Salesman=''' + Trim(DName) + '''');
        SQL.Add('OR FILLER=''' + Trim(DName) + '''');
        SQL.Add(')');
      end;
      case cxTabControl1.TabIndex of
        0:
          begin
            sql.Add(' and  isnull(A.status,''-1'')=''-1''');
          end;
        1:
          begin
            sql.Add(' and  isnull(A.status,''-1'')=''0''');
          end;
        2:
          begin
            sql.Add(' and  isnull(A.status,''-1'')=''1''');
          end;
        3:
          begin
            sql.Add(' and  isnull(A.status,''-1'')=''2''');
          end;
      end;
//     showmessage(sql.text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmContractListNX.InitForm();
var
  fsj: string;
begin
  readCxGrid(self.Caption + tv1.Name + 'SD1', Tv1, '销售合同管理');
  readCxGrid(self.Caption + tv2.Name + 'SD', Tv2, '销售合同管理');
  fsj := 'select distinct(BuyName) Name from SalesContract_Main where SCMType=''内销''  order by BuyName ';
  SInitComBoxBySql(ADOQueryCmd, BuyName, False, fsj);
//  if FCloth = 1 then
//  begin
//    v1Column4.Visible := True;
//  end
//  else
//  begin
//    v1Column4.Visible := False;
//  end;
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime - 90;
  InitGrid();

end;

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

procedure TfrmContractListNX.TBEditClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  if CANSHU1 <> '审核' then
  begin
    if Trim(CDS_Main.fieldbyname('Filler').AsString) <> Trim(DName) then
    begin
      Application.MessageBox('不能操作他人的数据!', '提示', 0);
      Exit;
    end;
  end;

  try
    frmConInPutNX := TfrmConInPutNX.Create(Application);
    with frmConInPutNX do
    begin
      PState := '修改';
      FMainId := Trim(Self.CDS_Main.fieldbyname('MainId').AsString);
      canshu1 := self.canshu1;
      FGSTT := self.FGSTT;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmConInPutNX.Free;
  end;
end;

procedure TfrmContractListNX.TBPrintClick(Sender: TObject);
var
  fPrintFile: string;
  EngMoney: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  ExportFtErpFile(Trim(SCJHD.Text) + '.rmf', ADOQueryTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(SCJHD.Text) + '.rmf';
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add('EXEC P_Print_XSHT  ');
    SQL.Add('@MAINID=' + QUOTEDSTR(Trim(CDS_Main.fieldbyname('MainId').AsString)));

    Open;
  end;
  if FileExists(fPrintFile) then
  begin
//    RMVariables['EngMoney']:=EngMoney;
    //RMVariables['enddate']:=enddate.DateTime;

    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;
end;

procedure TfrmContractListNX.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmContractListNX.TBAddClick(Sender: TObject);
var
  maxno: string;
begin
  try
    frmConInPutNX := TfrmConInPutNX.Create(Application);
    with frmConInPutNX do
    begin
      PState := '新增';
      FMainId := '';
      canshu1 := self.canshu1;
      FGSTT := self.FGSTT;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmConInPutNX.Free;
  end;
end;

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

procedure TfrmContractListNX.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  if ToolButton1.Visible = False then
    Exit;
  ToolButton1.Click;
end;

procedure TfrmContractListNX.CheckBox1Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmContractListNX.CheckBox2Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmContractListNX.ToolButton1Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  try
    frmConInPutNX := TfrmConInPutNX.Create(Application);
    with frmConInPutNX do
    begin
      PState := '查看';
      FMainId := Trim(Self.CDS_Main.fieldbyname('MainId').AsString);
      FGSTT := self.FGSTT;
      ToolBar2.Visible := False;
      TBSave.Visible := False;
      ScrollBox1.Enabled := False;
      Tv1.OptionsSelection.CellSelect := False;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmConInPutNX.Free;
  end;
end;

procedure TfrmContractListNX.ToolButton2Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  try
    frmConInPutNX := TfrmConInPutNX.Create(Application);
    with frmConInPutNX do
    begin
      PState := '复制';
      FMainId := Trim(Self.CDS_Main.fieldbyname('MainId').AsString);
      canshu1 := self.canshu1;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmConInPutNX.Free;
  end;
end;

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

procedure TfrmContractListNX.tchkClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      DisableControls;
      First;
      while not Eof do
      begin
        if Fieldbyname('Ssel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update  SalesContract_Main SET status=''1'',Chktime=getdate(),Chker=' + quotedstr(trim(DName)));
            sql.Add('where mainID=' + quotedstr(trim(CDS_Main.fieldbyname('mainID').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('conNo').AsString))));
            sql.Add(',' + quotedstr(trim('成功')));
            sql.Add(')');
            execsql;
          end;
        end;
        Next;
      end;
      EnableControls;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('审核成功！', '提示信息');
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('审核失败！', '提示信息');
  end;

end;

procedure TfrmContractListNX.TnochkClick(Sender: TObject);
begin

  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      DisableControls;
      First;
      while not Eof do
      begin
        if Fieldbyname('Ssel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update  SalesContract_Main SET status=''0'',Chktime=null,Chker=null ');
            sql.Add('where mainID=' + quotedstr(trim(CDS_Main.fieldbyname('mainID').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('conNo').AsString))));
            sql.Add(',' + quotedstr(trim('成功')));
            sql.Add(')');
            execsql;
          end;
        end;
        Next;
      end;
      EnableControls;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('撤销审核成功！', '提示信息');
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('撤销审核失败！', '提示信息', 0);
  end;

end;

procedure TfrmContractListNX.cxTabControl1Change(Sender: TObject);
begin
  SetStatus();
  TBRafresh.Click;
end;

procedure TfrmContractListNX.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  InitSub();
end;

procedure TfrmContractListNX.TqxClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      DisableControls;
      First;
      while not Eof do
      begin
        if Fieldbyname('Ssel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update  SalesContract_Main SET status=''2''  ');
            sql.Add('where mainID=' + quotedstr(trim(CDS_Main.fieldbyname('mainID').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('conNo').AsString))));
            sql.Add(',' + quotedstr(trim('成功')));
            sql.Add(')');
            execsql;
          end;
        end;
        Next;
      end;
      EnableControls;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('取消成功！', '提示信息');
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('取消失败！', '提示信息', 0);
  end;
end;

procedure TfrmContractListNX.TnoqxClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      DisableControls;
      First;
      while not Eof do
      begin
        if Fieldbyname('Ssel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update  SalesContract_Main SET status=''-1'' ');
            sql.Add('where mainID=' + quotedstr(trim(CDS_Main.fieldbyname('mainID').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('conNo').AsString))));
            sql.Add(',' + quotedstr(trim('成功')));
            sql.Add(')');
            execsql;
          end;
        end;
        Next;
      end;
      EnableControls;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('撤销合同取消成功！', '提示信息');
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('撤销合同取消失败！', '提示信息', 0);
  end;

end;

procedure TfrmContractListNX.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('MainId').AsString);
      fType := '合同';
      if ShowModal = 1 then
      begin

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

procedure TfrmContractListNX.Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  if AViewInfo.GridRecord.Values[tv1.GetColumnByFieldName('status').Index] = '2' then
    ACanvas.Brush.Color := clLime;
end;

procedure TfrmContractListNX.btnTijiaoClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      DisableControls;
      First;
      while not Eof do
      begin
        if Fieldbyname('Ssel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update  SalesContract_Main SET status=''0'' ');
            sql.Add('where mainID=' + quotedstr(trim(CDS_Main.fieldbyname('mainID').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('conNo').AsString))));
            sql.Add(',' + quotedstr(trim('成功')));
            sql.Add(')');
            execsql;
          end;
        end;
        Next;
      end;
      EnableControls;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('提交成功！', '提示信息');
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('提交失败！', '提示信息', 0);
  end;

end;

procedure TfrmContractListNX.btnCXTijiaoClick(Sender: TObject);
begin

  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      DisableControls;
      First;
      while not Eof do
      begin
        if Fieldbyname('Ssel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update  SalesContract_Main SET status=''-1'' ');
            sql.Add('where mainID=' + quotedstr(trim(CDS_Main.fieldbyname('mainID').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('conNo').AsString))));
            sql.Add(',' + quotedstr(trim('成功')));
            sql.Add(')');
            execsql;
          end;
        end;
        Next;
      end;
      EnableControls;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('撤销提交成功！', '提示信息');
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('撤销提交失败！', '提示信息', 0);
  end;

end;

procedure TfrmContractListNX.ToolButton3Click(Sender: TObject);
begin
  if trim(UpperCase(DName)) <> 'ADMIN' then
    Exit;
  frmSysLogHelp := TfrmSysLogHelp.create(self);
  with frmSysLogHelp do
  begin
    fModel := self.caption;
    showmodal;
    free;
  end;
end;

procedure TfrmContractListNX.ToolButton4Click(Sender: TObject);
begin
  try

    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete SalesContract_Main where MainId=''' + Trim(cds_Main.fieldbyname('MainId').AsString) + '''');
      sql.Add('delete SalesContract_Sub where MainId=''' + Trim(cds_Main.fieldbyname('MainId').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('conno').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      ExecSQL;
    end;
    cds_main.delete;
    application.MessageBox('合同删除成功！', '提示信息');
    ADOQueryCmd.Connection.CommitTrans;
    TBRafresh.Click;

  except
    ADOQueryCmd.Connection.RollbackTrans;

    Application.MessageBox('数据删除异常！', '提示', 0);
  end;
end;

end.

