﻿unit U_BillPrintList;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  Vcl.ComCtrls, Vcl.ToolWin, cxGraphics, cxControls, cxLookAndFeels,
  cxLookAndFeelPainters, cxStyles, cxCustomData, cxFilter, cxData, cxDataStorage,
  cxEdit, cxNavigator, dxDateRanges, dxScrollbarAnnotations, Data.DB, cxDBData,
  cxTextEdit, cxCheckBox, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxGridCustomView, U_BaseList, cxGrid,
  Data.Win.ADODB, cxDropDownEdit, Vcl.StdCtrls, Vcl.ExtCtrls, cxContainer,
  cxGroupBox, cxRadioGroup, cxProgressBar;

type
  TfrmBillPrintList = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TgroupAdd: TToolButton;
    TgroupMdy: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    tv1direction: TcxGridDBColumn;
    tv1billType: TcxGridDBColumn;
    tv1procedureName: TcxGridDBColumn;
    tv1pageRow: TcxGridDBColumn;
    Tv1PageCol: TcxGridDBColumn;
    tv1billLayoutPageField: TcxGridDBColumn;
    Tv1note: TcxGridDBColumn;
    tv1isValid: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    ToolButton1: TToolButton;
    ADOQueryMain: TADOQuery;
    DataSource1: TDataSource;
    ToolButton2: TToolButton;
    Tv1billName: TcxGridDBColumn;
    Tgetresult: TToolButton;
    tv2: TcxGridDBTableView;
    cxGrid2Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    DataSource2: TDataSource;
    ADOQueryResult: TADOQuery;
    Tv1procedureName2: TcxGridDBColumn;
    ToolButton3: TToolButton;
    Tmdgs: TToolButton;
    RzGroupBox1: TcxGroupBox;
    cxRadioGroup1: TcxRadioGroup;
    ToolButton4: TToolButton;
    procedure TBCloseClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure TgroupAddClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TgetresultClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure TmdgsClick(Sender: TObject);
    procedure RadioGroup1Click(Sender: TObject);
    procedure cxRadioGroup1Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
  private
    procedure DoQuery();
  public
    fuseSystem: string;
    FLMType: string;
    FFiltration1, FFiltration2, FFiltration3: string;
  end;

var
  frmBillPrintList: TfrmBillPrintList;

implementation

uses
  U_DataLink, U_RTfun, U_globalVar, U_MdPrint;
{$R *.dfm}

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

procedure TfrmBillPrintList.FormCreate(Sender: TObject);
begin
  inherited;
  cxGrid1.Align := alClient;
  fuseSystem := fParameters1;
end;

procedure TfrmBillPrintList.FormDestroy(Sender: TObject);
begin
  inherited;
  frmBillPrintList := nil;
end;

procedure TfrmBillPrintList.FormShow(Sender: TObject);
begin
  inherited;
  DoQuery();
  readCxGrid('码单格式管理列表T1', Tv1, gDllFileName);
end;

procedure TfrmBillPrintList.RadioGroup1Click(Sender: TObject);
begin
  TBRafresh.Click;

end;

procedure TfrmBillPrintList.TBCloseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmBillPrintList.TBDelClick(Sender: TObject);
begin
  inherited;
  ADOQueryMain.Delete;
end;

procedure TfrmBillPrintList.TBRafreshClick(Sender: TObject);
begin
  inherited;
  DoQuery();
end;

procedure TfrmBillPrintList.TgetresultClick(Sender: TObject);
begin
  if trim(ADOQueryMain.FieldByName('procedureName').AsString) = '' then
  begin
    Application.MessageBox('明细存储过程名称不能为空!', '提示信息', 0);
    exit;
  end;
  try
    with ADOQueryBaseTemp do
    begin
      close;
      Filtered := False;
      Sql.Clear;
      sql.Add('select max(ciid) as ciid from Trade_Cloth_Inspect');
     // if  Pos('���',trim(ADOQueryMain.FieldByName('billType').AsString),1)>=0 then
     //  sql.Add('where ioflag='+QuotedStr('���') )
     // else
     // sql.Add('where ioflag='+QuotedStr(' ����'));
      open;
    end;
    TV2.BeginUpdate();
    ADOQueryResult.DisableControls;
    with ADOQueryResult do
    begin
      Close;
      Filtered := False;
      SQL.Clear;
      sql.Add('exec ' + trim(ADOQueryMain.FieldByName('procedureName').AsString));
      if not ADOQueryBaseTemp.IsEmpty then
        sql.Add('@Filtration=' + quotedstr(FFiltration1)) //    Trim(ADOQueryBaseTemp.FieldByName('ciid').AsString)
      else
        sql.Add('@Filtration=' + quotedstr('92405270001'));
      Open;
    end;
    TV2.ClearItems; //�������
    (TV2.DataController as IcxCustomGridDataController).DeleteAllItems;  //ɾ��������
    (TV2.DataController as IcxCustomGridDataController).CreateAllItems(false); //��������Դ�е�������

  finally
    ADOQueryResult.EnableControls;
    TV2.EndUpdate;
    TV2.BeginBestFitUpdate;
    TV2.ApplyBestFit; //���п�����Ӧ .BestFitMaxWidth;
    TV2.EndBestFitUpdate;
  end;
