unit U_WJTJList;

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, cxCheckBox, RM_Common, RM_Class, RM_e_Xls, RM_Dataset,
  RM_System, RM_GridReport, Menus, cxCalendar, cxButtonEdit, cxTextEdit,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, cxCalc, math;

type
  TfrmWJTJList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    RMDBHZ: TRMDBDataSet;
    CDS_HZ: TClientDataSet;
    CDS_PRT: TClientDataSet;
    TBDel: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Label3: TLabel;
    Label15: TLabel;
    SPName: TEdit;
    OrderNo: TEdit;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    jgtype: TComboBox;
    Label4: TLabel;
    Tv1Column5: TcxGridDBColumn;
    Label5: TLabel;
    Edit1: TEdit;
    Tv1Column6: TcxGridDBColumn;
    Label6: TLabel;
    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 N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure SPIDKeyPress(Sender: TObject; var Key: Char);
    procedure SPSpecChange(Sender: TObject);
    procedure FactoryNameChange(Sender: TObject);
    procedure SPNameKeyPress(Sender: TObject; var Key: Char);
    procedure jgtypeChange(Sender: TObject);
    procedure Tv1TcxGridDBDataControllerTcxDataSummaryFooterSummaryItems6GetText(Sender: TcxDataSummaryItem; const AValue: Variant; AIsFooter: Boolean; var AText: string);
  private
    canshu1, canshu2, canshu3: string;
    procedure InitGrid();
    procedure js();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmWJTJList: TfrmWJTJList;

implementation

uses
  U_DataLink, U_RTFun, U_WJSJInPut;

{$R *.dfm}

procedure TfrmWJTJList.FormDestroy(Sender: TObject);
begin
  frmWJTJList := nil;
end;

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

procedure TfrmWJTJList.FormCreate(Sender: TObject);
begin
  //cxGrid1.Align:=alClient;
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime;
  canshu1 := Trim(DParameters1);
  canshu2 := Trim(DParameters2);
  canshu3 := Trim(DParameters3);
end;

procedure TfrmWJTJList.InitGrid();
var
  fwhere, Pwhere: string;
begin
  fwhere := '';
  Pwhere := SGetFilters(Panel1, 1, 2);
  if trim(Pwhere) <> '' then
    fwhere := fwhere + ' and ' + trim(Pwhere);
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('select * from (');
      sql.Add(' select A.*,jgtype=(select jgtype from jyorder_main b where b.mainid=a.ORDMainIdRK ) ');
      sql.Add(',DDBILI1=(cast( ddbili as varchar)+''%'' )');
      sql.Add(',MPRTCODE=(select MPRTCODE from jyorder_main b where b.mainid=a.ORDMainIdRK )');
      sql.Add(',PRTECOLOR=(select PRTECOLOR from jyorder_SUB C where C.SUBid=a.ORDSUBIdRK )');
      sql.Add(' from WJSJ A');
      sql.add(' where A.FILLTIME>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)) + '''');
      sql.Add(' and A.FILLTIME<=''' + Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)) + '''');
      sql.Add(')aa where 1=1');
      SQL.Add(fwhere);
//      sql.Add(' and isnull(CKName,'''')=''坯布''');
//      SQL.Add(' and CRFlag=''入库''  ');
//     ShowMessage(SQL.Text);
      Open;
      //ShowMessage(SQL.Text);
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
//  js();

end;

procedure TfrmWJTJList.js();
var
  f1, f2: Double;
begin
  if TV1.DataController.Summary.FooterSummaryValues[5] = null then
    Exit;
  if TV1.DataController.Summary.FooterSummaryValues[1] = null then
    Exit;
  f1 := TV1.DataController.Summary.FooterSummaryValues[5];
  f2 := TV1.DataController.Summary.FooterSummaryValues[1];

//  Label6.Caption := FloatToStr((f1 / f2 * 100, 2));
  Label6.Caption := '总打掉比例为:' + format('%.2f', [f1 / f2 * 100]) + '%';
//  Label6.Caption := FormatFloat('.2%F', [f1 / f2 * 100]);

end;

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

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

