unit U_SalesContractList;

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,
  dxDateRanges, dxBarBuiltInMenu, U_BaseList, RM_e_Graphic, RM_e_Jpeg,
  RM_E_llPDF, Vcl.Clipbrd, cxContainer, dxCore, cxDateUtils, cxMaskEdit,
  dxScrollbarAnnotations, FrameDateSel, cxProgressBar, dxSkinsDefaultPainters,
  dxSkinsCore, cxButtons;

type
  TfrmSalesContractList = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    GPM_1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DS_1: TDataSource;
    v1OrdDate: TcxGridDBColumn;
    v1OrdPerson1: TcxGridDBColumn;
    v1ConNo: TcxGridDBColumn;
    v1CustomerNoName: TcxGridDBColumn;
    ToolButton1: TToolButton;
    v1Column1: TcxGridDBColumn;
    ToolButton2: TToolButton;
    v1Column4: TcxGridDBColumn;
    pm2: TPopupMenu;
    Panel1: TPanel;
    Label4: TLabel;
    Label9: TLabel;
    cxTabControl1: TcxTabControl;
    tchk: TToolButton;
    Tnochk: TToolButton;
    cxSplitter1: TcxSplitter;
    DS_2: TDataSource;
    v1Column8: TcxGridDBColumn;
    Tqx: TToolButton;
    Tnoqx: TToolButton;
    ToolButton5: TToolButton;
    v1Column10: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    GPM_2: TcxGridPopupMenu;
    CDS_1: TClientDataSet;
    btnTijiao: TToolButton;
    btnCXTijiao: TToolButton;
    v1Column13: TcxGridDBColumn;
    Label3: TLabel;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    pm1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    btnWC: TToolButton;
    btnWCCX: TToolButton;
    Label7: TLabel;
    ADOQueryImage: TADOQuery;
    Tv1Column19: TcxGridDBColumn;
    ToolButton7: TToolButton;
    N3: TMenuItem;
    N4: TMenuItem;
    Tv1Column25: TcxGridDBColumn;
    Tv1Column27: TcxGridDBColumn;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    v1XHNo: TcxGridDBColumn;
    VC_SCSCode: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    v1PRTPrice: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    N5: TMenuItem;
    N6: TMenuItem;
    ConNo: TcxTextEdit;
    BuyConNo: TcxTextEdit;
    BuyName: TcxTextEdit;
    GC_Name: TcxTextEdit;
    TV2Column1: TcxGridDBColumn;
    TV2Column2: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    TV2Column4: TcxGridDBColumn;
    TV2Column5: TcxGridDBColumn;
    ToolButton3: TToolButton;
    TV2Column6: TcxGridDBColumn;
    TV2Column7: TcxGridDBColumn;
    TV2Column8: TcxGridDBColumn;
    frmFrameDateSel1: TfrmFrameDateSel;
    ToolButton4: TToolButton;
    TV2Column9: TcxGridDBColumn;
    TV2Column10: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Label1: TLabel;
    CraftCode: TcxTextEdit;
    Label2: TLabel;
    Saleser: TcxTextEdit;
    TV2Column11: TcxGridDBColumn;
    ToolButton6: TToolButton;
    ADO_2: TADOQuery;
    TV2Column12: 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 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 N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure btnWCClick(Sender: TObject);
    procedure btnWCCXClick(Sender: TObject);
    procedure ConNoKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton7Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure N5Click(Sender: TObject);
    procedure N6Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure TextEdit(Sender: TObject);
    procedure cxGridDBColumn6PropertiesEditValueChanged(Sender: TObject);
    procedure TV2Column3PropertiesEditValueChanged(Sender: TObject);
    procedure ConNoPropertiesChange(Sender: TObject);
    procedure cxButton4Click(Sender: TObject);
    procedure cxButton1Click(Sender: TObject);
    procedure cxButton5Click(Sender: TObject);
    procedure cxButton2Click(Sender: TObject);
    procedure cxButton3Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
  private
    fFlileFlag: string;
    procedure InitGrid();
    procedure SetStatus();
    procedure InitSub();
    procedure InitImage();

    { Private declarations }
  public
    FFInt, FCloth: Integer;
    FAuthority, FGSTT, FSubIdS: string;
    { Public declarations }
  end;

implementation

