unit U_JYOrderTestReport;

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, cxButtonEdit, cxDropDownEdit, cxPC;

type
  TfrmJYOrderTestReport = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    ToolButton1: TToolButton;
    Panel1: TPanel;
    Label9: TLabel;
    OrderNo: TEdit;
    v1Column2: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    Label34: TLabel;
    v1Column7: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    CPCode: TEdit;
    v1Column4: TcxGridDBColumn;
    Label3: TLabel;
    Label4: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport2: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    v1Column3: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    Label1: TLabel;
    KHName: TEdit;
    Label2: TLabel;
    FactoryName: TEdit;
    Label5: TLabel;
    GDPerson: TEdit;
    v1Column5: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure CustomerNoNameChange(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ZJStatusChange(Sender: TObject);
    procedure OrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
  private
    canshu1:string;
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    { Private declarations }
  public
    FFInt,FCloth:Integer;

    { Public declarations }
  end;

var
  frmJYOrderTestReport: TfrmJYOrderTestReport;

implementation
uses
  U_DataLink,U_RTFun, U_JYOrderTestReportInPut;

{$R *.dfm}

procedure TfrmJYOrderTestReport.FormDestroy(Sender: TObject);
begin
  frmJYOrderTestReport:=nil;
end;

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

procedure TfrmJYOrderTestReport.FormCreate(Sender: TObject);
begin
  cxgrid1.Align:=alClient;
  canshu1:=Trim(DParameters1);
end;

procedure TfrmJYOrderTestReport.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('检测报告1',Tv1,'指示单管理');
  Close;
end;

procedure TfrmJYOrderTestReport.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' select * ');
      sql.Add(' from Test_Report  ');
      sql.Add(' where 1=1 ');
      if Trim(canshu1)<>'高权限' then
      begin
        sql.Add(' and FillerCode='''+Trim(DCode)+'''');
      end;
      if cxTabControl1.TabIndex<>0 then
      begin
        sql.Add(' and TRDate>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''');
        sql.Add(' and TRDate<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date+1))+'''');
      end;
      if cxTabControl1.TabIndex=0 then
      begin
        sql.Add(' and isnull(SSPerson,'''')='''' ');
      end else
      if cxTabControl1.TabIndex=1 then
      begin
        sql.Add(' and isnull(SSPerson,'''')<>'''' and isnull(Chker,'''')='''' ');
      end  else
      if cxTabControl1.TabIndex=2 then
      begin
        sql.Add(' and isnull(Chker,'''')<>'''' and isnull(CeShiPerson,'''')='''' ');
      end  else
      if cxTabControl1.TabIndex=3 then
      begin
        sql.Add(' and isnull(CeShiPerson,'''')<>'''' ');
      end;

      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;


procedure TfrmJYOrderTestReport.InitForm();
begin
  BegDate.Date:=SGetServerDateMBeg(ADOQueryTemp);
  EndDate.Date:=SGetServerDate(ADOQueryTemp);
  ReadCxGrid('检测报告1',Tv1,'指示单管理');
  InitGrid();
end;

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

procedure TfrmJYOrderTestReport.TBEditClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if cxTabControl1.TabIndex<>0 then Exit;
  try
    frmJYOrderTestReportInPut:=TfrmJYOrderTestReportInPut.Create(Application);
    with frmJYOrderTestReportInPut do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('TRId').AsString);
      FRead:='';
      if ShowModal=1 then
      begin
        
      end;
    end;
  finally
    frmJYOrderTestReportInPut.Free;
  end;
end;

procedure TfrmJYOrderTestReport.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if cxTabControl1.TabIndex<>0 then Exit;
  {if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;}
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if DelData() then
  begin
    //Order_Main.Delete;
  end;
end;

function TfrmJYOrderTestReport.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      //First;
     // while Order_Main.Locate('SSel',True,[]) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Delete Test_Report  where TRId='''+Trim(Order_Main.fieldbyname('TRId').AsString)+'''');
          ExecSQL;
        end;
        Order_Main.Delete;
      end;
    end;
    Order_Main.EnableControls;


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

procedure TfrmJYOrderTestReport.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  TcxGridToExcel('质检检验报告列表',cxGrid1);
end;

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

procedure TfrmJYOrderTestReport.TBAddClick(Sender: TObject);
var
  maxno:string;
begin
  try
    frmJYOrderTestReportInPut:=TfrmJYOrderTestReportInPut.Create(Application);
    with frmJYOrderTestReportInPut do
    begin
      PState:=0;
      FMainId:='';
      FRead:='';
      if ShowModal=1 then
      begin
       
      end;
    end;
  finally
    frmJYOrderTestReportInPut.Free;
  end;
end;

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

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

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

procedure TfrmJYOrderTestReport.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    frmJYOrderTestReportInPut:=TfrmJYOrderTestReportInPut.Create(Application);
    with frmJYOrderTestReportInPut do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('TRId').AsString);
      TBSave.Visible:=False;
      FRead:='';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmJYOrderTestReportInPut.Free;
  end;
end;

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

procedure TfrmJYOrderTestReport.N2Click(Sender: TObject);
begin
  SelOKNo(Order_Main,True);
end;

procedure TfrmJYOrderTestReport.N1Click(Sender: TObject);
begin
  SelOKNo(Order_Main,False);
end;

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

procedure TfrmJYOrderTestReport.ZJStatusChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmJYOrderTestReport.OrderNoKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    if Trim(OrderNo.Text)='' then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add(' select * ');
        sql.Add(' from Test_Report  ');
        sql.Add(' where orderNo like '''+'%'+Trim(OrderNo.Text)+'%'+'''');
        if Trim(canshu1)<>'高权限' then
        begin
          sql.Add(' and FillerCode='''+Trim(DCode)+'''');
        end;
        if cxTabControl1.TabIndex<>0 then
        begin
          sql.Add(' and TRDate>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''');
          sql.Add(' and TRDate<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date+1))+'''');
        end;
        if cxTabControl1.TabIndex=0 then
        begin
          sql.Add(' and isnull(SSPerson,'''')='''' ');
        end else
        if cxTabControl1.TabIndex=1 then
        begin
          sql.Add(' and isnull(SSPerson,'''')<>'''' and isnull(Chker,'''')='''' ');
        end  else
        if cxTabControl1.TabIndex=2 then
        begin
          sql.Add(' and isnull(Chker,'''')<>'''' and isnull(CeShiPerson,'''')='''' ');
        end  else
        if cxTabControl1.TabIndex=3 then
        begin
          sql.Add(' and isnull(CeShiPerson,'''')<>'''' ');
        end;
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmJYOrderTestReport.ToolButton3Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  //if cxTabControl1.TabIndex<>0 then Exit;
  {if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;  }
  if cxTabControl1.TabIndex<>0 then Exit;
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      //First;
      //while Locate('SSel',True,[])=True do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate Test_Report Set SStime=getdate(),SSPerson='''+Trim(DName)+'''');
          sql.Add(' where TRID='''+Trim(Order_Main.fieldbyname('TRID').AsString)+'''');
          ExecSQL;
        end;
        Order_Main.Delete;
      end;
    end;
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
  except
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!','提示',0);
  end;
end;

procedure TfrmJYOrderTestReport.ToolButton2Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  //if cxTabControl1.TabIndex<>1 then Exit;
  {if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;}
  if cxTabControl1.TabIndex<>1 then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Test_Report where TRID='''+Trim(Order_Main.fieldbyname('TRID').AsString)+'''');
    sql.Add(' and isnull(Chker,'''')<>'''' ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('已审核不能撤销!','提示',0);
    Exit;
  end;
  {Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      if Order_Main.FieldByName('SSel').AsBoolean=True then
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from JYOrder_ZhiJian');
          sql.Add(' where ZJID='''+Trim(Order_Main.fieldbyname('ZJID').AsString)+'''');
          Open;
        end;
        if Trim(ADOQueryTemp.fieldbyname('Chker').AsString)<>'' then
        begin
          Order_Main.EnableControls;
          Application.MessageBox('已审批不能撤销!','提示',0);
          Exit;
        end;

      end;
      Next;
    end;
  end;
  Order_Main.EnableControls; }
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      //First;
      //while Locate('SSel',True,[])=True do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate Test_Report Set SStime=Null,SSPerson=Null');
          sql.Add(' where TRID='''+Trim(Order_Main.fieldbyname('TRID').AsString)+'''');
          ExecSQL;
        end;
        Order_Main.Delete;
      end;
    end;
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    
  except
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!','提示',0);
  end;
end;

procedure TfrmJYOrderTestReport.ToolButton4Click(Sender: TObject);
var
  fPrintFile:string;
begin
  if Order_Main.IsEmpty then Exit;
  if Trim(Order_Main.fieldbyname('Chker').AsString)='' then exit;
  fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\检测报告.rmf';
  RMXLSExport2:= TRMXLSExport.Create(RMXLSExport2);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Test_Report where TRId='''+Trim(Order_Main.fieldbyname('TRId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_PRT);
  SInitCDSData20(ADOQueryTemp,CDS_PRT);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['OrderUnit']:=Order_Main.fieldbyname('OrderUnit').Value;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end  else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\检测报告.rmf'),'提示',0);
  end;
end;

procedure TfrmJYOrderTestReport.Tv1DblClick(Sender: TObject);
begin
  ToolButton1.Click;
end;

end.