procedure TfrmWJTJList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('外检数据统计', Tv1, '坯布仓库');
  Close;
end;

procedure TfrmWJTJList.FormShow(Sender: TObject);
begin
  ReadCxGrid('外检数据统计', Tv1, '坯布仓库');
  if Trim(canshu2) = '查看' then
  begin
    TBAdd.Visible := False;
    TBDel.Visible := False;
    TBEdit.Visible := False;
  end
  else
  begin
    TBAdd.Visible := True;
    TBDel.Visible := True;
    TBEdit.Visible := True;
  end;
  //InitGrid();
end;

procedure TfrmWJTJList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  TcxGridToExcel('外检数据统计', cxGrid2);
end;

procedure TfrmWJTJList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  end;
end;

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

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

procedure TfrmWJTJList.TBDelClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;

  if Trim(CDS_Main.fieldbyname('SPID').AsString) <> '' then
  begin

    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    try
      ADOQueryCmd.Connection.BeginTrans;

      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add(' delete wjsj where SPID=''' + Trim(CDS_Main.fieldbyname('SPID').AsString) + '''');

        ExecSQL;
      end;
      ADOQueryCmd.Connection.CommitTrans;
      CDS_Main.Delete;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('删除异常!', '提示', 0);
    end;

  end
  else
  begin
    CDS_Main.Delete;
  end;
end;

procedure TfrmWJTJList.TBAddClick(Sender: TObject);
begin
  try
    frmWJSJInPut := TfrmWJSJInPut.Create(Application);
    with frmWJSJInPut do
    begin
      FBCId := '';
      frmWJSJInPut.canshu3 := Trim(Self.canshu3);
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmWJSJInPut.Free;
  end;
end;

procedure TfrmWJTJList.TBEditClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;

  try
    frmWJSJInPut := TfrmWJSJInPut.Create(Application);
    with frmWJSJInPut do
    begin
      FBCId := Trim(CDS_Main.fieldbyname('SPID').AsString);
      TBDel.Visible := False;
      TBAdd.Visible := False;
      ToolButton1.Visible := False;
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmWJSJInPut.Free;
  end;
end;

procedure TfrmWJTJList.SPIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    {if Length(Trim(SPID.Text))<4 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add(' select A.* ');
        sql.Add(',ConNo=(select ConNo from Contract_Main CM where CM.MainId=A.CGMainId)');
        //sql.Add(' ,ConNo=(select ConNo from Contract_Main CM where CM.MainId=A.CGMainId )');
        sql.Add(' from CK_SXPB_CR A');
        sql.add(' where SPID like :SXID');
        Parameters.ParamByName('SXID').Value:='%'+Trim(SXID.Text)+'%';
        Open;
        //ShowMessage(SQL.Text);
      end;
      SCreateCDS20(ADOQueryMain,CDS_Main);
      SInitCDSData20(ADOQueryMain,CDS_Main);
    finally
      ADOQueryMain.EnableControls;
    end;  }
  end;
end;

procedure TfrmWJTJList.SPSpecChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmWJTJList.FactoryNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

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

procedure TfrmWJTJList.jgtypeChange(Sender: TObject);
begin

  InitGrid();

end;

procedure TfrmWJTJList.Tv1TcxGridDBDataControllerTcxDataSummaryFooterSummaryItems6GetText(Sender: TcxDataSummaryItem; const AValue: Variant; AIsFooter: Boolean; var AText: string);
var
  xx: string;
  yy: string;
begin
  inherited;
  if AIsFooter then
  begin
    with Tv1.DataController.Summary do
    begin
      xx := vartostr(FooterSummaryValues[FooterSummaryItems.IndexOfItemLink(Tv1.GetColumnByFieldName('DDQTY'))]);
      yy := vartostr(FooterSummaryValues[FooterSummaryItems.IndexOfItemLink(Tv1.GetColumnByFieldName('SHQTY'))]);
    end;
    if strtofloatdef(yy, 0) > 0 then
      AText := format('%.2f', [strToFloatdef(xx, 0) / strtofloatdef(yy, 0) * 100]) + '%';
  end;

end;

end.