uses
  U_DataLink, U_SalesConInPut, U_RTFun, U_AttachmentUpload, U_LabelPrint,
  U_ContractImport;

{$R *.dfm}

procedure TfrmSalesContractList.InitImage();
begin

  ADOQueryImage.Close;
  if CDS_1.IsEmpty then
    exit;
  with ADOQueryImage do
  begin
    close;
    sql.Clear;
    sql.Add('select * from TP_File A');
    sql.Add('where WBID=' + quotedstr(trim(CDS_1.fieldbyname('SubId').AsString)));
    sql.Add('and TFType=' + quotedstr(trim('HX')));
    open;
  end;
end;

procedure TfrmSalesContractList.InitSub();
var
  SqlStr: string;
begin
  if CDS_1.IsEmpty then
    exit;
  SqlStr := 'select * from BS_Contract_sub A   where ConMID =' + quotedstr((CDS_1.fieldbyname('ConMID').AsString));
  InitAdoData(ADO_2, Tv2, SqlStr, '', '');
end;

procedure TfrmSalesContractList.N1Click(Sender: TObject);
begin
//  SelOKNo(CDS_2, True);
end;

procedure TfrmSalesContractList.N2Click(Sender: TObject);
begin
//  SelOKNo(CDS_2, True);
end;

procedure TfrmSalesContractList.N3Click(Sender: TObject);
begin

  CopyTVFocusValue(TV1);
end;

procedure TfrmSalesContractList.N4Click(Sender: TObject);
begin
  CopyTVFocusValue(TV2);
end;

procedure TfrmSalesContractList.N5Click(Sender: TObject);
begin
  SelOKNo(CDS_1, True);
end;

procedure TfrmSalesContractList.N6Click(Sender: TObject);
begin
  SelOKNo(CDS_1, False);

end;

procedure TfrmSalesContractList.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;
  btnWC.Visible := False;
  btnWCCX.Visible := False;
  ToolButton6.Visible := False;
  if Trim(FAuthority) = '录入' 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;
        end;
      1:
        begin
          tbedit.Visible := True;
          btnCXTijiao.Visible := True;
        end;
      2:
        begin
          btnWC.Visible := True;
        end;
      3:
        begin
          btnWCCX.Visible := True;
        end;
      4:
        begin
          TNoqx.Visible := True;
        end;
    end;
  end;
  if Trim(FAuthority) = '管理' 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;
        end;
      1:
        begin
          btnCXTijiao.Visible := True;
          tchk.Visible := True;
        end;
      2:
        begin
          tnochk.Visible := True;
          btnWC.Visible := True;
          ToolButton6.Visible := True;
        end;
      3:
        begin
          btnWCCX.Visible := True;
        end;
      4:
        begin
          TNoqx.Visible := true;
        end;
    end;
  end;
  if Trim(FAuthority) = '查询' then
  begin
    TBPrint.Visible := True;
  end;
end;

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

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

procedure TfrmSalesContractList.FormCreate(Sender: TObject);
begin
  inherited;
  frmFrameDateSel1.EndDate.Date := SGetServerDate(ADOQueryTemp);
  frmFrameDateSel1.BegDate.Date := frmFrameDateSel1.EndDate.Date - 90;

end;

procedure TfrmSalesContractList.TBCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmSalesContractList.InitGrid();
var
  Msql, mbegdate, menddate: string;
  SqlStr: string;
