unit U_TatWBOutputList1;

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
  TfrmTatWBOutputList1 = 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;
    Label3: TLabel;
    WBCID: TcxTextEdit;
    CDS_Main: TClientDataSet;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    MovePanel2: TMovePanel;
    RMGridReport1: TRMGridReport;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    TBdel: TToolButton;
    ToolButton1: TToolButton;
    Tv1Column14: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Label4: TLabel;
    GlideName: TcxTextEdit;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    Panel2: TPanel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    v1Column18: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    v1SPName: TcxGridDBColumn;
    v1SPSpec: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    ADO_2: TADOQuery;
    CDS_2: TClientDataSet;
    GPM_2: TcxGridPopupMenu;
    DS_2: TDataSource;
    GroupBox1: TGroupBox;
    ScrollBox1: TScrollBox;
    adoqueryPicture: TADOQuery;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column23: TcxGridDBColumn;
    Tv1Column24: TcxGridDBColumn;
    Tv1Column25: TcxGridDBColumn;
    Tv1Column26: TcxGridDBColumn;
    Tv1Column27: TcxGridDBColumn;
    Tv1Column28: TcxGridDBColumn;
    Tv1Column29: TcxGridDBColumn;
    Tv1Column30: TcxGridDBColumn;
    Tv1Column31: TcxGridDBColumn;
    Tv1Column32: TcxGridDBColumn;
    Tv1Column33: TcxGridDBColumn;
    Tv1Column34: TcxGridDBColumn;
    Tv1Column35: TcxGridDBColumn;
    Tv1Column36: TcxGridDBColumn;
    frmFrameDateSel1: TfrmFrameDateSel;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    Tv1Column16: TcxGridDBColumn;
    ToolButton2: TToolButton;
    Label1: TLabel;
    Price: TcxTextEdit;
    Button1: TButton;
    cxTabControl1: TcxTabControl;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    TV2Column1: TcxGridDBColumn;
    TV2Column2: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    TV2Column4: TcxGridDBColumn;
    Label2: TLabel;
    WB_Name: 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 Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure Button1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
  private
    FLeft, FTop: Integer;
    procedure InitGrid();
    procedure setstatus();
    procedure initZX();
    procedure InitImages();
    { Private declarations }
  public
    fsyrName, canshu1, canshu2: string;
    { Public declarations }
  end;

var
  frmTatWBOutputList1: TfrmTatWBOutputList1;
  Mach: array of TfrmSLTGDTP;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_OutPutHelp;

{$R *.dfm}
procedure TfrmTatWBOutputList1.InitImages();
var
  i, j: integer;
  jpg: TJpegImage;
  myStream: TADOBlobStream;
begin
  j := length(Mach);
  if j > 0 then
  begin
    for i := 0 to j - 1 do
    begin
      Mach[i].free;
    end;
  end;
  SetLength(Mach, 0);
  if CDS_Main.IsEmpty then
    exit;
  try
    with adoqueryPicture do
    begin
      close;
      sql.Clear;
      sql.Add('select * from TP_File A');
      sql.Add('where isnull(WBID,'''')<>'''' and WBID=' + quotedstr(Trim(CDS_Main.fieldbyname('TWIOID').AsString)));
      open;
    end;
    j := adoqueryPicture.RecordCount;
    if j < 1 then
      exit;
    adoqueryPicture.DisableControls;
    adoqueryPicture.First;
    SetLength(Mach, j);
    jpg := TJpegImage.Create();
    for i := 0 to j - 1 do
    begin
      if triM(adoqueryPicture.fieldbyname('FilesOther').AsString) <> '' then
      begin
        myStream := tadoblobstream.Create(tblobfield(adoqueryPicture.fieldbyname('FilesOther')), bmread);
        jpg.LoadFromStream(myStream);
        Mach[i] := TfrmSltGDTP.Create(Self);
        Mach[i].Name := trim(adoqueryPicture.fieldbyname('TFID').AsString);
        Mach[i].Parent := ScrollBox1;
        Mach[i].Left := 0 + i * 165;
        Mach[i].Init(adoqueryPicture.fieldbyname('TFID').AsString, adoqueryPicture.fieldbyname('FileName').AsString, jpg, adoqueryPicture.fieldbyname('URL').AsString, Trim(CDS_Main.fieldbyname('GOID').AsString));
      end;
      adoqueryPicture.Next;
    end;
    adoqueryPicture.EnableControls;
  finally
    jpg.free;
    application.ProcessMessages;
  end;
