unit U_finishSalesContractList;

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, dxSkinsCore,
  dxSkinsDefaultPainters, cxNavigator, dxDateRanges, dxBarBuiltInMenu,
  U_BaseList, RM_e_Graphic, RM_e_Jpeg, RM_E_llPDF, Vcl.Clipbrd, cxContainer,
  dxCore, cxDateUtils, cxMaskEdit, dxSkinOffice2013White, dxSkinSharpPlus,
  dxSkinWXI, dxScrollbarAnnotations, cxImage, cxDBEdit, cxProgressBar,
  cxCurrencyEdit;

type
  TfrmfinishSalesContractList = 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;
    v1ConNo: TcxGridDBColumn;
    CDS_2: TClientDataSet;
    v1CustomerNoName: TcxGridDBColumn;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    v1Column4: TcxGridDBColumn;
    pm2: TPopupMenu;
    Panel1: TPanel;
    Label4: TLabel;
    Label9: TLabel;
    cxTabControl1: TcxTabControl;
    tchk: TToolButton;
    Tnochk: TToolButton;
    cxSplitter1: TcxSplitter;
    DS_2: TDataSource;
    Tqx: TToolButton;
    Tnoqx: TToolButton;
    ToolButton5: TToolButton;
    GPM_2: TcxGridPopupMenu;
    CDS_1: TClientDataSet;
    btnTijiao: TToolButton;
    btnCXTijiao: TToolButton;
    Label3: TLabel;
    pm1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    btnWC: TToolButton;
    btnWCCX: TToolButton;
    Label7: TLabel;
    ADOQueryImage: TADOQuery;
    IsJYTime: TCheckBox;
    ToolButton7: TToolButton;
    N3: TMenuItem;
    N4: TMenuItem;
    N5: TMenuItem;
    N6: TMenuItem;
    EndDate: TcxDateEdit;
    ConNo: TcxTextEdit;
    BuyConNo: TcxTextEdit;
    BuyName: TcxTextEdit;
    MiddleName: TcxTextEdit;
    BegDate: TcxDateEdit;
    DSImage: TDataSource;
    ADOQuery1: TADOQuery;
    Panel2: TPanel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    v1XHNo: TcxGridDBColumn;
    VC_SCSCode: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1ConUnit: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Order_Sub: TClientDataSet;
    Tv1DlyDate: TcxGridDBColumn;
    TV2C_WidthUnit: TcxGridDBColumn;
    TV2density: TcxGridDBColumn;
    Tv1Status: TcxGridDBColumn;
    ToolButton3: TToolButton;
    Tv3: TcxGridDBTableView;
    cxGrid3Level1: TcxGridLevel;
    cxGrid3: TcxGrid;
    Tv3Column1: TcxGridDBColumn;
    Tv3Column2: TcxGridDBColumn;
    Tv3Column3: TcxGridDBColumn;
    CDS_3: TClientDataSet;
    DS_3: TDataSource;
    GPM_3: TcxGridPopupMenu;
    TV2ShaZhi: TcxGridDBColumn;
    TV2C_Color: TcxGridDBColumn;
    TV2SampleTime: TcxGridDBColumn;
    TV2ConPrice: TcxGridDBColumn;
    TV2Currency: TcxGridDBColumn;
    TV2ConTotalAmount: TcxGridDBColumn;
    Tv1InspStandard: TcxGridDBColumn;
    Tv1InspScore: TcxGridDBColumn;
    TbDel: TToolButton;
    Tv1C_Code: TcxGridDBColumn;
    Tv1C_Name: TcxGridDBColumn;
    Tv1C_Composition: TcxGridDBColumn;
    Tv1C_Spec: TcxGridDBColumn;
    Tv1density: TcxGridDBColumn;
    Tv1C_Width: TcxGridDBColumn;
    Tv1C_WidthUnit: TcxGridDBColumn;
    Tv1ConQty: TcxGridDBColumn;
    Tv1ConUnit: TcxGridDBColumn;
    Tv1ShaZhi: TcxGridDBColumn;
    Tv1C_Color: TcxGridDBColumn;
    Tv1SampleTime: TcxGridDBColumn;
    Tv1ConPrice: TcxGridDBColumn;
    Tv1Currency: TcxGridDBColumn;
    Tv1ConTotalAmount: 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 TV2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure ToolButton3Click(Sender: TObject);
    procedure TbDelClick(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_finishSalesConInput, U_HTImport;

{$R *.dfm}

procedure TfrmfinishSalesContractList.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 TfrmfinishSalesContractList.InitSub();
begin
  if CDS_1.IsEmpty then
    exit;
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from CP_Contract_sub A  ');
    sql.Add('where ConMID =' + quotedstr((CDS_1.fieldbyname('ConMID').AsString)));
//    ShowMessage(SQL.Text);
    open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_2);
