unit U_ProductOrderListSel;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxControls, cxGridCustomView, cxGrid, cxMemo,
  cxRichEdit, ComCtrls, cxContainer, cxTextEdit, cxMaskEdit, cxButtonEdit,
  StdCtrls, ToolWin, DBClient, ADODB, ExtCtrls, BtnEdit, cxCalendar, StrUtils,
  cxCurrencyEdit, cxImage, cxDBEdit, Menus, RM_Common, RM_Class, RM_e_Xls,
  RM_Dataset, RM_System, RM_GridReport, cxGridCustomPopupMenu, cxGridPopupMenu,
  cxPC, cxSplitter, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdFTP, ShellAPI, cxCheckBox, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator,math;

type
  TfrmProductOrderListSel = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    OrderNoM: TEdit;
    Order_Main: TClientDataSet;
    Label4: TLabel;
    CustomerNoName: TEdit;
    Label8: TLabel;
    MPRTCodeName: TEdit;
    Label9: TLabel;
    ConNo: TEdit;
    Label10: TLabel;
    MPRTSpec: TEdit;
    Label11: TLabel;
    MPRTCode: TEdit;
    Label12: TLabel;
    MPRTKZ: TEdit;
    btn1: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cv1Column2: TcxGridDBColumn;
    v1OrdDate: TcxGridDBColumn;
    v1OrderNo: TcxGridDBColumn;
    v1CustomerNoName: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    v1ConNo: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1MPRTCodeName: TcxGridDBColumn;
    v1MPRTSpec: TcxGridDBColumn;
    v1MPRTMF: TcxGridDBColumn;
    v1MPRTKZ: TcxGridDBColumn;
    cv1Column3: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column2: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    v1Column12: TcxGridDBColumn;
    Panel2: TPanel;
    Panel6: TPanel;
    Label7: TLabel;
    LBCPAP: TLabel;
    BTLP: TButton;
    BTNP: TButton;
    TCBNOR: TComboBox;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure OrderNoMChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure btn1Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure BTNPClick(Sender: TObject);
    procedure BTLPClick(Sender: TObject);
    procedure TCBNORChange(Sender: TObject);
  private
    CurrentPage, RecordsNumber: Integer;
    DQdate: TDateTime;
    procedure InitGrid();
    procedure InitForm();

    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmProductOrderListSel: TfrmProductOrderListSel;

implementation

uses
  U_DataLink, U_Fun;

{$R *.dfm}

procedure TfrmProductOrderListSel.FormDestroy(Sender: TObject);
begin
  frmProductOrderListSel := nil;
end;

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

procedure TfrmProductOrderListSel.FormCreate(Sender: TObject);
begin
  cxgrid1.Align := alClient;
  DQdate := SGetServerDate(ADOQueryTemp);

end;

procedure TfrmProductOrderListSel.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('指示单列表选择ZY', Tv1, '生产指示单管理');
end;

procedure TfrmProductOrderListSel.InitGrid();
var
  strwhere, fwhere, pwhere: string;
