unit U_ShaKCList;

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,Math,
  MovePanel, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxSkinsCore, dxSkinsDefaultPainters, dxSkinscxPCPainter;

type
  TfrmShaKCList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    CDS_Main: TClientDataSet;
    ToolButton1: TToolButton;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column14: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Label3: TLabel;
    Label4: TLabel;
    Label8: TLabel;
    Y_CodeName: TEdit;
    FactoryName: TEdit;
    Y_Spec: TEdit;
    Label1: TLabel;
    ToFactoryName: TEdit;
    ToolButton2: TToolButton;
    v1Column1: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    Label2: TLabel;
    BatchNo: TEdit;
    Label10: TLabel;
    Note: TEdit;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyleRepository2: TcxStyleRepository;
    cxStyle2: TcxStyle;
    cxStyle3: TcxStyle;
    cxStyle4: TcxStyle;
    ToolButton3: TToolButton;
    RM1: TRMGridReport;
    CDS_Print: TClientDataSet;
    RMDBPrint: TRMDBDataSet;
    v1Column2: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    CheckBox1: TMenuItem;
    CheckBox2: TMenuItem;
    v1Column4: TcxGridDBColumn;
    MovePanel2: TMovePanel;
    OldBuy: TMenuItem;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    cxStyleRepository3: TcxStyleRepository;
    cxStyle5: TcxStyle;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure MPRTCodeNameChange(Sender: TObject);
    procedure OldBuyClick(Sender: TObject);
    procedure SXBatchNOChange(Sender: TObject);
    procedure FactoryNameChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure TCBNORChange(Sender: TObject);
  private
    procedure InitGrid();
    { Private declarations }
  public
    FDYFlag: string;
    { Public declarations }
  end;

var
  frmShaKCList: TfrmShaKCList;

implementation

uses
  U_DataLink, U_RTFun, U_ShaOutList,U_Fun,U_OldShaRKList;

{$R *.dfm}

procedure TfrmShaKCList.FormDestroy(Sender: TObject);
begin
  frmShaKCList := nil;
end;

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

procedure TfrmShaKCList.InitGrid();
var
  fFSId : string;
begin
  MovePanel2.Left := (self.Width - MovePanel2.Width) div 2;
  MovePanel2.Visible := True;
  MovePanel2.Refresh;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
     // Filtered := False;
      Close;
      sql.Clear;
      sql.Add('exec P_View_SXKuC ');
      Open;

    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
    MovePanel2.Visible := false;
  finally
    ADOQueryMain.EnableControls;
    MovePanel2.Visible := false;
  end;
end;

procedure TfrmShaKCList.TBRafreshClick(Sender: TObject);
begin
  Panel1.SetFocus;
  InitGrid();
end;

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

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

procedure TfrmShaKCList.FormShow(Sender: TObject);
var
  fsj: string;
begin
  ReadCxGrid('纱线库存列表123', Tv1, '纱线仓库');
  if FDYFlag = '99' then
  begin
    InitGrid();
    TBFind.Click;
  end;
 
  //InitGrid();
end;

procedure TfrmShaKCList.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 TfrmShaKCList.MPRTCodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmShaKCList.OldBuyClick(Sender: TObject);
begin
  try
    frmOldShaRKList:= TfrmOldShaRKList.Create(Application);
    with frmOldShaRKList do
    begin
      FY_CodeName := Trim(Self.CDS_Main.fieldbyname('Y_CodeName').AsString);
      FY_Spec := Trim(Self.CDS_Main.fieldbyname('Y_Spec').AsString);
      FFactoryName := Trim(Self.CDS_Main.fieldbyname('FactoryName').AsString);
      FY_CF := Trim(Self.CDS_Main.fieldbyname('Y_CF').AsString);
      Fvalue:='KC';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmOldShaRKList.Free;
  end;
end;

procedure TfrmShaKCList.SXBatchNOChange(Sender: TObject);
begin
  TBFind.Click;
end;

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

procedure TfrmShaKCList.ToolButton1Click(Sender: TObject);
begin
  ModalResult := 1;
end;