begin

  SqlStr := ' select A.*  ';
  SqlStr := SqlStr + ' ,STUFF((SELECT '', '' + GC_Name FROM BS_Contract_sub X where X.conmid=A.conmid FOR XML PATH('''')), 1, 1, '''') AS GC_Name ';
  SqlStr := SqlStr + ' ,STUFF((SELECT '', '' + CraftCode FROM BS_Contract_sub X where X.conmid=A.conmid FOR XML PATH('''')), 1, 1, '''') AS CraftCode ';
  SqlStr := SqlStr + '   from BS_Contract_Main A ';
  SqlStr := SqlStr + ' where A.ConDate>=''' + Trim(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.BegDate.Date)) + '''';
  SqlStr := SqlStr + ' and A.ConDate<''' + Trim(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.enddate.Date + 1)) + '''';

  if Trim(FAuthority) = '录入' then
  begin
    SqlStr := SqlStr + 'and exists(select X.DataCode from BS_OperatorAuthority X where X.DataCode=A.conmid and X.UserId=' + quotedstr(trim(DCode)) + ' )   ';
//    SqlStr := SqlStr + ' and ( SalesId=' + quotedstr(trim(DCode)) + ' or exists (select * from [dbo].[F_Tool_SplitString](TallyId,'','') X where X.RTValue =' + quotedstr(trim(DCode)) + '))';
  end;

  case cxTabControl1.TabIndex of
    0:
      begin
        SqlStr := SqlStr + ' and  isnull(A.status,''0'')=''0''';
      end;
    1:
      begin
        SqlStr := SqlStr + ' and  isnull(A.status,''0'')=''1''';
      end;
    2:
      begin
        SqlStr := SqlStr + ' and  isnull(A.status,''0'')=''9''';
      end;
    3:
      begin
        SqlStr := SqlStr + ' and  isnull(A.status,''0'')=''10''';
      end;
    4:
      begin
        SqlStr := SqlStr + ' and  isnull(A.status,''0'')=''-1''';
      end;
  end;
//  ShowMessage(SqlStr);
  InitCDSData(ADOQueryMain, CDS_1, Tv1, SqlStr, SGetFilters(Panel1, 1, 2), 'conmid');

end;

procedure TfrmSalesContractList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS(ADOQueryMain, CDS_1);
  SInitCDSData(ADOQueryMain, CDS_1);
end;

procedure TfrmSalesContractList.TBPrintClick(Sender: TObject);
var
  WSql: string;
begin
  if CDS_1.IsEmpty then
    Exit;

  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'SalesConPrint';
      FFiltration1 := QuotedStr(Trim(Self.CDS_1.fieldbyname('ConMID').AsString));
      if ShowModal = 1 then
      begin
//        Self.InitGrid();
      end;
    end;
  finally
    frmLabelPrint.Free;
  end;

end;

procedure TfrmSalesContractList.TBEditClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('EXEC  P_Authority_Controls   ');
    sql.Add(' @DataType =''销售合同'' ');
    sql.Add(',@DataCode=' + QuotedStr(Trim(Self.CDS_1.fieldbyname('ConMID').AsString)));
    execsql;
  end;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.ConMID  from BS_Contract_Main A  ');
    sql.Add(' where ConMID=' + QuotedStr(Trim(Self.CDS_1.fieldbyname('ConMID').AsString)));
    sql.Add(' and exists(select X.DataCode from BS_OperatorAuthority X where X.DataCode=A.conmid and X.UserId=' + quotedstr(trim(DCode)) + ' )   ');
//    ShowMessage(sql.Text);
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    application.MessageBox('操作员权限不足', '提示信息', 0);
    exit;
  end;
  if not assigned(frmSalesConInPut) then
  begin
    try
      frmSalesConInPut := TfrmSalesConInPut.Create(Application);
      with frmSalesConInPut do
      begin
        PState := '修改';
        FConMId := Trim(Self.CDS_1.fieldbyname('ConMID').AsString);
        FAuthority := self.FAuthority;
        FGSTT := self.FGSTT;
        fFlileFlag := self.fFlileFlag;
        show;
//        if ShowModal = 1 then
//        begin
//
//        end;
      end;
    finally
//      frmSalesConInPut.Free;
    end;
  end
  else
  begin
    frmSalesConInPut.WindowState := wsMaximized;
    frmSalesConInPut.Show;
  end;

end;

procedure TfrmSalesContractList.TBRafreshClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  InitGrid();
end;

procedure TfrmSalesContractList.TBAddClick(Sender: TObject);
var
  maxno: string;
begin
  if not assigned(frmSalesConInPut) then
  begin
    try
      frmSalesConInPut := TfrmSalesConInPut.Create(Application);
      with frmSalesConInPut do
      begin
        PState := '新增';
        FConMId := '';
        FAuthority := self.FAuthority;
        FGSTT := self.FGSTT;
        fFlileFlag := self.fFlileFlag;
        show;
//        if ShowModal = 1 then
//        begin
//
//        end;
      end;
    finally
//      frmSalesConInPut.Free;
    end;
  end
  else
  begin
    frmSalesConInPut.WindowState := wsMaximized;
    frmSalesConInPut.Show;
  end;

