unit U_ContractDCListChk;

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, cxDropDownEdit, IdBaseComponent, IdComponent,
  IdTCPConnection, IdTCPClient, IdFTP,IniFiles,ShellAPI, BtnEdit;

type
  TfrmContractDCListChk = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBDataSet1: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    ToolButton1: TToolButton;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    PopupMenu2: TPopupMenu;
    N11: TMenuItem;
    N21: TMenuItem;
    N31: TMenuItem;
    Panel1: TPanel;
    Label1: TLabel;
    Label9: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    ConNo: TEdit;
    ToolButton4: TToolButton;
    cxSplitter1: TcxSplitter;
    CDS_Chk: TClientDataSet;
    DS_Chk: TDataSource;
    ToolButton2: TToolButton;
    cxTabControl1: TcxTabControl;
    Panel2: TPanel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn36: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxGridPopupMenu3: TcxGridPopupMenu;
    DS_Sub: TDataSource;
    CDS_Sub: TClientDataSet;
    ToolButton3: TToolButton;
    CDS_Chker: TClientDataSet;
    TBDelChker: TToolButton;
    cxGrid4: TcxGrid;
    TvSC: TcxGridDBTableView;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    Panel3: TPanel;
    CDS_SC: TClientDataSet;
    DS_SC: TDataSource;
    SaveDialog1: TSaveDialog;
    IdFTP1: TIdFTP;
    ODPat: TOpenDialog;
    cxGridPopupMenu4: TcxGridPopupMenu;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column1: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1ConNo: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    v1OrdPerson1: TcxGridDBColumn;
    v1CustomerNoName: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Label4: TLabel;
    DCNO: TEdit;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    v1PRTPrice: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    v1Column3: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    Panel4: TPanel;
    Label2: TLabel;
    Label14: TLabel;
    Panel10: TPanel;
    Image2: TImage;
    Button1: TButton;
    ChkNote: TRichEdit;
    v1YJDCDate: TcxGridDBColumn;
    v1FKPayMent: TcxGridDBColumn;
    Label3: TLabel;
    Label5: TLabel;
    v1YJSHDate: TcxGridDBColumn;
    v1EditNote: TcxGridDBColumn;
    Label8: TLabel;
    EditNote: TMemo;
    v1EditSQTime: TcxGridDBColumn;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    v1LCChuanQ: TcxGridDBColumn;
    v1YiDuanZ: TcxGridDBColumn;
    v1LCQty: TcxGridDBColumn;
    v1XGCount: TcxGridDBColumn;
    Panel5: TPanel;
    Label6: TLabel;
    Label7: TLabel;
    Panel6: TPanel;
    Image1: TImage;
    TSure: TButton;
    TaiTou: TBtnEditA;
    ETaiTou: TEdit;
    v1fillcode: TcxGridDBColumn;
    v1filler: TcxGridDBColumn;
    IFTT: TCheckBox;
    v1HCDay: TcxGridDBColumn;
    v1JHDate: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure TBTPClick(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure Tv1StylesGetContentStyle(Sender: TcxCustomGridTableView;
      ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem;
      out AStyle: TcxStyle);
    procedure v1DeliveryDateCustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure FPNOChange(Sender: TObject);
    procedure ConNoKeyPress(Sender: TObject; var Key: Char);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure Tv2CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure Panel10MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure Image2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure FPNOKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton3Click(Sender: TObject);
    procedure TBDelChkerClick(Sender: TObject);
    procedure TvSCCellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure TSureClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure Image1Click(Sender: TObject);
    procedure TaiTouBtnClick(Sender: TObject);
  private
    canshu1,canshu2,canshu3:string;
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    procedure ReadINIFile10();
    { Private declarations }
  public
    FFInt,FCloth:Integer;

    { Public declarations }
  end;

var
  frmContractDCListChk: TfrmContractDCListChk;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp, U_ZDYHelpSel, U_ConLCInPutDC;

{$R *.dfm}

procedure TfrmContractDCListChk.FormDestroy(Sender: TObject);
begin
  frmContractDCListChk:=nil;
end;

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

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

procedure TfrmContractDCListChk.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('订单合同审核订舱',Tv1,'生产指示单管理');
  WriteCxGrid('订单合同审核明细订舱',Tv2,'生产指示单管理');
  WriteCxGrid('订单合同审核S明细订舱',Tv3,'生产指示单管理');
end;

procedure TfrmContractDCListChk.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select ');
      sql.add('SYRName=(select top 1 T.SYRName From JYOrderCon_Main T where T.ConNO=A.ConNo),');
      sql.add('JHDate=(select Top 1 M.DlyDate from JYOrder_Main M where M.OrderNo=A.DCNO),');
      sql.add(' PRTMaoZ=isnull(A.PRTMaoZ,(select Sum(D.CHMZ) from JYOrderCon_Main E inner join JYOrderCon_Sub C on C.Mainid=E.Mainid Inner join JYOrder_CR D on D.ConSubid=C.Subid where E.conNo=A.conNo and isnull(D.CRFlag,'''')=''出库'' and E.ZuoFeiFlag=0))');
      sql.add(' ,PRTJIngZ=isnull(A.PRTJIngZ,(select Sum(D.CHJZ) from JYOrderCon_Main E inner join JYOrderCon_Sub C on C.Mainid=E.Mainid Inner join JYOrder_CR D on D.ConSubid=C.Subid where E.conNo=A.conNo and isnull(D.CRFlag,'''')=''出库'' and E.ZuoFeiFlag=0))');
      sql.add(' ,PRTJianQty=isnull(A.PRTJianQty,(select Sum(D.QtyJian) from JYOrderCon_Main E inner join JYOrderCon_Sub C on C.Mainid=E.Mainid Inner join JYOrder_CR D on D.ConSubid=C.Subid where E.conNo=A.conNo and isnull(D.CRFlag,'''')=''出库'' and E.ZuoFeiFlag=0))');

      SQL.ADD(',A.*,  ');
      sql.add('LCChuanQ=(select Top 1 B.LCChuanQ from JYOrderCon_TT B where B.ConNo=A.ConNo and B.TTType=''LC''),');
      sql.add('YiDuanZ=(select Top 1 B.YiDuanZ from JYOrderCon_TT B where B.ConNo=A.ConNo and B.TTType=''LC''),');
      sql.add('LCQty=(select Sum(C.PRTOrderQty) from JYOrderCon_TT_Sub C where C.TTID in (select B.TTID from JYOrderCon_TT B where B.ConNo=A.ConNo and B.TTType=''LC'')),');
      sql.add('FKPayment=isnull((select Top 1 Payment from JYOrderCon_Main JRM where JRM.ConNO=A.ConNo and JRM.ZuoFeiFlag=0),A.PayMent),');
      sql.Add('YWYZhuLi1=(select Top 1 YWYZhuLi from JYOrderCon_Main JRM where JRM.ConNO=A.ConNo and JRM.ZuoFeiFlag=0),');
      sql.Add('YWY1=(select Top 1 YWY from JYOrderCon_Main JRM where JRM.ConNO=A.ConNo and JRM.ZuoFeiFlag=0) ');
      sql.Add(' from JYOrderCon_TT A  ');
      if Trim(canshu1)='高权限'  then
      begin
        if cxTabControl1.TabIndex=0 then
        begin
          sql.Add(' where  isnull(chkStatus,'''')<>''审核通过'' ');
        end else
        begin
          SQL.Add('where  SKDate>='''+FormatDateTime('yyyy-MM-dd',BegDate.DateTime)+'''');
          SQL.Add('and  SKDate<'''+FormatDateTime('yyyy-MM-dd',enddate.DateTime+1)+'''');
          sql.Add(' and isnull(chkStatus,'''')=''审核通过'' ');
        end;
       // SQL.Add(' and A.ZuoFeiFlag=0 ');
      end else
      begin
        if cxTabControl1.TabIndex=0 then
        begin
          sql.Add(' where exists(select * from OrdConDanZH_Chk OC where OC.MainId=A.TTId  ');
          sql.Add(' and (OC.Chker='''+Trim(DName)+'''');
          sql.Add(' or OC.Chker in(select UserName from User_GongXiang UG where UG.UserId='''+Trim(DCode)+'''))');
          SQL.Add(' and isnull(chkStatus,'''')<>''审核通过'' )');
          sql.Add(' and isnull(chkStatus,'''')<>''审核通过'' and isnull(chkStatus,'''')<>''审核不通过''');
        end else
        begin
          if cxTabControl1.TabIndex=1 then
          begin
            SQL.Add('where  SKDate>='''+FormatDateTime('yyyy-MM-dd',BegDate.DateTime)+'''');
            SQL.Add('and  SKDate<'''+FormatDateTime('yyyy-MM-dd',enddate.DateTime+1)+'''');
            sql.Add(' and( ');
            SQL.Add('  exists(select * from OrdConDanZH_Chk OC where OC.MainId=A.TTId ');
            sql.Add(' and (OC.Chker='''+Trim(DName)+'''');
            sql.Add(' or OC.Chker in(select UserName from User_GongXiang UG where UG.UserId='''+Trim(DCode)+'''))');
            sql.Add(' and isnull(chkStatus,'''')=''审核通过''))');
          end
          else
          begin
            SQL.Add('where  SKDate>='''+FormatDateTime('yyyy-MM-dd',BegDate.DateTime)+'''');
            SQL.Add('and  SKDate<'''+FormatDateTime('yyyy-MM-dd',enddate.DateTime+1)+'''');
            sql.Add(' and isnull(chkStatus,'''')=''审核通过'' ');
          end;
        end;
        sql.add(' and A.SChkFlag=''1''');
      end;
      SQL.Add(' and A.TTType='''+Trim(canshu2)+''' ');
      sql.add(' Order by A.EditFlag desc');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
    if Order_Main.IsEmpty=False then
    Label5.Caption:=trim(Order_Main.fieldbyname('FKPayMent').AsString);
  finally
    ADOQueryMain.EnableControls;
  end;
end;


procedure TfrmContractDCListChk.InitForm();
begin
  ReadCxGrid('订单合同审核订舱',Tv1,'生产指示单管理');
  ReadCxGrid('订单合同审核明细订舱',Tv2,'生产指示单管理');
  ReadCxGrid('订单合同审核S明细订舱',Tv3,'生产指示单管理');
  BegDate.DateTime:=SGetServerDate10(ADOQueryTemp)-7;
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  Label5.Caption:='';
  InitGrid();
  if Trim(canshu1)='高权限' then
  begin
    v1fillcode.Visible:=True;
  end else
  begin
    v1fillcode.Visible:=False;
  end;
end;

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

function TfrmContractDCListChk.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrderCon_Sub where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrderCon_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      ExecSQL;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

procedure TfrmContractDCListChk.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  SelExportData(Tv1,ADOQueryMain,'订舱列表');
end;

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

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

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

procedure TfrmContractDCListChk.TBTPClick(Sender: TObject);
 var
   FQty,FQty1,FMxQty,FPQty,FMxQtyS,FPQtyS:String;
begin
end;

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

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

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

    end;
  except
  end; }
end;

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

procedure TfrmContractDCListChk.N1Click(Sender: TObject);
var
   fPrintFile:string;
   Porderno:string;
begin
  if Order_Main.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\生产指示单10.rmf' ;
  SDofilter(ADOQueryMain,' OrderNoM='''+Trim(Order_Main.fieldbyname('OrderNoM').AsString)+'''');
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  Porderno:=Trim(Order_Main.fieldbyname('OrderNoM').AsString);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['begindate']:=begindate.DateTime;
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    //RMVariables['printer']:=Trim(gUserName);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\生产指示单10.rmf'),'提示',0);
  end;
  SDofilter(ADOQueryMain,'');
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  Order_Main.Locate('ordernoM',Porderno,[]);
end;

procedure TfrmContractDCListChk.N2Click(Sender: TObject);
var
   fPrintFile:string;
   Porderno:string;
begin
  if Order_Main.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\生产指示单.rmf' ;
  SDofilter(ADOQueryMain,' OrderNoM='''+Trim(Order_Main.fieldbyname('OrderNoM').AsString)+'''');
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  Porderno:=Trim(Order_Main.fieldbyname('OrderNoM').AsString);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['begindate']:=begindate.DateTime;
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    //RMVariables['printer']:=Trim(gUserName);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\生产指示单.rmf'),'提示',0);
  end;
  SDofilter(ADOQueryMain,'');
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  Order_Main.Locate('ordernoM',Porderno,[]);
end;

procedure TfrmContractDCListChk.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    frmConLCInPutDC:=TfrmConLCInPutDC.Create(Application);
    with frmConLCInPutDC do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('TTId').AsString);
      TBSave.Visible:=False;
      ScrollBox1.Enabled:=False;
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmConLCInPutDC.Free;
  end;
end;

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

procedure TfrmContractDCListChk.ConNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    if Length(conno.Text)<3 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add('select ');
        sql.add('SYRName=(select Top 1 T.SYRName From JYOrderCon_Main T where T.ConNO=A.ConNo),');
        sql.add('JHDate=(select Top 1 M.DlyDate from JYOrder_Main M where M.OrderNo=A.DCNO),');
        sql.add(' PRTMaoZ=isnull(A.PRTMaoZ,(select Sum(D.CHMZ) from JYOrderCon_Main E inner join JYOrderCon_Sub C on C.Mainid=E.Mainid Inner join JYOrder_CR D on D.ConSubid=C.Subid where E.conNo=A.conNo and isnull(D.CRFlag,'''')=''出库'' and E.ZuoFeiFlag=0))');
        sql.add(' ,PRTJIngZ=isnull(A.PRTJIngZ,(select Sum(D.CHJZ) from JYOrderCon_Main E inner join JYOrderCon_Sub C on C.Mainid=E.Mainid Inner join JYOrder_CR D on D.ConSubid=C.Subid where E.conNo=A.conNo and isnull(D.CRFlag,'''')=''出库'' and E.ZuoFeiFlag=0))');
        sql.add(' ,PRTJianQty=isnull(A.PRTJianQty,(select Sum(D.QtyJian) from JYOrderCon_Main E inner join JYOrderCon_Sub C on C.Mainid=E.Mainid Inner join JYOrder_CR D on D.ConSubid=C.Subid where E.conNo=A.conNo and isnull(D.CRFlag,'''')=''出库'' and E.ZuoFeiFlag=0))');
        sql.add(',A.*,  ');
        sql.add('LCChuanQ=(select Top 1 B.LCChuanQ from JYOrderCon_TT B where B.ConNo=A.ConNo and B.TTType=''LC''),');
        sql.add('YiDuanZ=(select Top 1 B.YiDuanZ from JYOrderCon_TT B where B.ConNo=A.ConNo and B.TTType=''LC''),');
        sql.add('LCQty=(select Sum(C.PRTOrderQty) from JYOrderCon_TT_Sub C where C.TTID in (select B.TTID from JYOrderCon_TT B where B.ConNo=A.ConNo and B.TTType=''LC'')),');
        sql.add('FKPayment=isnull((select Top 1 Payment from JYOrderCon_Main JRM where JRM.ConNO=A.ConNo and JRM.ZuoFeiFlag=0),A.PayMent),');
        sql.Add('YWYZhuLi1=(select Top 1 YWYZhuLi from JYOrderCon_Main JRM where JRM.ConNO=A.ConNo and JRM.ZuoFeiFlag=0),');
        sql.Add('YWY1=(select Top 1 YWY from JYOrderCon_Main JRM where JRM.ConNO=A.ConNo and JRM.ZuoFeiFlag=0) ');
        sql.Add(' from JYOrderCon_TT A  ');

        if Trim(canshu1)='高权限'  then
        begin
          if cxTabControl1.TabIndex=0 then
          begin
            sql.Add(' where  isnull(chkStatus,'''')<>''审核通过'' ');
          end else
          begin
            SQL.Add('where  A.ConNo like '''+'%'+Trim(ConNo.Text)+'%'+'''');
            sql.Add(' and isnull(chkStatus,'''')=''审核通过'' ');
          end;
         // SQL.Add(' and A.ZuoFeiFlag=0 ');
        end else
        begin
          if cxTabControl1.TabIndex=0 then
        begin
          sql.Add(' where exists(select * from OrdConDanZH_Chk OC where OC.MainId=A.TTId  ');
          sql.Add(' and (OC.Chker='''+Trim(DName)+'''');
          sql.Add(' or OC.Chker in(select UserName from User_GongXiang UG where UG.UserId='''+Trim(DCode)+'''))');
          SQL.Add(' and isnull(chkStatus,'''')<>''审核通过'' )');
          sql.Add(' and isnull(chkStatus,'''')<>''审核通过'' and isnull(chkStatus,'''')<>''审核不通过''');
        end else
        begin
          if cxTabControl1.TabIndex=1 then
          begin
            SQL.Add('where ( ');
            SQL.Add('  exists(select * from OrdConDanZH_Chk OC where OC.MainId=A.TTId ');
            sql.Add(' and (OC.Chker='''+Trim(DName)+'''');
            sql.Add(' or OC.Chker in(select UserName from User_GongXiang UG where UG.UserId='''+Trim(DCode)+'''))');
            sql.Add(' and isnull(chkStatus,'''')=''审核通过''))');
          end
          else
          begin
            SQL.Add('where isnull(chkStatus,'''')=''审核通过'' ');
          end;
        end;
        sql.add(' and A.SChkFlag=''1''');
        sql.add(' and A.ConNo like '''+'%'+Trim(ConNo.Text)+'%'+'''');

        end;
        SQL.Add(' and A.TTType='''+Trim(canshu2)+'''');
        //ShowMessage(sql.Text);
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmContractDCListChk.Tv1CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
var
  maxno:string;
begin
  if Order_Main.IsEmpty then exit;
  Label5.Caption:=trim(Order_Main.fieldbyname('FKPayMent').AsString);
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from JYOrderCon_TT_Sub ');
    SQL.Add('  where TTId='''+Trim(Order_Main.fieldbyname('TTId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_Sub);
  SInitCDSData20(ADOQueryTemp,CDS_Sub);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from OrdConDanZH_Chk where MainId='''+Trim(Order_Main.fieldbyname('TTId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_Chk);
  SInitCDSData20(ADOQueryTemp,CDS_Chk);
 { with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from XD_File where CYId='''+Trim(Order_Main.fieldbyname('TTId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_SC);
  SInitCDSData20(ADOQueryTemp,CDS_SC);   }
  EditNote.Text:=trim(Order_Main.fieldbyname('EditNote').AsString);
  label13.Caption:=trim(Order_Main.fieldbyname('LCChuanQ').AsString);
  label11.Caption:=trim(Order_Main.fieldbyname('YiDuanZ').AsString);
  label16.Caption:=trim(floattostr(Order_Main.fieldbyname('LCQty').AsFloat));
end;

procedure TfrmContractDCListChk.Tv2CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  {if Order_Main.FieldByName('ConFlag1').AsBoolean=False then
  begin
    if CDS_Chk.IsEmpty then Exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete OrdCon_Chk where CKID='''+Trim(CDS_Chk.fieldbyname('CKID').AsString)+'''');
      ExecSQL;
    end;
    CDS_Chk.Delete;
  end;}
end;

procedure TfrmContractDCListChk.ToolButton4Click(Sender: TObject);
var
  maxno:string;
begin
  if Order_Main.IsEmpty then Exit;
  if cxTabControl1.TabIndex<>0 then Exit;
  if trim(Order_Main.fieldbyname('ChkStatus').AsString)='单据修改' then
  begin
    application.MessageBox('单据修改不能审核通过','提示');
    exit;
  end;
  if Order_Main.FieldByName('SChkFlag').AsBoolean=False then
  begin
    application.MessageBox('单据没送审,不能审核','提示',0);
    exit;
  end;
  if Trim(canshu1)='高权限' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select ChkStatus from OrdConDanZH_Chk where MainId='''+Trim(Order_Main.fieldbyname('TTId').AsString)+'''');
      sql.Add(' and Isnull(ChkType,'''')=''''  ');  //and isnull(ChkStatus,'''')<>''送审''
      sql.Add(' and isnull(Chker,'''')<>'''+Trim(DName)+'''');
      sql.Add('Group by ChkStatus');
      Open;
    end;
    if ADOQueryTemp.RecordCount>1 then
    begin
      Application.MessageBox('低权限审核未通过!','提示',0);
      Exit;
    end else
    if ADOQueryTemp.RecordCount=1 then
    begin
      if Trim(ADOQueryTemp.FieldByName('ChkStatus').AsString)='审核通过' then
      begin
        {with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYOrderCon_Main Set ChkStatus=''审核通过'',ChkTime=getdate()  where MainId='''+Trim(Order_Main.fieldbyname('Mainid').AsString)+'''');
          ExecSQL;
        end;}
      end else
      begin
        Exit;
      end;
    end else
    begin
      Exit;
    end;
  end;


  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  if Trim(canshu1)='高权限' then
  begin
    if CDS_Chk.Locate('Chker',Trim(DName),[])=True then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update JYOrderCon_TT Set ChkStatus=''审核通过'',ChkTime=getdate(),ChkType=''高权限审核''  where TTId='''+Trim(Order_Main.fieldbyname('TTId').AsString)+'''');
        sql.Add('Update OrdConDanZH_Chk Set ChkStatus=''审核通过'',ChkTime=getdate(),chknote='''',ChkType=''高权限审核'' where ');
        sql.Add('MainId='''+Trim(Order_Main.fieldbyname('TTId').AsString)+'''');
        sql.Add(' and Chker='''+Trim(DName)+'''');
        ExecSQL;
      end;
    end else
    begin
      ADOQueryCmd.Connection.BeginTrans;
      if GetLSNo(ADOQueryCmd,maxno,'GK','OrdConDanZH_Chk',4,1)=False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!','提示',0);
        Exit;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from OrdConDanZH_Chk where 1<>1');
        Open;
      end;
      with ADOQueryCmd do
      begin
        Append;
        FieldByName('CKID').Value:=Trim(maxno);
        FieldByName('MainID').Value:=Trim(Order_Main.fieldbyname('TTID').AsString);
        FieldByName('Chker').Value:=Trim(DName);
        FieldByName('ChkStatus').Value:='审核通过';
        FieldByName('ChkType').Value:='高权限审核';
        FieldByName('ChkTime').Value:=SGetServerDateTime(ADOQueryTemp);
        Post;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update JYOrderCon_TT Set ChkStatus=''审核通过'',ChkTime=getdate(),ChkType=''高权限审核'' ');
        sql.Add(' where TTId='''+Trim(Order_Main.fieldbyname('TTId').AsString)+'''');
        ExecSQL;
      end;
      ADOQueryCmd.Connection.CommitTrans;
      Order_Main.Delete;
    end;
  end else
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      //sql.Add('Update JYOrderCon_Main Set ChkStatus=''审核通过'',ChkTime=getdate()  where MainId='''+Trim(Order_Main.fieldbyname('Mainid').AsString)+'''');
      sql.Add('Update OrdConDanZH_Chk Set ChkStatus=''审核通过'',ChkTime=getdate(),chknote='''' where ');
      sql.Add('MainId='''+Trim(Order_Main.fieldbyname('TTId').AsString)+'''');
      sql.Add(' and Chker='''+Trim(DName)+'''');
      ExecSQL;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select ChkStatus from OrdConDanZH_Chk where MainId='''+Trim(Order_Main.fieldbyname('TTId').AsString)+'''');
      sql.Add('Group by ChkStatus');
      Open;
    end;
    if ADOQueryTemp.RecordCount>1 then
    begin
      Order_Main.Delete;
      Exit;
    end else
    if ADOQueryTemp.RecordCount=1 then
    begin
      if Trim(ADOQueryTemp.FieldByName('ChkStatus').AsString)='审核通过' then
      begin
        {with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYOrderCon_TT Set ChkStatus=''审核通过'',ChkTime=getdate()  where TTId='''+Trim(Order_Main.fieldbyname('TTId').AsString)+'''');
          ExecSQL;
        end;}
      end;
      Order_Main.Delete;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from OrdConDanZH_Chk where 1<>1');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_Chk);
  SInitCDSData20(ADOQueryTemp,CDS_Chk);
end;

procedure TfrmContractDCListChk.ToolButton2Click(Sender: TObject);
var
  maxno:string;
begin
  if Order_Main.IsEmpty then Exit;
  if Trim(canshu1)='高权限' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select ChkStatus from OrdConDanZH_Chk where MainId='''+Trim(Order_Main.fieldbyname('TTId').AsString)+'''');
      sql.Add(' and Isnull(ChkType,'''')='''' ');
      sql.Add(' and isnull(Chker,'''')<>'''+Trim(DName)+''' and ChkStatus<>''单据修改'' ');
      sql.Add('Group by ChkStatus');
      Open;
    end;
    if ADOQueryTemp.RecordCount>1 then
    begin
      Application.MessageBox('低权限审核未通过!','提示',0);
      Exit;
    end else
    if ADOQueryTemp.RecordCount=1 then
    begin
      if Trim(ADOQueryTemp.FieldByName('ChkStatus').AsString)='审核通过' then
      begin
        {with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYOrderCon_Main Set ChkStatus=''审核通过'',ChkTime=getdate()  where MainId='''+Trim(Order_Main.fieldbyname('Mainid').AsString)+'''');
          ExecSQL;
        end;}
      end else
      begin
        Exit;
      end;
    end else
    begin
      if Trim(Order_Main.FieldByName('ChkStatus').AsString)<>'单据修改' then
      begin
        Exit;
      end;
    end;
  end;
  if Trim(canshu1)<>'高权限' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrderCon_TT where TTId='''+Trim(Order_Main.fieldbyname('TTId').AsString)+'''');
      Open;
    end;
    if Trim(ADOQueryTemp.fieldbyname('ChkType').AsString)='高权限审核' then
    begin
      if  Trim(ADOQueryTemp.fieldbyname('ChkStatus').AsString)='审核通过' then
      begin
        Application.MessageBox('高权限已审核!','提示',0);
        Exit;
      end;
    end;
  end;

  Panel4.Visible:=True;
  ChkNote.Text:=trim(EditNote.Text);
end;

procedure TfrmContractDCListChk.Panel10MouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
begin
  ReleaseCapture;
  TWinControl(Panel4).Perform(WM_SYSCOMMAND,$F012,0);
end;

procedure TfrmContractDCListChk.Image2Click(Sender: TObject);
begin
  Panel4.Visible:=false;
end;

procedure TfrmContractDCListChk.Button1Click(Sender: TObject);
var
  maxno:string;
begin
  if Trim(ChkNote.Text)='' then
  begin
    Application.MessageBox('审核备注不能为空!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  if Trim(canshu1)='高权限' then
  begin
    if CDS_Chk.Locate('Chker',Trim(DName),[])=True then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update JYOrderCon_TT Set ChkStatus=''审核不通过'',EditFlag='''',ChkTime=getdate(),chknote='''',ChkType=''高权限审核'',SChkFlag=0  where TTId='''+Trim(Order_Main.fieldbyname('TTid').AsString)+'''');
        sql.Add('Update OrdConDanZH_Chk Set ChkStatus=''审核不通过'',ChkTime=getdate(),ChkType=''高权限审核'',ChkNote='''+Trim(ChkNote.Text)+'''');
        sql.Add(' where MainId='''+Trim(Order_Main.fieldbyname('TTId').AsString)+'''');
        sql.Add(' and Chker='''+Trim(DName)+'''');
        ExecSQL;
      end;
      CDS_Chk.Locate('Chker',Trim(DName),[]);
      with CDS_Chk do
      begin
        Edit;
        //FieldByName('Chker').Value:=Trim(DName);
        FieldByName('ChkStatus').Value:='审核不通过';
        FieldByName('ChkTime').Value:=SGetServerDateTime(ADOQueryTemp);
        FieldByName('ChkNOte').Value:=Trim(ChkNote.Text);
        Post;
      end;
    end else
    begin
      ADOQueryCmd.Connection.BeginTrans;
      if GetLSNo(ADOQueryCmd,maxno,'GK','OrdConDanZH_Chk',4,1)=False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!','提示',0);
        Exit;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from OrdConDanZH_Chk where 1<>1');
        Open;
      end;
      with ADOQueryCmd do
      begin
        Append;
        FieldByName('CKID').Value:=Trim(maxno);
        FieldByName('MainID').Value:=Trim(Order_Main.fieldbyname('TTID').AsString);
        FieldByName('Chker').Value:=Trim(DName);
        FieldByName('ChkTime').Value:=SGetServerDateTime(ADOQueryTemp);
        FieldByName('ChkStatus').Value:='审核不通过';
        FieldByName('ChkNote').Value:=Trim(ChkNote.Text);
        Post;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update JYOrderCon_TT Set ChkStatus=''审核不通过'',EditFlag='''',ChkTime=getdate(),chknote='''',SChkFlag=0  where TTId='''+Trim(Order_Main.fieldbyname('TTId').AsString)+'''');
        ExecSQL;
      end;
      ADOQueryCmd.Connection.CommitTrans;
      Order_Main.Delete;
      with CDS_Chk do
      begin
        Append;
        FieldByName('Chker').Value:=Trim(DName);
        FieldByName('ChkStatus').Value:='审核不通过';
        FieldByName('ChkTime').Value:=SGetServerDateTime(ADOQueryTemp);
        FieldByName('ChkNOte').Value:=Trim(ChkNote.Text);
        Post;
      end;
    end;
  end else
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update JYOrderCon_TT Set ChkStatus=''审核不通过'',EditFlag='''',ChkTime=getdate(),SChkFlag=0  where TTId='''+Trim(Order_Main.fieldbyname('TTid').AsString)+'''');
      sql.Add('Update OrdConDanZH_Chk Set ChkStatus=''审核不通过'',ChkTime=getdate()  ,ChkNote='''+Trim(ChkNote.Text)+'''');
      sql.Add(' where MainId='''+Trim(Order_Main.fieldbyname('TTId').AsString)+'''');
      sql.Add(' and Chker='''+Trim(DName)+'''');
      ExecSQL;
    end;
    with CDS_Chk do
    begin
      Edit;
      //FieldByName('Chker').Value:=Trim(DName);
      FieldByName('ChkStatus').Value:='审核不通过';
      FieldByName('ChkTime').Value:=SGetServerDateTime(ADOQueryTemp);
      FieldByName('ChkNOte').Value:=Trim(ChkNote.Text);
      Post;
    end;
    Order_Main.Delete;
    
  end;
  if cxTabControl1.TabIndex=1 then
  begin
    Order_Main.Delete;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from OrdConDanZH_Chk where 1<>1');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,CDS_Chk);
    SInitCDSData20(ADOQueryTemp,CDS_Chk);
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrderCon_TT where 1<>1');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,CDS_Sub);
    SInitCDSData20(ADOQueryTemp,CDS_Sub);
  end;
  Panel4.Visible:=False;
end;

procedure TfrmContractDCListChk.cxTabControl1Change(Sender: TObject);
begin
  InitGrid();
  if Order_Main.IsEmpty then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from OrdCon_Chk where 1<>1');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,CDS_Chk);
    SInitCDSData20(ADOQueryTemp,CDS_Chk);
  end;
end;

procedure TfrmContractDCListChk.FPNOKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    {if Length(FPNO.Text)<3 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        SQL.Clear;
        sql.Add('select A.* ');
        sql.Add(' from JYOrderCon_TT A  where 1=1 ');
        sql.Add(' and FPNO like '''+'%'+Trim(FPNO.Text)+'%'+'''');
        sql.Add(' and TTType='''+Trim(DParameters2)+'''');
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end; }
  end;
end;

procedure TfrmContractDCListChk.ToolButton3Click(Sender: TObject);
var
  maxno:string;
  FCount:Integer;
begin
  if Order_Main.IsEmpty then exit;
  if Trim(canshu1)='高权限' then
  begin
    ADOQueryCmd.Connection.BeginTrans;
    if GetLSNo(ADOQueryCmd,maxno,'GS','OrdConDanZH_Chk',4,1)=False then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取最大号失败!','提示',0);
      Exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from OrdConDanZH_Chk where 1=2');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('CKID').Value:=Trim(maxno);
      FieldByName('MainID').Value:=Trim(Order_Main.fieldbyname('TTID').AsString);
      FieldByName('Chker').Value:=Trim(DName);
      FieldByName('ChkTime').Value:=SGetServerDateTime(ADOQueryTemp);
      FieldByName('ChkStatus').Value:='送审';
      FieldByName('ChkType').Value:='高权限审核';
      //FieldByName('ChkNote').Value:=Trim(ChkNote.Text);
      Post;
    end;
    {with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update JYOrderCon_TT Set ChkStatus=''审核不通过'',ChkTime=getdate(),chknote=''''  where TTId='''+Trim(Order_Main.fieldbyname('TTId').AsString)+'''');
      ExecSQL;
    end; }
    ADOQueryCmd.Connection.CommitTrans;
    //Order_Main.Delete;

    with CDS_Chk do
    begin
      Append;
      FieldByName('Chker').Value:=Trim(DName);
      FieldByName('ChkStatus').Value:='送审';
      FieldByName('ChkTime').Value:=SGetServerDateTime(ADOQueryTemp);
      FieldByName('ChkType').Value:='高权限审核';
      Post;
    end;
  end else
  begin
    if Trim(Order_Main.fieldbyname('ChkStatus').AsString)='审核通过' then
    begin
      Application.MessageBox('已审核通过!','提示',0);
      Exit;
    end;
    {if CDS_Chk.Locate('Chker',Trim(DName),[])=False then
    begin
      Exit;
    end;}
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select Chker=Cast('''' as varchar(20))');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,CDS_Chker);
    SInitCDSData20(ADOQueryTemp,CDS_Chker);
    while CDS_Chker.IsEmpty=False do
    begin
      CDS_Chker.Delete;
    end;
    try
      frmZDYHelpSel:=TfrmZDYHelpSel.Create(Application);
      with frmZDYHelpSel do
      begin
        flag:='OrdConChkerLC';
        flagname:='LC审核人';
        MainType:=Trim(DName);
        if ShowModal=1 then
        begin
          with ClientDataSet1 do
          begin
            First;
            while not Eof do
            begin
              if FieldByName('SSEl').AsBoolean=True then
              begin
                CDS_Chker.Append;
                CDS_Chker.FieldByName('Chker').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
                CDS_Chker.Post;
              end;
              Next;
            end;
          end;
          if CDS_Chker.IsEmpty then FCount:=9
        end;
      end;
    finally
      frmZDYHelpSel.Free;
    end;
    if FCount=9 then
    begin
      if CDS_Chker.IsEmpty then
      begin
        Application.MessageBox('没有选择审核人!','提示',0);
        Exit;
      end;
    end;
    if CDS_Chker.IsEmpty then Exit;
    CDS_Chker.DisableControls;
    with CDS_Chker do
    begin
      First;
      while not eof do
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from SY_User where UserName='''+Trim(CDS_Chker.fieldbyname('Chker').AsString)+'''');
          Open;
        end;
        if ADOQueryTemp.IsEmpty then
        begin
          CDS_Chker.EnableControls;
          Application.MessageBox('审核人定义错误!','提示',0);
          Exit;
        end;
        Next;
      end;
    end;
    CDS_Chker.EnableControls;

    if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
    //if CDS_Chk.IsEmpty then
    try
      ADOQueryCmd.Connection.BeginTrans;
      {with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update JYOrderCon_TT Set SChkFlag=1,SChkTime=getdate() where TTId='''+Trim(Order_Main.fieldbyname('TTId').AsString)+'''');
        ExecSQL;
      end;
      with Order_Main do
      begin
        Edit;
        FieldByName('SChkFlag').Value:=1;
        Post;
      end; }
      if CDS_Chk.Locate('Chker',Trim(DName),[])=True then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update OrdConDanZH_Chk Set Chkstatus=''送审'',ChkTime=getdate() where CKID='''+Trim(CDS_Chk.fieldbyname('CKID').AsString)+'''');
          ExecSQL;
        end;
        with CDS_Chk do
        begin
          Edit;
          FieldByName('Chkstatus').Value:='送审';
          FieldByName('ChkTime').Value:=SGetServerDateTime(ADOQueryTemp);
          Post;
        end;
      end;
      with CDS_Chker do
      begin
        First;
        while not Eof do
        begin
          if CDS_Chk.Locate('Chker',Trim(CDS_Chker.fieldbyname('Chker').AsString),[])=False then
          begin
            if GetLSNo(ADOQueryCmd,maxno,'CK','OrdConDanZH_Chk',4,1)=False then
            begin
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('取最大号失败!','提示',0);
              Exit;
            end;
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              sql.Add('select * from OrdConDanZH_Chk where 1<>1');
              Open;
            end;
            with ADOQueryCmd do
            begin
              Append;
              FieldByName('CKID').Value:=Trim(maxno);
              FieldByName('MainID').Value:=Trim(Order_Main.fieldbyname('TTID').AsString);
              FieldByName('Chker').Value:=Trim(CDS_Chker.fieldbyname('Chker').AsString);
              Post;
            end;
            with CDS_Chk do
            begin
              Append;
              FieldByName('Chker').Value:=Trim(CDS_Chker.fieldbyname('Chker').AsString);
              Post;
            end;
          end;

          Next;
        end;
      end;
      ADOQueryCmd.Connection.CommitTrans;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('调用审核人失败!','提示',0);
    end;

  end;
end;

procedure TfrmContractDCListChk.TBDelChkerClick(Sender: TObject);
begin
  {if Trim(CDS_Chk.fieldbyname('ChkStatus').AsString)<>'' then
  begin
    Application.MessageBox('已审核不能删除审核人!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要删除此审核人吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('delete OrdConDanZH_Chk where CKID='''+Trim(CDS_Chk.fieldbyname('CKID').AsString)+'''');
    ExecSQL;
  end;
  CDS_Chk.Delete;}
end;
procedure TfrmContractDCListChk.ReadINIFile10();
var
  programIni:Tinifile;  //配置文件名
  FileName:string;
begin
  FileName:=ExtractFilePath(Paramstr(0))+'SYSTEMSET.INI';
  programIni:=Tinifile.create(FileName);
  server:=programIni.ReadString('SERVER','服务器地址','127.0.0.1');
  programIni.Free;
end;

procedure TfrmContractDCListChk.TvSCCellDblClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
var
  fHandle:THandle;
  FInt:Integer;
  FFName,FFName10,FPath:String;
begin
  if CDS_SC.IsEmpty then Exit;
  Panel3.Visible:=True;
  Panel3.Refresh;
  try
    ReadINIFile10();
    server:=ReadINIFileStr('SYSTEMSET.INI','SERVER','服务器地址','127.0.0.1');
    if Length(server)<6 then
    begin
      server:='127.0.0.1';
    end;
    IdFTP1.Host :=server;//PicSvr;
    IdFTP1.Username := 'three';
    IdFTP1.Password := '641010';
    IdFTP1.Connect();
  except
    IdFTP1.Quit;
    Application.MessageBox('无法连接到文件服务器，请检查！', '提示', MB_ICONWARNING);
    Exit;
  end;
  FPath:='D:\Right1209\';
  if not DirectoryExists(ExtractFileDir(FPath)) then
    CreateDir(ExtractFileDir(FPath));
  FFName:=Trim(CDS_SC.fieldbyname('XFID').AsString)+Trim(CDS_SC.fieldbyname('HZStr').AsString);
  FPath:=FPath+FFName;
  if FileExists(FPath)=false then
  begin
    IdFTP1.Get('LC\'+FFName,FPath);
  end;
  Panel3.Visible:=False;
  if IdFTP1.Connected then IdFTP1.Quit;
  ShellExecute(Handle, 'open',PChar(FPath),'', '', SW_SHOWNORMAL);

end;

procedure TfrmContractDCListChk.TSureClick(Sender: TObject);
var
   fPrintFile,FTaiTou:string;
   EngMoney:string;
begin
  if Order_Main.IsEmpty then Exit;
  if Trim(Order_Main.FieldByName('ChkStatus').AsString)<>'审核通过' then
  begin
    RM1.PreviewButtons:=[pbExit];
  end else
  begin
    RM1.PreviewButtons:=[pbZoom,pbLoad,pbSave,pbPrint,pbFind,pbPageSetup,pbExit,pbExport,pbNavigator];
  end;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\订舱指示书.rmf';
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    SQL.Add('select B.PRTPrice, ');
    sql.Add('YWYZhuLi=(select Top 1 YWYZhuLi from JYOrderCon_Main JRM where JRM.ConNO=A.ConNo and JRM.ZuoFeiFlag=0),');
    sql.Add('YWY=(select Top 1 YWY from JYOrderCon_Main JRM where JRM.ConNO=A.ConNo and JRM.ZuoFeiFlag=0), ');
    sql.add(' PRTMaoZ=isnull(A.PRTMaoZ,(select Sum(D.CHMZ) from JYOrderCon_Main E inner join JYOrderCon_Sub C on C.Mainid=E.Mainid Inner join JYOrder_CR D on D.ConSubid=C.Subid where E.conNo=A.conNo and isnull(D.CRFlag,'''')=''出库'' and E.ZuoFeiFlag=0))');
    sql.add(' ,PRTJIngZ=isnull(A.PRTJIngZ,(select Sum(D.CHJZ) from JYOrderCon_Main E inner join JYOrderCon_Sub C on C.Mainid=E.Mainid Inner join JYOrder_CR D on D.ConSubid=C.Subid where E.conNo=A.conNo and isnull(D.CRFlag,'''')=''出库'' and E.ZuoFeiFlag=0))');
    sql.add(' ,SumPRTQty=(select Sum(D.Qty) from JYOrderCon_Main E inner join JYOrderCon_Sub C on C.Mainid=E.Mainid Inner join JYOrder_CR D on D.ConSubid=C.Subid where E.conNo=A.conNo and isnull(D.CRFlag,'''')=''出库'' and E.ZuoFeiFlag=0)');
    sql.Add(' ,A.*,B.*');
    sql.Add(' from JYOrderCon_TT A');
    sql.Add('left join JYOrderCon_TT_Sub B on A.TTID=B.TTID');
    sql.Add(' where A.TTId='''+Trim(Order_Main.fieldbyname('TTId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryPrint,CDS_Print);
  SInitCDSData20(ADOQueryPrint,CDS_Print);
  if FileExists(fPrintFile) then
  begin
    if iftt.Checked=true then
    FTaiTou:='SHAOXING MNC IMP & EXP CO.,LTD'
    else
    FTaiTou:='';
    RMVariables['Taitou']:=trim(TaiTou.Text);
    RMVariables['FTaiTou']:=trim(FTaiTou);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\订舱指示书.rmf'),'提示',0);
  end;
end;

procedure TfrmContractDCListChk.TBPrintClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  TaiTou.Text:=trim(Order_Main.fieldbyname('SYRName').AsString);
  Panel5.Visible:=true;
end;

procedure TfrmContractDCListChk.Image1Click(Sender: TObject);
begin
  panel5.Visible:=False;
end;

procedure TfrmContractDCListChk.TaiTouBtnClick(Sender: TObject);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(Application);
  with frmZDYHelp do
  begin
    flag:='Taitou1';
    flagname:='公司抬头';
    if ShowModal=1 then
    begin
      Taitou.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
    end;
    free;
  end;
end;

end.