//  showMessage(intTostr(CDS_2.RecordCount));
  SInitCDSData(ADOQueryTemp, CDS_2);
//  showMessage(intTostr(CDS_2.RecordCount));

  TV2.DataController.Filter.Clear;
end;

procedure TfrmfinishSalesContractList.N1Click(Sender: TObject);
begin
  SelOKNoFiler(Tv1, True); //全选
end;

procedure TfrmfinishSalesContractList.N2Click(Sender: TObject);
begin
  SelOKNoFiler(Tv1, False); //全弃
end;

procedure TfrmfinishSalesContractList.N3Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(CDS_1.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

procedure TfrmfinishSalesContractList.N4Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(CDS_1.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

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

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

end;

procedure TfrmfinishSalesContractList.SetStatus();
begin
  TBAdd.Enabled := false;
  tbedit.Enabled := false;
  ToolButton2.Enabled := false;
  tchk.Enabled := false;
  tnochk.Enabled := false;
  Tqx.Enabled := false;
  TNoqx.Enabled := false;
  TBPrint.Enabled := False;
  btnTijiao.Enabled := False;
  btnCXTijiao.Enabled := False;
  btnWC.Enabled := False;
  btnWCCX.Enabled := False;
  TbDel.Enabled := False;
  if Trim(FAuthority) = '录入' then
  begin
    ToolButton2.Enabled := true;
    TBAdd.Enabled := true;
    TBPrint.Enabled := True;
    panel2.Visible := True;

    Tv1C_Code.Visible := False;
    Tv1C_Name.Visible := False;
    Tv1C_Composition.Visible := False;
    Tv1C_Spec.Visible := False;
    Tv1density.Visible := False;
    Tv1C_Width.Visible := False;
    Tv1C_WidthUnit.Visible := False;
    Tv1ConQty.Visible := False;
    Tv1ConUnit.Visible := False;
    Tv1ShaZhi.Visible := False;
    Tv1C_Color.Visible := False;
    Tv1SampleTime.Visible := False;
    Tv1ConPrice.Visible := False;
    Tv1Currency.Visible := False;
    Tv1ConTotalAmount.Visible := False;

    case cxTabControl1.TabIndex of
      0:
        begin
          tbedit.Enabled := true;
          Tqx.Enabled := True;
          btnTijiao.Enabled := True;
        end;
      1:
        begin
          tbedit.Enabled := True;
          btnCXTijiao.Enabled := True;
        end;
      2:
        begin
          btnWC.Enabled := True;
        end;
      3:
        begin
          btnWCCX.Enabled := True;
        end;
      4:
        begin
          TNoqx.Enabled := True;
          TbDel.Enabled := True;
        end;
    end;
  end;
  if Trim(FAuthority) = '管理' then
  begin
    ToolButton2.Enabled := true;
    TBAdd.Enabled := true;
    panel2.Visible := True;
    TBPrint.Enabled := True;


    Tv1C_Code.Visible := False;
    Tv1C_Name.Visible := False;
    Tv1C_Composition.Visible := False;
    Tv1C_Spec.Visible := False;
    Tv1density.Visible := False;
    Tv1C_Width.Visible := False;
    Tv1C_WidthUnit.Visible := False;
    Tv1ConQty.Visible := False;
    Tv1ConUnit.Visible := False;
    Tv1ShaZhi.Visible := False;
    Tv1C_Color.Visible := False;
    Tv1SampleTime.Visible := False;
    Tv1ConPrice.Visible := False;
    Tv1Currency.Visible := False;
    Tv1ConTotalAmount.Visible := False;

    case cxTabControl1.TabIndex of
      0:
        begin
          tbedit.Enabled := true;
          Tqx.Enabled := true;
          btnTijiao.Enabled := True;
        end;
      1:
        begin
          btnCXTijiao.Enabled := True;
          tchk.Enabled := True;
        end;
      2:
        begin
          tnochk.Enabled := True;
          btnWC.Enabled := True;
        end;
      3:
        begin
          btnWCCX.Enabled := True;
        end;
      4:
        begin
          TNoqx.Enabled := true;
          TbDel.Enabled := True;
        end;
    end;
  end;
  if Trim(FAuthority) = '查询' then
  begin
    TBPrint.Enabled := True;

  end;
end;

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

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

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

end;

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

procedure TfrmfinishSalesContractList.TbDelClick(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_CP_Contract_Del ');
      sql.Add(' @ConMIDS=' + QuotedStr(MConMIDS));
      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 TfrmfinishSalesContractList.InitGrid();
var
  WSql: string;
begin
  WSql := SGetFilters(Panel1, 1, 2);
  if trim(WSql) <> '' then
  begin
    WSql := ' and ' + trim(WSql);
  end;

  if IsJYTime.Checked then
  begin
    WSql := WSql + ' and ConDate>' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.Date));
    WSql := WSql + ' and ConDate<=' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.Date + 1));
  end
  else
  begin
    if trim(WSql) <> '' then
      WSql := ' and 1=1' + trim(WSql)
    else
      WSql := WSql + ' and 1=2 ';
  end;
  if trim(WSql) = 'and 1=2' then
    Application.MessageBox('勾选制单日期或填入搜索条件', '提示', 0);

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;

      if Trim(FAuthority) = '查询' then
      begin
        sql.Add('select AA.*,B.* from (select A.*');
        sql.Add(' from CP_Contract_Main A ) AA  ');
        sql.Add(' right join CP_Contract_Sub B  on AA.ConMID = B.ConMID  ');
        sql.Add('where 1=1 ' + (WSql));
      end
      else
      begin
        sql.Add('select AA.* from (select A.*');
        sql.Add(' from CP_Contract_Main A ) AA  ');
        sql.Add('where 1=1 ' + (WSql));
      end;


