unit U_CaiGouSQList;

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
  TfrmCaiGouSQList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: 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;
    SPName: TEdit;
    Label4: TLabel;
    SQPerson: TEdit;
    Label5: TLabel;
    TBSS: TToolButton;
    TBSSCX: TToolButton;
    cxTabControl1: TcxTabControl;
    Label3: TLabel;
    Label6: TLabel;
    PinPai: TEdit;
    FactoryName: TEdit;
    Label7: TLabel;
    ConNo: TEdit;
    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;
    v1Column19: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ToolButton1: TToolButton;
    CDS_PRT: TClientDataSet;
    RMDBPRT: TRMDBDataSet;
    RM1: TRMGridReport;
    v1Column21: TcxGridDBColumn;
    v1Column22: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    v1Column24: TcxGridDBColumn;
    v1Column25: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(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 cxPageControl1Change(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBSSClick(Sender: TObject);
    procedure TBSSCXClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
  private

    canshu1,canshu2,ChildFlag:string;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    { Private declarations }
  public

    { Public declarations }
  end;

//var
 // frmCaiGouSQList: TfrmCaiGouSQList;

implementation
uses
  U_DataLink,U_RTFun,U_CaiGouSQInPut,U_ModuleNote;

{$R *.dfm}

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

procedure TfrmCaiGouSQList.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid(Self.Caption,Tv1,'采购申请管理');
end;

procedure TfrmCaiGouSQList.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 SQType=''纱线'' ');
      if Trim(canshu1)='录入' then
      begin
        sql.Add(' and Filler='''+Trim(DName)+'''');
      end;
      if Trim(canshu1)<>'查询' then
      begin
        if cxTabControl1.TabIndex=0 then
        begin
          sql.Add(' and isnull(SChkStatus,'''')='''' ');
        end else
        if cxTabControl1.TabIndex=1 then
        begin
          sql.Add(' and isnull(SChkStatus,'''')<>'''' and isnull(ChkStatus,'''')='''' ');
        end  else
        if cxTabControl1.TabIndex=2 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;
      end else
      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))+'''');
        sql.Add(' and isnull(Chker,'''')<>'''' ');
      end;
      sql.Add(' order by SQDate desc');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCaiGouSQList.InitForm();
begin
  if Self.Caption='纱线采购加工申请录入' then
  begin
    canshu1:='录入';
  end else
  if Self.Caption='纱线采购加工申请高权限' then
  begin
    canshu1:='高权限';
  end else
  if Self.Caption='纱线采购加工申请查询' then
  begin
    canshu1:='查询';
  end;
  if canshu1='查询' then
  begin
    TBAdd.Visible:=False;TBEdit.Visible:=False;TBDel.Visible:=False;
    TBSS.Visible:=False;TBSSCX.Visible:=False;cxTabControl1.Visible:=False;
  end;
  EndDate.Date:=SGetServerDate(ADOQueryTemp);
  BegDate.Date:=EndDate.Date-7;
  ReadCxGrid(Self.Caption,Tv1,'采购申请管理');
  if Trim(canshu1)='查询' then
  begin
    v1SSel.Visible:=False;
    v1SSel.Hidden:=True;
  end;
  InitGrid();
end;

procedure TfrmCaiGouSQList.TBEditClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if cxTabControl1.TabIndex<>0 then Exit;
  {if Self.Caption<>'纱线采购加工申请高权限' then
  begin
    if cxTabControl1.TabIndex<>0 then Exit;
  end;}
  try
    frmCaiGouSQInPut:=TfrmCaiGouSQInPut.Create(Application);
    with frmCaiGouSQInPut do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('SQID').AsString);
      frmCaiGouSQInPut.ToolButton3.Visible:=False;
      frmCaiGouSQInPut.ToolButton4.Visible:=False;
      FSQType:=Trim(Self.canshu2);
      {if Self.Caption='纱线采购加工申请高权限' then
      begin
        ZFormName:='可修改';
      end; }

      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmCaiGouSQInPut.Free;
  end;
end;

procedure TfrmCaiGouSQList.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if cxTabControl1.TabIndex<>0 then Exit;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if DelData() then
  begin
    Order_Main.Delete;
  end;
end;

function TfrmCaiGouSQList.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete CaiGou_SQ  where SQId='''+Trim(Order_Main.fieldbyname('SQId').AsString)+'''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

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

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

procedure TfrmCaiGouSQList.TBAddClick(Sender: TObject);
begin
  try
    frmCaiGouSQInPut:=TfrmCaiGouSQInPut.Create(Application);
    with frmCaiGouSQInPut do
    begin
      PState:=0;
      FMainId:='';
      FSQType:=Trim(Self.canshu2);
      frmCaiGouSQInPut.Canshu1:=Trim(Self.canshu1);
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmCaiGouSQInPut.Free;
  end;
end;

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

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

procedure TfrmCaiGouSQList.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 TfrmCaiGouSQList.cxTabControl1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmCaiGouSQList.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 TfrmCaiGouSQList.TBSSClick(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 SChkStatus=''已送审'',SChktime=getdate(),SChker='''+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 TfrmCaiGouSQList.TBSSCXClick(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 ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CaiGou_SQ  ');
          sql.Add(' where SQID='''+Trim(Order_Main.fieldbyname('SQID').AsString)+'''');
          Open;
        end;
        if Trim(ADOQueryTemp.fieldbyname('ChkStatus').AsString)='已审核' then
        begin
          Order_Main.EnableControls;
          ADOQueryCmd.Connection.RollbackTrans;
          InitGrid();
          Application.MessageBox('已审核的数据不能撤销!','提示',0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate CaiGou_SQ Set SChkStatus=Null,SChktime=getdate(),SChker='''+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 TfrmCaiGouSQList.FormDestroy(Sender: TObject);
begin
  //frmCaiGouSQList:=nil;
end;

procedure TfrmCaiGouSQList.ToolButton1Click(Sender: TObject);
var
   fPrintFile,FPBName:string;
begin
  if Order_Main.IsEmpty then Exit;
  if cxTabControl1.TabIndex<>2 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;


end.