end;

procedure TfrmSalesContractList.FormShow(Sender: TObject);
begin
  inherited;
  fFlileFlag := UserDataFlag + 'HX';
  FAuthority := self.fParameters1;
  readCxGrid(trim(Self.Caption) + 'tv1', Tv1, '销售合同管理');
  readCxGrid(trim(Self.Caption) + 'tv2', Tv2, '销售合同管理');

  SetStatus();
  InitGrid();
end;

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

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

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

procedure TfrmSalesContractList.ConNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    InitGrid();
  end;
end;

procedure TfrmSalesContractList.ConNoPropertiesChange(Sender: TObject);
begin
  if Length(Trim(TcxTextEdit(Sender).Text)) < 4 then
    Exit;
  TBFind.Click;
end;

procedure TfrmSalesContractList.ToolButton1Click(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;

  if not assigned(frmSalesConInPut) then
  begin
    try
      frmSalesConInPut := TfrmSalesConInPut.Create(Application);
      with frmSalesConInPut do
      begin
        PState := '查看';
        FConMId := Trim(Self.CDS_1.fieldbyname('ConMID').AsString);
        FGSTT := self.FGSTT;
        ToolBar2.Visible := False;
        TBSave.Visible := False;
        show;
//        if ShowModal = 1 then
//        begin
//
//        end;
      end;
    finally
//      frmSalesConInPut.Free;
    end;
  end
  else
  begin
    frmSalesConInPut.WindowState := wsMaximized;
    frmSalesConInPut.Show;
  end;

end;

procedure TfrmSalesContractList.ToolButton2Click(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;

  if not assigned(frmSalesConInPut) then
  begin
    try
      frmSalesConInPut := TfrmSalesConInPut.Create(Application);
      with frmSalesConInPut do
      begin
        PState := '复制';
        FConMId := Trim(Self.CDS_1.fieldbyname('ConMID').AsString);
        FAuthority := self.FAuthority;
        fFlileFlag := self.fFlileFlag;
        show;
//        if ShowModal = 1 then
//        begin
//
//        end;
      end;
    finally
//      frmSalesConInPut.Free;
    end;
  end
  else
  begin
    frmSalesConInPut.WindowState := wsMaximized;
    frmSalesConInPut.Show;
  end;

end;

procedure TfrmSalesContractList.ToolButton3Click(Sender: TObject);
begin
  try
    frmContractImport := TfrmContractImport.Create(Application);
    with frmContractImport do
    begin
      if ShowModal = 1 then
      begin
        self.InitGrid();
      end;
    end;
  finally
    frmContractImport.Free;
  end;
end;

procedure TfrmSalesContractList.ToolButton4Click(Sender: TObject);
var
  rtvalues: TArray<string>;
begin
  if CDS_1.IsEmpty then
    exit;
  if cxTabControl1.TabIndex <> 4 then
    exit;
  rtvalues := SelCDSKey(CDS_1, ['ConMID']);
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;

    sql.Add('update BS_Contract_Main set DelId=' + QuotedStr(Trim(DCode)) + ',Deler=' + QuotedStr(Trim(dname)) + ',Deltime=GETDATE()  ');
    sql.Add('where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(rtvalues[0]) + ','','') X where BS_Contract_Main.ConMID=X.RTValue )  ');

    sql.Add('insert into BS_Contract_Main_Del select * from BS_Contract_Main ');
    sql.Add('where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(rtvalues[0]) + ','','') X where BS_Contract_Main.ConMID=X.RTValue )  ');

    sql.Add('delete BS_Contract_Main ');
    sql.Add('where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(rtvalues[0]) + ','','') X where BS_Contract_Main.ConMID=X.RTValue )  ');

    sql.Add('update BS_Contract_Sub set DelId=' + QuotedStr(Trim(DCode)) + ',Deler=' + QuotedStr(Trim(dname)) + ',Deltime=GETDATE()  ');
    sql.Add('where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(rtvalues[0]) + ','','') X where BS_Contract_Sub.ConMID=X.RTValue )  ');

    sql.Add('insert into BS_Contract_Sub_Del select * from BS_Contract_Sub ');
    sql.Add('where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(rtvalues[0]) + ','','') X where BS_Contract_Sub.ConMID=X.RTValue )  ');

    sql.Add('delete BS_Contract_Sub ');
    sql.Add('where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(rtvalues[0]) + ','','') X where BS_Contract_Sub.ConMID=X.RTValue )  ');

    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(rtvalues[0]))));
    sql.Add(',' + quotedstr(trim('成功')));
    sql.Add(')');

    ExecSQL;
  end;

  InitGrid();
