unit U_PBCKKCList;

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,
  cxCurrencyEdit, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxSkinsCore, dxSkinsDefaultPainters, dxSkinscxPCPainter, dxSkinBlack,
  dxSkinBlue, dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom,
  dxSkinDarkSide, dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle,
  dxSkinFoggy, dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary,
  dxSkinLilian, dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin,
  dxSkinMetropolis, dxSkinMetropolisDark, dxSkinMoneyTwins,
  dxSkinOffice2007Black, dxSkinOffice2007Blue, dxSkinOffice2007Green,
  dxSkinOffice2007Pink, dxSkinOffice2007Silver, dxSkinOffice2010Black,
  dxSkinOffice2010Blue, dxSkinOffice2010Silver, dxSkinOffice2013DarkGray,
  dxSkinOffice2013LightGray, dxSkinOffice2013White, dxSkinPumpkin,
  dxSkinSeven, dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus,
  dxSkinSilver, dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008,
  dxSkinTheAsphaltWorld, dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint,
  dxSkinXmas2008Blue;

type
  TfrmPBCKKCList = 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;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ToolButton1: TToolButton;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Label7: TLabel;
    OrderNo: TEdit;
    v1Column16: TcxGridDBColumn;
    ToolButton2: TToolButton;
    Label14: TLabel;
    SPCode: TEdit;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    ToolButton3: TToolButton;
    PopupMenu2: TPopupMenu;
    MenuItem1: TMenuItem;
    MenuItem2: TMenuItem;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    v1Column24: TcxGridDBColumn;
    v1Column25: TcxGridDBColumn;
    v1Column22: TcxGridDBColumn;
    Label1: TLabel;
    SPName: TEdit;
    Tv1Column1: TcxGridDBColumn;
    Label2: TLabel;
    CustomerNoName: TEdit;
    Tv1Column2: 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 Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
  private
    procedure InitGrid();
    { Private declarations }
  public
    canshu1: string;
    { Public declarations }
  end;

var
  frmPBCKKCList: TfrmPBCKKCList;

implementation

uses
  U_DataLink, U_RTFun, U_SXCKInPut;

{$R *.dfm}

procedure TfrmPBCKKCList.FormDestroy(Sender: TObject);
begin
  frmPBCKKCList := nil;
end;

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

procedure TfrmPBCKKCList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select  CustomerNoName, SPCode,spname,FactoryName,QtyUnit,ToFactoryName,sum(MXKCPiQty)MXKCPiQty,sum(MXKCQty)MXKCQty,note ');

      sql.Add(' from CK_SXPB_CR A where isnull(CKName,'''')=''坯布'' ');
      SQL.Add(' and A.CRFlag=''入库'' ');
      sql.Add('group by CustomerNoName,SPCode,spname,FactoryName,ToFactoryName,QtyUnit,note');
      SQL.ADD('HAVING sum(MXKCPiQty)>0 AND sum(MXKCQty)>0');

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

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

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

procedure TfrmPBCKKCList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('坯布库存列表', Tv1, '坯布仓库0326');
  Close;
end;

procedure TfrmPBCKKCList.FormShow(Sender: TObject);
begin
//  CANSHU1 := DParameters1;
  ReadCxGrid('坯布库存列表', Tv1, '坯布仓库0326');
//  if canshu1 <> '管理' then
//  begin
//    FILLER.Text := Trim(DName);
//  end;
  InitGrid();
end;

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

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

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

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

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

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

procedure TfrmPBCKKCList.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  ModalResult := 1;
end;

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

procedure TfrmPBCKKCList.ToolButton3Click(Sender: TObject);
var
  fPrintFile: string;
  Porderno, LBName, SYRName: string;
  i, j: Integer;
  OrderKg: Double;
begin
  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据/Please choose!', '提示', 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('SPNAME').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 ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add(' select SPCode,spname,FactoryName,QtyUnit,ToFactoryName,sum(MXKCPiQty)MXKCPiQty,sum(MXKCQty)MXKCQty ');

    sql.Add(' from CK_SXPB_CR A where isnull(CKName,'''')=''坯布'' ');
    sql.add(' and  A.spname in (SELECT distinct SubId FROM TBSubID where Dname=' + quotedstr(Trim(DCode)) + ')');
    SQL.Add(' and A.CRFlag=''入库'' ');
    sql.Add('group by SPCode,spname,FactoryName,ToFactoryName,QtyUnit');
    SQL.ADD('HAVING sum(MXKCPiQty)>0 AND sum(MXKCQty)>0');

//    ShowMessage(SQL.Text);
    Open;
  end;

  SCreateCDS20(ADOQueryPrint, CDS_Print);
  SInitCDSData20(ADOQueryPrint, CDS_Print);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\坯布库存列表.rmf';
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
//    RMVariables['SYRName']:=Trim(SYRName);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找Report\坯布库存列表.rmf'), '提示', 0);
  end;
end;

end.

