unit U_CaiGouSQChkList;

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, cxDropDownEdit, cxCheckBox;

type
  TfrmCaiGouSQChkList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    Panel1: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    TBFind: TToolButton;
    ToolButton1: TToolButton;
    ToolButton4: TToolButton;
    cxTabControl1: TcxTabControl;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1SSel: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Label4: TLabel;
    Label5: TLabel;
    Label3: TLabel;
    Label6: TLabel;
    SPName: TEdit;
    SQPerson: TEdit;
    PinPai: TEdit;
    FactoryName: TEdit;
    Label7: TLabel;
    ConNo: TEdit;
    v1Column19: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    ToolButton2: TToolButton;
    RM1: TRMGridReport;
    RMDBPRT: TRMDBDataSet;
    CDS_PRT: TClientDataSet;
    ToolButton3: TToolButton;
    v1Column21: TcxGridDBColumn;
    v1Column22: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    v1Column24: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure cxPageControl1Change(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
  private
    canshu1,canshu2:string;
    procedure InitGrid();
    procedure InitForm();
    { Private declarations }
  public
    { Public declarations }
  end;
var
  frmCaiGouSQChkList:TfrmCaiGouSQChkList;


implementation
uses
  U_DataLink,U_RTFun,U_CaiGouSQInPut,U_ModuleNote, U_ClothSCHZMXListSel;

{$R *.dfm}

procedure TfrmCaiGouSQChkList.FormDestroy(Sender: TObject);
begin
  frmCaiGouSQChkList:=nil;
end;

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

procedure TfrmCaiGouSQChkList.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('采购申请审批列表',Tv1,'采购申请管理');
end;

procedure TfrmCaiGouSQChkList.InitGrid();
begin
  BegDate.SetFocus;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' select A.* ');
      SQL.Add(' from CaiGou_SQ A where isnull(SChkStatus,'''')<>'''' and SQType=''纱线'' ');
      if cxTabControl1.TabIndex=0 then
      begin
        sql.Add(' and isnull(ChkStatus,'''')='''' ');
      end else
      if cxTabControl1.TabIndex=1 then
      begin
        sql.Add(' and isnull(ChkStatus,'''')=''已审核'' ');
      end;
      if cxTabControl1.TabIndex<>0 then
      begin
        sql.Add(' and A.SQDate>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''');
        sql.Add(' and A.SQDate<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date+1))+'''');
      end;
      sql.Add(' order by SQDate');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCaiGouSQChkList.InitForm();
begin
  EndDate.Date:=SGetServerDate(ADOQueryTemp);
  BegDate.Date:=EndDate.Date-7;
  ReadCxGrid('采购申请审批列表',Tv1,'采购申请管理');
  InitGrid();
end;

procedure TfrmCaiGouSQChkList.TBExportClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  TcxGridToExcel('纱线采购加工审核列表',cxGrid1);
end;

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

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

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

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

procedure TfrmCaiGouSQChkList.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 TfrmCaiGouSQChkList.ToolButton1Click(Sender: TObject);
begin
  if cxTabControl1.TabIndex<>0 then Exit;
  if Order_Main.IsEmpty then Exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  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 CaiGou_SQ Set ChkStatus=''已审核'',Chktime=getdate(),Chker='''+Trim(DName)+'''');
          sql.Add(' where SQID='''+Trim(Order_Main.fieldbyname('SQID').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 TfrmCaiGouSQChkList.ToolButton4Click(Sender: TObject);
begin
  if cxTabControl1.TabIndex<>1 then Exit;
  if Order_Main.IsEmpty then Exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  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 CaiGou_SQ Set ChkStatus=Null,Chktime=getdate(),Chker='''+Trim(DName)+'''');
          sql.Add(' where SQID='''+Trim(Order_Main.fieldbyname('SQID').AsString)+'''');
          sql.Add(' and not exists(select * from CK_SXPB_CR A where A.SQID=CaiGou_SQ.SQID)');
          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 TfrmCaiGouSQChkList.ToolButton2Click(Sender: TObject);
var
   fPrintFile,FPBName:string;
begin
  if Order_Main.IsEmpty then Exit;
  if cxTabControl1.TabIndex<>1 then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\原料采购单.rmf' ;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    sql.Add(',KHConNo=(select KHConNo from JYOrderCon_Main C where C.MainId=isnull(B.ConMainId,''''))');
    SQL.Add(' from CaiGou_SQ A  ');
    sql.Add(' left join Cloth_Main B on A.ConNo=B.ConNo');
    sql.add(' where isnull(A.chker,'''')<>'''' and A.SQType=''纱线''');
    SQL.Add(' and A.SQID='''+Trim(Order_Main.fieldbyname('SQID').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_PRT);
  SInitCDSData20(ADOQueryTemp,CDS_PRT);
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\原料采购单.rmf'),'提示',0);
  end;
end;

procedure TfrmCaiGouSQChkList.ToolButton3Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if Trim(Order_Main.fieldbyname('ConNo').AsString)='' then Exit;
  try
    frmClothSCHZMXListSel:=TfrmClothSCHZMXListSel.Create(Application);
    with frmClothSCHZMXListSel do
    begin
      RZConNO:=Trim(Self.Order_Main.fieldbyname('ConNo').AsString);
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmClothSCHZMXListSel.Free;
  end;
end;

end.
