unit U_TatWBNeedList;

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, MovePanel, cxButtonEdit, cxCalendar, RM_System, RM_Common,
  RM_Class, RM_GridReport, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxDateRanges, dxBarBuiltInMenu, U_BaseList, cxCheckBox, Vcl.Menus,
  dxScrollbarAnnotations, FrameDateTimeSel, cxContainer, cxTextEdit, U_SLTGDTP,
  cxMemo, Vcl.Imaging.jpeg, FrameDateSel, cxPC, dxSkinsCore,
  dxSkinsDefaultPainters, cxProgressBar, dxSkinWXI;

type
  TfrmTatWBNeedList = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DS_1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    CDS_Main: TClientDataSet;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    MovePanel2: TMovePanel;
    RMGridReport1: TRMGridReport;
    ToolButton1: TToolButton;
    Tv1Column14: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    CDS_2: TClientDataSet;
    GPM_2: TcxGridPopupMenu;
    DS_2: TDataSource;
    Tv1Column26: TcxGridDBColumn;
    Tv1Column27: TcxGridDBColumn;
    Tv1Column28: TcxGridDBColumn;
    frmFrameDateSel1: TfrmFrameDateSel;
    Label2: TLabel;
    WB_Name: TcxTextEdit;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    TbLook: TToolButton;
    Label1: TLabel;
    WB_Code: TcxTextEdit;
    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 pcidChange(Sender: TObject);
    procedure TBdelClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure pcidPropertiesChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure TbLookClick(Sender: TObject);
  private
    FLeft, FTop: Integer;
    procedure InitGrid();
    procedure setstatus();
    { Private declarations }
  public
    fsyrName, canshu1, canshu2: string;
    { Public declarations }
  end;

var
  frmTatWBNeedList: TfrmTatWBNeedList;
  Mach: array of TfrmSLTGDTP;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_OutPutHelp, U_WBNeedLook;

{$R *.dfm}

procedure TfrmTatWBNeedList.setstatus();
begin

  Label1.Visible := false;

end;

procedure TfrmTatWBNeedList.FormDestroy(Sender: TObject);
begin
  inherited;
  frmTatWBNeedList := nil;
end;

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

procedure TfrmTatWBNeedList.FormCreate(Sender: TObject);
begin
  inherited;

  frmFrameDateSel1.EndDate.Date := SGetServerDateTime(ADOQueryTemp);
  frmFrameDateSel1.BegDate.Date := frmFrameDateSel1.EndDate.Date - 30;
  fsyrName := trim(DdataBase);
  canshu1 := trim(DParameters1);
end;

procedure TfrmTatWBNeedList.InitGrid();
var
  Msql, mbegdate, menddate: string;
  SqlStr: string;
begin

//  mbegdate := FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.begdate.Date) + ' ' + FormatDateTime('HH:nn', frmFrameDateSel1.begTime.Time);
//  menddate := FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.enddate.Date) + ' ' + FormatDateTime('HH:nn', frmFrameDateSel1.endTime.Time);

//  SqlStr := SqlStr + 'and A.GOTime<''' + trim(menddate) + ''' ';

//  SqlStr := ' select A.*,wapqty=(OrdQty-ISNULL(KCQTY,0)-ISNULL(KKQTY,0)) from V_JZXQTJ A';
  SqlStr := ' select A.*,wapqty=(OrdQty-ISNULL(KKQTY,0)) from V_JZXQTJ A';

//  case cxTabControl1.TabIndex of
//    0:
//      begin
//        SqlStr := SqlStr + ' and isnull(ISDZ,0) = 0';
//      end;
//    1:
//      begin
//        SqlStr := SqlStr + ' and isnull(ISDZ,0) = 1';
//      end;
//    2:
//      begin
//
//      end;
//  end;

//  SqlStr := SqlStr + ' order by IOTime desc';
//  ShowMessage(SqlStr);

  InitCDSData(ADOQueryMain, CDS_Main, Tv1, SqlStr, SGetFilters(Panel1, 1, 2), '');
