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, Clipbrd,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator;

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;
    v1Column6: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label8: TLabel;
    Label12: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    SPName: TEdit;
    FactoryName: TEdit;
    SPID: TEdit;
    SPSpec: TEdit;
    CRType: TComboBox;
    SPCF: TEdit;
    PBNO: TEdit;
    Label1: TLabel;
    ToFactoryName: TEdit;
    v1Column16: TcxGridDBColumn;
    ToolButton2: TToolButton;
    v1Column17: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    Label2: TLabel;
    BatchNo: TEdit;
    v1Column18: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    Label9: TLabel;
    SXPinPai: TEdit;
    v1Column24: TcxGridDBColumn;
    Label10: TLabel;
    SPNameGYS: TEdit;
    v1Column25: TcxGridDBColumn;
    Label11: TLabel;
    SPColor: TEdit;
    ToolButton3: TToolButton;
    RM1: TRMGridReport;
    RMXLSExport1: TRMXLSExport;
    RMDBMain: TRMDBDataSet;
    CDS_Prt: TClientDataSet;
    v1Column26: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    Label13: TLabel;
    KuWei: TEdit;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    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 N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure SXBatchNOChange(Sender: TObject);
    procedure FactoryNameChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
  private
    procedure InitGrid();
    { Private declarations }
  public
    FDYFlag: string;
    { Public declarations }
  end;

var
  frmShaKCList: TfrmShaKCList;

implementation

uses
  U_DataLink, U_RTFun, U_SXCKInPut, U_ShaOutList;

{$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();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select A.* ,CKQty=isnull(Qty,0)-isnull(MXKCQty,0),CKPiQty=isnull(PiQty,0)-isnull(MXKCPiQty,0)');
      sql.Add(' from CK_SXPB_CR A where isnull(CKName,'''')=''纱线'' ');
      SQL.Add(' and A.CRFlag=''入库'' and MXKCQty>0 and ZFFlag=0 ');
      Open;
      //ShowMessage(SQL.Text);
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmShaKCList.TBRafreshClick(Sender: TObject);
begin
  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('纱线库存列表2', Tv1, '纱线仓库');
  Close;
end;

procedure TfrmShaKCList.FormShow(Sender: TObject);
begin
  ReadCxGrid('纱线库存列表2', 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.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, True);
end;

procedure TfrmShaKCList.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, False);
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.ToolButton2Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;
  TcxGridToExcel('纱线库存列表', cxGrid2);
end;

procedure TfrmShaKCList.ToolButton3Click(Sender: TObject);
var
  filepath: string;
begin
  if CDS_Main.IsEmpty then
    Exit;

  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据', '提示', 0);
    Exit;
  end;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete TBSubID where DName=''' + Trim(DCode) + '''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('SELECT * FROM TBSubID where 1=2 ');
    open;
  end;
  CDS_Main.DisableControls;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      First;
      while not Eof do
      begin
        if Fieldbyname('Ssel').AsBoolean then
        begin
          ADOQueryCmd.append;
          ADOQueryCmd.fieldbyname('SubId').Value := Trim(CDS_Main.fieldbyname('spid').AsString);
          ADOQueryCmd.fieldbyname('Dname').Value := Trim(DCode);
          ADOQueryCmd.post;
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;
  CDS_Main.EnableControls;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select FactoryName,SPName,SPCF,SPSpec,QtyUnit,sum(MXKCPiQty) MXKCPiQty,sum(MXKCQty) MXKCQty ');
    SQL.Add('from CK_SXPB_CR A where isnull(CKName,'''')=''纱线''');
    SQL.Add('and A.CRFlag=''入库'' and MXKCQty>0 and ZFFlag=0 ');
    SQL.Add(' group by FactoryName,SPName,SPCF,SPSpec,QtyUnit  ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_Prt);
  SInitCDSData20(ADOQueryTemp, CDS_Prt);
  try
    filepath := ExtractFilePath(Application.ExeName) + 'report\纱线库存报表.rmf';
    if not FileExists(Pchar(filepath)) then
    begin
      application.MessageBox(pchar('文件[' + filepath + ']不存在！'), '提示信息', MB_IConError);
      exit;
    end;
    RM1.LoadFromFile(filepath);
    RM1.ShowReport;
  finally

  end;

end;

procedure TfrmShaKCList.Tv1DblClick(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(CDS_Main.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

end.

