unit U_GetPGJBOneTwoInList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, cxCalendar, cxButtonEdit, cxDropDownEdit,
  DBClient, cxSplitter;

type
  TfrmGetPGJBOneTwoInList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Panel1: TPanel;
    ToolButton1: TToolButton;
    cxSplitter1: TcxSplitter;
    DataSource2: TDataSource;
    ADOQuery2: TADOQuery;
    ClientDataSet1: TClientDataSet;
    ClientDataSet2: TClientDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Label3: TLabel;
    Label4: TLabel;
    Label7: TLabel;
    YCLName: TEdit;
    YCLSpec: TEdit;
    GYSName: TEdit;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1P_ChnName: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1ShortName: TcxGridDBColumn;
    v1Quantity: TcxGridDBColumn;
    v1UnitName: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxGrid3: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    v1Column1: TcxGridDBColumn;
    V2Column1: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure PlanNoChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure P_ChnNameChange(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure TV2CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure YCLNameChange(Sender: TObject);
  private
    procedure InitGrid();
    procedure InitGrid10();
    procedure InitForm();
    function  DelData():Boolean;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmGetPGJBOneTwoInList: TfrmGetPGJBOneTwoInList;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp;

{$R *.dfm}

procedure TfrmGetPGJBOneTwoInList.FormDestroy(Sender: TObject);
begin
  frmGetPGJBOneTwoInList:=nil;
end;

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

procedure TfrmGetPGJBOneTwoInList.FormCreate(Sender: TObject);
begin
  cxgrid1.Align:=alClient;
end;

procedure TfrmGetPGJBOneTwoInList.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('库存列表',Tv1,'原材料仓库');
  WriteCxGrid('库存列表选择',Tv2,'原材料仓库');
end;

procedure TfrmGetPGJBOneTwoInList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' select YCLName=(select ZdyName from KH_Zdy KK where KK.ZdyNo=A.YCLCode), ');
      sql.Add(' A.*,GYSName=(select ZdyName from KH_ZDY B where A.GYS=B.ZdyNo) ');
      sql.Add(' from CK_YCL_KC A where A.KCQty>0');
      sql.Add(' and A.CKName in(''原材料仓库'',''原材料二级库'') ');
      Open;
      SCreateCDS20(ADOQueryMain,ClientDataSet1);
      SInitCDSData20(ADOQueryMain,ClientDataSet1);
    end;
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmGetPGJBOneTwoInList.InitGrid10();
begin
  try
    ADOQuery2.DisableControls;
    with ADOQuery2 do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' select YCLName=(select ZdyName from KH_Zdy KK where KK.ZdyNo=A.YCLCode), ');
      sql.Add(' A.*,GYSName=(select ZdyName from KH_ZDY B where A.GYS=B.ZdyNo) ');
      sql.Add('from CK_YCL_KC A where 1<>1');
      Open;
      SCreateCDS20(ADOQuery2,ClientDataSet2);
      SInitCDSData20(ADOQuery2,ClientDataSet2);
    end;
  finally
    ADOQuery2.EnableControls;
  end;
end;

procedure TfrmGetPGJBOneTwoInList.InitForm();
begin
  ReadCxGrid('库存列表',Tv1,'原材料仓库');
  ReadCxGrid('库存列表选择',Tv2,'原材料仓库');
  InitGrid();
  InitGrid10();
end;

procedure TfrmGetPGJBOneTwoInList.TBFindClick(Sender: TObject);
begin
  if not ADOQueryMain.Active then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,ClientDataSet1);
  SInitCDSData20(ADOQueryMain,ClientDataSet1);
end;

function TfrmGetPGJBOneTwoInList.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

procedure TfrmGetPGJBOneTwoInList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  TcxGridToExcel('库存列表',cxGrid1);
end;

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

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

procedure TfrmGetPGJBOneTwoInList.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmGetPGJBOneTwoInList.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    ModalResult:=-1
  else
    ModalResult:=1;
end;

procedure TfrmGetPGJBOneTwoInList.P_ChnNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmGetPGJBOneTwoInList.Tv1CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
var
  i:Integer;
begin
  if ClientDataSet1.IsEmpty then exit;
  ClientDataSet2.Append;
  for i:=0 to ClientDataSet2.FieldCount-1 do
  begin
    ClientDataSet2.Fields[i].Value:=ClientDataSet1.Fields[i].Value;
  end;
  ClientDataSet2.Post;
  ClientDataSet1.Delete;
end;

procedure TfrmGetPGJBOneTwoInList.TV2CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
var
  i:Integer;
begin
  if ClientDataSet2.IsEmpty then exit;
  ClientDataSet1.Append;
  for i:=0 to ClientDataSet1.FieldCount-1 do
  begin
    ClientDataSet1.Fields[i].Value:=ClientDataSet2.Fields[i].Value;
  end;
  ClientDataSet1.Post;
  ClientDataSet2.Delete;
end;

procedure TfrmGetPGJBOneTwoInList.YCLNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

end.
