unit U_CaiGouListSel;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxGrid, DBClient, cxCalendar, cxButtonEdit, cxSplitter, RM_Common, RM_Class,
  RM_e_Xls, RM_Dataset, RM_System, RM_GridReport, cxTextEdit, cxPC,
  cxDropDownEdit, cxCheckBox;

type
  TfrmCaiGouListSel = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Order_Main: TClientDataSet;
    Panel1: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    TBFind: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1SSel: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Label4: TLabel;
    Label5: TLabel;
    Label3: TLabel;
    Label6: TLabel;
    SPName: TEdit;
    SQPerson: TEdit;
    PinPai: TEdit;
    FactoryName: TEdit;
    Label7: TLabel;
    ConNo: TEdit;
    v1Column19: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    RM1: TRMGridReport;
    RMDBPRT: TRMDBDataSet;
    CDS_PRT: TClientDataSet;
    v1Column21: TcxGridDBColumn;
    v1Column22: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    v1Column24: TcxGridDBColumn;
    v1Column25: TcxGridDBColumn;
    Label8: TLabel;
    DataType: TComboBox;
    v1Column26: TcxGridDBColumn;
    v1Column27: TcxGridDBColumn;
    v1Column28: TcxGridDBColumn;
    v1Column29: TcxGridDBColumn;
    v1Column30: TcxGridDBColumn;
    v1Column31: TcxGridDBColumn;
    v1Column32: TcxGridDBColumn;
    ToolButton1: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure cxPageControl1Change(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure DataTypeChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
  private
    canshu1, canshu2: string;
    procedure InitGrid();
    procedure InitForm();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmCaiGouListSel: TfrmCaiGouListSel;

implementation

uses
  U_DataLink, U_RTFun, U_CaiGouSQInPut, U_ModuleNote;

{$R *.dfm}

procedure TfrmCaiGouListSel.FormDestroy(Sender: TObject);
begin
  frmCaiGouListSel := nil;
end;

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

procedure TfrmCaiGouListSel.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('采购申请审批列表', Tv1, '采购申请管理');
end;

procedure TfrmCaiGouListSel.InitGrid();
begin
  BegDate.SetFocus;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select A.* ');
      sql.Add(' ,SjSX=(case when isnull(MXPBID,'''')<>'''' then ''单纱'' else ''上机原料'' end )');
      sql.Add(',Case when (isnull(SQQtyUp,0)+isnull(SQQty,0))<>0 then Cast(SQQty*(1+SQQtyUp*0.01) as decimal(18,2)) end as MAXJHQty');
      sql.Add(',Case when isnull(DataType,'''')<>'''' then ''(''+DataType+'')'' else '''' end as DataTypePrt ');
      sql.Add(',DHBL=Cast( case when (SQQty*(1+isnull(SQQtyUp,0)*0.01))>0  then (case when  SQQty*(1+isnull(SQQtyUp,0)*0.01)>SQQty then DHQty/SQQty else DHQty/(SQQty*(1+isnull(SQQtyUp,0)*0.01)) end) else 1 end as decimal(18,2))');

      SQL.Add(' from SQ_CG A where isnull(SChkStatus,'''')<>'''' and SQType=''纱线'' ');
      sql.Add(' and isnull(SChkStatus,'''')=''已送审'' ');
      sql.Add(' order by SQDate');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCaiGouListSel.InitForm();
begin
  EndDate.Date := SGetServerDate(ADOQueryTemp);
  BegDate.Date := EndDate.Date - 7;
  ReadCxGrid('采购申请审批列表', Tv1, '采购申请管理');
  InitGrid();
end;

procedure TfrmCaiGouListSel.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmCaiGouListSel.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmCaiGouListSel.cxPageControl1Change(Sender: TObject);
begin
  InitGrid();
end;

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

procedure TfrmCaiGouListSel.TBFindClick(Sender: TObject);
begin
  BegDate.SetFocus;
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQueryMain, Order_Main);
  SInitCDSData20(ADOQueryMain, Order_Main);
end;

procedure TfrmCaiGouListSel.DataTypeChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCaiGouListSel.ToolButton1Click(Sender: TObject);
begin
  if order_Main.IsEmpty then
    Exit;
  if order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  ModalResult := 1;
end;

end.

