unit U_TradeClothTotalPHList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Vcl.Printers, 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, cxPC,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxDateRanges,
  dxBarBuiltInMenu, U_BaseList, Clipbrd, dxScrollbarAnnotations, FrameDateSel,
  cxContainer, cxMaskEdit, cxProgressBar, cxButtons, dxSkinsCore, U_BaseHelp,
  dxSkinsDefaultPainters;

type
  TfrmTradeClothTotalPHList = class(TfrmBaseHelp)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    GPM_1: TcxGridPopupMenu;
    CDS_Main: TClientDataSet;
    TBDel: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column20: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    ToolButton2: TToolButton;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    N3: TMenuItem;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    Tv1Column16: TcxGridDBColumn;
    Tv1Column17: TcxGridDBColumn;
    Tv1Column18: TcxGridDBColumn;
    Tv1Column19: TcxGridDBColumn;
    Tv1Column20: TcxGridDBColumn;
    Tv1Column21: TcxGridDBColumn;
    Tv1Column22: TcxGridDBColumn;
    Tv1Column24: TcxGridDBColumn;
    Tv1Column25: TcxGridDBColumn;
    Tv1Column26: TcxGridDBColumn;
    Tv1Column27: TcxGridDBColumn;
    Tv1Column23: TcxGridDBColumn;
    Tv1Column28: TcxGridDBColumn;
    Tv1Column29: TcxGridDBColumn;
    Tv1Column30: TcxGridDBColumn;
    Tv1Column31: TcxGridDBColumn;
    Tv1Column32: TcxGridDBColumn;
    Tv1Column33: TcxGridDBColumn;
    Tv1Column34: TcxGridDBColumn;
    ADOQueryPrint: TADOQuery;
    Tv1Column35: TcxGridDBColumn;
    ToolButton4: TToolButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure SPNameChange(Sender: TObject);
    procedure BCIOIDChange(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure C_specChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure BCIOIDPropertiesChange(Sender: TObject);
    procedure BCIONOKeyPress(Sender: TObject; var Key: Char);
    procedure IOTypePropertiesChange(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
  private
    canshu1, canshu2, canshu4: string;
    procedure InitGrid();
    { Private declarations }
  public
    FBCIONO: string;
    { Public declarations }
  end;

var
  frmTradeClothTotalPHList: TfrmTradeClothTotalPHList;

implementation

uses
  U_DataLink, U_RTFun, U_AttachmentUpload, U_LabelPrint, U_printPdf;

{$R *.dfm}

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

procedure TfrmTradeClothTotalPHList.FormCreate(Sender: TObject);
begin
  inherited;
  canshu1 := Trim(DParameters1);
  canshu2 := Trim(DParameters2);
end;

procedure TfrmTradeClothTotalPHList.InitGrid();
var
  SqlStr: string;
begin

  SqlStr := ' select A.* ,Pieceint=CEILING(A.Piece)';
  SqlStr := SqlStr + ' from BS_Cloth_IO A';
  SqlStr := SqlStr + ' where A.BCIONO =''' + Trim(FBCIONO) + '''';

  InitCDSData(ADOQueryMain, CDS_Main, Tv1, SqlStr, '', 'BCIOID');
end;

procedure TfrmTradeClothTotalPHList.IOTypePropertiesChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmTradeClothTotalPHList.TBRafreshClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  InitGrid();
end;

procedure TfrmTradeClothTotalPHList.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid(trim(Self.Caption), Tv1, '贸易汇总仓库');
end;

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

  MBCIOID := Trim(SelCDSKey(self.CDS_Main, ['BCIOID'])[0]);

  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'TradeFinishClothOutPrt1';
      FFiltration1 := MBCIOID;
      if ShowModal = 1 then
      begin

        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('update BS_Cloth_IO         ');
          sql.Add(' set PRTer=' + quotedstr(DName));
          sql.Add(', PRTCount=isnull(PRTCount,0)+1');
          sql.Add(', PRTDate=getdate()');
          sql.Add(' where EXISTS (select SP.RTValue from [dbo].[F_Tool_SplitString](' + quotedstr(MBCIOID) + ','','') SP where SP.RTValue=BS_Cloth_IO.BCIOID )');
          ExecSQL;
        end;
        Self.InitGrid();
      end;
    end;
  finally
    frmLabelPrint.Free;
  end;
end;

procedure TfrmTradeClothTotalPHList.ToolButton4Click(Sender: TObject);
begin
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'TradeLabVolume1';
      FFiltration1 := Trim(SelCDSKey(self.CDS_Main, ['BCIOID'])[0]);
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmLabelPrint.Free;
  end;
end;

procedure TfrmTradeClothTotalPHList.BCIOIDPropertiesChange(Sender: TObject);
begin
  if Length(Trim(TcxTextEdit(Sender).Text)) < 4 then
    Exit;
  TBFind.Click;
end;

procedure TfrmTradeClothTotalPHList.BCIONOKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
    InitGrid();
end;

procedure TfrmTradeClothTotalPHList.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmTradeClothTotalPHList.FormShow(Sender: TObject);
begin
  inherited;

  ReadCxGrid(trim(Self.Caption), Tv1, '贸易汇总仓库');
  InitGrid();
end;

procedure TfrmTradeClothTotalPHList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  TcxGridToExcel('出库列表', cxGrid1);
end;

procedure TfrmTradeClothTotalPHList.SPNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmTradeClothTotalPHList.BCIOIDChange(Sender: TObject);
begin

  TBFind.Click;
end;

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

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

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

procedure TfrmTradeClothTotalPHList.TBDelClick(Sender: TObject);
var
  MBCIOID, MBCIONO: string;
begin
  if CDS_Main.IsEmpty then
    Exit;

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

  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  MBCIOID := SelCDSKey(CDS_Main, ['BCIOID'])[0];
  MBCIONO := SelCDSKey(CDS_Main, ['BCIONO'])[0];
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('exec P_Fin_Flow_Judge ');
    Sql.Add(' @FFIDS=' + quotedstr(MBCIONO));
    Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
    Sql.Add(',@DName=' + quotedstr(Trim(DName)));
    Open;
  end;
  if ADOQueryTemp.FieldByName('intReturn').AsInteger = -1 then
  begin
    Application.MessageBox(PChar(ADOQueryTemp.fieldbyname('ShowMsg').AsString), '提示', 0);
    exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;
      Sql.Add('exec P_Trade_BSCloth_Judge ');
      Sql.Add(' @BCIOIDS=' + quotedstr(MBCIOID));
      Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
      Sql.Add(',@DName=' + quotedstr(Trim(DName)));
      Open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));

    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;
      sql.Add('insert into Finance_Need_Up(UType,UDataId,UOperation,UOperator,UModule,UDetails) ');
      sql.Add('values(''贸易布坯出库''    ');
      sql.Add(',' + quotedstr(MBCIONO));
      sql.Add(',''删除''   ');
      sql.Add(',' + quotedstr(DName));
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(MBCIOID));
      sql.Add(')  ');

      Sql.Add('exec P_BS_Cloth_Out_Del ');
      Sql.Add(' @BCIOIDS=' + quotedstr(MBCIOID));
      Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
      Sql.Add(',@DName=' + quotedstr(Trim(DName)));
      Open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid();
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    InitGrid();
  end;

end;

procedure TfrmTradeClothTotalPHList.C_specChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmTradeClothTotalPHList.cxTabControl1Change(Sender: TObject);
begin
  InitGrid();
end;

end.