end;

procedure TfrmSalesContractList.btnWCClick(Sender: TObject);
var
  MConMIDS: string;
begin
  if CDS_1.IsEmpty then
    exit;
  if not CDS_1.Locate('SSel', True, []) then
  begin
    application.MessageBox('请选择数据', '提示');
    Exit;
  end;
  MConMIDS := SelCDSKey(CDS_1, ['ConMID'])[0];

  try
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' exec P_BS_Contract_SetStatus ');
      sql.Add(' @ConMIDS=' + QuotedStr(MConMIDS));
      sql.Add(',@Operation=''完成'' ');
      Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
      Sql.Add(',@DName=' + quotedstr(Trim(DName)));

      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('合同ID：' + trim(MConMIDS))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    application.MessageBox('完成成功', '提示');
    InitGrid();
  except
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    InitGrid();
  end;

end;

procedure TfrmSalesContractList.BuyNameChange(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS(ADOQueryMain, CDS_1);
  SInitCDSData(ADOQueryMain, CDS_1);
end;

procedure TfrmSalesContractList.tchkClick(Sender: TObject);
var
  MConMIDS: string;
begin
  if CDS_1.IsEmpty then
    exit;
  if not CDS_1.Locate('SSel', True, []) then
  begin
    application.MessageBox('请选择数据', '提示');
    Exit;
  end;
  MConMIDS := SelCDSKey(CDS_1, ['ConMID'])[0];

  try
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' exec P_BS_Contract_SetStatus ');
      sql.Add(' @ConMIDS=' + QuotedStr(MConMIDS));
      sql.Add(',@Operation=''审核'' ');
      Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
      Sql.Add(',@DName=' + quotedstr(Trim(DName)));

      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('合同ID：' + trim(MConMIDS))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    application.MessageBox('审核成功', '提示');
    InitGrid();
  except
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    InitGrid();
  end;

end;

procedure TfrmSalesContractList.TnochkClick(Sender: TObject);
var
  MConMIDS: string;
begin
  if CDS_1.IsEmpty then
    exit;
  if not CDS_1.Locate('SSel', True, []) then
  begin
    application.MessageBox('请选择数据', '提示');
    Exit;
  end;
  MConMIDS := SelCDSKey(CDS_1, ['ConMID'])[0];

  try
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' exec P_BS_Contract_SetStatus ');
      sql.Add(' @ConMIDS=' + QuotedStr(MConMIDS));
      sql.Add(',@Operation=''撤销审核'' ');
      Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
      Sql.Add(',@DName=' + quotedstr(Trim(DName)));

      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('合同ID：' + trim(MConMIDS))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      open;
    end;

    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    application.MessageBox('审核成功', '提示');
    InitGrid();
  except
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    InitGrid();
  end;

end;

procedure TfrmSalesContractList.cxButton1Click(Sender: TObject);
begin
  inherited;
  frmFrameDateSel1.cxButton1Click(Sender);
  InitGrid();
end;

procedure TfrmSalesContractList.cxButton2Click(Sender: TObject);
begin
  inherited;
  frmFrameDateSel1.cxButton2Click(Sender);
  InitGrid();
end;

procedure TfrmSalesContractList.cxButton3Click(Sender: TObject);
begin
  inherited;
  frmFrameDateSel1.cxButton3Click(Sender);
  InitGrid();
end;

procedure TfrmSalesContractList.cxButton4Click(Sender: TObject);
begin
  inherited;
  frmFrameDateSel1.cxButton4Click(Sender);
  InitGrid();
end;

procedure TfrmSalesContractList.cxButton5Click(Sender: TObject);
begin
  inherited;
  frmFrameDateSel1.cxButton5Click(Sender);
  InitGrid();
end;

procedure TfrmSalesContractList.cxGridDBColumn6PropertiesEditValueChanged(Sender: TObject);
var
  FColorNo: string;