end;

procedure TfrmTatWBNeedList.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, true);
end;

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

procedure TfrmTatWBNeedList.TBRafreshClick(Sender: TObject);
begin
//  MovePanel2.Visible := True;
//  MovePanel2.Refresh;
  ToolBar1.SetFocus;
  InitGrid();
//  MovePanel2.Visible := False;
end;

procedure TfrmTatWBNeedList.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid(self.Caption + tv1.Name, Tv1, '成品仓库');
end;

procedure TfrmTatWBNeedList.ToolButton2Click(Sender: TObject);
var
  MGOIDS: string;
begin
  if CDS_Main.IsEmpty then
    exit;

  if not CDS_Main.Locate('SSel', True, []) then
  begin
    application.MessageBox('请选择数据', '提示');
    Exit;
  end;

  with CDS_Main do
  begin
    DisableControls;
    while CDS_Main.Locate('SSel', True, []) do
    begin
      MGOIDS := MGOIDS + Trim(CDS_Main.fieldbyname('GOID').AsString) + ',';
      CDS_Main.Delete;
    end;
    EnableControls;
  end;

end;

procedure TfrmTatWBNeedList.ToolButton3Click(Sender: TObject);
var
  WSql: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  WSql := SelCDSKey(CDS_Main, ['TWIOID'])[0];
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;
      Sql.Add('exec P_Tat_WB_IO_DZ ');
      Sql.Add(' @GOIDS=' + quotedstr(Trim(WSql)));
      Sql.Add(',@DCode=' + Quotedstr(DCode));
      Sql.Add(',@DName=' + Quotedstr(DName));
      Open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
    begin
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    end;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid();
    application.MessageBox('对账成功！', '提示信息');
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure TfrmTatWBNeedList.ToolButton4Click(Sender: TObject);
var
  WSql: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  WSql := SelCDSKey(CDS_Main, ['TWIOID'])[0];
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;
      Sql.Add('exec P_Tat_WB_IO_CXDZ ');
      Sql.Add(' @GOIDS=' + quotedstr(Trim(WSql)));
      Sql.Add(',@DCode=' + Quotedstr(DCode));
      Sql.Add(',@DName=' + Quotedstr(DName));
      Open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
    begin
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    end;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid();
    application.MessageBox('撤销对账成功！', '提示信息');
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

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

procedure TfrmTatWBNeedList.cxTabControl1Change(Sender: TObject);
begin
  setstatus();
  initgrid();
end;

procedure TfrmTatWBNeedList.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmTatWBNeedList.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(self.Caption + tv1.Name, Tv1, '成品仓库');
  canshu1 := self.fParameters1;
  canshu2 := self.fParameters2;
  setstatus();
  initgrid();
end;

procedure TfrmTatWBNeedList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  TcxGridToExcel(self.Caption, cxGrid1);
end;

procedure TfrmTatWBNeedList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_Main);
    SInitCDSData(ADOQueryMain, CDS_Main);
  end;
end;

procedure TfrmTatWBNeedList.TbLookClick(Sender: TObject);
begin
  inherited;
  try
    frmWBNeedLook := TfrmWBNeedLook.Create(Application);
    with frmWBNeedLook do
    begin
      FWB_Code := SELF.CDS_Main.FieldByName('WB_Code').ASSTRING;
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmWBNeedLook.Free;
  end;
end;

procedure TfrmTatWBNeedList.pcidChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmTatWBNeedList.pcidPropertiesChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmTatWBNeedList.TBdelClick(Sender: TObject);
var
  WSql: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  WSql := SelCDSKey(CDS_Main, ['GOID'])[0];
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;
      Sql.Add('exec P_Tat_Plan_OutPut_Del ');
      Sql.Add(' @GOIDS=' + quotedstr(Trim(WSql)));
      Sql.Add(',@DCode=' + Quotedstr(DCode));
      Sql.Add(',@DName=' + Quotedstr(DName));
      Open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
    begin
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    end;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid();
    application.MessageBox('删除成功！', '提示信息');
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

end.

