unit U_CPFHList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxGridCustomTableView, cxGridTableView,
  cxGridBandedTableView, cxGridDBBandedTableView, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridDBTableView, cxGrid, StdCtrls, ComCtrls,
  ExtCtrls, ToolWin, cxGridCustomPopupMenu, cxGridPopupMenu, ADODB, DBClient,
  cxDropDownEdit, cxCheckBox, RM_Common, RM_Class, RM_e_Xls, RM_Dataset,
  RM_System, RM_GridReport, Menus, cxCalendar, cxButtonEdit, cxTextEdit, cxPC,
  IniFiles, jpeg, U_SLT;

type
  TfrmCPFHList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    RMDBHZ: TRMDBDataSet;
    CDS_HZ: TClientDataSet;
    CDS_PRT: TClientDataSet;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label8: TLabel;
    Label12: TLabel;
    SPName: TEdit;
    FactoryName: TEdit;
    SPID: TEdit;
    SPSpec: TEdit;
    cbbDHType: TComboBox;
    Label6: TLabel;
    SPCF: TEdit;
    Label9: TLabel;
    ToFactoryName: TEdit;
    Label13: TLabel;
    BatchNo: TEdit;
    adoqueryPicture: TADOQuery;
    PopupMenu2: TPopupMenu;
    N3: TMenuItem;
    N4: TMenuItem;
    RMDBDataSet1: TRMDBDataSet;
    ClientDataSet1: TClientDataSet;
    RMGridReport1: TRMGridReport;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1CRType: TcxGridDBColumn;
    v1FactoryName: TcxGridDBColumn;
    v2SPName: TcxGridDBColumn;
    v1SPSpec: TcxGridDBColumn;
    v1SPCF: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1BatchNo: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    cxTabControl1: TcxTabControl;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure SPIDChange(Sender: TObject);
    procedure SPSpecChange(Sender: TObject);
    procedure FactoryNameChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure BatchNoKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton4Click(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
  private
    canshu1, canshu2, canshu3: string;
    procedure InitGrid();
    procedure ReadINIFile();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmCPFHList: TfrmCPFHList;

var
  Mach: array of TfrmSlt;

implementation

uses
  U_DataLink, U_RTFun; 

{$R *.dfm}

procedure TfrmCPFHList.FormDestroy(Sender: TObject);
begin
  frmCPFHList := nil;
end;

procedure TfrmCPFHList.FormClose(Sender: TObject; var Action: TCloseAction);
begin
   Action:=caHide;
end;

procedure TfrmCPFHList.ReadINIFile();
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 TfrmCPFHList.FormCreate(Sender: TObject);
begin
  //cxGrid1.Align:=alClient;
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime;
  canshu1 := Trim(DParameters1);
  canshu2 := Trim(DParameters2);
  canshu3 := Trim(DParameters3);
end;

procedure TfrmCPFHList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select A.* ');
      sql.Add(' from XH_Cloth_DH A');
      sql.add(' where A.DHDate>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)) + '''');
      sql.Add(' and A.DHDate<''' + Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)) + '''');
      sql.Add(' and DHType=''成品出库'' ');
      if cxTabControl1.TabIndex = 0 then
        SQL.Add(' and not exists(select YFPZNO from YF_Money_CR_Sub X where X.YFPZNO=A.DHID ) ')
      else
        SQL.Add(' and  exists(select YFPZNO from YF_Money_CR_Sub X where X.YFPZNO=A.DHID )');

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

procedure TfrmCPFHList.TBRafreshClick(Sender: TObject);
begin
  BegDate.SetFocus;
  InitGrid();
end;

procedure TfrmCPFHList.ConNoMChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  end;
end;

procedure TfrmCPFHList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('纱线入库列表', Tv1, '纱线仓库');
  Close;
end;

procedure TfrmCPFHList.FormShow(Sender: TObject);
begin
  ReadCxGrid('纱线入库列表', Tv1, '纱线仓库');

end;

procedure TfrmCPFHList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  end;
end;

procedure TfrmCPFHList.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, True);
end;

procedure TfrmCPFHList.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, False);
end;

procedure TfrmCPFHList.SPIDChange(Sender: TObject);
begin
  if Length(Trim(SPID.Text)) < 4 then
  begin
    if Trim(SPID.Text) <> '' then
      Exit;
  end;
  TBFind.Click;
end;

procedure TfrmCPFHList.SPSpecChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCPFHList.FactoryNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCPFHList.cxTabControl1Change(Sender: TObject);
begin
  INITGrid();
end;

procedure TfrmCPFHList.BatchNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key <> #13 then
    Exit;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select A.* ');
      sql.Add(' from CK_SXPB_CR A');
      sql.add(' where A.BatchNo=''' + Trim(BatchNo.Text) + '''');
      sql.Add(' and isnull(CKName,'''')=''纱线''');
      SQL.Add(' and CRFlag=''入库''  ');
      {if Trim(canshu2)<>'' then
      begin
        SQL.Add(' and isnull(DeptName,'''')='''+Trim(canshu2)+'''');
      end;}
      if cxTabControl1.TabIndex = 0 then
      begin
        Sql.Add(' and ZFFlag=0 ');
      end
      else if cxTabControl1.TabIndex = 1 then
      begin
        Sql.Add(' and ZFFlag=1 ');
      end;
      SQL.Add(' order by CRTime desc');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCPFHList.ToolButton4Click(Sender: TObject);
var
  fPrintFile: string;
begin
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    SQL.Add('exec P_Print_SXRK' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date))));
    SQL.Add(',''年'' ');
    Open;
//    ShowMessage(SQL.Text);
  end;
  SCreateCDS20(ADOQueryTemp, ClientDataSet1);
  SInitCDSData20(ADOQueryTemp, ClientDataSet1);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\纱线入库年报表.rmf';
  if FileExists(fPrintFile) then
  begin
//    RMVariables['ConNo']:=Order_Main.fieldbyname('ConNo').Value;
    RMGridReport1.LoadFromFile(fPrintFile);
    RMGridReport1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\纱线入库年报表.rmf'), '提示', 0);
  end;
end;

procedure TfrmCPFHList.TBExportClick(Sender: TObject);
begin
  if cxTabControl1.TabIndex <> 0 then  Exit;
  ModalResult := 1;
end;

end.