procedure TfrmShaKCList.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
 { try
    frmShaOutList:=TfrmShaOutList.Create(Application);
    with frmShaOutList do
    begin
      FFY_ID:=Trim(Self.CDS_Main.fieldbyname('Y_ID').AsString);
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmShaOutList.Free;
  end;}
end;

procedure TfrmShaKCList.ToolButton2Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;
  TcxGridToExcel('纱线库存列表', cxGrid2);
end;

procedure TfrmShaKCList.ToolButton3Click(Sender: TObject);
var
  fPrintFile: string;
  Txt, fImagePath,CNSql,FNSql,BNSql,CTSql: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin

  if CDS_Main.IsEmpty then
    Exit;

  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  CNSql := '';
  BNSql := '';
  FNSql := '';
  CTSql := '';
  CDS_Main.DisableControls;
  with CDS_Main do
  begin
    First;
    while CDS_Main.Locate('SSel', True, []) do
    begin
      if CNSql <> '' then
        CNSql := CNSql  +''+','+'' + QuotedStr(Trim(CDS_Main.fieldbyname('Y_CodeName').AsString))
      else
      CNSql :='and isnull(A.Y_CodeName,'''') in (' + QuotedStr(Trim(CDS_Main.fieldbyname('Y_CodeName').AsString));

      if FNSql <> '' then
        FNSql := FNSql  +''+','+'' + QuotedStr(Trim(CDS_Main.fieldbyname('FactoryName').AsString))
      else
      FNSql :='and isnull(A.FactoryName,'''') in (' + QuotedStr(Trim(CDS_Main.fieldbyname('FactoryName').AsString));

      if BNSql <> '' then
        BNSql := BNSql  +''+','+'' + QuotedStr(Trim(CDS_Main.fieldbyname('BatchNo').AsString))
      else
      BNSql :='and isnull(A.BatchNo,'''') in (' + QuotedStr(Trim(CDS_Main.fieldbyname('BatchNo').AsString));

//      if CTSql <> '' then
//        CTSql := CTSql  +''+','+'' + QuotedStr(Trim(CDS_Main.fieldbyname('CRType').AsString))
//      else
//      CTSql :='and A.CRType in (' + QuotedStr(Trim(CDS_Main.fieldbyname('CRType').AsString));

      Edit;
      FieldByName('SSel').Value := False;
      Post;
    end;
  end;
  CDS_Main.EnableControls;
  CNSql := CNSql+')';
  FNSql := FNSql+')';
  BNSql := BNSql+')';
//  CTSql := CTSql+')';
   with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select A.Y_CodeName,A.FactoryName,SUM(A.MXKCQty) MXKCQty,SUM (A.MXKCPiQty) MXKCPiQty ');
      sql.Add(' from CK_Yarn_CR A where isnull(CKName,'''')=''纱线'' ');
      SQL.Add(' and A.CRFlag=''入库'' and MXKCQty>0  ');
      SQL.Add(CNSql);
      SQL.Add(FNSql);
      SQL.Add(BNSql);
 //     SQL.Add(CTSql);
      SQL.Add(' GROUP BY A.CRType,A.Y_CodeName,A.ConNo,A.Y_Spec,Y_CF,A.FactoryName,A.Price,A.BatchNO,A.Note,A.QtyUnit');
    // ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryCmd, CDS_Print);
    SInitCDSData20(ADOQueryCmd, CDS_Print);

    if ADOQueryCmd.IsEmpty then
    begin
      application.MessageBox('标签内容未找到！', '提示信息', MB_ICONERROR);
      exit;
    end;
    ExportFtErpFile('纱线库存报表.rmf', ADOQueryTemp);
    fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\纱线库存报表.rmf';

    if FileExists(fPrintFile) then
    begin
      RM1.LoadFromFile(fPrintFile);
      RM1.ShowReport;
     // RM1.PrintReport;
    end
    else
    begin
      Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
    end;

end;

procedure TfrmShaKCList.CheckBox1Click(Sender: TObject);
begin
SelOKNo(CDS_Main, True);
end;

procedure TfrmShaKCList.CheckBox2Click(Sender: TObject);
begin
SelOKNo(CDS_Main, False);
end;

procedure TfrmShaKCList.TCBNORChange(Sender: TObject);
begin
  Panel1.SetFocus;
  InitGrid();
end;

end.

