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, dxSkinsCore,
  dxSkinsDefaultPainters, dxSkinscxPCPainter, cxTextEdit;

type
  TfrmProductOrderListSel = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    Panel1: TPanel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    OrderNo: TEdit;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    ToolButton3: TToolButton;
    Label1: TLabel;
    P_CodeName: TEdit;
    P_Color: TEdit;
    Label2: TLabel;
    ConNo: TEdit;
    Label4: TLabel;
    Panel2: TPanel;
    Picture4: TcxDBImage;
    ADOQueryImage: TADOQuery;
    DSImage: TDataSource;
    PopupMenu2: TPopupMenu;
    N1: TMenuItem;
    MenuItem1: TMenuItem;
    cxGrid4: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn35: TcxGridDBColumn;
    cxGridDBColumn36: TcxGridDBColumn;
    cxGridDBColumn37: TcxGridDBColumn;
    Tv3Column13: TcxGridDBColumn;
    cxGridDBColumn38: TcxGridDBColumn;
    cxGridDBColumn39: TcxGridDBColumn;
    cxGridDBColumn40: TcxGridDBColumn;
    cxGridDBColumn41: TcxGridDBColumn;
    cxGridDBColumn42: TcxGridDBColumn;
    cxGridDBColumn43: TcxGridDBColumn;
    cxGridDBColumn44: TcxGridDBColumn;
    cxGridDBColumn45: TcxGridDBColumn;
    cxGridDBColumn46: TcxGridDBColumn;
    cxGridDBColumn47: TcxGridDBColumn;
    cxGridDBColumn48: TcxGridDBColumn;
    cxGridDBColumn49: TcxGridDBColumn;
    cxGridDBColumn50: TcxGridDBColumn;
    cxGridDBColumn51: TcxGridDBColumn;
    Tv3Column1: TcxGridDBColumn;
    Tv3Column2: TcxGridDBColumn;
    Tv3Column3: TcxGridDBColumn;
    Tv3Column4: TcxGridDBColumn;
    Tv3Column5: TcxGridDBColumn;
    Tv3Column12: TcxGridDBColumn;
    Tv3Column6: TcxGridDBColumn;
    Tv3Column7: TcxGridDBColumn;
    Tv3Column9: TcxGridDBColumn;
    Tv3Column8: TcxGridDBColumn;
    Tv3Column10: TcxGridDBColumn;
    Tv3Column11: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    Label5: TLabel;
    KHName: TEdit;
    Tv3Column14: TcxGridDBColumn;
    cxStyleRepository2: TcxStyleRepository;
    cxStyle2: TcxStyle;
    Label6: TLabel;
    Label7: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    ToolButton1: TToolButton;
    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 ToolButton1Click(Sender: TObject);
  private
    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

  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime - 120;
  cxgrid4.Align := alClient;
end;

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

procedure TfrmProductOrderListSel.InitGrid();
var
  fwhere, Pwhere: string;
begin
  Pwhere := SGetFilters(Panel1, 1, 2);

  begin
    if trim(Pwhere) <> '' then
      fwhere := fwhere + ' and ' + trim(Pwhere);
  end;

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;

      sql.Add('select A.*,B.*  ');
      SQL.Add(',JSWSNote=CAST((SELECT C.PYXH +''.''+C.Y_Name+'';'' FROM PD_Plan_Yarn C WHERE C.MainId=A.MainId FOR xml path(''''))as VARCHAR(3000))');
      SQL.Add(',RKPS=ISNULL((SELECT COUNT(*) FROM JY_CLOTH H WHERE H.MAINID=A.MAINID),0)+ISNULL((SELECT SUM(PIQTY) FROM CK_SXPB_CR J where CRFlag=''入库'' AND J.ORDMainIdRK=A.MAINID),0)');
      sql.Add(' from PD_Plan_Main A inner join PD_Plan_Sub B on A.Mainid=B.MainId');

      SQL.Add('where  A.VALID=''Y''');
      SQL.ADD('AND ORDTYPE=''外加工''');
      sql.add(' and  A.filltime>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)) + '''');
      sql.Add(' and A.filltime<=''' + Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)) + '''');

      SQL.Add('AND isnull(ConNoStatu,'''')<>''已取消''');
      SQL.Add(fwhere);
//    isnull(B.status,''0'')=''1''

      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmProductOrderListSel.InitForm();
begin
  ReadCxGrid('指示单列表选择坯布仓库', Tv3, '生产指示单管理');
  InitGrid();

//  try
//    ADOQueryMain.DisableControls;
//    with ADOQueryMain do
//    begin
//      Filtered := False;
//      Close;
//      sql.Clear;
//
//      sql.Add('select A.*,B.*  ');
//      SQL.Add(',JSWSNote=CAST((SELECT C.PYXH +''.''+C.Y_Name+'';'' FROM PD_Plan_Yarn C WHERE C.MainId=A.MainId FOR xml path(''''))as VARCHAR(3000))');
//      sql.Add(' from PD_Plan_Main A inner join PD_Plan_Sub B on A.Mainid=B.MainId');
//      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('AND A.VALID=''Y''');
//      SQL.ADD('AND ORDTYPE=''外发''');
//      sQL.Add('and  a.filltime>getdate()-90 ');
//      SQL.Add(fwhere);
////    isnull(B.status,''0'')=''1''
//
//      Open;
//    end;
//    SCreateCDS20(ADOQueryMain, Order_Main);
//    SInitCDSData20(ADOQueryMain, Order_Main);
//  finally
//    ADOQueryMain.EnableControls;
//  end;
end;

procedure TfrmProductOrderListSel.FormShow(Sender: TObject);
begin
  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.ToolButton1Click(Sender: TObject);
begin
  initgrid();
end;

end.

