unit U_SXDZViewSCList;

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;

type
  TfrmSXDZViewSCList = 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;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    RMDBHZ: TRMDBDataSet;
    CDS_HZ: TClientDataSet;
    CDS_PRT: TClientDataSet;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    v1Column5: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    SPName: TEdit;
    FactoryName: TEdit;
    SXID: TEdit;
    v1Column6: TcxGridDBColumn;
    Label7: TLabel;
    ConNo: TEdit;
    v1Column9: TcxGridDBColumn;
    v1Column1: 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 TBExportClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure SXIDKeyPress(Sender: TObject; var Key: Char);
    procedure SXIDChange(Sender: TObject);
    procedure BatchNOChange(Sender: TObject);
    procedure FactoryNameChange(Sender: TObject);
  private
    procedure InitGrid();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmSXDZViewSCList: TfrmSXDZViewSCList;

implementation
uses
  U_DataLink,U_RTFun;

{$R *.dfm}

procedure TfrmSXDZViewSCList.FormDestroy(Sender: TObject);
begin
  frmSXDZViewSCList:=nil;
end;

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

procedure TfrmSXDZViewSCList.FormCreate(Sender: TObject);
begin
  //cxGrid1.Align:=alClient;
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime:=SGetServerDateMBeg(ADOQueryTemp);
end;

procedure TfrmSXDZViewSCList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' select A.CRTime,A.SPID,A.FactoryName,A.FactoryNo,A.SPName,A.PiQty,A.KGQty,A.Price,A.Note ');
      sql.Add(',ConNo=(select ConNo from Contract_Main CM where CM.MainId=A.CGMainId),Money=Cast(Price*KGQty as decimal(18,2))');
      sql.Add(' from CK_SXPB_CR A');
      sql.add(' where A.CRTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+'''');
      sql.Add(' and A.CRTime<='''+Trim(FormatDateTime('yyyy-MM-dd',enddate.DateTime+1))+'''');
      sql.Add(' and isnull(CKName,'''')=''纱线''');
      SQL.Add(' and CRFlag=''入库''  ');
      sql.Add(' union all');
      sql.Add(' select CRTime=A.MDDate,SPID=A.MDNO,A.FactoryName,A.FactoryNo,SPName=B.C_CodeName');
      sql.Add(',PiQty=A.PS,KGQty=A.Qty,Price=B.Price,Note='''',C.ConNo,Money=Cast(B.Price*A.Qty as decimal(18,2)) ');
      sql.Add(' from JYOrder_Main_MD A');
      sql.Add(' inner join Contract_Sub B on A.ConSubId=B.SubId');
      sql.Add(' inner join Contract_Main C on B.MainId=C.MainId');
      sql.Add(' where isnull(A.PSName,'''') in (''坯纱'',''染纱'')');
      sql.Add(' and isnull(A.CKName,'''')='''' ');
      Open;
      //ShowMessage(SQL.Text);
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

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

procedure TfrmSXDZViewSCList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('纱线对账信息列表',Tv1,'纱线仓库');
  Close;
end;

procedure TfrmSXDZViewSCList.FormShow(Sender: TObject);
begin
  ReadCxGrid('纱线对账信息列表',Tv1,'纱线仓库');
  //InitGrid();
end;

procedure TfrmSXDZViewSCList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then exit;
  TcxGridToExcel('纱线对账信息列表',cxGrid2);
end;

procedure TfrmSXDZViewSCList.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 TfrmSXDZViewSCList.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main,True);
end;

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

procedure TfrmSXDZViewSCList.SXIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    if Length(Trim(SXID.Text))<4 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add(' select A.* ');
        sql.Add(',ConNo=(select ConNo from Contract_Main CM where CM.MainId=A.CGMainId)');
        sql.Add(' from CK_SXPB_CR A');
        sql.add(' where SPID like :SXID');
        Parameters.ParamByName('SXID').Value:='%'+Trim(SXID.Text)+'%';
        Open;
        //ShowMessage(SQL.Text);
      end;
      SCreateCDS20(ADOQueryMain,CDS_Main);
      SInitCDSData20(ADOQueryMain,CDS_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmSXDZViewSCList.SXIDChange(Sender: TObject);
begin
  if Length(Trim(SXID.Text))<4 then
  begin
    if Trim(SXID.Text)<>'' then Exit;
  end;
  TBFind.Click;
end;

procedure TfrmSXDZViewSCList.BatchNOChange(Sender: TObject);
begin
  TBFind.Click;
end;

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

end.
