unit U_TradeClothTotalOutSel;

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_Basehelp, Clipbrd, dxScrollbarAnnotations, FrameDateSel,
  cxContainer, cxMaskEdit, cxProgressBar, cxButtons, U_BaseList,
  System.ImageList, Vcl.ImgList;

type
  TfrmTradeClothTotalOutSel = class(TfrmBasehelp)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    GPM_1: TcxGridPopupMenu;
    CDS_Main: TClientDataSet;
    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;
    Label3: TLabel;
    Label5: TLabel;
    Label8: TLabel;
    Label12: TLabel;
    C_NAME: TcxTextEdit;
    BCIONO: TcxTextEdit;
    C_spec: TcxTextEdit;
    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;
    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;
    Label4: TLabel;
    Label9: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    FromCoName: TcxTextEdit;
    ToCoName: TcxTextEdit;
    ToOrdNo: TcxTextEdit;
    FromOrdNo: TcxTextEdit;
    Tv1Column18: TcxGridDBColumn;
    Tv1Column19: TcxGridDBColumn;
    Tv1Column20: TcxGridDBColumn;
    Tv1Column21: TcxGridDBColumn;
    frmFrameDateSel1: TfrmFrameDateSel;
    IOType: TcxComboBox;
    Tv1Column22: TcxGridDBColumn;
    Tv1Column24: TcxGridDBColumn;
    Tv1Column25: TcxGridDBColumn;
    Tv1Column26: TcxGridDBColumn;
    Tv1Column27: TcxGridDBColumn;
    Tv1Column23: TcxGridDBColumn;
    Tv1Column30: TcxGridDBColumn;
    Tv1Column31: TcxGridDBColumn;
    Tv1Column32: TcxGridDBColumn;
    Tv1Column33: TcxGridDBColumn;
    Tv1Column34: TcxGridDBColumn;
    ADOQueryPrint: TADOQuery;
    Tv1Column35: TcxGridDBColumn;
    btnOK: TToolButton;
    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 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 N3Click(Sender: TObject);
    procedure BCIOIDPropertiesChange(Sender: TObject);
    procedure btnOKClick(Sender: TObject);
  private
    canshu1, canshu2: string;
    procedure InitGrid();
    { Private declarations }
  public
    FFSPID, FstkName, FC_Degree, FC_Pack: string;
    { Public declarations }
  end;

var
  frmTradeClothTotalOutSel: TfrmTradeClothTotalOutSel;

implementation

uses
  U_DataLink, U_RTFun;

{$R *.dfm}

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

procedure TfrmTradeClothTotalOutSel.FormCreate(Sender: TObject);
begin
  inherited;
  canshu1 := Trim(DParameters1);
  canshu2 := Trim(DParameters2);
  frmFrameDateSel1.EndDate.Date := SGetServerDate(ADOQueryTemp);
  frmFrameDateSel1.BegDate.Date := frmFrameDateSel1.EndDate.Date;
end;

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

  SqlStr := ' select A.* ';
  SqlStr := SqlStr + ',FJFlag=CAST((CASE WHEN (SELECT COUNT(X.FileName) FROM FJ_File X where X.TFType=''贸易汇总布'' and X.WBID=A.BCIOID)>0 THEN 1 ELSE 0 END) AS BIT)     ';
  SqlStr := SqlStr + ' from BS_Cloth_IO A    ';
  SqlStr := SqlStr + ' where A.IOFlag=''出库''   ';
  SqlStr := SqlStr + ' and 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,'''')=''贸易布匹''';

  SqlStr := SqlStr + ' and IOType = ''检验出库''  ';

  SqlStr := SqlStr + ' order by IOTime desc';

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

end;

procedure TfrmTradeClothTotalOutSel.TBRafreshClick(Sender: TObject);
begin
  frmFrameDateSel1.BegDate.SetFocus;
  InitGrid();
end;

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

procedure TfrmTradeClothTotalOutSel.BCIOIDPropertiesChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmTradeClothTotalOutSel.btnOKClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;
  ModalResult := 1;
end;

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

procedure TfrmTradeClothTotalOutSel.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmTradeClothTotalOutSel.FormShow(Sender: TObject);
var
  mSql: string;
begin
  inherited;
  mSql := 'select distinct name=A.IOType from BS_Cloth_IO A where IOFlag=''出库'' and isnull(STKNAME,'''')=''' + Trim(FSTKNAME) + ''' ';
  SInitTcxComBoxBySql(ADOQueryTemp, IOType, false, mSql);
  ReadCxGrid(trim(Self.Caption), Tv1, '贸易汇总仓库');
  InitGrid();
end;

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

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

procedure TfrmTradeClothTotalOutSel.BCIOIDChange(Sender: TObject);
begin
  if Length(Trim(BCIOno.Text)) < 4 then
  begin
    if Trim(BCIOno.Text) <> '' then
      Exit;
  end;
  TBFind.Click;
end;

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

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

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

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

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

end.

