unit U_ProductOrderListSel;

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, cxCheckBox, cxCalendar, cxSplitter, RM_Dataset, RM_System,
  RM_Common, RM_Class, RM_GridReport, RM_e_Xls, Menus, cxContainer, cxImage,
  cxDBEdit, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, Math;

type
  TfrmProductOrderListSel = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    Panel1: TPanel;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    OrderNo: TEdit;
    v1OrderNo: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    v1ConNo: TcxGridDBColumn;
    v1MPRTSpec: TcxGridDBColumn;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    v1MPRTCodeName: TcxGridDBColumn;
    v1MPRTMF: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    v1MPRTKZ: TcxGridDBColumn;
    v1OrdDefStr1: TcxGridDBColumn;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    ToolButton3: TToolButton;
    v1Column3: TcxGridDBColumn;
    v1KHConNO: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    Label1: TLabel;
    MPRTCodeName: TEdit;
    Panel2: TPanel;
    Picture4: TcxDBImage;
    ADOQueryImage: TADOQuery;
    DSImage: TDataSource;
    v1Column4: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    PopupMenu2: TPopupMenu;
    N1: TMenuItem;
    MenuItem1: TMenuItem;
    v1Column10: TcxGridDBColumn;
    Label7: TLabel;
    MPRTCode: TEdit;
    ToolButton1: TToolButton;
    Panel12: TPanel;
    Label30: TLabel;
    LBCPAP: TLabel;
    Label31: TLabel;
    BTLP: TButton;
    BTNP: TButton;
    TCBNOR: TEdit;
    Label6: TLabel;
    CustomerNoName: TEdit;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label8: TLabel;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure OrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure Tv1DblClick(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure N1Click(Sender: TObject);
    procedure MenuItem1Click(Sender: TObject);
    procedure TCBNORKeyPress(Sender: TObject; var Key: Char);
    procedure BTLPClick(Sender: TObject);
    procedure BTNPClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
  private
    CurrentPage, RecordsNumber: Integer;
    DQdate: TDateTime;
    procedure InitGrid();
    procedure InitForm();
    procedure InitImage(fsubID: string);
    { Private declarations }
  public
    FFInt, FCloth: Integer;

    { Public declarations }
  end;

var
  frmProductOrderListSel: TfrmProductOrderListSel;

implementation

uses
  U_DataLink, U_RTFun;

{$R *.dfm}
procedure TfrmProductOrderListSel.InitImage(fsubID: string);
begin
  ADOQueryImage.close;
 // IF fwbid='' then exit;
  with ADOQueryImage do
  begin
    close;
    sql.Clear;
    sql.Add('select * from TP_File A');
    sql.Add('inner join JYOrder_Sub B on B.HXFile=A.WBID');
    sql.Add('where B.SubID=' + quotedstr(trim(fsubID)));
    open;
  end;
end;

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;
end;

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

procedure TfrmProductOrderListSel.InitGrid();
var
  fwhere, Pwhere: string;
begin
  Pwhere := '';
  if Trim(OrderNo.Text) <> '' then
  begin
    Pwhere := Pwhere + ' and (OrderNo like ' + quotedstr('%' + Trim(OrderNo.Text) + '%') + ' or ZOrderNo like ' + quotedstr('%' + Trim(OrderNo.Text) + '%') + ') ';

  end;
  if SGetFilters(Panel1, 1, 2) <> '' then
    Pwhere := Pwhere + 'and ' + SGetFilters(Panel1, 1, 2);
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select top ' + inttostr(RecordsNumber) + ' temp_row.*     ');
//      SQL.add(',WRKPiQty=(select sum(PiQty) from CK_SXPB_CR x where CKName=''外加工'' and x.crflag=''入库'' and x.ORDMainIdRK=temp_row.Mainid and x.ORDSubIdRK=temp_row.Subid )');
//      SQL.add(',WRKQty=(select sum(Qty) from CK_SXPB_CR x where CKName=''外加工'' and x.crflag=''入库'' and x.ORDMainIdRK=temp_row.Mainid and x.ORDSubIdRK=temp_row.Subid )');
//      SQL.add(',WCKPiQty=(select sum(PiQty) from CK_SXPB_CR x where CKName=''外加工'' and x.crflag=''出库'' and x.ORDMainIdRK=temp_row.Mainid and x.ORDSubIdRK=temp_row.Subid )');
//      SQL.add(',WCKQty=(select sum(Qty) from CK_SXPB_CR x where CKName=''外加工'' and x.crflag=''出库'' and x.ORDMainIdRK=temp_row.Mainid and x.ORDSubIdRK=temp_row.Subid )');
//      SQL.add(',RKPiQty=(select sum(PiQty) from CK_SXPB_CR x where CKName=''待检布'' and x.crflag=''入库'' and x.ORDMainIdRK=temp_row.Mainid and x.ORDSubIdRK=temp_row.Subid and x.SPColor=temp_row.PRTColor and isnull(x.SPHX,'''')=isnull(temp_row.PRTHX,''''))');
//      SQL.add(',RKQty=(select sum(Qty) from CK_SXPB_CR x where CKName=''待检布'' and x.crflag=''入库'' and x.ORDMainIdRK=temp_row.Mainid and x.ORDSubIdRK=temp_row.Subid and x.SPColor=temp_row.PRTColor and isnull(x.SPHX,'''')=isnull(temp_row.PRTHX,''''))');
      sql.Add(' from ( select   rownumber=CAST((row_number() over(order by  AA.FILLTIME DESC  ) ) AS INT)  '); //--序号
      sql.Add(',COUNT(1) OVER() AS TotalCount '); //总记录数
      sql.Add(',* from (  ');
      sql.Add(' select  YWY,A.OrderNo,A.ConNo,A.CustomerNo,A.ZOrderNo,A.CustomerNoName,A.DlyDate,A.FILLTIME,A.OrdDate,A.MPRTCode,A.MPRTCodeName,A.MPRTSpec,A.MPRTCF,A.MPRTMF,A.MPRTKZ   ');
      SQL.Add(' FROM JYOrder_Main A');
//      sql.Add('inner join  JYOrder_sub B on B.mainID=A.mainID ');
      sql.Add(' where isnull(A.status,''0'')=''1'' ');
      sql.Add('and A.OrdDate>=''' + formatdateTime('yyyy-MM-dd', begdate.Date) + ''' ');
      sql.Add('and A.OrdDate<''' + formatdateTime('yyyy-MM-dd', enddate.Date + 1) + ''' ');
      sql.Add(' )  AA where 1=1 ' + Pwhere);
      sql.Add(') temp_row');
      sql.Add(' where rownumber> ' + inttostr((CurrentPage - 1) * RecordsNumber));
      sql.Add('order by rownumber');
//      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));
    Label31.Caption := '记录条数:' + Order_Main.FieldByName('TotalCount').AsString;
  finally
    ADOQueryMain.EnableControls;
  end;
//  try
//    ADOQueryMain.DisableControls;
//    with ADOQueryMain do
//    begin
//      Filtered := False;
//      Close;
//      sql.Clear;
//      sql.Add('select B.*,A.MainId,A.OrderNo,A.ConNo,A.CustomerNo,A.CustomerNoName,A.DlyDate,A.OrdDate,A.MPRTCode,A.MPRTCodeName,A.MPRTSpec,A.MPRTCF,A.MPRTMF,A.MPRTKZ  ');
//      SQL.add(',RKPiQty=(select sum(PiQty) from CK_SXPB_CR x where CKName=''待检布'' and x.crflag=''入库'' and x.ORDMainIdRK=A.Mainid and x.ORDSubIdRK=B.Subid and x.SPColor=B.PRTColor and isnull(x.SPHX,'''')=isnull(B.PRTHX,''''))');
//      SQL.add(',RKQty=(select sum(Qty) from CK_SXPB_CR x where CKName=''待检布'' and x.crflag=''入库'' and x.ORDMainIdRK=A.Mainid and x.ORDSubIdRK=B.Subid and x.SPColor=B.PRTColor and isnull(x.SPHX,'''')=isnull(B.PRTHX,''''))');
//      sql.add('from JYOrder_sub B');
//      sql.Add('inner join  JYOrder_Main A on B.mainID=A.mainID ');
//      sql.Add('where isnull(A.status,''0'')=''1'' ');
//      if Trim(Pwhere) <> '' then
//        sql.Add(Pwhere);
//
//      Open;
//    end;
//    SCreateCDS20(ADOQueryMain, Order_Main);
//    SInitCDSData20(ADOQueryMain, Order_Main);
//  finally
//    ADOQueryMain.EnableControls;
//  end;
end;

procedure TfrmProductOrderListSel.InitForm();
begin
  ReadCxGrid('指示单列表选择坯布仓库', Tv1, '生产指示单管理');
  BegDate.DateTime := SGetServerDate10(ADOQueryTemp) - 180;
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  InitGrid();

//  try
//    ADOQueryMain.DisableControls;
//    with ADOQueryMain do
//    begin
//      Filtered := False;
//      Close;
//      sql.Clear;
//      sql.Add('select B.*,A.* ');
//      SQL.add(',RKPiQty=(select sum(PiQty) from CK_SXPB_CR x where CKName=''待检布'' and x.crflag=''入库'' and x.ORDMainIdRK=A.Mainid and x.ORDSubIdRK=A.Subid and x.SPColor=A.PRTColor and isnull(x.SPHX,'''')=isnull(A.PRTHX,''''))');
//      SQL.add(',RKQty=(select sum(Qty) from CK_SXPB_CR x where CKName=''待检布'' and x.crflag=''入库'' and x.ORDMainIdRK=A.Mainid and x.ORDSubIdRK=A.Subid and x.SPColor=A.PRTColor and isnull(x.SPHX,'''')=isnull(A.PRTHX,''''))');
//      SQL.add(',WRKPiQty=(select sum(PiQty) from CK_SXPB_CR x where CKName=''外加工'' and x.crflag=''入库'' and x.ORDMainIdRK=A.Mainid and x.ORDSubIdRK=A.Subid and x.SPColor=A.PRTColor and isnull(x.SPHX,'''')=isnull(A.PRTHX,''''))');
//      SQL.add(',WRKQty=(select sum(Qty) from CK_SXPB_CR x where CKName=''外加工'' and x.crflag=''入库'' and x.ORDMainIdRK=A.Mainid and x.ORDSubIdRK=A.Subid and x.SPColor=A.PRTColor and isnull(x.SPHX,'''')=isnull(A.PRTHX,''''))');
//
//      sql.add('from JYOrder_sub A');
//      sql.Add('inner join  JYOrder_Main B on B.mainID=A.mainID ');
//      sQL.Add('where isnull(B.status,''0'')=''1'' and B.filltime>getdate()-10 ');
//
//      Open;
//    end;
//    SCreateCDS20(ADOQueryMain, Order_Main);
//    SInitCDSData20(ADOQueryMain, Order_Main);
//  finally
//    ADOQueryMain.EnableControls;
//  end;
end;

procedure TfrmProductOrderListSel.FormShow(Sender: TObject);
begin
  CurrentPage := 1;
  RecordsNumber := 100;
  TCBNOR.Text := '300';
  InitForm();

end;

procedure TfrmProductOrderListSel.ToolButton3Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  ModalResult := 1;
end;

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

procedure TfrmProductOrderListSel.Tv1DblClick(Sender: TObject);
begin
  ToolButton3.Click;
end;

procedure TfrmProductOrderListSel.Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  ADOQueryImage.Close;
  if Order_Main.IsEmpty then
    exit;
  InitImage(Order_Main.fieldbyname('subID').AsString);
end;

procedure TfrmProductOrderListSel.N1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      Edit;
      FieldByName('SSel').Value := True;
      Post;
      Next;
    end;
  end;
  Order_Main.EnableControls;
end;

procedure TfrmProductOrderListSel.MenuItem1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      Edit;
      FieldByName('SSel').Value := false;
      Post;
      Next;
    end;
  end;
  Order_Main.EnableControls;
end;

procedure TfrmProductOrderListSel.TCBNORKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    RecordsNumber := StrToIntDef(TCBNOR.Text, 10);
    CurrentPage := 1;
    InitGrid();
  end;
end;

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

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

procedure TfrmProductOrderListSel.ToolButton1Click(Sender: TObject);
begin
  CurrentPage := 1;
  InitGrid();
end;

end.