begin
  strwhere := '';

  try
    fwhere := ' where OrdDate>=' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.DateTime));
    fwhere := fwhere + ' and OrdDate<' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1));

    pwhere := SGetFilters(Panel1, 1, 2);
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
//      sql.Add('Select AA.*,KCY=Cast(KCM*1.0936 as decimal(18,1)) from(');
//      sql.add('select b.prtcolor,B.SUBID  ,B.PRTHX,B.OrderUnit,B.SLBName    ');
//      sql.add(',OrderQty=(select Sum(PRTOrderQty) from JYOrder_Sub B where B.MainId=A.MainId group by B.MainId)');
//      sql.add(',ZQty=(select Sum(PRTOrderQty) from JYOrder_Sub B where B.MainId=A.MainId group by B.MainId)');
////      sql.add(',OrderUnit=(select Top 1 OrderUnit  from JYOrder_Sub B where B.MainId=A.MainId)');
////      sql.add(', PriceUnit=(select Top 1 PriceUnit  from JYOrder_Sub B where B.MainId=A.MainId)');
////      sql.add(',PRTPrice=(select Top 1 PRTPrice  from JYOrder_Sub B where B.MainId=A.MainId)');
////      sql.add(',SLBName=(select Top 1 SLBName  from JYOrder_Sub B where B.MainId=A.MainId)');
//      sql.add(',A.*,A.OrderNo OrderNoM, isnull(b.prtorderqty,0)prtorderqty,isnull(b.prtps,0)prtps');
//      sql.Add(',KCKg=(select sum(KgQty) from CK_BanCP_CR CR where CR.subId=B.Subid and isnull(CR.CRFlag,'''')=''待出库'' and CPtype=''正品'') ');
//      sql.Add(',KCM=(select sum(Qty) from CK_BanCP_CR CR where CR.subId=B.Subid and isnull(CR.CRFlag,'''')=''待出库'' and CPtype=''正品'') ');
//      sql.add('from JYOrder_Main A inner join JYOrder_sub B  on a.mainid=b.mainid    ');
//      sql.add('where A.OrdDate>=' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)));
//      sql.add('and  A.OrdDate<' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)));
//      if Trim(CustomerNoName.Text) <> '' then
//      begin
//        sql.Add(' and A.CustomerNoName=''' + Trim(CustomerNoName.Text) + '''');
//      end;
//      Sql.Add(') AA order by KCM desc');
//      ShowMessage(SQL.Text);

//      SQL.Add('exec P_View_FHSQZSDXZ @BegDate=' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)));
//      sql.Add(',@EndDate=' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)));

      if trim(pwhere) <> '' then
        fwhere := fwhere + ' and ' + trim(pwhere);
      sql.Add('exec P_View_FHSQZSDXZ2 ');
      sql.Add('@pageIndex=' + inttostr(CurrentPage));
      sql.Add(',@pageSize=' + inttostr(RecordsNumber));
      sql.Add(',@criteria= ' + quotedstr(fwhere));
//      ShowMessage(sql.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
    LBCPAP.Caption := inttostr(CurrentPage) + '/' + inttostr(ceil(Order_Main.FieldByName('TotalCount').AsInteger / RecordsNumber));
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmProductOrderListSel.InitForm();
begin
  ReadCxGrid('指示单列表选择ZY', Tv1, '生产指示单管理');
  BegDate.DateTime := SGetServerDate10(ADOQueryTemp) - 90;
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);

end;

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

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

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

procedure TfrmProductOrderListSel.FormShow(Sender: TObject);
begin
  CurrentPage := 1;
  RecordsNumber := 500;
  InitForm();
  TBRafresh.Click;
end;

procedure TfrmProductOrderListSel.CheckBox1Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmProductOrderListSel.CheckBox2Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

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

procedure TfrmProductOrderListSel.btn1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  if Order_Main.Locate('SSel', True, []) = False then
    Exit;
  ModalResult := 1;
end;

procedure TfrmProductOrderListSel.N1Click(Sender: TObject);
begin
  SelOKNo(Order_Main, True);
end;

procedure TfrmProductOrderListSel.N2Click(Sender: TObject);
begin
  SelOKNo(Order_Main, False);
end;

procedure TfrmProductOrderListSel.BTNPClick(Sender: TObject);
begin
  if CurrentPage < Order_Main.FieldByName('TotalCount').AsInteger / RecordsNumber then
    CurrentPage := CurrentPage + 1;
  InitGrid();
end;

procedure TfrmProductOrderListSel.BTLPClick(Sender: TObject);
begin
  if CurrentPage > 1 then
    CurrentPage := CurrentPage - 1;
  InitGrid();
end;

procedure TfrmProductOrderListSel.TCBNORChange(Sender: TObject);
begin
  RecordsNumber := StrToInt(TCBNOR.Text);
  CurrentPage := 1;
  InitGrid();
end;

end.