end;

procedure TfrmBillPrintList.TgroupAddClick(Sender: TObject);
begin
  inherited;
  ADOQueryMain.Append;
  ADOQueryMain.FieldByName('billType').Value := '����뵥';
  ADOQueryMain.FieldByName('billName').Value := '';
  ADOQueryMain.FieldByName('direction').Value := '��';
  ADOQueryMain.FieldByName('procedureName').Value := '';
  ADOQueryMain.FieldByName('pageRow').Value := 10;
  ADOQueryMain.FieldByName('PageCol').Value := 10;
  ADOQueryMain.FieldByName('useSystem').Value := fuseSystem;

  ADOQueryMain.FieldByName('valid').Value := 1;
end;

procedure TfrmBillPrintList.TmdgsClick(Sender: TObject);
begin
  inherited;
  with ADOQueryBaseTemp do
  begin
    close;
    Filtered := False;
    Sql.Clear;
    sql.Add('select max(ciid) as ciid from Trade_Cloth_Inspect');
    open;
  end;
  try
    frmMdPrint := TfrmMdPrint.Create(Application);
    with frmMdPrint do
    begin
      FLMType := '入库码单';
      FFiltration1 := Trim(self.ADOQueryBaseTemp.FieldByName('ciid').AsString);
      FFiltration2 := Trim(self.ADOQueryBaseTemp.FieldByName('ciid').AsString);
      if ShowModal = 1 then
      begin
//        Self.InitGrid();
      end;
    end;
  finally
    frmMdPrint.Free;
  end;
end;

procedure TfrmBillPrintList.ToolButton1Click(Sender: TObject);
begin
  inherited;
  try
    if ADOQueryMain.Locate('billName', '', []) then
    begin
      Application.MessageBox('码单名称不能为空!', '提示信息', 0);
      exit;
    end;
    if ADOQueryMain.Locate('procedureName', '', []) then
    begin
      Application.MessageBox('存储过程名称不能为空!', '提示信息', 0);
      exit;
    end;

    ADOQueryMain.UpdateBatch();
    Application.MessageBox('保存成功!', '提示信息', 0);
  except
    Application.MessageBox('保存失败!', '警告信息', 0);
  end;
end;

procedure TfrmBillPrintList.ToolButton2Click(Sender: TObject);
begin
  inherited;
  WriteCxGrid('码单格式管理列表T1', Tv1, gDllFileName);
end;

procedure TfrmBillPrintList.ToolButton3Click(Sender: TObject);
begin
  if trim(ADOQueryMain.FieldByName('procedureName2').AsString) = '' then
  begin
    Application.MessageBox('汇总存储过程名称不能为空!', '提示信息', 0);
    exit;
  end;
  try
    with ADOQueryBaseTemp do
    begin
      close;
      Filtered := False;
      Sql.Clear;
      sql.Add('select max(ciid) as ciid from Trade_Cloth_Inspect');
      //if  Pos('���',trim(ADOQueryMain.FieldByName('billType').AsString),1)>=0 then
      // sql.Add('where ioflag='+QuotedStr('���') )
      //else
      //sql.Add('where ioflag='+QuotedStr(' ����'));
      open;
    end;
    TV2.BeginUpdate();
    ADOQueryResult.DisableControls;
    with ADOQueryResult do
    begin
      Close;
      Filtered := False;
      SQL.Clear;
      sql.Add('exec ' + trim(ADOQueryMain.FieldByName('procedureName2').AsString));
      if not ADOQueryBaseTemp.IsEmpty then
        sql.Add('@Filtration=' + quotedstr(FFiltration2))
      else
        sql.Add('@Filtration=' + quotedstr('92405270001'));
      Open;
    end;
    TV2.ClearItems; //�������
    (TV2.DataController as IcxCustomGridDataController).DeleteAllItems;  //ɾ��������
    (TV2.DataController as IcxCustomGridDataController).CreateAllItems(false); //��������Դ�е�������

  finally
    ADOQueryResult.EnableControls;
    TV2.EndUpdate;
    TV2.BeginBestFitUpdate;
    TV2.ApplyBestFit; //���п�����Ӧ .BestFitMaxWidth;
    TV2.EndBestFitUpdate;
  end;
end;

procedure TfrmBillPrintList.ToolButton4Click(Sender: TObject);
begin

  TcxGridToExcel('sql字段', cxgrid2);
end;

procedure TfrmBillPrintList.cxRadioGroup1Click(Sender: TObject);
begin
  TBRafresh.click;

end;

procedure TfrmBillPrintList.DoQuery();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add('select * from SD_BillPrint  ');
      if cxRadioGroup1.ItemIndex = 0 then
        sql.Add('where valid=1 ')
      else if cxRadioGroup1.ItemIndex = 1 then
        sql.Add('where valid=0')
      else
        sql.Add('where 1=1');
     // if FLMType<>'' then
      sql.Add('and billtype=' + QuotedStr(TRIM(FLMType)));
      sql.Add('order by recid');
      Open;
    end;
  finally
    ADOQueryMain.EnableControls;
  end;
end;

end.

