unit U_YMTOrderSel;

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, dxSkinsCore, dxSkinBlack, dxSkinBlue, dxSkinBlueprint,
  dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom, dxSkinDarkSide,
  dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray,
  dxSkinOffice2013LightGray, dxSkinOffice2013White, dxSkinPumpkin,
  dxSkinSeven, dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus,
  dxSkinSilver, dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008,
  dxSkinTheAsphaltWorld, dxSkinsDefaultPainters, dxSkinValentine,
  dxSkinVS2010, dxSkinWhiteprint, dxSkinXmas2008Blue, dxSkinscxPCPainter;

type
  TfrmOrderSel = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    MLOrderNo: TEdit;
    CDS_Main: TClientDataSet;
    Label4: TLabel;
    KHNameHZ: TEdit;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    Label9: TLabel;
    MLConNo: TEdit;
    Label12: TLabel;
    MPRTKZ: TEdit;
    cxSplitter1: TcxSplitter;
    MLCodeName: TEdit;
    Label5: TLabel;
    N1: TMenuItem;
    Label7: TLabel;
    status: TComboBox;
    N3: TMenuItem;
    ToolButton1: TToolButton;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column2: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure MLCodeNameKeyPress(Sender: TObject; var Key: Char);
    procedure MLConNoKeyPress(Sender: TObject; var Key: Char);
    procedure MLOrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure KHNameHZChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
  private
    DQdate: TDateTime;
    procedure InitGrid();
    procedure InitForm();

    { Private declarations }
  public
    FFInt, FCloth: Integer;
    canshu1, canshu2, canshu3: string;
    fFlileFlag: string;
    FKHNo, FTT: string;
    { Public declarations }
  end;

var
  frmOrderSel: TfrmOrderSel;

implementation

uses
  U_DataLink, U_Fun;
{$R *.dfm}

procedure TfrmOrderSel.FormDestroy(Sender: TObject);
begin
  frmOrderSel := nil;
end;

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

procedure TfrmOrderSel.FormCreate(Sender: TObject);
begin
  DQdate := SGetServerDate(ADOQueryTemp);
  canshu1 := trim(DParameters1);
  canshu2 := trim(DdataBase);
end;

procedure TfrmOrderSel.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('生产指示单新YX', Tv1, '生产指示单管理');
end;

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

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

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('SELECT AA.* FROM(');
      sql.add('select A.*  ,CPType=''成品'', C.NewMLID MainId, C. NewSubid SubId,C.MLColor,C.MLColorNo,C.MLHX,C.MLPrice,C.FeeName,C.FeeMoney');
      sql.Add(',KHConNo=(select TOP 1 X.KHConNo from ML_ConMain X where X.MLConID=A.ConMainId)');
      sql.Add(',CRType=(select TOP 1 X.ConType from ML_ConMain X where X.MLConID=A.ConMainId)');
      sql.Add(',SYRName=(select TOP 1 X.SYRName from ML_ConMain X where X.MLConID=A.ConMainId)');
      sql.Add(' from ML_OrderMainNew A ');
      sql.Add(' inner join ML_OrderSubidNew C on A.NewMLID=C.NewMLID ');
      sql.Add(' left join KH_Main KM on KM.KHNO=A.KHNO ');
      sql.Add(')AA');
      sql.Add(fwhere);
     // ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmOrderSel.InitForm();
begin
  ReadCxGrid('生产指示单新YX', Tv1, '生产指示单管理');
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime;
  InitGrid();

end;

procedure TfrmOrderSel.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  SelExportData(Tv1, ADOQueryMain, '面料染色计划单');
end;

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

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

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

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

procedure TfrmOrderSel.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, false);
end;

procedure TfrmOrderSel.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, True);
end;

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

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

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

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

procedure TfrmOrderSel.ToolButton1Click(Sender: TObject);
var
  FFKHName, FFTT: string;
begin
  with CDS_Main do
  begin
    First;
    FFKHName := '';
    FFTT := '';
    while not Eof do
    begin
      if CDS_Main.fieldbyname('SSel').AsBoolean = True then
      begin

//        if (Trim(FFTT) = '') and (Trim(CDS_Main.fieldbyname('SYRName').AsString) <> '') then
//        begin
//          FFTT := Trim(CDS_Main.fieldbyname('SYRName').AsString);
//        end;
//
//        if Trim(FFKHName) = '' then
//        begin
//          FFKHName := Trim(CDS_Main.fieldbyname('KHName').AsString);
//        end;


        //同时选的生产单不能包含多个抬头
//        if (Trim(CDS_Main.fieldbyname('SYRName').AsString) <> FFTT) and (Trim(CDS_Main.fieldbyname('SYRName').AsString) <> '') then
//        begin
//          Application.MessageBox('不能选择多个抬头!', '提示', 0);
//          exit;
//        end;

        //新选的抬头不能和已经选的抬头不同
//        if (Trim(CDS_Main.fieldbyname('SYRName').AsString) <> FTT) and (FTT <> '') and (Trim(CDS_Main.fieldbyname('SYRName').AsString) <> '') then
//        begin
//          Application.MessageBox('不能选择其它抬头!', '提示', 0);
//          exit;
//        end;


        //同时选的生产单不能包含多个客户
//        if (Trim(CDS_Main.fieldbyname('KHName').AsString) <> FFKHName) then
//        begin
//          Application.MessageBox('不能选择多个客户!', '提示', 0);
//          exit;
//        end;

        //新选的生产单不能和已经选的客户不同
//        if (Trim(CDS_Main.fieldbyname('KHNo').AsString) <> FKHNo) and (FKHNo <> '') then
//        begin
//          Application.MessageBox('不能选择其它客户!', '提示', 0);
//          exit;
//        end;

      end;

      Next;
    end;
  end;

  ModalResult := 1;
end;

end.

