unit U_JYOrderBGChk;

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, cxCalendar, cxButtonEdit, cxSplitter,
  RM_Common, RM_Class, RM_e_Xls, RM_Dataset, RM_System, RM_GridReport,
  cxTextEdit, cxPC;

type
  TfrmJYOrderBGChk = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    FPNo: TEdit;
    Label5: TLabel;
    JYConNo: TEdit;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    Label6: TLabel;
    JYStyleNo: TEdit;
    ToolButton1: TToolButton;
    ToolButton4: TToolButton;
    cxTabControl1: TcxTabControl;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Label4: TLabel;
    ConNo: TEdit;
    Label2: TLabel;
    BGNO: TEdit;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column1: TcxGridDBColumn;
    v1OrderNo: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1PRTSpec: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1PRTQty: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FPNoChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure conPress(Sender: TObject; var Key: Char);
    procedure cxPageControl1Change(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure JYConNoKeyPress(Sender: TObject; var Key: Char);
    procedure JYStyleNoKeyPress(Sender: TObject; var Key: Char);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ConNoKeyPress(Sender: TObject; var Key: Char);
    procedure BGNOKeyPress(Sender: TObject; var Key: Char);
  private
    FInt,PFInt:Integer;
    canshu1:string;
    procedure InitGrid();
    procedure InitGridWSql(fsj:string);
    procedure InitForm();
    function  DelData():Boolean;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmJYOrderBGChk: TfrmJYOrderBGChk;

implementation
uses
  U_DataLink,U_RTFun,U_JYOrderBGInPut
  {U_ClothContractInPutSX,
  U_ProductOrderList,U_ZDYHelp,
  ,U_ClothContractInPutHZ,U_ClothContractInPutPB}, U_ModuleNote;

{$R *.dfm}

procedure TfrmJYOrderBGChk.FormDestroy(Sender: TObject);
begin
  frmJYOrderBGChk:=nil;
end;

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

procedure TfrmJYOrderBGChk.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('报关单审核',Tv1,'合同管理');
end;

procedure TfrmJYOrderBGChk.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select A.*,FS.* ');
      SQL.Add(' from JYOrder_BG A inner join JYOrder_BG_Sub FS on A.BGID=FS.BGID');
      sql.Add(' where 1=1 ');
      if cxTabControl1.TabIndex=0 then
      begin
        sql.Add(' and isnull(A.ChkStatus,'''')<>''审核通过'' ');
      end else
      if cxTabControl1.TabIndex=1 then
      begin
        sql.Add(' and A.BGDate>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+'''');
        sql.Add(' and A.BGDate<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.DateTime+1))+'''');
        sql.Add(' and isnull(A.ChkStatus,'''')=''审核通过'' ');
      end else
      begin
        sql.Add(' and A.BGDate>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+'''');
        sql.Add(' and A.BGDate<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.DateTime+1))+'''');
      end;
      sql.Add(' order by A.BGNO');
      {if Trim(DParameters1)<>'高权限' then
      begin
        sql.Add(' and A.Filler='''+Trim(DName)+'''');
      end;}
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;

end;
procedure TfrmJYOrderBGChk.InitGridWSql(fsj:string);
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select A.*,FS.* ');
      SQL.Add(' from JYOrder_BG A inner join JYOrder_BG_Sub FS on A.BGID=FS.BGID');
      sql.Add(' where 1=1 ');
      if cxTabControl1.TabIndex=0 then
      begin
        sql.Add(' and isnull(A.ChkStatus,'''')<>''审核通过'' ');
      end else
      if cxTabControl1.TabIndex=1 then
      begin
        sql.Add(' and isnull(A.ChkStatus,'''')=''审核通过'' ');
      end else
      begin
      end;
      sql.Add(fsj);
      sql.Add(' order by A.BGNO');
      {if Trim(DParameters1)<>'高权限' then
      begin
        sql.Add(' and A.Filler='''+Trim(DName)+'''');
      end;}
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;

end;

procedure TfrmJYOrderBGChk.InitForm();
begin
  ReadCxGrid('报关单审核',Tv1,'生产合同管理');
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime:=EndDate.DateTime-30;
end;

procedure TfrmJYOrderBGChk.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 TfrmJYOrderBGChk.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Contract_Sub where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Contract_Sub where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      Open;
    end;
    if ADOQueryTemp.IsEmpty then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete Contract_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
        ExecSQL;
      end;
    end;
    if Trim(Order_Main.fieldbyname('SubId').AsString)='' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete Contract_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
        ExecSQL;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

procedure TfrmJYOrderBGChk.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  TcxGridToExcel('补充协议列表',cxGrid1);
end;

procedure TfrmJYOrderBGChk.TBPrintClick(Sender: TObject);
var
   fPrintFile,FConNoM,RPTName:string;
   i:Integer;
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 ADOQueryTemp do
  begin
    Filtered:=False;
    Close;
    sql.Clear;
    sql.Add('select A.*,B.* ');
    sql.Add(' ,C.ZdyName,C.ZdyStr1,C.ZdyStr2,C.ZdyStr3,C.ZdyStr4');
    sql.Add(' from ContractBC_Main A inner join ContractBC_Sub B on A.MainId=B.MainId');
    sql.Add(' left join KH_Zdy C on A.CompanyName=C.ZdyName and C.Type=''GSTT'' ');
    sql.Add(' where A.Mainid='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_PRT);
  SInitCDSData20(ADOQueryTemp,CDS_PRT);
  if FileExists(fPrintFile) then
  begin
    RM1.ShowPrintDialog:=True;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+RPTName),'提示',0);
  end;

end;

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

procedure TfrmJYOrderBGChk.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 TfrmJYOrderBGChk.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmJYOrderBGChk.conPress(Sender: TObject;
  var Key: Char);
var
  fsj:string;
begin
  if Key=#13 then
  begin
    if Length(Trim(FPNo.Text))<3 then Exit;
    fsj:=' and A.FPNo like '''+'%'+Trim(FPNo.text)+'%'+'''';
    InitGridWSql(fsj);
  end;
end;

procedure TfrmJYOrderBGChk.cxPageControl1Change(Sender: TObject);
begin
 // InitGrid();
end;

procedure TfrmJYOrderBGChk.ToolButton1Click(Sender: TObject);
var
  FFFId:string;
begin
  if cxTabControl1.TabIndex<>0 then Exit;
  if Order_Main.IsEmpty then Exit;
  if Application.MessageBox('确定要审核通过吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_BG Set Chker='''+Trim(DName)+'''');
    SQL.Add(',ChkTime=getdate(),ChkStatus=''审核通过'' ');
    sql.Add(' where BGId='''+Trim(Order_Main.fieldbyname('BGId').AsString)+'''');
    ExecSQL;
  end;
  FFFId:=Trim(Order_Main.fieldbyname('BGId').AsString);
  Order_Main.DisableControls;
  with Order_Main do
  begin
    while Locate('BGId',Trim(FFFId),[]) do
    begin
      Order_Main.Delete;
    end;
  end;
  Order_Main.EnableControls;
end;

procedure TfrmJYOrderBGChk.ToolButton2Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    frmJYOrderBGInPut:=TfrmJYOrderBGInPut.Create(Application);
    with frmJYOrderBGInPut do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('BGID').AsString);
      FConNo:=Trim(Self.Order_Main.fieldbyname('BGNo').AsString);
      TBSave.Visible:=False;
      ToolBar2.Visible:=False;
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmJYOrderBGInPut.Free;
  end;
end;

procedure TfrmJYOrderBGChk.ToolButton3Click(Sender: TObject);
begin
  try
    frmModuleNote:=TfrmModuleNote.Create(Application);
    with frmModuleNote do
    begin
      flag:='报关单审核';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmModuleNote.Free;
  end;
end;

procedure TfrmJYOrderBGChk.FormCreate(Sender: TObject);
begin
  canshu1:=Trim(DParameters1);
end;

procedure TfrmJYOrderBGChk.JYConNoKeyPress(Sender: TObject;
  var Key: Char);
var
  fsj:String;
begin
  if Key=#13 then
  begin
    if Length(Trim(JYConNo.Text))<3 then Exit;
    fsj:=' and JYConNo like '''+'%'+Trim(JYConNo.text)+'%'+'''';
    InitGridWSql(fsj);
  end;
end;

procedure TfrmJYOrderBGChk.JYStyleNoKeyPress(Sender: TObject;
  var Key: Char);
var
  fsj:String;
begin
  if Key=#13 then
  begin
    if Length(Trim(JYStyleNo.Text))<3 then Exit;
    fsj:=' and JYStyleNo like '''+'%'+Trim(JYStyleNo.text)+'%'+'''';
    InitGridWSql(fsj);
  end;
end;

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

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

procedure TfrmJYOrderBGChk.ToolButton4Click(Sender: TObject);
var
  FFFId:string;
begin
  if cxTabControl1.TabIndex<>1 then Exit;
  if Order_Main.IsEmpty then Exit;
  if Application.MessageBox('确定要审核撤销吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_BG Set Chker=Null');
    SQL.Add(',ChkTime=null,ChkStatus=null ');
    sql.Add(' where BGId='''+Trim(Order_Main.fieldbyname('BGId').AsString)+'''');
    ExecSQL;
  end;
  FFFId:=Trim(Order_Main.fieldbyname('BGId').AsString);
  Order_Main.DisableControls;
  with Order_Main do
  begin
    while Locate('BGId',Trim(FFFId),[]) do
    begin
      Order_Main.Delete;
    end;
  end;
  Order_Main.EnableControls;
end;

procedure TfrmJYOrderBGChk.ConNoKeyPress(Sender: TObject;
  var Key: Char);
var
  fsj:string;
begin
  if Key=#13 then
  begin
    if Length(Trim(ConNo.Text))<3 then Exit;
    fsj:=' and ConNo like '''+'%'+Trim(ConNo.text)+'%'+'''';
    InitGridWSql(fsj);
  end;
end;


procedure TfrmJYOrderBGChk.BGNOKeyPress(Sender: TObject; var Key: Char);
var
  fsj:string;
begin
  if Key=#13 then
  begin
    if Length(Trim(BGNo.Text))<3 then Exit;
    fsj:=' and A.BGNo like '''+'%'+Trim(BGNo.text)+'%'+'''';
    InitGridWSql(fsj);
  end;
end;


end.