begin
  FColorNo := TcxTextEdit(Sender).EditingText;
  try
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' update BS_Contract_Sub   ');
      sql.Add(' set  C_ColorNo=' + FColorNo);
      sql.Add('where ConSID =' + quotedstr((ADO_2.fieldbyname('ConSID').AsString)));
      ExecSQL;
    end;
    ModalResult := 1;
  except
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);

  end;

end;

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

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

procedure TfrmSalesContractList.TV2Column3PropertiesEditValueChanged(Sender: TObject);
var
  FColorConfirm: string;
begin
  FColorConfirm := TcxTextEdit(Sender).EditingText;
  try
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' update BS_Contract_Sub   ');
      sql.Add(' set  C_ColorConfirm=' + FColorConfirm);
      sql.Add('where ConSID =' + quotedstr((ADO_2.fieldbyname('ConSID').AsString)));

      ExecSQL;
    end;
    ModalResult := 1;
  except
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);

  end;

end;

procedure TfrmSalesContractList.TextEdit(Sender: TObject);
var
  FSJPrice: string;
begin
  FSJPrice := TcxTextEdit(Sender).EditingText;
  if StrToFloatDef(FSJPrice, 0) = 0 then
    FSJPrice := '0';
  try
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' update BS_Contract_Sub   ');
      sql.Add(' set  ActualUnitprice=' + FSJPrice);
      sql.Add('where ConSID =' + quotedstr((ADO_2.fieldbyname('ConSID').AsString)));
      ExecSQL;
    end;
    ModalResult := 1;
  except
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);

  end;

end;

procedure TfrmSalesContractList.TqxClick(Sender: TObject);
var
  MConMIDS: string;
begin
  if CDS_1.IsEmpty then
    exit;
  if not CDS_1.Locate('SSel', True, []) then
  begin
    application.MessageBox('请选择数据', '提示');
    Exit;
  end;
  with CDS_1 do
  begin
    DisableControls;
    First;
    MConMIDS := '';
    while not eof do
    begin
      if fieldbyname('SSel').AsBoolean then
      begin
        MConMIDS := MConMIDS + trim(fieldbyname('ConMID').AsString) + ',';
      end;
      next;
    end;
    MConMIDS := copy(MConMIDS, 1, Length(MConMIDS) - 1);
    EnableControls;
  end;

  try
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' exec P_BS_Contract_SetStatus ');
      sql.Add(' @ConMIDS=' + QuotedStr(MConMIDS));
      sql.Add(',@Operation=''取消'' ');
      Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
      Sql.Add(',@DName=' + quotedstr(Trim(DName)));

      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('合同ID：' + trim(MConMIDS))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    application.MessageBox('取消成功', '提示');
    InitGrid();
  except
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    InitGrid();
  end;

end;

procedure TfrmSalesContractList.TnoqxClick(Sender: TObject);
var
  MConMIDS: string;
begin
  if CDS_1.IsEmpty then
    exit;
  if not CDS_1.Locate('SSel', True, []) then
  begin
    application.MessageBox('请选择数据', '提示');
    Exit;
  end;
  with CDS_1 do
  begin
    DisableControls;
    First;
    MConMIDS := '';
    while not eof do
    begin
      if fieldbyname('SSel').AsBoolean then
      begin
        MConMIDS := MConMIDS + trim(fieldbyname('ConMID').AsString) + ',';
      end;
      next;
    end;
    MConMIDS := copy(MConMIDS, 1, Length(MConMIDS) - 1);
    EnableControls;
  end;

  try
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      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('合同ID：' + trim(MConMIDS))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');

      sql.Add(' exec P_BS_Contract_SetStatus ');
      sql.Add(' @ConMIDS=' + QuotedStr(MConMIDS));
      sql.Add(',@Operation=''撤销取消'' ');
      Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
      Sql.Add(',@DName=' + quotedstr(Trim(DName)));
//      ShowMessage(sql.Text);
      open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    application.MessageBox('撤销取消成功', '提示');
    InitGrid();
  except
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    InitGrid();
  end;

end;