//      sql.Add(' and AA.ConType in (''内销'',''外销'') ');

      if Trim(FAuthority) = '录入' then
      begin
        sql.Add(' 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
            sql.Add(' and  isnull(AA.status,''0'')=''0''');
          end;
        1:
          begin
            sql.Add(' and  isnull(AA.status,''0'')=''1''');
          end;
        2:
          begin
            sql.Add(' and  isnull(AA.status,''0'')=''9''');
          end;
        3:
          begin
            sql.Add(' and  isnull(AA.status,''0'')=''10''');
          end;
        4:
          begin
            sql.Add(' and  isnull(AA.status,''0'')=''-1''');
          end;
      end;
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  finally
    ADOQueryMain.EnableControls;
    TV1.DataController.Filter.Clear;

  end;
end;

procedure TfrmfinishSalesContractList.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 TfrmfinishSalesContractList.TBPrintClick(Sender: TObject);
var
  WSql: string;
begin
  if CDS_1.IsEmpty then
    Exit;

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

end;

procedure TfrmfinishSalesContractList.TBEditClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
//  if Trim(CDS_1.fieldbyname('Filler').AsString) <> Trim(DName) then
//  begin
//    Application.MessageBox('不能操作他人的数据!', '提示', 0);
//    Exit;
//  end;
  try
    frmfinishSalesConInput := TfrmfinishSalesConInput.Create(Application);
    with frmfinishSalesConInput do
    begin
      PState := '修改';
      FConMId := Trim(Self.CDS_1.fieldbyname('ConMID').AsString);
      FAuthority := self.FAuthority;
      FGSTT := self.FGSTT;
      fFlileFlag := self.fFlileFlag;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmfinishSalesConInput.Free;
  end;
end;

procedure TfrmfinishSalesContractList.TBRafreshClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  InitGrid();
  if Trim(FAuthority) = '查询' then
  begin
    InitSub();
  end;

end;

procedure TfrmfinishSalesContractList.TBAddClick(Sender: TObject);
var
  maxno: string;
begin
  try
    frmfinishSalesConInput := TfrmfinishSalesConInput.Create(Application);
    with frmfinishSalesConInput do
    begin
      PState := '新增';
      FConMId := '';
      FAuthority := self.FAuthority;
      FGSTT := self.FGSTT;
      fFlileFlag := self.fFlileFlag;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmfinishSalesConInput.Free;
  end;
end;

procedure TfrmfinishSalesContractList.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();
  if Trim(FAuthority) = '查询' then
  begin
    InitSub();
  end;
end;

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

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

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

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

procedure TfrmfinishSalesContractList.ToolButton1Click(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  try
    frmfinishSalesConInput := TfrmfinishSalesConInput.Create(Application);
    with frmfinishSalesConInput do
    begin
      PState := '查看';
      FConMId := Trim(Self.CDS_1.fieldbyname('ConMID').AsString);
      FGSTT := self.FGSTT;
      ToolBar2.Visible := False;
      TBSave.Visible := False;
//      ScrollBox1.Enabled := False;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmfinishSalesConInput.Free;
  end;
end;

procedure TfrmfinishSalesContractList.ToolButton2Click(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  try
    frmfinishSalesConInput := TfrmfinishSalesConInput.Create(Application);
    with frmfinishSalesConInput do
    begin
      PState := '复制';
      FConMId := Trim(Self.CDS_1.fieldbyname('ConMID').AsString);
      FAuthority := self.FAuthority;
      fFlileFlag := self.fFlileFlag;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmfinishSalesConInput.Free;
  end;
end;

procedure TfrmfinishSalesContractList.ToolButton3Click(Sender: TObject);
begin
  try
    frmhtImport := TFrmhtImport.Create(Application);
    with frmhtImport do
    begin

      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmhtImport.Free;
  end;
end;

procedure TfrmfinishSalesContractList.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;
  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_CP_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 TfrmfinishSalesContractList.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 TfrmfinishSalesContractList.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;
  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_CP_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 TfrmfinishSalesContractList.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;
  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_CP_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 TfrmfinishSalesContractList.cxTabControl1Change(Sender: TObject);
begin
  SetStatus();
  TBRafresh.Click;
end;

procedure TfrmfinishSalesContractList.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  if Trim(FAuthority) <> '查询' then
  begin
    InitSub();
  end;
end;

procedure TfrmfinishSalesContractList.TV2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  with ADOQuery1 do
  begin
    close;
    sql.Clear;
    sql.Add('select * from TP_File A');
    sql.Add('where WBID =' + quotedstr(trim(CDS_2.fieldbyname('AttName').AsString)));
//    ShowMessage(sql.text);
    open;
  end;

  if CDS_2.IsEmpty then
    exit;
  with ADOQueryCMD do
  begin
    close;
    sql.Clear;
    sql.Add('select * from BS_Contract_sub A  ');
    sql.Add('where CP_code =' + quotedstr((CDS_2.fieldbyname('C_Code').AsString)));
    open;
  end;
  SCreateCDS(ADOQueryCMD, CDS_3);
  SInitCDSData(ADOQueryCMD, CDS_3);

end;

procedure TfrmfinishSalesContractList.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_CP_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 TfrmfinishSalesContractList.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_CP_Contract_SetStatus ');
      sql.Add(' @ConMIDS=' + QuotedStr(MConMIDS));
      sql.Add(',@Operation=''撤销取消'' ');
      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)));
    application.MessageBox('撤销取消成功', '提示');
    InitGrid();
  except
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    InitGrid();
  end;

end;

procedure TfrmfinishSalesContractList.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 TfrmfinishSalesContractList.ToolButton7Click(Sender: TObject);
begin
  writeCxGrid(trim(Self.Caption) + 'tv1', Tv1, '销售合同管理');
  writeCxGrid(trim(Self.Caption) + 'tv2', Tv2, '销售合同管理');
end;

procedure TfrmfinishSalesContractList.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;
  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_CP_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 TfrmfinishSalesContractList.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 TfrmfinishSalesContractList.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;
//  if Order_Sub.IsEmpty then
//      raise Exception.Create('明细不能为空！');
  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_CP_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 TfrmfinishSalesContractList.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;
  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_CP_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.