end;

procedure TfrmTatWBOutputList1.setstatus();
begin
  ToolButton3.Enabled := False;
  ToolButton4.Enabled := False;
  TBdel.Enabled := False;

  Label1.Visible := false;
  Price.Visible := false;
  Button1.Visible := false;

  case cxTabControl1.TabIndex of
    0:
      begin
        ToolButton3.Enabled := true;
        TBdel.Enabled := true;

        Label1.Visible := true;
        Price.Visible := true;
        Button1.Visible := true;
      end;
    1:
      begin
        ToolButton4.Enabled := true;
      end;
    2:
      begin

      end;
  end;

end;

procedure TfrmTatWBOutputList1.initZX();
begin
  with ADO_2 do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* from Tat_WB_Formula A inner join Tat_WB_Spec B on A.WBSID=B.WBSID ');
    sql.Add(' where WB_Code=''' + Trim(CDS_Main.fieldbyname('WB_Code').AsString) + '''');
//    ShowMessage(SQL.Text);
    Open;
  end;
  SCreateCDS(ADO_2, CDS_2);
  SInitCDSData(ADO_2, CDS_2);
end;

procedure TfrmTatWBOutputList1.FormDestroy(Sender: TObject);
begin
  inherited;
  frmTatWBOutputList1 := nil;
end;

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

procedure TfrmTatWBOutputList1.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 TfrmTatWBOutputList1.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.*,B.* ';
  SqlStr := SqlStr + ' ,Money=round(( isnull(Price,0)* isnull(Qty,0) + isnull(Subsidy,0) - isnull(Deduct,0)  ),2) ';
  SqlStr := SqlStr + ' from Tat_WB_IO A inner join Tat_WB_Card B on A.WBCID=B.WBCID ';
  SqlStr := SqlStr + ' where A.IOTime>=''' + Trim(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.BegDate.Date)) + '''';
  SqlStr := SqlStr + ' and A.IOTime<''' + Trim(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.enddate.Date + 1)) + '''';
  SqlStr := SqlStr + ' and isnull(StkName,'''')=''经轴'' and  IOType in (''外厂整经'',''整经采购'') ';
  SqlStr := SqlStr + ' and IOFlag=''入库''  ';

  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), 'TWIOID');
  initZX();
  InitImages();
end;

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

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

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

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

procedure TfrmTatWBOutputList1.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 TfrmTatWBOutputList1.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 TfrmTatWBOutputList1.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 TfrmTatWBOutputList1.Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  initZX();
  InitImages();
end;

procedure TfrmTatWBOutputList1.Button1Click(Sender: TObject);
var
  MTWIOID: string;
begin
  if CDS_Main.IsEmpty then
    exit;

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

  if StrToFloatdef(Price.text, 0) = 0 then
  begin
    application.MessageBox('单价输入不正确', '提示');
    Exit;
  end;

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

  MTWIOID := Copy(MTWIOID, 1, Length(MTWIOID) - 1);

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add(' update Tat_WB_IO set Price =' + Trim(Price.text) + '');
    sql.Add(' where TWIOID in (' + trim(MTWIOID) + ')');

    sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
    sql.Add(' ' + quotedstr(trim(DName)));
    sql.Add(',getdate() ');
    sql.Add(',' + quotedstr(trim(self.Caption)));
    sql.Add(',' + quotedstr(trim('单价修改')));
    sql.Add(',' + quotedstr(trim('TWIOID：' + trim(MTWIOID) + '修改为' + trim(Price.text))));
    sql.Add(',' + quotedstr(trim('成功')));
    sql.Add(')');

    ExecSQL;
  end;
  initgrid();
end;

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

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

procedure TfrmTatWBOutputList1.TBCloseClick(Sender: TObject);
begin

  Close;
end;

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

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

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

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

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

procedure TfrmTatWBOutputList1.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.