procedure TfrmSalesContractList.ToolButton5Click(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  try
    frmAttachmentUpload := TfrmAttachmentUpload.Create(Application);
    with frmAttachmentUpload do
    begin
      fkeyNO := Trim(Self.CDS_1.fieldbyname('ConMID').AsString);
      fType := '合同';
      FEditAuthority := True;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmAttachmentUpload.Free;
  end;
end;

procedure TfrmSalesContractList.ToolButton6Click(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    exit;
//  with ADOQueryTemp do
//  begin
//    Close;
//    SQL.Clear;
//    sql.Add(' select A.ConMID  from BS_Contract_Main A  ');
//    sql.Add(' where ConMID=' + QuotedStr(Trim(Self.CDS_1.fieldbyname('ConMID').AsString)));
//    sql.Add(' and exists(select X.DataCode from BS_OperatorAuthority X where X.DataCode=A.conmid and X.UserId=' + quotedstr(trim(DCode)) + ' )   ');
//
//    Open;
//  end;
//  if ADOQueryTemp.IsEmpty then
//  begin
//    application.MessageBox('操作员权限不足', '提示信息', 0);
//    exit;
//  end;
  try
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' exec P_BS_Contract_backup ');
      sql.Add(' @ConNo=' + QuotedStr(trim(Self.CDS_1.fieldbyname('ConNo').AsString)));
      Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
      Sql.Add(',@DName=' + quotedstr(Trim(DName)));
      open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    cxTabControl1.TabIndex := 0;
  except
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    InitGrid();
  end;

end;

procedure TfrmSalesContractList.ToolButton7Click(Sender: TObject);
begin
  writeCxGrid(trim(Self.Caption) + 'tv1', Tv1, '销售合同管理');
  writeCxGrid(trim(Self.Caption) + 'tv2', Tv2, '销售合同管理');
end;

procedure TfrmSalesContractList.btnWCCXClick(Sender: TObject);
var
  MConMIDS: string;
begin
  if CDS_1.IsEmpty then
    exit;
  if not CDS_1.Locate('SSel', True, []) then
  begin
    application.MessageBox('请选择数据', '提示');
    Exit;
  end;
  MConMIDS := SelCDSKey(CDS_1, ['ConMID'])[0];

  try
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' exec P_BS_Contract_SetStatus ');
      sql.Add(' @ConMIDS=' + QuotedStr(MConMIDS));
      sql.Add(',@Operation=''撤销完成'' ');
      Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
      Sql.Add(',@DName=' + quotedstr(Trim(DName)));

      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('合同ID：' + trim(MConMIDS))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    application.MessageBox('撤销完成成功', '提示');
    InitGrid();
  except
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    InitGrid();
  end;

end;

procedure TfrmSalesContractList.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 TfrmSalesContractList.btnTijiaoClick(Sender: TObject);
var
  MConMIDS: string;
begin
  if CDS_1.IsEmpty then
    exit;
  if not CDS_1.Locate('SSel', True, []) then
  begin
    application.MessageBox('请选择数据', '提示');
    Exit;
  end;
  MConMIDS := SelCDSKey(CDS_1, ['ConMID'])[0];

  try
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' exec P_BS_Contract_SetStatus ');
      sql.Add(' @ConMIDS=' + QuotedStr(MConMIDS));
      sql.Add(',@Operation=''提交'' ');
      Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
      Sql.Add(',@DName=' + quotedstr(Trim(DName)));

      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('合同ID：' + trim(MConMIDS))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      open;
    end;

    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    application.MessageBox('提交成功', '提示');
    InitGrid();
  except
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    InitGrid();
  end;

end;

procedure TfrmSalesContractList.btnCXTijiaoClick(Sender: TObject);
var
  MConMIDS: string;
begin
  if CDS_1.IsEmpty then
    exit;
  if not CDS_1.Locate('SSel', True, []) then
  begin
    application.MessageBox('请选择数据', '提示');
    Exit;
  end;

  MConMIDS := SelCDSKey(CDS_1, ['ConMID'])[0];
  try
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' exec P_BS_Contract_SetStatus ');
      sql.Add(' @ConMIDS=' + QuotedStr(MConMIDS));
      sql.Add(',@Operation=''撤销提交'' ');
      Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
      Sql.Add(',@DName=' + quotedstr(Trim(DName)));

      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('合同ID：' + trim(MConMIDS))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    application.MessageBox('撤销提交成功', '提示');
    InitGrid();
  except
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    InitGrid();
  end;

end;

end.

